blob: a770a631843359cb10b5515421ed64c55511a1a5 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Josef Bacik817d52f2009-07-13 21:29:25 -040024#include <linux/kthread.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
David Sterbadff51cd2011-06-14 12:52:17 +020026#include <linux/ratelimit.h>
Josef Bacikb150a4f2013-06-19 15:00:04 -040027#include <linux/percpu_counter.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050028#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050029#include "hash.h"
Chris Masonfec577f2007-02-26 10:40:21 -050030#include "ctree.h"
31#include "disk-io.h"
32#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040033#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040034#include "volumes.h"
David Woodhouse53b381b2013-01-29 18:40:14 -050035#include "raid56.h"
Chris Mason925baed2008-06-25 16:01:30 -040036#include "locking.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040037#include "free-space-cache.h"
Miao Xie3fed40c2012-09-13 04:51:36 -060038#include "math.h"
Chris Masonfec577f2007-02-26 10:40:21 -050039
Arne Jansen709c0482011-09-12 12:22:57 +020040#undef SCRAMBLE_DELAYED_REFS
41
Miao Xie9e622d62012-01-26 15:01:12 -050042/*
43 * control flags for do_chunk_alloc's force field
Chris Mason0e4f8f82011-04-15 16:05:44 -040044 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
46 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040047 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
52 *
Miao Xie9e622d62012-01-26 15:01:12 -050053 * CHUNK_ALLOC_FORCE means it must try to allocate one
54 *
Chris Mason0e4f8f82011-04-15 16:05:44 -040055 */
56enum {
57 CHUNK_ALLOC_NO_FORCE = 0,
Miao Xie9e622d62012-01-26 15:01:12 -050058 CHUNK_ALLOC_LIMITED = 1,
59 CHUNK_ALLOC_FORCE = 2,
Chris Mason0e4f8f82011-04-15 16:05:44 -040060};
61
Josef Bacikfb25e912011-07-26 17:00:46 -040062/*
63 * Control how reservations are dealt with.
64 *
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
67 * ENOSPC accounting
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
70 */
71enum {
72 RESERVE_FREE = 0,
73 RESERVE_ALLOC = 1,
74 RESERVE_ALLOC_NO_ACCOUNT = 2,
75};
76
Liu Boc53d6132012-12-27 09:01:19 +000077static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -040078 u64 bytenr, u64 num_bytes, int alloc);
Yan Zheng5d4f98a2009-06-10 10:45:14 -040079static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
84 struct btrfs_delayed_extent_op *extra_op);
85static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
86 struct extent_buffer *leaf,
87 struct btrfs_extent_item *ei);
88static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
89 struct btrfs_root *root,
90 u64 parent, u64 root_objectid,
91 u64 flags, u64 owner, u64 offset,
92 struct btrfs_key *ins, int ref_mod);
93static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
94 struct btrfs_root *root,
95 u64 parent, u64 root_objectid,
96 u64 flags, struct btrfs_disk_key *key,
97 int level, struct btrfs_key *ins);
Josef Bacik6a632092009-02-20 11:00:09 -050098static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -040099 struct btrfs_root *extent_root, u64 flags,
100 int force);
Yan Zheng11833d62009-09-11 16:11:19 -0400101static int find_next_key(struct btrfs_path *path, int level,
102 struct btrfs_key *key);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400103static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
104 int dump_block_groups);
Josef Bacikfb25e912011-07-26 17:00:46 -0400105static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
106 u64 num_bytes, int reserve);
Josef Bacik5d803662013-02-07 16:06:02 -0500107static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
108 u64 num_bytes);
Eric Sandeen48a3b632013-04-25 20:41:01 +0000109int btrfs_pin_extent(struct btrfs_root *root,
110 u64 bytenr, u64 num_bytes, int reserved);
Josef Bacik6a632092009-02-20 11:00:09 -0500111
Josef Bacik817d52f2009-07-13 21:29:25 -0400112static noinline int
113block_group_cache_done(struct btrfs_block_group_cache *cache)
114{
115 smp_mb();
116 return cache->cached == BTRFS_CACHE_FINISHED;
117}
118
Josef Bacik0f9dd462008-09-23 13:14:11 -0400119static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
120{
121 return (cache->flags & bits) == bits;
122}
123
David Sterba62a45b62011-04-20 15:52:26 +0200124static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
Josef Bacik11dfe352009-11-13 20:12:59 +0000125{
126 atomic_inc(&cache->count);
127}
128
129void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
130{
Yan, Zhengf0486c62010-05-16 10:46:25 -0400131 if (atomic_dec_and_test(&cache->count)) {
132 WARN_ON(cache->pinned > 0);
133 WARN_ON(cache->reserved > 0);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800134 kfree(cache->free_space_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000135 kfree(cache);
Yan, Zhengf0486c62010-05-16 10:46:25 -0400136 }
Josef Bacik11dfe352009-11-13 20:12:59 +0000137}
138
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139/*
140 * this adds the block group to the fs_info rb tree for the block group
141 * cache
142 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500143static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400144 struct btrfs_block_group_cache *block_group)
145{
146 struct rb_node **p;
147 struct rb_node *parent = NULL;
148 struct btrfs_block_group_cache *cache;
149
150 spin_lock(&info->block_group_cache_lock);
151 p = &info->block_group_cache_tree.rb_node;
152
153 while (*p) {
154 parent = *p;
155 cache = rb_entry(parent, struct btrfs_block_group_cache,
156 cache_node);
157 if (block_group->key.objectid < cache->key.objectid) {
158 p = &(*p)->rb_left;
159 } else if (block_group->key.objectid > cache->key.objectid) {
160 p = &(*p)->rb_right;
161 } else {
162 spin_unlock(&info->block_group_cache_lock);
163 return -EEXIST;
164 }
165 }
166
167 rb_link_node(&block_group->cache_node, parent, p);
168 rb_insert_color(&block_group->cache_node,
169 &info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +0000170
171 if (info->first_logical_byte > block_group->key.objectid)
172 info->first_logical_byte = block_group->key.objectid;
173
Josef Bacik0f9dd462008-09-23 13:14:11 -0400174 spin_unlock(&info->block_group_cache_lock);
175
176 return 0;
177}
178
179/*
180 * This will return the block group at or after bytenr if contains is 0, else
181 * it will return the block group that contains the bytenr
182 */
183static struct btrfs_block_group_cache *
184block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
185 int contains)
186{
187 struct btrfs_block_group_cache *cache, *ret = NULL;
188 struct rb_node *n;
189 u64 end, start;
190
191 spin_lock(&info->block_group_cache_lock);
192 n = info->block_group_cache_tree.rb_node;
193
194 while (n) {
195 cache = rb_entry(n, struct btrfs_block_group_cache,
196 cache_node);
197 end = cache->key.objectid + cache->key.offset - 1;
198 start = cache->key.objectid;
199
200 if (bytenr < start) {
201 if (!contains && (!ret || start < ret->key.objectid))
202 ret = cache;
203 n = n->rb_left;
204 } else if (bytenr > start) {
205 if (contains && bytenr <= end) {
206 ret = cache;
207 break;
208 }
209 n = n->rb_right;
210 } else {
211 ret = cache;
212 break;
213 }
214 }
Liu Boa1897fd2012-12-27 09:01:23 +0000215 if (ret) {
Josef Bacik11dfe352009-11-13 20:12:59 +0000216 btrfs_get_block_group(ret);
Liu Boa1897fd2012-12-27 09:01:23 +0000217 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
218 info->first_logical_byte = ret->key.objectid;
219 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400220 spin_unlock(&info->block_group_cache_lock);
221
222 return ret;
223}
224
Yan Zheng11833d62009-09-11 16:11:19 -0400225static int add_excluded_extent(struct btrfs_root *root,
226 u64 start, u64 num_bytes)
Josef Bacik817d52f2009-07-13 21:29:25 -0400227{
Yan Zheng11833d62009-09-11 16:11:19 -0400228 u64 end = start + num_bytes - 1;
229 set_extent_bits(&root->fs_info->freed_extents[0],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
231 set_extent_bits(&root->fs_info->freed_extents[1],
232 start, end, EXTENT_UPTODATE, GFP_NOFS);
233 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400234}
235
Yan Zheng11833d62009-09-11 16:11:19 -0400236static void free_excluded_extents(struct btrfs_root *root,
237 struct btrfs_block_group_cache *cache)
Josef Bacik817d52f2009-07-13 21:29:25 -0400238{
Yan Zheng11833d62009-09-11 16:11:19 -0400239 u64 start, end;
240
241 start = cache->key.objectid;
242 end = start + cache->key.offset - 1;
243
244 clear_extent_bits(&root->fs_info->freed_extents[0],
245 start, end, EXTENT_UPTODATE, GFP_NOFS);
246 clear_extent_bits(&root->fs_info->freed_extents[1],
247 start, end, EXTENT_UPTODATE, GFP_NOFS);
248}
249
250static int exclude_super_stripes(struct btrfs_root *root,
251 struct btrfs_block_group_cache *cache)
252{
Josef Bacik817d52f2009-07-13 21:29:25 -0400253 u64 bytenr;
254 u64 *logical;
255 int stripe_len;
256 int i, nr, ret;
257
Yan, Zheng06b23312009-11-26 09:31:11 +0000258 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
259 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
260 cache->bytes_super += stripe_len;
261 ret = add_excluded_extent(root, cache->key.objectid,
262 stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400263 if (ret)
264 return ret;
Yan, Zheng06b23312009-11-26 09:31:11 +0000265 }
266
Josef Bacik817d52f2009-07-13 21:29:25 -0400267 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
268 bytenr = btrfs_sb_offset(i);
269 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
270 cache->key.objectid, bytenr,
271 0, &logical, &nr, &stripe_len);
Josef Bacik835d9742013-03-19 12:13:25 -0400272 if (ret)
273 return ret;
Yan Zheng11833d62009-09-11 16:11:19 -0400274
Josef Bacik817d52f2009-07-13 21:29:25 -0400275 while (nr--) {
Josef Bacik51bf5f02013-04-23 12:55:21 -0400276 u64 start, len;
277
278 if (logical[nr] > cache->key.objectid +
279 cache->key.offset)
280 continue;
281
282 if (logical[nr] + stripe_len <= cache->key.objectid)
283 continue;
284
285 start = logical[nr];
286 if (start < cache->key.objectid) {
287 start = cache->key.objectid;
288 len = (logical[nr] + stripe_len) - start;
289 } else {
290 len = min_t(u64, stripe_len,
291 cache->key.objectid +
292 cache->key.offset - start);
293 }
294
295 cache->bytes_super += len;
296 ret = add_excluded_extent(root, start, len);
Josef Bacik835d9742013-03-19 12:13:25 -0400297 if (ret) {
298 kfree(logical);
299 return ret;
300 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400301 }
Yan Zheng11833d62009-09-11 16:11:19 -0400302
Josef Bacik817d52f2009-07-13 21:29:25 -0400303 kfree(logical);
304 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400305 return 0;
306}
307
Yan Zheng11833d62009-09-11 16:11:19 -0400308static struct btrfs_caching_control *
309get_caching_control(struct btrfs_block_group_cache *cache)
310{
311 struct btrfs_caching_control *ctl;
312
313 spin_lock(&cache->lock);
314 if (cache->cached != BTRFS_CACHE_STARTED) {
315 spin_unlock(&cache->lock);
316 return NULL;
317 }
318
Josef Bacikdde5abe2010-09-16 16:17:03 -0400319 /* We're loading it the fast way, so we don't have a caching_ctl. */
320 if (!cache->caching_ctl) {
321 spin_unlock(&cache->lock);
322 return NULL;
323 }
324
Yan Zheng11833d62009-09-11 16:11:19 -0400325 ctl = cache->caching_ctl;
326 atomic_inc(&ctl->count);
327 spin_unlock(&cache->lock);
328 return ctl;
329}
330
331static void put_caching_control(struct btrfs_caching_control *ctl)
332{
333 if (atomic_dec_and_test(&ctl->count))
334 kfree(ctl);
335}
336
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337/*
338 * this is only called by cache_block_group, since we could have freed extents
339 * we need to check the pinned_extents for any extents that can't be used yet
340 * since their free space will be released as soon as the transaction commits.
341 */
Josef Bacik817d52f2009-07-13 21:29:25 -0400342static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 struct btrfs_fs_info *info, u64 start, u64 end)
344{
Josef Bacik817d52f2009-07-13 21:29:25 -0400345 u64 extent_start, extent_end, size, total_added = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400346 int ret;
347
348 while (start < end) {
Yan Zheng11833d62009-09-11 16:11:19 -0400349 ret = find_first_extent_bit(info->pinned_extents, start,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400350 &extent_start, &extent_end,
Josef Bacike6138872012-09-27 17:07:30 -0400351 EXTENT_DIRTY | EXTENT_UPTODATE,
352 NULL);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400353 if (ret)
354 break;
355
Yan, Zheng06b23312009-11-26 09:31:11 +0000356 if (extent_start <= start) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400357 start = extent_end + 1;
358 } else if (extent_start > start && extent_start < end) {
359 size = extent_start - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400360 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500361 ret = btrfs_add_free_space(block_group, start,
362 size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100363 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400364 start = extent_end + 1;
365 } else {
366 break;
367 }
368 }
369
370 if (start < end) {
371 size = end - start;
Josef Bacik817d52f2009-07-13 21:29:25 -0400372 total_added += size;
Josef Bacikea6a4782008-11-20 12:16:16 -0500373 ret = btrfs_add_free_space(block_group, start, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100374 BUG_ON(ret); /* -ENOMEM or logic error */
Josef Bacik0f9dd462008-09-23 13:14:11 -0400375 }
376
Josef Bacik817d52f2009-07-13 21:29:25 -0400377 return total_added;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400378}
379
Josef Bacikbab39bf2011-06-30 14:42:28 -0400380static noinline void caching_thread(struct btrfs_work *work)
Chris Masone37c9e62007-05-09 20:13:14 -0400381{
Josef Bacikbab39bf2011-06-30 14:42:28 -0400382 struct btrfs_block_group_cache *block_group;
383 struct btrfs_fs_info *fs_info;
384 struct btrfs_caching_control *caching_ctl;
385 struct btrfs_root *extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400386 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400387 struct extent_buffer *leaf;
Yan Zheng11833d62009-09-11 16:11:19 -0400388 struct btrfs_key key;
Josef Bacik817d52f2009-07-13 21:29:25 -0400389 u64 total_found = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400390 u64 last = 0;
391 u32 nritems;
392 int ret = 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400393
Josef Bacikbab39bf2011-06-30 14:42:28 -0400394 caching_ctl = container_of(work, struct btrfs_caching_control, work);
395 block_group = caching_ctl->block_group;
396 fs_info = block_group->fs_info;
397 extent_root = fs_info->extent_root;
398
Chris Masone37c9e62007-05-09 20:13:14 -0400399 path = btrfs_alloc_path();
400 if (!path)
Josef Bacikbab39bf2011-06-30 14:42:28 -0400401 goto out;
Yan7d7d6062007-09-14 16:15:28 -0400402
Josef Bacik817d52f2009-07-13 21:29:25 -0400403 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
Yan Zheng11833d62009-09-11 16:11:19 -0400404
Chris Mason5cd57b22008-06-25 16:01:30 -0400405 /*
Josef Bacik817d52f2009-07-13 21:29:25 -0400406 * We don't want to deadlock with somebody trying to allocate a new
407 * extent for the extent root while also trying to search the extent
408 * root to add free space. So we skip locking and search the commit
409 * root, since its read-only
Chris Mason5cd57b22008-06-25 16:01:30 -0400410 */
411 path->skip_locking = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400412 path->search_commit_root = 1;
Josef Bacik026fd312011-05-13 10:32:11 -0400413 path->reada = 1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400414
Yan Zhenge4404d62008-12-12 10:03:26 -0500415 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400416 key.offset = 0;
Yan Zheng11833d62009-09-11 16:11:19 -0400417 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason013f1b12009-07-31 14:57:55 -0400418again:
Yan Zheng11833d62009-09-11 16:11:19 -0400419 mutex_lock(&caching_ctl->mutex);
Chris Mason013f1b12009-07-31 14:57:55 -0400420 /* need to make sure the commit_root doesn't disappear */
421 down_read(&fs_info->extent_commit_sem);
422
Liu Bo52ee28d2013-07-11 17:51:15 +0800423next:
Yan Zheng11833d62009-09-11 16:11:19 -0400424 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
Chris Masone37c9e62007-05-09 20:13:14 -0400425 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400426 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500427
Yan Zheng11833d62009-09-11 16:11:19 -0400428 leaf = path->nodes[0];
429 nritems = btrfs_header_nritems(leaf);
430
Chris Masond3977122009-01-05 21:25:51 -0500431 while (1) {
David Sterba7841cb22011-05-31 18:07:27 +0200432 if (btrfs_fs_closing(fs_info) > 1) {
Yan Zhengf25784b2009-07-28 08:41:57 -0400433 last = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400434 break;
Yan Zhengf25784b2009-07-28 08:41:57 -0400435 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400436
Yan Zheng11833d62009-09-11 16:11:19 -0400437 if (path->slots[0] < nritems) {
438 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
439 } else {
440 ret = find_next_key(path, 0, &key);
441 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -0400442 break;
Josef Bacik817d52f2009-07-13 21:29:25 -0400443
Josef Bacik0a3896d2013-04-19 14:37:26 -0400444 if (need_resched()) {
Josef Bacik589d8ad2011-05-11 17:30:53 -0400445 caching_ctl->progress = last;
Chris Masonff5714c2011-05-28 07:00:39 -0400446 btrfs_release_path(path);
Josef Bacik589d8ad2011-05-11 17:30:53 -0400447 up_read(&fs_info->extent_commit_sem);
448 mutex_unlock(&caching_ctl->mutex);
Yan Zheng11833d62009-09-11 16:11:19 -0400449 cond_resched();
Josef Bacik589d8ad2011-05-11 17:30:53 -0400450 goto again;
451 }
Josef Bacik0a3896d2013-04-19 14:37:26 -0400452
453 ret = btrfs_next_leaf(extent_root, path);
454 if (ret < 0)
455 goto err;
456 if (ret)
457 break;
Josef Bacik589d8ad2011-05-11 17:30:53 -0400458 leaf = path->nodes[0];
459 nritems = btrfs_header_nritems(leaf);
460 continue;
Yan Zheng11833d62009-09-11 16:11:19 -0400461 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400462
Liu Bo52ee28d2013-07-11 17:51:15 +0800463 if (key.objectid < last) {
464 key.objectid = last;
465 key.offset = 0;
466 key.type = BTRFS_EXTENT_ITEM_KEY;
467
468 caching_ctl->progress = last;
469 btrfs_release_path(path);
470 goto next;
471 }
472
Yan Zheng11833d62009-09-11 16:11:19 -0400473 if (key.objectid < block_group->key.objectid) {
474 path->slots[0]++;
Josef Bacik817d52f2009-07-13 21:29:25 -0400475 continue;
Chris Masone37c9e62007-05-09 20:13:14 -0400476 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400477
Chris Masone37c9e62007-05-09 20:13:14 -0400478 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400479 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400480 break;
Yan7d7d6062007-09-14 16:15:28 -0400481
Josef Bacik3173a182013-03-07 14:22:04 -0500482 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
483 key.type == BTRFS_METADATA_ITEM_KEY) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400484 total_found += add_new_free_space(block_group,
485 fs_info, last,
486 key.objectid);
Josef Bacik3173a182013-03-07 14:22:04 -0500487 if (key.type == BTRFS_METADATA_ITEM_KEY)
488 last = key.objectid +
489 fs_info->tree_root->leafsize;
490 else
491 last = key.objectid + key.offset;
Josef Bacik817d52f2009-07-13 21:29:25 -0400492
Yan Zheng11833d62009-09-11 16:11:19 -0400493 if (total_found > (1024 * 1024 * 2)) {
494 total_found = 0;
495 wake_up(&caching_ctl->wait);
496 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400497 }
Chris Masone37c9e62007-05-09 20:13:14 -0400498 path->slots[0]++;
499 }
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400500 ret = 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400501
502 total_found += add_new_free_space(block_group, fs_info, last,
503 block_group->key.objectid +
504 block_group->key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -0400505 caching_ctl->progress = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -0400506
507 spin_lock(&block_group->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400508 block_group->caching_ctl = NULL;
Josef Bacik817d52f2009-07-13 21:29:25 -0400509 block_group->cached = BTRFS_CACHE_FINISHED;
510 spin_unlock(&block_group->lock);
511
Chris Mason54aa1f42007-06-22 14:16:25 -0400512err:
Chris Masone37c9e62007-05-09 20:13:14 -0400513 btrfs_free_path(path);
Yan Zheng276e6802009-07-30 09:40:40 -0400514 up_read(&fs_info->extent_commit_sem);
Josef Bacik817d52f2009-07-13 21:29:25 -0400515
Yan Zheng11833d62009-09-11 16:11:19 -0400516 free_excluded_extents(extent_root, block_group);
517
518 mutex_unlock(&caching_ctl->mutex);
Josef Bacikbab39bf2011-06-30 14:42:28 -0400519out:
Yan Zheng11833d62009-09-11 16:11:19 -0400520 wake_up(&caching_ctl->wait);
521
522 put_caching_control(caching_ctl);
Josef Bacik11dfe352009-11-13 20:12:59 +0000523 btrfs_put_block_group(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -0400524}
525
Josef Bacik9d66e232010-08-25 16:54:15 -0400526static int cache_block_group(struct btrfs_block_group_cache *cache,
Josef Bacik9d66e232010-08-25 16:54:15 -0400527 int load_cache_only)
Josef Bacik817d52f2009-07-13 21:29:25 -0400528{
Josef Bacik291c7d22011-11-14 13:52:14 -0500529 DEFINE_WAIT(wait);
Yan Zheng11833d62009-09-11 16:11:19 -0400530 struct btrfs_fs_info *fs_info = cache->fs_info;
531 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -0400532 int ret = 0;
533
Josef Bacik291c7d22011-11-14 13:52:14 -0500534 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100535 if (!caching_ctl)
536 return -ENOMEM;
Josef Bacik291c7d22011-11-14 13:52:14 -0500537
538 INIT_LIST_HEAD(&caching_ctl->list);
539 mutex_init(&caching_ctl->mutex);
540 init_waitqueue_head(&caching_ctl->wait);
541 caching_ctl->block_group = cache;
542 caching_ctl->progress = cache->key.objectid;
543 atomic_set(&caching_ctl->count, 1);
544 caching_ctl->work.func = caching_thread;
545
546 spin_lock(&cache->lock);
547 /*
548 * This should be a rare occasion, but this could happen I think in the
549 * case where one thread starts to load the space cache info, and then
550 * some other thread starts a transaction commit which tries to do an
551 * allocation while the other thread is still loading the space cache
552 * info. The previous loop should have kept us from choosing this block
553 * group, but if we've moved to the state where we will wait on caching
554 * block groups we need to first check if we're doing a fast load here,
555 * so we can wait for it to finish, otherwise we could end up allocating
556 * from a block group who's cache gets evicted for one reason or
557 * another.
558 */
559 while (cache->cached == BTRFS_CACHE_FAST) {
560 struct btrfs_caching_control *ctl;
561
562 ctl = cache->caching_ctl;
563 atomic_inc(&ctl->count);
564 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
565 spin_unlock(&cache->lock);
566
567 schedule();
568
569 finish_wait(&ctl->wait, &wait);
570 put_caching_control(ctl);
571 spin_lock(&cache->lock);
572 }
573
574 if (cache->cached != BTRFS_CACHE_NO) {
575 spin_unlock(&cache->lock);
576 kfree(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400577 return 0;
Josef Bacik291c7d22011-11-14 13:52:14 -0500578 }
579 WARN_ON(cache->caching_ctl);
580 cache->caching_ctl = caching_ctl;
581 cache->cached = BTRFS_CACHE_FAST;
582 spin_unlock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -0400583
Josef Bacikd53ba472012-04-12 16:03:57 -0400584 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400585 ret = load_free_space_cache(fs_info, cache);
586
587 spin_lock(&cache->lock);
588 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500589 cache->caching_ctl = NULL;
Josef Bacik9d66e232010-08-25 16:54:15 -0400590 cache->cached = BTRFS_CACHE_FINISHED;
591 cache->last_byte_to_unpin = (u64)-1;
592 } else {
Josef Bacik291c7d22011-11-14 13:52:14 -0500593 if (load_cache_only) {
594 cache->caching_ctl = NULL;
595 cache->cached = BTRFS_CACHE_NO;
596 } else {
597 cache->cached = BTRFS_CACHE_STARTED;
598 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400599 }
600 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500601 wake_up(&caching_ctl->wait);
Josef Bacik3c148742011-02-02 15:53:47 +0000602 if (ret == 1) {
Josef Bacik291c7d22011-11-14 13:52:14 -0500603 put_caching_control(caching_ctl);
Josef Bacik3c148742011-02-02 15:53:47 +0000604 free_excluded_extents(fs_info->extent_root, cache);
Josef Bacik9d66e232010-08-25 16:54:15 -0400605 return 0;
Josef Bacik3c148742011-02-02 15:53:47 +0000606 }
Josef Bacik291c7d22011-11-14 13:52:14 -0500607 } else {
608 /*
609 * We are not going to do the fast caching, set cached to the
610 * appropriate value and wakeup any waiters.
611 */
612 spin_lock(&cache->lock);
613 if (load_cache_only) {
614 cache->caching_ctl = NULL;
615 cache->cached = BTRFS_CACHE_NO;
616 } else {
617 cache->cached = BTRFS_CACHE_STARTED;
618 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400619 spin_unlock(&cache->lock);
Josef Bacik291c7d22011-11-14 13:52:14 -0500620 wake_up(&caching_ctl->wait);
621 }
622
623 if (load_cache_only) {
624 put_caching_control(caching_ctl);
Yan Zheng11833d62009-09-11 16:11:19 -0400625 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -0400626 }
Josef Bacik817d52f2009-07-13 21:29:25 -0400627
Yan Zheng11833d62009-09-11 16:11:19 -0400628 down_write(&fs_info->extent_commit_sem);
Josef Bacik291c7d22011-11-14 13:52:14 -0500629 atomic_inc(&caching_ctl->count);
Yan Zheng11833d62009-09-11 16:11:19 -0400630 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
631 up_write(&fs_info->extent_commit_sem);
632
Josef Bacik11dfe352009-11-13 20:12:59 +0000633 btrfs_get_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -0400634
Josef Bacikbab39bf2011-06-30 14:42:28 -0400635 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
Josef Bacik817d52f2009-07-13 21:29:25 -0400636
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400637 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400638}
639
Josef Bacik0f9dd462008-09-23 13:14:11 -0400640/*
641 * return the block group that starts at or after bytenr
642 */
Chris Masond3977122009-01-05 21:25:51 -0500643static struct btrfs_block_group_cache *
644btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400645{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400646 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400647
Josef Bacik0f9dd462008-09-23 13:14:11 -0400648 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400649
Josef Bacik0f9dd462008-09-23 13:14:11 -0400650 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400651}
652
Josef Bacik0f9dd462008-09-23 13:14:11 -0400653/*
Sankar P9f556842009-05-14 13:52:22 -0400654 * return the block group that contains the given bytenr
Josef Bacik0f9dd462008-09-23 13:14:11 -0400655 */
Chris Masond3977122009-01-05 21:25:51 -0500656struct btrfs_block_group_cache *btrfs_lookup_block_group(
657 struct btrfs_fs_info *info,
658 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400659{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400660 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400661
Josef Bacik0f9dd462008-09-23 13:14:11 -0400662 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400663
Josef Bacik0f9dd462008-09-23 13:14:11 -0400664 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400665}
Chris Mason0b86a832008-03-24 15:01:56 -0400666
Josef Bacik0f9dd462008-09-23 13:14:11 -0400667static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
668 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400669{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400670 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400671 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400672
Ilya Dryomov52ba6922012-01-16 22:04:47 +0200673 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
Yan, Zhengb742bb82010-05-16 10:46:24 -0400674
Chris Mason4184ea72009-03-10 12:39:20 -0400675 rcu_read_lock();
676 list_for_each_entry_rcu(found, head, list) {
Josef Bacik67377732010-09-16 16:19:09 -0400677 if (found->flags & flags) {
Chris Mason4184ea72009-03-10 12:39:20 -0400678 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400679 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400680 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400681 }
Chris Mason4184ea72009-03-10 12:39:20 -0400682 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400683 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400684}
685
Chris Mason4184ea72009-03-10 12:39:20 -0400686/*
687 * after adding space to the filesystem, we need to clear the full flags
688 * on all the space infos.
689 */
690void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
691{
692 struct list_head *head = &info->space_info;
693 struct btrfs_space_info *found;
694
695 rcu_read_lock();
696 list_for_each_entry_rcu(found, head, list)
697 found->full = 0;
698 rcu_read_unlock();
699}
700
Chris Masone02119d2008-09-05 16:13:11 -0400701/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400702int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400703{
704 int ret;
705 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400706 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400707
Zheng Yan31840ae2008-09-23 13:14:14 -0400708 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700709 if (!path)
710 return -ENOMEM;
711
Chris Masone02119d2008-09-05 16:13:11 -0400712 key.objectid = start;
713 key.offset = len;
Josef Bacik3173a182013-03-07 14:22:04 -0500714 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone02119d2008-09-05 16:13:11 -0400715 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
716 0, 0);
Josef Bacik3173a182013-03-07 14:22:04 -0500717 if (ret > 0) {
718 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
719 if (key.objectid == start &&
720 key.type == BTRFS_METADATA_ITEM_KEY)
721 ret = 0;
722 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400723 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500724 return ret;
725}
726
Chris Masond8d5f3e2007-12-11 12:42:00 -0500727/*
Josef Bacik3173a182013-03-07 14:22:04 -0500728 * helper function to lookup reference count and flags of a tree block.
Yan, Zhenga22285a2010-05-16 10:48:46 -0400729 *
730 * the head node for delayed ref is used to store the sum of all the
731 * reference count modifications queued up in the rbtree. the head
732 * node may also store the extent flags to set. This way you can check
733 * to see what the reference count and extent flags would be if all of
734 * the delayed refs are not processed.
735 */
736int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root, u64 bytenr,
Josef Bacik3173a182013-03-07 14:22:04 -0500738 u64 offset, int metadata, u64 *refs, u64 *flags)
Yan, Zhenga22285a2010-05-16 10:48:46 -0400739{
740 struct btrfs_delayed_ref_head *head;
741 struct btrfs_delayed_ref_root *delayed_refs;
742 struct btrfs_path *path;
743 struct btrfs_extent_item *ei;
744 struct extent_buffer *leaf;
745 struct btrfs_key key;
746 u32 item_size;
747 u64 num_refs;
748 u64 extent_flags;
749 int ret;
750
Josef Bacik3173a182013-03-07 14:22:04 -0500751 /*
752 * If we don't have skinny metadata, don't bother doing anything
753 * different
754 */
755 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
756 offset = root->leafsize;
757 metadata = 0;
758 }
759
Yan, Zhenga22285a2010-05-16 10:48:46 -0400760 path = btrfs_alloc_path();
761 if (!path)
762 return -ENOMEM;
763
Josef Bacik3173a182013-03-07 14:22:04 -0500764 if (metadata) {
765 key.objectid = bytenr;
766 key.type = BTRFS_METADATA_ITEM_KEY;
767 key.offset = offset;
768 } else {
769 key.objectid = bytenr;
770 key.type = BTRFS_EXTENT_ITEM_KEY;
771 key.offset = offset;
772 }
773
Yan, Zhenga22285a2010-05-16 10:48:46 -0400774 if (!trans) {
775 path->skip_locking = 1;
776 path->search_commit_root = 1;
777 }
778again:
779 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
780 &key, path, 0, 0);
781 if (ret < 0)
782 goto out_free;
783
Josef Bacik3173a182013-03-07 14:22:04 -0500784 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
Filipe David Borba Manana74be9512013-07-05 23:12:06 +0100785 metadata = 0;
786 if (path->slots[0]) {
787 path->slots[0]--;
788 btrfs_item_key_to_cpu(path->nodes[0], &key,
789 path->slots[0]);
790 if (key.objectid == bytenr &&
791 key.type == BTRFS_EXTENT_ITEM_KEY &&
792 key.offset == root->leafsize)
793 ret = 0;
794 }
795 if (ret) {
796 key.objectid = bytenr;
797 key.type = BTRFS_EXTENT_ITEM_KEY;
798 key.offset = root->leafsize;
799 btrfs_release_path(path);
800 goto again;
801 }
Josef Bacik3173a182013-03-07 14:22:04 -0500802 }
803
Yan, Zhenga22285a2010-05-16 10:48:46 -0400804 if (ret == 0) {
805 leaf = path->nodes[0];
806 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
807 if (item_size >= sizeof(*ei)) {
808 ei = btrfs_item_ptr(leaf, path->slots[0],
809 struct btrfs_extent_item);
810 num_refs = btrfs_extent_refs(leaf, ei);
811 extent_flags = btrfs_extent_flags(leaf, ei);
812 } else {
813#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
814 struct btrfs_extent_item_v0 *ei0;
815 BUG_ON(item_size != sizeof(*ei0));
816 ei0 = btrfs_item_ptr(leaf, path->slots[0],
817 struct btrfs_extent_item_v0);
818 num_refs = btrfs_extent_refs_v0(leaf, ei0);
819 /* FIXME: this isn't correct for data */
820 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
821#else
822 BUG();
823#endif
824 }
825 BUG_ON(num_refs == 0);
826 } else {
827 num_refs = 0;
828 extent_flags = 0;
829 ret = 0;
830 }
831
832 if (!trans)
833 goto out;
834
835 delayed_refs = &trans->transaction->delayed_refs;
836 spin_lock(&delayed_refs->lock);
837 head = btrfs_find_delayed_ref_head(trans, bytenr);
838 if (head) {
839 if (!mutex_trylock(&head->mutex)) {
840 atomic_inc(&head->node.refs);
841 spin_unlock(&delayed_refs->lock);
842
David Sterbab3b4aa72011-04-21 01:20:15 +0200843 btrfs_release_path(path);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400844
David Sterba8cc33e52011-05-02 15:29:25 +0200845 /*
846 * Mutex was contended, block until it's released and try
847 * again
848 */
Yan, Zhenga22285a2010-05-16 10:48:46 -0400849 mutex_lock(&head->mutex);
850 mutex_unlock(&head->mutex);
851 btrfs_put_delayed_ref(&head->node);
852 goto again;
853 }
854 if (head->extent_op && head->extent_op->update_flags)
855 extent_flags |= head->extent_op->flags_to_set;
856 else
857 BUG_ON(num_refs == 0);
858
859 num_refs += head->node.ref_mod;
860 mutex_unlock(&head->mutex);
861 }
862 spin_unlock(&delayed_refs->lock);
863out:
864 WARN_ON(num_refs == 0);
865 if (refs)
866 *refs = num_refs;
867 if (flags)
868 *flags = extent_flags;
869out_free:
870 btrfs_free_path(path);
871 return ret;
872}
873
874/*
Chris Masond8d5f3e2007-12-11 12:42:00 -0500875 * Back reference rules. Back refs have three main goals:
876 *
877 * 1) differentiate between all holders of references to an extent so that
878 * when a reference is dropped we can make sure it was a valid reference
879 * before freeing the extent.
880 *
881 * 2) Provide enough information to quickly find the holders of an extent
882 * if we notice a given block is corrupted or bad.
883 *
884 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
885 * maintenance. This is actually the same as #2, but with a slightly
886 * different use case.
887 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400888 * There are two kinds of back refs. The implicit back refs is optimized
889 * for pointers in non-shared tree blocks. For a given pointer in a block,
890 * back refs of this kind provide information about the block's owner tree
891 * and the pointer's key. These information allow us to find the block by
892 * b-tree searching. The full back refs is for pointers in tree blocks not
893 * referenced by their owner trees. The location of tree block is recorded
894 * in the back refs. Actually the full back refs is generic, and can be
895 * used in all cases the implicit back refs is used. The major shortcoming
896 * of the full back refs is its overhead. Every time a tree block gets
897 * COWed, we have to update back refs entry for all pointers in it.
898 *
899 * For a newly allocated tree block, we use implicit back refs for
900 * pointers in it. This means most tree related operations only involve
901 * implicit back refs. For a tree block created in old transaction, the
902 * only way to drop a reference to it is COW it. So we can detect the
903 * event that tree block loses its owner tree's reference and do the
904 * back refs conversion.
905 *
906 * When a tree block is COW'd through a tree, there are four cases:
907 *
908 * The reference count of the block is one and the tree is the block's
909 * owner tree. Nothing to do in this case.
910 *
911 * The reference count of the block is one and the tree is not the
912 * block's owner tree. In this case, full back refs is used for pointers
913 * in the block. Remove these full back refs, add implicit back refs for
914 * every pointers in the new block.
915 *
916 * The reference count of the block is greater than one and the tree is
917 * the block's owner tree. In this case, implicit back refs is used for
918 * pointers in the block. Add full back refs for every pointers in the
919 * block, increase lower level extents' reference counts. The original
920 * implicit back refs are entailed to the new block.
921 *
922 * The reference count of the block is greater than one and the tree is
923 * not the block's owner tree. Add implicit back refs for every pointer in
924 * the new block, increase lower level extents' reference count.
925 *
926 * Back Reference Key composing:
927 *
928 * The key objectid corresponds to the first byte in the extent,
929 * The key type is used to differentiate between types of back refs.
930 * There are different meanings of the key offset for different types
931 * of back refs.
932 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500933 * File extents can be referenced by:
934 *
935 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400936 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500937 * - different offsets inside a file (bookend extents in file.c)
938 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400939 * The extent ref structure for the implicit back refs has fields for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500940 *
941 * - Objectid of the subvolume root
Chris Masond8d5f3e2007-12-11 12:42:00 -0500942 * - objectid of the file holding the reference
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400943 * - original offset in the file
944 * - how many bookend extents
Zheng Yan31840ae2008-09-23 13:14:14 -0400945 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400946 * The key offset for the implicit back refs is hash of the first
947 * three fields.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500948 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400949 * The extent ref structure for the full back refs has field for:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500950 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400951 * - number of pointers in the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500952 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400953 * The key offset for the implicit back refs is the first byte of
954 * the tree leaf
Chris Masond8d5f3e2007-12-11 12:42:00 -0500955 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400956 * When a file extent is allocated, The implicit back refs is used.
957 * the fields are filled in:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500958 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400959 * (root_key.objectid, inode objectid, offset in file, 1)
960 *
961 * When a file extent is removed file truncation, we find the
962 * corresponding implicit back refs and check the following fields:
963 *
964 * (btrfs_header_owner(leaf), inode objectid, offset in file)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500965 *
966 * Btree extents can be referenced by:
967 *
968 * - Different subvolumes
Chris Masond8d5f3e2007-12-11 12:42:00 -0500969 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400970 * Both the implicit back refs and the full back refs for tree blocks
971 * only consist of key. The key offset for the implicit back refs is
972 * objectid of block's owner tree. The key offset for the full back refs
973 * is the first byte of parent block.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500974 *
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400975 * When implicit back refs is used, information about the lowest key and
976 * level of the tree block are required. These information are stored in
977 * tree block info structure.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500978 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400979
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400980#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
981static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
982 struct btrfs_root *root,
983 struct btrfs_path *path,
984 u64 owner, u32 extra_size)
Chris Mason74493f72007-12-11 09:25:06 -0500985{
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400986 struct btrfs_extent_item *item;
987 struct btrfs_extent_item_v0 *ei0;
988 struct btrfs_extent_ref_v0 *ref0;
989 struct btrfs_tree_block_info *bi;
Zheng Yan31840ae2008-09-23 13:14:14 -0400990 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400991 struct btrfs_key key;
992 struct btrfs_key found_key;
993 u32 new_size = sizeof(*item);
994 u64 refs;
Chris Mason74493f72007-12-11 09:25:06 -0500995 int ret;
996
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400997 leaf = path->nodes[0];
998 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
Chris Mason74493f72007-12-11 09:25:06 -0500999
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001000 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1001 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1002 struct btrfs_extent_item_v0);
1003 refs = btrfs_extent_refs_v0(leaf, ei0);
1004
1005 if (owner == (u64)-1) {
1006 while (1) {
1007 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1008 ret = btrfs_next_leaf(root, path);
1009 if (ret < 0)
1010 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001011 BUG_ON(ret > 0); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001012 leaf = path->nodes[0];
1013 }
1014 btrfs_item_key_to_cpu(leaf, &found_key,
1015 path->slots[0]);
1016 BUG_ON(key.objectid != found_key.objectid);
1017 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1018 path->slots[0]++;
1019 continue;
1020 }
1021 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1022 struct btrfs_extent_ref_v0);
1023 owner = btrfs_ref_objectid_v0(leaf, ref0);
1024 break;
1025 }
1026 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001027 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001028
1029 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1030 new_size += sizeof(*bi);
1031
1032 new_size -= sizeof(*ei0);
1033 ret = btrfs_search_slot(trans, root, &key, path,
1034 new_size + extra_size, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001035 if (ret < 0)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001036 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001037 BUG_ON(ret); /* Corruption */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001038
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001039 btrfs_extend_item(root, path, new_size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001040
1041 leaf = path->nodes[0];
1042 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1043 btrfs_set_extent_refs(leaf, item, refs);
1044 /* FIXME: get real generation */
1045 btrfs_set_extent_generation(leaf, item, 0);
1046 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1047 btrfs_set_extent_flags(leaf, item,
1048 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1049 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1050 bi = (struct btrfs_tree_block_info *)(item + 1);
1051 /* FIXME: get first key of the block */
1052 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1053 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1054 } else {
1055 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1056 }
1057 btrfs_mark_buffer_dirty(leaf);
1058 return 0;
1059}
1060#endif
1061
1062static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1063{
1064 u32 high_crc = ~(u32)0;
1065 u32 low_crc = ~(u32)0;
1066 __le64 lenum;
1067
1068 lenum = cpu_to_le64(root_objectid);
David Woodhouse163e7832009-04-19 13:02:41 +01001069 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001070 lenum = cpu_to_le64(owner);
David Woodhouse163e7832009-04-19 13:02:41 +01001071 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001072 lenum = cpu_to_le64(offset);
David Woodhouse163e7832009-04-19 13:02:41 +01001073 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001074
1075 return ((u64)high_crc << 31) ^ (u64)low_crc;
1076}
1077
1078static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1079 struct btrfs_extent_data_ref *ref)
1080{
1081 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1082 btrfs_extent_data_ref_objectid(leaf, ref),
1083 btrfs_extent_data_ref_offset(leaf, ref));
1084}
1085
1086static int match_extent_data_ref(struct extent_buffer *leaf,
1087 struct btrfs_extent_data_ref *ref,
1088 u64 root_objectid, u64 owner, u64 offset)
1089{
1090 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1091 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1092 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1093 return 0;
1094 return 1;
1095}
1096
1097static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root,
1099 struct btrfs_path *path,
1100 u64 bytenr, u64 parent,
1101 u64 root_objectid,
1102 u64 owner, u64 offset)
1103{
1104 struct btrfs_key key;
1105 struct btrfs_extent_data_ref *ref;
1106 struct extent_buffer *leaf;
1107 u32 nritems;
1108 int ret;
1109 int recow;
1110 int err = -ENOENT;
1111
1112 key.objectid = bytenr;
1113 if (parent) {
1114 key.type = BTRFS_SHARED_DATA_REF_KEY;
1115 key.offset = parent;
1116 } else {
1117 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1118 key.offset = hash_extent_data_ref(root_objectid,
1119 owner, offset);
1120 }
1121again:
1122 recow = 0;
1123 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1124 if (ret < 0) {
1125 err = ret;
1126 goto fail;
1127 }
1128
1129 if (parent) {
1130 if (!ret)
1131 return 0;
1132#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1133 key.type = BTRFS_EXTENT_REF_V0_KEY;
David Sterbab3b4aa72011-04-21 01:20:15 +02001134 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001135 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1136 if (ret < 0) {
1137 err = ret;
1138 goto fail;
1139 }
1140 if (!ret)
1141 return 0;
1142#endif
1143 goto fail;
Zheng Yan31840ae2008-09-23 13:14:14 -04001144 }
1145
1146 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001147 nritems = btrfs_header_nritems(leaf);
1148 while (1) {
1149 if (path->slots[0] >= nritems) {
1150 ret = btrfs_next_leaf(root, path);
1151 if (ret < 0)
1152 err = ret;
1153 if (ret)
1154 goto fail;
1155
1156 leaf = path->nodes[0];
1157 nritems = btrfs_header_nritems(leaf);
1158 recow = 1;
1159 }
1160
1161 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1162 if (key.objectid != bytenr ||
1163 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1164 goto fail;
1165
1166 ref = btrfs_item_ptr(leaf, path->slots[0],
1167 struct btrfs_extent_data_ref);
1168
1169 if (match_extent_data_ref(leaf, ref, root_objectid,
1170 owner, offset)) {
1171 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001172 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001173 goto again;
1174 }
1175 err = 0;
1176 break;
1177 }
1178 path->slots[0]++;
Zheng Yan31840ae2008-09-23 13:14:14 -04001179 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001180fail:
1181 return err;
Zheng Yan31840ae2008-09-23 13:14:14 -04001182}
1183
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001184static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root,
1186 struct btrfs_path *path,
1187 u64 bytenr, u64 parent,
1188 u64 root_objectid, u64 owner,
1189 u64 offset, int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -04001190{
1191 struct btrfs_key key;
1192 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001193 u32 size;
Zheng Yan31840ae2008-09-23 13:14:14 -04001194 u32 num_refs;
1195 int ret;
1196
1197 key.objectid = bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001198 if (parent) {
1199 key.type = BTRFS_SHARED_DATA_REF_KEY;
1200 key.offset = parent;
1201 size = sizeof(struct btrfs_shared_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001202 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001203 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1204 key.offset = hash_extent_data_ref(root_objectid,
1205 owner, offset);
1206 size = sizeof(struct btrfs_extent_data_ref);
Zheng Yan31840ae2008-09-23 13:14:14 -04001207 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001208
1209 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1210 if (ret && ret != -EEXIST)
1211 goto fail;
1212
1213 leaf = path->nodes[0];
1214 if (parent) {
1215 struct btrfs_shared_data_ref *ref;
1216 ref = btrfs_item_ptr(leaf, path->slots[0],
1217 struct btrfs_shared_data_ref);
1218 if (ret == 0) {
1219 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1220 } else {
1221 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1222 num_refs += refs_to_add;
1223 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1224 }
1225 } else {
1226 struct btrfs_extent_data_ref *ref;
1227 while (ret == -EEXIST) {
1228 ref = btrfs_item_ptr(leaf, path->slots[0],
1229 struct btrfs_extent_data_ref);
1230 if (match_extent_data_ref(leaf, ref, root_objectid,
1231 owner, offset))
1232 break;
David Sterbab3b4aa72011-04-21 01:20:15 +02001233 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001234 key.offset++;
1235 ret = btrfs_insert_empty_item(trans, root, path, &key,
1236 size);
1237 if (ret && ret != -EEXIST)
1238 goto fail;
1239
1240 leaf = path->nodes[0];
1241 }
1242 ref = btrfs_item_ptr(leaf, path->slots[0],
1243 struct btrfs_extent_data_ref);
1244 if (ret == 0) {
1245 btrfs_set_extent_data_ref_root(leaf, ref,
1246 root_objectid);
1247 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1248 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1249 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1250 } else {
1251 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1252 num_refs += refs_to_add;
1253 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1254 }
1255 }
1256 btrfs_mark_buffer_dirty(leaf);
1257 ret = 0;
1258fail:
David Sterbab3b4aa72011-04-21 01:20:15 +02001259 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05001260 return ret;
Chris Mason74493f72007-12-11 09:25:06 -05001261}
1262
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001263static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root,
1265 struct btrfs_path *path,
1266 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -04001267{
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001268 struct btrfs_key key;
1269 struct btrfs_extent_data_ref *ref1 = NULL;
1270 struct btrfs_shared_data_ref *ref2 = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -04001271 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001272 u32 num_refs = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001273 int ret = 0;
1274
1275 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001276 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1277
1278 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1279 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1280 struct btrfs_extent_data_ref);
1281 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1282 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1283 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_shared_data_ref);
1285 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1286#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1287 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1288 struct btrfs_extent_ref_v0 *ref0;
1289 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1290 struct btrfs_extent_ref_v0);
1291 num_refs = btrfs_ref_count_v0(leaf, ref0);
1292#endif
1293 } else {
1294 BUG();
1295 }
1296
Chris Mason56bec292009-03-13 10:10:06 -04001297 BUG_ON(num_refs < refs_to_drop);
1298 num_refs -= refs_to_drop;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001299
Zheng Yan31840ae2008-09-23 13:14:14 -04001300 if (num_refs == 0) {
1301 ret = btrfs_del_item(trans, root, path);
1302 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001303 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1304 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1305 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1306 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1307#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1308 else {
1309 struct btrfs_extent_ref_v0 *ref0;
1310 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1311 struct btrfs_extent_ref_v0);
1312 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1313 }
1314#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04001315 btrfs_mark_buffer_dirty(leaf);
1316 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001317 return ret;
1318}
1319
1320static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1321 struct btrfs_path *path,
1322 struct btrfs_extent_inline_ref *iref)
1323{
1324 struct btrfs_key key;
1325 struct extent_buffer *leaf;
1326 struct btrfs_extent_data_ref *ref1;
1327 struct btrfs_shared_data_ref *ref2;
1328 u32 num_refs = 0;
1329
1330 leaf = path->nodes[0];
1331 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1332 if (iref) {
1333 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1334 BTRFS_EXTENT_DATA_REF_KEY) {
1335 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1336 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1337 } else {
1338 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1339 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1340 }
1341 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1342 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1343 struct btrfs_extent_data_ref);
1344 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1345 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1346 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1347 struct btrfs_shared_data_ref);
1348 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1349#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1350 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1351 struct btrfs_extent_ref_v0 *ref0;
1352 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1353 struct btrfs_extent_ref_v0);
1354 num_refs = btrfs_ref_count_v0(leaf, ref0);
1355#endif
1356 } else {
1357 WARN_ON(1);
1358 }
1359 return num_refs;
1360}
1361
1362static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1363 struct btrfs_root *root,
1364 struct btrfs_path *path,
1365 u64 bytenr, u64 parent,
1366 u64 root_objectid)
1367{
1368 struct btrfs_key key;
1369 int ret;
1370
1371 key.objectid = bytenr;
1372 if (parent) {
1373 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1374 key.offset = parent;
1375 } else {
1376 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1377 key.offset = root_objectid;
1378 }
1379
1380 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1381 if (ret > 0)
1382 ret = -ENOENT;
1383#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1384 if (ret == -ENOENT && parent) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001385 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001386 key.type = BTRFS_EXTENT_REF_V0_KEY;
1387 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1388 if (ret > 0)
1389 ret = -ENOENT;
1390 }
1391#endif
1392 return ret;
1393}
1394
1395static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1396 struct btrfs_root *root,
1397 struct btrfs_path *path,
1398 u64 bytenr, u64 parent,
1399 u64 root_objectid)
1400{
1401 struct btrfs_key key;
1402 int ret;
1403
1404 key.objectid = bytenr;
1405 if (parent) {
1406 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1407 key.offset = parent;
1408 } else {
1409 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1410 key.offset = root_objectid;
1411 }
1412
1413 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
David Sterbab3b4aa72011-04-21 01:20:15 +02001414 btrfs_release_path(path);
Zheng Yan31840ae2008-09-23 13:14:14 -04001415 return ret;
1416}
1417
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001418static inline int extent_ref_type(u64 parent, u64 owner)
1419{
1420 int type;
1421 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1422 if (parent > 0)
1423 type = BTRFS_SHARED_BLOCK_REF_KEY;
1424 else
1425 type = BTRFS_TREE_BLOCK_REF_KEY;
1426 } else {
1427 if (parent > 0)
1428 type = BTRFS_SHARED_DATA_REF_KEY;
1429 else
1430 type = BTRFS_EXTENT_DATA_REF_KEY;
1431 }
1432 return type;
1433}
1434
Yan Zheng2c47e6052009-06-27 21:07:35 -04001435static int find_next_key(struct btrfs_path *path, int level,
1436 struct btrfs_key *key)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001437
1438{
Yan Zheng2c47e6052009-06-27 21:07:35 -04001439 for (; level < BTRFS_MAX_LEVEL; level++) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001440 if (!path->nodes[level])
1441 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001442 if (path->slots[level] + 1 >=
1443 btrfs_header_nritems(path->nodes[level]))
1444 continue;
1445 if (level == 0)
1446 btrfs_item_key_to_cpu(path->nodes[level], key,
1447 path->slots[level] + 1);
1448 else
1449 btrfs_node_key_to_cpu(path->nodes[level], key,
1450 path->slots[level] + 1);
1451 return 0;
1452 }
1453 return 1;
1454}
1455
1456/*
1457 * look for inline back ref. if back ref is found, *ref_ret is set
1458 * to the address of inline back ref, and 0 is returned.
1459 *
1460 * if back ref isn't found, *ref_ret is set to the address where it
1461 * should be inserted, and -ENOENT is returned.
1462 *
1463 * if insert is true and there are too many inline back refs, the path
1464 * points to the extent item, and -EAGAIN is returned.
1465 *
1466 * NOTE: inline back refs are ordered in the same way that back ref
1467 * items in the tree are ordered.
1468 */
1469static noinline_for_stack
1470int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1471 struct btrfs_root *root,
1472 struct btrfs_path *path,
1473 struct btrfs_extent_inline_ref **ref_ret,
1474 u64 bytenr, u64 num_bytes,
1475 u64 parent, u64 root_objectid,
1476 u64 owner, u64 offset, int insert)
1477{
1478 struct btrfs_key key;
1479 struct extent_buffer *leaf;
1480 struct btrfs_extent_item *ei;
1481 struct btrfs_extent_inline_ref *iref;
1482 u64 flags;
1483 u64 item_size;
1484 unsigned long ptr;
1485 unsigned long end;
1486 int extra_size;
1487 int type;
1488 int want;
1489 int ret;
1490 int err = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05001491 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1492 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001493
1494 key.objectid = bytenr;
1495 key.type = BTRFS_EXTENT_ITEM_KEY;
1496 key.offset = num_bytes;
1497
1498 want = extent_ref_type(parent, owner);
1499 if (insert) {
1500 extra_size = btrfs_extent_inline_ref_size(want);
Yan Zheng85d41982009-06-11 08:51:10 -04001501 path->keep_locks = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001502 } else
1503 extra_size = -1;
Josef Bacik3173a182013-03-07 14:22:04 -05001504
1505 /*
1506 * Owner is our parent level, so we can just add one to get the level
1507 * for the block we are interested in.
1508 */
1509 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1510 key.type = BTRFS_METADATA_ITEM_KEY;
1511 key.offset = owner;
1512 }
1513
1514again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001515 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1516 if (ret < 0) {
1517 err = ret;
1518 goto out;
1519 }
Josef Bacik3173a182013-03-07 14:22:04 -05001520
1521 /*
1522 * We may be a newly converted file system which still has the old fat
1523 * extent entries for metadata, so try and see if we have one of those.
1524 */
1525 if (ret > 0 && skinny_metadata) {
1526 skinny_metadata = false;
1527 if (path->slots[0]) {
1528 path->slots[0]--;
1529 btrfs_item_key_to_cpu(path->nodes[0], &key,
1530 path->slots[0]);
1531 if (key.objectid == bytenr &&
1532 key.type == BTRFS_EXTENT_ITEM_KEY &&
1533 key.offset == num_bytes)
1534 ret = 0;
1535 }
1536 if (ret) {
1537 key.type = BTRFS_EXTENT_ITEM_KEY;
1538 key.offset = num_bytes;
1539 btrfs_release_path(path);
1540 goto again;
1541 }
1542 }
1543
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 if (ret && !insert) {
1545 err = -ENOENT;
1546 goto out;
Josef Bacik492104c2013-03-08 15:41:02 -05001547 } else if (ret) {
1548 err = -EIO;
1549 WARN_ON(1);
1550 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001551 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001552
1553 leaf = path->nodes[0];
1554 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1555#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1556 if (item_size < sizeof(*ei)) {
1557 if (!insert) {
1558 err = -ENOENT;
1559 goto out;
1560 }
1561 ret = convert_extent_item_v0(trans, root, path, owner,
1562 extra_size);
1563 if (ret < 0) {
1564 err = ret;
1565 goto out;
1566 }
1567 leaf = path->nodes[0];
1568 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1569 }
1570#endif
1571 BUG_ON(item_size < sizeof(*ei));
1572
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001573 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1574 flags = btrfs_extent_flags(leaf, ei);
1575
1576 ptr = (unsigned long)(ei + 1);
1577 end = (unsigned long)ei + item_size;
1578
Josef Bacik3173a182013-03-07 14:22:04 -05001579 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001580 ptr += sizeof(struct btrfs_tree_block_info);
1581 BUG_ON(ptr > end);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001582 }
1583
1584 err = -ENOENT;
1585 while (1) {
1586 if (ptr >= end) {
1587 WARN_ON(ptr > end);
1588 break;
1589 }
1590 iref = (struct btrfs_extent_inline_ref *)ptr;
1591 type = btrfs_extent_inline_ref_type(leaf, iref);
1592 if (want < type)
1593 break;
1594 if (want > type) {
1595 ptr += btrfs_extent_inline_ref_size(type);
1596 continue;
1597 }
1598
1599 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1600 struct btrfs_extent_data_ref *dref;
1601 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1602 if (match_extent_data_ref(leaf, dref, root_objectid,
1603 owner, offset)) {
1604 err = 0;
1605 break;
1606 }
1607 if (hash_extent_data_ref_item(leaf, dref) <
1608 hash_extent_data_ref(root_objectid, owner, offset))
1609 break;
1610 } else {
1611 u64 ref_offset;
1612 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1613 if (parent > 0) {
1614 if (parent == ref_offset) {
1615 err = 0;
1616 break;
1617 }
1618 if (ref_offset < parent)
1619 break;
1620 } else {
1621 if (root_objectid == ref_offset) {
1622 err = 0;
1623 break;
1624 }
1625 if (ref_offset < root_objectid)
1626 break;
1627 }
1628 }
1629 ptr += btrfs_extent_inline_ref_size(type);
1630 }
1631 if (err == -ENOENT && insert) {
1632 if (item_size + extra_size >=
1633 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1634 err = -EAGAIN;
1635 goto out;
1636 }
1637 /*
1638 * To add new inline back ref, we have to make sure
1639 * there is no corresponding back ref item.
1640 * For simplicity, we just do not add new inline back
1641 * ref if there is any kind of item for this block
1642 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04001643 if (find_next_key(path, 0, &key) == 0 &&
1644 key.objectid == bytenr &&
Yan Zheng85d41982009-06-11 08:51:10 -04001645 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001646 err = -EAGAIN;
1647 goto out;
1648 }
1649 }
1650 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1651out:
Yan Zheng85d41982009-06-11 08:51:10 -04001652 if (insert) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001653 path->keep_locks = 0;
1654 btrfs_unlock_up_safe(path, 1);
1655 }
1656 return err;
1657}
1658
1659/*
1660 * helper to add new inline back ref
1661 */
1662static noinline_for_stack
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001663void setup_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001664 struct btrfs_path *path,
1665 struct btrfs_extent_inline_ref *iref,
1666 u64 parent, u64 root_objectid,
1667 u64 owner, u64 offset, int refs_to_add,
1668 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001669{
1670 struct extent_buffer *leaf;
1671 struct btrfs_extent_item *ei;
1672 unsigned long ptr;
1673 unsigned long end;
1674 unsigned long item_offset;
1675 u64 refs;
1676 int size;
1677 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001678
1679 leaf = path->nodes[0];
1680 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1681 item_offset = (unsigned long)iref - (unsigned long)ei;
1682
1683 type = extent_ref_type(parent, owner);
1684 size = btrfs_extent_inline_ref_size(type);
1685
Tsutomu Itoh4b90c682013-04-16 05:18:49 +00001686 btrfs_extend_item(root, path, size);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001687
1688 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1689 refs = btrfs_extent_refs(leaf, ei);
1690 refs += refs_to_add;
1691 btrfs_set_extent_refs(leaf, ei, refs);
1692 if (extent_op)
1693 __run_delayed_extent_op(extent_op, leaf, ei);
1694
1695 ptr = (unsigned long)ei + item_offset;
1696 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1697 if (ptr < end - size)
1698 memmove_extent_buffer(leaf, ptr + size, ptr,
1699 end - size - ptr);
1700
1701 iref = (struct btrfs_extent_inline_ref *)ptr;
1702 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1703 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1704 struct btrfs_extent_data_ref *dref;
1705 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1706 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1707 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1708 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1709 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1710 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1711 struct btrfs_shared_data_ref *sref;
1712 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1713 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1714 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1715 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1716 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1717 } else {
1718 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1719 }
1720 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001721}
1722
1723static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1724 struct btrfs_root *root,
1725 struct btrfs_path *path,
1726 struct btrfs_extent_inline_ref **ref_ret,
1727 u64 bytenr, u64 num_bytes, u64 parent,
1728 u64 root_objectid, u64 owner, u64 offset)
1729{
1730 int ret;
1731
1732 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1733 bytenr, num_bytes, parent,
1734 root_objectid, owner, offset, 0);
1735 if (ret != -ENOENT)
1736 return ret;
1737
David Sterbab3b4aa72011-04-21 01:20:15 +02001738 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001739 *ref_ret = NULL;
1740
1741 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1742 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1743 root_objectid);
1744 } else {
1745 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1746 root_objectid, owner, offset);
1747 }
1748 return ret;
1749}
1750
1751/*
1752 * helper to update/remove inline back ref
1753 */
1754static noinline_for_stack
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001755void update_inline_extent_backref(struct btrfs_root *root,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001756 struct btrfs_path *path,
1757 struct btrfs_extent_inline_ref *iref,
1758 int refs_to_mod,
1759 struct btrfs_delayed_extent_op *extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001760{
1761 struct extent_buffer *leaf;
1762 struct btrfs_extent_item *ei;
1763 struct btrfs_extent_data_ref *dref = NULL;
1764 struct btrfs_shared_data_ref *sref = NULL;
1765 unsigned long ptr;
1766 unsigned long end;
1767 u32 item_size;
1768 int size;
1769 int type;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001770 u64 refs;
1771
1772 leaf = path->nodes[0];
1773 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1774 refs = btrfs_extent_refs(leaf, ei);
1775 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1776 refs += refs_to_mod;
1777 btrfs_set_extent_refs(leaf, ei, refs);
1778 if (extent_op)
1779 __run_delayed_extent_op(extent_op, leaf, ei);
1780
1781 type = btrfs_extent_inline_ref_type(leaf, iref);
1782
1783 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1784 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1785 refs = btrfs_extent_data_ref_count(leaf, dref);
1786 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1787 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1788 refs = btrfs_shared_data_ref_count(leaf, sref);
1789 } else {
1790 refs = 1;
1791 BUG_ON(refs_to_mod != -1);
1792 }
1793
1794 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1795 refs += refs_to_mod;
1796
1797 if (refs > 0) {
1798 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1799 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1800 else
1801 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1802 } else {
1803 size = btrfs_extent_inline_ref_size(type);
1804 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1805 ptr = (unsigned long)iref;
1806 end = (unsigned long)ei + item_size;
1807 if (ptr + size < end)
1808 memmove_extent_buffer(leaf, ptr, ptr + size,
1809 end - ptr - size);
1810 item_size -= size;
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001811 btrfs_truncate_item(root, path, item_size, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001812 }
1813 btrfs_mark_buffer_dirty(leaf);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001814}
1815
1816static noinline_for_stack
1817int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1818 struct btrfs_root *root,
1819 struct btrfs_path *path,
1820 u64 bytenr, u64 num_bytes, u64 parent,
1821 u64 root_objectid, u64 owner,
1822 u64 offset, int refs_to_add,
1823 struct btrfs_delayed_extent_op *extent_op)
1824{
1825 struct btrfs_extent_inline_ref *iref;
1826 int ret;
1827
1828 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1829 bytenr, num_bytes, parent,
1830 root_objectid, owner, offset, 1);
1831 if (ret == 0) {
1832 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001833 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001834 refs_to_add, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001835 } else if (ret == -ENOENT) {
Tsutomu Itohfd279fa2013-04-16 05:19:11 +00001836 setup_inline_extent_backref(root, path, iref, parent,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001837 root_objectid, owner, offset,
1838 refs_to_add, extent_op);
1839 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001840 }
1841 return ret;
1842}
1843
1844static int insert_extent_backref(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *root,
1846 struct btrfs_path *path,
1847 u64 bytenr, u64 parent, u64 root_objectid,
1848 u64 owner, u64 offset, int refs_to_add)
1849{
1850 int ret;
1851 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1852 BUG_ON(refs_to_add != 1);
1853 ret = insert_tree_block_ref(trans, root, path, bytenr,
1854 parent, root_objectid);
1855 } else {
1856 ret = insert_extent_data_ref(trans, root, path, bytenr,
1857 parent, root_objectid,
1858 owner, offset, refs_to_add);
1859 }
1860 return ret;
1861}
1862
1863static int remove_extent_backref(struct btrfs_trans_handle *trans,
1864 struct btrfs_root *root,
1865 struct btrfs_path *path,
1866 struct btrfs_extent_inline_ref *iref,
1867 int refs_to_drop, int is_data)
1868{
Jeff Mahoney143bede2012-03-01 14:56:26 +01001869 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001870
1871 BUG_ON(!is_data && refs_to_drop != 1);
1872 if (iref) {
Tsutomu Itohafe5fea2013-04-16 05:18:22 +00001873 update_inline_extent_backref(root, path, iref,
Jeff Mahoney143bede2012-03-01 14:56:26 +01001874 -refs_to_drop, NULL);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001875 } else if (is_data) {
1876 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1877 } else {
1878 ret = btrfs_del_item(trans, root, path);
1879 }
1880 return ret;
1881}
1882
Li Dongyang5378e602011-03-24 10:24:27 +00001883static int btrfs_issue_discard(struct block_device *bdev,
Chris Mason15916de2008-11-19 21:17:22 -05001884 u64 start, u64 len)
1885{
Li Dongyang5378e602011-03-24 10:24:27 +00001886 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
Chris Mason15916de2008-11-19 21:17:22 -05001887}
Chris Mason15916de2008-11-19 21:17:22 -05001888
Liu Hui1f3c79a2009-01-05 15:57:51 -05001889static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00001890 u64 num_bytes, u64 *actual_bytes)
Liu Hui1f3c79a2009-01-05 15:57:51 -05001891{
Liu Hui1f3c79a2009-01-05 15:57:51 -05001892 int ret;
Li Dongyang5378e602011-03-24 10:24:27 +00001893 u64 discarded_bytes = 0;
Jan Schmidta1d3c472011-08-04 17:15:33 +02001894 struct btrfs_bio *bbio = NULL;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001895
Christoph Hellwige244a0a2009-10-14 09:24:59 -04001896
Liu Hui1f3c79a2009-01-05 15:57:51 -05001897 /* Tell the block device(s) that the sectors can be discarded */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001898 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
Jan Schmidta1d3c472011-08-04 17:15:33 +02001899 bytenr, &num_bytes, &bbio, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001900 /* Error condition is -ENOMEM */
Liu Hui1f3c79a2009-01-05 15:57:51 -05001901 if (!ret) {
Jan Schmidta1d3c472011-08-04 17:15:33 +02001902 struct btrfs_bio_stripe *stripe = bbio->stripes;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001903 int i;
1904
Liu Hui1f3c79a2009-01-05 15:57:51 -05001905
Jan Schmidta1d3c472011-08-04 17:15:33 +02001906 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
Josef Bacikd5e20032011-08-04 14:52:27 +00001907 if (!stripe->dev->can_discard)
1908 continue;
1909
Li Dongyang5378e602011-03-24 10:24:27 +00001910 ret = btrfs_issue_discard(stripe->dev->bdev,
1911 stripe->physical,
1912 stripe->length);
1913 if (!ret)
1914 discarded_bytes += stripe->length;
1915 else if (ret != -EOPNOTSUPP)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001916 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
Josef Bacikd5e20032011-08-04 14:52:27 +00001917
1918 /*
1919 * Just in case we get back EOPNOTSUPP for some reason,
1920 * just ignore the return value so we don't screw up
1921 * people calling discard_extent.
1922 */
1923 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001924 }
Jan Schmidta1d3c472011-08-04 17:15:33 +02001925 kfree(bbio);
Liu Hui1f3c79a2009-01-05 15:57:51 -05001926 }
Li Dongyang5378e602011-03-24 10:24:27 +00001927
1928 if (actual_bytes)
1929 *actual_bytes = discarded_bytes;
1930
Liu Hui1f3c79a2009-01-05 15:57:51 -05001931
David Woodhouse53b381b2013-01-29 18:40:14 -05001932 if (ret == -EOPNOTSUPP)
1933 ret = 0;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001934 return ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001935}
1936
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001937/* Can return -ENOMEM */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001938int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1939 struct btrfs_root *root,
1940 u64 bytenr, u64 num_bytes, u64 parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001941 u64 root_objectid, u64 owner, u64 offset, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04001942{
1943 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001944 struct btrfs_fs_info *fs_info = root->fs_info;
1945
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001946 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1947 root_objectid == BTRFS_TREE_LOG_OBJECTID);
Zheng Yan31840ae2008-09-23 13:14:14 -04001948
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001949 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001950 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1951 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001952 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001953 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001954 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001955 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1956 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001957 parent, root_objectid, owner, offset,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001958 BTRFS_ADD_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001959 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001960 return ret;
1961}
1962
Chris Mason925baed2008-06-25 16:01:30 -04001963static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001964 struct btrfs_root *root,
1965 u64 bytenr, u64 num_bytes,
1966 u64 parent, u64 root_objectid,
1967 u64 owner, u64 offset, int refs_to_add,
1968 struct btrfs_delayed_extent_op *extent_op)
Chris Mason56bec292009-03-13 10:10:06 -04001969{
Chris Mason5caf2a02007-04-02 11:20:42 -04001970 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001971 struct extent_buffer *leaf;
Chris Mason234b63a2007-03-13 10:46:10 -04001972 struct btrfs_extent_item *item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001973 u64 refs;
1974 int ret;
1975 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001976
Chris Mason5caf2a02007-04-02 11:20:42 -04001977 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001978 if (!path)
1979 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001980
Chris Mason3c12ac72008-04-21 12:01:38 -04001981 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04001982 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001983 /* this will setup the path even if it fails to insert the back ref */
1984 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1985 path, bytenr, num_bytes, parent,
1986 root_objectid, owner, offset,
1987 refs_to_add, extent_op);
1988 if (ret == 0)
1989 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -04001990
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001991 if (ret != -EAGAIN) {
1992 err = ret;
1993 goto out;
Chris Masonb9473432009-03-13 11:00:37 -04001994 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001996 leaf = path->nodes[0];
1997 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1998 refs = btrfs_extent_refs(leaf, item);
1999 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2000 if (extent_op)
2001 __run_delayed_extent_op(extent_op, leaf, item);
Zheng Yan31840ae2008-09-23 13:14:14 -04002002
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002003 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02002004 btrfs_release_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -05002005
Chris Mason3c12ac72008-04-21 12:01:38 -04002006 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002007 path->leave_spinning = 1;
2008
Chris Mason56bec292009-03-13 10:10:06 -04002009 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002010 ret = insert_extent_backref(trans, root->fs_info->extent_root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002011 path, bytenr, parent, root_objectid,
2012 owner, offset, refs_to_add);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002013 if (ret)
2014 btrfs_abort_transaction(trans, root, ret);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002015out:
Chris Mason74493f72007-12-11 09:25:06 -05002016 btrfs_free_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002017 return err;
Chris Mason02217ed2007-03-02 16:08:05 -05002018}
2019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002020static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2021 struct btrfs_root *root,
2022 struct btrfs_delayed_ref_node *node,
2023 struct btrfs_delayed_extent_op *extent_op,
2024 int insert_reserved)
Chris Masone9d0b132007-08-10 14:06:19 -04002025{
Chris Mason56bec292009-03-13 10:10:06 -04002026 int ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002027 struct btrfs_delayed_data_ref *ref;
2028 struct btrfs_key ins;
2029 u64 parent = 0;
2030 u64 ref_root = 0;
2031 u64 flags = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002032
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002033 ins.objectid = node->bytenr;
2034 ins.offset = node->num_bytes;
2035 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -04002036
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002037 ref = btrfs_delayed_node_to_data_ref(node);
2038 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2039 parent = ref->parent;
2040 else
2041 ref_root = ref->root;
2042
2043 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002044 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002045 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002046 ret = alloc_reserved_file_extent(trans, root,
2047 parent, ref_root, flags,
2048 ref->objectid, ref->offset,
2049 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002050 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2051 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2052 node->num_bytes, parent,
2053 ref_root, ref->objectid,
2054 ref->offset, node->ref_mod,
2055 extent_op);
2056 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2057 ret = __btrfs_free_extent(trans, root, node->bytenr,
2058 node->num_bytes, parent,
2059 ref_root, ref->objectid,
2060 ref->offset, node->ref_mod,
2061 extent_op);
2062 } else {
2063 BUG();
2064 }
Chris Mason56bec292009-03-13 10:10:06 -04002065 return ret;
2066}
2067
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002068static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2069 struct extent_buffer *leaf,
2070 struct btrfs_extent_item *ei)
2071{
2072 u64 flags = btrfs_extent_flags(leaf, ei);
2073 if (extent_op->update_flags) {
2074 flags |= extent_op->flags_to_set;
2075 btrfs_set_extent_flags(leaf, ei, flags);
2076 }
2077
2078 if (extent_op->update_key) {
2079 struct btrfs_tree_block_info *bi;
2080 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2081 bi = (struct btrfs_tree_block_info *)(ei + 1);
2082 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2083 }
2084}
2085
2086static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2087 struct btrfs_root *root,
2088 struct btrfs_delayed_ref_node *node,
2089 struct btrfs_delayed_extent_op *extent_op)
2090{
2091 struct btrfs_key key;
2092 struct btrfs_path *path;
2093 struct btrfs_extent_item *ei;
2094 struct extent_buffer *leaf;
2095 u32 item_size;
2096 int ret;
2097 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002098 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002099
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002100 if (trans->aborted)
2101 return 0;
2102
Josef Bacik3173a182013-03-07 14:22:04 -05002103 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2104 metadata = 0;
2105
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002106 path = btrfs_alloc_path();
2107 if (!path)
2108 return -ENOMEM;
2109
2110 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002111
Josef Bacik3173a182013-03-07 14:22:04 -05002112 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002113 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002114 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002115 } else {
2116 key.type = BTRFS_EXTENT_ITEM_KEY;
2117 key.offset = node->num_bytes;
2118 }
2119
2120again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002121 path->reada = 1;
2122 path->leave_spinning = 1;
2123 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2124 path, 0, 1);
2125 if (ret < 0) {
2126 err = ret;
2127 goto out;
2128 }
2129 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002130 if (metadata) {
2131 btrfs_release_path(path);
2132 metadata = 0;
2133
2134 key.offset = node->num_bytes;
2135 key.type = BTRFS_EXTENT_ITEM_KEY;
2136 goto again;
2137 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002138 err = -EIO;
2139 goto out;
2140 }
2141
2142 leaf = path->nodes[0];
2143 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2144#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2145 if (item_size < sizeof(*ei)) {
2146 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2147 path, (u64)-1, 0);
2148 if (ret < 0) {
2149 err = ret;
2150 goto out;
2151 }
2152 leaf = path->nodes[0];
2153 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2154 }
2155#endif
2156 BUG_ON(item_size < sizeof(*ei));
2157 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2158 __run_delayed_extent_op(extent_op, leaf, ei);
2159
2160 btrfs_mark_buffer_dirty(leaf);
2161out:
2162 btrfs_free_path(path);
2163 return err;
2164}
2165
2166static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 struct btrfs_delayed_ref_node *node,
2169 struct btrfs_delayed_extent_op *extent_op,
2170 int insert_reserved)
2171{
2172 int ret = 0;
2173 struct btrfs_delayed_tree_ref *ref;
2174 struct btrfs_key ins;
2175 u64 parent = 0;
2176 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002177 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2178 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002179
2180 ref = btrfs_delayed_node_to_tree_ref(node);
2181 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2182 parent = ref->parent;
2183 else
2184 ref_root = ref->root;
2185
Josef Bacik3173a182013-03-07 14:22:04 -05002186 ins.objectid = node->bytenr;
2187 if (skinny_metadata) {
2188 ins.offset = ref->level;
2189 ins.type = BTRFS_METADATA_ITEM_KEY;
2190 } else {
2191 ins.offset = node->num_bytes;
2192 ins.type = BTRFS_EXTENT_ITEM_KEY;
2193 }
2194
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002195 BUG_ON(node->ref_mod != 1);
2196 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002197 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002198 ret = alloc_reserved_tree_block(trans, root,
2199 parent, ref_root,
2200 extent_op->flags_to_set,
2201 &extent_op->key,
2202 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002203 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2204 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2205 node->num_bytes, parent, ref_root,
2206 ref->level, 0, 1, extent_op);
2207 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2208 ret = __btrfs_free_extent(trans, root, node->bytenr,
2209 node->num_bytes, parent, ref_root,
2210 ref->level, 0, 1, extent_op);
2211 } else {
2212 BUG();
2213 }
2214 return ret;
2215}
2216
Chris Mason56bec292009-03-13 10:10:06 -04002217/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002218static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root,
2220 struct btrfs_delayed_ref_node *node,
2221 struct btrfs_delayed_extent_op *extent_op,
2222 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002223{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002224 int ret = 0;
2225
2226 if (trans->aborted)
2227 return 0;
2228
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002229 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002230 struct btrfs_delayed_ref_head *head;
2231 /*
2232 * we've hit the end of the chain and we were supposed
2233 * to insert this extent into the tree. But, it got
2234 * deleted before we ever needed to insert it, so all
2235 * we have to do is clean up the accounting
2236 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002237 BUG_ON(extent_op);
2238 head = btrfs_delayed_node_to_head(node);
Chris Mason56bec292009-03-13 10:10:06 -04002239 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002240 btrfs_pin_extent(root, node->bytenr,
2241 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002242 if (head->is_data) {
2243 ret = btrfs_del_csums(trans, root,
2244 node->bytenr,
2245 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002246 }
Chris Mason56bec292009-03-13 10:10:06 -04002247 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002248 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002249 }
Josef Bacikeb099672009-02-12 09:27:38 -05002250
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002251 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2252 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2253 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2254 insert_reserved);
2255 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2256 node->type == BTRFS_SHARED_DATA_REF_KEY)
2257 ret = run_delayed_data_ref(trans, root, node, extent_op,
2258 insert_reserved);
2259 else
2260 BUG();
2261 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002262}
2263
Chris Mason56bec292009-03-13 10:10:06 -04002264static noinline struct btrfs_delayed_ref_node *
2265select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002266{
Chris Mason56bec292009-03-13 10:10:06 -04002267 struct rb_node *node;
2268 struct btrfs_delayed_ref_node *ref;
2269 int action = BTRFS_ADD_DELAYED_REF;
2270again:
2271 /*
2272 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2273 * this prevents ref count from going down to zero when
2274 * there still are pending delayed ref.
2275 */
2276 node = rb_prev(&head->node.rb_node);
2277 while (1) {
2278 if (!node)
2279 break;
2280 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2281 rb_node);
2282 if (ref->bytenr != head->node.bytenr)
2283 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002284 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002285 return ref;
2286 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002287 }
Chris Mason56bec292009-03-13 10:10:06 -04002288 if (action == BTRFS_ADD_DELAYED_REF) {
2289 action = BTRFS_DROP_DELAYED_REF;
2290 goto again;
2291 }
2292 return NULL;
2293}
2294
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002295/*
2296 * Returns 0 on success or if called with an already aborted transaction.
2297 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2298 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002299static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root,
2301 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002302{
Chris Mason56bec292009-03-13 10:10:06 -04002303 struct btrfs_delayed_ref_root *delayed_refs;
2304 struct btrfs_delayed_ref_node *ref;
2305 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002306 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002307 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002308 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002309 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002310 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002311
2312 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002313 while (1) {
2314 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002315 /* pick a new head ref from the cluster list */
2316 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002317 break;
Chris Mason56bec292009-03-13 10:10:06 -04002318
Chris Masonc3e69d52009-03-13 10:17:05 -04002319 locked_ref = list_entry(cluster->next,
2320 struct btrfs_delayed_ref_head, cluster);
2321
2322 /* grab the lock that says we are going to process
2323 * all the refs for this head */
2324 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2325
2326 /*
2327 * we may have dropped the spin lock to get the head
2328 * mutex lock, and that might have given someone else
2329 * time to free the head. If that's true, it has been
2330 * removed from our list and we can move on.
2331 */
2332 if (ret == -EAGAIN) {
2333 locked_ref = NULL;
2334 count++;
2335 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002336 }
2337 }
2338
2339 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002340 * We need to try and merge add/drops of the same ref since we
2341 * can run into issues with relocate dropping the implicit ref
2342 * and then it being added back again before the drop can
2343 * finish. If we merged anything we need to re-loop so we can
2344 * get a good ref.
2345 */
2346 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2347 locked_ref);
2348
2349 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002350 * locked_ref is the head node, so we have to go one
2351 * node back for any delayed ref updates
2352 */
2353 ref = select_delayed_ref(locked_ref);
2354
2355 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002356 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002357 /*
2358 * there are still refs with lower seq numbers in the
2359 * process of being added. Don't run this ref yet.
2360 */
2361 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002362 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002363 locked_ref = NULL;
2364 delayed_refs->num_heads_ready++;
2365 spin_unlock(&delayed_refs->lock);
2366 cond_resched();
2367 spin_lock(&delayed_refs->lock);
2368 continue;
2369 }
2370
2371 /*
Chris Mason56bec292009-03-13 10:10:06 -04002372 * record the must insert reserved flag before we
2373 * drop the spin lock.
2374 */
2375 must_insert_reserved = locked_ref->must_insert_reserved;
2376 locked_ref->must_insert_reserved = 0;
2377
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002378 extent_op = locked_ref->extent_op;
2379 locked_ref->extent_op = NULL;
2380
Chris Mason56bec292009-03-13 10:10:06 -04002381 if (!ref) {
2382 /* All delayed refs have been processed, Go ahead
2383 * and send the head node to run_one_delayed_ref,
2384 * so that any accounting fixes can happen
2385 */
2386 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002387
2388 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002389 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002390 extent_op = NULL;
2391 }
2392
2393 if (extent_op) {
2394 spin_unlock(&delayed_refs->lock);
2395
2396 ret = run_delayed_extent_op(trans, root,
2397 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002398 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002399
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002400 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002401 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002402 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002403 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002404 return ret;
2405 }
2406
Chris Mason203bf282012-01-06 15:23:57 -05002407 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002408 }
Chris Mason56bec292009-03-13 10:10:06 -04002409 }
2410
2411 ref->in_tree = 0;
2412 rb_erase(&ref->rb_node, &delayed_refs->root);
2413 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002414 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002415 /*
2416 * when we play the delayed ref, also correct the
2417 * ref_mod on head
2418 */
2419 switch (ref->action) {
2420 case BTRFS_ADD_DELAYED_REF:
2421 case BTRFS_ADD_DELAYED_EXTENT:
2422 locked_ref->node.ref_mod -= ref->ref_mod;
2423 break;
2424 case BTRFS_DROP_DELAYED_REF:
2425 locked_ref->node.ref_mod += ref->ref_mod;
2426 break;
2427 default:
2428 WARN_ON(1);
2429 }
2430 }
Chris Mason56bec292009-03-13 10:10:06 -04002431 spin_unlock(&delayed_refs->lock);
2432
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002433 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002434 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002435
Miao Xie78a61842012-11-21 02:21:28 +00002436 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002437 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002438 btrfs_delayed_ref_unlock(locked_ref);
2439 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002440 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002441 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002442 return ret;
2443 }
2444
Miao Xie093486c2012-12-19 08:10:10 +00002445 /*
2446 * If this node is a head, that means all the refs in this head
2447 * have been dealt with, and we will pick the next head to deal
2448 * with, so we must unlock the head and drop it from the cluster
2449 * list before we release it.
2450 */
2451 if (btrfs_delayed_ref_is_head(ref)) {
2452 list_del_init(&locked_ref->cluster);
2453 btrfs_delayed_ref_unlock(locked_ref);
2454 locked_ref = NULL;
2455 }
2456 btrfs_put_delayed_ref(ref);
2457 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002458next:
Chris Mason1887be62009-03-13 10:11:24 -04002459 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002460 spin_lock(&delayed_refs->lock);
2461 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002462 return count;
2463}
2464
Arne Jansen709c0482011-09-12 12:22:57 +02002465#ifdef SCRAMBLE_DELAYED_REFS
2466/*
2467 * Normally delayed refs get processed in ascending bytenr order. This
2468 * correlates in most cases to the order added. To expose dependencies on this
2469 * order, we start to process the tree in the middle instead of the beginning
2470 */
2471static u64 find_middle(struct rb_root *root)
2472{
2473 struct rb_node *n = root->rb_node;
2474 struct btrfs_delayed_ref_node *entry;
2475 int alt = 1;
2476 u64 middle;
2477 u64 first = 0, last = 0;
2478
2479 n = rb_first(root);
2480 if (n) {
2481 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2482 first = entry->bytenr;
2483 }
2484 n = rb_last(root);
2485 if (n) {
2486 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2487 last = entry->bytenr;
2488 }
2489 n = root->rb_node;
2490
2491 while (n) {
2492 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2493 WARN_ON(!entry->in_tree);
2494
2495 middle = entry->bytenr;
2496
2497 if (alt)
2498 n = n->rb_left;
2499 else
2500 n = n->rb_right;
2501
2502 alt = 1 - alt;
2503 }
2504 return middle;
2505}
2506#endif
2507
Arne Jansenbed92ea2012-06-28 18:03:02 +02002508int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2509 struct btrfs_fs_info *fs_info)
2510{
2511 struct qgroup_update *qgroup_update;
2512 int ret = 0;
2513
2514 if (list_empty(&trans->qgroup_ref_list) !=
2515 !trans->delayed_ref_elem.seq) {
2516 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002517 btrfs_err(fs_info,
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002518 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002519 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed72013-04-24 16:57:33 +00002520 (u32)(trans->delayed_ref_elem.seq >> 32),
2521 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002522 BUG();
2523 }
2524
2525 if (!trans->delayed_ref_elem.seq)
2526 return 0;
2527
2528 while (!list_empty(&trans->qgroup_ref_list)) {
2529 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2530 struct qgroup_update, list);
2531 list_del(&qgroup_update->list);
2532 if (!ret)
2533 ret = btrfs_qgroup_account_ref(
2534 trans, fs_info, qgroup_update->node,
2535 qgroup_update->extent_op);
2536 kfree(qgroup_update);
2537 }
2538
2539 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2540
2541 return ret;
2542}
2543
Chris Masonbb721702013-01-29 18:44:12 -05002544static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2545 int count)
2546{
2547 int val = atomic_read(&delayed_refs->ref_seq);
2548
2549 if (val < seq || val >= seq + count)
2550 return 1;
2551 return 0;
2552}
2553
Josef Bacik1be41b72013-06-12 13:56:06 -04002554static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2555{
2556 u64 num_bytes;
2557
2558 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2559 sizeof(struct btrfs_extent_inline_ref));
2560 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2561 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2562
2563 /*
2564 * We don't ever fill up leaves all the way so multiply by 2 just to be
2565 * closer to what we're really going to want to ouse.
2566 */
2567 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2568}
2569
2570int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2571 struct btrfs_root *root)
2572{
2573 struct btrfs_block_rsv *global_rsv;
2574 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2575 u64 num_bytes;
2576 int ret = 0;
2577
2578 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2579 num_heads = heads_to_leaves(root, num_heads);
2580 if (num_heads > 1)
2581 num_bytes += (num_heads - 1) * root->leafsize;
2582 num_bytes <<= 1;
2583 global_rsv = &root->fs_info->global_block_rsv;
2584
2585 /*
2586 * If we can't allocate any more chunks lets make sure we have _lots_ of
2587 * wiggle room since running delayed refs can create more delayed refs.
2588 */
2589 if (global_rsv->space_info->full)
2590 num_bytes <<= 1;
2591
2592 spin_lock(&global_rsv->lock);
2593 if (global_rsv->reserved <= num_bytes)
2594 ret = 1;
2595 spin_unlock(&global_rsv->lock);
2596 return ret;
2597}
2598
Chris Masonc3e69d52009-03-13 10:17:05 -04002599/*
2600 * this starts processing the delayed reference count updates and
2601 * extent insertions we have queued up so far. count can be
2602 * 0, which means to process everything in the tree at the start
2603 * of the run (but not newly added entries), or it can be some target
2604 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002605 *
2606 * Returns 0 on success or if called with an aborted transaction
2607 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002608 */
2609int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *root, unsigned long count)
2611{
2612 struct rb_node *node;
2613 struct btrfs_delayed_ref_root *delayed_refs;
2614 struct btrfs_delayed_ref_node *ref;
2615 struct list_head cluster;
2616 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002617 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002618 int run_all = count == (unsigned long)-1;
2619 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002620 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002621
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 /* We'll clean this up in btrfs_cleanup_transaction */
2623 if (trans->aborted)
2624 return 0;
2625
Chris Masonc3e69d52009-03-13 10:17:05 -04002626 if (root == root->fs_info->extent_root)
2627 root = root->fs_info->tree_root;
2628
Jan Schmidtedf39272012-06-28 18:04:55 +02002629 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2630
Chris Masonc3e69d52009-03-13 10:17:05 -04002631 delayed_refs = &trans->transaction->delayed_refs;
2632 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002633 if (count == 0) {
2634 count = delayed_refs->num_entries * 2;
2635 run_most = 1;
2636 }
2637
2638 if (!run_all && !run_most) {
2639 int old;
2640 int seq = atomic_read(&delayed_refs->ref_seq);
2641
2642progress:
2643 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2644 if (old) {
2645 DEFINE_WAIT(__wait);
Josef Bacik1be41b72013-06-12 13:56:06 -04002646 if (delayed_refs->flushing ||
2647 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonbb721702013-01-29 18:44:12 -05002648 return 0;
2649
2650 prepare_to_wait(&delayed_refs->wait, &__wait,
2651 TASK_UNINTERRUPTIBLE);
2652
2653 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2654 if (old) {
2655 schedule();
2656 finish_wait(&delayed_refs->wait, &__wait);
2657
2658 if (!refs_newer(delayed_refs, seq, 256))
2659 goto progress;
2660 else
2661 return 0;
2662 } else {
2663 finish_wait(&delayed_refs->wait, &__wait);
2664 goto again;
2665 }
2666 }
2667
2668 } else {
2669 atomic_inc(&delayed_refs->procs_running_refs);
2670 }
2671
Chris Masonc3e69d52009-03-13 10:17:05 -04002672again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002673 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002674 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002675
Arne Jansen709c0482011-09-12 12:22:57 +02002676#ifdef SCRAMBLE_DELAYED_REFS
2677 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2678#endif
2679
Chris Masonc3e69d52009-03-13 10:17:05 -04002680 while (1) {
2681 if (!(run_all || run_most) &&
Josef Bacik1be41b72013-06-12 13:56:06 -04002682 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonc3e69d52009-03-13 10:17:05 -04002683 break;
2684
2685 /*
2686 * go find something we can process in the rbtree. We start at
2687 * the beginning of the tree, and then build a cluster
2688 * of refs to process starting at the first one we are able to
2689 * lock
2690 */
Jan Schmidta1686502011-12-12 16:10:07 +01002691 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002692 ret = btrfs_find_ref_cluster(trans, &cluster,
2693 delayed_refs->run_delayed_start);
2694 if (ret)
2695 break;
2696
2697 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002698 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002699 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002700 spin_unlock(&delayed_refs->lock);
2701 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002702 atomic_dec(&delayed_refs->procs_running_refs);
Josef Bacikf971fe22013-06-10 11:52:32 -04002703 wake_up(&delayed_refs->wait);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002704 return ret;
2705 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002706
Chris Masonbb721702013-01-29 18:44:12 -05002707 atomic_add(ret, &delayed_refs->ref_seq);
2708
Chris Masonc3e69d52009-03-13 10:17:05 -04002709 count -= min_t(unsigned long, ret, count);
2710
2711 if (count == 0)
2712 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002713
Arne Jansen1fa11e22012-08-06 14:18:51 -06002714 if (delayed_start >= delayed_refs->run_delayed_start) {
2715 if (loops == 0) {
2716 /*
2717 * btrfs_find_ref_cluster looped. let's do one
2718 * more cycle. if we don't run any delayed ref
2719 * during that cycle (because we can't because
2720 * all of them are blocked), bail out.
2721 */
2722 loops = 1;
2723 } else {
2724 /*
2725 * no runnable refs left, stop trying
2726 */
2727 BUG_ON(run_all);
2728 break;
2729 }
2730 }
2731 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002732 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002733 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002734 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002735 }
2736
Chris Mason56bec292009-03-13 10:10:06 -04002737 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002738 if (!list_empty(&trans->new_bgs)) {
2739 spin_unlock(&delayed_refs->lock);
2740 btrfs_create_pending_block_groups(trans, root);
2741 spin_lock(&delayed_refs->lock);
2742 }
2743
Chris Mason56bec292009-03-13 10:10:06 -04002744 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002745 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002746 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002747 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002748
2749 while (node) {
2750 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2751 rb_node);
2752 if (btrfs_delayed_ref_is_head(ref)) {
2753 struct btrfs_delayed_ref_head *head;
2754
2755 head = btrfs_delayed_node_to_head(ref);
2756 atomic_inc(&ref->refs);
2757
2758 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002759 /*
2760 * Mutex was contended, block until it's
2761 * released and try again
2762 */
Chris Mason56bec292009-03-13 10:10:06 -04002763 mutex_lock(&head->mutex);
2764 mutex_unlock(&head->mutex);
2765
2766 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002767 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002768 goto again;
2769 }
2770 node = rb_next(node);
2771 }
2772 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002773 schedule_timeout(1);
2774 goto again;
2775 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002776out:
Chris Masonbb721702013-01-29 18:44:12 -05002777 atomic_dec(&delayed_refs->procs_running_refs);
2778 smp_mb();
2779 if (waitqueue_active(&delayed_refs->wait))
2780 wake_up(&delayed_refs->wait);
2781
Chris Masonc3e69d52009-03-13 10:17:05 -04002782 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002783 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002784 return 0;
2785}
2786
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002787int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2788 struct btrfs_root *root,
2789 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002790 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002791{
2792 struct btrfs_delayed_extent_op *extent_op;
2793 int ret;
2794
Miao Xie78a61842012-11-21 02:21:28 +00002795 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002796 if (!extent_op)
2797 return -ENOMEM;
2798
2799 extent_op->flags_to_set = flags;
2800 extent_op->update_flags = 1;
2801 extent_op->update_key = 0;
2802 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002803 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002804
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002805 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2806 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002807 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002808 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002809 return ret;
2810}
2811
2812static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2813 struct btrfs_root *root,
2814 struct btrfs_path *path,
2815 u64 objectid, u64 offset, u64 bytenr)
2816{
2817 struct btrfs_delayed_ref_head *head;
2818 struct btrfs_delayed_ref_node *ref;
2819 struct btrfs_delayed_data_ref *data_ref;
2820 struct btrfs_delayed_ref_root *delayed_refs;
2821 struct rb_node *node;
2822 int ret = 0;
2823
2824 ret = -ENOENT;
2825 delayed_refs = &trans->transaction->delayed_refs;
2826 spin_lock(&delayed_refs->lock);
2827 head = btrfs_find_delayed_ref_head(trans, bytenr);
2828 if (!head)
2829 goto out;
2830
2831 if (!mutex_trylock(&head->mutex)) {
2832 atomic_inc(&head->node.refs);
2833 spin_unlock(&delayed_refs->lock);
2834
David Sterbab3b4aa72011-04-21 01:20:15 +02002835 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002836
David Sterba8cc33e52011-05-02 15:29:25 +02002837 /*
2838 * Mutex was contended, block until it's released and let
2839 * caller try again
2840 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002841 mutex_lock(&head->mutex);
2842 mutex_unlock(&head->mutex);
2843 btrfs_put_delayed_ref(&head->node);
2844 return -EAGAIN;
2845 }
2846
2847 node = rb_prev(&head->node.rb_node);
2848 if (!node)
2849 goto out_unlock;
2850
2851 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2852
2853 if (ref->bytenr != bytenr)
2854 goto out_unlock;
2855
2856 ret = 1;
2857 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2858 goto out_unlock;
2859
2860 data_ref = btrfs_delayed_node_to_data_ref(ref);
2861
2862 node = rb_prev(node);
2863 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002864 int seq = ref->seq;
2865
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002866 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002867 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002868 goto out_unlock;
2869 }
2870
2871 if (data_ref->root != root->root_key.objectid ||
2872 data_ref->objectid != objectid || data_ref->offset != offset)
2873 goto out_unlock;
2874
2875 ret = 0;
2876out_unlock:
2877 mutex_unlock(&head->mutex);
2878out:
2879 spin_unlock(&delayed_refs->lock);
2880 return ret;
2881}
2882
2883static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2884 struct btrfs_root *root,
2885 struct btrfs_path *path,
2886 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002887{
2888 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002889 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002890 struct btrfs_extent_data_ref *ref;
2891 struct btrfs_extent_inline_ref *iref;
2892 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002893 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002894 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002895 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002896
Chris Masonbe20aa92007-12-17 20:14:01 -05002897 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002898 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002899 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002900
Chris Masonbe20aa92007-12-17 20:14:01 -05002901 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2902 if (ret < 0)
2903 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002904 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002905
2906 ret = -ENOENT;
2907 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002908 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002909
Zheng Yan31840ae2008-09-23 13:14:14 -04002910 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002911 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002912 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002913
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002914 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002915 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002916
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002917 ret = 1;
2918 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2919#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2920 if (item_size < sizeof(*ei)) {
2921 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2922 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002923 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002924#endif
2925 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2926
2927 if (item_size != sizeof(*ei) +
2928 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2929 goto out;
2930
2931 if (btrfs_extent_generation(leaf, ei) <=
2932 btrfs_root_last_snapshot(&root->root_item))
2933 goto out;
2934
2935 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2936 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2937 BTRFS_EXTENT_DATA_REF_KEY)
2938 goto out;
2939
2940 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2941 if (btrfs_extent_refs(leaf, ei) !=
2942 btrfs_extent_data_ref_count(leaf, ref) ||
2943 btrfs_extent_data_ref_root(leaf, ref) !=
2944 root->root_key.objectid ||
2945 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2946 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2947 goto out;
2948
Yan Zhengf321e492008-07-30 09:26:11 -04002949 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002950out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002951 return ret;
2952}
2953
2954int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2955 struct btrfs_root *root,
2956 u64 objectid, u64 offset, u64 bytenr)
2957{
2958 struct btrfs_path *path;
2959 int ret;
2960 int ret2;
2961
2962 path = btrfs_alloc_path();
2963 if (!path)
2964 return -ENOENT;
2965
2966 do {
2967 ret = check_committed_ref(trans, root, path, objectid,
2968 offset, bytenr);
2969 if (ret && ret != -ENOENT)
2970 goto out;
2971
2972 ret2 = check_delayed_ref(trans, root, path, objectid,
2973 offset, bytenr);
2974 } while (ret2 == -EAGAIN);
2975
2976 if (ret2 && ret2 != -ENOENT) {
2977 ret = ret2;
2978 goto out;
2979 }
2980
2981 if (ret != -ENOENT || ret2 != -ENOENT)
2982 ret = 0;
2983out:
Yan Zhengf321e492008-07-30 09:26:11 -04002984 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002985 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2986 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002987 return ret;
2988}
2989
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002990static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002991 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002992 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002993 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04002994{
2995 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996 u64 num_bytes;
2997 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04002998 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04002999 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003000 struct btrfs_key key;
3001 struct btrfs_file_extent_item *fi;
3002 int i;
3003 int level;
3004 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003005 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003006 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04003007
3008 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003009 nritems = btrfs_header_nritems(buf);
3010 level = btrfs_header_level(buf);
3011
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003012 if (!root->ref_cows && level == 0)
3013 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003014
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003015 if (inc)
3016 process_func = btrfs_inc_extent_ref;
3017 else
3018 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003019
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003020 if (full_backref)
3021 parent = buf->start;
3022 else
3023 parent = 0;
3024
Zheng Yan31840ae2008-09-23 13:14:14 -04003025 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003026 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003027 btrfs_item_key_to_cpu(buf, &key, i);
3028 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003029 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003030 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003031 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003032 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003033 BTRFS_FILE_EXTENT_INLINE)
3034 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003035 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3036 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003037 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003038
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003039 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3040 key.offset -= btrfs_file_extent_offset(buf, fi);
3041 ret = process_func(trans, root, bytenr, num_bytes,
3042 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003043 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003044 if (ret)
3045 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003046 } else {
3047 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003048 num_bytes = btrfs_level_size(root, level - 1);
3049 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003050 parent, ref_root, level - 1, 0,
3051 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003052 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003053 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003054 }
3055 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003056 return 0;
3057fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003058 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003059}
3060
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003061int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003062 struct extent_buffer *buf, int full_backref, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003063{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003064 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003065}
Zheng Yan31840ae2008-09-23 13:14:14 -04003066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003067int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003068 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003069{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003070 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003071}
3072
Chris Mason9078a3e2007-04-26 16:46:15 -04003073static int write_one_cache_group(struct btrfs_trans_handle *trans,
3074 struct btrfs_root *root,
3075 struct btrfs_path *path,
3076 struct btrfs_block_group_cache *cache)
3077{
3078 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003079 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003080 unsigned long bi;
3081 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003082
Chris Mason9078a3e2007-04-26 16:46:15 -04003083 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003084 if (ret < 0)
3085 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003086 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003087
3088 leaf = path->nodes[0];
3089 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3090 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3091 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003092 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003093fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003094 if (ret) {
3095 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003096 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003097 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003098 return 0;
3099
3100}
3101
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003102static struct btrfs_block_group_cache *
3103next_block_group(struct btrfs_root *root,
3104 struct btrfs_block_group_cache *cache)
3105{
3106 struct rb_node *node;
3107 spin_lock(&root->fs_info->block_group_cache_lock);
3108 node = rb_next(&cache->cache_node);
3109 btrfs_put_block_group(cache);
3110 if (node) {
3111 cache = rb_entry(node, struct btrfs_block_group_cache,
3112 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003113 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003114 } else
3115 cache = NULL;
3116 spin_unlock(&root->fs_info->block_group_cache_lock);
3117 return cache;
3118}
3119
Josef Bacik0af3d002010-06-21 14:48:16 -04003120static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3121 struct btrfs_trans_handle *trans,
3122 struct btrfs_path *path)
3123{
3124 struct btrfs_root *root = block_group->fs_info->tree_root;
3125 struct inode *inode = NULL;
3126 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003127 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003128 int num_pages = 0;
3129 int retries = 0;
3130 int ret = 0;
3131
3132 /*
3133 * If this block group is smaller than 100 megs don't bother caching the
3134 * block group.
3135 */
3136 if (block_group->key.offset < (100 * 1024 * 1024)) {
3137 spin_lock(&block_group->lock);
3138 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3139 spin_unlock(&block_group->lock);
3140 return 0;
3141 }
3142
3143again:
3144 inode = lookup_free_space_inode(root, block_group, path);
3145 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3146 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003147 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003148 goto out;
3149 }
3150
3151 if (IS_ERR(inode)) {
3152 BUG_ON(retries);
3153 retries++;
3154
3155 if (block_group->ro)
3156 goto out_free;
3157
3158 ret = create_free_space_inode(root, trans, block_group, path);
3159 if (ret)
3160 goto out_free;
3161 goto again;
3162 }
3163
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003164 /* We've already setup this transaction, go ahead and exit */
3165 if (block_group->cache_generation == trans->transid &&
3166 i_size_read(inode)) {
3167 dcs = BTRFS_DC_SETUP;
3168 goto out_put;
3169 }
3170
Josef Bacik0af3d002010-06-21 14:48:16 -04003171 /*
3172 * We want to set the generation to 0, that way if anything goes wrong
3173 * from here on out we know not to trust this cache when we load up next
3174 * time.
3175 */
3176 BTRFS_I(inode)->generation = 0;
3177 ret = btrfs_update_inode(trans, root, inode);
3178 WARN_ON(ret);
3179
3180 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003181 ret = btrfs_check_trunc_cache_free_space(root,
3182 &root->fs_info->global_block_rsv);
3183 if (ret)
3184 goto out_put;
3185
Josef Bacik0af3d002010-06-21 14:48:16 -04003186 ret = btrfs_truncate_free_space_cache(root, trans, path,
3187 inode);
3188 if (ret)
3189 goto out_put;
3190 }
3191
3192 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003193 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3194 !btrfs_test_opt(root, SPACE_CACHE)) {
3195 /*
3196 * don't bother trying to write stuff out _if_
3197 * a) we're not cached,
3198 * b) we're with nospace_cache mount option.
3199 */
Josef Bacik2b209822010-12-03 13:17:53 -05003200 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003201 spin_unlock(&block_group->lock);
3202 goto out_put;
3203 }
3204 spin_unlock(&block_group->lock);
3205
Josef Bacik6fc823b2012-08-06 13:46:38 -06003206 /*
3207 * Try to preallocate enough space based on how big the block group is.
3208 * Keep in mind this has to include any pinned space which could end up
3209 * taking up quite a bit since it's not folded into the other space
3210 * cache.
3211 */
3212 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003213 if (!num_pages)
3214 num_pages = 1;
3215
Josef Bacik0af3d002010-06-21 14:48:16 -04003216 num_pages *= 16;
3217 num_pages *= PAGE_CACHE_SIZE;
3218
3219 ret = btrfs_check_data_free_space(inode, num_pages);
3220 if (ret)
3221 goto out_put;
3222
3223 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3224 num_pages, num_pages,
3225 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003226 if (!ret)
3227 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003228 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003229
Josef Bacik0af3d002010-06-21 14:48:16 -04003230out_put:
3231 iput(inode);
3232out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003233 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003234out:
3235 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003236 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003237 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003238 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003239 spin_unlock(&block_group->lock);
3240
3241 return ret;
3242}
3243
Chris Mason96b51792007-10-15 16:15:19 -04003244int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3245 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003246{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003247 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003248 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003249 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003250 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003251
3252 path = btrfs_alloc_path();
3253 if (!path)
3254 return -ENOMEM;
3255
Josef Bacik0af3d002010-06-21 14:48:16 -04003256again:
3257 while (1) {
3258 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3259 while (cache) {
3260 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3261 break;
3262 cache = next_block_group(root, cache);
3263 }
3264 if (!cache) {
3265 if (last == 0)
3266 break;
3267 last = 0;
3268 continue;
3269 }
3270 err = cache_save_setup(cache, trans, path);
3271 last = cache->key.objectid + cache->key.offset;
3272 btrfs_put_block_group(cache);
3273 }
3274
Chris Masond3977122009-01-05 21:25:51 -05003275 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003276 if (last == 0) {
3277 err = btrfs_run_delayed_refs(trans, root,
3278 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003279 if (err) /* File system offline */
3280 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003281 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003282
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003283 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3284 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003285 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3286 btrfs_put_block_group(cache);
3287 goto again;
3288 }
3289
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003290 if (cache->dirty)
3291 break;
3292 cache = next_block_group(root, cache);
3293 }
3294 if (!cache) {
3295 if (last == 0)
3296 break;
3297 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003298 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003299 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003300
Josef Bacik0cb59c92010-07-02 12:14:14 -04003301 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3302 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003303 cache->dirty = 0;
3304 last = cache->key.objectid + cache->key.offset;
3305
3306 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003307 if (err) /* File system offline */
3308 goto out;
3309
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003310 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003311 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003312
Josef Bacik0cb59c92010-07-02 12:14:14 -04003313 while (1) {
3314 /*
3315 * I don't think this is needed since we're just marking our
3316 * preallocated extent as written, but just in case it can't
3317 * hurt.
3318 */
3319 if (last == 0) {
3320 err = btrfs_run_delayed_refs(trans, root,
3321 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003322 if (err) /* File system offline */
3323 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003324 }
3325
3326 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3327 while (cache) {
3328 /*
3329 * Really this shouldn't happen, but it could if we
3330 * couldn't write the entire preallocated extent and
3331 * splitting the extent resulted in a new block.
3332 */
3333 if (cache->dirty) {
3334 btrfs_put_block_group(cache);
3335 goto again;
3336 }
3337 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3338 break;
3339 cache = next_block_group(root, cache);
3340 }
3341 if (!cache) {
3342 if (last == 0)
3343 break;
3344 last = 0;
3345 continue;
3346 }
3347
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003348 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003349
3350 /*
3351 * If we didn't have an error then the cache state is still
3352 * NEED_WRITE, so we can set it to WRITTEN.
3353 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003354 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003355 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3356 last = cache->key.objectid + cache->key.offset;
3357 btrfs_put_block_group(cache);
3358 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003359out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003360
Chris Mason9078a3e2007-04-26 16:46:15 -04003361 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003362 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003363}
3364
Yan Zhengd2fb3432008-12-11 16:30:39 -05003365int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3366{
3367 struct btrfs_block_group_cache *block_group;
3368 int readonly = 0;
3369
3370 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3371 if (!block_group || block_group->ro)
3372 readonly = 1;
3373 if (block_group)
Chris Masonfa9c0d72009-04-03 09:47:43 -04003374 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003375 return readonly;
3376}
3377
Chris Mason593060d2008-03-25 16:50:33 -04003378static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3379 u64 total_bytes, u64 bytes_used,
3380 struct btrfs_space_info **space_info)
3381{
3382 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003383 int i;
3384 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003385 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003386
3387 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3388 BTRFS_BLOCK_GROUP_RAID10))
3389 factor = 2;
3390 else
3391 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003392
3393 found = __find_space_info(info, flags);
3394 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003395 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003396 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003397 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003398 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003399 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003400 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003401 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003402 *space_info = found;
3403 return 0;
3404 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003405 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003406 if (!found)
3407 return -ENOMEM;
3408
Josef Bacikb150a4f2013-06-19 15:00:04 -04003409 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3410 if (ret) {
3411 kfree(found);
3412 return ret;
3413 }
3414
Yan, Zhengb742bb82010-05-16 10:46:24 -04003415 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3416 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003417 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003418 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003419 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003420 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003421 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003422 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003423 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003424 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003425 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003426 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003427 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003428 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003429 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003430 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003431 found->flush = 0;
3432 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003433 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003434 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003435 if (flags & BTRFS_BLOCK_GROUP_DATA)
3436 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003437 return 0;
3438}
3439
Chris Mason8790d502008-04-03 16:29:03 -04003440static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3441{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003442 u64 extra_flags = chunk_to_extended(flags) &
3443 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003444
Miao Xiede98ced2013-01-29 10:13:12 +00003445 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003446 if (flags & BTRFS_BLOCK_GROUP_DATA)
3447 fs_info->avail_data_alloc_bits |= extra_flags;
3448 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3449 fs_info->avail_metadata_alloc_bits |= extra_flags;
3450 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3451 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003452 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003453}
Chris Mason593060d2008-03-25 16:50:33 -04003454
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003455/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003456 * returns target flags in extended format or 0 if restripe for this
3457 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003458 *
3459 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003460 */
3461static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3462{
3463 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3464 u64 target = 0;
3465
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003466 if (!bctl)
3467 return 0;
3468
3469 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3470 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3471 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3472 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3473 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3474 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3475 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3476 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3477 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3478 }
3479
3480 return target;
3481}
3482
3483/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003484 * @flags: available profiles in extended format (see ctree.h)
3485 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003486 * Returns reduced profile in chunk format. If profile changing is in
3487 * progress (either running or paused) picks the target profile (if it's
3488 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003489 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003490static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003491{
Chris Masoncd02dca2010-12-13 14:56:23 -05003492 /*
3493 * we add in the count of missing devices because we want
3494 * to make sure that any RAID levels on a degraded FS
3495 * continue to be honored.
3496 */
3497 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3498 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003499 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003500 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003501
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003502 /*
3503 * see if restripe for this chunk_type is in progress, if so
3504 * try to reduce to the target profile
3505 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003506 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003507 target = get_restripe_target(root->fs_info, flags);
3508 if (target) {
3509 /* pick target profile only if it's already available */
3510 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003511 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003512 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003513 }
3514 }
3515 spin_unlock(&root->fs_info->balance_lock);
3516
David Woodhouse53b381b2013-01-29 18:40:14 -05003517 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003518 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003519 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3520 BTRFS_BLOCK_GROUP_RAID5);
3521 if (num_devices < 3)
3522 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003523 if (num_devices < 4)
3524 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3525
David Woodhouse53b381b2013-01-29 18:40:14 -05003526 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3527 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3528 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3529 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003530
David Woodhouse53b381b2013-01-29 18:40:14 -05003531 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3532 tmp = BTRFS_BLOCK_GROUP_RAID6;
3533 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3534 tmp = BTRFS_BLOCK_GROUP_RAID5;
3535 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3536 tmp = BTRFS_BLOCK_GROUP_RAID10;
3537 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3538 tmp = BTRFS_BLOCK_GROUP_RAID1;
3539 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3540 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003541
David Woodhouse53b381b2013-01-29 18:40:14 -05003542 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003543}
3544
Yan, Zhengb742bb82010-05-16 10:46:24 -04003545static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003546{
Miao Xiede98ced2013-01-29 10:13:12 +00003547 unsigned seq;
3548
3549 do {
3550 seq = read_seqbegin(&root->fs_info->profiles_lock);
3551
3552 if (flags & BTRFS_BLOCK_GROUP_DATA)
3553 flags |= root->fs_info->avail_data_alloc_bits;
3554 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3555 flags |= root->fs_info->avail_system_alloc_bits;
3556 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3557 flags |= root->fs_info->avail_metadata_alloc_bits;
3558 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003559
Yan, Zhengb742bb82010-05-16 10:46:24 -04003560 return btrfs_reduce_alloc_profile(root, flags);
3561}
Josef Bacik6a632092009-02-20 11:00:09 -05003562
Miao Xie6d07bce2011-01-05 10:07:31 +00003563u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003564{
3565 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003566 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003567
Yan, Zhengb742bb82010-05-16 10:46:24 -04003568 if (data)
3569 flags = BTRFS_BLOCK_GROUP_DATA;
3570 else if (root == root->fs_info->chunk_root)
3571 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3572 else
3573 flags = BTRFS_BLOCK_GROUP_METADATA;
3574
David Woodhouse53b381b2013-01-29 18:40:14 -05003575 ret = get_alloc_profile(root, flags);
3576 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003577}
3578
Josef Bacik6a632092009-02-20 11:00:09 -05003579/*
3580 * This will check the space that the inode allocates from to make sure we have
3581 * enough space for bytes.
3582 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003583int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003584{
3585 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003586 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003587 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003588 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003589 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003590
3591 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003592 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003593
Li Zefan82d59022011-04-20 10:33:24 +08003594 if (root == root->fs_info->tree_root ||
3595 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003596 alloc_chunk = 0;
3597 committed = 1;
3598 }
3599
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003600 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003601 if (!data_sinfo)
3602 goto alloc;
3603
Josef Bacik6a632092009-02-20 11:00:09 -05003604again:
3605 /* make sure we have enough space to handle the data first */
3606 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003607 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3608 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3609 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003610
3611 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003612 struct btrfs_trans_handle *trans;
3613
Josef Bacik6a632092009-02-20 11:00:09 -05003614 /*
3615 * if we don't have enough free bytes in this space then we need
3616 * to alloc a new chunk.
3617 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003618 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003619 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003620
Chris Mason0e4f8f82011-04-15 16:05:44 -04003621 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003622 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003623alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003624 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003625 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003626 if (IS_ERR(trans))
3627 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003628
3629 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003630 alloc_target,
3631 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003632 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003633 if (ret < 0) {
3634 if (ret != -ENOSPC)
3635 return ret;
3636 else
3637 goto commit_trans;
3638 }
Chris Mason33b4d472009-09-22 14:45:50 -04003639
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003640 if (!data_sinfo)
3641 data_sinfo = fs_info->data_sinfo;
3642
Josef Bacik6a632092009-02-20 11:00:09 -05003643 goto again;
3644 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003645
3646 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003647 * If we don't have enough pinned space to deal with this
3648 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003649 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003650 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3651 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003652 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003653 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003654
3655 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003656commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003657 if (!committed &&
3658 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003659 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003660
Josef Bacik7a7eaa402011-04-13 12:54:33 -04003661 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662 if (IS_ERR(trans))
3663 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003664 ret = btrfs_commit_transaction(trans, root);
3665 if (ret)
3666 return ret;
3667 goto again;
3668 }
3669
Josef Bacik6a632092009-02-20 11:00:09 -05003670 return -ENOSPC;
3671 }
3672 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003673 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003674 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003675 spin_unlock(&data_sinfo->lock);
3676
Josef Bacik9ed74f22009-09-11 16:12:44 -04003677 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003678}
3679
3680/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003681 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003682 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003683void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003684{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003685 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003686 struct btrfs_space_info *data_sinfo;
3687
3688 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003689 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003690
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003691 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003692 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003693 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003694 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003695 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003696 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003697 spin_unlock(&data_sinfo->lock);
3698}
3699
Josef Bacik97e728d2009-04-21 17:40:57 -04003700static void force_metadata_allocation(struct btrfs_fs_info *info)
3701{
3702 struct list_head *head = &info->space_info;
3703 struct btrfs_space_info *found;
3704
3705 rcu_read_lock();
3706 list_for_each_entry_rcu(found, head, list) {
3707 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003708 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003709 }
3710 rcu_read_unlock();
3711}
3712
Miao Xie3c76cd82013-04-25 10:12:38 +00003713static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3714{
3715 return (global->size << 1);
3716}
3717
Chris Masone5bc2452010-10-26 13:37:56 -04003718static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003719 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003720{
Josef Bacikfb25e912011-07-26 17:00:46 -04003721 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003722 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003723 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003724 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003725
Chris Mason0e4f8f82011-04-15 16:05:44 -04003726 if (force == CHUNK_ALLOC_FORCE)
3727 return 1;
3728
3729 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003730 * We need to take into account the global rsv because for all intents
3731 * and purposes it's used space. Don't worry about locking the
3732 * global_rsv, it doesn't change except when the transaction commits.
3733 */
Josef Bacik54338b52012-08-14 16:20:52 -04003734 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003735 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003736
3737 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003738 * in limited mode, we want to have some free space up to
3739 * about 1% of the FS size.
3740 */
3741 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003742 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003743 thresh = max_t(u64, 64 * 1024 * 1024,
3744 div_factor_fine(thresh, 1));
3745
3746 if (num_bytes - num_allocated < thresh)
3747 return 1;
3748 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003749
Josef Bacik698d0082012-09-12 14:08:47 -04003750 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003751 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003752 return 1;
3753}
3754
Liu Bo15d1ff82012-03-29 09:57:44 -04003755static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3756{
3757 u64 num_dev;
3758
David Woodhouse53b381b2013-01-29 18:40:14 -05003759 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3760 BTRFS_BLOCK_GROUP_RAID0 |
3761 BTRFS_BLOCK_GROUP_RAID5 |
3762 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003763 num_dev = root->fs_info->fs_devices->rw_devices;
3764 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3765 num_dev = 2;
3766 else
3767 num_dev = 1; /* DUP or single */
3768
3769 /* metadata for updaing devices and chunk tree */
3770 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3771}
3772
3773static void check_system_chunk(struct btrfs_trans_handle *trans,
3774 struct btrfs_root *root, u64 type)
3775{
3776 struct btrfs_space_info *info;
3777 u64 left;
3778 u64 thresh;
3779
3780 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3781 spin_lock(&info->lock);
3782 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3783 info->bytes_reserved - info->bytes_readonly;
3784 spin_unlock(&info->lock);
3785
3786 thresh = get_system_chunk_thresh(root, type);
3787 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003788 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3789 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003790 dump_space_info(info, 0, 0);
3791 }
3792
3793 if (left < thresh) {
3794 u64 flags;
3795
3796 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3797 btrfs_alloc_chunk(trans, root, flags);
3798 }
3799}
3800
Chris Mason6324fbf2008-03-24 15:01:59 -04003801static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003802 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003803{
3804 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003805 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003806 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003807 int ret = 0;
3808
Josef Bacikc6b305a2012-12-18 09:16:16 -05003809 /* Don't re-enter if we're already allocating a chunk */
3810 if (trans->allocating_chunk)
3811 return -ENOSPC;
3812
Chris Mason6324fbf2008-03-24 15:01:59 -04003813 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003814 if (!space_info) {
3815 ret = update_space_info(extent_root->fs_info, flags,
3816 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003818 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003819 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003820
Josef Bacik6d741192011-04-11 20:20:11 -04003821again:
Josef Bacik25179202008-10-29 14:49:05 -04003822 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003823 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003824 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003825 if (space_info->full) {
3826 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003827 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003828 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003829
Josef Bacik698d0082012-09-12 14:08:47 -04003830 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003831 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003832 return 0;
3833 } else if (space_info->chunk_alloc) {
3834 wait_for_alloc = 1;
3835 } else {
3836 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003837 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003838
Josef Bacik25179202008-10-29 14:49:05 -04003839 spin_unlock(&space_info->lock);
3840
Josef Bacik6d741192011-04-11 20:20:11 -04003841 mutex_lock(&fs_info->chunk_mutex);
3842
3843 /*
3844 * The chunk_mutex is held throughout the entirety of a chunk
3845 * allocation, so once we've acquired the chunk_mutex we know that the
3846 * other guy is done and we need to recheck and see if we should
3847 * allocate.
3848 */
3849 if (wait_for_alloc) {
3850 mutex_unlock(&fs_info->chunk_mutex);
3851 wait_for_alloc = 0;
3852 goto again;
3853 }
3854
Josef Bacikc6b305a2012-12-18 09:16:16 -05003855 trans->allocating_chunk = true;
3856
Josef Bacik97e728d2009-04-21 17:40:57 -04003857 /*
Josef Bacik67377732010-09-16 16:19:09 -04003858 * If we have mixed data/metadata chunks we want to make sure we keep
3859 * allocating mixed chunks instead of individual chunks.
3860 */
3861 if (btrfs_mixed_space_info(space_info))
3862 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3863
3864 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003865 * if we're doing a data chunk, go ahead and make sure that
3866 * we keep a reasonable number of metadata chunks allocated in the
3867 * FS as well.
3868 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003869 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003870 fs_info->data_chunk_allocations++;
3871 if (!(fs_info->data_chunk_allocations %
3872 fs_info->metadata_ratio))
3873 force_metadata_allocation(fs_info);
3874 }
3875
Liu Bo15d1ff82012-03-29 09:57:44 -04003876 /*
3877 * Check if we have enough space in SYSTEM chunk because we may need
3878 * to update devices.
3879 */
3880 check_system_chunk(trans, extent_root, flags);
3881
Yan Zheng2b820322008-11-17 21:11:30 -05003882 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003883 trans->allocating_chunk = false;
Mark Fasheh92b8e892011-07-12 10:57:59 -07003884
Josef Bacik9ed74f22009-09-11 16:12:44 -04003885 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003886 if (ret < 0 && ret != -ENOSPC)
3887 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003888 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003889 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003890 else
3891 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003892
Chris Mason0e4f8f82011-04-15 16:05:44 -04003893 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003894out:
Josef Bacik6d741192011-04-11 20:20:11 -04003895 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003896 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003897 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003898 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003899}
3900
Josef Bacika80c8dc2012-09-06 16:59:33 -04003901static int can_overcommit(struct btrfs_root *root,
3902 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003903 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003904{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003905 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003906 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003907 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003908 u64 avail;
3909 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003910 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003911
3912 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003913 space_info->bytes_pinned + space_info->bytes_readonly;
3914
Josef Bacik96f1bb52013-01-30 17:02:51 -05003915 /*
3916 * We only want to allow over committing if we have lots of actual space
3917 * free, but if we don't have enough space to handle the global reserve
3918 * space then we could end up having a real enospc problem when trying
3919 * to allocate a chunk or some other such important allocation.
3920 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003921 spin_lock(&global_rsv->lock);
3922 space_size = calc_global_rsv_need_space(global_rsv);
3923 spin_unlock(&global_rsv->lock);
3924 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003925 return 0;
3926
3927 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003928
3929 spin_lock(&root->fs_info->free_chunk_lock);
3930 avail = root->fs_info->free_chunk_space;
3931 spin_unlock(&root->fs_info->free_chunk_lock);
3932
3933 /*
3934 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003935 * space is actually useable. For raid56, the space info used
3936 * doesn't include the parity drive, so we don't have to
3937 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003938 */
3939 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3940 BTRFS_BLOCK_GROUP_RAID1 |
3941 BTRFS_BLOCK_GROUP_RAID10))
3942 avail >>= 1;
3943
Josef Bacik70afa392013-02-06 13:53:19 -05003944 to_add = space_info->total_bytes;
3945
Josef Bacika80c8dc2012-09-06 16:59:33 -04003946 /*
Miao Xie561c2942012-10-16 11:32:18 +00003947 * If we aren't flushing all things, let us overcommit up to
3948 * 1/2th of the space. If we can flush, don't let us overcommit
3949 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003950 */
Miao Xie08e007d2012-10-16 11:33:38 +00003951 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003952 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003953 else
Josef Bacik70afa392013-02-06 13:53:19 -05003954 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003955
Josef Bacik70afa392013-02-06 13:53:19 -05003956 /*
3957 * Limit the overcommit to the amount of free space we could possibly
3958 * allocate for chunks.
3959 */
3960 to_add = min(avail, to_add);
3961
3962 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003963 return 1;
3964 return 0;
3965}
3966
Eric Sandeen48a3b632013-04-25 20:41:01 +00003967static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3968 unsigned long nr_pages)
Miao Xieda633a42012-12-20 11:19:09 +00003969{
3970 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00003971
Josef Bacik925a6ef2013-06-20 12:31:27 -04003972 if (down_read_trylock(&sb->s_umount)) {
3973 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
3974 up_read(&sb->s_umount);
3975 } else {
Miao Xieda633a42012-12-20 11:19:09 +00003976 /*
3977 * We needn't worry the filesystem going from r/w to r/o though
3978 * we don't acquire ->s_umount mutex, because the filesystem
3979 * should guarantee the delalloc inodes list be empty after
3980 * the filesystem is readonly(all dirty pages are written to
3981 * the disk).
3982 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00003983 btrfs_start_all_delalloc_inodes(root->fs_info, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003984 if (!current->journal_info)
Miao Xie199c2a92013-05-15 07:48:23 +00003985 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003986 }
3987}
3988
Yan, Zheng5da9d012010-05-16 10:46:25 -04003989/*
3990 * shrink metadata reservation for delalloc
3991 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003992static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3993 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04003994{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003995 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04003996 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04003997 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04003998 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04003999 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004000 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04004001 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004002 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004003 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004004
Josef Bacik663350a2011-11-03 22:54:25 -04004005 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004006 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004007 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004008
4009 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004010 delalloc_bytes = percpu_counter_sum_positive(
4011 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004012 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004013 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004014 return;
Miao Xie199c2a92013-05-15 07:48:23 +00004015 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004016 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004017 }
4018
Josef Bacikf4c738c2012-07-02 17:10:51 -04004019 while (delalloc_bytes && loops < 3) {
4020 max_reclaim = min(delalloc_bytes, to_reclaim);
4021 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00004022 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04004023 /*
4024 * We need to wait for the async pages to actually start before
4025 * we do anything.
4026 */
4027 wait_event(root->fs_info->async_submit_wait,
4028 !atomic_read(&root->fs_info->async_delalloc_pages));
4029
Miao Xie08e007d2012-10-16 11:33:38 +00004030 if (!trans)
4031 flush = BTRFS_RESERVE_FLUSH_ALL;
4032 else
4033 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004034 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004035 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004036 spin_unlock(&space_info->lock);
4037 break;
4038 }
Josef Bacik0019f102010-10-15 15:18:40 -04004039 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004040
Chris Mason36e39c42011-03-12 07:08:42 -05004041 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004042 if (wait_ordered && !trans) {
Miao Xie199c2a92013-05-15 07:48:23 +00004043 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04004044 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004045 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004046 if (time_left)
4047 break;
4048 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04004049 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004050 delalloc_bytes = percpu_counter_sum_positive(
4051 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004052 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004053}
4054
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004055/**
Josef Bacik663350a2011-11-03 22:54:25 -04004056 * maybe_commit_transaction - possibly commit the transaction if its ok to
4057 * @root - the root we're allocating for
4058 * @bytes - the number of bytes we want to reserve
4059 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004060 *
Josef Bacik663350a2011-11-03 22:54:25 -04004061 * This will check to make sure that committing the transaction will actually
4062 * get us somewhere and then commit the transaction if it does. Otherwise it
4063 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004064 */
Josef Bacik663350a2011-11-03 22:54:25 -04004065static int may_commit_transaction(struct btrfs_root *root,
4066 struct btrfs_space_info *space_info,
4067 u64 bytes, int force)
4068{
4069 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4070 struct btrfs_trans_handle *trans;
4071
4072 trans = (struct btrfs_trans_handle *)current->journal_info;
4073 if (trans)
4074 return -EAGAIN;
4075
4076 if (force)
4077 goto commit;
4078
4079 /* See if there is enough pinned space to make this reservation */
4080 spin_lock(&space_info->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004081 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4082 bytes) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004083 spin_unlock(&space_info->lock);
4084 goto commit;
4085 }
4086 spin_unlock(&space_info->lock);
4087
4088 /*
4089 * See if there is some space in the delayed insertion reservation for
4090 * this reservation.
4091 */
4092 if (space_info != delayed_rsv->space_info)
4093 return -ENOSPC;
4094
Liu Bod9b02182012-02-16 18:34:39 +08004095 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004096 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004097 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4098 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004099 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004100 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004101 return -ENOSPC;
4102 }
4103 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004104 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004105
4106commit:
4107 trans = btrfs_join_transaction(root);
4108 if (IS_ERR(trans))
4109 return -ENOSPC;
4110
4111 return btrfs_commit_transaction(trans, root);
4112}
4113
Josef Bacik96c3f432012-06-21 14:05:49 -04004114enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004115 FLUSH_DELAYED_ITEMS_NR = 1,
4116 FLUSH_DELAYED_ITEMS = 2,
4117 FLUSH_DELALLOC = 3,
4118 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004119 ALLOC_CHUNK = 5,
4120 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004121};
4122
4123static int flush_space(struct btrfs_root *root,
4124 struct btrfs_space_info *space_info, u64 num_bytes,
4125 u64 orig_bytes, int state)
4126{
4127 struct btrfs_trans_handle *trans;
4128 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004129 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004130
4131 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004132 case FLUSH_DELAYED_ITEMS_NR:
4133 case FLUSH_DELAYED_ITEMS:
4134 if (state == FLUSH_DELAYED_ITEMS_NR) {
4135 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4136
4137 nr = (int)div64_u64(num_bytes, bytes);
4138 if (!nr)
4139 nr = 1;
4140 nr *= 2;
4141 } else {
4142 nr = -1;
4143 }
4144 trans = btrfs_join_transaction(root);
4145 if (IS_ERR(trans)) {
4146 ret = PTR_ERR(trans);
4147 break;
4148 }
4149 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4150 btrfs_end_transaction(trans, root);
4151 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004152 case FLUSH_DELALLOC:
4153 case FLUSH_DELALLOC_WAIT:
4154 shrink_delalloc(root, num_bytes, orig_bytes,
4155 state == FLUSH_DELALLOC_WAIT);
4156 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004157 case ALLOC_CHUNK:
4158 trans = btrfs_join_transaction(root);
4159 if (IS_ERR(trans)) {
4160 ret = PTR_ERR(trans);
4161 break;
4162 }
4163 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004164 btrfs_get_alloc_profile(root, 0),
4165 CHUNK_ALLOC_NO_FORCE);
4166 btrfs_end_transaction(trans, root);
4167 if (ret == -ENOSPC)
4168 ret = 0;
4169 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004170 case COMMIT_TRANS:
4171 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4172 break;
4173 default:
4174 ret = -ENOSPC;
4175 break;
4176 }
4177
4178 return ret;
4179}
Josef Bacik663350a2011-11-03 22:54:25 -04004180/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004181 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4182 * @root - the root we're allocating for
4183 * @block_rsv - the block_rsv we're allocating for
4184 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004185 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004186 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004187 * This will reserve orgi_bytes number of bytes from the space info associated
4188 * with the block_rsv. If there is not enough space it will make an attempt to
4189 * flush out space to make room. It will do this by flushing delalloc if
4190 * possible or committing the transaction. If flush is 0 then no attempts to
4191 * regain reservations will be made and this will fail if there is not enough
4192 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004193 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004194static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004195 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004196 u64 orig_bytes,
4197 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004198{
4199 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004200 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004201 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004202 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004203 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004204 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004205
4206again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004207 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004208 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004209 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004210 * We only want to wait if somebody other than us is flushing and we
4211 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004212 */
Miao Xie08e007d2012-10-16 11:33:38 +00004213 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4214 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004215 spin_unlock(&space_info->lock);
4216 /*
4217 * If we have a trans handle we can't wait because the flusher
4218 * may have to commit the transaction, which would mean we would
4219 * deadlock since we are waiting for the flusher to finish, but
4220 * hold the current transaction open.
4221 */
Josef Bacik663350a2011-11-03 22:54:25 -04004222 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004223 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004224 ret = wait_event_killable(space_info->wait, !space_info->flush);
4225 /* Must have been killed, return */
4226 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004227 return -EINTR;
4228
4229 spin_lock(&space_info->lock);
4230 }
4231
4232 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004233 used = space_info->bytes_used + space_info->bytes_reserved +
4234 space_info->bytes_pinned + space_info->bytes_readonly +
4235 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004236
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004237 /*
4238 * The idea here is that we've not already over-reserved the block group
4239 * then we can go ahead and save our reservation first and then start
4240 * flushing if we need to. Otherwise if we've already overcommitted
4241 * lets start flushing stuff first and then come back and try to make
4242 * our reservation.
4243 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004244 if (used <= space_info->total_bytes) {
4245 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004246 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004247 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004248 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004249 ret = 0;
4250 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004251 /*
4252 * Ok set num_bytes to orig_bytes since we aren't
4253 * overocmmitted, this way we only try and reclaim what
4254 * we need.
4255 */
4256 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004257 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004258 } else {
4259 /*
4260 * Ok we're over committed, set num_bytes to the overcommitted
4261 * amount plus the amount of bytes that we need for this
4262 * reservation.
4263 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004264 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004265 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004266 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004267
Josef Bacik44734ed2012-09-28 16:04:19 -04004268 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4269 space_info->bytes_may_use += orig_bytes;
4270 trace_btrfs_space_reservation(root->fs_info, "space_info",
4271 space_info->flags, orig_bytes,
4272 1);
4273 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004274 }
4275
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004276 /*
4277 * Couldn't make our reservation, save our place so while we're trying
4278 * to reclaim space we can actually use it instead of somebody else
4279 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004280 *
4281 * We make the other tasks wait for the flush only when we can flush
4282 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004283 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004284 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004285 flushing = true;
4286 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004287 }
4288
Yan, Zhengf0486c62010-05-16 10:46:25 -04004289 spin_unlock(&space_info->lock);
4290
Miao Xie08e007d2012-10-16 11:33:38 +00004291 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004292 goto out;
4293
Josef Bacik96c3f432012-06-21 14:05:49 -04004294 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4295 flush_state);
4296 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004297
4298 /*
4299 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4300 * would happen. So skip delalloc flush.
4301 */
4302 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4303 (flush_state == FLUSH_DELALLOC ||
4304 flush_state == FLUSH_DELALLOC_WAIT))
4305 flush_state = ALLOC_CHUNK;
4306
Josef Bacik96c3f432012-06-21 14:05:49 -04004307 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004308 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004309 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4310 flush_state < COMMIT_TRANS)
4311 goto again;
4312 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4313 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004314 goto again;
4315
4316out:
Josef Bacik5d803662013-02-07 16:06:02 -05004317 if (ret == -ENOSPC &&
4318 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4319 struct btrfs_block_rsv *global_rsv =
4320 &root->fs_info->global_block_rsv;
4321
4322 if (block_rsv != global_rsv &&
4323 !block_rsv_use_bytes(global_rsv, orig_bytes))
4324 ret = 0;
4325 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004326 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004327 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004328 space_info->flush = 0;
4329 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004330 spin_unlock(&space_info->lock);
4331 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004332 return ret;
4333}
4334
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004335static struct btrfs_block_rsv *get_block_rsv(
4336 const struct btrfs_trans_handle *trans,
4337 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004338{
Josef Bacik4c13d752011-08-30 11:31:29 -04004339 struct btrfs_block_rsv *block_rsv = NULL;
4340
Josef Bacik0e721102012-06-26 16:13:18 -04004341 if (root->ref_cows)
4342 block_rsv = trans->block_rsv;
4343
4344 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004345 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004346
4347 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004348 block_rsv = root->block_rsv;
4349
4350 if (!block_rsv)
4351 block_rsv = &root->fs_info->empty_block_rsv;
4352
4353 return block_rsv;
4354}
4355
4356static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4357 u64 num_bytes)
4358{
4359 int ret = -ENOSPC;
4360 spin_lock(&block_rsv->lock);
4361 if (block_rsv->reserved >= num_bytes) {
4362 block_rsv->reserved -= num_bytes;
4363 if (block_rsv->reserved < block_rsv->size)
4364 block_rsv->full = 0;
4365 ret = 0;
4366 }
4367 spin_unlock(&block_rsv->lock);
4368 return ret;
4369}
4370
4371static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4372 u64 num_bytes, int update_size)
4373{
4374 spin_lock(&block_rsv->lock);
4375 block_rsv->reserved += num_bytes;
4376 if (update_size)
4377 block_rsv->size += num_bytes;
4378 else if (block_rsv->reserved >= block_rsv->size)
4379 block_rsv->full = 1;
4380 spin_unlock(&block_rsv->lock);
4381}
4382
Josef Bacikd52be812013-05-29 14:54:47 -04004383int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4384 struct btrfs_block_rsv *dest, u64 num_bytes,
4385 int min_factor)
4386{
4387 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4388 u64 min_bytes;
4389
4390 if (global_rsv->space_info != dest->space_info)
4391 return -ENOSPC;
4392
4393 spin_lock(&global_rsv->lock);
4394 min_bytes = div_factor(global_rsv->size, min_factor);
4395 if (global_rsv->reserved < min_bytes + num_bytes) {
4396 spin_unlock(&global_rsv->lock);
4397 return -ENOSPC;
4398 }
4399 global_rsv->reserved -= num_bytes;
4400 if (global_rsv->reserved < global_rsv->size)
4401 global_rsv->full = 0;
4402 spin_unlock(&global_rsv->lock);
4403
4404 block_rsv_add_bytes(dest, num_bytes, 1);
4405 return 0;
4406}
4407
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004408static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4409 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004410 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004411{
4412 struct btrfs_space_info *space_info = block_rsv->space_info;
4413
4414 spin_lock(&block_rsv->lock);
4415 if (num_bytes == (u64)-1)
4416 num_bytes = block_rsv->size;
4417 block_rsv->size -= num_bytes;
4418 if (block_rsv->reserved >= block_rsv->size) {
4419 num_bytes = block_rsv->reserved - block_rsv->size;
4420 block_rsv->reserved = block_rsv->size;
4421 block_rsv->full = 1;
4422 } else {
4423 num_bytes = 0;
4424 }
4425 spin_unlock(&block_rsv->lock);
4426
4427 if (num_bytes > 0) {
4428 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004429 spin_lock(&dest->lock);
4430 if (!dest->full) {
4431 u64 bytes_to_add;
4432
4433 bytes_to_add = dest->size - dest->reserved;
4434 bytes_to_add = min(num_bytes, bytes_to_add);
4435 dest->reserved += bytes_to_add;
4436 if (dest->reserved >= dest->size)
4437 dest->full = 1;
4438 num_bytes -= bytes_to_add;
4439 }
4440 spin_unlock(&dest->lock);
4441 }
4442 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004443 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004444 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004445 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004446 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004447 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004448 spin_unlock(&space_info->lock);
4449 }
4450 }
4451}
4452
4453static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4454 struct btrfs_block_rsv *dst, u64 num_bytes)
4455{
4456 int ret;
4457
4458 ret = block_rsv_use_bytes(src, num_bytes);
4459 if (ret)
4460 return ret;
4461
4462 block_rsv_add_bytes(dst, num_bytes, 1);
4463 return 0;
4464}
4465
Miao Xie66d8f3d2012-09-06 04:02:28 -06004466void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004467{
4468 memset(rsv, 0, sizeof(*rsv));
4469 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004470 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004471}
4472
Miao Xie66d8f3d2012-09-06 04:02:28 -06004473struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4474 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004475{
4476 struct btrfs_block_rsv *block_rsv;
4477 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004478
4479 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4480 if (!block_rsv)
4481 return NULL;
4482
Miao Xie66d8f3d2012-09-06 04:02:28 -06004483 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004484 block_rsv->space_info = __find_space_info(fs_info,
4485 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004486 return block_rsv;
4487}
4488
4489void btrfs_free_block_rsv(struct btrfs_root *root,
4490 struct btrfs_block_rsv *rsv)
4491{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004492 if (!rsv)
4493 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004494 btrfs_block_rsv_release(root, rsv, (u64)-1);
4495 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004496}
4497
Miao Xie08e007d2012-10-16 11:33:38 +00004498int btrfs_block_rsv_add(struct btrfs_root *root,
4499 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4500 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004501{
4502 int ret;
4503
4504 if (num_bytes == 0)
4505 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004506
Miao Xie61b520a2011-11-10 20:45:05 -05004507 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004508 if (!ret) {
4509 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4510 return 0;
4511 }
4512
Yan, Zhengf0486c62010-05-16 10:46:25 -04004513 return ret;
4514}
4515
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004516int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004517 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004518{
4519 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004520 int ret = -ENOSPC;
4521
4522 if (!block_rsv)
4523 return 0;
4524
4525 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004526 num_bytes = div_factor(block_rsv->size, min_factor);
4527 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004528 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004529 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004530
Josef Bacik36ba0222011-10-18 12:15:48 -04004531 return ret;
4532}
4533
Miao Xie08e007d2012-10-16 11:33:38 +00004534int btrfs_block_rsv_refill(struct btrfs_root *root,
4535 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4536 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004537{
4538 u64 num_bytes = 0;
4539 int ret = -ENOSPC;
4540
4541 if (!block_rsv)
4542 return 0;
4543
4544 spin_lock(&block_rsv->lock);
4545 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004546 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004547 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004548 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004549 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004550 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004551
Yan, Zhengf0486c62010-05-16 10:46:25 -04004552 if (!ret)
4553 return 0;
4554
Miao Xieaa38a712011-11-18 17:43:00 +08004555 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004556 if (!ret) {
4557 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004558 return 0;
4559 }
4560
Josef Bacik13553e52011-08-08 13:33:21 -04004561 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004562}
4563
4564int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4565 struct btrfs_block_rsv *dst_rsv,
4566 u64 num_bytes)
4567{
4568 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4569}
4570
4571void btrfs_block_rsv_release(struct btrfs_root *root,
4572 struct btrfs_block_rsv *block_rsv,
4573 u64 num_bytes)
4574{
4575 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4576 if (global_rsv->full || global_rsv == block_rsv ||
4577 block_rsv->space_info != global_rsv->space_info)
4578 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004579 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4580 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004581}
4582
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004583/*
4584 * helper to calculate size of global block reservation.
4585 * the desired value is sum of space used by extent tree,
4586 * checksum tree and root tree
4587 */
4588static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4589{
4590 struct btrfs_space_info *sinfo;
4591 u64 num_bytes;
4592 u64 meta_used;
4593 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004594 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004595
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004596 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4597 spin_lock(&sinfo->lock);
4598 data_used = sinfo->bytes_used;
4599 spin_unlock(&sinfo->lock);
4600
4601 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4602 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004603 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4604 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004605 meta_used = sinfo->bytes_used;
4606 spin_unlock(&sinfo->lock);
4607
4608 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4609 csum_size * 2;
4610 num_bytes += div64_u64(data_used + meta_used, 50);
4611
4612 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004613 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004614
4615 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4616}
4617
4618static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4619{
4620 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4621 struct btrfs_space_info *sinfo = block_rsv->space_info;
4622 u64 num_bytes;
4623
4624 num_bytes = calc_global_metadata_size(fs_info);
4625
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004626 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004627 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004628
Josef Bacikfdf30d12013-03-26 15:31:45 -04004629 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004630
4631 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004632 sinfo->bytes_reserved + sinfo->bytes_readonly +
4633 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004634
4635 if (sinfo->total_bytes > num_bytes) {
4636 num_bytes = sinfo->total_bytes - num_bytes;
4637 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004638 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004639 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004640 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004641 }
4642
4643 if (block_rsv->reserved >= block_rsv->size) {
4644 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004645 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004646 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004647 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004648 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004649 block_rsv->reserved = block_rsv->size;
4650 block_rsv->full = 1;
4651 }
David Sterba182608c2011-05-05 13:13:16 +02004652
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004653 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004654 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004655}
4656
Yan, Zhengf0486c62010-05-16 10:46:25 -04004657static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4658{
4659 struct btrfs_space_info *space_info;
4660
4661 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4662 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004663
4664 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004665 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004666 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004667 fs_info->trans_block_rsv.space_info = space_info;
4668 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004669 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004670
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004671 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4672 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4673 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4674 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004675 if (fs_info->quota_root)
4676 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004677 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004678
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004679 update_global_block_rsv(fs_info);
4680}
4681
4682static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4683{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004684 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4685 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004686 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4687 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4688 WARN_ON(fs_info->trans_block_rsv.size > 0);
4689 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4690 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4691 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004692 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4693 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004694}
4695
Yan, Zhenga22285a2010-05-16 10:48:46 -04004696void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4697 struct btrfs_root *root)
4698{
Josef Bacik0e721102012-06-26 16:13:18 -04004699 if (!trans->block_rsv)
4700 return;
4701
Yan, Zhenga22285a2010-05-16 10:48:46 -04004702 if (!trans->bytes_reserved)
4703 return;
4704
Chris Masone77266e2012-02-24 10:39:05 -05004705 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004706 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004707 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004708 trans->bytes_reserved = 0;
4709}
4710
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004711/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004712int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4713 struct inode *inode)
4714{
4715 struct btrfs_root *root = BTRFS_I(inode)->root;
4716 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4717 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4718
4719 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004720 * We need to hold space in order to delete our orphan item once we've
4721 * added it, so this takes the reservation so we can release it later
4722 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004723 */
Chris Masonff5714c2011-05-28 07:00:39 -04004724 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004725 trace_btrfs_space_reservation(root->fs_info, "orphan",
4726 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004727 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4728}
4729
4730void btrfs_orphan_release_metadata(struct inode *inode)
4731{
4732 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004733 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004734 trace_btrfs_space_reservation(root->fs_info, "orphan",
4735 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004736 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4737}
4738
Miao Xied5c12072013-02-28 10:04:33 +00004739/*
4740 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4741 * root: the root of the parent directory
4742 * rsv: block reservation
4743 * items: the number of items that we need do reservation
4744 * qgroup_reserved: used to return the reserved size in qgroup
4745 *
4746 * This function is used to reserve the space for snapshot/subvolume
4747 * creation and deletion. Those operations are different with the
4748 * common file/directory operations, they change two fs/file trees
4749 * and root tree, the number of items that the qgroup reserves is
4750 * different with the free space reservation. So we can not use
4751 * the space reseravtion mechanism in start_transaction().
4752 */
4753int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4754 struct btrfs_block_rsv *rsv,
4755 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004756 u64 *qgroup_reserved,
4757 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004758{
Miao Xied5c12072013-02-28 10:04:33 +00004759 u64 num_bytes;
4760 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004761 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004762
4763 if (root->fs_info->quota_enabled) {
4764 /* One for parent inode, two for dir entries */
4765 num_bytes = 3 * root->leafsize;
4766 ret = btrfs_qgroup_reserve(root, num_bytes);
4767 if (ret)
4768 return ret;
4769 } else {
4770 num_bytes = 0;
4771 }
4772
4773 *qgroup_reserved = num_bytes;
4774
4775 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4776 rsv->space_info = __find_space_info(root->fs_info,
4777 BTRFS_BLOCK_GROUP_METADATA);
4778 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4779 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004780
4781 if (ret == -ENOSPC && use_global_rsv)
4782 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
4783
Miao Xied5c12072013-02-28 10:04:33 +00004784 if (ret) {
4785 if (*qgroup_reserved)
4786 btrfs_qgroup_free(root, *qgroup_reserved);
4787 }
4788
4789 return ret;
4790}
4791
4792void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4793 struct btrfs_block_rsv *rsv,
4794 u64 qgroup_reserved)
4795{
4796 btrfs_block_rsv_release(root, rsv, (u64)-1);
4797 if (qgroup_reserved)
4798 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004799}
4800
Josef Bacik7709cde2011-08-04 10:25:02 -04004801/**
4802 * drop_outstanding_extent - drop an outstanding extent
4803 * @inode: the inode we're dropping the extent for
4804 *
4805 * This is called when we are freeing up an outstanding extent, either called
4806 * after an error or after an extent is written. This will return the number of
4807 * reserved extents that need to be freed. This must be called with
4808 * BTRFS_I(inode)->lock held.
4809 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004810static unsigned drop_outstanding_extent(struct inode *inode)
4811{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004812 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004813 unsigned dropped_extents = 0;
4814
Josef Bacik9e0baf62011-07-15 15:16:44 +00004815 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4816 BTRFS_I(inode)->outstanding_extents--;
4817
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004818 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004819 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4820 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004821 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004822
Josef Bacik9e0baf62011-07-15 15:16:44 +00004823 /*
4824 * If we have more or the same amount of outsanding extents than we have
4825 * reserved then we need to leave the reserved extents count alone.
4826 */
4827 if (BTRFS_I(inode)->outstanding_extents >=
4828 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004829 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004830
4831 dropped_extents = BTRFS_I(inode)->reserved_extents -
4832 BTRFS_I(inode)->outstanding_extents;
4833 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004834 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004835}
4836
Josef Bacik7709cde2011-08-04 10:25:02 -04004837/**
4838 * calc_csum_metadata_size - return the amount of metada space that must be
4839 * reserved/free'd for the given bytes.
4840 * @inode: the inode we're manipulating
4841 * @num_bytes: the number of bytes in question
4842 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4843 *
4844 * This adjusts the number of csum_bytes in the inode and then returns the
4845 * correct amount of metadata that must either be reserved or freed. We
4846 * calculate how many checksums we can fit into one leaf and then divide the
4847 * number of bytes that will need to be checksumed by this value to figure out
4848 * how many checksums will be required. If we are adding bytes then the number
4849 * may go up and we will return the number of additional bytes that must be
4850 * reserved. If it is going down we will return the number of bytes that must
4851 * be freed.
4852 *
4853 * This must be called with BTRFS_I(inode)->lock held.
4854 */
4855static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4856 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004857{
Josef Bacik7709cde2011-08-04 10:25:02 -04004858 struct btrfs_root *root = BTRFS_I(inode)->root;
4859 u64 csum_size;
4860 int num_csums_per_leaf;
4861 int num_csums;
4862 int old_csums;
4863
4864 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4865 BTRFS_I(inode)->csum_bytes == 0)
4866 return 0;
4867
4868 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4869 if (reserve)
4870 BTRFS_I(inode)->csum_bytes += num_bytes;
4871 else
4872 BTRFS_I(inode)->csum_bytes -= num_bytes;
4873 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4874 num_csums_per_leaf = (int)div64_u64(csum_size,
4875 sizeof(struct btrfs_csum_item) +
4876 sizeof(struct btrfs_disk_key));
4877 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4878 num_csums = num_csums + num_csums_per_leaf - 1;
4879 num_csums = num_csums / num_csums_per_leaf;
4880
4881 old_csums = old_csums + num_csums_per_leaf - 1;
4882 old_csums = old_csums / num_csums_per_leaf;
4883
4884 /* No change, no need to reserve more */
4885 if (old_csums == num_csums)
4886 return 0;
4887
4888 if (reserve)
4889 return btrfs_calc_trans_metadata_size(root,
4890 num_csums - old_csums);
4891
4892 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004893}
4894
4895int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4896{
4897 struct btrfs_root *root = BTRFS_I(inode)->root;
4898 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004899 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004900 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004901 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004902 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004903 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004904 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004905 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004906 u64 to_free = 0;
4907 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004908
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004909 /* If we are a free space inode we need to not flush since we will be in
4910 * the middle of a transaction commit. We also don't need the delalloc
4911 * mutex since we won't race with anybody. We need this mostly to make
4912 * lockdep shut its filthy mouth.
4913 */
4914 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004915 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004916 delalloc_lock = false;
4917 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004918
Miao Xie08e007d2012-10-16 11:33:38 +00004919 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4920 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004921 schedule_timeout(1);
4922
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004923 if (delalloc_lock)
4924 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4925
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004926 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004927
Josef Bacik9e0baf62011-07-15 15:16:44 +00004928 spin_lock(&BTRFS_I(inode)->lock);
4929 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004930
Josef Bacik9e0baf62011-07-15 15:16:44 +00004931 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004932 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004933 nr_extents = BTRFS_I(inode)->outstanding_extents -
4934 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004935
4936 /*
4937 * Add an item to reserve for updating the inode when we complete the
4938 * delalloc io.
4939 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004940 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4941 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004942 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004943 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004944 }
4945
4946 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004947 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004948 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004949 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004950
Wang Shilong88e081bf2013-03-01 11:36:01 +00004951 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004952 ret = btrfs_qgroup_reserve(root, num_bytes +
4953 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004954 if (ret)
4955 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004956 }
Arne Jansenc5567232011-09-14 15:44:05 +02004957
Wang Shilong88e081bf2013-03-01 11:36:01 +00004958 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4959 if (unlikely(ret)) {
4960 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004961 btrfs_qgroup_free(root, num_bytes +
4962 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004963 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004964 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004965
Josef Bacik660d3f62011-12-09 11:18:51 -05004966 spin_lock(&BTRFS_I(inode)->lock);
4967 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004968 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4969 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004970 nr_extents--;
4971 }
4972 BTRFS_I(inode)->reserved_extents += nr_extents;
4973 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004974
4975 if (delalloc_lock)
4976 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004977
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004978 if (to_reserve)
4979 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4980 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004981 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4982
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004983 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004984
4985out_fail:
4986 spin_lock(&BTRFS_I(inode)->lock);
4987 dropped = drop_outstanding_extent(inode);
4988 /*
4989 * If the inodes csum_bytes is the same as the original
4990 * csum_bytes then we know we haven't raced with any free()ers
4991 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004992 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004993 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00004994 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04004995 } else {
4996 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
4997 u64 bytes;
4998
4999 /*
5000 * This is tricky, but first we need to figure out how much we
5001 * free'd from any free-ers that occured during this
5002 * reservation, so we reset ->csum_bytes to the csum_bytes
5003 * before we dropped our lock, and then call the free for the
5004 * number of bytes that were freed while we were trying our
5005 * reservation.
5006 */
5007 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5008 BTRFS_I(inode)->csum_bytes = csum_bytes;
5009 to_free = calc_csum_metadata_size(inode, bytes, 0);
5010
5011
5012 /*
5013 * Now we need to see how much we would have freed had we not
5014 * been making this reservation and our ->csum_bytes were not
5015 * artificially inflated.
5016 */
5017 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5018 bytes = csum_bytes - orig_csum_bytes;
5019 bytes = calc_csum_metadata_size(inode, bytes, 0);
5020
5021 /*
5022 * Now reset ->csum_bytes to what it should be. If bytes is
5023 * more than to_free then we would have free'd more space had we
5024 * not had an artificially high ->csum_bytes, so we need to free
5025 * the remainder. If bytes is the same or less then we don't
5026 * need to do anything, the other free-ers did the correct
5027 * thing.
5028 */
5029 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5030 if (bytes > to_free)
5031 to_free = bytes - to_free;
5032 else
5033 to_free = 0;
5034 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005035 spin_unlock(&BTRFS_I(inode)->lock);
5036 if (dropped)
5037 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5038
5039 if (to_free) {
5040 btrfs_block_rsv_release(root, block_rsv, to_free);
5041 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5042 btrfs_ino(inode), to_free, 0);
5043 }
5044 if (delalloc_lock)
5045 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5046 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005047}
5048
Josef Bacik7709cde2011-08-04 10:25:02 -04005049/**
5050 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5051 * @inode: the inode to release the reservation for
5052 * @num_bytes: the number of bytes we're releasing
5053 *
5054 * This will release the metadata reservation for an inode. This can be called
5055 * once we complete IO for a given set of bytes to release their metadata
5056 * reservations.
5057 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005058void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5059{
5060 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005061 u64 to_free = 0;
5062 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005063
5064 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005065 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005066 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005067
Miao Xie09348562013-02-07 10:12:07 +00005068 if (num_bytes)
5069 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005070 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005071 if (dropped > 0)
5072 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005073
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005074 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5075 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005076 if (root->fs_info->quota_enabled) {
5077 btrfs_qgroup_free(root, num_bytes +
5078 dropped * root->leafsize);
5079 }
5080
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005081 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5082 to_free);
5083}
5084
Josef Bacik7709cde2011-08-04 10:25:02 -04005085/**
5086 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5087 * @inode: inode we're writing to
5088 * @num_bytes: the number of bytes we want to allocate
5089 *
5090 * This will do the following things
5091 *
5092 * o reserve space in the data space info for num_bytes
5093 * o reserve space in the metadata space info based on number of outstanding
5094 * extents and how much csums will be needed
5095 * o add to the inodes ->delalloc_bytes
5096 * o add it to the fs_info's delalloc inodes list.
5097 *
5098 * This will return 0 for success and -ENOSPC if there is no space left.
5099 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005100int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5101{
5102 int ret;
5103
5104 ret = btrfs_check_data_free_space(inode, num_bytes);
5105 if (ret)
5106 return ret;
5107
5108 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5109 if (ret) {
5110 btrfs_free_reserved_data_space(inode, num_bytes);
5111 return ret;
5112 }
5113
5114 return 0;
5115}
5116
Josef Bacik7709cde2011-08-04 10:25:02 -04005117/**
5118 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5119 * @inode: inode we're releasing space for
5120 * @num_bytes: the number of bytes we want to free up
5121 *
5122 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5123 * called in the case that we don't need the metadata AND data reservations
5124 * anymore. So if there is an error or we insert an inline extent.
5125 *
5126 * This function will release the metadata space that was not used and will
5127 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5128 * list if there are no delalloc bytes left.
5129 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005130void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5131{
5132 btrfs_delalloc_release_metadata(inode, num_bytes);
5133 btrfs_free_reserved_data_space(inode, num_bytes);
5134}
5135
Liu Boc53d6132012-12-27 09:01:19 +00005136static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005137 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005138{
Josef Bacik0af3d002010-06-21 14:48:16 -04005139 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005140 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005141 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005142 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005143 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005144 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005145
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005146 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005147 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005148 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005149 if (alloc)
5150 old_val += num_bytes;
5151 else
5152 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005153 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005154 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005155
Chris Masond3977122009-01-05 21:25:51 -05005156 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005157 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005158 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005159 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005160 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5161 BTRFS_BLOCK_GROUP_RAID1 |
5162 BTRFS_BLOCK_GROUP_RAID10))
5163 factor = 2;
5164 else
5165 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005166 /*
5167 * If this block group has free space cache written out, we
5168 * need to make sure to load it if we are removing space. This
5169 * is because we need the unpinning stage to actually add the
5170 * space back to the block group, otherwise we will leak space.
5171 */
5172 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005173 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005174
Chris Masondb945352007-10-15 16:15:53 -04005175 byte_in_group = bytenr - cache->key.objectid;
5176 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005177
Josef Bacik25179202008-10-29 14:49:05 -04005178 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005179 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005180
Josef Bacik73bc1872011-10-03 14:07:49 -04005181 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005182 cache->disk_cache_state < BTRFS_DC_CLEAR)
5183 cache->disk_cache_state = BTRFS_DC_CLEAR;
5184
Josef Bacik0f9dd462008-09-23 13:14:11 -04005185 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005186 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005187 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005188 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005189 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005190 btrfs_set_block_group_used(&cache->item, old_val);
5191 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005192 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005193 cache->space_info->bytes_used += num_bytes;
5194 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005195 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005196 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005197 } else {
Chris Masondb945352007-10-15 16:15:53 -04005198 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005199 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005200 cache->pinned += num_bytes;
5201 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005202 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005203 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005204 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005205 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005206
Yan, Zhengf0486c62010-05-16 10:46:25 -04005207 set_extent_dirty(info->pinned_extents,
5208 bytenr, bytenr + num_bytes - 1,
5209 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005210 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04005211 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005212 total -= num_bytes;
5213 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005214 }
5215 return 0;
5216}
Chris Mason6324fbf2008-03-24 15:01:59 -04005217
Chris Masona061fc82008-05-07 11:43:44 -04005218static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5219{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005220 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005221 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005222
Liu Boa1897fd2012-12-27 09:01:23 +00005223 spin_lock(&root->fs_info->block_group_cache_lock);
5224 bytenr = root->fs_info->first_logical_byte;
5225 spin_unlock(&root->fs_info->block_group_cache_lock);
5226
5227 if (bytenr < (u64)-1)
5228 return bytenr;
5229
Josef Bacik0f9dd462008-09-23 13:14:11 -04005230 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5231 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005232 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005233
Yan Zhengd2fb3432008-12-11 16:30:39 -05005234 bytenr = cache->key.objectid;
Chris Masonfa9c0d72009-04-03 09:47:43 -04005235 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005236
5237 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005238}
5239
Yan, Zhengf0486c62010-05-16 10:46:25 -04005240static int pin_down_extent(struct btrfs_root *root,
5241 struct btrfs_block_group_cache *cache,
5242 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005243{
Yan Zheng11833d62009-09-11 16:11:19 -04005244 spin_lock(&cache->space_info->lock);
5245 spin_lock(&cache->lock);
5246 cache->pinned += num_bytes;
5247 cache->space_info->bytes_pinned += num_bytes;
5248 if (reserved) {
5249 cache->reserved -= num_bytes;
5250 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005251 }
Yan Zheng11833d62009-09-11 16:11:19 -04005252 spin_unlock(&cache->lock);
5253 spin_unlock(&cache->space_info->lock);
5254
Yan, Zhengf0486c62010-05-16 10:46:25 -04005255 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5256 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005257 return 0;
5258}
Chris Mason9078a3e2007-04-26 16:46:15 -04005259
Yan, Zhengf0486c62010-05-16 10:46:25 -04005260/*
5261 * this function must be called within transaction
5262 */
5263int btrfs_pin_extent(struct btrfs_root *root,
5264 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005265{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005266 struct btrfs_block_group_cache *cache;
5267
5268 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005269 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005270
5271 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5272
5273 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005274 return 0;
5275}
Zheng Yane8569812008-09-26 10:05:48 -04005276
Yan, Zhengf0486c62010-05-16 10:46:25 -04005277/*
Chris Masone688b722011-10-31 20:52:39 -04005278 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005279 */
Liu Bodcfac412012-12-27 09:01:20 +00005280int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b722011-10-31 20:52:39 -04005281 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005282{
Chris Masone688b722011-10-31 20:52:39 -04005283 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005284 int ret;
Chris Masone688b722011-10-31 20:52:39 -04005285
5286 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005287 if (!cache)
5288 return -EINVAL;
Chris Masone688b722011-10-31 20:52:39 -04005289
5290 /*
5291 * pull in the free space cache (if any) so that our pin
5292 * removes the free space from the cache. We have load_only set
5293 * to one because the slow code to read in the free extents does check
5294 * the pinned extents.
5295 */
Liu Bof6373bf2012-12-27 09:01:18 +00005296 cache_block_group(cache, 1);
Chris Masone688b722011-10-31 20:52:39 -04005297
5298 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5299
5300 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005301 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b722011-10-31 20:52:39 -04005302 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005303 return ret;
Chris Masone688b722011-10-31 20:52:39 -04005304}
5305
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005306static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5307{
5308 int ret;
5309 struct btrfs_block_group_cache *block_group;
5310 struct btrfs_caching_control *caching_ctl;
5311
5312 block_group = btrfs_lookup_block_group(root->fs_info, start);
5313 if (!block_group)
5314 return -EINVAL;
5315
5316 cache_block_group(block_group, 0);
5317 caching_ctl = get_caching_control(block_group);
5318
5319 if (!caching_ctl) {
5320 /* Logic error */
5321 BUG_ON(!block_group_cache_done(block_group));
5322 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5323 } else {
5324 mutex_lock(&caching_ctl->mutex);
5325
5326 if (start >= caching_ctl->progress) {
5327 ret = add_excluded_extent(root, start, num_bytes);
5328 } else if (start + num_bytes <= caching_ctl->progress) {
5329 ret = btrfs_remove_free_space(block_group,
5330 start, num_bytes);
5331 } else {
5332 num_bytes = caching_ctl->progress - start;
5333 ret = btrfs_remove_free_space(block_group,
5334 start, num_bytes);
5335 if (ret)
5336 goto out_lock;
5337
5338 num_bytes = (start + num_bytes) -
5339 caching_ctl->progress;
5340 start = caching_ctl->progress;
5341 ret = add_excluded_extent(root, start, num_bytes);
5342 }
5343out_lock:
5344 mutex_unlock(&caching_ctl->mutex);
5345 put_caching_control(caching_ctl);
5346 }
5347 btrfs_put_block_group(block_group);
5348 return ret;
5349}
5350
5351int btrfs_exclude_logged_extents(struct btrfs_root *log,
5352 struct extent_buffer *eb)
5353{
5354 struct btrfs_file_extent_item *item;
5355 struct btrfs_key key;
5356 int found_type;
5357 int i;
5358
5359 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5360 return 0;
5361
5362 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5363 btrfs_item_key_to_cpu(eb, &key, i);
5364 if (key.type != BTRFS_EXTENT_DATA_KEY)
5365 continue;
5366 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5367 found_type = btrfs_file_extent_type(eb, item);
5368 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5369 continue;
5370 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5371 continue;
5372 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5373 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5374 __exclude_logged_extent(log, key.objectid, key.offset);
5375 }
5376
5377 return 0;
5378}
5379
Josef Bacikfb25e912011-07-26 17:00:46 -04005380/**
5381 * btrfs_update_reserved_bytes - update the block_group and space info counters
5382 * @cache: The cache we are manipulating
5383 * @num_bytes: The number of bytes in question
5384 * @reserve: One of the reservation enums
5385 *
5386 * This is called by the allocator when it reserves space, or by somebody who is
5387 * freeing space that was never actually used on disk. For example if you
5388 * reserve some space for a new leaf in transaction A and before transaction A
5389 * commits you free that leaf, you call this with reserve set to 0 in order to
5390 * clear the reservation.
5391 *
5392 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5393 * ENOSPC accounting. For data we handle the reservation through clearing the
5394 * delalloc bits in the io_tree. We have to do this since we could end up
5395 * allocating less disk space for the amount of data we have reserved in the
5396 * case of compression.
5397 *
5398 * If this is a reservation and the block group has become read only we cannot
5399 * make the reservation and return -EAGAIN, otherwise this function always
5400 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005401 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005402static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5403 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005404{
Josef Bacikfb25e912011-07-26 17:00:46 -04005405 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005406 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005407
Josef Bacikfb25e912011-07-26 17:00:46 -04005408 spin_lock(&space_info->lock);
5409 spin_lock(&cache->lock);
5410 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005411 if (cache->ro) {
5412 ret = -EAGAIN;
5413 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005414 cache->reserved += num_bytes;
5415 space_info->bytes_reserved += num_bytes;
5416 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005417 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005418 "space_info", space_info->flags,
5419 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005420 space_info->bytes_may_use -= num_bytes;
5421 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005422 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005423 } else {
5424 if (cache->ro)
5425 space_info->bytes_readonly += num_bytes;
5426 cache->reserved -= num_bytes;
5427 space_info->bytes_reserved -= num_bytes;
5428 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005429 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005430 spin_unlock(&cache->lock);
5431 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005432 return ret;
5433}
5434
Jeff Mahoney143bede2012-03-01 14:56:26 +01005435void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005436 struct btrfs_root *root)
5437{
5438 struct btrfs_fs_info *fs_info = root->fs_info;
5439 struct btrfs_caching_control *next;
5440 struct btrfs_caching_control *caching_ctl;
5441 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005442 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005443
5444 down_write(&fs_info->extent_commit_sem);
5445
5446 list_for_each_entry_safe(caching_ctl, next,
5447 &fs_info->caching_block_groups, list) {
5448 cache = caching_ctl->block_group;
5449 if (block_group_cache_done(cache)) {
5450 cache->last_byte_to_unpin = (u64)-1;
5451 list_del_init(&caching_ctl->list);
5452 put_caching_control(caching_ctl);
5453 } else {
5454 cache->last_byte_to_unpin = caching_ctl->progress;
5455 }
5456 }
5457
5458 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5459 fs_info->pinned_extents = &fs_info->freed_extents[1];
5460 else
5461 fs_info->pinned_extents = &fs_info->freed_extents[0];
5462
5463 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005464
Josef Bacikb150a4f2013-06-19 15:00:04 -04005465 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5466 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5467
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005468 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005469}
5470
5471static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5472{
5473 struct btrfs_fs_info *fs_info = root->fs_info;
5474 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005475 struct btrfs_space_info *space_info;
5476 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005477 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005478 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005479
5480 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005481 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005482 if (!cache ||
5483 start >= cache->key.objectid + cache->key.offset) {
5484 if (cache)
5485 btrfs_put_block_group(cache);
5486 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005487 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005488 }
5489
5490 len = cache->key.objectid + cache->key.offset - start;
5491 len = min(len, end + 1 - start);
5492
5493 if (start < cache->last_byte_to_unpin) {
5494 len = min(len, cache->last_byte_to_unpin - start);
5495 btrfs_add_free_space(cache, start, len);
5496 }
Josef Bacik25179202008-10-29 14:49:05 -04005497
Yan, Zhengf0486c62010-05-16 10:46:25 -04005498 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005499 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005500
Josef Bacik7b398f82012-10-22 15:52:28 -04005501 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005502 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005503 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005504 space_info->bytes_pinned -= len;
5505 if (cache->ro) {
5506 space_info->bytes_readonly += len;
5507 readonly = true;
5508 }
Josef Bacik25179202008-10-29 14:49:05 -04005509 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005510 if (!readonly && global_rsv->space_info == space_info) {
5511 spin_lock(&global_rsv->lock);
5512 if (!global_rsv->full) {
5513 len = min(len, global_rsv->size -
5514 global_rsv->reserved);
5515 global_rsv->reserved += len;
5516 space_info->bytes_may_use += len;
5517 if (global_rsv->reserved >= global_rsv->size)
5518 global_rsv->full = 1;
5519 }
5520 spin_unlock(&global_rsv->lock);
5521 }
5522 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005523 }
5524
5525 if (cache)
Chris Masonfa9c0d72009-04-03 09:47:43 -04005526 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005527 return 0;
5528}
5529
5530int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005531 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005532{
Yan Zheng11833d62009-09-11 16:11:19 -04005533 struct btrfs_fs_info *fs_info = root->fs_info;
5534 struct extent_io_tree *unpin;
Chris Mason1a5bc162007-10-15 16:15:26 -04005535 u64 start;
5536 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005537 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005538
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005539 if (trans->aborted)
5540 return 0;
5541
Yan Zheng11833d62009-09-11 16:11:19 -04005542 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5543 unpin = &fs_info->freed_extents[1];
5544 else
5545 unpin = &fs_info->freed_extents[0];
5546
Chris Masond3977122009-01-05 21:25:51 -05005547 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04005548 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005549 EXTENT_DIRTY, NULL);
Chris Mason1a5bc162007-10-15 16:15:26 -04005550 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005551 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005552
Li Dongyang5378e602011-03-24 10:24:27 +00005553 if (btrfs_test_opt(root, DISCARD))
5554 ret = btrfs_discard_extent(root, start,
5555 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005556
Chris Mason1a5bc162007-10-15 16:15:26 -04005557 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005558 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005559 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005560 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005561
Chris Masone20d96d2007-03-22 12:13:20 -04005562 return 0;
5563}
5564
Josef Bacikb150a4f2013-06-19 15:00:04 -04005565static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5566 u64 owner, u64 root_objectid)
5567{
5568 struct btrfs_space_info *space_info;
5569 u64 flags;
5570
5571 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5572 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5573 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5574 else
5575 flags = BTRFS_BLOCK_GROUP_METADATA;
5576 } else {
5577 flags = BTRFS_BLOCK_GROUP_DATA;
5578 }
5579
5580 space_info = __find_space_info(fs_info, flags);
5581 BUG_ON(!space_info); /* Logic bug */
5582 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5583}
5584
5585
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005586static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5587 struct btrfs_root *root,
5588 u64 bytenr, u64 num_bytes, u64 parent,
5589 u64 root_objectid, u64 owner_objectid,
5590 u64 owner_offset, int refs_to_drop,
5591 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005592{
Chris Masone2fa7222007-03-12 16:22:34 -04005593 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005594 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005595 struct btrfs_fs_info *info = root->fs_info;
5596 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005597 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005598 struct btrfs_extent_item *ei;
5599 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005600 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005601 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005602 int extent_slot = 0;
5603 int found_extent = 0;
5604 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005605 u32 item_size;
5606 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005607 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5608 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005609
Chris Mason5caf2a02007-04-02 11:20:42 -04005610 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005611 if (!path)
5612 return -ENOMEM;
5613
Chris Mason3c12ac72008-04-21 12:01:38 -04005614 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005615 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005616
5617 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5618 BUG_ON(!is_data && refs_to_drop != 1);
5619
Josef Bacik3173a182013-03-07 14:22:04 -05005620 if (is_data)
5621 skinny_metadata = 0;
5622
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005623 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5624 bytenr, num_bytes, parent,
5625 root_objectid, owner_objectid,
5626 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005627 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005628 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629 while (extent_slot >= 0) {
5630 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005631 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005632 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005633 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005634 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5635 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005636 found_extent = 1;
5637 break;
5638 }
Josef Bacik3173a182013-03-07 14:22:04 -05005639 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5640 key.offset == owner_objectid) {
5641 found_extent = 1;
5642 break;
5643 }
Chris Mason952fcca2008-02-18 16:33:44 -05005644 if (path->slots[0] - extent_slot > 5)
5645 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005646 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005647 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005648#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5649 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5650 if (found_extent && item_size < sizeof(*ei))
5651 found_extent = 0;
5652#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005653 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005655 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005656 NULL, refs_to_drop,
5657 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005658 if (ret) {
5659 btrfs_abort_transaction(trans, extent_root, ret);
5660 goto out;
5661 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005662 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005663 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005664
5665 key.objectid = bytenr;
5666 key.type = BTRFS_EXTENT_ITEM_KEY;
5667 key.offset = num_bytes;
5668
Josef Bacik3173a182013-03-07 14:22:04 -05005669 if (!is_data && skinny_metadata) {
5670 key.type = BTRFS_METADATA_ITEM_KEY;
5671 key.offset = owner_objectid;
5672 }
5673
Zheng Yan31840ae2008-09-23 13:14:14 -04005674 ret = btrfs_search_slot(trans, extent_root,
5675 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005676 if (ret > 0 && skinny_metadata && path->slots[0]) {
5677 /*
5678 * Couldn't find our skinny metadata item,
5679 * see if we have ye olde extent item.
5680 */
5681 path->slots[0]--;
5682 btrfs_item_key_to_cpu(path->nodes[0], &key,
5683 path->slots[0]);
5684 if (key.objectid == bytenr &&
5685 key.type == BTRFS_EXTENT_ITEM_KEY &&
5686 key.offset == num_bytes)
5687 ret = 0;
5688 }
5689
5690 if (ret > 0 && skinny_metadata) {
5691 skinny_metadata = false;
5692 key.type = BTRFS_EXTENT_ITEM_KEY;
5693 key.offset = num_bytes;
5694 btrfs_release_path(path);
5695 ret = btrfs_search_slot(trans, extent_root,
5696 &key, path, -1, 1);
5697 }
5698
Josef Bacikf3465ca2008-11-12 14:19:50 -05005699 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005700 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5701 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005702 if (ret > 0)
5703 btrfs_print_leaf(extent_root,
5704 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005705 }
David Sterba005d6422012-09-18 07:52:32 -06005706 if (ret < 0) {
5707 btrfs_abort_transaction(trans, extent_root, ret);
5708 goto out;
5709 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005710 extent_slot = path->slots[0];
5711 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005712 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005713 btrfs_print_leaf(extent_root, path->nodes[0]);
5714 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005715 btrfs_err(info,
5716 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5717 (unsigned long long)bytenr,
5718 (unsigned long long)parent,
5719 (unsigned long long)root_objectid,
5720 (unsigned long long)owner_objectid,
5721 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005722 } else {
David Sterba005d6422012-09-18 07:52:32 -06005723 btrfs_abort_transaction(trans, extent_root, ret);
5724 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005725 }
Chris Mason5f39d392007-10-15 16:14:19 -04005726
5727 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005728 item_size = btrfs_item_size_nr(leaf, extent_slot);
5729#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5730 if (item_size < sizeof(*ei)) {
5731 BUG_ON(found_extent || extent_slot != path->slots[0]);
5732 ret = convert_extent_item_v0(trans, extent_root, path,
5733 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005734 if (ret < 0) {
5735 btrfs_abort_transaction(trans, extent_root, ret);
5736 goto out;
5737 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005738
David Sterbab3b4aa72011-04-21 01:20:15 +02005739 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005740 path->leave_spinning = 1;
5741
5742 key.objectid = bytenr;
5743 key.type = BTRFS_EXTENT_ITEM_KEY;
5744 key.offset = num_bytes;
5745
5746 ret = btrfs_search_slot(trans, extent_root, &key, path,
5747 -1, 1);
5748 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005749 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5750 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005751 btrfs_print_leaf(extent_root, path->nodes[0]);
5752 }
David Sterba005d6422012-09-18 07:52:32 -06005753 if (ret < 0) {
5754 btrfs_abort_transaction(trans, extent_root, ret);
5755 goto out;
5756 }
5757
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005758 extent_slot = path->slots[0];
5759 leaf = path->nodes[0];
5760 item_size = btrfs_item_size_nr(leaf, extent_slot);
5761 }
5762#endif
5763 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005764 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005765 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005766 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5767 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005768 struct btrfs_tree_block_info *bi;
5769 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5770 bi = (struct btrfs_tree_block_info *)(ei + 1);
5771 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005772 }
5773
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005774 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005775 if (refs < refs_to_drop) {
5776 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5777 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5778 ret = -EINVAL;
5779 btrfs_abort_transaction(trans, extent_root, ret);
5780 goto out;
5781 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005782 refs -= refs_to_drop;
5783
5784 if (refs > 0) {
5785 if (extent_op)
5786 __run_delayed_extent_op(extent_op, leaf, ei);
5787 /*
5788 * In the case of inline back ref, reference count will
5789 * be updated by remove_extent_backref
5790 */
5791 if (iref) {
5792 BUG_ON(!found_extent);
5793 } else {
5794 btrfs_set_extent_refs(leaf, ei, refs);
5795 btrfs_mark_buffer_dirty(leaf);
5796 }
5797 if (found_extent) {
5798 ret = remove_extent_backref(trans, extent_root, path,
5799 iref, refs_to_drop,
5800 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005801 if (ret) {
5802 btrfs_abort_transaction(trans, extent_root, ret);
5803 goto out;
5804 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005805 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005806 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5807 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005808 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005809 if (found_extent) {
5810 BUG_ON(is_data && refs_to_drop !=
5811 extent_data_ref_count(root, path, iref));
5812 if (iref) {
5813 BUG_ON(path->slots[0] != extent_slot);
5814 } else {
5815 BUG_ON(path->slots[0] != extent_slot + 1);
5816 path->slots[0] = extent_slot;
5817 num_to_del = 2;
5818 }
Chris Mason78fae272007-03-25 11:35:08 -04005819 }
Chris Masonb9473432009-03-13 11:00:37 -04005820
Chris Mason952fcca2008-02-18 16:33:44 -05005821 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5822 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005823 if (ret) {
5824 btrfs_abort_transaction(trans, extent_root, ret);
5825 goto out;
5826 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005827 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005828
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005829 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005830 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005831 if (ret) {
5832 btrfs_abort_transaction(trans, extent_root, ret);
5833 goto out;
5834 }
Chris Mason459931e2008-12-10 09:10:46 -05005835 }
5836
Liu Boc53d6132012-12-27 09:01:19 +00005837 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005838 if (ret) {
5839 btrfs_abort_transaction(trans, extent_root, ret);
5840 goto out;
5841 }
Chris Masona28ec192007-03-06 20:08:01 -05005842 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005843out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005844 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005845 return ret;
5846}
5847
5848/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005849 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005850 * delayed ref for that extent as well. This searches the delayed ref tree for
5851 * a given extent, and if there are no other delayed refs to be processed, it
5852 * removes it from the tree.
5853 */
5854static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5855 struct btrfs_root *root, u64 bytenr)
5856{
5857 struct btrfs_delayed_ref_head *head;
5858 struct btrfs_delayed_ref_root *delayed_refs;
5859 struct btrfs_delayed_ref_node *ref;
5860 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005861 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005862
5863 delayed_refs = &trans->transaction->delayed_refs;
5864 spin_lock(&delayed_refs->lock);
5865 head = btrfs_find_delayed_ref_head(trans, bytenr);
5866 if (!head)
5867 goto out;
5868
5869 node = rb_prev(&head->node.rb_node);
5870 if (!node)
5871 goto out;
5872
5873 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5874
5875 /* there are still entries for this ref, we can't drop it */
5876 if (ref->bytenr == bytenr)
5877 goto out;
5878
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005879 if (head->extent_op) {
5880 if (!head->must_insert_reserved)
5881 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005882 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005883 head->extent_op = NULL;
5884 }
5885
Chris Mason1887be62009-03-13 10:11:24 -04005886 /*
5887 * waiting for the lock here would deadlock. If someone else has it
5888 * locked they are already in the process of dropping it anyway
5889 */
5890 if (!mutex_trylock(&head->mutex))
5891 goto out;
5892
5893 /*
5894 * at this point we have a head with no other entries. Go
5895 * ahead and process it.
5896 */
5897 head->node.in_tree = 0;
5898 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005899
Chris Mason1887be62009-03-13 10:11:24 -04005900 delayed_refs->num_entries--;
5901
5902 /*
5903 * we don't take a ref on the node because we're removing it from the
5904 * tree, so we just steal the ref the tree was holding.
5905 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005906 delayed_refs->num_heads--;
5907 if (list_empty(&head->cluster))
5908 delayed_refs->num_heads_ready--;
5909
5910 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005911 spin_unlock(&delayed_refs->lock);
5912
Yan, Zhengf0486c62010-05-16 10:46:25 -04005913 BUG_ON(head->extent_op);
5914 if (head->must_insert_reserved)
5915 ret = 1;
5916
5917 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005918 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005919 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005920out:
5921 spin_unlock(&delayed_refs->lock);
5922 return 0;
5923}
5924
Yan, Zhengf0486c62010-05-16 10:46:25 -04005925void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5926 struct btrfs_root *root,
5927 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005928 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005929{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005930 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005931 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005932 int ret;
5933
5934 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005935 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5936 buf->start, buf->len,
5937 parent, root->root_key.objectid,
5938 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005939 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005940 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005941 }
5942
5943 if (!last_ref)
5944 return;
5945
Yan, Zhengf0486c62010-05-16 10:46:25 -04005946 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005947
5948 if (btrfs_header_generation(buf) == trans->transid) {
5949 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5950 ret = check_ref_cleanup(trans, root, buf->start);
5951 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005952 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005953 }
5954
5955 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5956 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005957 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005958 }
5959
5960 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5961
5962 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005963 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacikb150a4f2013-06-19 15:00:04 -04005964 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005965 }
5966out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04005967 if (pin)
5968 add_pinned_bytes(root->fs_info, buf->len,
5969 btrfs_header_level(buf),
5970 root->root_key.objectid);
5971
Josef Bacika826d6d2011-03-16 13:42:43 -04005972 /*
5973 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5974 * anymore.
5975 */
5976 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005977 btrfs_put_block_group(cache);
5978}
5979
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005980/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005981int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5982 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5983 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005984{
5985 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005986 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005987
Josef Bacikb150a4f2013-06-19 15:00:04 -04005988 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
5989
Chris Mason56bec292009-03-13 10:10:06 -04005990 /*
5991 * tree log blocks never actually go into the extent allocation
5992 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005993 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005994 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
5995 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04005996 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04005997 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04005998 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005999 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006000 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6001 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006002 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006003 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006004 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006005 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6006 num_bytes,
6007 parent, root_objectid, owner,
6008 offset, BTRFS_DROP_DELAYED_REF,
6009 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04006010 }
Chris Mason925baed2008-06-25 16:01:30 -04006011 return ret;
6012}
6013
David Woodhouse53b381b2013-01-29 18:40:14 -05006014static u64 stripe_align(struct btrfs_root *root,
6015 struct btrfs_block_group_cache *cache,
6016 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006017{
Qu Wenruofda28322013-02-26 08:10:22 +00006018 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006019 return ret;
6020}
6021
Chris Masonfec577f2007-02-26 10:40:21 -05006022/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006023 * when we wait for progress in the block group caching, its because
6024 * our allocation attempt failed at least once. So, we must sleep
6025 * and let some progress happen before we try again.
6026 *
6027 * This function will sleep at least once waiting for new free space to
6028 * show up, and then it will check the block group free space numbers
6029 * for our min num_bytes. Another option is to have it go ahead
6030 * and look in the rbtree for a free extent of a given size, but this
6031 * is a good start.
6032 */
6033static noinline int
6034wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6035 u64 num_bytes)
6036{
Yan Zheng11833d62009-09-11 16:11:19 -04006037 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006038
Yan Zheng11833d62009-09-11 16:11:19 -04006039 caching_ctl = get_caching_control(cache);
6040 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04006041 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04006042
Yan Zheng11833d62009-09-11 16:11:19 -04006043 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb6c2011-03-29 13:46:06 +08006044 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006045
6046 put_caching_control(caching_ctl);
6047 return 0;
6048}
6049
6050static noinline int
6051wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6052{
6053 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04006054
6055 caching_ctl = get_caching_control(cache);
6056 if (!caching_ctl)
6057 return 0;
6058
6059 wait_event(caching_ctl->wait, block_group_cache_done(cache));
6060
6061 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04006062 return 0;
6063}
6064
Liu Bo31e50222012-11-21 14:18:10 +00006065int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006066{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006067 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006068 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006069 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006070 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006071 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006072 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006073 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006074 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006075 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006076 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006077 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006078 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006079
Chris Masone942f882013-02-20 14:06:05 -05006080 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006081}
6082
Ilya Dryomov7738a532012-03-27 17:09:17 +03006083static int get_block_group_index(struct btrfs_block_group_cache *cache)
6084{
Liu Bo31e50222012-11-21 14:18:10 +00006085 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006086}
6087
Josef Bacik817d52f2009-07-13 21:29:25 -04006088enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006089 LOOP_CACHING_NOWAIT = 0,
6090 LOOP_CACHING_WAIT = 1,
6091 LOOP_ALLOC_CHUNK = 2,
6092 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006093};
6094
6095/*
Chris Masonfec577f2007-02-26 10:40:21 -05006096 * walks the btree of allocated extents and find a hole of a given size.
6097 * The key ins is changed to record the hole:
6098 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04006099 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05006100 * ins->offset == number of blocks
6101 * Any available blocks before search_start are skipped.
6102 */
Chris Masond3977122009-01-05 21:25:51 -05006103static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006104 struct btrfs_root *orig_root,
6105 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006106 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006107 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006108{
Josef Bacik80eb2342008-10-29 14:49:05 -04006109 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006110 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006111 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006112 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006113 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006114 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006115 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006116 struct btrfs_space_info *space_info;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006117 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006118 int index = __get_raid_index(flags);
6119 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006120 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04006121 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04006122 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006123 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006124 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006125 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006126
Chris Masondb945352007-10-15 16:15:53 -04006127 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006128 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006129 ins->objectid = 0;
6130 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006131
David Sterbab6919a52013-04-29 13:39:40 +00006132 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006133
David Sterbab6919a52013-04-29 13:39:40 +00006134 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006135 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006136 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006137 return -ENOSPC;
6138 }
Josef Bacik2552d172009-04-03 10:14:19 -04006139
Josef Bacik67377732010-09-16 16:19:09 -04006140 /*
6141 * If the space info is for both data and metadata it means we have a
6142 * small filesystem and we can't use the clustering stuff.
6143 */
6144 if (btrfs_mixed_space_info(space_info))
6145 use_cluster = false;
6146
David Sterbab6919a52013-04-29 13:39:40 +00006147 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006148 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006149 if (!btrfs_test_opt(root, SSD))
6150 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006151 }
6152
David Sterbab6919a52013-04-29 13:39:40 +00006153 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006154 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006155 last_ptr = &root->fs_info->data_alloc_cluster;
6156 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006157
Chris Mason239b14b2008-03-24 15:02:07 -04006158 if (last_ptr) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006159 spin_lock(&last_ptr->lock);
6160 if (last_ptr->block_group)
6161 hint_byte = last_ptr->window_start;
6162 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006163 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006164
Chris Masona061fc82008-05-07 11:43:44 -04006165 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006166 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006167
Josef Bacik817d52f2009-07-13 21:29:25 -04006168 if (!last_ptr)
Chris Masonfa9c0d72009-04-03 09:47:43 -04006169 empty_cluster = 0;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006170
Josef Bacik2552d172009-04-03 10:14:19 -04006171 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006172 block_group = btrfs_lookup_block_group(root->fs_info,
6173 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006174 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04006175 /*
6176 * we don't want to use the block group if it doesn't match our
6177 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006178 *
6179 * However if we are re-searching with an ideal block group
6180 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006181 */
David Sterbab6919a52013-04-29 13:39:40 +00006182 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006183 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006184 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006185 if (list_empty(&block_group->list) ||
6186 block_group->ro) {
6187 /*
6188 * someone is removing this block group,
6189 * we can't jump into the have_block_group
6190 * target because our list pointers are not
6191 * valid
6192 */
6193 btrfs_put_block_group(block_group);
6194 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006195 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006196 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006197 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006198 }
Josef Bacik2552d172009-04-03 10:14:19 -04006199 } else if (block_group) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006200 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006201 }
Chris Mason42e70e72008-11-07 18:17:11 -05006202 }
Josef Bacik2552d172009-04-03 10:14:19 -04006203search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006204 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006205 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006206 list_for_each_entry(block_group, &space_info->block_groups[index],
6207 list) {
Josef Bacik6226cb02009-04-03 10:14:18 -04006208 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006209 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006210
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006211 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006212 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006213 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006214
Chris Mason83a50de2010-12-13 15:06:46 -05006215 /*
6216 * this can happen if we end up cycling through all the
6217 * raid types, but we want to make sure we only allocate
6218 * for the proper type.
6219 */
David Sterbab6919a52013-04-29 13:39:40 +00006220 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006221 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6222 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006223 BTRFS_BLOCK_GROUP_RAID5 |
6224 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006225 BTRFS_BLOCK_GROUP_RAID10;
6226
6227 /*
6228 * if they asked for extra copies and this block group
6229 * doesn't provide them, bail. This does allow us to
6230 * fill raid0 from raid1.
6231 */
David Sterbab6919a52013-04-29 13:39:40 +00006232 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006233 goto loop;
6234 }
6235
Josef Bacik2552d172009-04-03 10:14:19 -04006236have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006237 cached = block_group_cache_done(block_group);
6238 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006239 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006240 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006241 BUG_ON(ret < 0);
6242 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006243 }
6244
Josef Bacikea6a4782008-11-20 12:16:16 -05006245 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006246 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006247
Josef Bacik0a243252009-09-11 16:11:20 -04006248 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006249 * Ok we want to try and use the cluster allocator, so
6250 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006251 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006252 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006253 unsigned long aligned_cluster;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006254 /*
6255 * the refill lock keeps out other
6256 * people trying to start a new cluster
6257 */
6258 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006259 used_block_group = last_ptr->block_group;
6260 if (used_block_group != block_group &&
6261 (!used_block_group ||
6262 used_block_group->ro ||
David Sterbab6919a52013-04-29 13:39:40 +00006263 !block_group_bits(used_block_group, flags))) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006264 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006265 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006266 }
Chris Mason44fb5512009-06-04 15:34:51 -04006267
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006268 if (used_block_group != block_group)
6269 btrfs_get_block_group(used_block_group);
6270
6271 offset = btrfs_alloc_from_cluster(used_block_group,
6272 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006273 if (offset) {
6274 /* we have a block, we're done */
6275 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006276 trace_btrfs_reserve_extent_cluster(root,
6277 block_group, search_start, num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006278 goto checks;
6279 }
6280
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006281 WARN_ON(last_ptr->block_group != used_block_group);
6282 if (used_block_group != block_group) {
6283 btrfs_put_block_group(used_block_group);
6284 used_block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006285 }
Chris Mason44fb5512009-06-04 15:34:51 -04006286refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006287 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006288 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6289 * set up a new clusters, so lets just skip it
6290 * and let the allocator find whatever block
6291 * it can find. If we reach this point, we
6292 * will have tried the cluster allocator
6293 * plenty of times and not have found
6294 * anything, so we are likely way too
6295 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006296 * anything.
6297 *
6298 * However, if the cluster is taken from the
6299 * current block group, release the cluster
6300 * first, so that we stand a better chance of
6301 * succeeding in the unclustered
6302 * allocation. */
6303 if (loop >= LOOP_NO_EMPTY_SIZE &&
6304 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006305 spin_unlock(&last_ptr->refill_lock);
6306 goto unclustered_alloc;
6307 }
6308
Chris Masonfa9c0d72009-04-03 09:47:43 -04006309 /*
6310 * this cluster didn't work out, free it and
6311 * start over
6312 */
6313 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6314
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006315 if (loop >= LOOP_NO_EMPTY_SIZE) {
6316 spin_unlock(&last_ptr->refill_lock);
6317 goto unclustered_alloc;
6318 }
6319
Chris Mason8de972b2013-01-04 15:39:43 -05006320 aligned_cluster = max_t(unsigned long,
6321 empty_cluster + empty_size,
6322 block_group->full_stripe_len);
6323
Chris Masonfa9c0d72009-04-03 09:47:43 -04006324 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006325 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04006326 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006327 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006328 aligned_cluster);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006329 if (ret == 0) {
6330 /*
6331 * now pull our allocation out of this
6332 * cluster
6333 */
6334 offset = btrfs_alloc_from_cluster(block_group,
6335 last_ptr, num_bytes,
6336 search_start);
6337 if (offset) {
6338 /* we found one, proceed */
6339 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006340 trace_btrfs_reserve_extent_cluster(root,
6341 block_group, search_start,
6342 num_bytes);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006343 goto checks;
6344 }
Josef Bacik0a243252009-09-11 16:11:20 -04006345 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6346 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006347 spin_unlock(&last_ptr->refill_lock);
6348
Josef Bacik0a243252009-09-11 16:11:20 -04006349 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006350 wait_block_group_cache_progress(block_group,
6351 num_bytes + empty_cluster + empty_size);
6352 goto have_block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006353 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006354
Chris Masonfa9c0d72009-04-03 09:47:43 -04006355 /*
6356 * at this point we either didn't find a cluster
6357 * or we weren't able to allocate a block from our
6358 * cluster. Free the cluster we've been trying
6359 * to use, and go to the next block group
6360 */
Josef Bacik0a243252009-09-11 16:11:20 -04006361 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006362 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006363 goto loop;
Chris Masonfa9c0d72009-04-03 09:47:43 -04006364 }
6365
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006366unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006367 spin_lock(&block_group->free_space_ctl->tree_lock);
6368 if (cached &&
6369 block_group->free_space_ctl->free_space <
6370 num_bytes + empty_cluster + empty_size) {
6371 spin_unlock(&block_group->free_space_ctl->tree_lock);
6372 goto loop;
6373 }
6374 spin_unlock(&block_group->free_space_ctl->tree_lock);
6375
Josef Bacik6226cb02009-04-03 10:14:18 -04006376 offset = btrfs_find_space_for_alloc(block_group, search_start,
6377 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006378 /*
6379 * If we didn't find a chunk, and we haven't failed on this
6380 * block group before, and this block group is in the middle of
6381 * caching and we are ok with waiting, then go ahead and wait
6382 * for progress to be made, and set failed_alloc to true.
6383 *
6384 * If failed_alloc is true then we've already waited on this
6385 * block group once and should move on to the next block group.
6386 */
6387 if (!offset && !failed_alloc && !cached &&
6388 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006389 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006390 num_bytes + empty_size);
6391 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006392 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006393 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006394 if (!cached)
6395 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006396 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006397 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006398checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006399 search_start = stripe_align(root, used_block_group,
6400 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006401
Josef Bacik2552d172009-04-03 10:14:19 -04006402 /* move on to the next group */
6403 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006404 used_block_group->key.objectid + used_block_group->key.offset) {
6405 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006406 goto loop;
Josef Bacik6226cb02009-04-03 10:14:18 -04006407 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006408
Josef Bacik6226cb02009-04-03 10:14:18 -04006409 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006410 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb02009-04-03 10:14:18 -04006411 search_start - offset);
6412 BUG_ON(offset > search_start);
6413
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006414 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006415 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006416 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006417 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006418 goto loop;
6419 }
Yan Zheng11833d62009-09-11 16:11:19 -04006420
Josef Bacik2552d172009-04-03 10:14:19 -04006421 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006422 ins->objectid = search_start;
6423 ins->offset = num_bytes;
6424
Josef Bacik3f7de032011-11-10 08:29:20 -05006425 trace_btrfs_reserve_extent(orig_root, block_group,
6426 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006427 if (used_block_group != block_group)
6428 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006429 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006430 break;
6431loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006432 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006433 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006434 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006435 if (used_block_group != block_group)
6436 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04006437 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006438 }
6439 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006440
Miao Xie60d2adb2011-09-09 17:34:35 +08006441 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6442 goto search;
6443
Yan, Zhengb742bb82010-05-16 10:46:24 -04006444 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6445 goto search;
6446
Josef Bacik285ff5a2012-01-13 15:27:45 -05006447 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006448 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6449 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006450 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6451 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6452 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6453 * again
Chris Masonfa9c0d72009-04-03 09:47:43 -04006454 */
Josef Bacik723bda22011-05-27 16:11:38 -04006455 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006456 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006457 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006458 if (loop == LOOP_ALLOC_CHUNK) {
David Sterbab6919a52013-04-29 13:39:40 +00006459 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006460 CHUNK_ALLOC_FORCE);
6461 /*
6462 * Do not bail out on ENOSPC since we
6463 * can do more things.
6464 */
6465 if (ret < 0 && ret != -ENOSPC) {
6466 btrfs_abort_transaction(trans,
6467 root, ret);
6468 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006469 }
Josef Bacik723bda22011-05-27 16:11:38 -04006470 }
6471
6472 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04006473 empty_size = 0;
6474 empty_cluster = 0;
6475 }
Chris Mason42e70e72008-11-07 18:17:11 -05006476
Josef Bacik723bda22011-05-27 16:11:38 -04006477 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006478 } else if (!ins->objectid) {
6479 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006480 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006481 ret = 0;
6482 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006483out:
Chris Mason0b86a832008-03-24 15:01:56 -04006484
Chris Mason0f70abe2007-02-28 16:46:22 -05006485 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006486}
Chris Masonec44a352008-04-28 15:29:52 -04006487
Josef Bacik9ed74f22009-09-11 16:12:44 -04006488static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6489 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006490{
6491 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006492 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006493
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006495 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6496 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006497 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006498 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006499 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006500 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006501 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6502 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006503 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006504 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006505 (unsigned long long)info->bytes_pinned,
6506 (unsigned long long)info->bytes_reserved,
6507 (unsigned long long)info->bytes_may_use,
6508 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006509 spin_unlock(&info->lock);
6510
6511 if (!dump_block_groups)
6512 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006513
Josef Bacik80eb2342008-10-29 14:49:05 -04006514 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006515again:
6516 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006517 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006518 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006519 (unsigned long long)cache->key.objectid,
6520 (unsigned long long)cache->key.offset,
6521 (unsigned long long)btrfs_block_group_used(&cache->item),
6522 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006523 (unsigned long long)cache->reserved,
6524 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006525 btrfs_dump_free_space(cache, bytes);
6526 spin_unlock(&cache->lock);
6527 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006528 if (++index < BTRFS_NR_RAID_TYPES)
6529 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006530 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006531}
Zheng Yane8569812008-09-26 10:05:48 -04006532
Yan Zheng11833d62009-09-11 16:11:19 -04006533int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6534 struct btrfs_root *root,
6535 u64 num_bytes, u64 min_alloc_size,
6536 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006537 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006538{
Miao Xie9e622d62012-01-26 15:01:12 -05006539 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006540 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006541 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006542
David Sterbab6919a52013-04-29 13:39:40 +00006543 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006544again:
Chris Masondb945352007-10-15 16:15:53 -04006545 WARN_ON(num_bytes < root->sectorsize);
6546 ret = find_free_extent(trans, root, num_bytes, empty_size,
David Sterbab6919a52013-04-29 13:39:40 +00006547 hint_byte, ins, flags);
Chris Mason3b951512008-04-17 11:29:12 -04006548
Miao Xie9e622d62012-01-26 15:01:12 -05006549 if (ret == -ENOSPC) {
6550 if (!final_tried) {
6551 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006552 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006553 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006554 if (num_bytes == min_alloc_size)
6555 final_tried = true;
6556 goto again;
6557 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6558 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006559
David Sterbab6919a52013-04-29 13:39:40 +00006560 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006561 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
David Sterbab6919a52013-04-29 13:39:40 +00006562 (unsigned long long)flags,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006563 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006564 if (sinfo)
6565 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006566 }
Chris Mason925baed2008-06-25 16:01:30 -04006567 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006568
liubo1abe9b82011-03-24 11:18:59 +00006569 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6570
Josef Bacik0f9dd462008-09-23 13:14:11 -04006571 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006572}
6573
Chris Masone688b722011-10-31 20:52:39 -04006574static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6575 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006576{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006577 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006578 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006579
Josef Bacik0f9dd462008-09-23 13:14:11 -04006580 cache = btrfs_lookup_block_group(root->fs_info, start);
6581 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006582 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6583 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006584 return -ENOSPC;
6585 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006586
Li Dongyang5378e602011-03-24 10:24:27 +00006587 if (btrfs_test_opt(root, DISCARD))
6588 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006589
Chris Masone688b722011-10-31 20:52:39 -04006590 if (pin)
6591 pin_down_extent(root, cache, start, len, 1);
6592 else {
6593 btrfs_add_free_space(cache, start, len);
6594 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6595 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04006596 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006597
liubo1abe9b82011-03-24 11:18:59 +00006598 trace_btrfs_reserved_extent_free(root, start, len);
6599
Chris Masone6dcd2d2008-07-17 12:53:50 -04006600 return ret;
6601}
6602
Chris Masone688b722011-10-31 20:52:39 -04006603int btrfs_free_reserved_extent(struct btrfs_root *root,
6604 u64 start, u64 len)
6605{
6606 return __btrfs_free_reserved_extent(root, start, len, 0);
6607}
6608
6609int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6610 u64 start, u64 len)
6611{
6612 return __btrfs_free_reserved_extent(root, start, len, 1);
6613}
6614
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006615static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6616 struct btrfs_root *root,
6617 u64 parent, u64 root_objectid,
6618 u64 flags, u64 owner, u64 offset,
6619 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006620{
6621 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006622 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006623 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006624 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006625 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006626 struct extent_buffer *leaf;
6627 int type;
6628 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006629
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006630 if (parent > 0)
6631 type = BTRFS_SHARED_DATA_REF_KEY;
6632 else
6633 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006634
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006635 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006636
6637 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006638 if (!path)
6639 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006640
Chris Masonb9473432009-03-13 11:00:37 -04006641 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006642 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6643 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006644 if (ret) {
6645 btrfs_free_path(path);
6646 return ret;
6647 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006648
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006649 leaf = path->nodes[0];
6650 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006651 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006652 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6653 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6654 btrfs_set_extent_flags(leaf, extent_item,
6655 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006656
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006657 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6658 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6659 if (parent > 0) {
6660 struct btrfs_shared_data_ref *ref;
6661 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6662 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6663 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6664 } else {
6665 struct btrfs_extent_data_ref *ref;
6666 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6667 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6668 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6669 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6670 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6671 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006672
6673 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006674 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006675
Liu Boc53d6132012-12-27 09:01:19 +00006676 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006677 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006678 btrfs_err(fs_info, "update block group failed for %llu %llu",
6679 (unsigned long long)ins->objectid,
6680 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006681 BUG();
6682 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006683 return ret;
6684}
6685
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006686static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6687 struct btrfs_root *root,
6688 u64 parent, u64 root_objectid,
6689 u64 flags, struct btrfs_disk_key *key,
6690 int level, struct btrfs_key *ins)
6691{
6692 int ret;
6693 struct btrfs_fs_info *fs_info = root->fs_info;
6694 struct btrfs_extent_item *extent_item;
6695 struct btrfs_tree_block_info *block_info;
6696 struct btrfs_extent_inline_ref *iref;
6697 struct btrfs_path *path;
6698 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006699 u32 size = sizeof(*extent_item) + sizeof(*iref);
6700 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6701 SKINNY_METADATA);
6702
6703 if (!skinny_metadata)
6704 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006705
6706 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006707 if (!path)
6708 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006709
6710 path->leave_spinning = 1;
6711 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6712 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006713 if (ret) {
6714 btrfs_free_path(path);
6715 return ret;
6716 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006717
6718 leaf = path->nodes[0];
6719 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6720 struct btrfs_extent_item);
6721 btrfs_set_extent_refs(leaf, extent_item, 1);
6722 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6723 btrfs_set_extent_flags(leaf, extent_item,
6724 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006725
Josef Bacik3173a182013-03-07 14:22:04 -05006726 if (skinny_metadata) {
6727 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6728 } else {
6729 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6730 btrfs_set_tree_block_key(leaf, block_info, key);
6731 btrfs_set_tree_block_level(leaf, block_info, level);
6732 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6733 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006734
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006735 if (parent > 0) {
6736 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6737 btrfs_set_extent_inline_ref_type(leaf, iref,
6738 BTRFS_SHARED_BLOCK_REF_KEY);
6739 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6740 } else {
6741 btrfs_set_extent_inline_ref_type(leaf, iref,
6742 BTRFS_TREE_BLOCK_REF_KEY);
6743 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6744 }
6745
6746 btrfs_mark_buffer_dirty(leaf);
6747 btrfs_free_path(path);
6748
Josef Bacik3173a182013-03-07 14:22:04 -05006749 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006750 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006751 btrfs_err(fs_info, "update block group failed for %llu %llu",
6752 (unsigned long long)ins->objectid,
6753 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006754 BUG();
6755 }
6756 return ret;
6757}
6758
6759int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6760 struct btrfs_root *root,
6761 u64 root_objectid, u64 owner,
6762 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006763{
6764 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006765
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006766 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006767
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006768 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6769 ins->offset, 0,
6770 root_objectid, owner, offset,
6771 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006772 return ret;
6773}
Chris Masone02119d2008-09-05 16:13:11 -04006774
6775/*
6776 * this is used by the tree logging recovery code. It records that
6777 * an extent has been allocated and makes sure to clear the free
6778 * space cache bits as well
6779 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006780int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6781 struct btrfs_root *root,
6782 u64 root_objectid, u64 owner, u64 offset,
6783 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006784{
6785 int ret;
6786 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006787
6788 /*
6789 * Mixed block groups will exclude before processing the log so we only
6790 * need to do the exlude dance if this fs isn't mixed.
6791 */
6792 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
6793 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
6794 if (ret)
6795 return ret;
6796 }
Chris Masone02119d2008-09-05 16:13:11 -04006797
Chris Masone02119d2008-09-05 16:13:11 -04006798 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006799 if (!block_group)
6800 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006801
Josef Bacikfb25e912011-07-26 17:00:46 -04006802 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6803 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006804 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006805 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6806 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006807 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006808 return ret;
6809}
6810
Eric Sandeen48a3b632013-04-25 20:41:01 +00006811static struct extent_buffer *
6812btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6813 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006814{
6815 struct extent_buffer *buf;
6816
6817 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6818 if (!buf)
6819 return ERR_PTR(-ENOMEM);
6820 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006821 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006822 btrfs_tree_lock(buf);
6823 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006824 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006825
6826 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006827 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006828
Chris Masond0c803c2008-09-11 16:17:57 -04006829 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006830 /*
6831 * we allow two log transactions at a time, use different
6832 * EXENT bit to differentiate dirty pages.
6833 */
6834 if (root->log_transid % 2 == 0)
6835 set_extent_dirty(&root->dirty_log_pages, buf->start,
6836 buf->start + buf->len - 1, GFP_NOFS);
6837 else
6838 set_extent_new(&root->dirty_log_pages, buf->start,
6839 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006840 } else {
6841 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6842 buf->start + buf->len - 1, GFP_NOFS);
6843 }
Chris Mason65b51a02008-08-01 15:11:20 -04006844 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006845 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006846 return buf;
6847}
6848
Yan, Zhengf0486c62010-05-16 10:46:25 -04006849static struct btrfs_block_rsv *
6850use_block_rsv(struct btrfs_trans_handle *trans,
6851 struct btrfs_root *root, u32 blocksize)
6852{
6853 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006854 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006855 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00006856 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006857
6858 block_rsv = get_block_rsv(trans, root);
6859
Miao Xieb586b322013-05-13 13:55:10 +00006860 if (unlikely(block_rsv->size == 0))
6861 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00006862again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006863 ret = block_rsv_use_bytes(block_rsv, blocksize);
6864 if (!ret)
6865 return block_rsv;
6866
Miao Xieb586b322013-05-13 13:55:10 +00006867 if (block_rsv->failfast)
6868 return ERR_PTR(ret);
6869
Miao Xied88033d2013-05-13 13:55:12 +00006870 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
6871 global_updated = true;
6872 update_global_block_rsv(root->fs_info);
6873 goto again;
6874 }
6875
Miao Xieb586b322013-05-13 13:55:10 +00006876 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6877 static DEFINE_RATELIMIT_STATE(_rs,
6878 DEFAULT_RATELIMIT_INTERVAL * 10,
6879 /*DEFAULT_RATELIMIT_BURST*/ 1);
6880 if (__ratelimit(&_rs))
6881 WARN(1, KERN_DEBUG
6882 "btrfs: block rsv returned %d\n", ret);
6883 }
6884try_reserve:
6885 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6886 BTRFS_RESERVE_NO_FLUSH);
6887 if (!ret)
6888 return block_rsv;
6889 /*
6890 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00006891 * the global reserve if its space type is the same as the global
6892 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00006893 */
Miao Xie5881cfc2013-05-13 13:55:11 +00006894 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
6895 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00006896 ret = block_rsv_use_bytes(global_rsv, blocksize);
6897 if (!ret)
6898 return global_rsv;
6899 }
6900 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006901}
6902
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006903static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6904 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006905{
6906 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006907 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006908}
6909
Chris Masonfec577f2007-02-26 10:40:21 -05006910/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006911 * finds a free extent and does all the dirty work required for allocation
6912 * returns the key for the extent through ins, and a tree buffer for
6913 * the first block of the extent through buf.
6914 *
Chris Masonfec577f2007-02-26 10:40:21 -05006915 * returns the tree buffer or NULL.
6916 */
Chris Mason5f39d392007-10-15 16:14:19 -04006917struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006918 struct btrfs_root *root, u32 blocksize,
6919 u64 parent, u64 root_objectid,
6920 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006921 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006922{
Chris Masone2fa7222007-03-12 16:22:34 -04006923 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006924 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006925 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006926 u64 flags = 0;
6927 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006928 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6929 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006930
6931 block_rsv = use_block_rsv(trans, root, blocksize);
6932 if (IS_ERR(block_rsv))
6933 return ERR_CAST(block_rsv);
6934
6935 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006936 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006937 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006938 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006939 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006940 }
Chris Mason55c69072008-01-09 15:55:33 -05006941
Chris Mason4008c042009-02-12 14:09:45 -05006942 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6943 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006944 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006945
6946 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6947 if (parent == 0)
6948 parent = ins.objectid;
6949 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6950 } else
6951 BUG_ON(parent > 0);
6952
6953 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6954 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006955 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006956 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006957 if (key)
6958 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6959 else
6960 memset(&extent_op->key, 0, sizeof(extent_op->key));
6961 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006962 if (skinny_metadata)
6963 extent_op->update_key = 0;
6964 else
6965 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006966 extent_op->update_flags = 1;
6967 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04006968 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006969
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006970 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6971 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006972 ins.offset, parent, root_objectid,
6973 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006974 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006975 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006976 }
Chris Masonfec577f2007-02-26 10:40:21 -05006977 return buf;
6978}
Chris Masona28ec192007-03-06 20:08:01 -05006979
Yan Zheng2c47e6052009-06-27 21:07:35 -04006980struct walk_control {
6981 u64 refs[BTRFS_MAX_LEVEL];
6982 u64 flags[BTRFS_MAX_LEVEL];
6983 struct btrfs_key update_progress;
6984 int stage;
6985 int level;
6986 int shared_level;
6987 int update_ref;
6988 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006989 int reada_slot;
6990 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006991 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006992};
6993
6994#define DROP_REFERENCE 1
6995#define UPDATE_BACKREF 2
6996
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006997static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6998 struct btrfs_root *root,
6999 struct walk_control *wc,
7000 struct btrfs_path *path)
7001{
7002 u64 bytenr;
7003 u64 generation;
7004 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007005 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007006 u32 nritems;
7007 u32 blocksize;
7008 struct btrfs_key key;
7009 struct extent_buffer *eb;
7010 int ret;
7011 int slot;
7012 int nread = 0;
7013
7014 if (path->slots[wc->level] < wc->reada_slot) {
7015 wc->reada_count = wc->reada_count * 2 / 3;
7016 wc->reada_count = max(wc->reada_count, 2);
7017 } else {
7018 wc->reada_count = wc->reada_count * 3 / 2;
7019 wc->reada_count = min_t(int, wc->reada_count,
7020 BTRFS_NODEPTRS_PER_BLOCK(root));
7021 }
7022
7023 eb = path->nodes[wc->level];
7024 nritems = btrfs_header_nritems(eb);
7025 blocksize = btrfs_level_size(root, wc->level - 1);
7026
7027 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7028 if (nread >= wc->reada_count)
7029 break;
7030
7031 cond_resched();
7032 bytenr = btrfs_node_blockptr(eb, slot);
7033 generation = btrfs_node_ptr_generation(eb, slot);
7034
7035 if (slot == path->slots[wc->level])
7036 goto reada;
7037
7038 if (wc->stage == UPDATE_BACKREF &&
7039 generation <= root->root_key.offset)
7040 continue;
7041
Yan, Zheng94fcca92009-10-09 09:25:16 -04007042 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007043 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7044 wc->level - 1, 1, &refs,
7045 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007046 /* We don't care about errors in readahead. */
7047 if (ret < 0)
7048 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007049 BUG_ON(refs == 0);
7050
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007051 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007052 if (refs == 1)
7053 goto reada;
7054
Yan, Zheng94fcca92009-10-09 09:25:16 -04007055 if (wc->level == 1 &&
7056 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7057 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007058 if (!wc->update_ref ||
7059 generation <= root->root_key.offset)
7060 continue;
7061 btrfs_node_key_to_cpu(eb, &key, slot);
7062 ret = btrfs_comp_cpu_keys(&key,
7063 &wc->update_progress);
7064 if (ret < 0)
7065 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007066 } else {
7067 if (wc->level == 1 &&
7068 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7069 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007070 }
7071reada:
7072 ret = readahead_tree_block(root, bytenr, blocksize,
7073 generation);
7074 if (ret)
7075 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007076 nread++;
7077 }
7078 wc->reada_slot = slot;
7079}
7080
Chris Mason9aca1d52007-03-13 11:09:37 -04007081/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007082 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007083 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007084 * when wc->stage == UPDATE_BACKREF, this function updates
7085 * back refs for pointers in the block.
7086 *
7087 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007088 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007089static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7090 struct btrfs_root *root,
7091 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007092 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007093{
7094 int level = wc->level;
7095 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007096 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7097 int ret;
7098
7099 if (wc->stage == UPDATE_BACKREF &&
7100 btrfs_header_owner(eb) != root->root_key.objectid)
7101 return 1;
7102
7103 /*
7104 * when reference count of tree block is 1, it won't increase
7105 * again. once full backref flag is set, we never clear it.
7106 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007107 if (lookup_info &&
7108 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7109 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007110 BUG_ON(!path->locks[level]);
7111 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007112 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007113 &wc->refs[level],
7114 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007115 BUG_ON(ret == -ENOMEM);
7116 if (ret)
7117 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007118 BUG_ON(wc->refs[level] == 0);
7119 }
7120
Yan Zheng2c47e6052009-06-27 21:07:35 -04007121 if (wc->stage == DROP_REFERENCE) {
7122 if (wc->refs[level] > 1)
7123 return 1;
7124
7125 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007126 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007127 path->locks[level] = 0;
7128 }
7129 return 0;
7130 }
7131
7132 /* wc->stage == UPDATE_BACKREF */
7133 if (!(wc->flags[level] & flag)) {
7134 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007135 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007136 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007137 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007138 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007139 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007140 eb->len, flag,
7141 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007142 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007143 wc->flags[level] |= flag;
7144 }
7145
7146 /*
7147 * the block is shared by multiple trees, so it's not good to
7148 * keep the tree lock
7149 */
7150 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007151 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007152 path->locks[level] = 0;
7153 }
7154 return 0;
7155}
7156
7157/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007158 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007159 *
7160 * when wc->stage == DROP_REFERENCE, this function checks
7161 * reference count of the block pointed to. if the block
7162 * is shared and we need update back refs for the subtree
7163 * rooted at the block, this function changes wc->stage to
7164 * UPDATE_BACKREF. if the block is shared and there is no
7165 * need to update back, this function drops the reference
7166 * to the block.
7167 *
7168 * NOTE: return value 1 means we should stop walking down.
7169 */
7170static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7171 struct btrfs_root *root,
7172 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007173 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007174{
7175 u64 bytenr;
7176 u64 generation;
7177 u64 parent;
7178 u32 blocksize;
7179 struct btrfs_key key;
7180 struct extent_buffer *next;
7181 int level = wc->level;
7182 int reada = 0;
7183 int ret = 0;
7184
7185 generation = btrfs_node_ptr_generation(path->nodes[level],
7186 path->slots[level]);
7187 /*
7188 * if the lower level block was created before the snapshot
7189 * was created, we know there is no need to update back refs
7190 * for the subtree
7191 */
7192 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007193 generation <= root->root_key.offset) {
7194 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007195 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007196 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007197
7198 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7199 blocksize = btrfs_level_size(root, level - 1);
7200
7201 next = btrfs_find_tree_block(root, bytenr, blocksize);
7202 if (!next) {
7203 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c512010-03-25 12:37:12 +00007204 if (!next)
7205 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007206 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7207 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007208 reada = 1;
7209 }
7210 btrfs_tree_lock(next);
7211 btrfs_set_lock_blocking(next);
7212
Josef Bacik3173a182013-03-07 14:22:04 -05007213 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007214 &wc->refs[level - 1],
7215 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007216 if (ret < 0) {
7217 btrfs_tree_unlock(next);
7218 return ret;
7219 }
7220
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007221 if (unlikely(wc->refs[level - 1] == 0)) {
7222 btrfs_err(root->fs_info, "Missing references.");
7223 BUG();
7224 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007225 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007226
Yan, Zheng94fcca92009-10-09 09:25:16 -04007227 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007228 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007229 if (level == 1 &&
7230 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7231 goto skip;
7232
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007233 if (!wc->update_ref ||
7234 generation <= root->root_key.offset)
7235 goto skip;
7236
7237 btrfs_node_key_to_cpu(path->nodes[level], &key,
7238 path->slots[level]);
7239 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7240 if (ret < 0)
7241 goto skip;
7242
7243 wc->stage = UPDATE_BACKREF;
7244 wc->shared_level = level - 1;
7245 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007246 } else {
7247 if (level == 1 &&
7248 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7249 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007250 }
7251
Chris Masonb9fab912012-05-06 07:23:47 -04007252 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007253 btrfs_tree_unlock(next);
7254 free_extent_buffer(next);
7255 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007256 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007257 }
7258
7259 if (!next) {
7260 if (reada && level == 1)
7261 reada_walk_down(trans, root, wc, path);
7262 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007263 if (!next || !extent_buffer_uptodate(next)) {
7264 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007265 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007266 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007267 btrfs_tree_lock(next);
7268 btrfs_set_lock_blocking(next);
7269 }
7270
7271 level--;
7272 BUG_ON(level != btrfs_header_level(next));
7273 path->nodes[level] = next;
7274 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007275 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007276 wc->level = level;
7277 if (wc->level == 1)
7278 wc->reada_slot = 0;
7279 return 0;
7280skip:
7281 wc->refs[level - 1] = 0;
7282 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007283 if (wc->stage == DROP_REFERENCE) {
7284 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7285 parent = path->nodes[level]->start;
7286 } else {
7287 BUG_ON(root->root_key.objectid !=
7288 btrfs_header_owner(path->nodes[level]));
7289 parent = 0;
7290 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007291
Yan, Zheng94fcca92009-10-09 09:25:16 -04007292 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007293 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007294 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007295 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007296 btrfs_tree_unlock(next);
7297 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007298 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007299 return 1;
7300}
7301
7302/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007303 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007304 *
7305 * when wc->stage == DROP_REFERENCE, this function drops
7306 * reference count on the block.
7307 *
7308 * when wc->stage == UPDATE_BACKREF, this function changes
7309 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7310 * to UPDATE_BACKREF previously while processing the block.
7311 *
7312 * NOTE: return value 1 means we should stop walking up.
7313 */
7314static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7315 struct btrfs_root *root,
7316 struct btrfs_path *path,
7317 struct walk_control *wc)
7318{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007319 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007320 int level = wc->level;
7321 struct extent_buffer *eb = path->nodes[level];
7322 u64 parent = 0;
7323
7324 if (wc->stage == UPDATE_BACKREF) {
7325 BUG_ON(wc->shared_level < level);
7326 if (level < wc->shared_level)
7327 goto out;
7328
Yan Zheng2c47e6052009-06-27 21:07:35 -04007329 ret = find_next_key(path, level + 1, &wc->update_progress);
7330 if (ret > 0)
7331 wc->update_ref = 0;
7332
7333 wc->stage = DROP_REFERENCE;
7334 wc->shared_level = -1;
7335 path->slots[level] = 0;
7336
7337 /*
7338 * check reference count again if the block isn't locked.
7339 * we should start walking down the tree again if reference
7340 * count is one.
7341 */
7342 if (!path->locks[level]) {
7343 BUG_ON(level == 0);
7344 btrfs_tree_lock(eb);
7345 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007346 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007347
7348 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007349 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007350 &wc->refs[level],
7351 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007352 if (ret < 0) {
7353 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007354 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007355 return ret;
7356 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007357 BUG_ON(wc->refs[level] == 0);
7358 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007359 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007360 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007361 return 1;
7362 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007363 }
7364 }
7365
7366 /* wc->stage == DROP_REFERENCE */
7367 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7368
7369 if (wc->refs[level] == 1) {
7370 if (level == 0) {
7371 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007372 ret = btrfs_dec_ref(trans, root, eb, 1,
7373 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007374 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007375 ret = btrfs_dec_ref(trans, root, eb, 0,
7376 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007377 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007378 }
7379 /* make block locked assertion in clean_tree_block happy */
7380 if (!path->locks[level] &&
7381 btrfs_header_generation(eb) == trans->transid) {
7382 btrfs_tree_lock(eb);
7383 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007384 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007385 }
7386 clean_tree_block(trans, root, eb);
7387 }
7388
7389 if (eb == root->node) {
7390 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7391 parent = eb->start;
7392 else
7393 BUG_ON(root->root_key.objectid !=
7394 btrfs_header_owner(eb));
7395 } else {
7396 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7397 parent = path->nodes[level + 1]->start;
7398 else
7399 BUG_ON(root->root_key.objectid !=
7400 btrfs_header_owner(path->nodes[level + 1]));
7401 }
7402
Jan Schmidt5581a512012-05-16 17:04:52 +02007403 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007404out:
7405 wc->refs[level] = 0;
7406 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007407 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007408}
7409
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007410static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7411 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007412 struct btrfs_path *path,
7413 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007414{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007415 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007416 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007417 int ret;
7418
Yan Zheng2c47e6052009-06-27 21:07:35 -04007419 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007420 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007421 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007422 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007423
Yan Zheng2c47e6052009-06-27 21:07:35 -04007424 if (level == 0)
7425 break;
7426
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007427 if (path->slots[level] >=
7428 btrfs_header_nritems(path->nodes[level]))
7429 break;
7430
Yan, Zheng94fcca92009-10-09 09:25:16 -04007431 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007432 if (ret > 0) {
7433 path->slots[level]++;
7434 continue;
Miao Xie90d2c512010-03-25 12:37:12 +00007435 } else if (ret < 0)
7436 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007437 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007438 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007439 return 0;
7440}
7441
Chris Masond3977122009-01-05 21:25:51 -05007442static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007443 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007444 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007445 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007446{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007447 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007448 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007449
Yan Zheng2c47e6052009-06-27 21:07:35 -04007450 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7451 while (level < max_level && path->nodes[level]) {
7452 wc->level = level;
7453 if (path->slots[level] + 1 <
7454 btrfs_header_nritems(path->nodes[level])) {
7455 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007456 return 0;
7457 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007458 ret = walk_up_proc(trans, root, path, wc);
7459 if (ret > 0)
7460 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007461
Yan Zheng2c47e6052009-06-27 21:07:35 -04007462 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007463 btrfs_tree_unlock_rw(path->nodes[level],
7464 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007465 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007466 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007467 free_extent_buffer(path->nodes[level]);
7468 path->nodes[level] = NULL;
7469 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007470 }
7471 }
7472 return 1;
7473}
7474
Chris Mason9aca1d52007-03-13 11:09:37 -04007475/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007476 * drop a subvolume tree.
7477 *
7478 * this function traverses the tree freeing any blocks that only
7479 * referenced by the tree.
7480 *
7481 * when a shared tree block is found. this function decreases its
7482 * reference count by one. if update_ref is true, this function
7483 * also make sure backrefs for the shared block and all lower level
7484 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007485 *
7486 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007487 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007488int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007489 struct btrfs_block_rsv *block_rsv, int update_ref,
7490 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007491{
Chris Mason5caf2a02007-04-02 11:20:42 -04007492 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007493 struct btrfs_trans_handle *trans;
7494 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007495 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007496 struct walk_control *wc;
7497 struct btrfs_key key;
7498 int err = 0;
7499 int ret;
7500 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04007501 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05007502
Chris Mason5caf2a02007-04-02 11:20:42 -04007503 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007504 if (!path) {
7505 err = -ENOMEM;
7506 goto out;
7507 }
Chris Mason20524f02007-03-10 06:35:47 -05007508
Yan Zheng2c47e6052009-06-27 21:07:35 -04007509 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007510 if (!wc) {
7511 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007512 err = -ENOMEM;
7513 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007514 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007515
Yan, Zhenga22285a2010-05-16 10:48:46 -04007516 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007517 if (IS_ERR(trans)) {
7518 err = PTR_ERR(trans);
7519 goto out_free;
7520 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007521
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007522 if (block_rsv)
7523 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007524
Chris Mason9f3a7422007-08-07 15:52:19 -04007525 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007526 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007527 path->nodes[level] = btrfs_lock_root_node(root);
7528 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007529 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007530 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007531 memset(&wc->update_progress, 0,
7532 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007533 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007534 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007535 memcpy(&wc->update_progress, &key,
7536 sizeof(wc->update_progress));
7537
Chris Mason6702ed42007-08-07 16:15:09 -04007538 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007539 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007540 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007541 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7542 path->lowest_level = 0;
7543 if (ret < 0) {
7544 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007545 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007546 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007547 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007548
Chris Mason7d9eb122008-07-08 14:19:17 -04007549 /*
7550 * unlock our path, this is safe because only this
7551 * function is allowed to delete this snapshot
7552 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007553 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007554
Yan Zheng2c47e6052009-06-27 21:07:35 -04007555 level = btrfs_header_level(root->node);
7556 while (1) {
7557 btrfs_tree_lock(path->nodes[level]);
7558 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007559 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007560
7561 ret = btrfs_lookup_extent_info(trans, root,
7562 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007563 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007564 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007565 if (ret < 0) {
7566 err = ret;
7567 goto out_end_trans;
7568 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007569 BUG_ON(wc->refs[level] == 0);
7570
7571 if (level == root_item->drop_level)
7572 break;
7573
7574 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007575 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007576 WARN_ON(wc->refs[level] != 1);
7577 level--;
7578 }
7579 }
7580
7581 wc->level = level;
7582 wc->shared_level = -1;
7583 wc->stage = DROP_REFERENCE;
7584 wc->update_ref = update_ref;
7585 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007586 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007587 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007588
7589 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007590
Yan Zheng2c47e6052009-06-27 21:07:35 -04007591 ret = walk_down_tree(trans, root, path, wc);
7592 if (ret < 0) {
7593 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007594 break;
7595 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007596
7597 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7598 if (ret < 0) {
7599 err = ret;
7600 break;
7601 }
7602
7603 if (ret > 0) {
7604 BUG_ON(wc->stage != DROP_REFERENCE);
7605 break;
7606 }
7607
7608 if (wc->stage == DROP_REFERENCE) {
7609 level = wc->level;
7610 btrfs_node_key(path->nodes[level],
7611 &root_item->drop_progress,
7612 path->slots[level]);
7613 root_item->drop_level = level;
7614 }
7615
7616 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007617 if (btrfs_should_end_transaction(trans, tree_root) ||
7618 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007619 ret = btrfs_update_root(trans, tree_root,
7620 &root->root_key,
7621 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007622 if (ret) {
7623 btrfs_abort_transaction(trans, tree_root, ret);
7624 err = ret;
7625 goto out_end_trans;
7626 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007627
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007628 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007629 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
7630 pr_debug("btrfs: drop snapshot early exit\n");
7631 err = -EAGAIN;
7632 goto out_free;
7633 }
7634
Yan, Zhenga22285a2010-05-16 10:48:46 -04007635 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007636 if (IS_ERR(trans)) {
7637 err = PTR_ERR(trans);
7638 goto out_free;
7639 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007640 if (block_rsv)
7641 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007642 }
Chris Mason20524f02007-03-10 06:35:47 -05007643 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007644 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007645 if (err)
7646 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007647
7648 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007649 if (ret) {
7650 btrfs_abort_transaction(trans, tree_root, ret);
7651 goto out_end_trans;
7652 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007653
Yan, Zheng76dda932009-09-21 16:00:26 -04007654 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007655 ret = btrfs_find_root(tree_root, &root->root_key, path,
7656 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007657 if (ret < 0) {
7658 btrfs_abort_transaction(trans, tree_root, ret);
7659 err = ret;
7660 goto out_end_trans;
7661 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007662 /* if we fail to delete the orphan item this time
7663 * around, it'll get picked up the next time.
7664 *
7665 * The most common failure here is just -ENOENT.
7666 */
7667 btrfs_del_orphan_item(trans, tree_root,
7668 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007669 }
7670 }
7671
7672 if (root->in_radix) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007673 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007674 } else {
7675 free_extent_buffer(root->node);
7676 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007677 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007678 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007679 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007680out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007681 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007682out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007683 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007684 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007685out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007686 /*
7687 * So if we need to stop dropping the snapshot for whatever reason we
7688 * need to make sure to add it back to the dead root list so that we
7689 * keep trying to do the work later. This also cleans up roots if we
7690 * don't have it in the radix (like when we recover after a power fail
7691 * or unmount) so we don't leak memory.
7692 */
7693 if (root_dropped == false)
7694 btrfs_add_dead_root(root);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007695 if (err)
7696 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007697 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007698}
Chris Mason9078a3e2007-04-26 16:46:15 -04007699
Yan Zheng2c47e6052009-06-27 21:07:35 -04007700/*
7701 * drop subtree rooted at tree block 'node'.
7702 *
7703 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007704 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007705 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007706int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7707 struct btrfs_root *root,
7708 struct extent_buffer *node,
7709 struct extent_buffer *parent)
7710{
7711 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007712 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007713 int level;
7714 int parent_level;
7715 int ret = 0;
7716 int wret;
7717
Yan Zheng2c47e6052009-06-27 21:07:35 -04007718 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7719
Yan Zhengf82d02d2008-10-29 14:49:05 -04007720 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007721 if (!path)
7722 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007723
Yan Zheng2c47e6052009-06-27 21:07:35 -04007724 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007725 if (!wc) {
7726 btrfs_free_path(path);
7727 return -ENOMEM;
7728 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007729
Chris Masonb9447ef2009-03-09 11:45:38 -04007730 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007731 parent_level = btrfs_header_level(parent);
7732 extent_buffer_get(parent);
7733 path->nodes[parent_level] = parent;
7734 path->slots[parent_level] = btrfs_header_nritems(parent);
7735
Chris Masonb9447ef2009-03-09 11:45:38 -04007736 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007737 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007738 path->nodes[level] = node;
7739 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007740 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007741
7742 wc->refs[parent_level] = 1;
7743 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7744 wc->level = level;
7745 wc->shared_level = -1;
7746 wc->stage = DROP_REFERENCE;
7747 wc->update_ref = 0;
7748 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007749 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007750 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007751
7752 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007753 wret = walk_down_tree(trans, root, path, wc);
7754 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007755 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007756 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007757 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007758
Yan Zheng2c47e6052009-06-27 21:07:35 -04007759 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007760 if (wret < 0)
7761 ret = wret;
7762 if (wret != 0)
7763 break;
7764 }
7765
Yan Zheng2c47e6052009-06-27 21:07:35 -04007766 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007767 btrfs_free_path(path);
7768 return ret;
7769}
7770
Chris Masonec44a352008-04-28 15:29:52 -04007771static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7772{
7773 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007774 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007775
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007776 /*
7777 * if restripe for this chunk_type is on pick target profile and
7778 * return, otherwise do the usual balance
7779 */
7780 stripped = get_restripe_target(root->fs_info, flags);
7781 if (stripped)
7782 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007783
Chris Masoncd02dca2010-12-13 14:56:23 -05007784 /*
7785 * we add in the count of missing devices because we want
7786 * to make sure that any RAID levels on a degraded FS
7787 * continue to be honored.
7788 */
7789 num_devices = root->fs_info->fs_devices->rw_devices +
7790 root->fs_info->fs_devices->missing_devices;
7791
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007792 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007793 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007794 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7795
Chris Masonec44a352008-04-28 15:29:52 -04007796 if (num_devices == 1) {
7797 stripped |= BTRFS_BLOCK_GROUP_DUP;
7798 stripped = flags & ~stripped;
7799
7800 /* turn raid0 into single device chunks */
7801 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7802 return stripped;
7803
7804 /* turn mirroring into duplication */
7805 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7806 BTRFS_BLOCK_GROUP_RAID10))
7807 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007808 } else {
7809 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007810 if (flags & stripped)
7811 return flags;
7812
7813 stripped |= BTRFS_BLOCK_GROUP_DUP;
7814 stripped = flags & ~stripped;
7815
7816 /* switch duplicated blocks with raid1 */
7817 if (flags & BTRFS_BLOCK_GROUP_DUP)
7818 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7819
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007820 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007821 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007822
Chris Masonec44a352008-04-28 15:29:52 -04007823 return flags;
7824}
7825
Miao Xie199c36e2011-07-15 10:34:36 +00007826static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007827{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007828 struct btrfs_space_info *sinfo = cache->space_info;
7829 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007830 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007831 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007832
Chris Masonc286ac42008-07-22 23:06:41 -04007833
Miao Xie199c36e2011-07-15 10:34:36 +00007834 /*
7835 * We need some metadata space and system metadata space for
7836 * allocating chunks in some corner cases until we force to set
7837 * it to be readonly.
7838 */
7839 if ((sinfo->flags &
7840 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7841 !force)
7842 min_allocable_bytes = 1 * 1024 * 1024;
7843 else
7844 min_allocable_bytes = 0;
7845
Yan, Zhengf0486c62010-05-16 10:46:25 -04007846 spin_lock(&sinfo->lock);
7847 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007848
7849 if (cache->ro) {
7850 ret = 0;
7851 goto out;
7852 }
7853
Yan, Zhengf0486c62010-05-16 10:46:25 -04007854 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7855 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007856
Yan, Zhengf0486c62010-05-16 10:46:25 -04007857 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007858 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7859 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007860 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007861 cache->ro = 1;
7862 ret = 0;
7863 }
WuBo61cfea92011-07-26 03:30:11 +00007864out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007865 spin_unlock(&cache->lock);
7866 spin_unlock(&sinfo->lock);
7867 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007868}
7869
Yan, Zhengf0486c62010-05-16 10:46:25 -04007870int btrfs_set_block_group_ro(struct btrfs_root *root,
7871 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007872
7873{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007874 struct btrfs_trans_handle *trans;
7875 u64 alloc_flags;
7876 int ret;
7877
7878 BUG_ON(cache->ro);
7879
Josef Bacik7a7eaa402011-04-13 12:54:33 -04007880 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007881 if (IS_ERR(trans))
7882 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007883
7884 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007885 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007886 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007887 CHUNK_ALLOC_FORCE);
7888 if (ret < 0)
7889 goto out;
7890 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007891
Miao Xie199c36e2011-07-15 10:34:36 +00007892 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007893 if (!ret)
7894 goto out;
7895 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007896 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007897 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007898 if (ret < 0)
7899 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007900 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007901out:
7902 btrfs_end_transaction(trans, root);
7903 return ret;
7904}
7905
Chris Masonc87f08c2011-02-16 13:57:04 -05007906int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7907 struct btrfs_root *root, u64 type)
7908{
7909 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007910 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007911 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007912}
7913
Miao Xie6d07bce2011-01-05 10:07:31 +00007914/*
7915 * helper to account the unused space of all the readonly block group in the
7916 * list. takes mirrors into account.
7917 */
7918static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7919{
7920 struct btrfs_block_group_cache *block_group;
7921 u64 free_bytes = 0;
7922 int factor;
7923
7924 list_for_each_entry(block_group, groups_list, list) {
7925 spin_lock(&block_group->lock);
7926
7927 if (!block_group->ro) {
7928 spin_unlock(&block_group->lock);
7929 continue;
7930 }
7931
7932 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7933 BTRFS_BLOCK_GROUP_RAID10 |
7934 BTRFS_BLOCK_GROUP_DUP))
7935 factor = 2;
7936 else
7937 factor = 1;
7938
7939 free_bytes += (block_group->key.offset -
7940 btrfs_block_group_used(&block_group->item)) *
7941 factor;
7942
7943 spin_unlock(&block_group->lock);
7944 }
7945
7946 return free_bytes;
7947}
7948
7949/*
7950 * helper to account the unused space of all the readonly block group in the
7951 * space_info. takes mirrors into account.
7952 */
7953u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7954{
7955 int i;
7956 u64 free_bytes = 0;
7957
7958 spin_lock(&sinfo->lock);
7959
7960 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7961 if (!list_empty(&sinfo->block_groups[i]))
7962 free_bytes += __btrfs_get_ro_block_group_free_space(
7963 &sinfo->block_groups[i]);
7964
7965 spin_unlock(&sinfo->lock);
7966
7967 return free_bytes;
7968}
7969
Jeff Mahoney143bede2012-03-01 14:56:26 +01007970void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007971 struct btrfs_block_group_cache *cache)
7972{
7973 struct btrfs_space_info *sinfo = cache->space_info;
7974 u64 num_bytes;
7975
7976 BUG_ON(!cache->ro);
7977
7978 spin_lock(&sinfo->lock);
7979 spin_lock(&cache->lock);
7980 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7981 cache->bytes_super - btrfs_block_group_used(&cache->item);
7982 sinfo->bytes_readonly -= num_bytes;
7983 cache->ro = 0;
7984 spin_unlock(&cache->lock);
7985 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007986}
7987
Josef Bacikba1bf482009-09-11 16:11:19 -04007988/*
7989 * checks to see if its even possible to relocate this block group.
7990 *
7991 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7992 * ok to go ahead and try.
7993 */
7994int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04007995{
Zheng Yan1a40e232008-09-26 10:09:34 -04007996 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04007997 struct btrfs_space_info *space_info;
7998 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7999 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008000 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008001 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008002 u64 dev_min = 1;
8003 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008004 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008005 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008006 int full = 0;
8007 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008008
Josef Bacikba1bf482009-09-11 16:11:19 -04008009 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008010
Josef Bacikba1bf482009-09-11 16:11:19 -04008011 /* odd, couldn't find the block group, leave it alone */
8012 if (!block_group)
8013 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008014
liubocdcb7252011-08-03 10:15:25 +00008015 min_free = btrfs_block_group_used(&block_group->item);
8016
Josef Bacikba1bf482009-09-11 16:11:19 -04008017 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008018 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008019 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008020
Josef Bacikba1bf482009-09-11 16:11:19 -04008021 space_info = block_group->space_info;
8022 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008023
Josef Bacikba1bf482009-09-11 16:11:19 -04008024 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008025
Josef Bacikba1bf482009-09-11 16:11:19 -04008026 /*
8027 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008028 * relocate it unless we're able to allocate a new chunk below.
8029 *
8030 * Otherwise, we need to make sure we have room in the space to handle
8031 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008032 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008033 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008034 (space_info->bytes_used + space_info->bytes_reserved +
8035 space_info->bytes_pinned + space_info->bytes_readonly +
8036 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008037 spin_unlock(&space_info->lock);
8038 goto out;
8039 }
8040 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008041
Josef Bacikba1bf482009-09-11 16:11:19 -04008042 /*
8043 * ok we don't have enough space, but maybe we have free space on our
8044 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008045 * alloc devices and guess if we have enough space. if this block
8046 * group is going to be restriped, run checks against the target
8047 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008048 */
8049 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008050
liubocdcb7252011-08-03 10:15:25 +00008051 /*
8052 * index:
8053 * 0: raid10
8054 * 1: raid1
8055 * 2: dup
8056 * 3: raid0
8057 * 4: single
8058 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008059 target = get_restripe_target(root->fs_info, block_group->flags);
8060 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008061 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008062 } else {
8063 /*
8064 * this is just a balance, so if we were marked as full
8065 * we know there is no space for a new chunk
8066 */
8067 if (full)
8068 goto out;
8069
8070 index = get_block_group_index(block_group);
8071 }
8072
Miao Xiee6ec7162013-01-17 05:38:51 +00008073 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008074 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008075 /* Divide by 2 */
8076 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008077 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008078 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008079 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008080 /* Multiply by 2 */
8081 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008082 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008083 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008084 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008085 }
8086
Josef Bacik6df9a952013-06-27 13:22:46 -04008087 /* We need to do this so that we can look at pending chunks */
8088 trans = btrfs_join_transaction(root);
8089 if (IS_ERR(trans)) {
8090 ret = PTR_ERR(trans);
8091 goto out;
8092 }
8093
Josef Bacikba1bf482009-09-11 16:11:19 -04008094 mutex_lock(&root->fs_info->chunk_mutex);
8095 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008096 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008097
Josef Bacikba1bf482009-09-11 16:11:19 -04008098 /*
8099 * check to make sure we can actually find a chunk with enough
8100 * space to fit our block group in.
8101 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008102 if (device->total_bytes > device->bytes_used + min_free &&
8103 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008104 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008105 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008106 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008107 dev_nr++;
8108
8109 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008110 break;
liubocdcb7252011-08-03 10:15:25 +00008111
Josef Bacikba1bf482009-09-11 16:11:19 -04008112 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008113 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008114 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008115 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008116 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008117out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008118 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008119 return ret;
8120}
8121
Christoph Hellwigb2950862008-12-02 09:54:17 -05008122static int find_first_block_group(struct btrfs_root *root,
8123 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008124{
Chris Mason925baed2008-06-25 16:01:30 -04008125 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008126 struct btrfs_key found_key;
8127 struct extent_buffer *leaf;
8128 int slot;
8129
8130 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8131 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008132 goto out;
8133
Chris Masond3977122009-01-05 21:25:51 -05008134 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008135 slot = path->slots[0];
8136 leaf = path->nodes[0];
8137 if (slot >= btrfs_header_nritems(leaf)) {
8138 ret = btrfs_next_leaf(root, path);
8139 if (ret == 0)
8140 continue;
8141 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008142 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008143 break;
8144 }
8145 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8146
8147 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008148 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8149 ret = 0;
8150 goto out;
8151 }
Chris Mason0b86a832008-03-24 15:01:56 -04008152 path->slots[0]++;
8153 }
Chris Mason925baed2008-06-25 16:01:30 -04008154out:
Chris Mason0b86a832008-03-24 15:01:56 -04008155 return ret;
8156}
8157
Josef Bacik0af3d002010-06-21 14:48:16 -04008158void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8159{
8160 struct btrfs_block_group_cache *block_group;
8161 u64 last = 0;
8162
8163 while (1) {
8164 struct inode *inode;
8165
8166 block_group = btrfs_lookup_first_block_group(info, last);
8167 while (block_group) {
8168 spin_lock(&block_group->lock);
8169 if (block_group->iref)
8170 break;
8171 spin_unlock(&block_group->lock);
8172 block_group = next_block_group(info->tree_root,
8173 block_group);
8174 }
8175 if (!block_group) {
8176 if (last == 0)
8177 break;
8178 last = 0;
8179 continue;
8180 }
8181
8182 inode = block_group->inode;
8183 block_group->iref = 0;
8184 block_group->inode = NULL;
8185 spin_unlock(&block_group->lock);
8186 iput(inode);
8187 last = block_group->key.objectid + block_group->key.offset;
8188 btrfs_put_block_group(block_group);
8189 }
8190}
8191
Zheng Yan1a40e232008-09-26 10:09:34 -04008192int btrfs_free_block_groups(struct btrfs_fs_info *info)
8193{
8194 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008195 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008196 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008197 struct rb_node *n;
8198
Yan Zheng11833d62009-09-11 16:11:19 -04008199 down_write(&info->extent_commit_sem);
8200 while (!list_empty(&info->caching_block_groups)) {
8201 caching_ctl = list_entry(info->caching_block_groups.next,
8202 struct btrfs_caching_control, list);
8203 list_del(&caching_ctl->list);
8204 put_caching_control(caching_ctl);
8205 }
8206 up_write(&info->extent_commit_sem);
8207
Zheng Yan1a40e232008-09-26 10:09:34 -04008208 spin_lock(&info->block_group_cache_lock);
8209 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8210 block_group = rb_entry(n, struct btrfs_block_group_cache,
8211 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008212 rb_erase(&block_group->cache_node,
8213 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008214 spin_unlock(&info->block_group_cache_lock);
8215
Josef Bacik80eb2342008-10-29 14:49:05 -04008216 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008217 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008218 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008219
Josef Bacik817d52f2009-07-13 21:29:25 -04008220 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008221 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008222
Josef Bacik3c148742011-02-02 15:53:47 +00008223 /*
8224 * We haven't cached this block group, which means we could
8225 * possibly have excluded extents on this block group.
8226 */
8227 if (block_group->cached == BTRFS_CACHE_NO)
8228 free_excluded_extents(info->extent_root, block_group);
8229
Josef Bacik817d52f2009-07-13 21:29:25 -04008230 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008231 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008232
8233 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008234 }
8235 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008236
8237 /* now that all the block groups are freed, go through and
8238 * free all the space_info structs. This is only called during
8239 * the final stages of unmount, and so we know nobody is
8240 * using them. We call synchronize_rcu() once before we start,
8241 * just to be on the safe side.
8242 */
8243 synchronize_rcu();
8244
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008245 release_global_block_rsv(info);
8246
Chris Mason4184ea72009-03-10 12:39:20 -04008247 while(!list_empty(&info->space_info)) {
8248 space_info = list_entry(info->space_info.next,
8249 struct btrfs_space_info,
8250 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008251 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8252 if (space_info->bytes_pinned > 0 ||
8253 space_info->bytes_reserved > 0 ||
8254 space_info->bytes_may_use > 0) {
8255 WARN_ON(1);
8256 dump_space_info(space_info, 0, 0);
8257 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008258 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04008259 percpu_counter_destroy(&space_info->total_bytes_pinned);
Chris Mason4184ea72009-03-10 12:39:20 -04008260 list_del(&space_info->list);
8261 kfree(space_info);
8262 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008263 return 0;
8264}
8265
Yan, Zhengb742bb82010-05-16 10:46:24 -04008266static void __link_block_group(struct btrfs_space_info *space_info,
8267 struct btrfs_block_group_cache *cache)
8268{
8269 int index = get_block_group_index(cache);
8270
8271 down_write(&space_info->groups_sem);
8272 list_add_tail(&cache->list, &space_info->block_groups[index]);
8273 up_write(&space_info->groups_sem);
8274}
8275
Chris Mason9078a3e2007-04-26 16:46:15 -04008276int btrfs_read_block_groups(struct btrfs_root *root)
8277{
8278 struct btrfs_path *path;
8279 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008280 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008281 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008282 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008283 struct btrfs_key key;
8284 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008285 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008286 int need_clear = 0;
8287 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008288
Chris Masonbe744172007-05-06 10:15:01 -04008289 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008290 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008291 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008292 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008293 path = btrfs_alloc_path();
8294 if (!path)
8295 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008296 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008297
David Sterba6c417612011-04-13 15:41:04 +02008298 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008299 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008300 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008301 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008302 if (btrfs_test_opt(root, CLEAR_CACHE))
8303 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008304
Chris Masond3977122009-01-05 21:25:51 -05008305 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008306 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008307 if (ret > 0)
8308 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008309 if (ret != 0)
8310 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008311 leaf = path->nodes[0];
8312 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008313 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008314 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008315 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008316 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008317 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008318 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8319 GFP_NOFS);
8320 if (!cache->free_space_ctl) {
8321 kfree(cache);
8322 ret = -ENOMEM;
8323 goto error;
8324 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008325
Yan Zhengd2fb3432008-12-11 16:30:39 -05008326 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008327 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008328 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008329 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008330 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008331
Liu Bocf7c1ef2012-07-06 03:31:34 -06008332 if (need_clear) {
8333 /*
8334 * When we mount with old space cache, we need to
8335 * set BTRFS_DC_CLEAR and set dirty flag.
8336 *
8337 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8338 * truncate the old free space cache inode and
8339 * setup a new one.
8340 * b) Setting 'dirty flag' makes sure that we flush
8341 * the new space cache info onto disk.
8342 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008343 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008344 if (btrfs_test_opt(root, SPACE_CACHE))
8345 cache->dirty = 1;
8346 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008347
Chris Mason5f39d392007-10-15 16:14:19 -04008348 read_extent_buffer(leaf, &cache->item,
8349 btrfs_item_ptr_offset(leaf, path->slots[0]),
8350 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008351 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008352
Chris Mason9078a3e2007-04-26 16:46:15 -04008353 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008354 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008355 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008356 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008357 cache->full_stripe_len = btrfs_full_stripe_len(root,
8358 &root->fs_info->mapping_tree,
8359 found_key.objectid);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008360 btrfs_init_free_space_ctl(cache);
8361
Josef Bacik817d52f2009-07-13 21:29:25 -04008362 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008363 * We need to exclude the super stripes now so that the space
8364 * info has super bytes accounted for, otherwise we'll think
8365 * we have more space than we actually do.
8366 */
Josef Bacik835d9742013-03-19 12:13:25 -04008367 ret = exclude_super_stripes(root, cache);
8368 if (ret) {
8369 /*
8370 * We may have excluded something, so call this just in
8371 * case.
8372 */
8373 free_excluded_extents(root, cache);
8374 kfree(cache->free_space_ctl);
8375 kfree(cache);
8376 goto error;
8377 }
Josef Bacik3c148742011-02-02 15:53:47 +00008378
8379 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008380 * check for two cases, either we are full, and therefore
8381 * don't need to bother with the caching work since we won't
8382 * find any space, or we are empty, and we can just add all
8383 * the space in and be done with it. This saves us _alot_ of
8384 * time, particularly in the full case.
8385 */
8386 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008387 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008388 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008389 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008390 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008391 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008392 cache->cached = BTRFS_CACHE_FINISHED;
8393 add_new_free_space(cache, root->fs_info,
8394 found_key.objectid,
8395 found_key.objectid +
8396 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008397 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008398 }
Chris Mason96b51792007-10-15 16:15:19 -04008399
Josef Bacik8c579fe2013-04-02 12:40:42 -04008400 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8401 if (ret) {
8402 btrfs_remove_free_space_cache(cache);
8403 btrfs_put_block_group(cache);
8404 goto error;
8405 }
8406
Chris Mason6324fbf2008-03-24 15:01:59 -04008407 ret = update_space_info(info, cache->flags, found_key.offset,
8408 btrfs_block_group_used(&cache->item),
8409 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008410 if (ret) {
8411 btrfs_remove_free_space_cache(cache);
8412 spin_lock(&info->block_group_cache_lock);
8413 rb_erase(&cache->cache_node,
8414 &info->block_group_cache_tree);
8415 spin_unlock(&info->block_group_cache_lock);
8416 btrfs_put_block_group(cache);
8417 goto error;
8418 }
8419
Chris Mason6324fbf2008-03-24 15:01:59 -04008420 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008421 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008422 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008423 spin_unlock(&cache->space_info->lock);
8424
Yan, Zhengb742bb82010-05-16 10:46:24 -04008425 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008426
Chris Mason75ccf472008-09-30 19:24:06 -04008427 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008428 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008429 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008430 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008431
8432 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8433 if (!(get_alloc_profile(root, space_info->flags) &
8434 (BTRFS_BLOCK_GROUP_RAID10 |
8435 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008436 BTRFS_BLOCK_GROUP_RAID5 |
8437 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008438 BTRFS_BLOCK_GROUP_DUP)))
8439 continue;
8440 /*
8441 * avoid allocating from un-mirrored block group if there are
8442 * mirrored block groups.
8443 */
chandan1095cc02013-07-16 12:28:56 +05308444 list_for_each_entry(cache,
8445 &space_info->block_groups[BTRFS_RAID_RAID0],
8446 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008447 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05308448 list_for_each_entry(cache,
8449 &space_info->block_groups[BTRFS_RAID_SINGLE],
8450 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008451 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008452 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008453
8454 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008455 ret = 0;
8456error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008457 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008458 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008459}
Chris Mason6324fbf2008-03-24 15:01:59 -04008460
Josef Bacikea658ba2012-09-11 16:57:25 -04008461void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8462 struct btrfs_root *root)
8463{
8464 struct btrfs_block_group_cache *block_group, *tmp;
8465 struct btrfs_root *extent_root = root->fs_info->extent_root;
8466 struct btrfs_block_group_item item;
8467 struct btrfs_key key;
8468 int ret = 0;
8469
8470 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8471 new_bg_list) {
8472 list_del_init(&block_group->new_bg_list);
8473
8474 if (ret)
8475 continue;
8476
8477 spin_lock(&block_group->lock);
8478 memcpy(&item, &block_group->item, sizeof(item));
8479 memcpy(&key, &block_group->key, sizeof(key));
8480 spin_unlock(&block_group->lock);
8481
8482 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8483 sizeof(item));
8484 if (ret)
8485 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04008486 ret = btrfs_finish_chunk_alloc(trans, extent_root,
8487 key.objectid, key.offset);
8488 if (ret)
8489 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04008490 }
8491}
8492
Chris Mason6324fbf2008-03-24 15:01:59 -04008493int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8494 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008495 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008496 u64 size)
8497{
8498 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008499 struct btrfs_root *extent_root;
8500 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008501
8502 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008503
Chris Mason12fcfd22009-03-24 10:24:20 -04008504 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008505
Chris Mason8f18cf12008-04-25 16:53:30 -04008506 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008507 if (!cache)
8508 return -ENOMEM;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008509 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8510 GFP_NOFS);
8511 if (!cache->free_space_ctl) {
8512 kfree(cache);
8513 return -ENOMEM;
8514 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008515
Chris Masone17cade2008-04-15 15:41:47 -04008516 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008517 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008518 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008519 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008520 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008521 cache->full_stripe_len = btrfs_full_stripe_len(root,
8522 &root->fs_info->mapping_tree,
8523 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008524
Yan Zhengd2fb3432008-12-11 16:30:39 -05008525 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008526 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008527 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04008528 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008529 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008530
Li Zefan34d52cb6c2011-03-29 13:46:06 +08008531 btrfs_init_free_space_ctl(cache);
8532
Chris Mason6324fbf2008-03-24 15:01:59 -04008533 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008534 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8535 cache->flags = type;
8536 btrfs_set_block_group_flags(&cache->item, type);
8537
Yan Zheng11833d62009-09-11 16:11:19 -04008538 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008539 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008540 ret = exclude_super_stripes(root, cache);
8541 if (ret) {
8542 /*
8543 * We may have excluded something, so call this just in
8544 * case.
8545 */
8546 free_excluded_extents(root, cache);
8547 kfree(cache->free_space_ctl);
8548 kfree(cache);
8549 return ret;
8550 }
Josef Bacik96303082009-07-13 21:29:25 -04008551
Josef Bacik817d52f2009-07-13 21:29:25 -04008552 add_new_free_space(cache, root->fs_info, chunk_offset,
8553 chunk_offset + size);
8554
Yan Zheng11833d62009-09-11 16:11:19 -04008555 free_excluded_extents(root, cache);
8556
Josef Bacik8c579fe2013-04-02 12:40:42 -04008557 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8558 if (ret) {
8559 btrfs_remove_free_space_cache(cache);
8560 btrfs_put_block_group(cache);
8561 return ret;
8562 }
8563
Chris Mason6324fbf2008-03-24 15:01:59 -04008564 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8565 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008566 if (ret) {
8567 btrfs_remove_free_space_cache(cache);
8568 spin_lock(&root->fs_info->block_group_cache_lock);
8569 rb_erase(&cache->cache_node,
8570 &root->fs_info->block_group_cache_tree);
8571 spin_unlock(&root->fs_info->block_group_cache_lock);
8572 btrfs_put_block_group(cache);
8573 return ret;
8574 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008575 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008576
8577 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008578 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008579 spin_unlock(&cache->space_info->lock);
8580
Yan, Zhengb742bb82010-05-16 10:46:24 -04008581 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008582
Josef Bacikea658ba2012-09-11 16:57:25 -04008583 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008584
Chris Masond18a2c42008-04-04 15:40:00 -04008585 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008586
Chris Mason6324fbf2008-03-24 15:01:59 -04008587 return 0;
8588}
Zheng Yan1a40e232008-09-26 10:09:34 -04008589
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008590static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8591{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008592 u64 extra_flags = chunk_to_extended(flags) &
8593 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008594
Miao Xiede98ced2013-01-29 10:13:12 +00008595 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008596 if (flags & BTRFS_BLOCK_GROUP_DATA)
8597 fs_info->avail_data_alloc_bits &= ~extra_flags;
8598 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8599 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8600 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8601 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008602 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008603}
8604
Zheng Yan1a40e232008-09-26 10:09:34 -04008605int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8606 struct btrfs_root *root, u64 group_start)
8607{
8608 struct btrfs_path *path;
8609 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008610 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008611 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008612 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008613 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008614 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008615 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008616 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008617
Zheng Yan1a40e232008-09-26 10:09:34 -04008618 root = root->fs_info->extent_root;
8619
8620 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8621 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008622 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008623
liubo9f7c43c2011-03-07 02:13:33 +00008624 /*
8625 * Free the reserved super bytes from this block group before
8626 * remove it.
8627 */
8628 free_excluded_extents(root, block_group);
8629
Zheng Yan1a40e232008-09-26 10:09:34 -04008630 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008631 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008632 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8633 BTRFS_BLOCK_GROUP_RAID1 |
8634 BTRFS_BLOCK_GROUP_RAID10))
8635 factor = 2;
8636 else
8637 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008638
Chris Mason44fb5512009-06-04 15:34:51 -04008639 /* make sure this block group isn't part of an allocation cluster */
8640 cluster = &root->fs_info->data_alloc_cluster;
8641 spin_lock(&cluster->refill_lock);
8642 btrfs_return_cluster_to_free_space(block_group, cluster);
8643 spin_unlock(&cluster->refill_lock);
8644
8645 /*
8646 * make sure this block group isn't part of a metadata
8647 * allocation cluster
8648 */
8649 cluster = &root->fs_info->meta_alloc_cluster;
8650 spin_lock(&cluster->refill_lock);
8651 btrfs_return_cluster_to_free_space(block_group, cluster);
8652 spin_unlock(&cluster->refill_lock);
8653
Zheng Yan1a40e232008-09-26 10:09:34 -04008654 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008655 if (!path) {
8656 ret = -ENOMEM;
8657 goto out;
8658 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008659
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008660 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008661 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008662 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008663 if (ret) {
8664 btrfs_add_delayed_iput(inode);
8665 goto out;
8666 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008667 clear_nlink(inode);
8668 /* One for the block groups ref */
8669 spin_lock(&block_group->lock);
8670 if (block_group->iref) {
8671 block_group->iref = 0;
8672 block_group->inode = NULL;
8673 spin_unlock(&block_group->lock);
8674 iput(inode);
8675 } else {
8676 spin_unlock(&block_group->lock);
8677 }
8678 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008679 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008680 }
8681
8682 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8683 key.offset = block_group->key.objectid;
8684 key.type = 0;
8685
8686 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8687 if (ret < 0)
8688 goto out;
8689 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008690 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008691 if (ret == 0) {
8692 ret = btrfs_del_item(trans, tree_root, path);
8693 if (ret)
8694 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008695 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008696 }
8697
Yan Zheng3dfdb932009-01-21 10:49:16 -05008698 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008699 rb_erase(&block_group->cache_node,
8700 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008701
8702 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8703 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008704 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008705
Josef Bacik80eb2342008-10-29 14:49:05 -04008706 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008707 /*
8708 * we must use list_del_init so people can check to see if they
8709 * are still on the list after taking the semaphore
8710 */
8711 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008712 if (list_empty(&block_group->space_info->block_groups[index]))
8713 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008714 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008715
Josef Bacik817d52f2009-07-13 21:29:25 -04008716 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008717 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008718
8719 btrfs_remove_free_space_cache(block_group);
8720
Yan Zhengc146afa2008-11-12 14:34:12 -05008721 spin_lock(&block_group->space_info->lock);
8722 block_group->space_info->total_bytes -= block_group->key.offset;
8723 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008724 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008725 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008726
Josef Bacik0af3d002010-06-21 14:48:16 -04008727 memcpy(&key, &block_group->key, sizeof(key));
8728
Chris Mason283bb192009-07-24 16:30:55 -04008729 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008730
Chris Masonfa9c0d72009-04-03 09:47:43 -04008731 btrfs_put_block_group(block_group);
8732 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008733
8734 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8735 if (ret > 0)
8736 ret = -EIO;
8737 if (ret < 0)
8738 goto out;
8739
8740 ret = btrfs_del_item(trans, root, path);
8741out:
8742 btrfs_free_path(path);
8743 return ret;
8744}
liuboacce9522011-01-06 19:30:25 +08008745
liuboc59021f2011-03-07 02:13:14 +00008746int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8747{
8748 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008749 struct btrfs_super_block *disk_super;
8750 u64 features;
8751 u64 flags;
8752 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008753 int ret;
8754
David Sterba6c417612011-04-13 15:41:04 +02008755 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008756 if (!btrfs_super_root(disk_super))
8757 return 1;
liuboc59021f2011-03-07 02:13:14 +00008758
liubo1aba86d2011-04-08 08:44:37 +00008759 features = btrfs_super_incompat_flags(disk_super);
8760 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8761 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008762
liubo1aba86d2011-04-08 08:44:37 +00008763 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8764 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008765 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008766 goto out;
liuboc59021f2011-03-07 02:13:14 +00008767
liubo1aba86d2011-04-08 08:44:37 +00008768 if (mixed) {
8769 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8770 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8771 } else {
8772 flags = BTRFS_BLOCK_GROUP_METADATA;
8773 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8774 if (ret)
8775 goto out;
8776
8777 flags = BTRFS_BLOCK_GROUP_DATA;
8778 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8779 }
8780out:
liuboc59021f2011-03-07 02:13:14 +00008781 return ret;
8782}
8783
liuboacce9522011-01-06 19:30:25 +08008784int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8785{
8786 return unpin_extent_range(root, start, end);
8787}
8788
8789int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008790 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008791{
Li Dongyang5378e602011-03-24 10:24:27 +00008792 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008793}
Li Dongyangf7039b12011-03-24 10:24:28 +00008794
8795int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8796{
8797 struct btrfs_fs_info *fs_info = root->fs_info;
8798 struct btrfs_block_group_cache *cache = NULL;
8799 u64 group_trimmed;
8800 u64 start;
8801 u64 end;
8802 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008803 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008804 int ret = 0;
8805
Liu Bo2cac13e2012-02-09 18:17:41 +08008806 /*
8807 * try to trim all FS space, our block group may start from non-zero.
8808 */
8809 if (range->len == total_bytes)
8810 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8811 else
8812 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008813
8814 while (cache) {
8815 if (cache->key.objectid >= (range->start + range->len)) {
8816 btrfs_put_block_group(cache);
8817 break;
8818 }
8819
8820 start = max(range->start, cache->key.objectid);
8821 end = min(range->start + range->len,
8822 cache->key.objectid + cache->key.offset);
8823
8824 if (end - start >= range->minlen) {
8825 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008826 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04008827 if (ret) {
8828 btrfs_put_block_group(cache);
8829 break;
8830 }
8831 ret = wait_block_group_cache_done(cache);
8832 if (ret) {
8833 btrfs_put_block_group(cache);
8834 break;
8835 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008836 }
8837 ret = btrfs_trim_block_group(cache,
8838 &group_trimmed,
8839 start,
8840 end,
8841 range->minlen);
8842
8843 trimmed += group_trimmed;
8844 if (ret) {
8845 btrfs_put_block_group(cache);
8846 break;
8847 }
8848 }
8849
8850 cache = next_block_group(fs_info->tree_root, cache);
8851 }
8852
8853 range->len = trimmed;
8854 return ret;
8855}