blob: 343eb10230a199c4db63e5c7ff589a0e219722d5 [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 Mason74493f72007-12-11 09:25:06 -050028#include "hash.h"
Miao Xie995946d2014-04-02 19:51:06 +080029#include "tree-log.h"
Chris Masonfec577f2007-02-26 10:40:21 -050030#include "disk-io.h"
31#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040032#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050033#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040034#include "locking.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040035#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060036#include "math.h"
Jeff Mahoney6ab0a202013-11-01 13:07:04 -040037#include "sysfs.h"
Josef Bacikfcebe452014-05-13 17:30:47 -070038#include "qgroup.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,
Josef Bacikfcebe452014-05-13 17:30:47 -070084 struct btrfs_delayed_extent_op *extra_op,
85 int no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040086static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
87 struct extent_buffer *leaf,
88 struct btrfs_extent_item *ei);
89static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
90 struct btrfs_root *root,
91 u64 parent, u64 root_objectid,
92 u64 flags, u64 owner, u64 offset,
93 struct btrfs_key *ins, int ref_mod);
94static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 u64 parent, u64 root_objectid,
97 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -070098 int level, struct btrfs_key *ins,
99 int no_quota);
Josef Bacik6a632092009-02-20 11:00:09 -0500100static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -0400101 struct btrfs_root *extent_root, u64 flags,
102 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400103static int find_next_key(struct btrfs_path *path, int level,
104 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400105static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
108 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500109static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
110 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000111int btrfs_pin_extent(struct btrfs_root *root,
112 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500113
Josef Bacik817d52f2009-07-13 21:29:25 -0400114static noinline int
115block_group_cache_done(struct btrfs_block_group_cache *cache)
116{
117 smp_mb();
Josef Bacik36cce922013-08-05 11:15:21 -0400118 return cache->cached == BTRFS_CACHE_FINISHED ||
119 cache->cached == BTRFS_CACHE_ERROR;
Josef Bacik817d52f2009-07-13 21:29:25 -0400120}
121
Josef Bacik0f9dd462008-09-23 13:14:11 -0400122static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
123{
124 return (cache->flags & bits) == bits;
125}
126
David Sterba62a45b62011-04-20 15:52:26 +0200127static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000128{
129 atomic_inc(&cache->count);
130}
131
132void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
133{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400134 if (atomic_dec_and_test(&cache->count)) {
135 WARN_ON(cache->pinned > 0);
136 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb2011-03-29 13:46:06 +0800137 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000138 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400139 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000140}
141
Josef Bacik0f9dd462008-09-23 13:14:11 -0400142/*
143 * this adds the block group to the fs_info rb tree for the block group
144 * cache
145 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500146static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400147 struct btrfs_block_group_cache *block_group)
148{
149 struct rb_node **p;
150 struct rb_node *parent = NULL;
151 struct btrfs_block_group_cache *cache;
152
153 spin_lock(&info->block_group_cache_lock);
154 p = &info->block_group_cache_tree.rb_node;
155
156 while (*p) {
157 parent = *p;
158 cache = rb_entry(parent, struct btrfs_block_group_cache,
159 cache_node);
160 if (block_group->key.objectid < cache->key.objectid) {
161 p = &(*p)->rb_left;
162 } else if (block_group->key.objectid > cache->key.objectid) {
163 p = &(*p)->rb_right;
164 } else {
165 spin_unlock(&info->block_group_cache_lock);
166 return -EEXIST;
167 }
168 }
169
170 rb_link_node(&block_group->cache_node, parent, p);
171 rb_insert_color(&block_group->cache_node,
172 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000173
174 if (info->first_logical_byte > block_group->key.objectid)
175 info->first_logical_byte = block_group->key.objectid;
176
Josef Bacik0f9dd462008-09-23 13:14:11 -0400177 spin_unlock(&info->block_group_cache_lock);
178
179 return 0;
180}
181
182/*
183 * This will return the block group at or after bytenr if contains is 0, else
184 * it will return the block group that contains the bytenr
185 */
186static struct btrfs_block_group_cache *
187block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
188 int contains)
189{
190 struct btrfs_block_group_cache *cache, *ret = NULL;
191 struct rb_node *n;
192 u64 end, start;
193
194 spin_lock(&info->block_group_cache_lock);
195 n = info->block_group_cache_tree.rb_node;
196
197 while (n) {
198 cache = rb_entry(n, struct btrfs_block_group_cache,
199 cache_node);
200 end = cache->key.objectid + cache->key.offset - 1;
201 start = cache->key.objectid;
202
203 if (bytenr < start) {
204 if (!contains && (!ret || start < ret->key.objectid))
205 ret = cache;
206 n = n->rb_left;
207 } else if (bytenr > start) {
208 if (contains && bytenr <= end) {
209 ret = cache;
210 break;
211 }
212 n = n->rb_right;
213 } else {
214 ret = cache;
215 break;
216 }
217 }
Liu Boa1897fd2012-12-27 09:01:23 +0000218 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000219 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000220 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
221 info->first_logical_byte = ret->key.objectid;
222 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400223 spin_unlock(&info->block_group_cache_lock);
224
225 return ret;
226}
227
Yan Zheng11833d62009-09-11 16:11:19 -0400228static int add_excluded_extent(struct btrfs_root *root,
229 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400230{
Yan Zheng11833d62009-09-11 16:11:19 -0400231 u64 end = start + num_bytes - 1;
232 set_extent_bits(&root->fs_info->freed_extents[0],
233 start, end, EXTENT_UPTODATE, GFP_NOFS);
234 set_extent_bits(&root->fs_info->freed_extents[1],
235 start, end, EXTENT_UPTODATE, GFP_NOFS);
236 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400237}
238
Yan Zheng11833d62009-09-11 16:11:19 -0400239static void free_excluded_extents(struct btrfs_root *root,
240 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400241{
Yan Zheng11833d62009-09-11 16:11:19 -0400242 u64 start, end;
243
244 start = cache->key.objectid;
245 end = start + cache->key.offset - 1;
246
247 clear_extent_bits(&root->fs_info->freed_extents[0],
248 start, end, EXTENT_UPTODATE, GFP_NOFS);
249 clear_extent_bits(&root->fs_info->freed_extents[1],
250 start, end, EXTENT_UPTODATE, GFP_NOFS);
251}
252
253static int exclude_super_stripes(struct btrfs_root *root,
254 struct btrfs_block_group_cache *cache)
255{
Josef Bacik817d52f2009-07-13 21:29:25 -0400256 u64 bytenr;
257 u64 *logical;
258 int stripe_len;
259 int i, nr, ret;
260
Yan, Zheng06b23312009-11-26 09:31:11 +0000261 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
262 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
263 cache->bytes_super += stripe_len;
264 ret = add_excluded_extent(root, cache->key.objectid,
265 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400266 if (ret)
267 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000268 }
269
Josef Bacik817d52f2009-07-13 21:29:25 -0400270 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
271 bytenr = btrfs_sb_offset(i);
272 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
273 cache->key.objectid, bytenr,
274 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400275 if (ret)
276 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400277
Josef Bacik817d52f2009-07-13 21:29:25 -0400278 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400279 u64 start, len;
280
281 if (logical[nr] > cache->key.objectid +
282 cache->key.offset)
283 continue;
284
285 if (logical[nr] + stripe_len <= cache->key.objectid)
286 continue;
287
288 start = logical[nr];
289 if (start < cache->key.objectid) {
290 start = cache->key.objectid;
291 len = (logical[nr] + stripe_len) - start;
292 } else {
293 len = min_t(u64, stripe_len,
294 cache->key.objectid +
295 cache->key.offset - start);
296 }
297
298 cache->bytes_super += len;
299 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400300 if (ret) {
301 kfree(logical);
302 return ret;
303 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 }
Yan Zheng11833d62009-09-11 16:11:19 -0400305
Josef Bacik817d52f2009-07-13 21:29:25 -0400306 kfree(logical);
307 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400308 return 0;
309}
310
Yan Zheng11833d62009-09-11 16:11:19 -0400311static struct btrfs_caching_control *
312get_caching_control(struct btrfs_block_group_cache *cache)
313{
314 struct btrfs_caching_control *ctl;
315
316 spin_lock(&cache->lock);
317 if (cache->cached != BTRFS_CACHE_STARTED) {
318 spin_unlock(&cache->lock);
319 return NULL;
320 }
321
Josef Bacikdde5abe2010-09-16 16:17:03 -0400322 /* We're loading it the fast way, so we don't have a caching_ctl. */
323 if (!cache->caching_ctl) {
324 spin_unlock(&cache->lock);
325 return NULL;
326 }
327
Yan Zheng11833d62009-09-11 16:11:19 -0400328 ctl = cache->caching_ctl;
329 atomic_inc(&ctl->count);
330 spin_unlock(&cache->lock);
331 return ctl;
332}
333
334static void put_caching_control(struct btrfs_caching_control *ctl)
335{
336 if (atomic_dec_and_test(&ctl->count))
337 kfree(ctl);
338}
339
Josef Bacik0f9dd462008-09-23 13:14:11 -0400340/*
341 * this is only called by cache_block_group, since we could have freed extents
342 * we need to check the pinned_extents for any extents that can't be used yet
343 * since their free space will be released as soon as the transaction commits.
344 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400345static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 struct btrfs_fs_info *info, u64 start, u64 end)
347{
Josef Bacik817d52f2009-07-13 21:29:25 -0400348 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349 int ret;
350
351 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400352 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400353 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400354 EXTENT_DIRTY | EXTENT_UPTODATE,
355 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400356 if (ret)
357 break;
358
Yan, Zheng06b23312009-11-26 09:31:11 +0000359 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400360 start = extent_end + 1;
361 } else if (extent_start > start && extent_start < end) {
362 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400363 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500364 ret = btrfs_add_free_space(block_group, start,
365 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100366 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400367 start = extent_end + 1;
368 } else {
369 break;
370 }
371 }
372
373 if (start < end) {
374 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400375 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500376 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100377 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378 }
379
Josef Bacik817d52f2009-07-13 21:29:25 -0400380 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400381}
382
Qu Wenruod458b052014-02-28 10:46:19 +0800383static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400384{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400385 struct btrfs_block_group_cache *block_group;
386 struct btrfs_fs_info *fs_info;
387 struct btrfs_caching_control *caching_ctl;
388 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400389 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400390 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400391 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400392 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400393 u64 last = 0;
394 u32 nritems;
Josef Bacik36cce922013-08-05 11:15:21 -0400395 int ret = -ENOMEM;
Chris Masonf510cfe2007-10-15 16:14:48 -0400396
Josef Bacikbab39bf2011-06-30 14:42:28 -0400397 caching_ctl = container_of(work, struct btrfs_caching_control, work);
398 block_group = caching_ctl->block_group;
399 fs_info = block_group->fs_info;
400 extent_root = fs_info->extent_root;
401
Chris Masone37c9e62007-05-09 20:13:14 -0400402 path = btrfs_alloc_path();
403 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400404 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400405
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400407
Chris Mason5cd57b22008-06-25 16:01:30 -0400408 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400409 * We don't want to deadlock with somebody trying to allocate a new
410 * extent for the extent root while also trying to search the extent
411 * root to add free space. So we skip locking and search the commit
412 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400413 */
414 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400415 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400416 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400417
Yan Zhenge4404d62008-12-12 10:03:26 -0500418 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400419 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400420 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400421again:
Yan Zheng11833d62009-09-11 16:11:19 -0400422 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400423 /* need to make sure the commit_root doesn't disappear */
Josef Bacik9e351cc2014-03-13 15:42:13 -0400424 down_read(&fs_info->commit_root_sem);
Chris Mason013f1b12009-07-31 14:57:55 -0400425
Liu Bo52ee28d2013-07-11 17:51:15 +0800426next:
Yan Zheng11833d62009-09-11 16:11:19 -0400427 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400428 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400429 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500430
Yan Zheng11833d62009-09-11 16:11:19 -0400431 leaf = path->nodes[0];
432 nritems = btrfs_header_nritems(leaf);
433
Chris Masond3977122009-01-05 21:25:51 -0500434 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200435 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400436 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400437 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400438 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400439
Yan Zheng11833d62009-09-11 16:11:19 -0400440 if (path->slots[0] < nritems) {
441 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
442 } else {
443 ret = find_next_key(path, 0, &key);
444 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400445 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400446
Josef Bacikc9ea7b22013-09-19 10:02:11 -0400447 if (need_resched() ||
Josef Bacik9e351cc2014-03-13 15:42:13 -0400448 rwsem_is_contended(&fs_info->commit_root_sem)) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400449 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400450 btrfs_release_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400451 up_read(&fs_info->commit_root_sem);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400452 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400453 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400454 goto again;
455 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400456
457 ret = btrfs_next_leaf(extent_root, path);
458 if (ret < 0)
459 goto err;
460 if (ret)
461 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400462 leaf = path->nodes[0];
463 nritems = btrfs_header_nritems(leaf);
464 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400465 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400466
Liu Bo52ee28d2013-07-11 17:51:15 +0800467 if (key.objectid < last) {
468 key.objectid = last;
469 key.offset = 0;
470 key.type = BTRFS_EXTENT_ITEM_KEY;
471
472 caching_ctl->progress = last;
473 btrfs_release_path(path);
474 goto next;
475 }
476
Yan Zheng11833d62009-09-11 16:11:19 -0400477 if (key.objectid < block_group->key.objectid) {
478 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400479 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400480 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400481
Chris Masone37c9e62007-05-09 20:13:14 -0400482 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400483 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400484 break;
Yan7d7d6062007-09-14 16:15:28 -0400485
Josef Bacik3173a182013-03-07 14:22:04 -0500486 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
487 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400488 total_found += add_new_free_space(block_group,
489 fs_info, last,
490 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500491 if (key.type == BTRFS_METADATA_ITEM_KEY)
492 last = key.objectid +
493 fs_info->tree_root->leafsize;
494 else
495 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400496
Yan Zheng11833d62009-09-11 16:11:19 -0400497 if (total_found > (1024 * 1024 * 2)) {
498 total_found = 0;
499 wake_up(&caching_ctl->wait);
500 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400501 }
Chris Masone37c9e62007-05-09 20:13:14 -0400502 path->slots[0]++;
503 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400504 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400505
506 total_found += add_new_free_space(block_group, fs_info, last,
507 block_group->key.objectid +
508 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400509 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400510
511 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400512 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400513 block_group->cached = BTRFS_CACHE_FINISHED;
514 spin_unlock(&block_group->lock);
515
Chris Mason54aa1f42007-06-22 14:16:25 -0400516err:
Chris Masone37c9e62007-05-09 20:13:14 -0400517 btrfs_free_path(path);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400518 up_read(&fs_info->commit_root_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400519
Yan Zheng11833d62009-09-11 16:11:19 -0400520 free_excluded_extents(extent_root, block_group);
521
522 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400523out:
Josef Bacik36cce922013-08-05 11:15:21 -0400524 if (ret) {
525 spin_lock(&block_group->lock);
526 block_group->caching_ctl = NULL;
527 block_group->cached = BTRFS_CACHE_ERROR;
528 spin_unlock(&block_group->lock);
529 }
Yan Zheng11833d62009-09-11 16:11:19 -0400530 wake_up(&caching_ctl->wait);
531
532 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000533 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400534}
535
Josef Bacik9d66e232010-08-25 16:54:15 -0400536static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400537 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400538{
Josef Bacik291c7d22011-11-14 13:52:14 -0500539 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400540 struct btrfs_fs_info *fs_info = cache->fs_info;
541 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400542 int ret = 0;
543
Josef Bacik291c7d22011-11-14 13:52:14 -0500544 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100545 if (!caching_ctl)
546 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500547
548 INIT_LIST_HEAD(&caching_ctl->list);
549 mutex_init(&caching_ctl->mutex);
550 init_waitqueue_head(&caching_ctl->wait);
551 caching_ctl->block_group = cache;
552 caching_ctl->progress = cache->key.objectid;
553 atomic_set(&caching_ctl->count, 1);
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800554 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
Josef Bacik291c7d22011-11-14 13:52:14 -0500555
556 spin_lock(&cache->lock);
557 /*
558 * This should be a rare occasion, but this could happen I think in the
559 * case where one thread starts to load the space cache info, and then
560 * some other thread starts a transaction commit which tries to do an
561 * allocation while the other thread is still loading the space cache
562 * info. The previous loop should have kept us from choosing this block
563 * group, but if we've moved to the state where we will wait on caching
564 * block groups we need to first check if we're doing a fast load here,
565 * so we can wait for it to finish, otherwise we could end up allocating
566 * from a block group who's cache gets evicted for one reason or
567 * another.
568 */
569 while (cache->cached == BTRFS_CACHE_FAST) {
570 struct btrfs_caching_control *ctl;
571
572 ctl = cache->caching_ctl;
573 atomic_inc(&ctl->count);
574 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
575 spin_unlock(&cache->lock);
576
577 schedule();
578
579 finish_wait(&ctl->wait, &wait);
580 put_caching_control(ctl);
581 spin_lock(&cache->lock);
582 }
583
584 if (cache->cached != BTRFS_CACHE_NO) {
585 spin_unlock(&cache->lock);
586 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400587 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500588 }
589 WARN_ON(cache->caching_ctl);
590 cache->caching_ctl = caching_ctl;
591 cache->cached = BTRFS_CACHE_FAST;
592 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400593
Josef Bacikd53ba472012-04-12 16:03:57 -0400594 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400595 ret = load_free_space_cache(fs_info, cache);
596
597 spin_lock(&cache->lock);
598 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500599 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400600 cache->cached = BTRFS_CACHE_FINISHED;
601 cache->last_byte_to_unpin = (u64)-1;
602 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500603 if (load_cache_only) {
604 cache->caching_ctl = NULL;
605 cache->cached = BTRFS_CACHE_NO;
606 } else {
607 cache->cached = BTRFS_CACHE_STARTED;
608 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400609 }
610 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500611 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000612 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500613 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000614 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400615 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000616 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500617 } else {
618 /*
619 * We are not going to do the fast caching, set cached to the
620 * appropriate value and wakeup any waiters.
621 */
622 spin_lock(&cache->lock);
623 if (load_cache_only) {
624 cache->caching_ctl = NULL;
625 cache->cached = BTRFS_CACHE_NO;
626 } else {
627 cache->cached = BTRFS_CACHE_STARTED;
628 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400629 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500630 wake_up(&caching_ctl->wait);
631 }
632
633 if (load_cache_only) {
634 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400635 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400636 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400637
Josef Bacik9e351cc2014-03-13 15:42:13 -0400638 down_write(&fs_info->commit_root_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500639 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400640 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
Josef Bacik9e351cc2014-03-13 15:42:13 -0400641 up_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -0400642
Josef Bacik11dfe352009-11-13 20:12:59 +0000643 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400644
Qu Wenruoe66f0bb2014-02-28 10:46:12 +0800645 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400646
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400647 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400648}
649
Josef Bacik0f9dd462008-09-23 13:14:11 -0400650/*
651 * return the block group that starts at or after bytenr
652 */
Chris Masond3977122009-01-05 21:25:51 -0500653static struct btrfs_block_group_cache *
654btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400655{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400656 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400657
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400659
Josef Bacik0f9dd462008-09-23 13:14:11 -0400660 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400661}
662
Josef Bacik0f9dd462008-09-23 13:14:11 -0400663/*
Sankar P9f556842009-05-14 13:52:22 -0400664 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400665 */
Chris Masond3977122009-01-05 21:25:51 -0500666struct btrfs_block_group_cache *btrfs_lookup_block_group(
667 struct btrfs_fs_info *info,
668 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400669{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400670 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400671
Josef Bacik0f9dd462008-09-23 13:14:11 -0400672 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400673
Josef Bacik0f9dd462008-09-23 13:14:11 -0400674 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400675}
Chris Mason0b86a832008-03-24 15:01:56 -0400676
Josef Bacik0f9dd462008-09-23 13:14:11 -0400677static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
678 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400679{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400680 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400681 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400682
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200683 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400684
Chris Mason4184ea72009-03-10 12:39:20 -0400685 rcu_read_lock();
686 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400687 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400688 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400689 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400690 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400691 }
Chris Mason4184ea72009-03-10 12:39:20 -0400692 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400693 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400694}
695
Chris Mason4184ea72009-03-10 12:39:20 -0400696/*
697 * after adding space to the filesystem, we need to clear the full flags
698 * on all the space infos.
699 */
700void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
701{
702 struct list_head *head = &info->space_info;
703 struct btrfs_space_info *found;
704
705 rcu_read_lock();
706 list_for_each_entry_rcu(found, head, list)
707 found->full = 0;
708 rcu_read_unlock();
709}
710
Chris Masone02119d2008-09-05 16:13:11 -0400711/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400712int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400713{
714 int ret;
715 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400716 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400717
Zheng Yan31840ae2008-09-23 13:14:14 -0400718 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700719 if (!path)
720 return -ENOMEM;
721
Chris Masone02119d2008-09-05 16:13:11 -0400722 key.objectid = start;
723 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500724 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400725 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
726 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500727 if (ret > 0) {
728 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
729 if (key.objectid == start &&
730 key.type == BTRFS_METADATA_ITEM_KEY)
731 ret = 0;
732 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400733 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500734 return ret;
735}
736
Chris Masond8d5f3e2007-12-11 12:42:00 -0500737/*
Josef Bacik3173a182013-03-07 14:22:04 -0500738 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400739 *
740 * the head node for delayed ref is used to store the sum of all the
741 * reference count modifications queued up in the rbtree. the head
742 * node may also store the extent flags to set. This way you can check
743 * to see what the reference count and extent flags would be if all of
744 * the delayed refs are not processed.
745 */
746int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
747 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500748 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400749{
750 struct btrfs_delayed_ref_head *head;
751 struct btrfs_delayed_ref_root *delayed_refs;
752 struct btrfs_path *path;
753 struct btrfs_extent_item *ei;
754 struct extent_buffer *leaf;
755 struct btrfs_key key;
756 u32 item_size;
757 u64 num_refs;
758 u64 extent_flags;
759 int ret;
760
Josef Bacik3173a182013-03-07 14:22:04 -0500761 /*
762 * If we don't have skinny metadata, don't bother doing anything
763 * different
764 */
765 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
766 offset = root->leafsize;
767 metadata = 0;
768 }
769
Yan, Zhenga22285a2010-05-16 10:48:46 -0400770 path = btrfs_alloc_path();
771 if (!path)
772 return -ENOMEM;
773
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774 if (!trans) {
775 path->skip_locking = 1;
776 path->search_commit_root = 1;
777 }
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000778
779search_again:
780 key.objectid = bytenr;
781 key.offset = offset;
782 if (metadata)
783 key.type = BTRFS_METADATA_ITEM_KEY;
784 else
785 key.type = BTRFS_EXTENT_ITEM_KEY;
786
Yan, Zhenga22285a2010-05-16 10:48:46 -0400787again:
788 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
789 &key, path, 0, 0);
790 if (ret < 0)
791 goto out_free;
792
Josef Bacik3173a182013-03-07 14:22:04 -0500793 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100794 if (path->slots[0]) {
795 path->slots[0]--;
796 btrfs_item_key_to_cpu(path->nodes[0], &key,
797 path->slots[0]);
798 if (key.objectid == bytenr &&
799 key.type == BTRFS_EXTENT_ITEM_KEY &&
800 key.offset == root->leafsize)
801 ret = 0;
802 }
803 if (ret) {
804 key.objectid = bytenr;
805 key.type = BTRFS_EXTENT_ITEM_KEY;
806 key.offset = root->leafsize;
807 btrfs_release_path(path);
808 goto again;
809 }
Josef Bacik3173a182013-03-07 14:22:04 -0500810 }
811
Yan, Zhenga22285a2010-05-16 10:48:46 -0400812 if (ret == 0) {
813 leaf = path->nodes[0];
814 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
815 if (item_size >= sizeof(*ei)) {
816 ei = btrfs_item_ptr(leaf, path->slots[0],
817 struct btrfs_extent_item);
818 num_refs = btrfs_extent_refs(leaf, ei);
819 extent_flags = btrfs_extent_flags(leaf, ei);
820 } else {
821#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
822 struct btrfs_extent_item_v0 *ei0;
823 BUG_ON(item_size != sizeof(*ei0));
824 ei0 = btrfs_item_ptr(leaf, path->slots[0],
825 struct btrfs_extent_item_v0);
826 num_refs = btrfs_extent_refs_v0(leaf, ei0);
827 /* FIXME: this isn't correct for data */
828 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
829#else
830 BUG();
831#endif
832 }
833 BUG_ON(num_refs == 0);
834 } else {
835 num_refs = 0;
836 extent_flags = 0;
837 ret = 0;
838 }
839
840 if (!trans)
841 goto out;
842
843 delayed_refs = &trans->transaction->delayed_refs;
844 spin_lock(&delayed_refs->lock);
845 head = btrfs_find_delayed_ref_head(trans, bytenr);
846 if (head) {
847 if (!mutex_trylock(&head->mutex)) {
848 atomic_inc(&head->node.refs);
849 spin_unlock(&delayed_refs->lock);
850
David Sterbab3b4aa72011-04-21 01:20:15 +0200851 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400852
David Sterba8cc33e52011-05-02 15:29:25 +0200853 /*
854 * Mutex was contended, block until it's released and try
855 * again
856 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400857 mutex_lock(&head->mutex);
858 mutex_unlock(&head->mutex);
859 btrfs_put_delayed_ref(&head->node);
Filipe David Borba Manana639eefc2013-12-08 00:26:29 +0000860 goto search_again;
Yan, Zhenga22285a2010-05-16 10:48:46 -0400861 }
Josef Bacikd7df2c72014-01-23 09:21:38 -0500862 spin_lock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400863 if (head->extent_op && head->extent_op->update_flags)
864 extent_flags |= head->extent_op->flags_to_set;
865 else
866 BUG_ON(num_refs == 0);
867
868 num_refs += head->node.ref_mod;
Josef Bacikd7df2c72014-01-23 09:21:38 -0500869 spin_unlock(&head->lock);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400870 mutex_unlock(&head->mutex);
871 }
872 spin_unlock(&delayed_refs->lock);
873out:
874 WARN_ON(num_refs == 0);
875 if (refs)
876 *refs = num_refs;
877 if (flags)
878 *flags = extent_flags;
879out_free:
880 btrfs_free_path(path);
881 return ret;
882}
883
884/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500885 * Back reference rules. Back refs have three main goals:
886 *
887 * 1) differentiate between all holders of references to an extent so that
888 * when a reference is dropped we can make sure it was a valid reference
889 * before freeing the extent.
890 *
891 * 2) Provide enough information to quickly find the holders of an extent
892 * if we notice a given block is corrupted or bad.
893 *
894 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
895 * maintenance. This is actually the same as #2, but with a slightly
896 * different use case.
897 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400898 * There are two kinds of back refs. The implicit back refs is optimized
899 * for pointers in non-shared tree blocks. For a given pointer in a block,
900 * back refs of this kind provide information about the block's owner tree
901 * and the pointer's key. These information allow us to find the block by
902 * b-tree searching. The full back refs is for pointers in tree blocks not
903 * referenced by their owner trees. The location of tree block is recorded
904 * in the back refs. Actually the full back refs is generic, and can be
905 * used in all cases the implicit back refs is used. The major shortcoming
906 * of the full back refs is its overhead. Every time a tree block gets
907 * COWed, we have to update back refs entry for all pointers in it.
908 *
909 * For a newly allocated tree block, we use implicit back refs for
910 * pointers in it. This means most tree related operations only involve
911 * implicit back refs. For a tree block created in old transaction, the
912 * only way to drop a reference to it is COW it. So we can detect the
913 * event that tree block loses its owner tree's reference and do the
914 * back refs conversion.
915 *
916 * When a tree block is COW'd through a tree, there are four cases:
917 *
918 * The reference count of the block is one and the tree is the block's
919 * owner tree. Nothing to do in this case.
920 *
921 * The reference count of the block is one and the tree is not the
922 * block's owner tree. In this case, full back refs is used for pointers
923 * in the block. Remove these full back refs, add implicit back refs for
924 * every pointers in the new block.
925 *
926 * The reference count of the block is greater than one and the tree is
927 * the block's owner tree. In this case, implicit back refs is used for
928 * pointers in the block. Add full back refs for every pointers in the
929 * block, increase lower level extents' reference counts. The original
930 * implicit back refs are entailed to the new block.
931 *
932 * The reference count of the block is greater than one and the tree is
933 * not the block's owner tree. Add implicit back refs for every pointer in
934 * the new block, increase lower level extents' reference count.
935 *
936 * Back Reference Key composing:
937 *
938 * The key objectid corresponds to the first byte in the extent,
939 * The key type is used to differentiate between types of back refs.
940 * There are different meanings of the key offset for different types
941 * of back refs.
942 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500943 * File extents can be referenced by:
944 *
945 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400946 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500947 * - different offsets inside a file (bookend extents in file.c)
948 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 *
951 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500952 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 * - original offset in the file
954 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400955 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 * The key offset for the implicit back refs is hash of the first
957 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500958 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500960 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400961 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500962 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400963 * The key offset for the implicit back refs is the first byte of
964 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500965 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400966 * When a file extent is allocated, The implicit back refs is used.
967 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500968 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400969 * (root_key.objectid, inode objectid, offset in file, 1)
970 *
971 * When a file extent is removed file truncation, we find the
972 * corresponding implicit back refs and check the following fields:
973 *
974 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500975 *
976 * Btree extents can be referenced by:
977 *
978 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500979 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400980 * Both the implicit back refs and the full back refs for tree blocks
981 * only consist of key. The key offset for the implicit back refs is
982 * objectid of block's owner tree. The key offset for the full back refs
983 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500984 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400985 * When implicit back refs is used, information about the lowest key and
986 * level of the tree block are required. These information are stored in
987 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500988 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400989
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400990#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
991static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
992 struct btrfs_root *root,
993 struct btrfs_path *path,
994 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500995{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400996 struct btrfs_extent_item *item;
997 struct btrfs_extent_item_v0 *ei0;
998 struct btrfs_extent_ref_v0 *ref0;
999 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -04001000 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001001 struct btrfs_key key;
1002 struct btrfs_key found_key;
1003 u32 new_size = sizeof(*item);
1004 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -05001005 int ret;
1006
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001007 leaf = path->nodes[0];
1008 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -05001009
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001010 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1011 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1012 struct btrfs_extent_item_v0);
1013 refs = btrfs_extent_refs_v0(leaf, ei0);
1014
1015 if (owner == (u64)-1) {
1016 while (1) {
1017 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1018 ret = btrfs_next_leaf(root, path);
1019 if (ret < 0)
1020 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001021 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001022 leaf = path->nodes[0];
1023 }
1024 btrfs_item_key_to_cpu(leaf, &found_key,
1025 path->slots[0]);
1026 BUG_ON(key.objectid != found_key.objectid);
1027 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1028 path->slots[0]++;
1029 continue;
1030 }
1031 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1032 struct btrfs_extent_ref_v0);
1033 owner = btrfs_ref_objectid_v0(leaf, ref0);
1034 break;
1035 }
1036 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001037 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001038
1039 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1040 new_size += sizeof(*bi);
1041
1042 new_size -= sizeof(*ei0);
1043 ret = btrfs_search_slot(trans, root, &key, path,
1044 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001045 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001046 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001047 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001048
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001049 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001050
1051 leaf = path->nodes[0];
1052 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1053 btrfs_set_extent_refs(leaf, item, refs);
1054 /* FIXME: get real generation */
1055 btrfs_set_extent_generation(leaf, item, 0);
1056 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1057 btrfs_set_extent_flags(leaf, item,
1058 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1059 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1060 bi = (struct btrfs_tree_block_info *)(item + 1);
1061 /* FIXME: get first key of the block */
1062 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1063 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1064 } else {
1065 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1066 }
1067 btrfs_mark_buffer_dirty(leaf);
1068 return 0;
1069}
1070#endif
1071
1072static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1073{
1074 u32 high_crc = ~(u32)0;
1075 u32 low_crc = ~(u32)0;
1076 __le64 lenum;
1077
1078 lenum = cpu_to_le64(root_objectid);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001079 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001080 lenum = cpu_to_le64(owner);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001081 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001082 lenum = cpu_to_le64(offset);
Filipe David Borba Manana14a958e2014-01-12 02:22:46 +00001083 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001084
1085 return ((u64)high_crc << 31) ^ (u64)low_crc;
1086}
1087
1088static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1089 struct btrfs_extent_data_ref *ref)
1090{
1091 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1092 btrfs_extent_data_ref_objectid(leaf, ref),
1093 btrfs_extent_data_ref_offset(leaf, ref));
1094}
1095
1096static int match_extent_data_ref(struct extent_buffer *leaf,
1097 struct btrfs_extent_data_ref *ref,
1098 u64 root_objectid, u64 owner, u64 offset)
1099{
1100 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1101 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1102 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1103 return 0;
1104 return 1;
1105}
1106
1107static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1108 struct btrfs_root *root,
1109 struct btrfs_path *path,
1110 u64 bytenr, u64 parent,
1111 u64 root_objectid,
1112 u64 owner, u64 offset)
1113{
1114 struct btrfs_key key;
1115 struct btrfs_extent_data_ref *ref;
1116 struct extent_buffer *leaf;
1117 u32 nritems;
1118 int ret;
1119 int recow;
1120 int err = -ENOENT;
1121
1122 key.objectid = bytenr;
1123 if (parent) {
1124 key.type = BTRFS_SHARED_DATA_REF_KEY;
1125 key.offset = parent;
1126 } else {
1127 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1128 key.offset = hash_extent_data_ref(root_objectid,
1129 owner, offset);
1130 }
1131again:
1132 recow = 0;
1133 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1134 if (ret < 0) {
1135 err = ret;
1136 goto fail;
1137 }
1138
1139 if (parent) {
1140 if (!ret)
1141 return 0;
1142#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1143 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001144 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001145 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1146 if (ret < 0) {
1147 err = ret;
1148 goto fail;
1149 }
1150 if (!ret)
1151 return 0;
1152#endif
1153 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001154 }
1155
1156 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001157 nritems = btrfs_header_nritems(leaf);
1158 while (1) {
1159 if (path->slots[0] >= nritems) {
1160 ret = btrfs_next_leaf(root, path);
1161 if (ret < 0)
1162 err = ret;
1163 if (ret)
1164 goto fail;
1165
1166 leaf = path->nodes[0];
1167 nritems = btrfs_header_nritems(leaf);
1168 recow = 1;
1169 }
1170
1171 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1172 if (key.objectid != bytenr ||
1173 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1174 goto fail;
1175
1176 ref = btrfs_item_ptr(leaf, path->slots[0],
1177 struct btrfs_extent_data_ref);
1178
1179 if (match_extent_data_ref(leaf, ref, root_objectid,
1180 owner, offset)) {
1181 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001182 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001183 goto again;
1184 }
1185 err = 0;
1186 break;
1187 }
1188 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001189 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001190fail:
1191 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001192}
1193
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001194static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root,
1196 struct btrfs_path *path,
1197 u64 bytenr, u64 parent,
1198 u64 root_objectid, u64 owner,
1199 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001200{
1201 struct btrfs_key key;
1202 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 u32 num_refs;
1205 int ret;
1206
1207 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208 if (parent) {
1209 key.type = BTRFS_SHARED_DATA_REF_KEY;
1210 key.offset = parent;
1211 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001212 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001213 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1214 key.offset = hash_extent_data_ref(root_objectid,
1215 owner, offset);
1216 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001217 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001218
1219 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1220 if (ret && ret != -EEXIST)
1221 goto fail;
1222
1223 leaf = path->nodes[0];
1224 if (parent) {
1225 struct btrfs_shared_data_ref *ref;
1226 ref = btrfs_item_ptr(leaf, path->slots[0],
1227 struct btrfs_shared_data_ref);
1228 if (ret == 0) {
1229 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1230 } else {
1231 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1232 num_refs += refs_to_add;
1233 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1234 }
1235 } else {
1236 struct btrfs_extent_data_ref *ref;
1237 while (ret == -EEXIST) {
1238 ref = btrfs_item_ptr(leaf, path->slots[0],
1239 struct btrfs_extent_data_ref);
1240 if (match_extent_data_ref(leaf, ref, root_objectid,
1241 owner, offset))
1242 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001243 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001244 key.offset++;
1245 ret = btrfs_insert_empty_item(trans, root, path, &key,
1246 size);
1247 if (ret && ret != -EEXIST)
1248 goto fail;
1249
1250 leaf = path->nodes[0];
1251 }
1252 ref = btrfs_item_ptr(leaf, path->slots[0],
1253 struct btrfs_extent_data_ref);
1254 if (ret == 0) {
1255 btrfs_set_extent_data_ref_root(leaf, ref,
1256 root_objectid);
1257 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1258 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1259 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1260 } else {
1261 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1262 num_refs += refs_to_add;
1263 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1264 }
1265 }
1266 btrfs_mark_buffer_dirty(leaf);
1267 ret = 0;
1268fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001269 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001270 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001271}
1272
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001273static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
Josef Bacikfcebe452014-05-13 17:30:47 -07001276 int refs_to_drop, int *last_ref)
Zheng Yan31840ae2008-09-23 13:14:14 -04001277{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001278 struct btrfs_key key;
1279 struct btrfs_extent_data_ref *ref1 = NULL;
1280 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001281 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001282 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001283 int ret = 0;
1284
1285 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001286 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1287
1288 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1289 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1290 struct btrfs_extent_data_ref);
1291 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1292 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1293 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1294 struct btrfs_shared_data_ref);
1295 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1296#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1297 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1298 struct btrfs_extent_ref_v0 *ref0;
1299 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1300 struct btrfs_extent_ref_v0);
1301 num_refs = btrfs_ref_count_v0(leaf, ref0);
1302#endif
1303 } else {
1304 BUG();
1305 }
1306
Chris Mason56bec292009-03-13 10:10:06 -04001307 BUG_ON(num_refs < refs_to_drop);
1308 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001309
Zheng Yan31840ae2008-09-23 13:14:14 -04001310 if (num_refs == 0) {
1311 ret = btrfs_del_item(trans, root, path);
Josef Bacikfcebe452014-05-13 17:30:47 -07001312 *last_ref = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001314 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1315 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1316 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1317 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1318#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1319 else {
1320 struct btrfs_extent_ref_v0 *ref0;
1321 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1322 struct btrfs_extent_ref_v0);
1323 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1324 }
1325#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001326 btrfs_mark_buffer_dirty(leaf);
1327 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001328 return ret;
1329}
1330
1331static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1332 struct btrfs_path *path,
1333 struct btrfs_extent_inline_ref *iref)
1334{
1335 struct btrfs_key key;
1336 struct extent_buffer *leaf;
1337 struct btrfs_extent_data_ref *ref1;
1338 struct btrfs_shared_data_ref *ref2;
1339 u32 num_refs = 0;
1340
1341 leaf = path->nodes[0];
1342 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1343 if (iref) {
1344 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1345 BTRFS_EXTENT_DATA_REF_KEY) {
1346 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1347 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1348 } else {
1349 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1350 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1351 }
1352 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1353 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_extent_data_ref);
1355 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1356 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1357 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1358 struct btrfs_shared_data_ref);
1359 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1360#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1361 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1362 struct btrfs_extent_ref_v0 *ref0;
1363 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1364 struct btrfs_extent_ref_v0);
1365 num_refs = btrfs_ref_count_v0(leaf, ref0);
1366#endif
1367 } else {
1368 WARN_ON(1);
1369 }
1370 return num_refs;
1371}
1372
1373static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root,
1375 struct btrfs_path *path,
1376 u64 bytenr, u64 parent,
1377 u64 root_objectid)
1378{
1379 struct btrfs_key key;
1380 int ret;
1381
1382 key.objectid = bytenr;
1383 if (parent) {
1384 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1385 key.offset = parent;
1386 } else {
1387 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1388 key.offset = root_objectid;
1389 }
1390
1391 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1392 if (ret > 0)
1393 ret = -ENOENT;
1394#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1395 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001396 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001397 key.type = BTRFS_EXTENT_REF_V0_KEY;
1398 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1399 if (ret > 0)
1400 ret = -ENOENT;
1401 }
1402#endif
1403 return ret;
1404}
1405
1406static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1407 struct btrfs_root *root,
1408 struct btrfs_path *path,
1409 u64 bytenr, u64 parent,
1410 u64 root_objectid)
1411{
1412 struct btrfs_key key;
1413 int ret;
1414
1415 key.objectid = bytenr;
1416 if (parent) {
1417 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1418 key.offset = parent;
1419 } else {
1420 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1421 key.offset = root_objectid;
1422 }
1423
1424 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001425 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001426 return ret;
1427}
1428
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001429static inline int extent_ref_type(u64 parent, u64 owner)
1430{
1431 int type;
1432 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1433 if (parent > 0)
1434 type = BTRFS_SHARED_BLOCK_REF_KEY;
1435 else
1436 type = BTRFS_TREE_BLOCK_REF_KEY;
1437 } else {
1438 if (parent > 0)
1439 type = BTRFS_SHARED_DATA_REF_KEY;
1440 else
1441 type = BTRFS_EXTENT_DATA_REF_KEY;
1442 }
1443 return type;
1444}
1445
Yan Zheng2c47e6052009-06-27 21:07:35 -04001446static int find_next_key(struct btrfs_path *path, int level,
1447 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001448
1449{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001450 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001451 if (!path->nodes[level])
1452 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001453 if (path->slots[level] + 1 >=
1454 btrfs_header_nritems(path->nodes[level]))
1455 continue;
1456 if (level == 0)
1457 btrfs_item_key_to_cpu(path->nodes[level], key,
1458 path->slots[level] + 1);
1459 else
1460 btrfs_node_key_to_cpu(path->nodes[level], key,
1461 path->slots[level] + 1);
1462 return 0;
1463 }
1464 return 1;
1465}
1466
1467/*
1468 * look for inline back ref. if back ref is found, *ref_ret is set
1469 * to the address of inline back ref, and 0 is returned.
1470 *
1471 * if back ref isn't found, *ref_ret is set to the address where it
1472 * should be inserted, and -ENOENT is returned.
1473 *
1474 * if insert is true and there are too many inline back refs, the path
1475 * points to the extent item, and -EAGAIN is returned.
1476 *
1477 * NOTE: inline back refs are ordered in the same way that back ref
1478 * items in the tree are ordered.
1479 */
1480static noinline_for_stack
1481int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1482 struct btrfs_root *root,
1483 struct btrfs_path *path,
1484 struct btrfs_extent_inline_ref **ref_ret,
1485 u64 bytenr, u64 num_bytes,
1486 u64 parent, u64 root_objectid,
1487 u64 owner, u64 offset, int insert)
1488{
1489 struct btrfs_key key;
1490 struct extent_buffer *leaf;
1491 struct btrfs_extent_item *ei;
1492 struct btrfs_extent_inline_ref *iref;
1493 u64 flags;
1494 u64 item_size;
1495 unsigned long ptr;
1496 unsigned long end;
1497 int extra_size;
1498 int type;
1499 int want;
1500 int ret;
1501 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001502 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1503 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001504
1505 key.objectid = bytenr;
1506 key.type = BTRFS_EXTENT_ITEM_KEY;
1507 key.offset = num_bytes;
1508
1509 want = extent_ref_type(parent, owner);
1510 if (insert) {
1511 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001512 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001513 } else
1514 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001515
1516 /*
1517 * Owner is our parent level, so we can just add one to get the level
1518 * for the block we are interested in.
1519 */
1520 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1521 key.type = BTRFS_METADATA_ITEM_KEY;
1522 key.offset = owner;
1523 }
1524
1525again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001526 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1527 if (ret < 0) {
1528 err = ret;
1529 goto out;
1530 }
Josef Bacik3173a182013-03-07 14:22:04 -05001531
1532 /*
1533 * We may be a newly converted file system which still has the old fat
1534 * extent entries for metadata, so try and see if we have one of those.
1535 */
1536 if (ret > 0 && skinny_metadata) {
1537 skinny_metadata = false;
1538 if (path->slots[0]) {
1539 path->slots[0]--;
1540 btrfs_item_key_to_cpu(path->nodes[0], &key,
1541 path->slots[0]);
1542 if (key.objectid == bytenr &&
1543 key.type == BTRFS_EXTENT_ITEM_KEY &&
1544 key.offset == num_bytes)
1545 ret = 0;
1546 }
1547 if (ret) {
Filipe Manana9ce49a02014-04-24 15:15:28 +01001548 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05001549 key.type = BTRFS_EXTENT_ITEM_KEY;
1550 key.offset = num_bytes;
1551 btrfs_release_path(path);
1552 goto again;
1553 }
1554 }
1555
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001556 if (ret && !insert) {
1557 err = -ENOENT;
1558 goto out;
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301559 } else if (WARN_ON(ret)) {
Josef Bacik492104c2013-03-08 15:41:02 -05001560 err = -EIO;
Josef Bacik492104c2013-03-08 15:41:02 -05001561 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001562 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001563
1564 leaf = path->nodes[0];
1565 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1566#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1567 if (item_size < sizeof(*ei)) {
1568 if (!insert) {
1569 err = -ENOENT;
1570 goto out;
1571 }
1572 ret = convert_extent_item_v0(trans, root, path, owner,
1573 extra_size);
1574 if (ret < 0) {
1575 err = ret;
1576 goto out;
1577 }
1578 leaf = path->nodes[0];
1579 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1580 }
1581#endif
1582 BUG_ON(item_size < sizeof(*ei));
1583
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001584 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1585 flags = btrfs_extent_flags(leaf, ei);
1586
1587 ptr = (unsigned long)(ei + 1);
1588 end = (unsigned long)ei + item_size;
1589
Josef Bacik3173a182013-03-07 14:22:04 -05001590 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001591 ptr += sizeof(struct btrfs_tree_block_info);
1592 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001593 }
1594
1595 err = -ENOENT;
1596 while (1) {
1597 if (ptr >= end) {
1598 WARN_ON(ptr > end);
1599 break;
1600 }
1601 iref = (struct btrfs_extent_inline_ref *)ptr;
1602 type = btrfs_extent_inline_ref_type(leaf, iref);
1603 if (want < type)
1604 break;
1605 if (want > type) {
1606 ptr += btrfs_extent_inline_ref_size(type);
1607 continue;
1608 }
1609
1610 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1611 struct btrfs_extent_data_ref *dref;
1612 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1613 if (match_extent_data_ref(leaf, dref, root_objectid,
1614 owner, offset)) {
1615 err = 0;
1616 break;
1617 }
1618 if (hash_extent_data_ref_item(leaf, dref) <
1619 hash_extent_data_ref(root_objectid, owner, offset))
1620 break;
1621 } else {
1622 u64 ref_offset;
1623 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1624 if (parent > 0) {
1625 if (parent == ref_offset) {
1626 err = 0;
1627 break;
1628 }
1629 if (ref_offset < parent)
1630 break;
1631 } else {
1632 if (root_objectid == ref_offset) {
1633 err = 0;
1634 break;
1635 }
1636 if (ref_offset < root_objectid)
1637 break;
1638 }
1639 }
1640 ptr += btrfs_extent_inline_ref_size(type);
1641 }
1642 if (err == -ENOENT && insert) {
1643 if (item_size + extra_size >=
1644 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1645 err = -EAGAIN;
1646 goto out;
1647 }
1648 /*
1649 * To add new inline back ref, we have to make sure
1650 * there is no corresponding back ref item.
1651 * For simplicity, we just do not add new inline back
1652 * ref if there is any kind of item for this block
1653 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001654 if (find_next_key(path, 0, &key) == 0 &&
1655 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001656 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001657 err = -EAGAIN;
1658 goto out;
1659 }
1660 }
1661 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1662out:
Yan Zheng85d41982009-06-11 08:51:10 -04001663 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001664 path->keep_locks = 0;
1665 btrfs_unlock_up_safe(path, 1);
1666 }
1667 return err;
1668}
1669
1670/*
1671 * helper to add new inline back ref
1672 */
1673static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001674void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001675 struct btrfs_path *path,
1676 struct btrfs_extent_inline_ref *iref,
1677 u64 parent, u64 root_objectid,
1678 u64 owner, u64 offset, int refs_to_add,
1679 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680{
1681 struct extent_buffer *leaf;
1682 struct btrfs_extent_item *ei;
1683 unsigned long ptr;
1684 unsigned long end;
1685 unsigned long item_offset;
1686 u64 refs;
1687 int size;
1688 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001689
1690 leaf = path->nodes[0];
1691 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1692 item_offset = (unsigned long)iref - (unsigned long)ei;
1693
1694 type = extent_ref_type(parent, owner);
1695 size = btrfs_extent_inline_ref_size(type);
1696
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001697 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001698
1699 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1700 refs = btrfs_extent_refs(leaf, ei);
1701 refs += refs_to_add;
1702 btrfs_set_extent_refs(leaf, ei, refs);
1703 if (extent_op)
1704 __run_delayed_extent_op(extent_op, leaf, ei);
1705
1706 ptr = (unsigned long)ei + item_offset;
1707 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1708 if (ptr < end - size)
1709 memmove_extent_buffer(leaf, ptr + size, ptr,
1710 end - size - ptr);
1711
1712 iref = (struct btrfs_extent_inline_ref *)ptr;
1713 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1714 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1715 struct btrfs_extent_data_ref *dref;
1716 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1717 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1718 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1719 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1720 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1721 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1722 struct btrfs_shared_data_ref *sref;
1723 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1724 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1725 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1726 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1727 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1728 } else {
1729 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1730 }
1731 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001732}
1733
1734static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1735 struct btrfs_root *root,
1736 struct btrfs_path *path,
1737 struct btrfs_extent_inline_ref **ref_ret,
1738 u64 bytenr, u64 num_bytes, u64 parent,
1739 u64 root_objectid, u64 owner, u64 offset)
1740{
1741 int ret;
1742
1743 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1744 bytenr, num_bytes, parent,
1745 root_objectid, owner, offset, 0);
1746 if (ret != -ENOENT)
1747 return ret;
1748
David Sterbab3b4aa72011-04-21 01:20:15 +02001749 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001750 *ref_ret = NULL;
1751
1752 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1753 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1754 root_objectid);
1755 } else {
1756 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1757 root_objectid, owner, offset);
1758 }
1759 return ret;
1760}
1761
1762/*
1763 * helper to update/remove inline back ref
1764 */
1765static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001766void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001767 struct btrfs_path *path,
1768 struct btrfs_extent_inline_ref *iref,
1769 int refs_to_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07001770 struct btrfs_delayed_extent_op *extent_op,
1771 int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001772{
1773 struct extent_buffer *leaf;
1774 struct btrfs_extent_item *ei;
1775 struct btrfs_extent_data_ref *dref = NULL;
1776 struct btrfs_shared_data_ref *sref = NULL;
1777 unsigned long ptr;
1778 unsigned long end;
1779 u32 item_size;
1780 int size;
1781 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001782 u64 refs;
1783
1784 leaf = path->nodes[0];
1785 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1786 refs = btrfs_extent_refs(leaf, ei);
1787 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1788 refs += refs_to_mod;
1789 btrfs_set_extent_refs(leaf, ei, refs);
1790 if (extent_op)
1791 __run_delayed_extent_op(extent_op, leaf, ei);
1792
1793 type = btrfs_extent_inline_ref_type(leaf, iref);
1794
1795 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1796 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1797 refs = btrfs_extent_data_ref_count(leaf, dref);
1798 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1799 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1800 refs = btrfs_shared_data_ref_count(leaf, sref);
1801 } else {
1802 refs = 1;
1803 BUG_ON(refs_to_mod != -1);
1804 }
1805
1806 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1807 refs += refs_to_mod;
1808
1809 if (refs > 0) {
1810 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1811 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1812 else
1813 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1814 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001815 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001816 size = btrfs_extent_inline_ref_size(type);
1817 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1818 ptr = (unsigned long)iref;
1819 end = (unsigned long)ei + item_size;
1820 if (ptr + size < end)
1821 memmove_extent_buffer(leaf, ptr, ptr + size,
1822 end - ptr - size);
1823 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001824 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001825 }
1826 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001827}
1828
1829static noinline_for_stack
1830int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1831 struct btrfs_root *root,
1832 struct btrfs_path *path,
1833 u64 bytenr, u64 num_bytes, u64 parent,
1834 u64 root_objectid, u64 owner,
1835 u64 offset, int refs_to_add,
1836 struct btrfs_delayed_extent_op *extent_op)
1837{
1838 struct btrfs_extent_inline_ref *iref;
1839 int ret;
1840
1841 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1842 bytenr, num_bytes, parent,
1843 root_objectid, owner, offset, 1);
1844 if (ret == 0) {
1845 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001846 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001847 refs_to_add, extent_op, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001848 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001849 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001850 root_objectid, owner, offset,
1851 refs_to_add, extent_op);
1852 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001853 }
1854 return ret;
1855}
1856
1857static int insert_extent_backref(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root,
1859 struct btrfs_path *path,
1860 u64 bytenr, u64 parent, u64 root_objectid,
1861 u64 owner, u64 offset, int refs_to_add)
1862{
1863 int ret;
1864 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1865 BUG_ON(refs_to_add != 1);
1866 ret = insert_tree_block_ref(trans, root, path, bytenr,
1867 parent, root_objectid);
1868 } else {
1869 ret = insert_extent_data_ref(trans, root, path, bytenr,
1870 parent, root_objectid,
1871 owner, offset, refs_to_add);
1872 }
1873 return ret;
1874}
1875
1876static int remove_extent_backref(struct btrfs_trans_handle *trans,
1877 struct btrfs_root *root,
1878 struct btrfs_path *path,
1879 struct btrfs_extent_inline_ref *iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001880 int refs_to_drop, int is_data, int *last_ref)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001881{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001882 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001883
1884 BUG_ON(!is_data && refs_to_drop != 1);
1885 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001886 update_inline_extent_backref(root, path, iref,
Josef Bacikfcebe452014-05-13 17:30:47 -07001887 -refs_to_drop, NULL, last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001888 } else if (is_data) {
Josef Bacikfcebe452014-05-13 17:30:47 -07001889 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1890 last_ref);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001891 } else {
Josef Bacikfcebe452014-05-13 17:30:47 -07001892 *last_ref = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001893 ret = btrfs_del_item(trans, root, path);
1894 }
1895 return ret;
1896}
1897
Li Dongyang5378e602011-03-24 10:24:27 +00001898static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001899 u64 start, u64 len)
1900{
Li Dongyang5378e602011-03-24 10:24:27 +00001901 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001902}
Chris Mason15916de2008-11-19 21:17:22 -05001903
Liu Hui1f3c79a2009-01-05 15:57:51 -05001904static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001905 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001906{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001907 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001908 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001909 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001910
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001911
Liu Hui1f3c79a2009-01-05 15:57:51 -05001912 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001913 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001914 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001915 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001916 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001917 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001918 int i;
1919
Liu Hui1f3c79a2009-01-05 15:57:51 -05001920
Jan Schmidta1d3c472011-08-04 17:15:33 +02001921 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001922 if (!stripe->dev->can_discard)
1923 continue;
1924
Li Dongyang5378e602011-03-24 10:24:27 +00001925 ret = btrfs_issue_discard(stripe->dev->bdev,
1926 stripe->physical,
1927 stripe->length);
1928 if (!ret)
1929 discarded_bytes += stripe->length;
1930 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001931 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001932
1933 /*
1934 * Just in case we get back EOPNOTSUPP for some reason,
1935 * just ignore the return value so we don't screw up
1936 * people calling discard_extent.
1937 */
1938 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001939 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001940 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001941 }
Li Dongyang5378e602011-03-24 10:24:27 +00001942
1943 if (actual_bytes)
1944 *actual_bytes = discarded_bytes;
1945
Liu Hui1f3c79a2009-01-05 15:57:51 -05001946
David Woodhouse53b381b2013-01-29 18:40:14 -05001947 if (ret == -EOPNOTSUPP)
1948 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001949 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001950}
1951
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001952/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001953int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root,
1955 u64 bytenr, u64 num_bytes, u64 parent,
Josef Bacikfcebe452014-05-13 17:30:47 -07001956 u64 root_objectid, u64 owner, u64 offset,
1957 int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04001958{
1959 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001960 struct btrfs_fs_info *fs_info = root->fs_info;
1961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001962 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1963 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001964
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001965 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001966 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1967 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001968 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07001969 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001970 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001971 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1972 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 parent, root_objectid, owner, offset,
Josef Bacikfcebe452014-05-13 17:30:47 -07001974 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001975 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001976 return ret;
1977}
1978
Chris Mason925baed2008-06-25 16:01:30 -04001979static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001980 struct btrfs_root *root,
1981 u64 bytenr, u64 num_bytes,
1982 u64 parent, u64 root_objectid,
1983 u64 owner, u64 offset, int refs_to_add,
Josef Bacikfcebe452014-05-13 17:30:47 -07001984 int no_quota,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001985 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001986{
Josef Bacikfcebe452014-05-13 17:30:47 -07001987 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason5caf2a02007-04-02 11:20:42 -04001988 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001989 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001990 struct btrfs_extent_item *item;
Josef Bacikfcebe452014-05-13 17:30:47 -07001991 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001992 u64 refs;
1993 int ret;
Josef Bacikfcebe452014-05-13 17:30:47 -07001994 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL;
Chris Mason037e6392007-03-07 11:50:24 -05001995
Chris Mason5caf2a02007-04-02 11:20:42 -04001996 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001997 if (!path)
1998 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001999
Josef Bacikfcebe452014-05-13 17:30:47 -07002000 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2001 no_quota = 1;
2002
Chris Mason3c12ac72008-04-21 12:01:38 -04002003 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002004 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002005 /* this will setup the path even if it fails to insert the back ref */
Josef Bacikfcebe452014-05-13 17:30:47 -07002006 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2007 bytenr, num_bytes, parent,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002008 root_objectid, owner, offset,
2009 refs_to_add, extent_op);
Josef Bacikfcebe452014-05-13 17:30:47 -07002010 if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota))
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 goto out;
Josef Bacikfcebe452014-05-13 17:30:47 -07002012 /*
2013 * Ok we were able to insert an inline extent and it appears to be a new
2014 * reference, deal with the qgroup accounting.
2015 */
2016 if (!ret && !no_quota) {
2017 ASSERT(root->fs_info->quota_enabled);
2018 leaf = path->nodes[0];
2019 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2020 item = btrfs_item_ptr(leaf, path->slots[0],
2021 struct btrfs_extent_item);
2022 if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add)
2023 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2024 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04002025
Josef Bacikfcebe452014-05-13 17:30:47 -07002026 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2027 bytenr, num_bytes, type, 0);
2028 goto out;
2029 }
2030
2031 /*
2032 * Ok we had -EAGAIN which means we didn't have space to insert and
2033 * inline extent ref, so just update the reference count and add a
2034 * normal backref.
2035 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002036 leaf = path->nodes[0];
Josef Bacikfcebe452014-05-13 17:30:47 -07002037 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002038 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2039 refs = btrfs_extent_refs(leaf, item);
Josef Bacikfcebe452014-05-13 17:30:47 -07002040 if (refs)
2041 type = BTRFS_QGROUP_OPER_ADD_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002042 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2043 if (extent_op)
2044 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002047 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002048
Josef Bacikfcebe452014-05-13 17:30:47 -07002049 if (!no_quota) {
2050 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2051 bytenr, num_bytes, type, 0);
2052 if (ret)
2053 goto out;
2054 }
2055
Chris Mason3c12ac72008-04-21 12:01:38 -04002056 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002057 path->leave_spinning = 1;
Chris Mason56bec292009-03-13 10:10:06 -04002058 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002059 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002060 path, bytenr, parent, root_objectid,
2061 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002062 if (ret)
2063 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002064out:
Chris Mason74493f72007-12-11 09:25:06 -05002065 btrfs_free_path(path);
Liu Bo30d133f2013-10-11 16:30:23 +08002066 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05002067}
2068
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002069static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *root,
2071 struct btrfs_delayed_ref_node *node,
2072 struct btrfs_delayed_extent_op *extent_op,
2073 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002074{
Chris Mason56bec292009-03-13 10:10:06 -04002075 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002076 struct btrfs_delayed_data_ref *ref;
2077 struct btrfs_key ins;
2078 u64 parent = 0;
2079 u64 ref_root = 0;
2080 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002081
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002082 ins.objectid = node->bytenr;
2083 ins.offset = node->num_bytes;
2084 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002085
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002086 ref = btrfs_delayed_node_to_data_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002087 trace_run_delayed_data_ref(node, ref, node->action);
2088
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002089 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2090 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002091 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002092
2093 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002094 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002095 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002096 ret = alloc_reserved_file_extent(trans, root,
2097 parent, ref_root, flags,
2098 ref->objectid, ref->offset,
2099 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002100 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2101 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2102 node->num_bytes, parent,
2103 ref_root, ref->objectid,
2104 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002105 node->no_quota, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2107 ret = __btrfs_free_extent(trans, root, node->bytenr,
2108 node->num_bytes, parent,
2109 ref_root, ref->objectid,
2110 ref->offset, node->ref_mod,
Josef Bacikfcebe452014-05-13 17:30:47 -07002111 extent_op, node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002112 } else {
2113 BUG();
2114 }
Chris Mason56bec292009-03-13 10:10:06 -04002115 return ret;
2116}
2117
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002118static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2119 struct extent_buffer *leaf,
2120 struct btrfs_extent_item *ei)
2121{
2122 u64 flags = btrfs_extent_flags(leaf, ei);
2123 if (extent_op->update_flags) {
2124 flags |= extent_op->flags_to_set;
2125 btrfs_set_extent_flags(leaf, ei, flags);
2126 }
2127
2128 if (extent_op->update_key) {
2129 struct btrfs_tree_block_info *bi;
2130 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2131 bi = (struct btrfs_tree_block_info *)(ei + 1);
2132 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2133 }
2134}
2135
2136static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
2138 struct btrfs_delayed_ref_node *node,
2139 struct btrfs_delayed_extent_op *extent_op)
2140{
2141 struct btrfs_key key;
2142 struct btrfs_path *path;
2143 struct btrfs_extent_item *ei;
2144 struct extent_buffer *leaf;
2145 u32 item_size;
2146 int ret;
2147 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002148 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002149
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002150 if (trans->aborted)
2151 return 0;
2152
Josef Bacik3173a182013-03-07 14:22:04 -05002153 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2154 metadata = 0;
2155
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002156 path = btrfs_alloc_path();
2157 if (!path)
2158 return -ENOMEM;
2159
2160 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002161
Josef Bacik3173a182013-03-07 14:22:04 -05002162 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002163 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002164 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002165 } else {
2166 key.type = BTRFS_EXTENT_ITEM_KEY;
2167 key.offset = node->num_bytes;
2168 }
2169
2170again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002171 path->reada = 1;
2172 path->leave_spinning = 1;
2173 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2174 path, 0, 1);
2175 if (ret < 0) {
2176 err = ret;
2177 goto out;
2178 }
2179 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002180 if (metadata) {
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002181 if (path->slots[0] > 0) {
2182 path->slots[0]--;
2183 btrfs_item_key_to_cpu(path->nodes[0], &key,
2184 path->slots[0]);
2185 if (key.objectid == node->bytenr &&
2186 key.type == BTRFS_EXTENT_ITEM_KEY &&
2187 key.offset == node->num_bytes)
2188 ret = 0;
2189 }
2190 if (ret > 0) {
2191 btrfs_release_path(path);
2192 metadata = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002193
Filipe David Borba Manana55994882013-10-18 15:42:56 +01002194 key.objectid = node->bytenr;
2195 key.offset = node->num_bytes;
2196 key.type = BTRFS_EXTENT_ITEM_KEY;
2197 goto again;
2198 }
2199 } else {
2200 err = -EIO;
2201 goto out;
Josef Bacik3173a182013-03-07 14:22:04 -05002202 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 }
2204
2205 leaf = path->nodes[0];
2206 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2207#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2208 if (item_size < sizeof(*ei)) {
2209 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2210 path, (u64)-1, 0);
2211 if (ret < 0) {
2212 err = ret;
2213 goto out;
2214 }
2215 leaf = path->nodes[0];
2216 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2217 }
2218#endif
2219 BUG_ON(item_size < sizeof(*ei));
2220 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2221 __run_delayed_extent_op(extent_op, leaf, ei);
2222
2223 btrfs_mark_buffer_dirty(leaf);
2224out:
2225 btrfs_free_path(path);
2226 return err;
2227}
2228
2229static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2230 struct btrfs_root *root,
2231 struct btrfs_delayed_ref_node *node,
2232 struct btrfs_delayed_extent_op *extent_op,
2233 int insert_reserved)
2234{
2235 int ret = 0;
2236 struct btrfs_delayed_tree_ref *ref;
2237 struct btrfs_key ins;
2238 u64 parent = 0;
2239 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002240 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2241 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242
2243 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002244 trace_run_delayed_tree_ref(node, ref, node->action);
2245
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2247 parent = ref->parent;
Josef Bacikfcebe452014-05-13 17:30:47 -07002248 ref_root = ref->root;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002249
Josef Bacik3173a182013-03-07 14:22:04 -05002250 ins.objectid = node->bytenr;
2251 if (skinny_metadata) {
2252 ins.offset = ref->level;
2253 ins.type = BTRFS_METADATA_ITEM_KEY;
2254 } else {
2255 ins.offset = node->num_bytes;
2256 ins.type = BTRFS_EXTENT_ITEM_KEY;
2257 }
2258
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002259 BUG_ON(node->ref_mod != 1);
2260 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002261 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002262 ret = alloc_reserved_tree_block(trans, root,
2263 parent, ref_root,
2264 extent_op->flags_to_set,
2265 &extent_op->key,
Josef Bacikfcebe452014-05-13 17:30:47 -07002266 ref->level, &ins,
2267 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002268 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2269 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2270 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002271 ref->level, 0, 1, node->no_quota,
2272 extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002273 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2274 ret = __btrfs_free_extent(trans, root, node->bytenr,
2275 node->num_bytes, parent, ref_root,
Josef Bacikfcebe452014-05-13 17:30:47 -07002276 ref->level, 0, 1, extent_op,
2277 node->no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002278 } else {
2279 BUG();
2280 }
2281 return ret;
2282}
2283
Chris Mason56bec292009-03-13 10:10:06 -04002284/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002285static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2286 struct btrfs_root *root,
2287 struct btrfs_delayed_ref_node *node,
2288 struct btrfs_delayed_extent_op *extent_op,
2289 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002290{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002291 int ret = 0;
2292
Josef Bacik857cc2f2013-10-07 15:21:08 -04002293 if (trans->aborted) {
2294 if (insert_reserved)
2295 btrfs_pin_extent(root, node->bytenr,
2296 node->num_bytes, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002297 return 0;
Josef Bacik857cc2f2013-10-07 15:21:08 -04002298 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002299
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002300 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002301 struct btrfs_delayed_ref_head *head;
2302 /*
2303 * we've hit the end of the chain and we were supposed
2304 * to insert this extent into the tree. But, it got
2305 * deleted before we ever needed to insert it, so all
2306 * we have to do is clean up the accounting
2307 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002308 BUG_ON(extent_op);
2309 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002310 trace_run_delayed_ref_head(node, head, node->action);
2311
Chris Mason56bec292009-03-13 10:10:06 -04002312 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002313 btrfs_pin_extent(root, node->bytenr,
2314 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002315 if (head->is_data) {
2316 ret = btrfs_del_csums(trans, root,
2317 node->bytenr,
2318 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002319 }
Chris Mason56bec292009-03-13 10:10:06 -04002320 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002321 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002322 }
Josef Bacikeb099672009-02-12 09:27:38 -05002323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002324 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2325 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2326 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2327 insert_reserved);
2328 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2329 node->type == BTRFS_SHARED_DATA_REF_KEY)
2330 ret = run_delayed_data_ref(trans, root, node, extent_op,
2331 insert_reserved);
2332 else
2333 BUG();
2334 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002335}
2336
Chris Mason56bec292009-03-13 10:10:06 -04002337static noinline struct btrfs_delayed_ref_node *
2338select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002339{
Chris Mason56bec292009-03-13 10:10:06 -04002340 struct rb_node *node;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002341 struct btrfs_delayed_ref_node *ref, *last = NULL;;
2342
Chris Mason56bec292009-03-13 10:10:06 -04002343 /*
2344 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2345 * this prevents ref count from going down to zero when
2346 * there still are pending delayed ref.
2347 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002348 node = rb_first(&head->ref_root);
2349 while (node) {
Chris Mason56bec292009-03-13 10:10:06 -04002350 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2351 rb_node);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002352 if (ref->action == BTRFS_ADD_DELAYED_REF)
Chris Mason56bec292009-03-13 10:10:06 -04002353 return ref;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002354 else if (last == NULL)
2355 last = ref;
2356 node = rb_next(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002357 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002358 return last;
Chris Mason56bec292009-03-13 10:10:06 -04002359}
2360
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002361/*
2362 * Returns 0 on success or if called with an already aborted transaction.
2363 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2364 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002365static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2366 struct btrfs_root *root,
2367 unsigned long nr)
Chris Mason56bec292009-03-13 10:10:06 -04002368{
Chris Mason56bec292009-03-13 10:10:06 -04002369 struct btrfs_delayed_ref_root *delayed_refs;
2370 struct btrfs_delayed_ref_node *ref;
2371 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002372 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002373 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002374 ktime_t start = ktime_get();
Chris Mason56bec292009-03-13 10:10:06 -04002375 int ret;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002376 unsigned long count = 0;
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002377 unsigned long actual_count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002378 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002379
2380 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002381 while (1) {
2382 if (!locked_ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002383 if (count >= nr)
Chris Mason56bec292009-03-13 10:10:06 -04002384 break;
Chris Mason56bec292009-03-13 10:10:06 -04002385
Josef Bacikd7df2c72014-01-23 09:21:38 -05002386 spin_lock(&delayed_refs->lock);
2387 locked_ref = btrfs_select_ref_head(trans);
2388 if (!locked_ref) {
2389 spin_unlock(&delayed_refs->lock);
2390 break;
2391 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002392
2393 /* grab the lock that says we are going to process
2394 * all the refs for this head */
2395 ret = btrfs_delayed_ref_lock(trans, locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002396 spin_unlock(&delayed_refs->lock);
Chris Masonc3e69d52009-03-13 10:17:05 -04002397 /*
2398 * we may have dropped the spin lock to get the head
2399 * mutex lock, and that might have given someone else
2400 * time to free the head. If that's true, it has been
2401 * removed from our list and we can move on.
2402 */
2403 if (ret == -EAGAIN) {
2404 locked_ref = NULL;
2405 count++;
2406 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002407 }
2408 }
2409
2410 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002411 * We need to try and merge add/drops of the same ref since we
2412 * can run into issues with relocate dropping the implicit ref
2413 * and then it being added back again before the drop can
2414 * finish. If we merged anything we need to re-loop so we can
2415 * get a good ref.
2416 */
Josef Bacikd7df2c72014-01-23 09:21:38 -05002417 spin_lock(&locked_ref->lock);
Josef Bacikae1e2062012-08-07 16:00:32 -04002418 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2419 locked_ref);
2420
2421 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002422 * locked_ref is the head node, so we have to go one
2423 * node back for any delayed ref updates
2424 */
2425 ref = select_delayed_ref(locked_ref);
2426
2427 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002428 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002429 spin_unlock(&locked_ref->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002430 btrfs_delayed_ref_unlock(locked_ref);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002431 spin_lock(&delayed_refs->lock);
2432 locked_ref->processing = 0;
Arne Jansend1270cd2011-09-13 15:16:43 +02002433 delayed_refs->num_heads_ready++;
2434 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002435 locked_ref = NULL;
Arne Jansend1270cd2011-09-13 15:16:43 +02002436 cond_resched();
Josef Bacik27a377d2014-02-07 13:57:59 -05002437 count++;
Arne Jansend1270cd2011-09-13 15:16:43 +02002438 continue;
2439 }
2440
2441 /*
Chris Mason56bec292009-03-13 10:10:06 -04002442 * record the must insert reserved flag before we
2443 * drop the spin lock.
2444 */
2445 must_insert_reserved = locked_ref->must_insert_reserved;
2446 locked_ref->must_insert_reserved = 0;
2447
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002448 extent_op = locked_ref->extent_op;
2449 locked_ref->extent_op = NULL;
2450
Chris Mason56bec292009-03-13 10:10:06 -04002451 if (!ref) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002452
2453
Chris Mason56bec292009-03-13 10:10:06 -04002454 /* All delayed refs have been processed, Go ahead
2455 * and send the head node to run_one_delayed_ref,
2456 * so that any accounting fixes can happen
2457 */
2458 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002459
2460 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002461 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002462 extent_op = NULL;
2463 }
2464
2465 if (extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002466 spin_unlock(&locked_ref->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002467 ret = run_delayed_extent_op(trans, root,
2468 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002469 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002470
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002471 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04002472 /*
2473 * Need to reset must_insert_reserved if
2474 * there was an error so the abort stuff
2475 * can cleanup the reserved space
2476 * properly.
2477 */
2478 if (must_insert_reserved)
2479 locked_ref->must_insert_reserved = 1;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002480 locked_ref->processing = 0;
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002481 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Miao Xie093486c2012-12-19 08:10:10 +00002482 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002483 return ret;
2484 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002485 continue;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002486 }
Chris Mason56bec292009-03-13 10:10:06 -04002487
Josef Bacikd7df2c72014-01-23 09:21:38 -05002488 /*
2489 * Need to drop our head ref lock and re-aqcuire the
2490 * delayed ref lock and then re-check to make sure
2491 * nobody got added.
2492 */
2493 spin_unlock(&locked_ref->lock);
2494 spin_lock(&delayed_refs->lock);
2495 spin_lock(&locked_ref->lock);
Josef Bacik573a0752014-03-27 19:41:34 -04002496 if (rb_first(&locked_ref->ref_root) ||
2497 locked_ref->extent_op) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002498 spin_unlock(&locked_ref->lock);
2499 spin_unlock(&delayed_refs->lock);
2500 continue;
2501 }
2502 ref->in_tree = 0;
2503 delayed_refs->num_heads--;
Liu Boc46effa2013-10-14 12:59:45 +08002504 rb_erase(&locked_ref->href_node,
2505 &delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002506 spin_unlock(&delayed_refs->lock);
2507 } else {
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002508 actual_count++;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002509 ref->in_tree = 0;
2510 rb_erase(&ref->rb_node, &locked_ref->ref_root);
Liu Boc46effa2013-10-14 12:59:45 +08002511 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002512 atomic_dec(&delayed_refs->num_entries);
2513
Miao Xie093486c2012-12-19 08:10:10 +00002514 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002515 /*
2516 * when we play the delayed ref, also correct the
2517 * ref_mod on head
2518 */
2519 switch (ref->action) {
2520 case BTRFS_ADD_DELAYED_REF:
2521 case BTRFS_ADD_DELAYED_EXTENT:
2522 locked_ref->node.ref_mod -= ref->ref_mod;
2523 break;
2524 case BTRFS_DROP_DELAYED_REF:
2525 locked_ref->node.ref_mod += ref->ref_mod;
2526 break;
2527 default:
2528 WARN_ON(1);
2529 }
2530 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002531 spin_unlock(&locked_ref->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002532
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002533 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002534 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002535
Miao Xie78a61842012-11-21 02:21:28 +00002536 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002537 if (ret) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002538 locked_ref->processing = 0;
Miao Xie093486c2012-12-19 08:10:10 +00002539 btrfs_delayed_ref_unlock(locked_ref);
2540 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002541 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002542 return ret;
2543 }
2544
Miao Xie093486c2012-12-19 08:10:10 +00002545 /*
2546 * If this node is a head, that means all the refs in this head
2547 * have been dealt with, and we will pick the next head to deal
2548 * with, so we must unlock the head and drop it from the cluster
2549 * list before we release it.
2550 */
2551 if (btrfs_delayed_ref_is_head(ref)) {
Miao Xie093486c2012-12-19 08:10:10 +00002552 btrfs_delayed_ref_unlock(locked_ref);
2553 locked_ref = NULL;
2554 }
2555 btrfs_put_delayed_ref(ref);
2556 count++;
Chris Mason1887be62009-03-13 10:11:24 -04002557 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002558 }
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002559
2560 /*
2561 * We don't want to include ref heads since we can have empty ref heads
2562 * and those will drastically skew our runtime down since we just do
2563 * accounting, no actual extent tree updates.
2564 */
2565 if (actual_count > 0) {
2566 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2567 u64 avg;
2568
2569 /*
2570 * We weigh the current average higher than our current runtime
2571 * to avoid large swings in the average.
2572 */
2573 spin_lock(&delayed_refs->lock);
2574 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2575 avg = div64_u64(avg, 4);
2576 fs_info->avg_delayed_ref_runtime = avg;
2577 spin_unlock(&delayed_refs->lock);
2578 }
Josef Bacikd7df2c72014-01-23 09:21:38 -05002579 return 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002580}
2581
Arne Jansen709c0482011-09-12 12:22:57 +02002582#ifdef SCRAMBLE_DELAYED_REFS
2583/*
2584 * Normally delayed refs get processed in ascending bytenr order. This
2585 * correlates in most cases to the order added. To expose dependencies on this
2586 * order, we start to process the tree in the middle instead of the beginning
2587 */
2588static u64 find_middle(struct rb_root *root)
2589{
2590 struct rb_node *n = root->rb_node;
2591 struct btrfs_delayed_ref_node *entry;
2592 int alt = 1;
2593 u64 middle;
2594 u64 first = 0, last = 0;
2595
2596 n = rb_first(root);
2597 if (n) {
2598 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2599 first = entry->bytenr;
2600 }
2601 n = rb_last(root);
2602 if (n) {
2603 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2604 last = entry->bytenr;
2605 }
2606 n = root->rb_node;
2607
2608 while (n) {
2609 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2610 WARN_ON(!entry->in_tree);
2611
2612 middle = entry->bytenr;
2613
2614 if (alt)
2615 n = n->rb_left;
2616 else
2617 n = n->rb_right;
2618
2619 alt = 1 - alt;
2620 }
2621 return middle;
2622}
2623#endif
2624
Josef Bacik1be41b72013-06-12 13:56:06 -04002625static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2626{
2627 u64 num_bytes;
2628
2629 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2630 sizeof(struct btrfs_extent_inline_ref));
2631 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2632 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2633
2634 /*
2635 * We don't ever fill up leaves all the way so multiply by 2 just to be
2636 * closer to what we're really going to want to ouse.
2637 */
2638 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2639}
2640
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002641int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
Josef Bacik1be41b72013-06-12 13:56:06 -04002642 struct btrfs_root *root)
2643{
2644 struct btrfs_block_rsv *global_rsv;
2645 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2646 u64 num_bytes;
2647 int ret = 0;
2648
2649 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2650 num_heads = heads_to_leaves(root, num_heads);
2651 if (num_heads > 1)
2652 num_bytes += (num_heads - 1) * root->leafsize;
2653 num_bytes <<= 1;
2654 global_rsv = &root->fs_info->global_block_rsv;
2655
2656 /*
2657 * If we can't allocate any more chunks lets make sure we have _lots_ of
2658 * wiggle room since running delayed refs can create more delayed refs.
2659 */
2660 if (global_rsv->space_info->full)
2661 num_bytes <<= 1;
2662
2663 spin_lock(&global_rsv->lock);
2664 if (global_rsv->reserved <= num_bytes)
2665 ret = 1;
2666 spin_unlock(&global_rsv->lock);
2667 return ret;
2668}
2669
Josef Bacik0a2b2a82014-01-23 10:54:11 -05002670int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2671 struct btrfs_root *root)
2672{
2673 struct btrfs_fs_info *fs_info = root->fs_info;
2674 u64 num_entries =
2675 atomic_read(&trans->transaction->delayed_refs.num_entries);
2676 u64 avg_runtime;
2677
2678 smp_mb();
2679 avg_runtime = fs_info->avg_delayed_ref_runtime;
2680 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2681 return 1;
2682
2683 return btrfs_check_space_for_delayed_refs(trans, root);
2684}
2685
Chris Masonc3e69d52009-03-13 10:17:05 -04002686/*
2687 * this starts processing the delayed reference count updates and
2688 * extent insertions we have queued up so far. count can be
2689 * 0, which means to process everything in the tree at the start
2690 * of the run (but not newly added entries), or it can be some target
2691 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002692 *
2693 * Returns 0 on success or if called with an aborted transaction
2694 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002695 */
2696int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2697 struct btrfs_root *root, unsigned long count)
2698{
2699 struct rb_node *node;
2700 struct btrfs_delayed_ref_root *delayed_refs;
Liu Boc46effa2013-10-14 12:59:45 +08002701 struct btrfs_delayed_ref_head *head;
Chris Masonc3e69d52009-03-13 10:17:05 -04002702 int ret;
2703 int run_all = count == (unsigned long)-1;
2704 int run_most = 0;
2705
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002706 /* We'll clean this up in btrfs_cleanup_transaction */
2707 if (trans->aborted)
2708 return 0;
2709
Chris Masonc3e69d52009-03-13 10:17:05 -04002710 if (root == root->fs_info->extent_root)
2711 root = root->fs_info->tree_root;
2712
2713 delayed_refs = &trans->transaction->delayed_refs;
Chris Masonbb721702013-01-29 18:44:12 -05002714 if (count == 0) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002715 count = atomic_read(&delayed_refs->num_entries) * 2;
Chris Masonbb721702013-01-29 18:44:12 -05002716 run_most = 1;
2717 }
2718
Chris Masonc3e69d52009-03-13 10:17:05 -04002719again:
Arne Jansen709c0482011-09-12 12:22:57 +02002720#ifdef SCRAMBLE_DELAYED_REFS
2721 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2722#endif
Josef Bacikd7df2c72014-01-23 09:21:38 -05002723 ret = __btrfs_run_delayed_refs(trans, root, count);
2724 if (ret < 0) {
2725 btrfs_abort_transaction(trans, root, ret);
2726 return ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002727 }
2728
Chris Mason56bec292009-03-13 10:10:06 -04002729 if (run_all) {
Josef Bacikd7df2c72014-01-23 09:21:38 -05002730 if (!list_empty(&trans->new_bgs))
Josef Bacikea658ba2012-09-11 16:57:25 -04002731 btrfs_create_pending_block_groups(trans, root);
Josef Bacikea658ba2012-09-11 16:57:25 -04002732
Josef Bacikd7df2c72014-01-23 09:21:38 -05002733 spin_lock(&delayed_refs->lock);
Liu Boc46effa2013-10-14 12:59:45 +08002734 node = rb_first(&delayed_refs->href_root);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002735 if (!node) {
2736 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002737 goto out;
Josef Bacikd7df2c72014-01-23 09:21:38 -05002738 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002739 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002740
2741 while (node) {
Liu Boc46effa2013-10-14 12:59:45 +08002742 head = rb_entry(node, struct btrfs_delayed_ref_head,
2743 href_node);
2744 if (btrfs_delayed_ref_is_head(&head->node)) {
2745 struct btrfs_delayed_ref_node *ref;
Chris Mason56bec292009-03-13 10:10:06 -04002746
Liu Boc46effa2013-10-14 12:59:45 +08002747 ref = &head->node;
Chris Mason56bec292009-03-13 10:10:06 -04002748 atomic_inc(&ref->refs);
2749
2750 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002751 /*
2752 * Mutex was contended, block until it's
2753 * released and try again
2754 */
Chris Mason56bec292009-03-13 10:10:06 -04002755 mutex_lock(&head->mutex);
2756 mutex_unlock(&head->mutex);
2757
2758 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002759 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002760 goto again;
Liu Boc46effa2013-10-14 12:59:45 +08002761 } else {
2762 WARN_ON(1);
Chris Mason56bec292009-03-13 10:10:06 -04002763 }
2764 node = rb_next(node);
2765 }
2766 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002767 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002768 goto again;
2769 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002770out:
Josef Bacikfcebe452014-05-13 17:30:47 -07002771 ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
2772 if (ret)
2773 return ret;
Jan Schmidtedf39272012-06-28 18:04:55 +02002774 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002775 return 0;
2776}
2777
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002778int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2779 struct btrfs_root *root,
2780 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002781 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002782{
2783 struct btrfs_delayed_extent_op *extent_op;
2784 int ret;
2785
Miao Xie78a61842012-11-21 02:21:28 +00002786 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787 if (!extent_op)
2788 return -ENOMEM;
2789
2790 extent_op->flags_to_set = flags;
2791 extent_op->update_flags = 1;
2792 extent_op->update_key = 0;
2793 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002794 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002795
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002796 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2797 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002798 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002799 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002800 return ret;
2801}
2802
2803static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2804 struct btrfs_root *root,
2805 struct btrfs_path *path,
2806 u64 objectid, u64 offset, u64 bytenr)
2807{
2808 struct btrfs_delayed_ref_head *head;
2809 struct btrfs_delayed_ref_node *ref;
2810 struct btrfs_delayed_data_ref *data_ref;
2811 struct btrfs_delayed_ref_root *delayed_refs;
2812 struct rb_node *node;
2813 int ret = 0;
2814
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815 delayed_refs = &trans->transaction->delayed_refs;
2816 spin_lock(&delayed_refs->lock);
2817 head = btrfs_find_delayed_ref_head(trans, bytenr);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002818 if (!head) {
2819 spin_unlock(&delayed_refs->lock);
2820 return 0;
2821 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002822
2823 if (!mutex_trylock(&head->mutex)) {
2824 atomic_inc(&head->node.refs);
2825 spin_unlock(&delayed_refs->lock);
2826
David Sterbab3b4aa72011-04-21 01:20:15 +02002827 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002828
David Sterba8cc33e52011-05-02 15:29:25 +02002829 /*
2830 * Mutex was contended, block until it's released and let
2831 * caller try again
2832 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002833 mutex_lock(&head->mutex);
2834 mutex_unlock(&head->mutex);
2835 btrfs_put_delayed_ref(&head->node);
2836 return -EAGAIN;
2837 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002838 spin_unlock(&delayed_refs->lock);
Josef Bacikd7df2c72014-01-23 09:21:38 -05002839
2840 spin_lock(&head->lock);
2841 node = rb_first(&head->ref_root);
2842 while (node) {
2843 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2844 node = rb_next(node);
2845
2846 /* If it's a shared ref we know a cross reference exists */
2847 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2848 ret = 1;
2849 break;
2850 }
2851
2852 data_ref = btrfs_delayed_node_to_data_ref(ref);
2853
2854 /*
2855 * If our ref doesn't match the one we're currently looking at
2856 * then we have a cross reference.
2857 */
2858 if (data_ref->root != root->root_key.objectid ||
2859 data_ref->objectid != objectid ||
2860 data_ref->offset != offset) {
2861 ret = 1;
2862 break;
2863 }
2864 }
2865 spin_unlock(&head->lock);
2866 mutex_unlock(&head->mutex);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002867 return ret;
2868}
2869
2870static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2871 struct btrfs_root *root,
2872 struct btrfs_path *path,
2873 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002874{
2875 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002876 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002877 struct btrfs_extent_data_ref *ref;
2878 struct btrfs_extent_inline_ref *iref;
2879 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002880 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002881 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002882 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002883
Chris Masonbe20aa92007-12-17 20:14:01 -05002884 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002885 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002886 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002887
Chris Masonbe20aa92007-12-17 20:14:01 -05002888 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2889 if (ret < 0)
2890 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002891 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002892
2893 ret = -ENOENT;
2894 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002895 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002896
Zheng Yan31840ae2008-09-23 13:14:14 -04002897 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002898 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002899 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002900
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002901 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002902 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002903
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002904 ret = 1;
2905 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2906#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2907 if (item_size < sizeof(*ei)) {
2908 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2909 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002910 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002911#endif
2912 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2913
2914 if (item_size != sizeof(*ei) +
2915 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2916 goto out;
2917
2918 if (btrfs_extent_generation(leaf, ei) <=
2919 btrfs_root_last_snapshot(&root->root_item))
2920 goto out;
2921
2922 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2923 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2924 BTRFS_EXTENT_DATA_REF_KEY)
2925 goto out;
2926
2927 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2928 if (btrfs_extent_refs(leaf, ei) !=
2929 btrfs_extent_data_ref_count(leaf, ref) ||
2930 btrfs_extent_data_ref_root(leaf, ref) !=
2931 root->root_key.objectid ||
2932 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2933 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2934 goto out;
2935
Yan Zhengf321e492008-07-30 09:26:11 -04002936 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002937out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002938 return ret;
2939}
2940
2941int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2942 struct btrfs_root *root,
2943 u64 objectid, u64 offset, u64 bytenr)
2944{
2945 struct btrfs_path *path;
2946 int ret;
2947 int ret2;
2948
2949 path = btrfs_alloc_path();
2950 if (!path)
2951 return -ENOENT;
2952
2953 do {
2954 ret = check_committed_ref(trans, root, path, objectid,
2955 offset, bytenr);
2956 if (ret && ret != -ENOENT)
2957 goto out;
2958
2959 ret2 = check_delayed_ref(trans, root, path, objectid,
2960 offset, bytenr);
2961 } while (ret2 == -EAGAIN);
2962
2963 if (ret2 && ret2 != -ENOENT) {
2964 ret = ret2;
2965 goto out;
2966 }
2967
2968 if (ret != -ENOENT || ret2 != -ENOENT)
2969 ret = 0;
2970out:
Yan Zhengf321e492008-07-30 09:26:11 -04002971 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002972 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2973 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002974 return ret;
2975}
2976
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002977static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002978 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002979 struct extent_buffer *buf,
Josef Bacikfcebe452014-05-13 17:30:47 -07002980 int full_backref, int inc, int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04002981{
2982 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002983 u64 num_bytes;
2984 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002985 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002986 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04002987 struct btrfs_key key;
2988 struct btrfs_file_extent_item *fi;
2989 int i;
2990 int level;
2991 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002992 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002993 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04002994
2995 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002996 nritems = btrfs_header_nritems(buf);
2997 level = btrfs_header_level(buf);
2998
Miao Xie27cdeb72014-04-02 19:51:05 +08002999 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003000 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003001
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003002 if (inc)
3003 process_func = btrfs_inc_extent_ref;
3004 else
3005 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003006
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003007 if (full_backref)
3008 parent = buf->start;
3009 else
3010 parent = 0;
3011
Zheng Yan31840ae2008-09-23 13:14:14 -04003012 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003013 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003014 btrfs_item_key_to_cpu(buf, &key, i);
3015 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003016 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003017 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003018 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003019 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003020 BTRFS_FILE_EXTENT_INLINE)
3021 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003022 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3023 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003024 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003025
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3027 key.offset -= btrfs_file_extent_offset(buf, fi);
3028 ret = process_func(trans, root, bytenr, num_bytes,
3029 parent, ref_root, key.objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07003030 key.offset, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003031 if (ret)
3032 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003033 } else {
3034 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003035 num_bytes = btrfs_level_size(root, level - 1);
3036 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003037 parent, ref_root, level - 1, 0,
Josef Bacikfcebe452014-05-13 17:30:47 -07003038 no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003040 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003041 }
3042 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003043 return 0;
3044fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003045 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003046}
3047
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacikfcebe452014-05-13 17:30:47 -07003049 struct extent_buffer *buf, int full_backref, int no_quota)
Zheng Yan31840ae2008-09-23 13:14:14 -04003050{
Josef Bacikfcebe452014-05-13 17:30:47 -07003051 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003052}
Zheng Yan31840ae2008-09-23 13:14:14 -04003053
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003054int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Josef Bacikfcebe452014-05-13 17:30:47 -07003055 struct extent_buffer *buf, int full_backref, int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003056{
Josef Bacikfcebe452014-05-13 17:30:47 -07003057 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, no_quota);
Zheng Yan31840ae2008-09-23 13:14:14 -04003058}
3059
Chris Mason9078a3e2007-04-26 16:46:15 -04003060static int write_one_cache_group(struct btrfs_trans_handle *trans,
3061 struct btrfs_root *root,
3062 struct btrfs_path *path,
3063 struct btrfs_block_group_cache *cache)
3064{
3065 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003066 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003067 unsigned long bi;
3068 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003069
Chris Mason9078a3e2007-04-26 16:46:15 -04003070 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003071 if (ret < 0)
3072 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003073 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003074
3075 leaf = path->nodes[0];
3076 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3077 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3078 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003079 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003080fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003081 if (ret) {
3082 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003083 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003084 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003085 return 0;
3086
3087}
3088
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003089static struct btrfs_block_group_cache *
3090next_block_group(struct btrfs_root *root,
3091 struct btrfs_block_group_cache *cache)
3092{
3093 struct rb_node *node;
3094 spin_lock(&root->fs_info->block_group_cache_lock);
3095 node = rb_next(&cache->cache_node);
3096 btrfs_put_block_group(cache);
3097 if (node) {
3098 cache = rb_entry(node, struct btrfs_block_group_cache,
3099 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003100 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003101 } else
3102 cache = NULL;
3103 spin_unlock(&root->fs_info->block_group_cache_lock);
3104 return cache;
3105}
3106
Josef Bacik0af3d002010-06-21 14:48:16 -04003107static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3108 struct btrfs_trans_handle *trans,
3109 struct btrfs_path *path)
3110{
3111 struct btrfs_root *root = block_group->fs_info->tree_root;
3112 struct inode *inode = NULL;
3113 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003114 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003115 int num_pages = 0;
3116 int retries = 0;
3117 int ret = 0;
3118
3119 /*
3120 * If this block group is smaller than 100 megs don't bother caching the
3121 * block group.
3122 */
3123 if (block_group->key.offset < (100 * 1024 * 1024)) {
3124 spin_lock(&block_group->lock);
3125 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3126 spin_unlock(&block_group->lock);
3127 return 0;
3128 }
3129
3130again:
3131 inode = lookup_free_space_inode(root, block_group, path);
3132 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3133 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003134 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003135 goto out;
3136 }
3137
3138 if (IS_ERR(inode)) {
3139 BUG_ON(retries);
3140 retries++;
3141
3142 if (block_group->ro)
3143 goto out_free;
3144
3145 ret = create_free_space_inode(root, trans, block_group, path);
3146 if (ret)
3147 goto out_free;
3148 goto again;
3149 }
3150
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003151 /* We've already setup this transaction, go ahead and exit */
3152 if (block_group->cache_generation == trans->transid &&
3153 i_size_read(inode)) {
3154 dcs = BTRFS_DC_SETUP;
3155 goto out_put;
3156 }
3157
Josef Bacik0af3d002010-06-21 14:48:16 -04003158 /*
3159 * We want to set the generation to 0, that way if anything goes wrong
3160 * from here on out we know not to trust this cache when we load up next
3161 * time.
3162 */
3163 BTRFS_I(inode)->generation = 0;
3164 ret = btrfs_update_inode(trans, root, inode);
3165 WARN_ON(ret);
3166
3167 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003168 ret = btrfs_check_trunc_cache_free_space(root,
3169 &root->fs_info->global_block_rsv);
3170 if (ret)
3171 goto out_put;
3172
Filipe David Borba Manana74514322013-09-20 14:46:51 +01003173 ret = btrfs_truncate_free_space_cache(root, trans, inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04003174 if (ret)
3175 goto out_put;
3176 }
3177
3178 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003179 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3180 !btrfs_test_opt(root, SPACE_CACHE)) {
3181 /*
3182 * don't bother trying to write stuff out _if_
3183 * a) we're not cached,
3184 * b) we're with nospace_cache mount option.
3185 */
Josef Bacik2b209822010-12-03 13:17:53 -05003186 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003187 spin_unlock(&block_group->lock);
3188 goto out_put;
3189 }
3190 spin_unlock(&block_group->lock);
3191
Josef Bacik6fc823b2012-08-06 13:46:38 -06003192 /*
3193 * Try to preallocate enough space based on how big the block group is.
3194 * Keep in mind this has to include any pinned space which could end up
3195 * taking up quite a bit since it's not folded into the other space
3196 * cache.
3197 */
3198 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003199 if (!num_pages)
3200 num_pages = 1;
3201
Josef Bacik0af3d002010-06-21 14:48:16 -04003202 num_pages *= 16;
3203 num_pages *= PAGE_CACHE_SIZE;
3204
3205 ret = btrfs_check_data_free_space(inode, num_pages);
3206 if (ret)
3207 goto out_put;
3208
3209 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3210 num_pages, num_pages,
3211 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003212 if (!ret)
3213 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003214 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003215
Josef Bacik0af3d002010-06-21 14:48:16 -04003216out_put:
3217 iput(inode);
3218out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003219 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003220out:
3221 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003222 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003223 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003224 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003225 spin_unlock(&block_group->lock);
3226
3227 return ret;
3228}
3229
Chris Mason96b51792007-10-15 16:15:19 -04003230int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3231 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003232{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003233 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003234 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003235 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003236 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003237
3238 path = btrfs_alloc_path();
3239 if (!path)
3240 return -ENOMEM;
3241
Josef Bacik0af3d002010-06-21 14:48:16 -04003242again:
3243 while (1) {
3244 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3245 while (cache) {
3246 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3247 break;
3248 cache = next_block_group(root, cache);
3249 }
3250 if (!cache) {
3251 if (last == 0)
3252 break;
3253 last = 0;
3254 continue;
3255 }
3256 err = cache_save_setup(cache, trans, path);
3257 last = cache->key.objectid + cache->key.offset;
3258 btrfs_put_block_group(cache);
3259 }
3260
Chris Masond3977122009-01-05 21:25:51 -05003261 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003262 if (last == 0) {
3263 err = btrfs_run_delayed_refs(trans, root,
3264 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003265 if (err) /* File system offline */
3266 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003267 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003268
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003269 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3270 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003271 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3272 btrfs_put_block_group(cache);
3273 goto again;
3274 }
3275
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003276 if (cache->dirty)
3277 break;
3278 cache = next_block_group(root, cache);
3279 }
3280 if (!cache) {
3281 if (last == 0)
3282 break;
3283 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003284 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003285 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003286
Josef Bacik0cb59c92010-07-02 12:14:14 -04003287 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3288 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003289 cache->dirty = 0;
3290 last = cache->key.objectid + cache->key.offset;
3291
3292 err = write_one_cache_group(trans, root, path, cache);
Filipe David Borba Mananae84cc142013-09-09 19:49:43 +01003293 btrfs_put_block_group(cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003294 if (err) /* File system offline */
3295 goto out;
Chris Mason9078a3e2007-04-26 16:46:15 -04003296 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003297
Josef Bacik0cb59c92010-07-02 12:14:14 -04003298 while (1) {
3299 /*
3300 * I don't think this is needed since we're just marking our
3301 * preallocated extent as written, but just in case it can't
3302 * hurt.
3303 */
3304 if (last == 0) {
3305 err = btrfs_run_delayed_refs(trans, root,
3306 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003307 if (err) /* File system offline */
3308 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003309 }
3310
3311 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3312 while (cache) {
3313 /*
3314 * Really this shouldn't happen, but it could if we
3315 * couldn't write the entire preallocated extent and
3316 * splitting the extent resulted in a new block.
3317 */
3318 if (cache->dirty) {
3319 btrfs_put_block_group(cache);
3320 goto again;
3321 }
3322 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3323 break;
3324 cache = next_block_group(root, cache);
3325 }
3326 if (!cache) {
3327 if (last == 0)
3328 break;
3329 last = 0;
3330 continue;
3331 }
3332
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003333 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003334
3335 /*
3336 * If we didn't have an error then the cache state is still
3337 * NEED_WRITE, so we can set it to WRITTEN.
3338 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003339 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003340 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3341 last = cache->key.objectid + cache->key.offset;
3342 btrfs_put_block_group(cache);
3343 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003344out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003345
Chris Mason9078a3e2007-04-26 16:46:15 -04003346 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003347 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003348}
3349
Yan Zhengd2fb3432008-12-11 16:30:39 -05003350int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3351{
3352 struct btrfs_block_group_cache *block_group;
3353 int readonly = 0;
3354
3355 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3356 if (!block_group || block_group->ro)
3357 readonly = 1;
3358 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003359 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003360 return readonly;
3361}
3362
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003363static const char *alloc_name(u64 flags)
3364{
3365 switch (flags) {
3366 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3367 return "mixed";
3368 case BTRFS_BLOCK_GROUP_METADATA:
3369 return "metadata";
3370 case BTRFS_BLOCK_GROUP_DATA:
3371 return "data";
3372 case BTRFS_BLOCK_GROUP_SYSTEM:
3373 return "system";
3374 default:
3375 WARN_ON(1);
3376 return "invalid-combination";
3377 };
3378}
3379
Chris Mason593060d2008-03-25 16:50:33 -04003380static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3381 u64 total_bytes, u64 bytes_used,
3382 struct btrfs_space_info **space_info)
3383{
3384 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003385 int i;
3386 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003387 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003388
3389 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3390 BTRFS_BLOCK_GROUP_RAID10))
3391 factor = 2;
3392 else
3393 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003394
3395 found = __find_space_info(info, flags);
3396 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003397 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003398 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003399 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003400 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003401 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003402 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003403 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003404 *space_info = found;
3405 return 0;
3406 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003407 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003408 if (!found)
3409 return -ENOMEM;
3410
Josef Bacikb150a4f2013-06-19 15:00:04 -04003411 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3412 if (ret) {
3413 kfree(found);
3414 return ret;
3415 }
3416
Miao Xie536cd962013-12-17 12:01:12 +08003417 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04003418 INIT_LIST_HEAD(&found->block_groups[i]);
Miao Xie536cd962013-12-17 12:01:12 +08003419 kobject_init(&found->block_group_kobjs[i], &btrfs_raid_ktype);
3420 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003421 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003422 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003423 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003424 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003425 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003426 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003427 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003428 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003429 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003430 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003431 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003432 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003433 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003434 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003435 found->flush = 0;
3436 init_waitqueue_head(&found->wait);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003437
3438 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3439 info->space_info_kobj, "%s",
3440 alloc_name(found->flags));
3441 if (ret) {
3442 kfree(found);
3443 return ret;
3444 }
3445
Chris Mason593060d2008-03-25 16:50:33 -04003446 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003447 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003448 if (flags & BTRFS_BLOCK_GROUP_DATA)
3449 info->data_sinfo = found;
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04003450
3451 return ret;
Chris Mason593060d2008-03-25 16:50:33 -04003452}
3453
Chris Mason8790d502008-04-03 16:29:03 -04003454static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3455{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003456 u64 extra_flags = chunk_to_extended(flags) &
3457 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003458
Miao Xiede98ced2013-01-29 10:13:12 +00003459 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003460 if (flags & BTRFS_BLOCK_GROUP_DATA)
3461 fs_info->avail_data_alloc_bits |= extra_flags;
3462 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3463 fs_info->avail_metadata_alloc_bits |= extra_flags;
3464 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3465 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003466 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003467}
Chris Mason593060d2008-03-25 16:50:33 -04003468
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003469/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003470 * returns target flags in extended format or 0 if restripe for this
3471 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003472 *
3473 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003474 */
3475static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3476{
3477 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3478 u64 target = 0;
3479
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003480 if (!bctl)
3481 return 0;
3482
3483 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3484 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3485 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3486 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3487 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3488 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3489 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3490 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3491 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3492 }
3493
3494 return target;
3495}
3496
3497/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003498 * @flags: available profiles in extended format (see ctree.h)
3499 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003500 * Returns reduced profile in chunk format. If profile changing is in
3501 * progress (either running or paused) picks the target profile (if it's
3502 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003503 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003504static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003505{
Chris Masoncd02dca2010-12-13 14:56:23 -05003506 /*
3507 * we add in the count of missing devices because we want
3508 * to make sure that any RAID levels on a degraded FS
3509 * continue to be honored.
3510 */
3511 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3512 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003513 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003514 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003515
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003516 /*
3517 * see if restripe for this chunk_type is in progress, if so
3518 * try to reduce to the target profile
3519 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003520 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003521 target = get_restripe_target(root->fs_info, flags);
3522 if (target) {
3523 /* pick target profile only if it's already available */
3524 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003525 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003526 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003527 }
3528 }
3529 spin_unlock(&root->fs_info->balance_lock);
3530
David Woodhouse53b381b2013-01-29 18:40:14 -05003531 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003532 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003533 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3534 BTRFS_BLOCK_GROUP_RAID5);
3535 if (num_devices < 3)
3536 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003537 if (num_devices < 4)
3538 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3539
David Woodhouse53b381b2013-01-29 18:40:14 -05003540 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3541 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3542 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3543 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003544
David Woodhouse53b381b2013-01-29 18:40:14 -05003545 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3546 tmp = BTRFS_BLOCK_GROUP_RAID6;
3547 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3548 tmp = BTRFS_BLOCK_GROUP_RAID5;
3549 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3550 tmp = BTRFS_BLOCK_GROUP_RAID10;
3551 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3552 tmp = BTRFS_BLOCK_GROUP_RAID1;
3553 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3554 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003555
David Woodhouse53b381b2013-01-29 18:40:14 -05003556 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003557}
3558
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003559static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003560{
Miao Xiede98ced2013-01-29 10:13:12 +00003561 unsigned seq;
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003562 u64 flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003563
3564 do {
Filipe Mananaf8213bd2014-04-24 15:15:29 +01003565 flags = orig_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003566 seq = read_seqbegin(&root->fs_info->profiles_lock);
3567
3568 if (flags & BTRFS_BLOCK_GROUP_DATA)
3569 flags |= root->fs_info->avail_data_alloc_bits;
3570 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3571 flags |= root->fs_info->avail_system_alloc_bits;
3572 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3573 flags |= root->fs_info->avail_metadata_alloc_bits;
3574 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003575
Yan, Zhengb742bb82010-05-16 10:46:24 -04003576 return btrfs_reduce_alloc_profile(root, flags);
3577}
Josef Bacik6a632092009-02-20 11:00:09 -05003578
Miao Xie6d07bce2011-01-05 10:07:31 +00003579u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003580{
3581 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003582 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003583
Yan, Zhengb742bb82010-05-16 10:46:24 -04003584 if (data)
3585 flags = BTRFS_BLOCK_GROUP_DATA;
3586 else if (root == root->fs_info->chunk_root)
3587 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3588 else
3589 flags = BTRFS_BLOCK_GROUP_METADATA;
3590
David Woodhouse53b381b2013-01-29 18:40:14 -05003591 ret = get_alloc_profile(root, flags);
3592 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003593}
3594
Josef Bacik6a632092009-02-20 11:00:09 -05003595/*
3596 * This will check the space that the inode allocates from to make sure we have
3597 * enough space for bytes.
3598 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003599int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003600{
3601 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003602 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003603 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003604 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003605 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003606
3607 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003608 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003609
Miao Xie9dced182013-10-25 17:33:36 +08003610 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003611 committed = 1;
Miao Xie9dced182013-10-25 17:33:36 +08003612 ASSERT(current->journal_info);
Josef Bacik0af3d002010-06-21 14:48:16 -04003613 }
3614
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003615 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003616 if (!data_sinfo)
3617 goto alloc;
3618
Josef Bacik6a632092009-02-20 11:00:09 -05003619again:
3620 /* make sure we have enough space to handle the data first */
3621 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003622 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3623 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3624 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003625
3626 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003627 struct btrfs_trans_handle *trans;
3628
Josef Bacik6a632092009-02-20 11:00:09 -05003629 /*
3630 * if we don't have enough free bytes in this space then we need
3631 * to alloc a new chunk.
3632 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003633 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003634 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003635
Chris Mason0e4f8f82011-04-15 16:05:44 -04003636 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003637 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003638alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003639 alloc_target = btrfs_get_alloc_profile(root, 1);
Miao Xie9dced182013-10-25 17:33:36 +08003640 /*
3641 * It is ugly that we don't call nolock join
3642 * transaction for the free space inode case here.
3643 * But it is safe because we only do the data space
3644 * reservation for the free space cache in the
3645 * transaction context, the common join transaction
3646 * just increase the counter of the current transaction
3647 * handler, doesn't try to acquire the trans_lock of
3648 * the fs.
3649 */
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003650 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003651 if (IS_ERR(trans))
3652 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003653
3654 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003655 alloc_target,
3656 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003657 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003658 if (ret < 0) {
3659 if (ret != -ENOSPC)
3660 return ret;
3661 else
3662 goto commit_trans;
3663 }
Chris Mason33b4d472009-09-22 14:45:50 -04003664
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003665 if (!data_sinfo)
3666 data_sinfo = fs_info->data_sinfo;
3667
Josef Bacik6a632092009-02-20 11:00:09 -05003668 goto again;
3669 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003670
3671 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003672 * If we don't have enough pinned space to deal with this
3673 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003674 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003675 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3676 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003677 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003678 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003679
3680 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003681commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003682 if (!committed &&
3683 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003684 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003685
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003686 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687 if (IS_ERR(trans))
3688 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003689 ret = btrfs_commit_transaction(trans, root);
3690 if (ret)
3691 return ret;
3692 goto again;
3693 }
3694
Jeff Mahoneycab45e22013-10-16 16:27:01 -04003695 trace_btrfs_space_reservation(root->fs_info,
3696 "space_info:enospc",
3697 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003698 return -ENOSPC;
3699 }
3700 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003701 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003702 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003703 spin_unlock(&data_sinfo->lock);
3704
Josef Bacik9ed74f22009-09-11 16:12:44 -04003705 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003706}
3707
3708/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003709 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003710 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003711void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003712{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003713 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003714 struct btrfs_space_info *data_sinfo;
3715
3716 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003717 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003718
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003719 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003720 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003721 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003722 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003723 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003724 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003725 spin_unlock(&data_sinfo->lock);
3726}
3727
Josef Bacik97e728d2009-04-21 17:40:57 -04003728static void force_metadata_allocation(struct btrfs_fs_info *info)
3729{
3730 struct list_head *head = &info->space_info;
3731 struct btrfs_space_info *found;
3732
3733 rcu_read_lock();
3734 list_for_each_entry_rcu(found, head, list) {
3735 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003736 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003737 }
3738 rcu_read_unlock();
3739}
3740
Miao Xie3c76cd82013-04-25 10:12:38 +00003741static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3742{
3743 return (global->size << 1);
3744}
3745
Chris Masone5bc2452010-10-26 13:37:56 -04003746static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003747 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003748{
Josef Bacikfb25e912011-07-26 17:00:46 -04003749 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003750 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003751 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003752 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003753
Chris Mason0e4f8f82011-04-15 16:05:44 -04003754 if (force == CHUNK_ALLOC_FORCE)
3755 return 1;
3756
3757 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003758 * We need to take into account the global rsv because for all intents
3759 * and purposes it's used space. Don't worry about locking the
3760 * global_rsv, it doesn't change except when the transaction commits.
3761 */
Josef Bacik54338b52012-08-14 16:20:52 -04003762 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003763 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003764
3765 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003766 * in limited mode, we want to have some free space up to
3767 * about 1% of the FS size.
3768 */
3769 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003770 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003771 thresh = max_t(u64, 64 * 1024 * 1024,
3772 div_factor_fine(thresh, 1));
3773
3774 if (num_bytes - num_allocated < thresh)
3775 return 1;
3776 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003777
Josef Bacik698d0082012-09-12 14:08:47 -04003778 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003779 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003780 return 1;
3781}
3782
Liu Bo15d1ff82012-03-29 09:57:44 -04003783static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3784{
3785 u64 num_dev;
3786
David Woodhouse53b381b2013-01-29 18:40:14 -05003787 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3788 BTRFS_BLOCK_GROUP_RAID0 |
3789 BTRFS_BLOCK_GROUP_RAID5 |
3790 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003791 num_dev = root->fs_info->fs_devices->rw_devices;
3792 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3793 num_dev = 2;
3794 else
3795 num_dev = 1; /* DUP or single */
3796
3797 /* metadata for updaing devices and chunk tree */
3798 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3799}
3800
3801static void check_system_chunk(struct btrfs_trans_handle *trans,
3802 struct btrfs_root *root, u64 type)
3803{
3804 struct btrfs_space_info *info;
3805 u64 left;
3806 u64 thresh;
3807
3808 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3809 spin_lock(&info->lock);
3810 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3811 info->bytes_reserved - info->bytes_readonly;
3812 spin_unlock(&info->lock);
3813
3814 thresh = get_system_chunk_thresh(root, type);
3815 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003816 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3817 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003818 dump_space_info(info, 0, 0);
3819 }
3820
3821 if (left < thresh) {
3822 u64 flags;
3823
3824 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3825 btrfs_alloc_chunk(trans, root, flags);
3826 }
3827}
3828
Chris Mason6324fbf2008-03-24 15:01:59 -04003829static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003830 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003831{
3832 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003833 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003834 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003835 int ret = 0;
3836
Josef Bacikc6b305a2012-12-18 09:16:16 -05003837 /* Don't re-enter if we're already allocating a chunk */
3838 if (trans->allocating_chunk)
3839 return -ENOSPC;
3840
Chris Mason6324fbf2008-03-24 15:01:59 -04003841 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003842 if (!space_info) {
3843 ret = update_space_info(extent_root->fs_info, flags,
3844 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003845 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003846 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003847 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003848
Josef Bacik6d741192011-04-11 20:20:11 -04003849again:
Josef Bacik25179202008-10-29 14:49:05 -04003850 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003851 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003852 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003853 if (space_info->full) {
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003854 if (should_alloc_chunk(extent_root, space_info, force))
3855 ret = -ENOSPC;
3856 else
3857 ret = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003858 spin_unlock(&space_info->lock);
Filipe David Borba Manana09fb99a2013-08-05 16:25:12 +01003859 return ret;
Josef Bacik25179202008-10-29 14:49:05 -04003860 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003861
Josef Bacik698d0082012-09-12 14:08:47 -04003862 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003863 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003864 return 0;
3865 } else if (space_info->chunk_alloc) {
3866 wait_for_alloc = 1;
3867 } else {
3868 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003869 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003870
Josef Bacik25179202008-10-29 14:49:05 -04003871 spin_unlock(&space_info->lock);
3872
Josef Bacik6d741192011-04-11 20:20:11 -04003873 mutex_lock(&fs_info->chunk_mutex);
3874
3875 /*
3876 * The chunk_mutex is held throughout the entirety of a chunk
3877 * allocation, so once we've acquired the chunk_mutex we know that the
3878 * other guy is done and we need to recheck and see if we should
3879 * allocate.
3880 */
3881 if (wait_for_alloc) {
3882 mutex_unlock(&fs_info->chunk_mutex);
3883 wait_for_alloc = 0;
3884 goto again;
3885 }
3886
Josef Bacikc6b305a2012-12-18 09:16:16 -05003887 trans->allocating_chunk = true;
3888
Josef Bacik97e728d2009-04-21 17:40:57 -04003889 /*
Josef Bacik67377732010-09-16 16:19:09 -04003890 * If we have mixed data/metadata chunks we want to make sure we keep
3891 * allocating mixed chunks instead of individual chunks.
3892 */
3893 if (btrfs_mixed_space_info(space_info))
3894 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3895
3896 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003897 * if we're doing a data chunk, go ahead and make sure that
3898 * we keep a reasonable number of metadata chunks allocated in the
3899 * FS as well.
3900 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003901 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003902 fs_info->data_chunk_allocations++;
3903 if (!(fs_info->data_chunk_allocations %
3904 fs_info->metadata_ratio))
3905 force_metadata_allocation(fs_info);
3906 }
3907
Liu Bo15d1ff82012-03-29 09:57:44 -04003908 /*
3909 * Check if we have enough space in SYSTEM chunk because we may need
3910 * to update devices.
3911 */
3912 check_system_chunk(trans, extent_root, flags);
3913
Yan Zheng2b820322008-11-17 21:11:30 -05003914 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003915 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003916
Josef Bacik9ed74f22009-09-11 16:12:44 -04003917 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003918 if (ret < 0 && ret != -ENOSPC)
3919 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003920 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003921 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003922 else
3923 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003924
Chris Mason0e4f8f82011-04-15 16:05:44 -04003925 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003926out:
Josef Bacik6d741192011-04-11 20:20:11 -04003927 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003928 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003929 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003930 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003931}
3932
Josef Bacika80c8dc2012-09-06 16:59:33 -04003933static int can_overcommit(struct btrfs_root *root,
3934 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003935 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003936{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003937 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003938 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003939 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003940 u64 avail;
3941 u64 used;
3942
3943 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003944 space_info->bytes_pinned + space_info->bytes_readonly;
3945
Josef Bacik96f1bb52013-01-30 17:02:51 -05003946 /*
3947 * We only want to allow over committing if we have lots of actual space
3948 * free, but if we don't have enough space to handle the global reserve
3949 * space then we could end up having a real enospc problem when trying
3950 * to allocate a chunk or some other such important allocation.
3951 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003952 spin_lock(&global_rsv->lock);
3953 space_size = calc_global_rsv_need_space(global_rsv);
3954 spin_unlock(&global_rsv->lock);
3955 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003956 return 0;
3957
3958 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003959
3960 spin_lock(&root->fs_info->free_chunk_lock);
3961 avail = root->fs_info->free_chunk_space;
3962 spin_unlock(&root->fs_info->free_chunk_lock);
3963
3964 /*
3965 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003966 * space is actually useable. For raid56, the space info used
3967 * doesn't include the parity drive, so we don't have to
3968 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003969 */
3970 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3971 BTRFS_BLOCK_GROUP_RAID1 |
3972 BTRFS_BLOCK_GROUP_RAID10))
3973 avail >>= 1;
3974
3975 /*
Miao Xie561c2942012-10-16 11:32:18 +00003976 * If we aren't flushing all things, let us overcommit up to
3977 * 1/2th of the space. If we can flush, don't let us overcommit
3978 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003979 */
Miao Xie08e007d2012-10-16 11:33:38 +00003980 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik14575ae2013-09-17 10:48:00 -04003981 avail >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003982 else
Josef Bacik14575ae2013-09-17 10:48:00 -04003983 avail >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003984
Josef Bacik14575ae2013-09-17 10:48:00 -04003985 if (used + bytes < space_info->total_bytes + avail)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003986 return 1;
3987 return 0;
3988}
3989
Eric Sandeen48a3b632013-04-25 20:41:01 +00003990static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
Miao Xie6c255e62014-03-06 13:55:01 +08003991 unsigned long nr_pages, int nr_items)
Miao Xieda633a42012-12-20 11:19:09 +00003992{
3993 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00003994
Josef Bacik925a6ef2013-06-20 12:31:27 -04003995 if (down_read_trylock(&sb->s_umount)) {
3996 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
3997 up_read(&sb->s_umount);
3998 } else {
Miao Xieda633a42012-12-20 11:19:09 +00003999 /*
4000 * We needn't worry the filesystem going from r/w to r/o though
4001 * we don't acquire ->s_umount mutex, because the filesystem
4002 * should guarantee the delalloc inodes list be empty after
4003 * the filesystem is readonly(all dirty pages are written to
4004 * the disk).
4005 */
Miao Xie6c255e62014-03-06 13:55:01 +08004006 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
Josef Bacik98ad69c2013-04-04 11:55:49 -04004007 if (!current->journal_info)
Miao Xie6c255e62014-03-06 13:55:01 +08004008 btrfs_wait_ordered_roots(root->fs_info, nr_items);
Miao Xieda633a42012-12-20 11:19:09 +00004009 }
4010}
4011
Miao Xie18cd8ea2013-11-04 23:13:22 +08004012static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4013{
4014 u64 bytes;
4015 int nr;
4016
4017 bytes = btrfs_calc_trans_metadata_size(root, 1);
4018 nr = (int)div64_u64(to_reclaim, bytes);
4019 if (!nr)
4020 nr = 1;
4021 return nr;
4022}
4023
Miao Xiec61a16a2013-11-04 23:13:23 +08004024#define EXTENT_SIZE_PER_ITEM (256 * 1024)
4025
Yan, Zheng5da9d012010-05-16 10:46:25 -04004026/*
4027 * shrink metadata reservation for delalloc
4028 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04004029static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4030 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004031{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004032 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004033 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004034 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004035 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004036 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004037 long time_left;
Miao Xied3ee29e32013-11-04 23:13:20 +08004038 unsigned long nr_pages;
4039 int loops;
Miao Xieb0244192013-11-04 23:13:25 +08004040 int items;
Miao Xie08e007d2012-10-16 11:33:38 +00004041 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004042
Miao Xiec61a16a2013-11-04 23:13:23 +08004043 /* Calc the number of the pages we need flush for space reservation */
Miao Xieb0244192013-11-04 23:13:25 +08004044 items = calc_reclaim_items_nr(root, to_reclaim);
4045 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
Miao Xiec61a16a2013-11-04 23:13:23 +08004046
Josef Bacik663350a2011-11-03 22:54:25 -04004047 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004048 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004049 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004050
Miao Xie963d6782013-01-29 10:10:51 +00004051 delalloc_bytes = percpu_counter_sum_positive(
4052 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004053 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004054 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004055 return;
Miao Xie38c135a2013-11-04 23:13:21 +08004056 if (wait_ordered)
Miao Xieb0244192013-11-04 23:13:25 +08004057 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004058 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004059 }
4060
Miao Xied3ee29e32013-11-04 23:13:20 +08004061 loops = 0;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004062 while (delalloc_bytes && loops < 3) {
4063 max_reclaim = min(delalloc_bytes, to_reclaim);
4064 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xie6c255e62014-03-06 13:55:01 +08004065 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
Josef Bacikdea31f52012-09-06 16:47:00 -04004066 /*
4067 * We need to wait for the async pages to actually start before
4068 * we do anything.
4069 */
Miao Xie9f3a0742013-11-04 23:13:24 +08004070 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4071 if (!max_reclaim)
4072 goto skip_async;
Josef Bacikdea31f52012-09-06 16:47:00 -04004073
Miao Xie9f3a0742013-11-04 23:13:24 +08004074 if (max_reclaim <= nr_pages)
4075 max_reclaim = 0;
4076 else
4077 max_reclaim -= nr_pages;
4078
4079 wait_event(root->fs_info->async_submit_wait,
4080 atomic_read(&root->fs_info->async_delalloc_pages) <=
4081 (int)max_reclaim);
4082skip_async:
Miao Xie08e007d2012-10-16 11:33:38 +00004083 if (!trans)
4084 flush = BTRFS_RESERVE_FLUSH_ALL;
4085 else
4086 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004087 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004088 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004089 spin_unlock(&space_info->lock);
4090 break;
4091 }
Josef Bacik0019f102010-10-15 15:18:40 -04004092 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004093
Chris Mason36e39c42011-03-12 07:08:42 -05004094 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004095 if (wait_ordered && !trans) {
Miao Xieb0244192013-11-04 23:13:25 +08004096 btrfs_wait_ordered_roots(root->fs_info, items);
Josef Bacikf104d042011-10-14 13:56:58 -04004097 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004098 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004099 if (time_left)
4100 break;
4101 }
Miao Xie963d6782013-01-29 10:10:51 +00004102 delalloc_bytes = percpu_counter_sum_positive(
4103 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004104 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004105}
4106
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004107/**
Josef Bacik663350a2011-11-03 22:54:25 -04004108 * maybe_commit_transaction - possibly commit the transaction if its ok to
4109 * @root - the root we're allocating for
4110 * @bytes - the number of bytes we want to reserve
4111 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004112 *
Josef Bacik663350a2011-11-03 22:54:25 -04004113 * This will check to make sure that committing the transaction will actually
4114 * get us somewhere and then commit the transaction if it does. Otherwise it
4115 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004116 */
Josef Bacik663350a2011-11-03 22:54:25 -04004117static int may_commit_transaction(struct btrfs_root *root,
4118 struct btrfs_space_info *space_info,
4119 u64 bytes, int force)
4120{
4121 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4122 struct btrfs_trans_handle *trans;
4123
4124 trans = (struct btrfs_trans_handle *)current->journal_info;
4125 if (trans)
4126 return -EAGAIN;
4127
4128 if (force)
4129 goto commit;
4130
4131 /* See if there is enough pinned space to make this reservation */
Josef Bacikb150a4f2013-06-19 15:00:04 -04004132 if (percpu_counter_compare(&space_info->total_bytes_pinned,
Miao Xie0424c542014-03-06 13:54:59 +08004133 bytes) >= 0)
Josef Bacik663350a2011-11-03 22:54:25 -04004134 goto commit;
Josef Bacik663350a2011-11-03 22:54:25 -04004135
4136 /*
4137 * See if there is some space in the delayed insertion reservation for
4138 * this reservation.
4139 */
4140 if (space_info != delayed_rsv->space_info)
4141 return -ENOSPC;
4142
4143 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004144 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4145 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004146 spin_unlock(&delayed_rsv->lock);
4147 return -ENOSPC;
4148 }
4149 spin_unlock(&delayed_rsv->lock);
4150
4151commit:
4152 trans = btrfs_join_transaction(root);
4153 if (IS_ERR(trans))
4154 return -ENOSPC;
4155
4156 return btrfs_commit_transaction(trans, root);
4157}
4158
Josef Bacik96c3f432012-06-21 14:05:49 -04004159enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004160 FLUSH_DELAYED_ITEMS_NR = 1,
4161 FLUSH_DELAYED_ITEMS = 2,
4162 FLUSH_DELALLOC = 3,
4163 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004164 ALLOC_CHUNK = 5,
4165 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004166};
4167
4168static int flush_space(struct btrfs_root *root,
4169 struct btrfs_space_info *space_info, u64 num_bytes,
4170 u64 orig_bytes, int state)
4171{
4172 struct btrfs_trans_handle *trans;
4173 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004174 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004175
4176 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004177 case FLUSH_DELAYED_ITEMS_NR:
4178 case FLUSH_DELAYED_ITEMS:
Miao Xie18cd8ea2013-11-04 23:13:22 +08004179 if (state == FLUSH_DELAYED_ITEMS_NR)
4180 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4181 else
Josef Bacik96c3f432012-06-21 14:05:49 -04004182 nr = -1;
Miao Xie18cd8ea2013-11-04 23:13:22 +08004183
Josef Bacik96c3f432012-06-21 14:05:49 -04004184 trans = btrfs_join_transaction(root);
4185 if (IS_ERR(trans)) {
4186 ret = PTR_ERR(trans);
4187 break;
4188 }
4189 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4190 btrfs_end_transaction(trans, root);
4191 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004192 case FLUSH_DELALLOC:
4193 case FLUSH_DELALLOC_WAIT:
Miao Xie24af7dd2014-03-06 13:55:00 +08004194 shrink_delalloc(root, num_bytes * 2, orig_bytes,
Josef Bacik67b0fd62012-09-24 13:42:00 -04004195 state == FLUSH_DELALLOC_WAIT);
4196 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004197 case ALLOC_CHUNK:
4198 trans = btrfs_join_transaction(root);
4199 if (IS_ERR(trans)) {
4200 ret = PTR_ERR(trans);
4201 break;
4202 }
4203 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004204 btrfs_get_alloc_profile(root, 0),
4205 CHUNK_ALLOC_NO_FORCE);
4206 btrfs_end_transaction(trans, root);
4207 if (ret == -ENOSPC)
4208 ret = 0;
4209 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004210 case COMMIT_TRANS:
4211 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4212 break;
4213 default:
4214 ret = -ENOSPC;
4215 break;
4216 }
4217
4218 return ret;
4219}
Miao Xie21c7e752014-05-13 17:29:04 -07004220
4221static inline u64
4222btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4223 struct btrfs_space_info *space_info)
4224{
4225 u64 used;
4226 u64 expected;
4227 u64 to_reclaim;
4228
4229 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4230 16 * 1024 * 1024);
4231 spin_lock(&space_info->lock);
4232 if (can_overcommit(root, space_info, to_reclaim,
4233 BTRFS_RESERVE_FLUSH_ALL)) {
4234 to_reclaim = 0;
4235 goto out;
4236 }
4237
4238 used = space_info->bytes_used + space_info->bytes_reserved +
4239 space_info->bytes_pinned + space_info->bytes_readonly +
4240 space_info->bytes_may_use;
4241 if (can_overcommit(root, space_info, 1024 * 1024,
4242 BTRFS_RESERVE_FLUSH_ALL))
4243 expected = div_factor_fine(space_info->total_bytes, 95);
4244 else
4245 expected = div_factor_fine(space_info->total_bytes, 90);
4246
4247 if (used > expected)
4248 to_reclaim = used - expected;
4249 else
4250 to_reclaim = 0;
4251 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4252 space_info->bytes_reserved);
4253out:
4254 spin_unlock(&space_info->lock);
4255
4256 return to_reclaim;
4257}
4258
4259static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4260 struct btrfs_fs_info *fs_info, u64 used)
4261{
4262 return (used >= div_factor_fine(space_info->total_bytes, 98) &&
4263 !btrfs_fs_closing(fs_info) &&
4264 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4265}
4266
4267static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
4268 struct btrfs_fs_info *fs_info)
4269{
4270 u64 used;
4271
4272 spin_lock(&space_info->lock);
4273 used = space_info->bytes_used + space_info->bytes_reserved +
4274 space_info->bytes_pinned + space_info->bytes_readonly +
4275 space_info->bytes_may_use;
4276 if (need_do_async_reclaim(space_info, fs_info, used)) {
4277 spin_unlock(&space_info->lock);
4278 return 1;
4279 }
4280 spin_unlock(&space_info->lock);
4281
4282 return 0;
4283}
4284
4285static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4286{
4287 struct btrfs_fs_info *fs_info;
4288 struct btrfs_space_info *space_info;
4289 u64 to_reclaim;
4290 int flush_state;
4291
4292 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4293 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4294
4295 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4296 space_info);
4297 if (!to_reclaim)
4298 return;
4299
4300 flush_state = FLUSH_DELAYED_ITEMS_NR;
4301 do {
4302 flush_space(fs_info->fs_root, space_info, to_reclaim,
4303 to_reclaim, flush_state);
4304 flush_state++;
4305 if (!btrfs_need_do_async_reclaim(space_info, fs_info))
4306 return;
4307 } while (flush_state <= COMMIT_TRANS);
4308
4309 if (btrfs_need_do_async_reclaim(space_info, fs_info))
4310 queue_work(system_unbound_wq, work);
4311}
4312
4313void btrfs_init_async_reclaim_work(struct work_struct *work)
4314{
4315 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4316}
4317
Josef Bacik663350a2011-11-03 22:54:25 -04004318/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004319 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4320 * @root - the root we're allocating for
4321 * @block_rsv - the block_rsv we're allocating for
4322 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004323 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004324 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004325 * This will reserve orgi_bytes number of bytes from the space info associated
4326 * with the block_rsv. If there is not enough space it will make an attempt to
4327 * flush out space to make room. It will do this by flushing delalloc if
4328 * possible or committing the transaction. If flush is 0 then no attempts to
4329 * regain reservations will be made and this will fail if there is not enough
4330 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004331 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004332static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004333 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004334 u64 orig_bytes,
4335 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004336{
4337 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004338 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004339 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004340 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004341 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004342 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004343
4344again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004345 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004346 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004347 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004348 * We only want to wait if somebody other than us is flushing and we
4349 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004350 */
Miao Xie08e007d2012-10-16 11:33:38 +00004351 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4352 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004353 spin_unlock(&space_info->lock);
4354 /*
4355 * If we have a trans handle we can't wait because the flusher
4356 * may have to commit the transaction, which would mean we would
4357 * deadlock since we are waiting for the flusher to finish, but
4358 * hold the current transaction open.
4359 */
Josef Bacik663350a2011-11-03 22:54:25 -04004360 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004361 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004362 ret = wait_event_killable(space_info->wait, !space_info->flush);
4363 /* Must have been killed, return */
4364 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004365 return -EINTR;
4366
4367 spin_lock(&space_info->lock);
4368 }
4369
4370 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004371 used = space_info->bytes_used + space_info->bytes_reserved +
4372 space_info->bytes_pinned + space_info->bytes_readonly +
4373 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004374
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004375 /*
4376 * The idea here is that we've not already over-reserved the block group
4377 * then we can go ahead and save our reservation first and then start
4378 * flushing if we need to. Otherwise if we've already overcommitted
4379 * lets start flushing stuff first and then come back and try to make
4380 * our reservation.
4381 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004382 if (used <= space_info->total_bytes) {
4383 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004384 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004385 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004386 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004387 ret = 0;
4388 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004389 /*
4390 * Ok set num_bytes to orig_bytes since we aren't
4391 * overocmmitted, this way we only try and reclaim what
4392 * we need.
4393 */
4394 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004395 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004396 } else {
4397 /*
4398 * Ok we're over committed, set num_bytes to the overcommitted
4399 * amount plus the amount of bytes that we need for this
4400 * reservation.
4401 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004402 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004403 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004404 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004405
Josef Bacik44734ed2012-09-28 16:04:19 -04004406 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4407 space_info->bytes_may_use += orig_bytes;
4408 trace_btrfs_space_reservation(root->fs_info, "space_info",
4409 space_info->flags, orig_bytes,
4410 1);
4411 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004412 }
4413
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004414 /*
4415 * Couldn't make our reservation, save our place so while we're trying
4416 * to reclaim space we can actually use it instead of somebody else
4417 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004418 *
4419 * We make the other tasks wait for the flush only when we can flush
4420 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004421 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004422 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004423 flushing = true;
4424 space_info->flush = 1;
Miao Xie21c7e752014-05-13 17:29:04 -07004425 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4426 used += orig_bytes;
4427 if (need_do_async_reclaim(space_info, root->fs_info, used) &&
4428 !work_busy(&root->fs_info->async_reclaim_work))
4429 queue_work(system_unbound_wq,
4430 &root->fs_info->async_reclaim_work);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004431 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004432 spin_unlock(&space_info->lock);
4433
Miao Xie08e007d2012-10-16 11:33:38 +00004434 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004435 goto out;
4436
Josef Bacik96c3f432012-06-21 14:05:49 -04004437 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4438 flush_state);
4439 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004440
4441 /*
4442 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4443 * would happen. So skip delalloc flush.
4444 */
4445 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4446 (flush_state == FLUSH_DELALLOC ||
4447 flush_state == FLUSH_DELALLOC_WAIT))
4448 flush_state = ALLOC_CHUNK;
4449
Josef Bacik96c3f432012-06-21 14:05:49 -04004450 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004451 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004452 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4453 flush_state < COMMIT_TRANS)
4454 goto again;
4455 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4456 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004457 goto again;
4458
4459out:
Josef Bacik5d803662013-02-07 16:06:02 -05004460 if (ret == -ENOSPC &&
4461 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4462 struct btrfs_block_rsv *global_rsv =
4463 &root->fs_info->global_block_rsv;
4464
4465 if (block_rsv != global_rsv &&
4466 !block_rsv_use_bytes(global_rsv, orig_bytes))
4467 ret = 0;
4468 }
Jeff Mahoneycab45e22013-10-16 16:27:01 -04004469 if (ret == -ENOSPC)
4470 trace_btrfs_space_reservation(root->fs_info,
4471 "space_info:enospc",
4472 space_info->flags, orig_bytes, 1);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004473 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004474 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004475 space_info->flush = 0;
4476 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004477 spin_unlock(&space_info->lock);
4478 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004479 return ret;
4480}
4481
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004482static struct btrfs_block_rsv *get_block_rsv(
4483 const struct btrfs_trans_handle *trans,
4484 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004485{
Josef Bacik4c13d752011-08-30 11:31:29 -04004486 struct btrfs_block_rsv *block_rsv = NULL;
4487
Miao Xie27cdeb72014-04-02 19:51:05 +08004488 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Josef Bacik0e721102012-06-26 16:13:18 -04004489 block_rsv = trans->block_rsv;
4490
4491 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004492 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004493
Stefan Behrensf7a81ea2013-08-15 17:11:19 +02004494 if (root == root->fs_info->uuid_root)
4495 block_rsv = trans->block_rsv;
4496
Josef Bacik4c13d752011-08-30 11:31:29 -04004497 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004498 block_rsv = root->block_rsv;
4499
4500 if (!block_rsv)
4501 block_rsv = &root->fs_info->empty_block_rsv;
4502
4503 return block_rsv;
4504}
4505
4506static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4507 u64 num_bytes)
4508{
4509 int ret = -ENOSPC;
4510 spin_lock(&block_rsv->lock);
4511 if (block_rsv->reserved >= num_bytes) {
4512 block_rsv->reserved -= num_bytes;
4513 if (block_rsv->reserved < block_rsv->size)
4514 block_rsv->full = 0;
4515 ret = 0;
4516 }
4517 spin_unlock(&block_rsv->lock);
4518 return ret;
4519}
4520
4521static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4522 u64 num_bytes, int update_size)
4523{
4524 spin_lock(&block_rsv->lock);
4525 block_rsv->reserved += num_bytes;
4526 if (update_size)
4527 block_rsv->size += num_bytes;
4528 else if (block_rsv->reserved >= block_rsv->size)
4529 block_rsv->full = 1;
4530 spin_unlock(&block_rsv->lock);
4531}
4532
Josef Bacikd52be812013-05-29 14:54:47 -04004533int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4534 struct btrfs_block_rsv *dest, u64 num_bytes,
4535 int min_factor)
4536{
4537 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4538 u64 min_bytes;
4539
4540 if (global_rsv->space_info != dest->space_info)
4541 return -ENOSPC;
4542
4543 spin_lock(&global_rsv->lock);
4544 min_bytes = div_factor(global_rsv->size, min_factor);
4545 if (global_rsv->reserved < min_bytes + num_bytes) {
4546 spin_unlock(&global_rsv->lock);
4547 return -ENOSPC;
4548 }
4549 global_rsv->reserved -= num_bytes;
4550 if (global_rsv->reserved < global_rsv->size)
4551 global_rsv->full = 0;
4552 spin_unlock(&global_rsv->lock);
4553
4554 block_rsv_add_bytes(dest, num_bytes, 1);
4555 return 0;
4556}
4557
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004558static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4559 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004560 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004561{
4562 struct btrfs_space_info *space_info = block_rsv->space_info;
4563
4564 spin_lock(&block_rsv->lock);
4565 if (num_bytes == (u64)-1)
4566 num_bytes = block_rsv->size;
4567 block_rsv->size -= num_bytes;
4568 if (block_rsv->reserved >= block_rsv->size) {
4569 num_bytes = block_rsv->reserved - block_rsv->size;
4570 block_rsv->reserved = block_rsv->size;
4571 block_rsv->full = 1;
4572 } else {
4573 num_bytes = 0;
4574 }
4575 spin_unlock(&block_rsv->lock);
4576
4577 if (num_bytes > 0) {
4578 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004579 spin_lock(&dest->lock);
4580 if (!dest->full) {
4581 u64 bytes_to_add;
4582
4583 bytes_to_add = dest->size - dest->reserved;
4584 bytes_to_add = min(num_bytes, bytes_to_add);
4585 dest->reserved += bytes_to_add;
4586 if (dest->reserved >= dest->size)
4587 dest->full = 1;
4588 num_bytes -= bytes_to_add;
4589 }
4590 spin_unlock(&dest->lock);
4591 }
4592 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004593 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004594 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004595 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004596 space_info->flags, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004597 spin_unlock(&space_info->lock);
4598 }
4599 }
4600}
4601
4602static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4603 struct btrfs_block_rsv *dst, u64 num_bytes)
4604{
4605 int ret;
4606
4607 ret = block_rsv_use_bytes(src, num_bytes);
4608 if (ret)
4609 return ret;
4610
4611 block_rsv_add_bytes(dst, num_bytes, 1);
4612 return 0;
4613}
4614
Miao Xie66d8f3d2012-09-06 04:02:28 -06004615void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004616{
4617 memset(rsv, 0, sizeof(*rsv));
4618 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004619 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004620}
4621
Miao Xie66d8f3d2012-09-06 04:02:28 -06004622struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4623 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004624{
4625 struct btrfs_block_rsv *block_rsv;
4626 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004627
4628 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4629 if (!block_rsv)
4630 return NULL;
4631
Miao Xie66d8f3d2012-09-06 04:02:28 -06004632 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004633 block_rsv->space_info = __find_space_info(fs_info,
4634 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004635 return block_rsv;
4636}
4637
4638void btrfs_free_block_rsv(struct btrfs_root *root,
4639 struct btrfs_block_rsv *rsv)
4640{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004641 if (!rsv)
4642 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004643 btrfs_block_rsv_release(root, rsv, (u64)-1);
4644 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004645}
4646
Miao Xie08e007d2012-10-16 11:33:38 +00004647int btrfs_block_rsv_add(struct btrfs_root *root,
4648 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4649 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004650{
4651 int ret;
4652
4653 if (num_bytes == 0)
4654 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004655
Miao Xie61b520a2011-11-10 20:45:05 -05004656 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004657 if (!ret) {
4658 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4659 return 0;
4660 }
4661
Yan, Zhengf0486c62010-05-16 10:46:25 -04004662 return ret;
4663}
4664
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004665int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004666 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004667{
4668 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004669 int ret = -ENOSPC;
4670
4671 if (!block_rsv)
4672 return 0;
4673
4674 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004675 num_bytes = div_factor(block_rsv->size, min_factor);
4676 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004677 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004678 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004679
Josef Bacik36ba0222011-10-18 12:15:48 -04004680 return ret;
4681}
4682
Miao Xie08e007d2012-10-16 11:33:38 +00004683int btrfs_block_rsv_refill(struct btrfs_root *root,
4684 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4685 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004686{
4687 u64 num_bytes = 0;
4688 int ret = -ENOSPC;
4689
4690 if (!block_rsv)
4691 return 0;
4692
4693 spin_lock(&block_rsv->lock);
4694 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004695 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004696 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004697 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004698 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004699 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004700
Yan, Zhengf0486c62010-05-16 10:46:25 -04004701 if (!ret)
4702 return 0;
4703
Miao Xieaa38a712011-11-18 17:43:00 +08004704 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004705 if (!ret) {
4706 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004707 return 0;
4708 }
4709
Josef Bacik13553e52011-08-08 13:33:21 -04004710 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004711}
4712
4713int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4714 struct btrfs_block_rsv *dst_rsv,
4715 u64 num_bytes)
4716{
4717 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4718}
4719
4720void btrfs_block_rsv_release(struct btrfs_root *root,
4721 struct btrfs_block_rsv *block_rsv,
4722 u64 num_bytes)
4723{
4724 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Liu Bo17504582013-12-29 21:44:50 +08004725 if (global_rsv == block_rsv ||
Yan, Zhengf0486c62010-05-16 10:46:25 -04004726 block_rsv->space_info != global_rsv->space_info)
4727 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004728 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4729 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004730}
4731
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004732/*
4733 * helper to calculate size of global block reservation.
4734 * the desired value is sum of space used by extent tree,
4735 * checksum tree and root tree
4736 */
4737static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4738{
4739 struct btrfs_space_info *sinfo;
4740 u64 num_bytes;
4741 u64 meta_used;
4742 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004743 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004744
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004745 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4746 spin_lock(&sinfo->lock);
4747 data_used = sinfo->bytes_used;
4748 spin_unlock(&sinfo->lock);
4749
4750 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4751 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004752 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4753 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004754 meta_used = sinfo->bytes_used;
4755 spin_unlock(&sinfo->lock);
4756
4757 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4758 csum_size * 2;
4759 num_bytes += div64_u64(data_used + meta_used, 50);
4760
4761 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004762 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004763
4764 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4765}
4766
4767static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4768{
4769 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4770 struct btrfs_space_info *sinfo = block_rsv->space_info;
4771 u64 num_bytes;
4772
4773 num_bytes = calc_global_metadata_size(fs_info);
4774
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004775 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004776 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004777
Josef Bacikfdf30d12013-03-26 15:31:45 -04004778 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004779
4780 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004781 sinfo->bytes_reserved + sinfo->bytes_readonly +
4782 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004783
4784 if (sinfo->total_bytes > num_bytes) {
4785 num_bytes = sinfo->total_bytes - num_bytes;
4786 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004787 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004788 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004789 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004790 }
4791
4792 if (block_rsv->reserved >= block_rsv->size) {
4793 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004794 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004795 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004796 sinfo->flags, num_bytes, 0);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004797 block_rsv->reserved = block_rsv->size;
4798 block_rsv->full = 1;
4799 }
David Sterba182608c2011-05-05 13:13:16 +02004800
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004801 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004802 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004803}
4804
Yan, Zhengf0486c62010-05-16 10:46:25 -04004805static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4806{
4807 struct btrfs_space_info *space_info;
4808
4809 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4810 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004811
4812 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004813 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004814 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004815 fs_info->trans_block_rsv.space_info = space_info;
4816 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004817 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004818
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004819 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4820 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4821 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4822 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004823 if (fs_info->quota_root)
4824 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004825 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004826
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004827 update_global_block_rsv(fs_info);
4828}
4829
4830static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4831{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004832 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4833 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004834 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4835 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4836 WARN_ON(fs_info->trans_block_rsv.size > 0);
4837 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4838 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4839 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004840 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4841 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004842}
4843
Yan, Zhenga22285a2010-05-16 10:48:46 -04004844void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4845 struct btrfs_root *root)
4846{
Josef Bacik0e721102012-06-26 16:13:18 -04004847 if (!trans->block_rsv)
4848 return;
4849
Yan, Zhenga22285a2010-05-16 10:48:46 -04004850 if (!trans->bytes_reserved)
4851 return;
4852
Chris Masone77266e2012-02-24 10:39:05 -05004853 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004854 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004855 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004856 trans->bytes_reserved = 0;
4857}
4858
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004859/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004860int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4861 struct inode *inode)
4862{
4863 struct btrfs_root *root = BTRFS_I(inode)->root;
4864 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4865 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4866
4867 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004868 * We need to hold space in order to delete our orphan item once we've
4869 * added it, so this takes the reservation so we can release it later
4870 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004871 */
Chris Masonff5714c2011-05-28 07:00:39 -04004872 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004873 trace_btrfs_space_reservation(root->fs_info, "orphan",
4874 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004875 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4876}
4877
4878void btrfs_orphan_release_metadata(struct inode *inode)
4879{
4880 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004881 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004882 trace_btrfs_space_reservation(root->fs_info, "orphan",
4883 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004884 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4885}
4886
Miao Xied5c12072013-02-28 10:04:33 +00004887/*
4888 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4889 * root: the root of the parent directory
4890 * rsv: block reservation
4891 * items: the number of items that we need do reservation
4892 * qgroup_reserved: used to return the reserved size in qgroup
4893 *
4894 * This function is used to reserve the space for snapshot/subvolume
4895 * creation and deletion. Those operations are different with the
4896 * common file/directory operations, they change two fs/file trees
4897 * and root tree, the number of items that the qgroup reserves is
4898 * different with the free space reservation. So we can not use
4899 * the space reseravtion mechanism in start_transaction().
4900 */
4901int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4902 struct btrfs_block_rsv *rsv,
4903 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004904 u64 *qgroup_reserved,
4905 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004906{
Miao Xied5c12072013-02-28 10:04:33 +00004907 u64 num_bytes;
4908 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004909 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004910
4911 if (root->fs_info->quota_enabled) {
4912 /* One for parent inode, two for dir entries */
4913 num_bytes = 3 * root->leafsize;
4914 ret = btrfs_qgroup_reserve(root, num_bytes);
4915 if (ret)
4916 return ret;
4917 } else {
4918 num_bytes = 0;
4919 }
4920
4921 *qgroup_reserved = num_bytes;
4922
4923 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4924 rsv->space_info = __find_space_info(root->fs_info,
4925 BTRFS_BLOCK_GROUP_METADATA);
4926 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4927 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004928
4929 if (ret == -ENOSPC && use_global_rsv)
4930 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
4931
Miao Xied5c12072013-02-28 10:04:33 +00004932 if (ret) {
4933 if (*qgroup_reserved)
4934 btrfs_qgroup_free(root, *qgroup_reserved);
4935 }
4936
4937 return ret;
4938}
4939
4940void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4941 struct btrfs_block_rsv *rsv,
4942 u64 qgroup_reserved)
4943{
4944 btrfs_block_rsv_release(root, rsv, (u64)-1);
4945 if (qgroup_reserved)
4946 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004947}
4948
Josef Bacik7709cde2011-08-04 10:25:02 -04004949/**
4950 * drop_outstanding_extent - drop an outstanding extent
4951 * @inode: the inode we're dropping the extent for
4952 *
4953 * This is called when we are freeing up an outstanding extent, either called
4954 * after an error or after an extent is written. This will return the number of
4955 * reserved extents that need to be freed. This must be called with
4956 * BTRFS_I(inode)->lock held.
4957 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004958static unsigned drop_outstanding_extent(struct inode *inode)
4959{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004960 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004961 unsigned dropped_extents = 0;
4962
Josef Bacik9e0baf62011-07-15 15:16:44 +00004963 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4964 BTRFS_I(inode)->outstanding_extents--;
4965
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004966 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004967 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4968 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004969 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004970
Josef Bacik9e0baf62011-07-15 15:16:44 +00004971 /*
4972 * If we have more or the same amount of outsanding extents than we have
4973 * reserved then we need to leave the reserved extents count alone.
4974 */
4975 if (BTRFS_I(inode)->outstanding_extents >=
4976 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004977 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004978
4979 dropped_extents = BTRFS_I(inode)->reserved_extents -
4980 BTRFS_I(inode)->outstanding_extents;
4981 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004982 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004983}
4984
Josef Bacik7709cde2011-08-04 10:25:02 -04004985/**
4986 * calc_csum_metadata_size - return the amount of metada space that must be
4987 * reserved/free'd for the given bytes.
4988 * @inode: the inode we're manipulating
4989 * @num_bytes: the number of bytes in question
4990 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4991 *
4992 * This adjusts the number of csum_bytes in the inode and then returns the
4993 * correct amount of metadata that must either be reserved or freed. We
4994 * calculate how many checksums we can fit into one leaf and then divide the
4995 * number of bytes that will need to be checksumed by this value to figure out
4996 * how many checksums will be required. If we are adding bytes then the number
4997 * may go up and we will return the number of additional bytes that must be
4998 * reserved. If it is going down we will return the number of bytes that must
4999 * be freed.
5000 *
5001 * This must be called with BTRFS_I(inode)->lock held.
5002 */
5003static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5004 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005005{
Josef Bacik7709cde2011-08-04 10:25:02 -04005006 struct btrfs_root *root = BTRFS_I(inode)->root;
5007 u64 csum_size;
5008 int num_csums_per_leaf;
5009 int num_csums;
5010 int old_csums;
5011
5012 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5013 BTRFS_I(inode)->csum_bytes == 0)
5014 return 0;
5015
5016 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5017 if (reserve)
5018 BTRFS_I(inode)->csum_bytes += num_bytes;
5019 else
5020 BTRFS_I(inode)->csum_bytes -= num_bytes;
5021 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
5022 num_csums_per_leaf = (int)div64_u64(csum_size,
5023 sizeof(struct btrfs_csum_item) +
5024 sizeof(struct btrfs_disk_key));
5025 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5026 num_csums = num_csums + num_csums_per_leaf - 1;
5027 num_csums = num_csums / num_csums_per_leaf;
5028
5029 old_csums = old_csums + num_csums_per_leaf - 1;
5030 old_csums = old_csums / num_csums_per_leaf;
5031
5032 /* No change, no need to reserve more */
5033 if (old_csums == num_csums)
5034 return 0;
5035
5036 if (reserve)
5037 return btrfs_calc_trans_metadata_size(root,
5038 num_csums - old_csums);
5039
5040 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005041}
5042
5043int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5044{
5045 struct btrfs_root *root = BTRFS_I(inode)->root;
5046 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005047 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005048 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005049 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05005050 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00005051 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07005052 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005053 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005054 u64 to_free = 0;
5055 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005056
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005057 /* If we are a free space inode we need to not flush since we will be in
5058 * the middle of a transaction commit. We also don't need the delalloc
5059 * mutex since we won't race with anybody. We need this mostly to make
5060 * lockdep shut its filthy mouth.
5061 */
5062 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00005063 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005064 delalloc_lock = false;
5065 }
Josef Bacikc09544e2011-08-30 10:19:10 -04005066
Miao Xie08e007d2012-10-16 11:33:38 +00005067 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5068 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005069 schedule_timeout(1);
5070
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005071 if (delalloc_lock)
5072 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5073
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005074 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04005075
Josef Bacik9e0baf62011-07-15 15:16:44 +00005076 spin_lock(&BTRFS_I(inode)->lock);
5077 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05005078
Josef Bacik9e0baf62011-07-15 15:16:44 +00005079 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05005080 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00005081 nr_extents = BTRFS_I(inode)->outstanding_extents -
5082 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005083
5084 /*
5085 * Add an item to reserve for updating the inode when we complete the
5086 * delalloc io.
5087 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04005088 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5089 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005090 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05005091 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05005092 }
5093
5094 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04005095 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05005096 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005097 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05005098
Wang Shilong88e081bf2013-03-01 11:36:01 +00005099 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02005100 ret = btrfs_qgroup_reserve(root, num_bytes +
5101 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005102 if (ret)
5103 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00005104 }
Arne Jansenc5567232011-09-14 15:44:05 +02005105
Wang Shilong88e081bf2013-03-01 11:36:01 +00005106 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5107 if (unlikely(ret)) {
5108 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00005109 btrfs_qgroup_free(root, num_bytes +
5110 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00005111 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005112 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005113
Josef Bacik660d3f62011-12-09 11:18:51 -05005114 spin_lock(&BTRFS_I(inode)->lock);
5115 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04005116 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5117 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05005118 nr_extents--;
5119 }
5120 BTRFS_I(inode)->reserved_extents += nr_extents;
5121 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05005122
5123 if (delalloc_lock)
5124 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05005125
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005126 if (to_reserve)
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05305127 trace_btrfs_space_reservation(root->fs_info, "delalloc",
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005128 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005129 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5130
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005131 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00005132
5133out_fail:
5134 spin_lock(&BTRFS_I(inode)->lock);
5135 dropped = drop_outstanding_extent(inode);
5136 /*
5137 * If the inodes csum_bytes is the same as the original
5138 * csum_bytes then we know we haven't raced with any free()ers
5139 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00005140 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04005141 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005142 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005143 } else {
5144 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5145 u64 bytes;
5146
5147 /*
5148 * This is tricky, but first we need to figure out how much we
5149 * free'd from any free-ers that occured during this
5150 * reservation, so we reset ->csum_bytes to the csum_bytes
5151 * before we dropped our lock, and then call the free for the
5152 * number of bytes that were freed while we were trying our
5153 * reservation.
5154 */
5155 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5156 BTRFS_I(inode)->csum_bytes = csum_bytes;
5157 to_free = calc_csum_metadata_size(inode, bytes, 0);
5158
5159
5160 /*
5161 * Now we need to see how much we would have freed had we not
5162 * been making this reservation and our ->csum_bytes were not
5163 * artificially inflated.
5164 */
5165 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5166 bytes = csum_bytes - orig_csum_bytes;
5167 bytes = calc_csum_metadata_size(inode, bytes, 0);
5168
5169 /*
5170 * Now reset ->csum_bytes to what it should be. If bytes is
5171 * more than to_free then we would have free'd more space had we
5172 * not had an artificially high ->csum_bytes, so we need to free
5173 * the remainder. If bytes is the same or less then we don't
5174 * need to do anything, the other free-ers did the correct
5175 * thing.
5176 */
5177 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5178 if (bytes > to_free)
5179 to_free = bytes - to_free;
5180 else
5181 to_free = 0;
5182 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005183 spin_unlock(&BTRFS_I(inode)->lock);
5184 if (dropped)
5185 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5186
5187 if (to_free) {
5188 btrfs_block_rsv_release(root, block_rsv, to_free);
5189 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5190 btrfs_ino(inode), to_free, 0);
5191 }
5192 if (delalloc_lock)
5193 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5194 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005195}
5196
Josef Bacik7709cde2011-08-04 10:25:02 -04005197/**
5198 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5199 * @inode: the inode to release the reservation for
5200 * @num_bytes: the number of bytes we're releasing
5201 *
5202 * This will release the metadata reservation for an inode. This can be called
5203 * once we complete IO for a given set of bytes to release their metadata
5204 * reservations.
5205 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005206void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5207{
5208 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005209 u64 to_free = 0;
5210 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005211
5212 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005213 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005214 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005215
Miao Xie09348562013-02-07 10:12:07 +00005216 if (num_bytes)
5217 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005218 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005219 if (dropped > 0)
5220 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005221
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005222 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5223 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005224 if (root->fs_info->quota_enabled) {
5225 btrfs_qgroup_free(root, num_bytes +
5226 dropped * root->leafsize);
5227 }
5228
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005229 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5230 to_free);
5231}
5232
Josef Bacik7709cde2011-08-04 10:25:02 -04005233/**
5234 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5235 * @inode: inode we're writing to
5236 * @num_bytes: the number of bytes we want to allocate
5237 *
5238 * This will do the following things
5239 *
5240 * o reserve space in the data space info for num_bytes
5241 * o reserve space in the metadata space info based on number of outstanding
5242 * extents and how much csums will be needed
5243 * o add to the inodes ->delalloc_bytes
5244 * o add it to the fs_info's delalloc inodes list.
5245 *
5246 * This will return 0 for success and -ENOSPC if there is no space left.
5247 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005248int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5249{
5250 int ret;
5251
5252 ret = btrfs_check_data_free_space(inode, num_bytes);
5253 if (ret)
5254 return ret;
5255
5256 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5257 if (ret) {
5258 btrfs_free_reserved_data_space(inode, num_bytes);
5259 return ret;
5260 }
5261
5262 return 0;
5263}
5264
Josef Bacik7709cde2011-08-04 10:25:02 -04005265/**
5266 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5267 * @inode: inode we're releasing space for
5268 * @num_bytes: the number of bytes we want to free up
5269 *
5270 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5271 * called in the case that we don't need the metadata AND data reservations
5272 * anymore. So if there is an error or we insert an inline extent.
5273 *
5274 * This function will release the metadata space that was not used and will
5275 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5276 * list if there are no delalloc bytes left.
5277 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005278void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5279{
5280 btrfs_delalloc_release_metadata(inode, num_bytes);
5281 btrfs_free_reserved_data_space(inode, num_bytes);
5282}
5283
Liu Boc53d6132012-12-27 09:01:19 +00005284static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005285 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005286{
Josef Bacik0af3d002010-06-21 14:48:16 -04005287 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005288 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005289 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005290 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005291 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005292 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005293
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005294 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005295 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005296 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005297 if (alloc)
5298 old_val += num_bytes;
5299 else
5300 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005301 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005302 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005303
Chris Masond3977122009-01-05 21:25:51 -05005304 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005305 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005306 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005307 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005308 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5309 BTRFS_BLOCK_GROUP_RAID1 |
5310 BTRFS_BLOCK_GROUP_RAID10))
5311 factor = 2;
5312 else
5313 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005314 /*
5315 * If this block group has free space cache written out, we
5316 * need to make sure to load it if we are removing space. This
5317 * is because we need the unpinning stage to actually add the
5318 * space back to the block group, otherwise we will leak space.
5319 */
5320 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005321 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005322
Chris Masondb945352007-10-15 16:15:53 -04005323 byte_in_group = bytenr - cache->key.objectid;
5324 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005325
Josef Bacik25179202008-10-29 14:49:05 -04005326 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005327 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005328
Josef Bacik73bc1872011-10-03 14:07:49 -04005329 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005330 cache->disk_cache_state < BTRFS_DC_CLEAR)
5331 cache->disk_cache_state = BTRFS_DC_CLEAR;
5332
Josef Bacik0f9dd462008-09-23 13:14:11 -04005333 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005334 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005335 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005336 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005337 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005338 btrfs_set_block_group_used(&cache->item, old_val);
5339 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005340 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005341 cache->space_info->bytes_used += num_bytes;
5342 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005343 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005344 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005345 } else {
Chris Masondb945352007-10-15 16:15:53 -04005346 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005347 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005348 cache->pinned += num_bytes;
5349 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005350 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005351 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005352 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005353 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005354
Yan, Zhengf0486c62010-05-16 10:46:25 -04005355 set_extent_dirty(info->pinned_extents,
5356 bytenr, bytenr + num_bytes - 1,
5357 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005358 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005359 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005360 total -= num_bytes;
5361 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005362 }
5363 return 0;
5364}
Chris Mason6324fbf2008-03-24 15:01:59 -04005365
Chris Masona061fc82008-05-07 11:43:44 -04005366static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5367{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005368 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005369 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005370
Liu Boa1897fd2012-12-27 09:01:23 +00005371 spin_lock(&root->fs_info->block_group_cache_lock);
5372 bytenr = root->fs_info->first_logical_byte;
5373 spin_unlock(&root->fs_info->block_group_cache_lock);
5374
5375 if (bytenr < (u64)-1)
5376 return bytenr;
5377
Josef Bacik0f9dd462008-09-23 13:14:11 -04005378 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5379 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005380 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005381
Yan Zhengd2fb3432008-12-11 16:30:39 -05005382 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005383 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005384
5385 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005386}
5387
Yan, Zhengf0486c62010-05-16 10:46:25 -04005388static int pin_down_extent(struct btrfs_root *root,
5389 struct btrfs_block_group_cache *cache,
5390 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005391{
Yan Zheng11833d62009-09-11 16:11:19 -04005392 spin_lock(&cache->space_info->lock);
5393 spin_lock(&cache->lock);
5394 cache->pinned += num_bytes;
5395 cache->space_info->bytes_pinned += num_bytes;
5396 if (reserved) {
5397 cache->reserved -= num_bytes;
5398 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005399 }
Yan Zheng11833d62009-09-11 16:11:19 -04005400 spin_unlock(&cache->lock);
5401 spin_unlock(&cache->space_info->lock);
5402
Yan, Zhengf0486c62010-05-16 10:46:25 -04005403 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5404 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Josef Bacik0be5dc62013-10-07 15:18:52 -04005405 if (reserved)
5406 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
Yan324ae4d2007-11-16 14:57:08 -05005407 return 0;
5408}
Chris Mason9078a3e2007-04-26 16:46:15 -04005409
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410/*
5411 * this function must be called within transaction
5412 */
5413int btrfs_pin_extent(struct btrfs_root *root,
5414 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005415{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005416 struct btrfs_block_group_cache *cache;
5417
5418 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005419 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005420
5421 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5422
5423 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005424 return 0;
5425}
Zheng Yane8569812008-09-26 10:05:48 -04005426
Yan, Zhengf0486c62010-05-16 10:46:25 -04005427/*
Chris Masone688b7252011-10-31 20:52:39 -04005428 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005429 */
Liu Bodcfac412012-12-27 09:01:20 +00005430int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005431 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005432{
Chris Masone688b7252011-10-31 20:52:39 -04005433 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005434 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005435
5436 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005437 if (!cache)
5438 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005439
5440 /*
5441 * pull in the free space cache (if any) so that our pin
5442 * removes the free space from the cache. We have load_only set
5443 * to one because the slow code to read in the free extents does check
5444 * the pinned extents.
5445 */
Liu Bof6373bf2012-12-27 09:01:18 +00005446 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005447
5448 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5449
5450 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005451 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005452 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005453 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005454}
5455
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005456static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5457{
5458 int ret;
5459 struct btrfs_block_group_cache *block_group;
5460 struct btrfs_caching_control *caching_ctl;
5461
5462 block_group = btrfs_lookup_block_group(root->fs_info, start);
5463 if (!block_group)
5464 return -EINVAL;
5465
5466 cache_block_group(block_group, 0);
5467 caching_ctl = get_caching_control(block_group);
5468
5469 if (!caching_ctl) {
5470 /* Logic error */
5471 BUG_ON(!block_group_cache_done(block_group));
5472 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5473 } else {
5474 mutex_lock(&caching_ctl->mutex);
5475
5476 if (start >= caching_ctl->progress) {
5477 ret = add_excluded_extent(root, start, num_bytes);
5478 } else if (start + num_bytes <= caching_ctl->progress) {
5479 ret = btrfs_remove_free_space(block_group,
5480 start, num_bytes);
5481 } else {
5482 num_bytes = caching_ctl->progress - start;
5483 ret = btrfs_remove_free_space(block_group,
5484 start, num_bytes);
5485 if (ret)
5486 goto out_lock;
5487
5488 num_bytes = (start + num_bytes) -
5489 caching_ctl->progress;
5490 start = caching_ctl->progress;
5491 ret = add_excluded_extent(root, start, num_bytes);
5492 }
5493out_lock:
5494 mutex_unlock(&caching_ctl->mutex);
5495 put_caching_control(caching_ctl);
5496 }
5497 btrfs_put_block_group(block_group);
5498 return ret;
5499}
5500
5501int btrfs_exclude_logged_extents(struct btrfs_root *log,
5502 struct extent_buffer *eb)
5503{
5504 struct btrfs_file_extent_item *item;
5505 struct btrfs_key key;
5506 int found_type;
5507 int i;
5508
5509 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5510 return 0;
5511
5512 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5513 btrfs_item_key_to_cpu(eb, &key, i);
5514 if (key.type != BTRFS_EXTENT_DATA_KEY)
5515 continue;
5516 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5517 found_type = btrfs_file_extent_type(eb, item);
5518 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5519 continue;
5520 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5521 continue;
5522 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5523 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5524 __exclude_logged_extent(log, key.objectid, key.offset);
5525 }
5526
5527 return 0;
5528}
5529
Josef Bacikfb25e912011-07-26 17:00:46 -04005530/**
5531 * btrfs_update_reserved_bytes - update the block_group and space info counters
5532 * @cache: The cache we are manipulating
5533 * @num_bytes: The number of bytes in question
5534 * @reserve: One of the reservation enums
5535 *
5536 * This is called by the allocator when it reserves space, or by somebody who is
5537 * freeing space that was never actually used on disk. For example if you
5538 * reserve some space for a new leaf in transaction A and before transaction A
5539 * commits you free that leaf, you call this with reserve set to 0 in order to
5540 * clear the reservation.
5541 *
5542 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5543 * ENOSPC accounting. For data we handle the reservation through clearing the
5544 * delalloc bits in the io_tree. We have to do this since we could end up
5545 * allocating less disk space for the amount of data we have reserved in the
5546 * case of compression.
5547 *
5548 * If this is a reservation and the block group has become read only we cannot
5549 * make the reservation and return -EAGAIN, otherwise this function always
5550 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005551 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005552static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5553 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005554{
Josef Bacikfb25e912011-07-26 17:00:46 -04005555 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005556 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005557
Josef Bacikfb25e912011-07-26 17:00:46 -04005558 spin_lock(&space_info->lock);
5559 spin_lock(&cache->lock);
5560 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005561 if (cache->ro) {
5562 ret = -EAGAIN;
5563 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005564 cache->reserved += num_bytes;
5565 space_info->bytes_reserved += num_bytes;
5566 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005567 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005568 "space_info", space_info->flags,
5569 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005570 space_info->bytes_may_use -= num_bytes;
5571 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005572 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005573 } else {
5574 if (cache->ro)
5575 space_info->bytes_readonly += num_bytes;
5576 cache->reserved -= num_bytes;
5577 space_info->bytes_reserved -= num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005578 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005579 spin_unlock(&cache->lock);
5580 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005581 return ret;
5582}
5583
Jeff Mahoney143bede2012-03-01 14:56:26 +01005584void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005585 struct btrfs_root *root)
5586{
5587 struct btrfs_fs_info *fs_info = root->fs_info;
5588 struct btrfs_caching_control *next;
5589 struct btrfs_caching_control *caching_ctl;
5590 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005591 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005592
Josef Bacik9e351cc2014-03-13 15:42:13 -04005593 down_write(&fs_info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04005594
5595 list_for_each_entry_safe(caching_ctl, next,
5596 &fs_info->caching_block_groups, list) {
5597 cache = caching_ctl->block_group;
5598 if (block_group_cache_done(cache)) {
5599 cache->last_byte_to_unpin = (u64)-1;
5600 list_del_init(&caching_ctl->list);
5601 put_caching_control(caching_ctl);
5602 } else {
5603 cache->last_byte_to_unpin = caching_ctl->progress;
5604 }
5605 }
5606
5607 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5608 fs_info->pinned_extents = &fs_info->freed_extents[1];
5609 else
5610 fs_info->pinned_extents = &fs_info->freed_extents[0];
5611
Josef Bacik9e351cc2014-03-13 15:42:13 -04005612 up_write(&fs_info->commit_root_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005613
Josef Bacikb150a4f2013-06-19 15:00:04 -04005614 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5615 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5616
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005617 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005618}
5619
5620static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5621{
5622 struct btrfs_fs_info *fs_info = root->fs_info;
5623 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005624 struct btrfs_space_info *space_info;
5625 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005626 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005627 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005628
5629 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005630 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005631 if (!cache ||
5632 start >= cache->key.objectid + cache->key.offset) {
5633 if (cache)
5634 btrfs_put_block_group(cache);
5635 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005636 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005637 }
5638
5639 len = cache->key.objectid + cache->key.offset - start;
5640 len = min(len, end + 1 - start);
5641
5642 if (start < cache->last_byte_to_unpin) {
5643 len = min(len, cache->last_byte_to_unpin - start);
5644 btrfs_add_free_space(cache, start, len);
5645 }
Josef Bacik25179202008-10-29 14:49:05 -04005646
Yan, Zhengf0486c62010-05-16 10:46:25 -04005647 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005648 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005649
Josef Bacik7b398f82012-10-22 15:52:28 -04005650 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005651 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005652 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005653 space_info->bytes_pinned -= len;
5654 if (cache->ro) {
5655 space_info->bytes_readonly += len;
5656 readonly = true;
5657 }
Josef Bacik25179202008-10-29 14:49:05 -04005658 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005659 if (!readonly && global_rsv->space_info == space_info) {
5660 spin_lock(&global_rsv->lock);
5661 if (!global_rsv->full) {
5662 len = min(len, global_rsv->size -
5663 global_rsv->reserved);
5664 global_rsv->reserved += len;
5665 space_info->bytes_may_use += len;
5666 if (global_rsv->reserved >= global_rsv->size)
5667 global_rsv->full = 1;
5668 }
5669 spin_unlock(&global_rsv->lock);
5670 }
5671 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005672 }
5673
5674 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005675 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005676 return 0;
5677}
5678
5679int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005680 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005681{
Yan Zheng11833d62009-09-11 16:11:19 -04005682 struct btrfs_fs_info *fs_info = root->fs_info;
5683 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005684 u64 start;
5685 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005686 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005687
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005688 if (trans->aborted)
5689 return 0;
5690
Yan Zheng11833d62009-09-11 16:11:19 -04005691 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5692 unpin = &fs_info->freed_extents[1];
5693 else
5694 unpin = &fs_info->freed_extents[0];
5695
Chris Masond3977122009-01-05 21:25:51 -05005696 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005697 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005698 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005699 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005700 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005701
Li Dongyang5378e602011-03-24 10:24:27 +00005702 if (btrfs_test_opt(root, DISCARD))
5703 ret = btrfs_discard_extent(root, start,
5704 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005705
Chris Mason1a5bc162007-10-15 16:15:26 -04005706 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005707 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005708 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005709 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005710
Chris Masone20d96d2007-03-22 12:13:20 -04005711 return 0;
5712}
5713
Josef Bacikb150a4f2013-06-19 15:00:04 -04005714static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5715 u64 owner, u64 root_objectid)
5716{
5717 struct btrfs_space_info *space_info;
5718 u64 flags;
5719
5720 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5721 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5722 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5723 else
5724 flags = BTRFS_BLOCK_GROUP_METADATA;
5725 } else {
5726 flags = BTRFS_BLOCK_GROUP_DATA;
5727 }
5728
5729 space_info = __find_space_info(fs_info, flags);
5730 BUG_ON(!space_info); /* Logic bug */
5731 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5732}
5733
5734
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005735static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5736 struct btrfs_root *root,
5737 u64 bytenr, u64 num_bytes, u64 parent,
5738 u64 root_objectid, u64 owner_objectid,
5739 u64 owner_offset, int refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005740 struct btrfs_delayed_extent_op *extent_op,
5741 int no_quota)
Chris Masona28ec192007-03-06 20:08:01 -05005742{
Chris Masone2fa7222007-03-12 16:22:34 -04005743 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005744 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005745 struct btrfs_fs_info *info = root->fs_info;
5746 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005747 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005748 struct btrfs_extent_item *ei;
5749 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005750 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005751 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005752 int extent_slot = 0;
5753 int found_extent = 0;
5754 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005755 u32 item_size;
5756 u64 refs;
Josef Bacikfcebe452014-05-13 17:30:47 -07005757 int last_ref = 0;
5758 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
Josef Bacik3173a182013-03-07 14:22:04 -05005759 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5760 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005761
Josef Bacikfcebe452014-05-13 17:30:47 -07005762 if (!info->quota_enabled || !is_fstree(root_objectid))
5763 no_quota = 1;
5764
Chris Mason5caf2a02007-04-02 11:20:42 -04005765 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005766 if (!path)
5767 return -ENOMEM;
5768
Chris Mason3c12ac72008-04-21 12:01:38 -04005769 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005770 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005771
5772 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5773 BUG_ON(!is_data && refs_to_drop != 1);
5774
Josef Bacik3173a182013-03-07 14:22:04 -05005775 if (is_data)
5776 skinny_metadata = 0;
5777
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005778 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5779 bytenr, num_bytes, parent,
5780 root_objectid, owner_objectid,
5781 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005782 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005783 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005784 while (extent_slot >= 0) {
5785 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005786 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005787 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005788 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005789 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5790 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005791 found_extent = 1;
5792 break;
5793 }
Josef Bacik3173a182013-03-07 14:22:04 -05005794 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5795 key.offset == owner_objectid) {
5796 found_extent = 1;
5797 break;
5798 }
Chris Mason952fcca2008-02-18 16:33:44 -05005799 if (path->slots[0] - extent_slot > 5)
5800 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005801 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005802 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005803#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5804 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5805 if (found_extent && item_size < sizeof(*ei))
5806 found_extent = 0;
5807#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005808 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005809 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005810 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005811 NULL, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005812 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06005813 if (ret) {
5814 btrfs_abort_transaction(trans, extent_root, ret);
5815 goto out;
5816 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005817 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005818 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005819
5820 key.objectid = bytenr;
5821 key.type = BTRFS_EXTENT_ITEM_KEY;
5822 key.offset = num_bytes;
5823
Josef Bacik3173a182013-03-07 14:22:04 -05005824 if (!is_data && skinny_metadata) {
5825 key.type = BTRFS_METADATA_ITEM_KEY;
5826 key.offset = owner_objectid;
5827 }
5828
Zheng Yan31840ae2008-09-23 13:14:14 -04005829 ret = btrfs_search_slot(trans, extent_root,
5830 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005831 if (ret > 0 && skinny_metadata && path->slots[0]) {
5832 /*
5833 * Couldn't find our skinny metadata item,
5834 * see if we have ye olde extent item.
5835 */
5836 path->slots[0]--;
5837 btrfs_item_key_to_cpu(path->nodes[0], &key,
5838 path->slots[0]);
5839 if (key.objectid == bytenr &&
5840 key.type == BTRFS_EXTENT_ITEM_KEY &&
5841 key.offset == num_bytes)
5842 ret = 0;
5843 }
5844
5845 if (ret > 0 && skinny_metadata) {
5846 skinny_metadata = false;
Filipe Manana9ce49a02014-04-24 15:15:28 +01005847 key.objectid = bytenr;
Josef Bacik3173a182013-03-07 14:22:04 -05005848 key.type = BTRFS_EXTENT_ITEM_KEY;
5849 key.offset = num_bytes;
5850 btrfs_release_path(path);
5851 ret = btrfs_search_slot(trans, extent_root,
5852 &key, path, -1, 1);
5853 }
5854
Josef Bacikf3465ca2008-11-12 14:19:50 -05005855 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005856 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005857 ret, bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005858 if (ret > 0)
5859 btrfs_print_leaf(extent_root,
5860 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005861 }
David Sterba005d6422012-09-18 07:52:32 -06005862 if (ret < 0) {
5863 btrfs_abort_transaction(trans, extent_root, ret);
5864 goto out;
5865 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005866 extent_slot = path->slots[0];
5867 }
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05305868 } else if (WARN_ON(ret == -ENOENT)) {
Chris Mason7bb86312007-12-11 09:25:06 -05005869 btrfs_print_leaf(extent_root, path->nodes[0]);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005870 btrfs_err(info,
5871 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005872 bytenr, parent, root_objectid, owner_objectid,
5873 owner_offset);
Josef Bacikc4a050b2014-03-14 16:36:53 -04005874 btrfs_abort_transaction(trans, extent_root, ret);
5875 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005876 } else {
David Sterba005d6422012-09-18 07:52:32 -06005877 btrfs_abort_transaction(trans, extent_root, ret);
5878 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005879 }
Chris Mason5f39d392007-10-15 16:14:19 -04005880
5881 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005882 item_size = btrfs_item_size_nr(leaf, extent_slot);
5883#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5884 if (item_size < sizeof(*ei)) {
5885 BUG_ON(found_extent || extent_slot != path->slots[0]);
5886 ret = convert_extent_item_v0(trans, extent_root, path,
5887 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005888 if (ret < 0) {
5889 btrfs_abort_transaction(trans, extent_root, ret);
5890 goto out;
5891 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005892
David Sterbab3b4aa72011-04-21 01:20:15 +02005893 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005894 path->leave_spinning = 1;
5895
5896 key.objectid = bytenr;
5897 key.type = BTRFS_EXTENT_ITEM_KEY;
5898 key.offset = num_bytes;
5899
5900 ret = btrfs_search_slot(trans, extent_root, &key, path,
5901 -1, 1);
5902 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005903 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02005904 ret, bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005905 btrfs_print_leaf(extent_root, path->nodes[0]);
5906 }
David Sterba005d6422012-09-18 07:52:32 -06005907 if (ret < 0) {
5908 btrfs_abort_transaction(trans, extent_root, ret);
5909 goto out;
5910 }
5911
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005912 extent_slot = path->slots[0];
5913 leaf = path->nodes[0];
5914 item_size = btrfs_item_size_nr(leaf, extent_slot);
5915 }
5916#endif
5917 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005918 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005919 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005920 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5921 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005922 struct btrfs_tree_block_info *bi;
5923 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5924 bi = (struct btrfs_tree_block_info *)(ei + 1);
5925 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005926 }
5927
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005928 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005929 if (refs < refs_to_drop) {
5930 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5931 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5932 ret = -EINVAL;
5933 btrfs_abort_transaction(trans, extent_root, ret);
5934 goto out;
5935 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005936 refs -= refs_to_drop;
5937
5938 if (refs > 0) {
Josef Bacikfcebe452014-05-13 17:30:47 -07005939 type = BTRFS_QGROUP_OPER_SUB_SHARED;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005940 if (extent_op)
5941 __run_delayed_extent_op(extent_op, leaf, ei);
5942 /*
5943 * In the case of inline back ref, reference count will
5944 * be updated by remove_extent_backref
5945 */
5946 if (iref) {
5947 BUG_ON(!found_extent);
5948 } else {
5949 btrfs_set_extent_refs(leaf, ei, refs);
5950 btrfs_mark_buffer_dirty(leaf);
5951 }
5952 if (found_extent) {
5953 ret = remove_extent_backref(trans, extent_root, path,
5954 iref, refs_to_drop,
Josef Bacikfcebe452014-05-13 17:30:47 -07005955 is_data, &last_ref);
David Sterba005d6422012-09-18 07:52:32 -06005956 if (ret) {
5957 btrfs_abort_transaction(trans, extent_root, ret);
5958 goto out;
5959 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005960 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005961 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5962 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005963 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005964 if (found_extent) {
5965 BUG_ON(is_data && refs_to_drop !=
5966 extent_data_ref_count(root, path, iref));
5967 if (iref) {
5968 BUG_ON(path->slots[0] != extent_slot);
5969 } else {
5970 BUG_ON(path->slots[0] != extent_slot + 1);
5971 path->slots[0] = extent_slot;
5972 num_to_del = 2;
5973 }
Chris Mason78fae272007-03-25 11:35:08 -04005974 }
Chris Masonb9473432009-03-13 11:00:37 -04005975
Josef Bacikfcebe452014-05-13 17:30:47 -07005976 last_ref = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05005977 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5978 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005979 if (ret) {
5980 btrfs_abort_transaction(trans, extent_root, ret);
5981 goto out;
5982 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005983 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005984
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005985 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005986 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005987 if (ret) {
5988 btrfs_abort_transaction(trans, extent_root, ret);
5989 goto out;
5990 }
Chris Mason459931e2008-12-10 09:10:46 -05005991 }
5992
Liu Boc53d6132012-12-27 09:01:19 +00005993 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005994 if (ret) {
5995 btrfs_abort_transaction(trans, extent_root, ret);
5996 goto out;
5997 }
Chris Masona28ec192007-03-06 20:08:01 -05005998 }
Josef Bacikfcebe452014-05-13 17:30:47 -07005999 btrfs_release_path(path);
6000
6001 /* Deal with the quota accounting */
6002 if (!ret && last_ref && !no_quota) {
6003 int mod_seq = 0;
6004
6005 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
6006 type == BTRFS_QGROUP_OPER_SUB_SHARED)
6007 mod_seq = 1;
6008
6009 ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
6010 bytenr, num_bytes, type,
6011 mod_seq);
6012 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006013out:
Chris Mason5caf2a02007-04-02 11:20:42 -04006014 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05006015 return ret;
6016}
6017
6018/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006019 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04006020 * delayed ref for that extent as well. This searches the delayed ref tree for
6021 * a given extent, and if there are no other delayed refs to be processed, it
6022 * removes it from the tree.
6023 */
6024static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6025 struct btrfs_root *root, u64 bytenr)
6026{
6027 struct btrfs_delayed_ref_head *head;
6028 struct btrfs_delayed_ref_root *delayed_refs;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006029 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04006030
6031 delayed_refs = &trans->transaction->delayed_refs;
6032 spin_lock(&delayed_refs->lock);
6033 head = btrfs_find_delayed_ref_head(trans, bytenr);
6034 if (!head)
Chris Masoncf93da72014-01-29 07:02:40 -08006035 goto out_delayed_unlock;
Chris Mason1887be62009-03-13 10:11:24 -04006036
Josef Bacikd7df2c72014-01-23 09:21:38 -05006037 spin_lock(&head->lock);
6038 if (rb_first(&head->ref_root))
Chris Mason1887be62009-03-13 10:11:24 -04006039 goto out;
6040
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006041 if (head->extent_op) {
6042 if (!head->must_insert_reserved)
6043 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00006044 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006045 head->extent_op = NULL;
6046 }
6047
Chris Mason1887be62009-03-13 10:11:24 -04006048 /*
6049 * waiting for the lock here would deadlock. If someone else has it
6050 * locked they are already in the process of dropping it anyway
6051 */
6052 if (!mutex_trylock(&head->mutex))
6053 goto out;
6054
6055 /*
6056 * at this point we have a head with no other entries. Go
6057 * ahead and process it.
6058 */
6059 head->node.in_tree = 0;
Liu Boc46effa2013-10-14 12:59:45 +08006060 rb_erase(&head->href_node, &delayed_refs->href_root);
Chris Masonc3e69d52009-03-13 10:17:05 -04006061
Josef Bacikd7df2c72014-01-23 09:21:38 -05006062 atomic_dec(&delayed_refs->num_entries);
Chris Mason1887be62009-03-13 10:11:24 -04006063
6064 /*
6065 * we don't take a ref on the node because we're removing it from the
6066 * tree, so we just steal the ref the tree was holding.
6067 */
Chris Masonc3e69d52009-03-13 10:17:05 -04006068 delayed_refs->num_heads--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006069 if (head->processing == 0)
Chris Masonc3e69d52009-03-13 10:17:05 -04006070 delayed_refs->num_heads_ready--;
Josef Bacikd7df2c72014-01-23 09:21:38 -05006071 head->processing = 0;
6072 spin_unlock(&head->lock);
Chris Mason1887be62009-03-13 10:11:24 -04006073 spin_unlock(&delayed_refs->lock);
6074
Yan, Zhengf0486c62010-05-16 10:46:25 -04006075 BUG_ON(head->extent_op);
6076 if (head->must_insert_reserved)
6077 ret = 1;
6078
6079 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04006080 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006081 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04006082out:
Josef Bacikd7df2c72014-01-23 09:21:38 -05006083 spin_unlock(&head->lock);
Chris Masoncf93da72014-01-29 07:02:40 -08006084
6085out_delayed_unlock:
Chris Mason1887be62009-03-13 10:11:24 -04006086 spin_unlock(&delayed_refs->lock);
6087 return 0;
6088}
6089
Yan, Zhengf0486c62010-05-16 10:46:25 -04006090void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6091 struct btrfs_root *root,
6092 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02006093 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006094{
Yan, Zhengf0486c62010-05-16 10:46:25 -04006095 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04006096 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006097 int ret;
6098
6099 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006100 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6101 buf->start, buf->len,
6102 parent, root->root_key.objectid,
6103 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02006104 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006105 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006106 }
6107
6108 if (!last_ref)
6109 return;
6110
Yan, Zhengf0486c62010-05-16 10:46:25 -04006111 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006112
6113 if (btrfs_header_generation(buf) == trans->transid) {
6114 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6115 ret = check_ref_cleanup(trans, root, buf->start);
6116 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04006117 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006118 }
6119
6120 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6121 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04006122 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006123 }
6124
6125 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6126
6127 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04006128 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacik0be5dc62013-10-07 15:18:52 -04006129 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
Josef Bacikb150a4f2013-06-19 15:00:04 -04006130 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006131 }
6132out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04006133 if (pin)
6134 add_pinned_bytes(root->fs_info, buf->len,
6135 btrfs_header_level(buf),
6136 root->root_key.objectid);
6137
Josef Bacika826d6d2011-03-16 13:42:43 -04006138 /*
6139 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6140 * anymore.
6141 */
6142 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006143 btrfs_put_block_group(cache);
6144}
6145
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006146/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006147int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6148 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
Josef Bacikfcebe452014-05-13 17:30:47 -07006149 u64 owner, u64 offset, int no_quota)
Chris Mason925baed2008-06-25 16:01:30 -04006150{
6151 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006152 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04006153
Josef Bacikb150a4f2013-06-19 15:00:04 -04006154 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6155
Chris Mason56bec292009-03-13 10:10:06 -04006156 /*
6157 * tree log blocks never actually go into the extent allocation
6158 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04006159 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006160 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6161 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006162 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006163 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006164 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006165 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006166 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6167 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006168 parent, root_objectid, (int)owner,
Josef Bacikfcebe452014-05-13 17:30:47 -07006169 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006170 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006171 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6172 num_bytes,
6173 parent, root_objectid, owner,
6174 offset, BTRFS_DROP_DELAYED_REF,
Josef Bacikfcebe452014-05-13 17:30:47 -07006175 NULL, no_quota);
Chris Mason56bec292009-03-13 10:10:06 -04006176 }
Chris Mason925baed2008-06-25 16:01:30 -04006177 return ret;
6178}
6179
David Woodhouse53b381b2013-01-29 18:40:14 -05006180static u64 stripe_align(struct btrfs_root *root,
6181 struct btrfs_block_group_cache *cache,
6182 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006183{
Qu Wenruofda28322013-02-26 08:10:22 +00006184 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006185 return ret;
6186}
6187
Chris Masonfec577f2007-02-26 10:40:21 -05006188/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006189 * when we wait for progress in the block group caching, its because
6190 * our allocation attempt failed at least once. So, we must sleep
6191 * and let some progress happen before we try again.
6192 *
6193 * This function will sleep at least once waiting for new free space to
6194 * show up, and then it will check the block group free space numbers
6195 * for our min num_bytes. Another option is to have it go ahead
6196 * and look in the rbtree for a free extent of a given size, but this
6197 * is a good start.
Josef Bacik36cce922013-08-05 11:15:21 -04006198 *
6199 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6200 * any of the information in this block group.
Josef Bacik817d52f2009-07-13 21:29:25 -04006201 */
Josef Bacik36cce922013-08-05 11:15:21 -04006202static noinline void
Josef Bacik817d52f2009-07-13 21:29:25 -04006203wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6204 u64 num_bytes)
6205{
Yan Zheng11833d62009-09-11 16:11:19 -04006206 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006207
Yan Zheng11833d62009-09-11 16:11:19 -04006208 caching_ctl = get_caching_control(cache);
6209 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006210 return;
Josef Bacik817d52f2009-07-13 21:29:25 -04006211
Yan Zheng11833d62009-09-11 16:11:19 -04006212 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006213 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006214
6215 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -04006216}
6217
6218static noinline int
6219wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6220{
6221 struct btrfs_caching_control *caching_ctl;
Josef Bacik36cce922013-08-05 11:15:21 -04006222 int ret = 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006223
6224 caching_ctl = get_caching_control(cache);
6225 if (!caching_ctl)
Josef Bacik36cce922013-08-05 11:15:21 -04006226 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
Yan Zheng11833d62009-09-11 16:11:19 -04006227
6228 wait_event(caching_ctl->wait, block_group_cache_done(cache));
Josef Bacik36cce922013-08-05 11:15:21 -04006229 if (cache->cached == BTRFS_CACHE_ERROR)
6230 ret = -EIO;
Yan Zheng11833d62009-09-11 16:11:19 -04006231 put_caching_control(caching_ctl);
Josef Bacik36cce922013-08-05 11:15:21 -04006232 return ret;
Josef Bacik817d52f2009-07-13 21:29:25 -04006233}
6234
Liu Bo31e50222012-11-21 14:18:10 +00006235int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006236{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006237 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006238 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006239 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006240 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006241 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006242 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006243 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006244 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006245 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006246 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006247 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006248 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006249
Chris Masone942f882013-02-20 14:06:05 -05006250 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006251}
6252
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006253int get_block_group_index(struct btrfs_block_group_cache *cache)
Ilya Dryomov7738a532012-03-27 17:09:17 +03006254{
Liu Bo31e50222012-11-21 14:18:10 +00006255 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006256}
6257
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006258static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6259 [BTRFS_RAID_RAID10] = "raid10",
6260 [BTRFS_RAID_RAID1] = "raid1",
6261 [BTRFS_RAID_DUP] = "dup",
6262 [BTRFS_RAID_RAID0] = "raid0",
6263 [BTRFS_RAID_SINGLE] = "single",
6264 [BTRFS_RAID_RAID5] = "raid5",
6265 [BTRFS_RAID_RAID6] = "raid6",
6266};
6267
Jeff Mahoney1b8e5df2013-11-20 16:50:23 -05006268static const char *get_raid_name(enum btrfs_raid_types type)
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04006269{
6270 if (type >= BTRFS_NR_RAID_TYPES)
6271 return NULL;
6272
6273 return btrfs_raid_type_names[type];
6274}
6275
Josef Bacik817d52f2009-07-13 21:29:25 -04006276enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006277 LOOP_CACHING_NOWAIT = 0,
6278 LOOP_CACHING_WAIT = 1,
6279 LOOP_ALLOC_CHUNK = 2,
6280 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006281};
6282
6283/*
Chris Masonfec577f2007-02-26 10:40:21 -05006284 * walks the btree of allocated extents and find a hole of a given size.
6285 * The key ins is changed to record the hole:
Miao Xiea4820392013-09-09 13:19:42 +08006286 * ins->objectid == start position
Chris Mason62e27492007-03-15 12:56:47 -04006287 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Miao Xiea4820392013-09-09 13:19:42 +08006288 * ins->offset == the size of the hole.
Chris Masonfec577f2007-02-26 10:40:21 -05006289 * Any available blocks before search_start are skipped.
Miao Xiea4820392013-09-09 13:19:42 +08006290 *
6291 * If there is no suitable free space, we will record the max size of
6292 * the free space extent currently.
Chris Masonfec577f2007-02-26 10:40:21 -05006293 */
Josef Bacik00361582013-08-14 14:02:47 -04006294static noinline int find_free_extent(struct btrfs_root *orig_root,
Chris Mason98ed5172008-01-03 10:01:48 -05006295 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006296 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006297 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006298{
Josef Bacik80eb2342008-10-29 14:49:05 -04006299 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006300 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006301 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006302 struct btrfs_block_group_cache *block_group = NULL;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006303 u64 search_start = 0;
Miao Xiea4820392013-09-09 13:19:42 +08006304 u64 max_extent_size = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006305 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006306 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006307 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006308 int index = __get_raid_index(flags);
6309 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006310 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik0a243252009-09-11 16:11:20 -04006311 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006312 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006313 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006314 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006315
Chris Masondb945352007-10-15 16:15:53 -04006316 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006317 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006318 ins->objectid = 0;
6319 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006320
David Sterbab6919a52013-04-29 13:39:40 +00006321 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006322
David Sterbab6919a52013-04-29 13:39:40 +00006323 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006324 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006325 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006326 return -ENOSPC;
6327 }
Josef Bacik2552d172009-04-03 10:14:19 -04006328
Josef Bacik67377732010-09-16 16:19:09 -04006329 /*
6330 * If the space info is for both data and metadata it means we have a
6331 * small filesystem and we can't use the clustering stuff.
6332 */
6333 if (btrfs_mixed_space_info(space_info))
6334 use_cluster = false;
6335
David Sterbab6919a52013-04-29 13:39:40 +00006336 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006337 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006338 if (!btrfs_test_opt(root, SSD))
6339 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006340 }
6341
David Sterbab6919a52013-04-29 13:39:40 +00006342 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006343 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006344 last_ptr = &root->fs_info->data_alloc_cluster;
6345 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006346
Chris Mason239b14b2008-03-24 15:02:07 -04006347 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006348 spin_lock(&last_ptr->lock);
6349 if (last_ptr->block_group)
6350 hint_byte = last_ptr->window_start;
6351 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006352 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006353
Chris Masona061fc82008-05-07 11:43:44 -04006354 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006355 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006356
Josef Bacik817d52f2009-07-13 21:29:25 -04006357 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006358 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006359
Josef Bacik2552d172009-04-03 10:14:19 -04006360 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006361 block_group = btrfs_lookup_block_group(root->fs_info,
6362 search_start);
Josef Bacik817d52f2009-07-13 21:29:25 -04006363 /*
6364 * we don't want to use the block group if it doesn't match our
6365 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006366 *
6367 * However if we are re-searching with an ideal block group
6368 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006369 */
David Sterbab6919a52013-04-29 13:39:40 +00006370 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006371 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006372 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006373 if (list_empty(&block_group->list) ||
6374 block_group->ro) {
6375 /*
6376 * someone is removing this block group,
6377 * we can't jump into the have_block_group
6378 * target because our list pointers are not
6379 * valid
6380 */
6381 btrfs_put_block_group(block_group);
6382 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006383 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006384 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006385 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006386 }
Josef Bacik2552d172009-04-03 10:14:19 -04006387 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006388 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006389 }
Chris Mason42e70e72008-11-07 18:17:11 -05006390 }
Josef Bacik2552d172009-04-03 10:14:19 -04006391search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006392 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006393 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006394 list_for_each_entry(block_group, &space_info->block_groups[index],
6395 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006396 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006397 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006398
Josef Bacik11dfe352009-11-13 20:12:59 +00006399 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006400 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006401
Chris Mason83a50de2010-12-13 15:06:46 -05006402 /*
6403 * this can happen if we end up cycling through all the
6404 * raid types, but we want to make sure we only allocate
6405 * for the proper type.
6406 */
David Sterbab6919a52013-04-29 13:39:40 +00006407 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006408 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6409 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006410 BTRFS_BLOCK_GROUP_RAID5 |
6411 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006412 BTRFS_BLOCK_GROUP_RAID10;
6413
6414 /*
6415 * if they asked for extra copies and this block group
6416 * doesn't provide them, bail. This does allow us to
6417 * fill raid0 from raid1.
6418 */
David Sterbab6919a52013-04-29 13:39:40 +00006419 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006420 goto loop;
6421 }
6422
Josef Bacik2552d172009-04-03 10:14:19 -04006423have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006424 cached = block_group_cache_done(block_group);
6425 if (unlikely(!cached)) {
Liu Bof6373bf2012-12-27 09:01:18 +00006426 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006427 BUG_ON(ret < 0);
6428 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006429 }
6430
Josef Bacik36cce922013-08-05 11:15:21 -04006431 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6432 goto loop;
Josef Bacikea6a4782008-11-20 12:16:16 -05006433 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006434 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006435
Josef Bacik0a243252009-09-11 16:11:20 -04006436 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006437 * Ok we want to try and use the cluster allocator, so
6438 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006439 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006440 if (last_ptr) {
Miao Xie215a63d2014-01-15 20:00:56 +08006441 struct btrfs_block_group_cache *used_block_group;
Chris Mason8de972b2013-01-04 15:39:43 -05006442 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006443 /*
6444 * the refill lock keeps out other
6445 * people trying to start a new cluster
6446 */
6447 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006448 used_block_group = last_ptr->block_group;
6449 if (used_block_group != block_group &&
6450 (!used_block_group ||
6451 used_block_group->ro ||
Miao Xie215a63d2014-01-15 20:00:56 +08006452 !block_group_bits(used_block_group, flags)))
Chris Mason44fb5512009-06-04 15:34:51 -04006453 goto refill_cluster;
Chris Mason44fb5512009-06-04 15:34:51 -04006454
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006455 if (used_block_group != block_group)
6456 btrfs_get_block_group(used_block_group);
6457
6458 offset = btrfs_alloc_from_cluster(used_block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006459 last_ptr,
6460 num_bytes,
6461 used_block_group->key.objectid,
6462 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006463 if (offset) {
6464 /* we have a block, we're done */
6465 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006466 trace_btrfs_reserve_extent_cluster(root,
Miao Xie89d43462014-01-15 20:00:57 +08006467 used_block_group,
6468 search_start, num_bytes);
Miao Xie215a63d2014-01-15 20:00:56 +08006469 if (used_block_group != block_group) {
6470 btrfs_put_block_group(block_group);
6471 block_group = used_block_group;
6472 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006473 goto checks;
6474 }
6475
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006476 WARN_ON(last_ptr->block_group != used_block_group);
Miao Xie215a63d2014-01-15 20:00:56 +08006477 if (used_block_group != block_group)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006478 btrfs_put_block_group(used_block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006479refill_cluster:
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006480 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6481 * set up a new clusters, so lets just skip it
6482 * and let the allocator find whatever block
6483 * it can find. If we reach this point, we
6484 * will have tried the cluster allocator
6485 * plenty of times and not have found
6486 * anything, so we are likely way too
6487 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006488 * anything.
6489 *
6490 * However, if the cluster is taken from the
6491 * current block group, release the cluster
6492 * first, so that we stand a better chance of
6493 * succeeding in the unclustered
6494 * allocation. */
6495 if (loop >= LOOP_NO_EMPTY_SIZE &&
6496 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006497 spin_unlock(&last_ptr->refill_lock);
6498 goto unclustered_alloc;
6499 }
6500
Chris Masonfa9c0d792009-04-03 09:47:43 -04006501 /*
6502 * this cluster didn't work out, free it and
6503 * start over
6504 */
6505 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6506
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006507 if (loop >= LOOP_NO_EMPTY_SIZE) {
6508 spin_unlock(&last_ptr->refill_lock);
6509 goto unclustered_alloc;
6510 }
6511
Chris Mason8de972b2013-01-04 15:39:43 -05006512 aligned_cluster = max_t(unsigned long,
6513 empty_cluster + empty_size,
6514 block_group->full_stripe_len);
6515
Chris Masonfa9c0d792009-04-03 09:47:43 -04006516 /* allocate a cluster in this block group */
Josef Bacik00361582013-08-14 14:02:47 -04006517 ret = btrfs_find_space_cluster(root, block_group,
6518 last_ptr, search_start,
6519 num_bytes,
6520 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006521 if (ret == 0) {
6522 /*
6523 * now pull our allocation out of this
6524 * cluster
6525 */
6526 offset = btrfs_alloc_from_cluster(block_group,
Miao Xiea4820392013-09-09 13:19:42 +08006527 last_ptr,
6528 num_bytes,
6529 search_start,
6530 &max_extent_size);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006531 if (offset) {
6532 /* we found one, proceed */
6533 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006534 trace_btrfs_reserve_extent_cluster(root,
6535 block_group, search_start,
6536 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006537 goto checks;
6538 }
Josef Bacik0a243252009-09-11 16:11:20 -04006539 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6540 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006541 spin_unlock(&last_ptr->refill_lock);
6542
Josef Bacik0a243252009-09-11 16:11:20 -04006543 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006544 wait_block_group_cache_progress(block_group,
6545 num_bytes + empty_cluster + empty_size);
6546 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006547 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006548
Chris Masonfa9c0d792009-04-03 09:47:43 -04006549 /*
6550 * at this point we either didn't find a cluster
6551 * or we weren't able to allocate a block from our
6552 * cluster. Free the cluster we've been trying
6553 * to use, and go to the next block group
6554 */
Josef Bacik0a243252009-09-11 16:11:20 -04006555 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006556 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006557 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006558 }
6559
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006560unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006561 spin_lock(&block_group->free_space_ctl->tree_lock);
6562 if (cached &&
6563 block_group->free_space_ctl->free_space <
6564 num_bytes + empty_cluster + empty_size) {
Miao Xiea4820392013-09-09 13:19:42 +08006565 if (block_group->free_space_ctl->free_space >
6566 max_extent_size)
6567 max_extent_size =
6568 block_group->free_space_ctl->free_space;
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006569 spin_unlock(&block_group->free_space_ctl->tree_lock);
6570 goto loop;
6571 }
6572 spin_unlock(&block_group->free_space_ctl->tree_lock);
6573
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006574 offset = btrfs_find_space_for_alloc(block_group, search_start,
Miao Xiea4820392013-09-09 13:19:42 +08006575 num_bytes, empty_size,
6576 &max_extent_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006577 /*
6578 * If we didn't find a chunk, and we haven't failed on this
6579 * block group before, and this block group is in the middle of
6580 * caching and we are ok with waiting, then go ahead and wait
6581 * for progress to be made, and set failed_alloc to true.
6582 *
6583 * If failed_alloc is true then we've already waited on this
6584 * block group once and should move on to the next block group.
6585 */
6586 if (!offset && !failed_alloc && !cached &&
6587 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006588 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006589 num_bytes + empty_size);
6590 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006591 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006592 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006593 if (!cached)
6594 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006595 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006596 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006597checks:
Miao Xie215a63d2014-01-15 20:00:56 +08006598 search_start = stripe_align(root, block_group,
David Woodhouse53b381b2013-01-29 18:40:14 -05006599 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006600
Josef Bacik2552d172009-04-03 10:14:19 -04006601 /* move on to the next group */
6602 if (search_start + num_bytes >
Miao Xie215a63d2014-01-15 20:00:56 +08006603 block_group->key.objectid + block_group->key.offset) {
6604 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006605 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006606 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006607
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006608 if (offset < search_start)
Miao Xie215a63d2014-01-15 20:00:56 +08006609 btrfs_add_free_space(block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006610 search_start - offset);
6611 BUG_ON(offset > search_start);
6612
Miao Xie215a63d2014-01-15 20:00:56 +08006613 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006614 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006615 if (ret == -EAGAIN) {
Miao Xie215a63d2014-01-15 20:00:56 +08006616 btrfs_add_free_space(block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006617 goto loop;
6618 }
Yan Zheng11833d62009-09-11 16:11:19 -04006619
Josef Bacik2552d172009-04-03 10:14:19 -04006620 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006621 ins->objectid = search_start;
6622 ins->offset = num_bytes;
6623
Josef Bacik3f7de032011-11-10 08:29:20 -05006624 trace_btrfs_reserve_extent(orig_root, block_group,
6625 search_start, num_bytes);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006626 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006627 break;
6628loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006629 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006630 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006631 BUG_ON(index != get_block_group_index(block_group));
Chris Masonfa9c0d792009-04-03 09:47:43 -04006632 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006633 }
6634 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006635
Miao Xie60d2adb2011-09-09 17:34:35 +08006636 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6637 goto search;
6638
Yan, Zhengb742bb82010-05-16 10:46:24 -04006639 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6640 goto search;
6641
Josef Bacik285ff5a2012-01-13 15:27:45 -05006642 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006643 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6644 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006645 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6646 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6647 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6648 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006649 */
Josef Bacik723bda22011-05-27 16:11:38 -04006650 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006651 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006652 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006653 if (loop == LOOP_ALLOC_CHUNK) {
Josef Bacik00361582013-08-14 14:02:47 -04006654 struct btrfs_trans_handle *trans;
Wang Shilongf017f152014-03-13 13:19:47 +08006655 int exist = 0;
Josef Bacik00361582013-08-14 14:02:47 -04006656
Wang Shilongf017f152014-03-13 13:19:47 +08006657 trans = current->journal_info;
6658 if (trans)
6659 exist = 1;
6660 else
6661 trans = btrfs_join_transaction(root);
6662
Josef Bacik00361582013-08-14 14:02:47 -04006663 if (IS_ERR(trans)) {
6664 ret = PTR_ERR(trans);
6665 goto out;
6666 }
6667
David Sterbab6919a52013-04-29 13:39:40 +00006668 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006669 CHUNK_ALLOC_FORCE);
6670 /*
6671 * Do not bail out on ENOSPC since we
6672 * can do more things.
6673 */
Josef Bacik00361582013-08-14 14:02:47 -04006674 if (ret < 0 && ret != -ENOSPC)
Josef Bacikea658ba2012-09-11 16:57:25 -04006675 btrfs_abort_transaction(trans,
6676 root, ret);
Josef Bacik00361582013-08-14 14:02:47 -04006677 else
6678 ret = 0;
Wang Shilongf017f152014-03-13 13:19:47 +08006679 if (!exist)
6680 btrfs_end_transaction(trans, root);
Josef Bacik00361582013-08-14 14:02:47 -04006681 if (ret)
Josef Bacikea658ba2012-09-11 16:57:25 -04006682 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006683 }
6684
6685 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006686 empty_size = 0;
6687 empty_cluster = 0;
6688 }
Chris Mason42e70e72008-11-07 18:17:11 -05006689
Josef Bacik723bda22011-05-27 16:11:38 -04006690 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006691 } else if (!ins->objectid) {
6692 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006693 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006694 ret = 0;
6695 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006696out:
Miao Xiea4820392013-09-09 13:19:42 +08006697 if (ret == -ENOSPC)
6698 ins->offset = max_extent_size;
Chris Mason0f70abe2007-02-28 16:46:22 -05006699 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006700}
Chris Masonec44a352008-04-28 15:29:52 -04006701
Josef Bacik9ed74f22009-09-11 16:12:44 -04006702static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6703 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006704{
6705 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006706 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006707
Josef Bacik9ed74f22009-09-11 16:12:44 -04006708 spin_lock(&info->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006709 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006710 info->flags,
6711 info->total_bytes - info->bytes_used - info->bytes_pinned -
6712 info->bytes_reserved - info->bytes_readonly,
Chris Masond3977122009-01-05 21:25:51 -05006713 (info->full) ? "" : "not ");
Frank Holtonefe120a2013-12-20 11:37:06 -05006714 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006715 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006716 info->total_bytes, info->bytes_used, info->bytes_pinned,
6717 info->bytes_reserved, info->bytes_may_use,
6718 info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006719 spin_unlock(&info->lock);
6720
6721 if (!dump_block_groups)
6722 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006723
Josef Bacik80eb2342008-10-29 14:49:05 -04006724 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006725again:
6726 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006727 spin_lock(&cache->lock);
Frank Holtonefe120a2013-12-20 11:37:06 -05006728 printk(KERN_INFO "BTRFS: "
6729 "block group %llu has %llu bytes, "
6730 "%llu used %llu pinned %llu reserved %s\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006731 cache->key.objectid, cache->key.offset,
6732 btrfs_block_group_used(&cache->item), cache->pinned,
6733 cache->reserved, cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006734 btrfs_dump_free_space(cache, bytes);
6735 spin_unlock(&cache->lock);
6736 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006737 if (++index < BTRFS_NR_RAID_TYPES)
6738 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006739 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006740}
Zheng Yane8569812008-09-26 10:05:48 -04006741
Josef Bacik00361582013-08-14 14:02:47 -04006742int btrfs_reserve_extent(struct btrfs_root *root,
Yan Zheng11833d62009-09-11 16:11:19 -04006743 u64 num_bytes, u64 min_alloc_size,
6744 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006745 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006746{
Miao Xie9e622d62012-01-26 15:01:12 -05006747 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006748 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006749 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006750
David Sterbab6919a52013-04-29 13:39:40 +00006751 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006752again:
Chris Masondb945352007-10-15 16:15:53 -04006753 WARN_ON(num_bytes < root->sectorsize);
Josef Bacik00361582013-08-14 14:02:47 -04006754 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6755 flags);
Chris Mason3b951512008-04-17 11:29:12 -04006756
Miao Xie9e622d62012-01-26 15:01:12 -05006757 if (ret == -ENOSPC) {
Miao Xiea4820392013-09-09 13:19:42 +08006758 if (!final_tried && ins->offset) {
6759 num_bytes = min(num_bytes >> 1, ins->offset);
Zach Brown24542bf2012-11-16 00:04:43 +00006760 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006761 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006762 if (num_bytes == min_alloc_size)
6763 final_tried = true;
6764 goto again;
6765 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6766 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006767
David Sterbab6919a52013-04-29 13:39:40 +00006768 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006769 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006770 flags, num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006771 if (sinfo)
6772 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006773 }
Chris Mason925baed2008-06-25 16:01:30 -04006774 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006775
6776 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006777}
6778
Chris Masone688b7252011-10-31 20:52:39 -04006779static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6780 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006781{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006782 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006783 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006784
Josef Bacik0f9dd462008-09-23 13:14:11 -04006785 cache = btrfs_lookup_block_group(root->fs_info, start);
6786 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006787 btrfs_err(root->fs_info, "Unable to find block group for %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006788 start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006789 return -ENOSPC;
6790 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006791
Li Dongyang5378e602011-03-24 10:24:27 +00006792 if (btrfs_test_opt(root, DISCARD))
6793 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006794
Chris Masone688b7252011-10-31 20:52:39 -04006795 if (pin)
6796 pin_down_extent(root, cache, start, len, 1);
6797 else {
6798 btrfs_add_free_space(cache, start, len);
6799 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6800 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006801 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006802
liubo1abe9b82011-03-24 11:18:59 +00006803 trace_btrfs_reserved_extent_free(root, start, len);
6804
Chris Masone6dcd2d2008-07-17 12:53:50 -04006805 return ret;
6806}
6807
Chris Masone688b7252011-10-31 20:52:39 -04006808int btrfs_free_reserved_extent(struct btrfs_root *root,
6809 u64 start, u64 len)
6810{
6811 return __btrfs_free_reserved_extent(root, start, len, 0);
6812}
6813
6814int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6815 u64 start, u64 len)
6816{
6817 return __btrfs_free_reserved_extent(root, start, len, 1);
6818}
6819
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006820static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6821 struct btrfs_root *root,
6822 u64 parent, u64 root_objectid,
6823 u64 flags, u64 owner, u64 offset,
6824 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006825{
6826 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006827 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006828 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006829 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006830 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006831 struct extent_buffer *leaf;
6832 int type;
6833 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006835 if (parent > 0)
6836 type = BTRFS_SHARED_DATA_REF_KEY;
6837 else
6838 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006839
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006840 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006841
6842 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006843 if (!path)
6844 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006845
Chris Masonb9473432009-03-13 11:00:37 -04006846 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006847 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6848 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006849 if (ret) {
6850 btrfs_free_path(path);
6851 return ret;
6852 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006853
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006854 leaf = path->nodes[0];
6855 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006856 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006857 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6858 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6859 btrfs_set_extent_flags(leaf, extent_item,
6860 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006861
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006862 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6863 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6864 if (parent > 0) {
6865 struct btrfs_shared_data_ref *ref;
6866 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6867 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6868 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6869 } else {
6870 struct btrfs_extent_data_ref *ref;
6871 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6872 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6873 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6874 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6875 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6876 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006877
6878 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006879 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006880
Josef Bacikfcebe452014-05-13 17:30:47 -07006881 /* Always set parent to 0 here since its exclusive anyway. */
6882 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
6883 ins->objectid, ins->offset,
6884 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
6885 if (ret)
6886 return ret;
6887
Liu Boc53d6132012-12-27 09:01:19 +00006888 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006889 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006890 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006891 ins->objectid, ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006892 BUG();
6893 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006894 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006895 return ret;
6896}
6897
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006898static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6899 struct btrfs_root *root,
6900 u64 parent, u64 root_objectid,
6901 u64 flags, struct btrfs_disk_key *key,
Josef Bacikfcebe452014-05-13 17:30:47 -07006902 int level, struct btrfs_key *ins,
6903 int no_quota)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006904{
6905 int ret;
6906 struct btrfs_fs_info *fs_info = root->fs_info;
6907 struct btrfs_extent_item *extent_item;
6908 struct btrfs_tree_block_info *block_info;
6909 struct btrfs_extent_inline_ref *iref;
6910 struct btrfs_path *path;
6911 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006912 u32 size = sizeof(*extent_item) + sizeof(*iref);
Josef Bacikfcebe452014-05-13 17:30:47 -07006913 u64 num_bytes = ins->offset;
Josef Bacik3173a182013-03-07 14:22:04 -05006914 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6915 SKINNY_METADATA);
6916
6917 if (!skinny_metadata)
6918 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006919
6920 path = btrfs_alloc_path();
Josef Bacik857cc2f2013-10-07 15:21:08 -04006921 if (!path) {
6922 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6923 root->leafsize);
Mark Fashehd8926bb2011-07-13 10:38:47 -07006924 return -ENOMEM;
Josef Bacik857cc2f2013-10-07 15:21:08 -04006925 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006926
6927 path->leave_spinning = 1;
6928 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6929 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006930 if (ret) {
Josef Bacik857cc2f2013-10-07 15:21:08 -04006931 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
6932 root->leafsize);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006933 btrfs_free_path(path);
6934 return ret;
6935 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006936
6937 leaf = path->nodes[0];
6938 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6939 struct btrfs_extent_item);
6940 btrfs_set_extent_refs(leaf, extent_item, 1);
6941 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6942 btrfs_set_extent_flags(leaf, extent_item,
6943 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006944
Josef Bacik3173a182013-03-07 14:22:04 -05006945 if (skinny_metadata) {
6946 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
Josef Bacikfcebe452014-05-13 17:30:47 -07006947 num_bytes = root->leafsize;
Josef Bacik3173a182013-03-07 14:22:04 -05006948 } else {
6949 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6950 btrfs_set_tree_block_key(leaf, block_info, key);
6951 btrfs_set_tree_block_level(leaf, block_info, level);
6952 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6953 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006954
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006955 if (parent > 0) {
6956 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6957 btrfs_set_extent_inline_ref_type(leaf, iref,
6958 BTRFS_SHARED_BLOCK_REF_KEY);
6959 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6960 } else {
6961 btrfs_set_extent_inline_ref_type(leaf, iref,
6962 BTRFS_TREE_BLOCK_REF_KEY);
6963 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6964 }
6965
6966 btrfs_mark_buffer_dirty(leaf);
6967 btrfs_free_path(path);
6968
Josef Bacikfcebe452014-05-13 17:30:47 -07006969 if (!no_quota) {
6970 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
6971 ins->objectid, num_bytes,
6972 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
6973 if (ret)
6974 return ret;
6975 }
6976
Josef Bacik3173a182013-03-07 14:22:04 -05006977 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006978 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006979 btrfs_err(fs_info, "update block group failed for %llu %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02006980 ins->objectid, ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006981 BUG();
6982 }
Josef Bacik0be5dc62013-10-07 15:18:52 -04006983
6984 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006985 return ret;
6986}
6987
6988int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6989 struct btrfs_root *root,
6990 u64 root_objectid, u64 owner,
6991 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006992{
6993 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006994
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006995 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006996
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006997 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6998 ins->offset, 0,
6999 root_objectid, owner, offset,
7000 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007001 return ret;
7002}
Chris Masone02119d2008-09-05 16:13:11 -04007003
7004/*
7005 * this is used by the tree logging recovery code. It records that
7006 * an extent has been allocated and makes sure to clear the free
7007 * space cache bits as well
7008 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007009int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7010 struct btrfs_root *root,
7011 u64 root_objectid, u64 owner, u64 offset,
7012 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04007013{
7014 int ret;
7015 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007016
7017 /*
7018 * Mixed block groups will exclude before processing the log so we only
7019 * need to do the exlude dance if this fs isn't mixed.
7020 */
7021 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7022 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7023 if (ret)
7024 return ret;
7025 }
Chris Masone02119d2008-09-05 16:13:11 -04007026
Chris Masone02119d2008-09-05 16:13:11 -04007027 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04007028 if (!block_group)
7029 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04007030
Josef Bacikfb25e912011-07-26 17:00:46 -04007031 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
7032 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007033 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007034 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7035 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04007036 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04007037 return ret;
7038}
7039
Eric Sandeen48a3b632013-04-25 20:41:01 +00007040static struct extent_buffer *
7041btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7042 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04007043{
7044 struct extent_buffer *buf;
7045
7046 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
7047 if (!buf)
7048 return ERR_PTR(-ENOMEM);
7049 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04007050 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04007051 btrfs_tree_lock(buf);
7052 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05007053 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007054
7055 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04007056 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05007057
Chris Masond0c803c2008-09-11 16:17:57 -04007058 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00007059 /*
7060 * we allow two log transactions at a time, use different
7061 * EXENT bit to differentiate dirty pages.
7062 */
7063 if (root->log_transid % 2 == 0)
7064 set_extent_dirty(&root->dirty_log_pages, buf->start,
7065 buf->start + buf->len - 1, GFP_NOFS);
7066 else
7067 set_extent_new(&root->dirty_log_pages, buf->start,
7068 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04007069 } else {
7070 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7071 buf->start + buf->len - 1, GFP_NOFS);
7072 }
Chris Mason65b51a02008-08-01 15:11:20 -04007073 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05007074 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04007075 return buf;
7076}
7077
Yan, Zhengf0486c62010-05-16 10:46:25 -04007078static struct btrfs_block_rsv *
7079use_block_rsv(struct btrfs_trans_handle *trans,
7080 struct btrfs_root *root, u32 blocksize)
7081{
7082 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00007083 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007084 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00007085 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007086
7087 block_rsv = get_block_rsv(trans, root);
7088
Miao Xieb586b322013-05-13 13:55:10 +00007089 if (unlikely(block_rsv->size == 0))
7090 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00007091again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007092 ret = block_rsv_use_bytes(block_rsv, blocksize);
7093 if (!ret)
7094 return block_rsv;
7095
Miao Xieb586b322013-05-13 13:55:10 +00007096 if (block_rsv->failfast)
7097 return ERR_PTR(ret);
7098
Miao Xied88033d2013-05-13 13:55:12 +00007099 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7100 global_updated = true;
7101 update_global_block_rsv(root->fs_info);
7102 goto again;
7103 }
7104
Miao Xieb586b322013-05-13 13:55:10 +00007105 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7106 static DEFINE_RATELIMIT_STATE(_rs,
7107 DEFAULT_RATELIMIT_INTERVAL * 10,
7108 /*DEFAULT_RATELIMIT_BURST*/ 1);
7109 if (__ratelimit(&_rs))
7110 WARN(1, KERN_DEBUG
Frank Holtonefe120a2013-12-20 11:37:06 -05007111 "BTRFS: block rsv returned %d\n", ret);
Miao Xieb586b322013-05-13 13:55:10 +00007112 }
7113try_reserve:
7114 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7115 BTRFS_RESERVE_NO_FLUSH);
7116 if (!ret)
7117 return block_rsv;
7118 /*
7119 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00007120 * the global reserve if its space type is the same as the global
7121 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00007122 */
Miao Xie5881cfc2013-05-13 13:55:11 +00007123 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7124 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00007125 ret = block_rsv_use_bytes(global_rsv, blocksize);
7126 if (!ret)
7127 return global_rsv;
7128 }
7129 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007130}
7131
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007132static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7133 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04007134{
7135 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007136 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007137}
7138
Chris Masonfec577f2007-02-26 10:40:21 -05007139/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04007140 * finds a free extent and does all the dirty work required for allocation
7141 * returns the key for the extent through ins, and a tree buffer for
7142 * the first block of the extent through buf.
7143 *
Chris Masonfec577f2007-02-26 10:40:21 -05007144 * returns the tree buffer or NULL.
7145 */
Chris Mason5f39d392007-10-15 16:14:19 -04007146struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007147 struct btrfs_root *root, u32 blocksize,
7148 u64 parent, u64 root_objectid,
7149 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02007150 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05007151{
Chris Masone2fa7222007-03-12 16:22:34 -04007152 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007153 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04007154 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007155 u64 flags = 0;
7156 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05007157 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7158 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007159
7160 block_rsv = use_block_rsv(trans, root, blocksize);
7161 if (IS_ERR(block_rsv))
7162 return ERR_CAST(block_rsv);
7163
Josef Bacik00361582013-08-14 14:02:47 -04007164 ret = btrfs_reserve_extent(root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05007165 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05007166 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05007167 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04007168 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05007169 }
Chris Mason55c69072008-01-09 15:55:33 -05007170
Chris Mason4008c042009-02-12 14:09:45 -05007171 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7172 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007173 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007174
7175 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7176 if (parent == 0)
7177 parent = ins.objectid;
7178 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7179 } else
7180 BUG_ON(parent > 0);
7181
7182 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7183 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00007184 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007185 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007186 if (key)
7187 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7188 else
7189 memset(&extent_op->key, 0, sizeof(extent_op->key));
7190 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05007191 if (skinny_metadata)
7192 extent_op->update_key = 0;
7193 else
7194 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007195 extent_op->update_flags = 1;
7196 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04007197 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007198
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007199 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7200 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007201 ins.offset, parent, root_objectid,
7202 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02007203 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007204 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04007205 }
Chris Masonfec577f2007-02-26 10:40:21 -05007206 return buf;
7207}
Chris Masona28ec192007-03-06 20:08:01 -05007208
Yan Zheng2c47e6052009-06-27 21:07:35 -04007209struct walk_control {
7210 u64 refs[BTRFS_MAX_LEVEL];
7211 u64 flags[BTRFS_MAX_LEVEL];
7212 struct btrfs_key update_progress;
7213 int stage;
7214 int level;
7215 int shared_level;
7216 int update_ref;
7217 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007218 int reada_slot;
7219 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007220 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007221};
7222
7223#define DROP_REFERENCE 1
7224#define UPDATE_BACKREF 2
7225
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007226static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7227 struct btrfs_root *root,
7228 struct walk_control *wc,
7229 struct btrfs_path *path)
7230{
7231 u64 bytenr;
7232 u64 generation;
7233 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007234 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007235 u32 nritems;
7236 u32 blocksize;
7237 struct btrfs_key key;
7238 struct extent_buffer *eb;
7239 int ret;
7240 int slot;
7241 int nread = 0;
7242
7243 if (path->slots[wc->level] < wc->reada_slot) {
7244 wc->reada_count = wc->reada_count * 2 / 3;
7245 wc->reada_count = max(wc->reada_count, 2);
7246 } else {
7247 wc->reada_count = wc->reada_count * 3 / 2;
7248 wc->reada_count = min_t(int, wc->reada_count,
7249 BTRFS_NODEPTRS_PER_BLOCK(root));
7250 }
7251
7252 eb = path->nodes[wc->level];
7253 nritems = btrfs_header_nritems(eb);
7254 blocksize = btrfs_level_size(root, wc->level - 1);
7255
7256 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7257 if (nread >= wc->reada_count)
7258 break;
7259
7260 cond_resched();
7261 bytenr = btrfs_node_blockptr(eb, slot);
7262 generation = btrfs_node_ptr_generation(eb, slot);
7263
7264 if (slot == path->slots[wc->level])
7265 goto reada;
7266
7267 if (wc->stage == UPDATE_BACKREF &&
7268 generation <= root->root_key.offset)
7269 continue;
7270
Yan, Zheng94fcca92009-10-09 09:25:16 -04007271 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007272 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7273 wc->level - 1, 1, &refs,
7274 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007275 /* We don't care about errors in readahead. */
7276 if (ret < 0)
7277 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007278 BUG_ON(refs == 0);
7279
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007280 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007281 if (refs == 1)
7282 goto reada;
7283
Yan, Zheng94fcca92009-10-09 09:25:16 -04007284 if (wc->level == 1 &&
7285 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7286 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007287 if (!wc->update_ref ||
7288 generation <= root->root_key.offset)
7289 continue;
7290 btrfs_node_key_to_cpu(eb, &key, slot);
7291 ret = btrfs_comp_cpu_keys(&key,
7292 &wc->update_progress);
7293 if (ret < 0)
7294 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007295 } else {
7296 if (wc->level == 1 &&
7297 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7298 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007299 }
7300reada:
7301 ret = readahead_tree_block(root, bytenr, blocksize,
7302 generation);
7303 if (ret)
7304 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007305 nread++;
7306 }
7307 wc->reada_slot = slot;
7308}
7309
Chris Mason9aca1d52007-03-13 11:09:37 -04007310/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007311 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007312 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007313 * when wc->stage == UPDATE_BACKREF, this function updates
7314 * back refs for pointers in the block.
7315 *
7316 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007317 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007318static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7319 struct btrfs_root *root,
7320 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007321 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007322{
7323 int level = wc->level;
7324 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007325 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7326 int ret;
7327
7328 if (wc->stage == UPDATE_BACKREF &&
7329 btrfs_header_owner(eb) != root->root_key.objectid)
7330 return 1;
7331
7332 /*
7333 * when reference count of tree block is 1, it won't increase
7334 * again. once full backref flag is set, we never clear it.
7335 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007336 if (lookup_info &&
7337 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7338 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007339 BUG_ON(!path->locks[level]);
7340 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007341 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007342 &wc->refs[level],
7343 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007344 BUG_ON(ret == -ENOMEM);
7345 if (ret)
7346 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007347 BUG_ON(wc->refs[level] == 0);
7348 }
7349
Yan Zheng2c47e6052009-06-27 21:07:35 -04007350 if (wc->stage == DROP_REFERENCE) {
7351 if (wc->refs[level] > 1)
7352 return 1;
7353
7354 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007355 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007356 path->locks[level] = 0;
7357 }
7358 return 0;
7359 }
7360
7361 /* wc->stage == UPDATE_BACKREF */
7362 if (!(wc->flags[level] & flag)) {
7363 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007364 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007365 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007366 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007367 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007368 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007369 eb->len, flag,
7370 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007371 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007372 wc->flags[level] |= flag;
7373 }
7374
7375 /*
7376 * the block is shared by multiple trees, so it's not good to
7377 * keep the tree lock
7378 */
7379 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007380 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007381 path->locks[level] = 0;
7382 }
7383 return 0;
7384}
7385
7386/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007387 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007388 *
7389 * when wc->stage == DROP_REFERENCE, this function checks
7390 * reference count of the block pointed to. if the block
7391 * is shared and we need update back refs for the subtree
7392 * rooted at the block, this function changes wc->stage to
7393 * UPDATE_BACKREF. if the block is shared and there is no
7394 * need to update back, this function drops the reference
7395 * to the block.
7396 *
7397 * NOTE: return value 1 means we should stop walking down.
7398 */
7399static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7400 struct btrfs_root *root,
7401 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007402 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007403{
7404 u64 bytenr;
7405 u64 generation;
7406 u64 parent;
7407 u32 blocksize;
7408 struct btrfs_key key;
7409 struct extent_buffer *next;
7410 int level = wc->level;
7411 int reada = 0;
7412 int ret = 0;
7413
7414 generation = btrfs_node_ptr_generation(path->nodes[level],
7415 path->slots[level]);
7416 /*
7417 * if the lower level block was created before the snapshot
7418 * was created, we know there is no need to update back refs
7419 * for the subtree
7420 */
7421 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007422 generation <= root->root_key.offset) {
7423 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007424 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007425 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007426
7427 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7428 blocksize = btrfs_level_size(root, level - 1);
7429
7430 next = btrfs_find_tree_block(root, bytenr, blocksize);
7431 if (!next) {
7432 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007433 if (!next)
7434 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007435 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7436 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007437 reada = 1;
7438 }
7439 btrfs_tree_lock(next);
7440 btrfs_set_lock_blocking(next);
7441
Josef Bacik3173a182013-03-07 14:22:04 -05007442 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007443 &wc->refs[level - 1],
7444 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007445 if (ret < 0) {
7446 btrfs_tree_unlock(next);
7447 return ret;
7448 }
7449
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007450 if (unlikely(wc->refs[level - 1] == 0)) {
7451 btrfs_err(root->fs_info, "Missing references.");
7452 BUG();
7453 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007454 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007455
Yan, Zheng94fcca92009-10-09 09:25:16 -04007456 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007457 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007458 if (level == 1 &&
7459 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7460 goto skip;
7461
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007462 if (!wc->update_ref ||
7463 generation <= root->root_key.offset)
7464 goto skip;
7465
7466 btrfs_node_key_to_cpu(path->nodes[level], &key,
7467 path->slots[level]);
7468 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7469 if (ret < 0)
7470 goto skip;
7471
7472 wc->stage = UPDATE_BACKREF;
7473 wc->shared_level = level - 1;
7474 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007475 } else {
7476 if (level == 1 &&
7477 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7478 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007479 }
7480
Chris Masonb9fab912012-05-06 07:23:47 -04007481 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007482 btrfs_tree_unlock(next);
7483 free_extent_buffer(next);
7484 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007485 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007486 }
7487
7488 if (!next) {
7489 if (reada && level == 1)
7490 reada_walk_down(trans, root, wc, path);
7491 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007492 if (!next || !extent_buffer_uptodate(next)) {
7493 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007494 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007495 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007496 btrfs_tree_lock(next);
7497 btrfs_set_lock_blocking(next);
7498 }
7499
7500 level--;
7501 BUG_ON(level != btrfs_header_level(next));
7502 path->nodes[level] = next;
7503 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007504 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007505 wc->level = level;
7506 if (wc->level == 1)
7507 wc->reada_slot = 0;
7508 return 0;
7509skip:
7510 wc->refs[level - 1] = 0;
7511 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007512 if (wc->stage == DROP_REFERENCE) {
7513 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7514 parent = path->nodes[level]->start;
7515 } else {
7516 BUG_ON(root->root_key.objectid !=
7517 btrfs_header_owner(path->nodes[level]));
7518 parent = 0;
7519 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007520
Yan, Zheng94fcca92009-10-09 09:25:16 -04007521 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007522 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007523 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007524 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007525 btrfs_tree_unlock(next);
7526 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007527 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007528 return 1;
7529}
7530
7531/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007532 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007533 *
7534 * when wc->stage == DROP_REFERENCE, this function drops
7535 * reference count on the block.
7536 *
7537 * when wc->stage == UPDATE_BACKREF, this function changes
7538 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7539 * to UPDATE_BACKREF previously while processing the block.
7540 *
7541 * NOTE: return value 1 means we should stop walking up.
7542 */
7543static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7544 struct btrfs_root *root,
7545 struct btrfs_path *path,
7546 struct walk_control *wc)
7547{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007548 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007549 int level = wc->level;
7550 struct extent_buffer *eb = path->nodes[level];
7551 u64 parent = 0;
7552
7553 if (wc->stage == UPDATE_BACKREF) {
7554 BUG_ON(wc->shared_level < level);
7555 if (level < wc->shared_level)
7556 goto out;
7557
Yan Zheng2c47e6052009-06-27 21:07:35 -04007558 ret = find_next_key(path, level + 1, &wc->update_progress);
7559 if (ret > 0)
7560 wc->update_ref = 0;
7561
7562 wc->stage = DROP_REFERENCE;
7563 wc->shared_level = -1;
7564 path->slots[level] = 0;
7565
7566 /*
7567 * check reference count again if the block isn't locked.
7568 * we should start walking down the tree again if reference
7569 * count is one.
7570 */
7571 if (!path->locks[level]) {
7572 BUG_ON(level == 0);
7573 btrfs_tree_lock(eb);
7574 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007575 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007576
7577 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007578 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007579 &wc->refs[level],
7580 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007581 if (ret < 0) {
7582 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007583 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007584 return ret;
7585 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007586 BUG_ON(wc->refs[level] == 0);
7587 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007588 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007589 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007590 return 1;
7591 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007592 }
7593 }
7594
7595 /* wc->stage == DROP_REFERENCE */
7596 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7597
7598 if (wc->refs[level] == 1) {
7599 if (level == 0) {
7600 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007601 ret = btrfs_dec_ref(trans, root, eb, 1,
7602 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007603 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007604 ret = btrfs_dec_ref(trans, root, eb, 0,
7605 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007606 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007607 }
7608 /* make block locked assertion in clean_tree_block happy */
7609 if (!path->locks[level] &&
7610 btrfs_header_generation(eb) == trans->transid) {
7611 btrfs_tree_lock(eb);
7612 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007613 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007614 }
7615 clean_tree_block(trans, root, eb);
7616 }
7617
7618 if (eb == root->node) {
7619 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7620 parent = eb->start;
7621 else
7622 BUG_ON(root->root_key.objectid !=
7623 btrfs_header_owner(eb));
7624 } else {
7625 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7626 parent = path->nodes[level + 1]->start;
7627 else
7628 BUG_ON(root->root_key.objectid !=
7629 btrfs_header_owner(path->nodes[level + 1]));
7630 }
7631
Jan Schmidt5581a512012-05-16 17:04:52 +02007632 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007633out:
7634 wc->refs[level] = 0;
7635 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007636 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007637}
7638
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007639static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7640 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007641 struct btrfs_path *path,
7642 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007643{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007644 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007645 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007646 int ret;
7647
Yan Zheng2c47e6052009-06-27 21:07:35 -04007648 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007649 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007650 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007651 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007652
Yan Zheng2c47e6052009-06-27 21:07:35 -04007653 if (level == 0)
7654 break;
7655
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007656 if (path->slots[level] >=
7657 btrfs_header_nritems(path->nodes[level]))
7658 break;
7659
Yan, Zheng94fcca92009-10-09 09:25:16 -04007660 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007661 if (ret > 0) {
7662 path->slots[level]++;
7663 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007664 } else if (ret < 0)
7665 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007666 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007667 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007668 return 0;
7669}
7670
Chris Masond3977122009-01-05 21:25:51 -05007671static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007672 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007673 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007674 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007675{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007676 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007677 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007678
Yan Zheng2c47e6052009-06-27 21:07:35 -04007679 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7680 while (level < max_level && path->nodes[level]) {
7681 wc->level = level;
7682 if (path->slots[level] + 1 <
7683 btrfs_header_nritems(path->nodes[level])) {
7684 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007685 return 0;
7686 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007687 ret = walk_up_proc(trans, root, path, wc);
7688 if (ret > 0)
7689 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007690
Yan Zheng2c47e6052009-06-27 21:07:35 -04007691 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007692 btrfs_tree_unlock_rw(path->nodes[level],
7693 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007694 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007695 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007696 free_extent_buffer(path->nodes[level]);
7697 path->nodes[level] = NULL;
7698 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007699 }
7700 }
7701 return 1;
7702}
7703
Chris Mason9aca1d52007-03-13 11:09:37 -04007704/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007705 * drop a subvolume tree.
7706 *
7707 * this function traverses the tree freeing any blocks that only
7708 * referenced by the tree.
7709 *
7710 * when a shared tree block is found. this function decreases its
7711 * reference count by one. if update_ref is true, this function
7712 * also make sure backrefs for the shared block and all lower level
7713 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007714 *
7715 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007716 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007717int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007718 struct btrfs_block_rsv *block_rsv, int update_ref,
7719 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007720{
Chris Mason5caf2a02007-04-02 11:20:42 -04007721 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007722 struct btrfs_trans_handle *trans;
7723 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007724 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007725 struct walk_control *wc;
7726 struct btrfs_key key;
7727 int err = 0;
7728 int ret;
7729 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04007730 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05007731
Chris Mason5caf2a02007-04-02 11:20:42 -04007732 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007733 if (!path) {
7734 err = -ENOMEM;
7735 goto out;
7736 }
Chris Mason20524f02007-03-10 06:35:47 -05007737
Yan Zheng2c47e6052009-06-27 21:07:35 -04007738 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007739 if (!wc) {
7740 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007741 err = -ENOMEM;
7742 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007743 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007744
Yan, Zhenga22285a2010-05-16 10:48:46 -04007745 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007746 if (IS_ERR(trans)) {
7747 err = PTR_ERR(trans);
7748 goto out_free;
7749 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007750
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007751 if (block_rsv)
7752 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007753
Chris Mason9f3a7422007-08-07 15:52:19 -04007754 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007755 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007756 path->nodes[level] = btrfs_lock_root_node(root);
7757 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007758 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007759 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007760 memset(&wc->update_progress, 0,
7761 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007762 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007763 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007764 memcpy(&wc->update_progress, &key,
7765 sizeof(wc->update_progress));
7766
Chris Mason6702ed42007-08-07 16:15:09 -04007767 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007768 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007769 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007770 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7771 path->lowest_level = 0;
7772 if (ret < 0) {
7773 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007774 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007775 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007776 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007777
Chris Mason7d9eb122008-07-08 14:19:17 -04007778 /*
7779 * unlock our path, this is safe because only this
7780 * function is allowed to delete this snapshot
7781 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007782 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007783
Yan Zheng2c47e6052009-06-27 21:07:35 -04007784 level = btrfs_header_level(root->node);
7785 while (1) {
7786 btrfs_tree_lock(path->nodes[level]);
7787 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007788 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007789
7790 ret = btrfs_lookup_extent_info(trans, root,
7791 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007792 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007793 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007794 if (ret < 0) {
7795 err = ret;
7796 goto out_end_trans;
7797 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007798 BUG_ON(wc->refs[level] == 0);
7799
7800 if (level == root_item->drop_level)
7801 break;
7802
7803 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007804 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007805 WARN_ON(wc->refs[level] != 1);
7806 level--;
7807 }
7808 }
7809
7810 wc->level = level;
7811 wc->shared_level = -1;
7812 wc->stage = DROP_REFERENCE;
7813 wc->update_ref = update_ref;
7814 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007815 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007816 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007817
7818 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007819
Yan Zheng2c47e6052009-06-27 21:07:35 -04007820 ret = walk_down_tree(trans, root, path, wc);
7821 if (ret < 0) {
7822 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007823 break;
7824 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007825
7826 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7827 if (ret < 0) {
7828 err = ret;
7829 break;
7830 }
7831
7832 if (ret > 0) {
7833 BUG_ON(wc->stage != DROP_REFERENCE);
7834 break;
7835 }
7836
7837 if (wc->stage == DROP_REFERENCE) {
7838 level = wc->level;
7839 btrfs_node_key(path->nodes[level],
7840 &root_item->drop_progress,
7841 path->slots[level]);
7842 root_item->drop_level = level;
7843 }
7844
7845 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007846 if (btrfs_should_end_transaction(trans, tree_root) ||
7847 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007848 ret = btrfs_update_root(trans, tree_root,
7849 &root->root_key,
7850 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007851 if (ret) {
7852 btrfs_abort_transaction(trans, tree_root, ret);
7853 err = ret;
7854 goto out_end_trans;
7855 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007856
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007857 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007858 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
Frank Holtonefe120a2013-12-20 11:37:06 -05007859 pr_debug("BTRFS: drop snapshot early exit\n");
Josef Bacik3c8f2422013-07-15 11:57:06 -04007860 err = -EAGAIN;
7861 goto out_free;
7862 }
7863
Yan, Zhenga22285a2010-05-16 10:48:46 -04007864 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007865 if (IS_ERR(trans)) {
7866 err = PTR_ERR(trans);
7867 goto out_free;
7868 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007869 if (block_rsv)
7870 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007871 }
Chris Mason20524f02007-03-10 06:35:47 -05007872 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007873 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007874 if (err)
7875 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007876
7877 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007878 if (ret) {
7879 btrfs_abort_transaction(trans, tree_root, ret);
7880 goto out_end_trans;
7881 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007882
Yan, Zheng76dda932009-09-21 16:00:26 -04007883 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007884 ret = btrfs_find_root(tree_root, &root->root_key, path,
7885 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007886 if (ret < 0) {
7887 btrfs_abort_transaction(trans, tree_root, ret);
7888 err = ret;
7889 goto out_end_trans;
7890 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007891 /* if we fail to delete the orphan item this time
7892 * around, it'll get picked up the next time.
7893 *
7894 * The most common failure here is just -ENOENT.
7895 */
7896 btrfs_del_orphan_item(trans, tree_root,
7897 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007898 }
7899 }
7900
Miao Xie27cdeb72014-04-02 19:51:05 +08007901 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007902 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007903 } else {
7904 free_extent_buffer(root->node);
7905 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007906 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007907 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007908 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007909out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007910 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007911out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007912 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007913 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007914out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007915 /*
7916 * So if we need to stop dropping the snapshot for whatever reason we
7917 * need to make sure to add it back to the dead root list so that we
7918 * keep trying to do the work later. This also cleans up roots if we
7919 * don't have it in the radix (like when we recover after a power fail
7920 * or unmount) so we don't leak memory.
7921 */
Josef Bacikb37b39c2013-07-23 16:57:15 -04007922 if (!for_reloc && root_dropped == false)
Josef Bacikd29a9f62013-07-17 19:30:20 -04007923 btrfs_add_dead_root(root);
Wang Shilong90515e72014-01-07 17:26:58 +08007924 if (err && err != -EAGAIN)
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007925 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007926 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007927}
Chris Mason9078a3e2007-04-26 16:46:15 -04007928
Yan Zheng2c47e6052009-06-27 21:07:35 -04007929/*
7930 * drop subtree rooted at tree block 'node'.
7931 *
7932 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007933 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007934 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007935int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7936 struct btrfs_root *root,
7937 struct extent_buffer *node,
7938 struct extent_buffer *parent)
7939{
7940 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007941 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007942 int level;
7943 int parent_level;
7944 int ret = 0;
7945 int wret;
7946
Yan Zheng2c47e6052009-06-27 21:07:35 -04007947 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7948
Yan Zhengf82d02d2008-10-29 14:49:05 -04007949 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007950 if (!path)
7951 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007952
Yan Zheng2c47e6052009-06-27 21:07:35 -04007953 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007954 if (!wc) {
7955 btrfs_free_path(path);
7956 return -ENOMEM;
7957 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007958
Chris Masonb9447ef2009-03-09 11:45:38 -04007959 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007960 parent_level = btrfs_header_level(parent);
7961 extent_buffer_get(parent);
7962 path->nodes[parent_level] = parent;
7963 path->slots[parent_level] = btrfs_header_nritems(parent);
7964
Chris Masonb9447ef2009-03-09 11:45:38 -04007965 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007966 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007967 path->nodes[level] = node;
7968 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007969 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007970
7971 wc->refs[parent_level] = 1;
7972 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7973 wc->level = level;
7974 wc->shared_level = -1;
7975 wc->stage = DROP_REFERENCE;
7976 wc->update_ref = 0;
7977 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007978 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007979 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007980
7981 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007982 wret = walk_down_tree(trans, root, path, wc);
7983 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007984 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007985 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007986 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007987
Yan Zheng2c47e6052009-06-27 21:07:35 -04007988 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007989 if (wret < 0)
7990 ret = wret;
7991 if (wret != 0)
7992 break;
7993 }
7994
Yan Zheng2c47e6052009-06-27 21:07:35 -04007995 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007996 btrfs_free_path(path);
7997 return ret;
7998}
7999
Chris Masonec44a352008-04-28 15:29:52 -04008000static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8001{
8002 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008003 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04008004
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008005 /*
8006 * if restripe for this chunk_type is on pick target profile and
8007 * return, otherwise do the usual balance
8008 */
8009 stripped = get_restripe_target(root->fs_info, flags);
8010 if (stripped)
8011 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02008012
Chris Masoncd02dca2010-12-13 14:56:23 -05008013 /*
8014 * we add in the count of missing devices because we want
8015 * to make sure that any RAID levels on a degraded FS
8016 * continue to be honored.
8017 */
8018 num_devices = root->fs_info->fs_devices->rw_devices +
8019 root->fs_info->fs_devices->missing_devices;
8020
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008021 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008022 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03008023 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8024
Chris Masonec44a352008-04-28 15:29:52 -04008025 if (num_devices == 1) {
8026 stripped |= BTRFS_BLOCK_GROUP_DUP;
8027 stripped = flags & ~stripped;
8028
8029 /* turn raid0 into single device chunks */
8030 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8031 return stripped;
8032
8033 /* turn mirroring into duplication */
8034 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8035 BTRFS_BLOCK_GROUP_RAID10))
8036 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04008037 } else {
8038 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04008039 if (flags & stripped)
8040 return flags;
8041
8042 stripped |= BTRFS_BLOCK_GROUP_DUP;
8043 stripped = flags & ~stripped;
8044
8045 /* switch duplicated blocks with raid1 */
8046 if (flags & BTRFS_BLOCK_GROUP_DUP)
8047 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8048
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008049 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04008050 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03008051
Chris Masonec44a352008-04-28 15:29:52 -04008052 return flags;
8053}
8054
Miao Xie199c36e2011-07-15 10:34:36 +00008055static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04008056{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008057 struct btrfs_space_info *sinfo = cache->space_info;
8058 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00008059 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008060 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04008061
Chris Masonc286ac42008-07-22 23:06:41 -04008062
Miao Xie199c36e2011-07-15 10:34:36 +00008063 /*
8064 * We need some metadata space and system metadata space for
8065 * allocating chunks in some corner cases until we force to set
8066 * it to be readonly.
8067 */
8068 if ((sinfo->flags &
8069 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
8070 !force)
8071 min_allocable_bytes = 1 * 1024 * 1024;
8072 else
8073 min_allocable_bytes = 0;
8074
Yan, Zhengf0486c62010-05-16 10:46:25 -04008075 spin_lock(&sinfo->lock);
8076 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00008077
8078 if (cache->ro) {
8079 ret = 0;
8080 goto out;
8081 }
8082
Yan, Zhengf0486c62010-05-16 10:46:25 -04008083 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8084 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04008085
Yan, Zhengf0486c62010-05-16 10:46:25 -04008086 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04008087 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
8088 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04008089 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008090 cache->ro = 1;
8091 ret = 0;
8092 }
WuBo61cfea92011-07-26 03:30:11 +00008093out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04008094 spin_unlock(&cache->lock);
8095 spin_unlock(&sinfo->lock);
8096 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04008097}
8098
Yan, Zhengf0486c62010-05-16 10:46:25 -04008099int btrfs_set_block_group_ro(struct btrfs_root *root,
8100 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008101
8102{
Yan, Zhengf0486c62010-05-16 10:46:25 -04008103 struct btrfs_trans_handle *trans;
8104 u64 alloc_flags;
8105 int ret;
8106
8107 BUG_ON(cache->ro);
8108
Josef Bacik7a7eaa42011-04-13 12:54:33 -04008109 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008110 if (IS_ERR(trans))
8111 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008112
8113 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008114 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04008115 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008116 CHUNK_ALLOC_FORCE);
8117 if (ret < 0)
8118 goto out;
8119 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008120
Miao Xie199c36e2011-07-15 10:34:36 +00008121 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008122 if (!ret)
8123 goto out;
8124 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04008125 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008126 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008127 if (ret < 0)
8128 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00008129 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008130out:
8131 btrfs_end_transaction(trans, root);
8132 return ret;
8133}
8134
Chris Masonc87f08c2011-02-16 13:57:04 -05008135int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8136 struct btrfs_root *root, u64 type)
8137{
8138 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04008139 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04008140 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05008141}
8142
Miao Xie6d07bce2011-01-05 10:07:31 +00008143/*
8144 * helper to account the unused space of all the readonly block group in the
8145 * list. takes mirrors into account.
8146 */
8147static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8148{
8149 struct btrfs_block_group_cache *block_group;
8150 u64 free_bytes = 0;
8151 int factor;
8152
8153 list_for_each_entry(block_group, groups_list, list) {
8154 spin_lock(&block_group->lock);
8155
8156 if (!block_group->ro) {
8157 spin_unlock(&block_group->lock);
8158 continue;
8159 }
8160
8161 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8162 BTRFS_BLOCK_GROUP_RAID10 |
8163 BTRFS_BLOCK_GROUP_DUP))
8164 factor = 2;
8165 else
8166 factor = 1;
8167
8168 free_bytes += (block_group->key.offset -
8169 btrfs_block_group_used(&block_group->item)) *
8170 factor;
8171
8172 spin_unlock(&block_group->lock);
8173 }
8174
8175 return free_bytes;
8176}
8177
8178/*
8179 * helper to account the unused space of all the readonly block group in the
8180 * space_info. takes mirrors into account.
8181 */
8182u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8183{
8184 int i;
8185 u64 free_bytes = 0;
8186
8187 spin_lock(&sinfo->lock);
8188
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308189 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
Miao Xie6d07bce2011-01-05 10:07:31 +00008190 if (!list_empty(&sinfo->block_groups[i]))
8191 free_bytes += __btrfs_get_ro_block_group_free_space(
8192 &sinfo->block_groups[i]);
8193
8194 spin_unlock(&sinfo->lock);
8195
8196 return free_bytes;
8197}
8198
Jeff Mahoney143bede2012-03-01 14:56:26 +01008199void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04008200 struct btrfs_block_group_cache *cache)
8201{
8202 struct btrfs_space_info *sinfo = cache->space_info;
8203 u64 num_bytes;
8204
8205 BUG_ON(!cache->ro);
8206
8207 spin_lock(&sinfo->lock);
8208 spin_lock(&cache->lock);
8209 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8210 cache->bytes_super - btrfs_block_group_used(&cache->item);
8211 sinfo->bytes_readonly -= num_bytes;
8212 cache->ro = 0;
8213 spin_unlock(&cache->lock);
8214 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008215}
8216
Josef Bacikba1bf482009-09-11 16:11:19 -04008217/*
8218 * checks to see if its even possible to relocate this block group.
8219 *
8220 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8221 * ok to go ahead and try.
8222 */
8223int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008224{
Zheng Yan1a40e232008-09-26 10:09:34 -04008225 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008226 struct btrfs_space_info *space_info;
8227 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8228 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008229 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008230 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008231 u64 dev_min = 1;
8232 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008233 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008234 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008235 int full = 0;
8236 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008237
Josef Bacikba1bf482009-09-11 16:11:19 -04008238 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008239
Josef Bacikba1bf482009-09-11 16:11:19 -04008240 /* odd, couldn't find the block group, leave it alone */
8241 if (!block_group)
8242 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008243
liubocdcb7252011-08-03 10:15:25 +00008244 min_free = btrfs_block_group_used(&block_group->item);
8245
Josef Bacikba1bf482009-09-11 16:11:19 -04008246 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008247 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008248 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008249
Josef Bacikba1bf482009-09-11 16:11:19 -04008250 space_info = block_group->space_info;
8251 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008252
Josef Bacikba1bf482009-09-11 16:11:19 -04008253 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008254
Josef Bacikba1bf482009-09-11 16:11:19 -04008255 /*
8256 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008257 * relocate it unless we're able to allocate a new chunk below.
8258 *
8259 * Otherwise, we need to make sure we have room in the space to handle
8260 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008261 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008262 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008263 (space_info->bytes_used + space_info->bytes_reserved +
8264 space_info->bytes_pinned + space_info->bytes_readonly +
8265 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008266 spin_unlock(&space_info->lock);
8267 goto out;
8268 }
8269 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008270
Josef Bacikba1bf482009-09-11 16:11:19 -04008271 /*
8272 * ok we don't have enough space, but maybe we have free space on our
8273 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008274 * alloc devices and guess if we have enough space. if this block
8275 * group is going to be restriped, run checks against the target
8276 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008277 */
8278 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008279
liubocdcb7252011-08-03 10:15:25 +00008280 /*
8281 * index:
8282 * 0: raid10
8283 * 1: raid1
8284 * 2: dup
8285 * 3: raid0
8286 * 4: single
8287 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008288 target = get_restripe_target(root->fs_info, block_group->flags);
8289 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008290 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008291 } else {
8292 /*
8293 * this is just a balance, so if we were marked as full
8294 * we know there is no space for a new chunk
8295 */
8296 if (full)
8297 goto out;
8298
8299 index = get_block_group_index(block_group);
8300 }
8301
Miao Xiee6ec7162013-01-17 05:38:51 +00008302 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008303 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008304 /* Divide by 2 */
8305 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008306 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008307 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008308 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008309 /* Multiply by 2 */
8310 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008311 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008312 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008313 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008314 }
8315
Josef Bacik6df9a952013-06-27 13:22:46 -04008316 /* We need to do this so that we can look at pending chunks */
8317 trans = btrfs_join_transaction(root);
8318 if (IS_ERR(trans)) {
8319 ret = PTR_ERR(trans);
8320 goto out;
8321 }
8322
Josef Bacikba1bf482009-09-11 16:11:19 -04008323 mutex_lock(&root->fs_info->chunk_mutex);
8324 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008325 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008326
Josef Bacikba1bf482009-09-11 16:11:19 -04008327 /*
8328 * check to make sure we can actually find a chunk with enough
8329 * space to fit our block group in.
8330 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008331 if (device->total_bytes > device->bytes_used + min_free &&
8332 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008333 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008334 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008335 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008336 dev_nr++;
8337
8338 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008339 break;
liubocdcb7252011-08-03 10:15:25 +00008340
Josef Bacikba1bf482009-09-11 16:11:19 -04008341 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008342 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008343 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008344 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008345 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008346out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008347 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008348 return ret;
8349}
8350
Christoph Hellwigb2950862008-12-02 09:54:17 -05008351static int find_first_block_group(struct btrfs_root *root,
8352 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008353{
Chris Mason925baed2008-06-25 16:01:30 -04008354 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008355 struct btrfs_key found_key;
8356 struct extent_buffer *leaf;
8357 int slot;
8358
8359 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8360 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008361 goto out;
8362
Chris Masond3977122009-01-05 21:25:51 -05008363 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008364 slot = path->slots[0];
8365 leaf = path->nodes[0];
8366 if (slot >= btrfs_header_nritems(leaf)) {
8367 ret = btrfs_next_leaf(root, path);
8368 if (ret == 0)
8369 continue;
8370 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008371 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008372 break;
8373 }
8374 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8375
8376 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008377 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8378 ret = 0;
8379 goto out;
8380 }
Chris Mason0b86a832008-03-24 15:01:56 -04008381 path->slots[0]++;
8382 }
Chris Mason925baed2008-06-25 16:01:30 -04008383out:
Chris Mason0b86a832008-03-24 15:01:56 -04008384 return ret;
8385}
8386
Josef Bacik0af3d002010-06-21 14:48:16 -04008387void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8388{
8389 struct btrfs_block_group_cache *block_group;
8390 u64 last = 0;
8391
8392 while (1) {
8393 struct inode *inode;
8394
8395 block_group = btrfs_lookup_first_block_group(info, last);
8396 while (block_group) {
8397 spin_lock(&block_group->lock);
8398 if (block_group->iref)
8399 break;
8400 spin_unlock(&block_group->lock);
8401 block_group = next_block_group(info->tree_root,
8402 block_group);
8403 }
8404 if (!block_group) {
8405 if (last == 0)
8406 break;
8407 last = 0;
8408 continue;
8409 }
8410
8411 inode = block_group->inode;
8412 block_group->iref = 0;
8413 block_group->inode = NULL;
8414 spin_unlock(&block_group->lock);
8415 iput(inode);
8416 last = block_group->key.objectid + block_group->key.offset;
8417 btrfs_put_block_group(block_group);
8418 }
8419}
8420
Zheng Yan1a40e232008-09-26 10:09:34 -04008421int btrfs_free_block_groups(struct btrfs_fs_info *info)
8422{
8423 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008424 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008425 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008426 struct rb_node *n;
8427
Josef Bacik9e351cc2014-03-13 15:42:13 -04008428 down_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008429 while (!list_empty(&info->caching_block_groups)) {
8430 caching_ctl = list_entry(info->caching_block_groups.next,
8431 struct btrfs_caching_control, list);
8432 list_del(&caching_ctl->list);
8433 put_caching_control(caching_ctl);
8434 }
Josef Bacik9e351cc2014-03-13 15:42:13 -04008435 up_write(&info->commit_root_sem);
Yan Zheng11833d62009-09-11 16:11:19 -04008436
Zheng Yan1a40e232008-09-26 10:09:34 -04008437 spin_lock(&info->block_group_cache_lock);
8438 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8439 block_group = rb_entry(n, struct btrfs_block_group_cache,
8440 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008441 rb_erase(&block_group->cache_node,
8442 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008443 spin_unlock(&info->block_group_cache_lock);
8444
Josef Bacik80eb2342008-10-29 14:49:05 -04008445 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008446 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008447 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008448
Josef Bacik817d52f2009-07-13 21:29:25 -04008449 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008450 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008451
Josef Bacik3c148742011-02-02 15:53:47 +00008452 /*
8453 * We haven't cached this block group, which means we could
8454 * possibly have excluded extents on this block group.
8455 */
Josef Bacik36cce922013-08-05 11:15:21 -04008456 if (block_group->cached == BTRFS_CACHE_NO ||
8457 block_group->cached == BTRFS_CACHE_ERROR)
Josef Bacik3c148742011-02-02 15:53:47 +00008458 free_excluded_extents(info->extent_root, block_group);
8459
Josef Bacik817d52f2009-07-13 21:29:25 -04008460 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008461 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008462
8463 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008464 }
8465 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008466
8467 /* now that all the block groups are freed, go through and
8468 * free all the space_info structs. This is only called during
8469 * the final stages of unmount, and so we know nobody is
8470 * using them. We call synchronize_rcu() once before we start,
8471 * just to be on the safe side.
8472 */
8473 synchronize_rcu();
8474
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008475 release_global_block_rsv(info);
8476
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05308477 while (!list_empty(&info->space_info)) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008478 int i;
8479
Chris Mason4184ea72009-03-10 12:39:20 -04008480 space_info = list_entry(info->space_info.next,
8481 struct btrfs_space_info,
8482 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008483 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308484 if (WARN_ON(space_info->bytes_pinned > 0 ||
David Sterbab069e0c2013-02-08 21:28:17 +00008485 space_info->bytes_reserved > 0 ||
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05308486 space_info->bytes_may_use > 0)) {
David Sterbab069e0c2013-02-08 21:28:17 +00008487 dump_space_info(space_info, 0, 0);
8488 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008489 }
Chris Mason4184ea72009-03-10 12:39:20 -04008490 list_del(&space_info->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008491 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
8492 struct kobject *kobj;
8493 kobj = &space_info->block_group_kobjs[i];
8494 if (kobj->parent) {
8495 kobject_del(kobj);
8496 kobject_put(kobj);
8497 }
8498 }
8499 kobject_del(&space_info->kobj);
8500 kobject_put(&space_info->kobj);
Chris Mason4184ea72009-03-10 12:39:20 -04008501 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008502 return 0;
8503}
8504
Yan, Zhengb742bb82010-05-16 10:46:24 -04008505static void __link_block_group(struct btrfs_space_info *space_info,
8506 struct btrfs_block_group_cache *cache)
8507{
8508 int index = get_block_group_index(cache);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008509 bool first = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04008510
8511 down_write(&space_info->groups_sem);
Jeff Mahoneyed55b6a2014-03-26 14:11:26 -04008512 if (list_empty(&space_info->block_groups[index]))
8513 first = true;
8514 list_add_tail(&cache->list, &space_info->block_groups[index]);
8515 up_write(&space_info->groups_sem);
8516
8517 if (first) {
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008518 struct kobject *kobj = &space_info->block_group_kobjs[index];
8519 int ret;
8520
8521 kobject_get(&space_info->kobj); /* put in release */
Miao Xie536cd962013-12-17 12:01:12 +08008522 ret = kobject_add(kobj, &space_info->kobj, "%s",
8523 get_raid_name(index));
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008524 if (ret) {
Frank Holtonefe120a2013-12-20 11:37:06 -05008525 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008526 kobject_put(&space_info->kobj);
8527 }
8528 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008529}
8530
Miao Xie920e4a52014-01-15 20:00:55 +08008531static struct btrfs_block_group_cache *
8532btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8533{
8534 struct btrfs_block_group_cache *cache;
8535
8536 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8537 if (!cache)
8538 return NULL;
8539
8540 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8541 GFP_NOFS);
8542 if (!cache->free_space_ctl) {
8543 kfree(cache);
8544 return NULL;
8545 }
8546
8547 cache->key.objectid = start;
8548 cache->key.offset = size;
8549 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8550
8551 cache->sectorsize = root->sectorsize;
8552 cache->fs_info = root->fs_info;
8553 cache->full_stripe_len = btrfs_full_stripe_len(root,
8554 &root->fs_info->mapping_tree,
8555 start);
8556 atomic_set(&cache->count, 1);
8557 spin_lock_init(&cache->lock);
8558 INIT_LIST_HEAD(&cache->list);
8559 INIT_LIST_HEAD(&cache->cluster_list);
8560 INIT_LIST_HEAD(&cache->new_bg_list);
8561 btrfs_init_free_space_ctl(cache);
8562
8563 return cache;
8564}
8565
Chris Mason9078a3e2007-04-26 16:46:15 -04008566int btrfs_read_block_groups(struct btrfs_root *root)
8567{
8568 struct btrfs_path *path;
8569 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008570 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008571 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008572 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008573 struct btrfs_key key;
8574 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008575 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008576 int need_clear = 0;
8577 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008578
Chris Masonbe744172007-05-06 10:15:01 -04008579 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008580 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008581 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008582 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008583 path = btrfs_alloc_path();
8584 if (!path)
8585 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008586 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008587
David Sterba6c417612011-04-13 15:41:04 +02008588 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008589 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008590 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008591 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008592 if (btrfs_test_opt(root, CLEAR_CACHE))
8593 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008594
Chris Masond3977122009-01-05 21:25:51 -05008595 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008596 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008597 if (ret > 0)
8598 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008599 if (ret != 0)
8600 goto error;
Miao Xie920e4a52014-01-15 20:00:55 +08008601
Chris Mason5f39d392007-10-15 16:14:19 -04008602 leaf = path->nodes[0];
8603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Miao Xie920e4a52014-01-15 20:00:55 +08008604
8605 cache = btrfs_create_block_group_cache(root, found_key.objectid,
8606 found_key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04008607 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008608 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008609 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008610 }
Josef Bacik96303082009-07-13 21:29:25 -04008611
Liu Bocf7c1ef2012-07-06 03:31:34 -06008612 if (need_clear) {
8613 /*
8614 * When we mount with old space cache, we need to
8615 * set BTRFS_DC_CLEAR and set dirty flag.
8616 *
8617 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8618 * truncate the old free space cache inode and
8619 * setup a new one.
8620 * b) Setting 'dirty flag' makes sure that we flush
8621 * the new space cache info onto disk.
8622 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008623 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008624 if (btrfs_test_opt(root, SPACE_CACHE))
8625 cache->dirty = 1;
8626 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008627
Chris Mason5f39d392007-10-15 16:14:19 -04008628 read_extent_buffer(leaf, &cache->item,
8629 btrfs_item_ptr_offset(leaf, path->slots[0]),
8630 sizeof(cache->item));
Miao Xie920e4a52014-01-15 20:00:55 +08008631 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason0b86a832008-03-24 15:01:56 -04008632
Chris Mason9078a3e2007-04-26 16:46:15 -04008633 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008634 btrfs_release_path(path);
Li Zefan34d52cb2011-03-29 13:46:06 +08008635
Josef Bacik817d52f2009-07-13 21:29:25 -04008636 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008637 * We need to exclude the super stripes now so that the space
8638 * info has super bytes accounted for, otherwise we'll think
8639 * we have more space than we actually do.
8640 */
Josef Bacik835d9742013-03-19 12:13:25 -04008641 ret = exclude_super_stripes(root, cache);
8642 if (ret) {
8643 /*
8644 * We may have excluded something, so call this just in
8645 * case.
8646 */
8647 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08008648 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04008649 goto error;
8650 }
Josef Bacik3c148742011-02-02 15:53:47 +00008651
8652 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008653 * check for two cases, either we are full, and therefore
8654 * don't need to bother with the caching work since we won't
8655 * find any space, or we are empty, and we can just add all
8656 * the space in and be done with it. This saves us _alot_ of
8657 * time, particularly in the full case.
8658 */
8659 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008660 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008661 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008662 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008663 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008664 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008665 cache->cached = BTRFS_CACHE_FINISHED;
8666 add_new_free_space(cache, root->fs_info,
8667 found_key.objectid,
8668 found_key.objectid +
8669 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008670 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008671 }
Chris Mason96b51792007-10-15 16:15:19 -04008672
Josef Bacik8c579fe2013-04-02 12:40:42 -04008673 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8674 if (ret) {
8675 btrfs_remove_free_space_cache(cache);
8676 btrfs_put_block_group(cache);
8677 goto error;
8678 }
8679
Chris Mason6324fbf2008-03-24 15:01:59 -04008680 ret = update_space_info(info, cache->flags, found_key.offset,
8681 btrfs_block_group_used(&cache->item),
8682 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008683 if (ret) {
8684 btrfs_remove_free_space_cache(cache);
8685 spin_lock(&info->block_group_cache_lock);
8686 rb_erase(&cache->cache_node,
8687 &info->block_group_cache_tree);
8688 spin_unlock(&info->block_group_cache_lock);
8689 btrfs_put_block_group(cache);
8690 goto error;
8691 }
8692
Chris Mason6324fbf2008-03-24 15:01:59 -04008693 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008694 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008695 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008696 spin_unlock(&cache->space_info->lock);
8697
Yan, Zhengb742bb82010-05-16 10:46:24 -04008698 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008699
Chris Mason75ccf472008-09-30 19:24:06 -04008700 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008701 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008702 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008703 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008704
8705 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8706 if (!(get_alloc_profile(root, space_info->flags) &
8707 (BTRFS_BLOCK_GROUP_RAID10 |
8708 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008709 BTRFS_BLOCK_GROUP_RAID5 |
8710 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008711 BTRFS_BLOCK_GROUP_DUP)))
8712 continue;
8713 /*
8714 * avoid allocating from un-mirrored block group if there are
8715 * mirrored block groups.
8716 */
chandan1095cc02013-07-16 12:28:56 +05308717 list_for_each_entry(cache,
8718 &space_info->block_groups[BTRFS_RAID_RAID0],
8719 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008720 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05308721 list_for_each_entry(cache,
8722 &space_info->block_groups[BTRFS_RAID_SINGLE],
8723 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008724 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008725 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008726
8727 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008728 ret = 0;
8729error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008730 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008731 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008732}
Chris Mason6324fbf2008-03-24 15:01:59 -04008733
Josef Bacikea658ba2012-09-11 16:57:25 -04008734void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8735 struct btrfs_root *root)
8736{
8737 struct btrfs_block_group_cache *block_group, *tmp;
8738 struct btrfs_root *extent_root = root->fs_info->extent_root;
8739 struct btrfs_block_group_item item;
8740 struct btrfs_key key;
8741 int ret = 0;
8742
8743 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8744 new_bg_list) {
8745 list_del_init(&block_group->new_bg_list);
8746
8747 if (ret)
8748 continue;
8749
8750 spin_lock(&block_group->lock);
8751 memcpy(&item, &block_group->item, sizeof(item));
8752 memcpy(&key, &block_group->key, sizeof(key));
8753 spin_unlock(&block_group->lock);
8754
8755 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8756 sizeof(item));
8757 if (ret)
8758 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04008759 ret = btrfs_finish_chunk_alloc(trans, extent_root,
8760 key.objectid, key.offset);
8761 if (ret)
8762 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04008763 }
8764}
8765
Chris Mason6324fbf2008-03-24 15:01:59 -04008766int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8767 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008768 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008769 u64 size)
8770{
8771 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008772 struct btrfs_root *extent_root;
8773 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008774
8775 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008776
Miao Xie995946d2014-04-02 19:51:06 +08008777 btrfs_set_log_full_commit(root->fs_info, trans);
Chris Masone02119d2008-09-05 16:13:11 -04008778
Miao Xie920e4a52014-01-15 20:00:55 +08008779 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008780 if (!cache)
8781 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008782
Chris Mason6324fbf2008-03-24 15:01:59 -04008783 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008784 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
Chris Mason6324fbf2008-03-24 15:01:59 -04008785 btrfs_set_block_group_flags(&cache->item, type);
8786
Miao Xie920e4a52014-01-15 20:00:55 +08008787 cache->flags = type;
Yan Zheng11833d62009-09-11 16:11:19 -04008788 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008789 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008790 ret = exclude_super_stripes(root, cache);
8791 if (ret) {
8792 /*
8793 * We may have excluded something, so call this just in
8794 * case.
8795 */
8796 free_excluded_extents(root, cache);
Miao Xie920e4a52014-01-15 20:00:55 +08008797 btrfs_put_block_group(cache);
Josef Bacik835d9742013-03-19 12:13:25 -04008798 return ret;
8799 }
Josef Bacik96303082009-07-13 21:29:25 -04008800
Josef Bacik817d52f2009-07-13 21:29:25 -04008801 add_new_free_space(cache, root->fs_info, chunk_offset,
8802 chunk_offset + size);
8803
Yan Zheng11833d62009-09-11 16:11:19 -04008804 free_excluded_extents(root, cache);
8805
Josef Bacik8c579fe2013-04-02 12:40:42 -04008806 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8807 if (ret) {
8808 btrfs_remove_free_space_cache(cache);
8809 btrfs_put_block_group(cache);
8810 return ret;
8811 }
8812
Chris Mason6324fbf2008-03-24 15:01:59 -04008813 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8814 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008815 if (ret) {
8816 btrfs_remove_free_space_cache(cache);
8817 spin_lock(&root->fs_info->block_group_cache_lock);
8818 rb_erase(&cache->cache_node,
8819 &root->fs_info->block_group_cache_tree);
8820 spin_unlock(&root->fs_info->block_group_cache_lock);
8821 btrfs_put_block_group(cache);
8822 return ret;
8823 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008824 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008825
8826 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008827 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008828 spin_unlock(&cache->space_info->lock);
8829
Yan, Zhengb742bb82010-05-16 10:46:24 -04008830 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008831
Josef Bacikea658ba2012-09-11 16:57:25 -04008832 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008833
Chris Masond18a2c42008-04-04 15:40:00 -04008834 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008835
Chris Mason6324fbf2008-03-24 15:01:59 -04008836 return 0;
8837}
Zheng Yan1a40e232008-09-26 10:09:34 -04008838
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008839static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8840{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008841 u64 extra_flags = chunk_to_extended(flags) &
8842 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008843
Miao Xiede98ced2013-01-29 10:13:12 +00008844 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008845 if (flags & BTRFS_BLOCK_GROUP_DATA)
8846 fs_info->avail_data_alloc_bits &= ~extra_flags;
8847 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8848 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8849 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8850 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008851 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008852}
8853
Zheng Yan1a40e232008-09-26 10:09:34 -04008854int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8855 struct btrfs_root *root, u64 group_start)
8856{
8857 struct btrfs_path *path;
8858 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008859 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008860 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008861 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008862 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008863 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008864 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008865 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008866
Zheng Yan1a40e232008-09-26 10:09:34 -04008867 root = root->fs_info->extent_root;
8868
8869 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8870 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008871 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008872
liubo9f7c43c2011-03-07 02:13:33 +00008873 /*
8874 * Free the reserved super bytes from this block group before
8875 * remove it.
8876 */
8877 free_excluded_extents(root, block_group);
8878
Zheng Yan1a40e232008-09-26 10:09:34 -04008879 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008880 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008881 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8882 BTRFS_BLOCK_GROUP_RAID1 |
8883 BTRFS_BLOCK_GROUP_RAID10))
8884 factor = 2;
8885 else
8886 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008887
Chris Mason44fb5512009-06-04 15:34:51 -04008888 /* make sure this block group isn't part of an allocation cluster */
8889 cluster = &root->fs_info->data_alloc_cluster;
8890 spin_lock(&cluster->refill_lock);
8891 btrfs_return_cluster_to_free_space(block_group, cluster);
8892 spin_unlock(&cluster->refill_lock);
8893
8894 /*
8895 * make sure this block group isn't part of a metadata
8896 * allocation cluster
8897 */
8898 cluster = &root->fs_info->meta_alloc_cluster;
8899 spin_lock(&cluster->refill_lock);
8900 btrfs_return_cluster_to_free_space(block_group, cluster);
8901 spin_unlock(&cluster->refill_lock);
8902
Zheng Yan1a40e232008-09-26 10:09:34 -04008903 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008904 if (!path) {
8905 ret = -ENOMEM;
8906 goto out;
8907 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008908
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008909 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008910 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008911 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008912 if (ret) {
8913 btrfs_add_delayed_iput(inode);
8914 goto out;
8915 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008916 clear_nlink(inode);
8917 /* One for the block groups ref */
8918 spin_lock(&block_group->lock);
8919 if (block_group->iref) {
8920 block_group->iref = 0;
8921 block_group->inode = NULL;
8922 spin_unlock(&block_group->lock);
8923 iput(inode);
8924 } else {
8925 spin_unlock(&block_group->lock);
8926 }
8927 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008928 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008929 }
8930
8931 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8932 key.offset = block_group->key.objectid;
8933 key.type = 0;
8934
8935 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8936 if (ret < 0)
8937 goto out;
8938 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008939 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008940 if (ret == 0) {
8941 ret = btrfs_del_item(trans, tree_root, path);
8942 if (ret)
8943 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008944 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008945 }
8946
Yan Zheng3dfdb932009-01-21 10:49:16 -05008947 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008948 rb_erase(&block_group->cache_node,
8949 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008950
8951 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8952 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008953 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008954
Josef Bacik80eb2342008-10-29 14:49:05 -04008955 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008956 /*
8957 * we must use list_del_init so people can check to see if they
8958 * are still on the list after taking the semaphore
8959 */
8960 list_del_init(&block_group->list);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008961 if (list_empty(&block_group->space_info->block_groups[index])) {
8962 kobject_del(&block_group->space_info->block_group_kobjs[index]);
8963 kobject_put(&block_group->space_info->block_group_kobjs[index]);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008964 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Jeff Mahoney6ab0a202013-11-01 13:07:04 -04008965 }
Josef Bacik80eb2342008-10-29 14:49:05 -04008966 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008967
Josef Bacik817d52f2009-07-13 21:29:25 -04008968 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008969 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008970
8971 btrfs_remove_free_space_cache(block_group);
8972
Yan Zhengc146afa2008-11-12 14:34:12 -05008973 spin_lock(&block_group->space_info->lock);
8974 block_group->space_info->total_bytes -= block_group->key.offset;
8975 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008976 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008977 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008978
Josef Bacik0af3d002010-06-21 14:48:16 -04008979 memcpy(&key, &block_group->key, sizeof(key));
8980
Chris Mason283bb192009-07-24 16:30:55 -04008981 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008982
Chris Masonfa9c0d792009-04-03 09:47:43 -04008983 btrfs_put_block_group(block_group);
8984 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008985
8986 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8987 if (ret > 0)
8988 ret = -EIO;
8989 if (ret < 0)
8990 goto out;
8991
8992 ret = btrfs_del_item(trans, root, path);
8993out:
8994 btrfs_free_path(path);
8995 return ret;
8996}
liuboacce9522011-01-06 19:30:25 +08008997
liuboc59021f2011-03-07 02:13:14 +00008998int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8999{
9000 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00009001 struct btrfs_super_block *disk_super;
9002 u64 features;
9003 u64 flags;
9004 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00009005 int ret;
9006
David Sterba6c417612011-04-13 15:41:04 +02009007 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00009008 if (!btrfs_super_root(disk_super))
9009 return 1;
liuboc59021f2011-03-07 02:13:14 +00009010
liubo1aba86d2011-04-08 08:44:37 +00009011 features = btrfs_super_incompat_flags(disk_super);
9012 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
9013 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00009014
liubo1aba86d2011-04-08 08:44:37 +00009015 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9016 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00009017 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00009018 goto out;
liuboc59021f2011-03-07 02:13:14 +00009019
liubo1aba86d2011-04-08 08:44:37 +00009020 if (mixed) {
9021 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
9022 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9023 } else {
9024 flags = BTRFS_BLOCK_GROUP_METADATA;
9025 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9026 if (ret)
9027 goto out;
9028
9029 flags = BTRFS_BLOCK_GROUP_DATA;
9030 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9031 }
9032out:
liuboc59021f2011-03-07 02:13:14 +00009033 return ret;
9034}
9035
liuboacce9522011-01-06 19:30:25 +08009036int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
9037{
9038 return unpin_extent_range(root, start, end);
9039}
9040
9041int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00009042 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08009043{
Li Dongyang5378e602011-03-24 10:24:27 +00009044 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08009045}
Li Dongyangf7039b12011-03-24 10:24:28 +00009046
9047int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
9048{
9049 struct btrfs_fs_info *fs_info = root->fs_info;
9050 struct btrfs_block_group_cache *cache = NULL;
9051 u64 group_trimmed;
9052 u64 start;
9053 u64 end;
9054 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08009055 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00009056 int ret = 0;
9057
Liu Bo2cac13e2012-02-09 18:17:41 +08009058 /*
9059 * try to trim all FS space, our block group may start from non-zero.
9060 */
9061 if (range->len == total_bytes)
9062 cache = btrfs_lookup_first_block_group(fs_info, range->start);
9063 else
9064 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00009065
9066 while (cache) {
9067 if (cache->key.objectid >= (range->start + range->len)) {
9068 btrfs_put_block_group(cache);
9069 break;
9070 }
9071
9072 start = max(range->start, cache->key.objectid);
9073 end = min(range->start + range->len,
9074 cache->key.objectid + cache->key.offset);
9075
9076 if (end - start >= range->minlen) {
9077 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00009078 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04009079 if (ret) {
9080 btrfs_put_block_group(cache);
9081 break;
9082 }
9083 ret = wait_block_group_cache_done(cache);
9084 if (ret) {
9085 btrfs_put_block_group(cache);
9086 break;
9087 }
Li Dongyangf7039b12011-03-24 10:24:28 +00009088 }
9089 ret = btrfs_trim_block_group(cache,
9090 &group_trimmed,
9091 start,
9092 end,
9093 range->minlen);
9094
9095 trimmed += group_trimmed;
9096 if (ret) {
9097 btrfs_put_block_group(cache);
9098 break;
9099 }
9100 }
9101
9102 cache = next_block_group(fs_info->tree_root, cache);
9103 }
9104
9105 range->len = trimmed;
9106 return ret;
9107}
Miao Xie8257b2d2014-03-06 13:38:19 +08009108
9109/*
9110 * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(),
9111 * they are used to prevent the some tasks writing data into the page cache
9112 * by nocow before the subvolume is snapshoted, but flush the data into
9113 * the disk after the snapshot creation.
9114 */
9115void btrfs_end_nocow_write(struct btrfs_root *root)
9116{
9117 percpu_counter_dec(&root->subv_writers->counter);
9118 /*
9119 * Make sure counter is updated before we wake up
9120 * waiters.
9121 */
9122 smp_mb();
9123 if (waitqueue_active(&root->subv_writers->wait))
9124 wake_up(&root->subv_writers->wait);
9125}
9126
9127int btrfs_start_nocow_write(struct btrfs_root *root)
9128{
9129 if (unlikely(atomic_read(&root->will_be_snapshoted)))
9130 return 0;
9131
9132 percpu_counter_inc(&root->subv_writers->counter);
9133 /*
9134 * Make sure counter is updated before we check for snapshot creation.
9135 */
9136 smp_mb();
9137 if (unlikely(atomic_read(&root->will_be_snapshoted))) {
9138 btrfs_end_nocow_write(root);
9139 return 0;
9140 }
9141 return 1;
9142}