blob: ca02d3c1724c980ce4031026eee91016b9df2b0a [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 Masonfa9c0d792009-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 Zefan34d52cb2011-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);
Liu Bo599c75e2013-07-16 19:03:36 +08002038 trace_run_delayed_data_ref(node, ref, node->action);
2039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002040 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2041 parent = ref->parent;
2042 else
2043 ref_root = ref->root;
2044
2045 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002046 if (extent_op)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002047 flags |= extent_op->flags_to_set;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002048 ret = alloc_reserved_file_extent(trans, root,
2049 parent, ref_root, flags,
2050 ref->objectid, ref->offset,
2051 &ins, node->ref_mod);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002052 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2053 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2054 node->num_bytes, parent,
2055 ref_root, ref->objectid,
2056 ref->offset, node->ref_mod,
2057 extent_op);
2058 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2059 ret = __btrfs_free_extent(trans, root, node->bytenr,
2060 node->num_bytes, parent,
2061 ref_root, ref->objectid,
2062 ref->offset, node->ref_mod,
2063 extent_op);
2064 } else {
2065 BUG();
2066 }
Chris Mason56bec292009-03-13 10:10:06 -04002067 return ret;
2068}
2069
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002070static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2071 struct extent_buffer *leaf,
2072 struct btrfs_extent_item *ei)
2073{
2074 u64 flags = btrfs_extent_flags(leaf, ei);
2075 if (extent_op->update_flags) {
2076 flags |= extent_op->flags_to_set;
2077 btrfs_set_extent_flags(leaf, ei, flags);
2078 }
2079
2080 if (extent_op->update_key) {
2081 struct btrfs_tree_block_info *bi;
2082 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2083 bi = (struct btrfs_tree_block_info *)(ei + 1);
2084 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2085 }
2086}
2087
2088static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2089 struct btrfs_root *root,
2090 struct btrfs_delayed_ref_node *node,
2091 struct btrfs_delayed_extent_op *extent_op)
2092{
2093 struct btrfs_key key;
2094 struct btrfs_path *path;
2095 struct btrfs_extent_item *ei;
2096 struct extent_buffer *leaf;
2097 u32 item_size;
2098 int ret;
2099 int err = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002100 int metadata = !extent_op->is_data;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002101
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002102 if (trans->aborted)
2103 return 0;
2104
Josef Bacik3173a182013-03-07 14:22:04 -05002105 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2106 metadata = 0;
2107
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002108 path = btrfs_alloc_path();
2109 if (!path)
2110 return -ENOMEM;
2111
2112 key.objectid = node->bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002113
Josef Bacik3173a182013-03-07 14:22:04 -05002114 if (metadata) {
Josef Bacik3173a182013-03-07 14:22:04 -05002115 key.type = BTRFS_METADATA_ITEM_KEY;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002116 key.offset = extent_op->level;
Josef Bacik3173a182013-03-07 14:22:04 -05002117 } else {
2118 key.type = BTRFS_EXTENT_ITEM_KEY;
2119 key.offset = node->num_bytes;
2120 }
2121
2122again:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002123 path->reada = 1;
2124 path->leave_spinning = 1;
2125 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2126 path, 0, 1);
2127 if (ret < 0) {
2128 err = ret;
2129 goto out;
2130 }
2131 if (ret > 0) {
Josef Bacik3173a182013-03-07 14:22:04 -05002132 if (metadata) {
2133 btrfs_release_path(path);
2134 metadata = 0;
2135
2136 key.offset = node->num_bytes;
2137 key.type = BTRFS_EXTENT_ITEM_KEY;
2138 goto again;
2139 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002140 err = -EIO;
2141 goto out;
2142 }
2143
2144 leaf = path->nodes[0];
2145 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2146#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2147 if (item_size < sizeof(*ei)) {
2148 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2149 path, (u64)-1, 0);
2150 if (ret < 0) {
2151 err = ret;
2152 goto out;
2153 }
2154 leaf = path->nodes[0];
2155 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2156 }
2157#endif
2158 BUG_ON(item_size < sizeof(*ei));
2159 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2160 __run_delayed_extent_op(extent_op, leaf, ei);
2161
2162 btrfs_mark_buffer_dirty(leaf);
2163out:
2164 btrfs_free_path(path);
2165 return err;
2166}
2167
2168static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2169 struct btrfs_root *root,
2170 struct btrfs_delayed_ref_node *node,
2171 struct btrfs_delayed_extent_op *extent_op,
2172 int insert_reserved)
2173{
2174 int ret = 0;
2175 struct btrfs_delayed_tree_ref *ref;
2176 struct btrfs_key ins;
2177 u64 parent = 0;
2178 u64 ref_root = 0;
Josef Bacik3173a182013-03-07 14:22:04 -05002179 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2180 SKINNY_METADATA);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002181
2182 ref = btrfs_delayed_node_to_tree_ref(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002183 trace_run_delayed_tree_ref(node, ref, node->action);
2184
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002185 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2186 parent = ref->parent;
2187 else
2188 ref_root = ref->root;
2189
Josef Bacik3173a182013-03-07 14:22:04 -05002190 ins.objectid = node->bytenr;
2191 if (skinny_metadata) {
2192 ins.offset = ref->level;
2193 ins.type = BTRFS_METADATA_ITEM_KEY;
2194 } else {
2195 ins.offset = node->num_bytes;
2196 ins.type = BTRFS_EXTENT_ITEM_KEY;
2197 }
2198
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002199 BUG_ON(node->ref_mod != 1);
2200 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
Josef Bacik3173a182013-03-07 14:22:04 -05002201 BUG_ON(!extent_op || !extent_op->update_flags);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002202 ret = alloc_reserved_tree_block(trans, root,
2203 parent, ref_root,
2204 extent_op->flags_to_set,
2205 &extent_op->key,
2206 ref->level, &ins);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002207 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2208 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2209 node->num_bytes, parent, ref_root,
2210 ref->level, 0, 1, extent_op);
2211 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2212 ret = __btrfs_free_extent(trans, root, node->bytenr,
2213 node->num_bytes, parent, ref_root,
2214 ref->level, 0, 1, extent_op);
2215 } else {
2216 BUG();
2217 }
2218 return ret;
2219}
2220
Chris Mason56bec292009-03-13 10:10:06 -04002221/* helper function to actually process a single delayed ref entry */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002222static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2223 struct btrfs_root *root,
2224 struct btrfs_delayed_ref_node *node,
2225 struct btrfs_delayed_extent_op *extent_op,
2226 int insert_reserved)
Chris Mason56bec292009-03-13 10:10:06 -04002227{
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002228 int ret = 0;
2229
2230 if (trans->aborted)
2231 return 0;
2232
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002233 if (btrfs_delayed_ref_is_head(node)) {
Chris Mason56bec292009-03-13 10:10:06 -04002234 struct btrfs_delayed_ref_head *head;
2235 /*
2236 * we've hit the end of the chain and we were supposed
2237 * to insert this extent into the tree. But, it got
2238 * deleted before we ever needed to insert it, so all
2239 * we have to do is clean up the accounting
2240 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002241 BUG_ON(extent_op);
2242 head = btrfs_delayed_node_to_head(node);
Liu Bo599c75e2013-07-16 19:03:36 +08002243 trace_run_delayed_ref_head(node, head, node->action);
2244
Chris Mason56bec292009-03-13 10:10:06 -04002245 if (insert_reserved) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04002246 btrfs_pin_extent(root, node->bytenr,
2247 node->num_bytes, 1);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002248 if (head->is_data) {
2249 ret = btrfs_del_csums(trans, root,
2250 node->bytenr,
2251 node->num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002252 }
Chris Mason56bec292009-03-13 10:10:06 -04002253 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002254 return ret;
Chris Mason56bec292009-03-13 10:10:06 -04002255 }
Josef Bacikeb099672009-02-12 09:27:38 -05002256
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002257 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2258 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2259 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2260 insert_reserved);
2261 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2262 node->type == BTRFS_SHARED_DATA_REF_KEY)
2263 ret = run_delayed_data_ref(trans, root, node, extent_op,
2264 insert_reserved);
2265 else
2266 BUG();
2267 return ret;
Chris Masone9d0b132007-08-10 14:06:19 -04002268}
2269
Chris Mason56bec292009-03-13 10:10:06 -04002270static noinline struct btrfs_delayed_ref_node *
2271select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -05002272{
Chris Mason56bec292009-03-13 10:10:06 -04002273 struct rb_node *node;
2274 struct btrfs_delayed_ref_node *ref;
2275 int action = BTRFS_ADD_DELAYED_REF;
2276again:
2277 /*
2278 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2279 * this prevents ref count from going down to zero when
2280 * there still are pending delayed ref.
2281 */
2282 node = rb_prev(&head->node.rb_node);
2283 while (1) {
2284 if (!node)
2285 break;
2286 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2287 rb_node);
2288 if (ref->bytenr != head->node.bytenr)
2289 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002290 if (ref->action == action)
Chris Mason56bec292009-03-13 10:10:06 -04002291 return ref;
2292 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -04002293 }
Chris Mason56bec292009-03-13 10:10:06 -04002294 if (action == BTRFS_ADD_DELAYED_REF) {
2295 action = BTRFS_DROP_DELAYED_REF;
2296 goto again;
2297 }
2298 return NULL;
2299}
2300
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002301/*
2302 * Returns 0 on success or if called with an already aborted transaction.
2303 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2304 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002305static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2306 struct btrfs_root *root,
2307 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -04002308{
Chris Mason56bec292009-03-13 10:10:06 -04002309 struct btrfs_delayed_ref_root *delayed_refs;
2310 struct btrfs_delayed_ref_node *ref;
2311 struct btrfs_delayed_ref_head *locked_ref = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002312 struct btrfs_delayed_extent_op *extent_op;
Jan Schmidt097b8a72012-06-21 11:08:04 +02002313 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason56bec292009-03-13 10:10:06 -04002314 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -04002315 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002316 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -04002317
2318 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -04002319 while (1) {
2320 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -04002321 /* pick a new head ref from the cluster list */
2322 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -04002323 break;
Chris Mason56bec292009-03-13 10:10:06 -04002324
Chris Masonc3e69d52009-03-13 10:17:05 -04002325 locked_ref = list_entry(cluster->next,
2326 struct btrfs_delayed_ref_head, cluster);
2327
2328 /* grab the lock that says we are going to process
2329 * all the refs for this head */
2330 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2331
2332 /*
2333 * we may have dropped the spin lock to get the head
2334 * mutex lock, and that might have given someone else
2335 * time to free the head. If that's true, it has been
2336 * removed from our list and we can move on.
2337 */
2338 if (ret == -EAGAIN) {
2339 locked_ref = NULL;
2340 count++;
2341 continue;
Chris Mason56bec292009-03-13 10:10:06 -04002342 }
2343 }
2344
2345 /*
Josef Bacikae1e2062012-08-07 16:00:32 -04002346 * We need to try and merge add/drops of the same ref since we
2347 * can run into issues with relocate dropping the implicit ref
2348 * and then it being added back again before the drop can
2349 * finish. If we merged anything we need to re-loop so we can
2350 * get a good ref.
2351 */
2352 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2353 locked_ref);
2354
2355 /*
Arne Jansend1270cd2011-09-13 15:16:43 +02002356 * locked_ref is the head node, so we have to go one
2357 * node back for any delayed ref updates
2358 */
2359 ref = select_delayed_ref(locked_ref);
2360
2361 if (ref && ref->seq &&
Jan Schmidt097b8a72012-06-21 11:08:04 +02002362 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
Arne Jansend1270cd2011-09-13 15:16:43 +02002363 /*
2364 * there are still refs with lower seq numbers in the
2365 * process of being added. Don't run this ref yet.
2366 */
2367 list_del_init(&locked_ref->cluster);
Miao Xie093486c2012-12-19 08:10:10 +00002368 btrfs_delayed_ref_unlock(locked_ref);
Arne Jansend1270cd2011-09-13 15:16:43 +02002369 locked_ref = NULL;
2370 delayed_refs->num_heads_ready++;
2371 spin_unlock(&delayed_refs->lock);
2372 cond_resched();
2373 spin_lock(&delayed_refs->lock);
2374 continue;
2375 }
2376
2377 /*
Chris Mason56bec292009-03-13 10:10:06 -04002378 * record the must insert reserved flag before we
2379 * drop the spin lock.
2380 */
2381 must_insert_reserved = locked_ref->must_insert_reserved;
2382 locked_ref->must_insert_reserved = 0;
2383
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002384 extent_op = locked_ref->extent_op;
2385 locked_ref->extent_op = NULL;
2386
Chris Mason56bec292009-03-13 10:10:06 -04002387 if (!ref) {
2388 /* All delayed refs have been processed, Go ahead
2389 * and send the head node to run_one_delayed_ref,
2390 * so that any accounting fixes can happen
2391 */
2392 ref = &locked_ref->node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002393
2394 if (extent_op && must_insert_reserved) {
Miao Xie78a61842012-11-21 02:21:28 +00002395 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002396 extent_op = NULL;
2397 }
2398
2399 if (extent_op) {
2400 spin_unlock(&delayed_refs->lock);
2401
2402 ret = run_delayed_extent_op(trans, root,
2403 ref, extent_op);
Miao Xie78a61842012-11-21 02:21:28 +00002404 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002405
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002406 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002407 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002408 spin_lock(&delayed_refs->lock);
Miao Xie093486c2012-12-19 08:10:10 +00002409 btrfs_delayed_ref_unlock(locked_ref);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002410 return ret;
2411 }
2412
Chris Mason203bf282012-01-06 15:23:57 -05002413 goto next;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002414 }
Chris Mason56bec292009-03-13 10:10:06 -04002415 }
2416
2417 ref->in_tree = 0;
2418 rb_erase(&ref->rb_node, &delayed_refs->root);
2419 delayed_refs->num_entries--;
Miao Xie093486c2012-12-19 08:10:10 +00002420 if (!btrfs_delayed_ref_is_head(ref)) {
Arne Jansen22cd2e72012-08-09 00:16:53 -06002421 /*
2422 * when we play the delayed ref, also correct the
2423 * ref_mod on head
2424 */
2425 switch (ref->action) {
2426 case BTRFS_ADD_DELAYED_REF:
2427 case BTRFS_ADD_DELAYED_EXTENT:
2428 locked_ref->node.ref_mod -= ref->ref_mod;
2429 break;
2430 case BTRFS_DROP_DELAYED_REF:
2431 locked_ref->node.ref_mod += ref->ref_mod;
2432 break;
2433 default:
2434 WARN_ON(1);
2435 }
2436 }
Chris Mason56bec292009-03-13 10:10:06 -04002437 spin_unlock(&delayed_refs->lock);
2438
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002439 ret = run_one_delayed_ref(trans, root, ref, extent_op,
Chris Mason56bec292009-03-13 10:10:06 -04002440 must_insert_reserved);
Chris Mason56bec292009-03-13 10:10:06 -04002441
Miao Xie78a61842012-11-21 02:21:28 +00002442 btrfs_free_delayed_extent_op(extent_op);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002443 if (ret) {
Miao Xie093486c2012-12-19 08:10:10 +00002444 btrfs_delayed_ref_unlock(locked_ref);
2445 btrfs_put_delayed_ref(ref);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002446 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
Dan Carpenter253beeb2012-04-18 09:59:03 +03002447 spin_lock(&delayed_refs->lock);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002448 return ret;
2449 }
2450
Miao Xie093486c2012-12-19 08:10:10 +00002451 /*
2452 * If this node is a head, that means all the refs in this head
2453 * have been dealt with, and we will pick the next head to deal
2454 * with, so we must unlock the head and drop it from the cluster
2455 * list before we release it.
2456 */
2457 if (btrfs_delayed_ref_is_head(ref)) {
2458 list_del_init(&locked_ref->cluster);
2459 btrfs_delayed_ref_unlock(locked_ref);
2460 locked_ref = NULL;
2461 }
2462 btrfs_put_delayed_ref(ref);
2463 count++;
Chris Mason203bf282012-01-06 15:23:57 -05002464next:
Chris Mason1887be62009-03-13 10:11:24 -04002465 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002466 spin_lock(&delayed_refs->lock);
2467 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002468 return count;
2469}
2470
Arne Jansen709c0482011-09-12 12:22:57 +02002471#ifdef SCRAMBLE_DELAYED_REFS
2472/*
2473 * Normally delayed refs get processed in ascending bytenr order. This
2474 * correlates in most cases to the order added. To expose dependencies on this
2475 * order, we start to process the tree in the middle instead of the beginning
2476 */
2477static u64 find_middle(struct rb_root *root)
2478{
2479 struct rb_node *n = root->rb_node;
2480 struct btrfs_delayed_ref_node *entry;
2481 int alt = 1;
2482 u64 middle;
2483 u64 first = 0, last = 0;
2484
2485 n = rb_first(root);
2486 if (n) {
2487 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2488 first = entry->bytenr;
2489 }
2490 n = rb_last(root);
2491 if (n) {
2492 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2493 last = entry->bytenr;
2494 }
2495 n = root->rb_node;
2496
2497 while (n) {
2498 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2499 WARN_ON(!entry->in_tree);
2500
2501 middle = entry->bytenr;
2502
2503 if (alt)
2504 n = n->rb_left;
2505 else
2506 n = n->rb_right;
2507
2508 alt = 1 - alt;
2509 }
2510 return middle;
2511}
2512#endif
2513
Arne Jansenbed92ea2012-06-28 18:03:02 +02002514int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2515 struct btrfs_fs_info *fs_info)
2516{
2517 struct qgroup_update *qgroup_update;
2518 int ret = 0;
2519
2520 if (list_empty(&trans->qgroup_ref_list) !=
2521 !trans->delayed_ref_elem.seq) {
2522 /* list without seq or seq without list */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00002523 btrfs_err(fs_info,
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002524 "qgroup accounting update error, list is%s empty, seq is %#x.%x",
Arne Jansenbed92ea2012-06-28 18:03:02 +02002525 list_empty(&trans->qgroup_ref_list) ? "" : " not",
Jan Schmidtfc36ed7e2013-04-24 16:57:33 +00002526 (u32)(trans->delayed_ref_elem.seq >> 32),
2527 (u32)trans->delayed_ref_elem.seq);
Arne Jansenbed92ea2012-06-28 18:03:02 +02002528 BUG();
2529 }
2530
2531 if (!trans->delayed_ref_elem.seq)
2532 return 0;
2533
2534 while (!list_empty(&trans->qgroup_ref_list)) {
2535 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2536 struct qgroup_update, list);
2537 list_del(&qgroup_update->list);
2538 if (!ret)
2539 ret = btrfs_qgroup_account_ref(
2540 trans, fs_info, qgroup_update->node,
2541 qgroup_update->extent_op);
2542 kfree(qgroup_update);
2543 }
2544
2545 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2546
2547 return ret;
2548}
2549
Chris Masonbb721702013-01-29 18:44:12 -05002550static int refs_newer(struct btrfs_delayed_ref_root *delayed_refs, int seq,
2551 int count)
2552{
2553 int val = atomic_read(&delayed_refs->ref_seq);
2554
2555 if (val < seq || val >= seq + count)
2556 return 1;
2557 return 0;
2558}
2559
Josef Bacik1be41b72013-06-12 13:56:06 -04002560static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2561{
2562 u64 num_bytes;
2563
2564 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2565 sizeof(struct btrfs_extent_inline_ref));
2566 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2567 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2568
2569 /*
2570 * We don't ever fill up leaves all the way so multiply by 2 just to be
2571 * closer to what we're really going to want to ouse.
2572 */
2573 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2574}
2575
2576int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2577 struct btrfs_root *root)
2578{
2579 struct btrfs_block_rsv *global_rsv;
2580 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2581 u64 num_bytes;
2582 int ret = 0;
2583
2584 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2585 num_heads = heads_to_leaves(root, num_heads);
2586 if (num_heads > 1)
2587 num_bytes += (num_heads - 1) * root->leafsize;
2588 num_bytes <<= 1;
2589 global_rsv = &root->fs_info->global_block_rsv;
2590
2591 /*
2592 * If we can't allocate any more chunks lets make sure we have _lots_ of
2593 * wiggle room since running delayed refs can create more delayed refs.
2594 */
2595 if (global_rsv->space_info->full)
2596 num_bytes <<= 1;
2597
2598 spin_lock(&global_rsv->lock);
2599 if (global_rsv->reserved <= num_bytes)
2600 ret = 1;
2601 spin_unlock(&global_rsv->lock);
2602 return ret;
2603}
2604
Chris Masonc3e69d52009-03-13 10:17:05 -04002605/*
2606 * this starts processing the delayed reference count updates and
2607 * extent insertions we have queued up so far. count can be
2608 * 0, which means to process everything in the tree at the start
2609 * of the run (but not newly added entries), or it can be some target
2610 * number you'd like to process.
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002611 *
2612 * Returns 0 on success or if called with an aborted transaction
2613 * Returns <0 on error and aborts the transaction
Chris Masonc3e69d52009-03-13 10:17:05 -04002614 */
2615int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2616 struct btrfs_root *root, unsigned long count)
2617{
2618 struct rb_node *node;
2619 struct btrfs_delayed_ref_root *delayed_refs;
2620 struct btrfs_delayed_ref_node *ref;
2621 struct list_head cluster;
2622 int ret;
Jan Schmidta1686502011-12-12 16:10:07 +01002623 u64 delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002624 int run_all = count == (unsigned long)-1;
2625 int run_most = 0;
Arne Jansen1fa11e22012-08-06 14:18:51 -06002626 int loops;
Chris Masonc3e69d52009-03-13 10:17:05 -04002627
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002628 /* We'll clean this up in btrfs_cleanup_transaction */
2629 if (trans->aborted)
2630 return 0;
2631
Chris Masonc3e69d52009-03-13 10:17:05 -04002632 if (root == root->fs_info->extent_root)
2633 root = root->fs_info->tree_root;
2634
Jan Schmidtedf39272012-06-28 18:04:55 +02002635 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2636
Chris Masonc3e69d52009-03-13 10:17:05 -04002637 delayed_refs = &trans->transaction->delayed_refs;
2638 INIT_LIST_HEAD(&cluster);
Chris Masonbb721702013-01-29 18:44:12 -05002639 if (count == 0) {
2640 count = delayed_refs->num_entries * 2;
2641 run_most = 1;
2642 }
2643
2644 if (!run_all && !run_most) {
2645 int old;
2646 int seq = atomic_read(&delayed_refs->ref_seq);
2647
2648progress:
2649 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2650 if (old) {
2651 DEFINE_WAIT(__wait);
Josef Bacik1be41b72013-06-12 13:56:06 -04002652 if (delayed_refs->flushing ||
2653 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonbb721702013-01-29 18:44:12 -05002654 return 0;
2655
2656 prepare_to_wait(&delayed_refs->wait, &__wait,
2657 TASK_UNINTERRUPTIBLE);
2658
2659 old = atomic_cmpxchg(&delayed_refs->procs_running_refs, 0, 1);
2660 if (old) {
2661 schedule();
2662 finish_wait(&delayed_refs->wait, &__wait);
2663
2664 if (!refs_newer(delayed_refs, seq, 256))
2665 goto progress;
2666 else
2667 return 0;
2668 } else {
2669 finish_wait(&delayed_refs->wait, &__wait);
2670 goto again;
2671 }
2672 }
2673
2674 } else {
2675 atomic_inc(&delayed_refs->procs_running_refs);
2676 }
2677
Chris Masonc3e69d52009-03-13 10:17:05 -04002678again:
Arne Jansen1fa11e22012-08-06 14:18:51 -06002679 loops = 0;
Chris Masonc3e69d52009-03-13 10:17:05 -04002680 spin_lock(&delayed_refs->lock);
Jan Schmidt097b8a72012-06-21 11:08:04 +02002681
Arne Jansen709c0482011-09-12 12:22:57 +02002682#ifdef SCRAMBLE_DELAYED_REFS
2683 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2684#endif
2685
Chris Masonc3e69d52009-03-13 10:17:05 -04002686 while (1) {
2687 if (!(run_all || run_most) &&
Josef Bacik1be41b72013-06-12 13:56:06 -04002688 !btrfs_should_throttle_delayed_refs(trans, root))
Chris Masonc3e69d52009-03-13 10:17:05 -04002689 break;
2690
2691 /*
2692 * go find something we can process in the rbtree. We start at
2693 * the beginning of the tree, and then build a cluster
2694 * of refs to process starting at the first one we are able to
2695 * lock
2696 */
Jan Schmidta1686502011-12-12 16:10:07 +01002697 delayed_start = delayed_refs->run_delayed_start;
Chris Masonc3e69d52009-03-13 10:17:05 -04002698 ret = btrfs_find_ref_cluster(trans, &cluster,
2699 delayed_refs->run_delayed_start);
2700 if (ret)
2701 break;
2702
2703 ret = run_clustered_refs(trans, root, &cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002704 if (ret < 0) {
Miao Xie093486c2012-12-19 08:10:10 +00002705 btrfs_release_ref_cluster(&cluster);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002706 spin_unlock(&delayed_refs->lock);
2707 btrfs_abort_transaction(trans, root, ret);
Chris Masonbb721702013-01-29 18:44:12 -05002708 atomic_dec(&delayed_refs->procs_running_refs);
Josef Bacikf971fe22013-06-10 11:52:32 -04002709 wake_up(&delayed_refs->wait);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002710 return ret;
2711 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002712
Chris Masonbb721702013-01-29 18:44:12 -05002713 atomic_add(ret, &delayed_refs->ref_seq);
2714
Chris Masonc3e69d52009-03-13 10:17:05 -04002715 count -= min_t(unsigned long, ret, count);
2716
2717 if (count == 0)
2718 break;
Jan Schmidta1686502011-12-12 16:10:07 +01002719
Arne Jansen1fa11e22012-08-06 14:18:51 -06002720 if (delayed_start >= delayed_refs->run_delayed_start) {
2721 if (loops == 0) {
2722 /*
2723 * btrfs_find_ref_cluster looped. let's do one
2724 * more cycle. if we don't run any delayed ref
2725 * during that cycle (because we can't because
2726 * all of them are blocked), bail out.
2727 */
2728 loops = 1;
2729 } else {
2730 /*
2731 * no runnable refs left, stop trying
2732 */
2733 BUG_ON(run_all);
2734 break;
2735 }
2736 }
2737 if (ret) {
Jan Schmidta1686502011-12-12 16:10:07 +01002738 /* refs were run, let's reset staleness detection */
Arne Jansen1fa11e22012-08-06 14:18:51 -06002739 loops = 0;
Jan Schmidta1686502011-12-12 16:10:07 +01002740 }
Chris Masonc3e69d52009-03-13 10:17:05 -04002741 }
2742
Chris Mason56bec292009-03-13 10:10:06 -04002743 if (run_all) {
Josef Bacikea658ba2012-09-11 16:57:25 -04002744 if (!list_empty(&trans->new_bgs)) {
2745 spin_unlock(&delayed_refs->lock);
2746 btrfs_create_pending_block_groups(trans, root);
2747 spin_lock(&delayed_refs->lock);
2748 }
2749
Chris Mason56bec292009-03-13 10:10:06 -04002750 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002751 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04002752 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04002753 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04002754
2755 while (node) {
2756 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2757 rb_node);
2758 if (btrfs_delayed_ref_is_head(ref)) {
2759 struct btrfs_delayed_ref_head *head;
2760
2761 head = btrfs_delayed_node_to_head(ref);
2762 atomic_inc(&ref->refs);
2763
2764 spin_unlock(&delayed_refs->lock);
David Sterba8cc33e52011-05-02 15:29:25 +02002765 /*
2766 * Mutex was contended, block until it's
2767 * released and try again
2768 */
Chris Mason56bec292009-03-13 10:10:06 -04002769 mutex_lock(&head->mutex);
2770 mutex_unlock(&head->mutex);
2771
2772 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04002773 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04002774 goto again;
2775 }
2776 node = rb_next(node);
2777 }
2778 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04002779 schedule_timeout(1);
2780 goto again;
2781 }
Chris Mason54aa1f42007-06-22 14:16:25 -04002782out:
Chris Masonbb721702013-01-29 18:44:12 -05002783 atomic_dec(&delayed_refs->procs_running_refs);
2784 smp_mb();
2785 if (waitqueue_active(&delayed_refs->wait))
2786 wake_up(&delayed_refs->wait);
2787
Chris Masonc3e69d52009-03-13 10:17:05 -04002788 spin_unlock(&delayed_refs->lock);
Jan Schmidtedf39272012-06-28 18:04:55 +02002789 assert_qgroups_uptodate(trans);
Chris Masona28ec192007-03-06 20:08:01 -05002790 return 0;
2791}
2792
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002793int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2794 struct btrfs_root *root,
2795 u64 bytenr, u64 num_bytes, u64 flags,
Josef Bacikb1c79e02013-05-09 13:49:30 -04002796 int level, int is_data)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002797{
2798 struct btrfs_delayed_extent_op *extent_op;
2799 int ret;
2800
Miao Xie78a61842012-11-21 02:21:28 +00002801 extent_op = btrfs_alloc_delayed_extent_op();
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002802 if (!extent_op)
2803 return -ENOMEM;
2804
2805 extent_op->flags_to_set = flags;
2806 extent_op->update_flags = 1;
2807 extent_op->update_key = 0;
2808 extent_op->is_data = is_data ? 1 : 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04002809 extent_op->level = level;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002810
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002811 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2812 num_bytes, extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002813 if (ret)
Miao Xie78a61842012-11-21 02:21:28 +00002814 btrfs_free_delayed_extent_op(extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002815 return ret;
2816}
2817
2818static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2819 struct btrfs_root *root,
2820 struct btrfs_path *path,
2821 u64 objectid, u64 offset, u64 bytenr)
2822{
2823 struct btrfs_delayed_ref_head *head;
2824 struct btrfs_delayed_ref_node *ref;
2825 struct btrfs_delayed_data_ref *data_ref;
2826 struct btrfs_delayed_ref_root *delayed_refs;
2827 struct rb_node *node;
2828 int ret = 0;
2829
2830 ret = -ENOENT;
2831 delayed_refs = &trans->transaction->delayed_refs;
2832 spin_lock(&delayed_refs->lock);
2833 head = btrfs_find_delayed_ref_head(trans, bytenr);
2834 if (!head)
2835 goto out;
2836
2837 if (!mutex_trylock(&head->mutex)) {
2838 atomic_inc(&head->node.refs);
2839 spin_unlock(&delayed_refs->lock);
2840
David Sterbab3b4aa72011-04-21 01:20:15 +02002841 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002842
David Sterba8cc33e52011-05-02 15:29:25 +02002843 /*
2844 * Mutex was contended, block until it's released and let
2845 * caller try again
2846 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002847 mutex_lock(&head->mutex);
2848 mutex_unlock(&head->mutex);
2849 btrfs_put_delayed_ref(&head->node);
2850 return -EAGAIN;
2851 }
2852
2853 node = rb_prev(&head->node.rb_node);
2854 if (!node)
2855 goto out_unlock;
2856
2857 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2858
2859 if (ref->bytenr != bytenr)
2860 goto out_unlock;
2861
2862 ret = 1;
2863 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2864 goto out_unlock;
2865
2866 data_ref = btrfs_delayed_node_to_data_ref(ref);
2867
2868 node = rb_prev(node);
2869 if (node) {
Liu Bodf57dbe2012-07-23 05:50:03 -06002870 int seq = ref->seq;
2871
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002872 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
Liu Bodf57dbe2012-07-23 05:50:03 -06002873 if (ref->bytenr == bytenr && ref->seq == seq)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002874 goto out_unlock;
2875 }
2876
2877 if (data_ref->root != root->root_key.objectid ||
2878 data_ref->objectid != objectid || data_ref->offset != offset)
2879 goto out_unlock;
2880
2881 ret = 0;
2882out_unlock:
2883 mutex_unlock(&head->mutex);
2884out:
2885 spin_unlock(&delayed_refs->lock);
2886 return ret;
2887}
2888
2889static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2890 struct btrfs_root *root,
2891 struct btrfs_path *path,
2892 u64 objectid, u64 offset, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05002893{
2894 struct btrfs_root *extent_root = root->fs_info->extent_root;
Yan Zhengf321e492008-07-30 09:26:11 -04002895 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002896 struct btrfs_extent_data_ref *ref;
2897 struct btrfs_extent_inline_ref *iref;
2898 struct btrfs_extent_item *ei;
Chris Masonbe20aa92007-12-17 20:14:01 -05002899 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002900 u32 item_size;
Yan Zhengf321e492008-07-30 09:26:11 -04002901 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05002902
Chris Masonbe20aa92007-12-17 20:14:01 -05002903 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04002904 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04002905 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05002906
Chris Masonbe20aa92007-12-17 20:14:01 -05002907 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2908 if (ret < 0)
2909 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002910 BUG_ON(ret == 0); /* Corruption */
Yan Zheng80ff3852008-10-30 14:20:02 -04002911
2912 ret = -ENOENT;
2913 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04002914 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002915
Zheng Yan31840ae2008-09-23 13:14:14 -04002916 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04002917 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002918 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05002919
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002920 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05002921 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002922
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002923 ret = 1;
2924 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2925#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2926 if (item_size < sizeof(*ei)) {
2927 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2928 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05002929 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002930#endif
2931 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2932
2933 if (item_size != sizeof(*ei) +
2934 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2935 goto out;
2936
2937 if (btrfs_extent_generation(leaf, ei) <=
2938 btrfs_root_last_snapshot(&root->root_item))
2939 goto out;
2940
2941 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2942 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2943 BTRFS_EXTENT_DATA_REF_KEY)
2944 goto out;
2945
2946 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2947 if (btrfs_extent_refs(leaf, ei) !=
2948 btrfs_extent_data_ref_count(leaf, ref) ||
2949 btrfs_extent_data_ref_root(leaf, ref) !=
2950 root->root_key.objectid ||
2951 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2952 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2953 goto out;
2954
Yan Zhengf321e492008-07-30 09:26:11 -04002955 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05002956out:
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002957 return ret;
2958}
2959
2960int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2961 struct btrfs_root *root,
2962 u64 objectid, u64 offset, u64 bytenr)
2963{
2964 struct btrfs_path *path;
2965 int ret;
2966 int ret2;
2967
2968 path = btrfs_alloc_path();
2969 if (!path)
2970 return -ENOENT;
2971
2972 do {
2973 ret = check_committed_ref(trans, root, path, objectid,
2974 offset, bytenr);
2975 if (ret && ret != -ENOENT)
2976 goto out;
2977
2978 ret2 = check_delayed_ref(trans, root, path, objectid,
2979 offset, bytenr);
2980 } while (ret2 == -EAGAIN);
2981
2982 if (ret2 && ret2 != -ENOENT) {
2983 ret = ret2;
2984 goto out;
2985 }
2986
2987 if (ret != -ENOENT || ret2 != -ENOENT)
2988 ret = 0;
2989out:
Yan Zhengf321e492008-07-30 09:26:11 -04002990 btrfs_free_path(path);
Yan, Zhengf0486c62010-05-16 10:46:25 -04002991 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2992 WARN_ON(ret > 0);
Yan Zhengf321e492008-07-30 09:26:11 -04002993 return ret;
2994}
2995
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002996static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
Chris Masonb7a9f292009-02-04 09:23:45 -05002997 struct btrfs_root *root,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002998 struct extent_buffer *buf,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02002999 int full_backref, int inc, int for_cow)
Zheng Yan31840ae2008-09-23 13:14:14 -04003000{
3001 u64 bytenr;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003002 u64 num_bytes;
3003 u64 parent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003004 u64 ref_root;
Zheng Yan31840ae2008-09-23 13:14:14 -04003005 u32 nritems;
Zheng Yan31840ae2008-09-23 13:14:14 -04003006 struct btrfs_key key;
3007 struct btrfs_file_extent_item *fi;
3008 int i;
3009 int level;
3010 int ret = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003011 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003012 u64, u64, u64, u64, u64, u64, int);
Zheng Yan31840ae2008-09-23 13:14:14 -04003013
3014 ref_root = btrfs_header_owner(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04003015 nritems = btrfs_header_nritems(buf);
3016 level = btrfs_header_level(buf);
3017
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003018 if (!root->ref_cows && level == 0)
3019 return 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05003020
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003021 if (inc)
3022 process_func = btrfs_inc_extent_ref;
3023 else
3024 process_func = btrfs_free_extent;
Zheng Yan31840ae2008-09-23 13:14:14 -04003025
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003026 if (full_backref)
3027 parent = buf->start;
3028 else
3029 parent = 0;
3030
Zheng Yan31840ae2008-09-23 13:14:14 -04003031 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003032 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04003033 btrfs_item_key_to_cpu(buf, &key, i);
3034 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04003035 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04003036 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04003037 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003038 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04003039 BTRFS_FILE_EXTENT_INLINE)
3040 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003041 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3042 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04003043 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04003044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3046 key.offset -= btrfs_file_extent_offset(buf, fi);
3047 ret = process_func(trans, root, bytenr, num_bytes,
3048 parent, ref_root, key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003049 key.offset, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003050 if (ret)
3051 goto fail;
Chris Masonb7a9f292009-02-04 09:23:45 -05003052 } else {
3053 bytenr = btrfs_node_blockptr(buf, i);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003054 num_bytes = btrfs_level_size(root, level - 1);
3055 ret = process_func(trans, root, bytenr, num_bytes,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003056 parent, ref_root, level - 1, 0,
3057 for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003058 if (ret)
Zheng Yan31840ae2008-09-23 13:14:14 -04003059 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04003060 }
3061 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003062 return 0;
3063fail:
Chris Mason54aa1f42007-06-22 14:16:25 -04003064 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05003065}
3066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003067int btrfs_inc_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)
Zheng Yan31840ae2008-09-23 13:14:14 -04003069{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003070 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003071}
Zheng Yan31840ae2008-09-23 13:14:14 -04003072
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003073int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003074 struct extent_buffer *buf, int full_backref, int for_cow)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003075{
Arne Jansen66d7e7f2011-09-12 15:26:38 +02003076 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow);
Zheng Yan31840ae2008-09-23 13:14:14 -04003077}
3078
Chris Mason9078a3e2007-04-26 16:46:15 -04003079static int write_one_cache_group(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 struct btrfs_path *path,
3082 struct btrfs_block_group_cache *cache)
3083{
3084 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003085 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04003086 unsigned long bi;
3087 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04003088
Chris Mason9078a3e2007-04-26 16:46:15 -04003089 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003090 if (ret < 0)
3091 goto fail;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003092 BUG_ON(ret); /* Corruption */
Chris Mason5f39d392007-10-15 16:14:19 -04003093
3094 leaf = path->nodes[0];
3095 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3096 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3097 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02003098 btrfs_release_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -04003099fail:
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003100 if (ret) {
3101 btrfs_abort_transaction(trans, root, ret);
Chris Mason9078a3e2007-04-26 16:46:15 -04003102 return ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003103 }
Chris Mason9078a3e2007-04-26 16:46:15 -04003104 return 0;
3105
3106}
3107
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003108static struct btrfs_block_group_cache *
3109next_block_group(struct btrfs_root *root,
3110 struct btrfs_block_group_cache *cache)
3111{
3112 struct rb_node *node;
3113 spin_lock(&root->fs_info->block_group_cache_lock);
3114 node = rb_next(&cache->cache_node);
3115 btrfs_put_block_group(cache);
3116 if (node) {
3117 cache = rb_entry(node, struct btrfs_block_group_cache,
3118 cache_node);
Josef Bacik11dfe352009-11-13 20:12:59 +00003119 btrfs_get_block_group(cache);
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003120 } else
3121 cache = NULL;
3122 spin_unlock(&root->fs_info->block_group_cache_lock);
3123 return cache;
3124}
3125
Josef Bacik0af3d002010-06-21 14:48:16 -04003126static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3127 struct btrfs_trans_handle *trans,
3128 struct btrfs_path *path)
3129{
3130 struct btrfs_root *root = block_group->fs_info->tree_root;
3131 struct inode *inode = NULL;
3132 u64 alloc_hint = 0;
Josef Bacik2b209822010-12-03 13:17:53 -05003133 int dcs = BTRFS_DC_ERROR;
Josef Bacik0af3d002010-06-21 14:48:16 -04003134 int num_pages = 0;
3135 int retries = 0;
3136 int ret = 0;
3137
3138 /*
3139 * If this block group is smaller than 100 megs don't bother caching the
3140 * block group.
3141 */
3142 if (block_group->key.offset < (100 * 1024 * 1024)) {
3143 spin_lock(&block_group->lock);
3144 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3145 spin_unlock(&block_group->lock);
3146 return 0;
3147 }
3148
3149again:
3150 inode = lookup_free_space_inode(root, block_group, path);
3151 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3152 ret = PTR_ERR(inode);
David Sterbab3b4aa72011-04-21 01:20:15 +02003153 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003154 goto out;
3155 }
3156
3157 if (IS_ERR(inode)) {
3158 BUG_ON(retries);
3159 retries++;
3160
3161 if (block_group->ro)
3162 goto out_free;
3163
3164 ret = create_free_space_inode(root, trans, block_group, path);
3165 if (ret)
3166 goto out_free;
3167 goto again;
3168 }
3169
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003170 /* We've already setup this transaction, go ahead and exit */
3171 if (block_group->cache_generation == trans->transid &&
3172 i_size_read(inode)) {
3173 dcs = BTRFS_DC_SETUP;
3174 goto out_put;
3175 }
3176
Josef Bacik0af3d002010-06-21 14:48:16 -04003177 /*
3178 * We want to set the generation to 0, that way if anything goes wrong
3179 * from here on out we know not to trust this cache when we load up next
3180 * time.
3181 */
3182 BTRFS_I(inode)->generation = 0;
3183 ret = btrfs_update_inode(trans, root, inode);
3184 WARN_ON(ret);
3185
3186 if (i_size_read(inode) > 0) {
Miao Xie7b61cd92013-05-13 13:55:09 +00003187 ret = btrfs_check_trunc_cache_free_space(root,
3188 &root->fs_info->global_block_rsv);
3189 if (ret)
3190 goto out_put;
3191
Josef Bacik0af3d002010-06-21 14:48:16 -04003192 ret = btrfs_truncate_free_space_cache(root, trans, path,
3193 inode);
3194 if (ret)
3195 goto out_put;
3196 }
3197
3198 spin_lock(&block_group->lock);
Liu Bocf7c1ef2012-07-06 03:31:34 -06003199 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3200 !btrfs_test_opt(root, SPACE_CACHE)) {
3201 /*
3202 * don't bother trying to write stuff out _if_
3203 * a) we're not cached,
3204 * b) we're with nospace_cache mount option.
3205 */
Josef Bacik2b209822010-12-03 13:17:53 -05003206 dcs = BTRFS_DC_WRITTEN;
Josef Bacik0af3d002010-06-21 14:48:16 -04003207 spin_unlock(&block_group->lock);
3208 goto out_put;
3209 }
3210 spin_unlock(&block_group->lock);
3211
Josef Bacik6fc823b2012-08-06 13:46:38 -06003212 /*
3213 * Try to preallocate enough space based on how big the block group is.
3214 * Keep in mind this has to include any pinned space which could end up
3215 * taking up quite a bit since it's not folded into the other space
3216 * cache.
3217 */
3218 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
Josef Bacik0af3d002010-06-21 14:48:16 -04003219 if (!num_pages)
3220 num_pages = 1;
3221
Josef Bacik0af3d002010-06-21 14:48:16 -04003222 num_pages *= 16;
3223 num_pages *= PAGE_CACHE_SIZE;
3224
3225 ret = btrfs_check_data_free_space(inode, num_pages);
3226 if (ret)
3227 goto out_put;
3228
3229 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3230 num_pages, num_pages,
3231 &alloc_hint);
Josef Bacik2b209822010-12-03 13:17:53 -05003232 if (!ret)
3233 dcs = BTRFS_DC_SETUP;
Josef Bacik0af3d002010-06-21 14:48:16 -04003234 btrfs_free_reserved_data_space(inode, num_pages);
Josef Bacikc09544e2011-08-30 10:19:10 -04003235
Josef Bacik0af3d002010-06-21 14:48:16 -04003236out_put:
3237 iput(inode);
3238out_free:
David Sterbab3b4aa72011-04-21 01:20:15 +02003239 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04003240out:
3241 spin_lock(&block_group->lock);
Josef Bacike65cbb92011-12-13 16:04:54 -05003242 if (!ret && dcs == BTRFS_DC_SETUP)
Josef Bacik5b0e95b2011-10-06 08:58:24 -04003243 block_group->cache_generation = trans->transid;
Josef Bacik2b209822010-12-03 13:17:53 -05003244 block_group->disk_cache_state = dcs;
Josef Bacik0af3d002010-06-21 14:48:16 -04003245 spin_unlock(&block_group->lock);
3246
3247 return ret;
3248}
3249
Chris Mason96b51792007-10-15 16:15:19 -04003250int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3251 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04003252{
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003253 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003254 int err = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003255 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04003256 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003257
3258 path = btrfs_alloc_path();
3259 if (!path)
3260 return -ENOMEM;
3261
Josef Bacik0af3d002010-06-21 14:48:16 -04003262again:
3263 while (1) {
3264 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3265 while (cache) {
3266 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3267 break;
3268 cache = next_block_group(root, cache);
3269 }
3270 if (!cache) {
3271 if (last == 0)
3272 break;
3273 last = 0;
3274 continue;
3275 }
3276 err = cache_save_setup(cache, trans, path);
3277 last = cache->key.objectid + cache->key.offset;
3278 btrfs_put_block_group(cache);
3279 }
3280
Chris Masond3977122009-01-05 21:25:51 -05003281 while (1) {
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003282 if (last == 0) {
3283 err = btrfs_run_delayed_refs(trans, root,
3284 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003285 if (err) /* File system offline */
3286 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003287 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003288
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003289 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3290 while (cache) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003291 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3292 btrfs_put_block_group(cache);
3293 goto again;
3294 }
3295
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003296 if (cache->dirty)
3297 break;
3298 cache = next_block_group(root, cache);
3299 }
3300 if (!cache) {
3301 if (last == 0)
3302 break;
3303 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -04003304 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04003305 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003306
Josef Bacik0cb59c92010-07-02 12:14:14 -04003307 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3308 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003309 cache->dirty = 0;
3310 last = cache->key.objectid + cache->key.offset;
3311
3312 err = write_one_cache_group(trans, root, path, cache);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003313 if (err) /* File system offline */
3314 goto out;
3315
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003316 btrfs_put_block_group(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04003317 }
Yan Zheng4a8c9a62009-07-22 10:07:05 -04003318
Josef Bacik0cb59c92010-07-02 12:14:14 -04003319 while (1) {
3320 /*
3321 * I don't think this is needed since we're just marking our
3322 * preallocated extent as written, but just in case it can't
3323 * hurt.
3324 */
3325 if (last == 0) {
3326 err = btrfs_run_delayed_refs(trans, root,
3327 (unsigned long)-1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003328 if (err) /* File system offline */
3329 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -04003330 }
3331
3332 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3333 while (cache) {
3334 /*
3335 * Really this shouldn't happen, but it could if we
3336 * couldn't write the entire preallocated extent and
3337 * splitting the extent resulted in a new block.
3338 */
3339 if (cache->dirty) {
3340 btrfs_put_block_group(cache);
3341 goto again;
3342 }
3343 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3344 break;
3345 cache = next_block_group(root, cache);
3346 }
3347 if (!cache) {
3348 if (last == 0)
3349 break;
3350 last = 0;
3351 continue;
3352 }
3353
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003354 err = btrfs_write_out_cache(root, trans, cache, path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04003355
3356 /*
3357 * If we didn't have an error then the cache state is still
3358 * NEED_WRITE, so we can set it to WRITTEN.
3359 */
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003360 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
Josef Bacik0cb59c92010-07-02 12:14:14 -04003361 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3362 last = cache->key.objectid + cache->key.offset;
3363 btrfs_put_block_group(cache);
3364 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003365out:
Josef Bacik0cb59c92010-07-02 12:14:14 -04003366
Chris Mason9078a3e2007-04-26 16:46:15 -04003367 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003368 return err;
Chris Mason9078a3e2007-04-26 16:46:15 -04003369}
3370
Yan Zhengd2fb3432008-12-11 16:30:39 -05003371int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3372{
3373 struct btrfs_block_group_cache *block_group;
3374 int readonly = 0;
3375
3376 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3377 if (!block_group || block_group->ro)
3378 readonly = 1;
3379 if (block_group)
Chris Masonfa9c0d792009-04-03 09:47:43 -04003380 btrfs_put_block_group(block_group);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003381 return readonly;
3382}
3383
Chris Mason593060d2008-03-25 16:50:33 -04003384static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3385 u64 total_bytes, u64 bytes_used,
3386 struct btrfs_space_info **space_info)
3387{
3388 struct btrfs_space_info *found;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003389 int i;
3390 int factor;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003391 int ret;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003392
3393 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3394 BTRFS_BLOCK_GROUP_RAID10))
3395 factor = 2;
3396 else
3397 factor = 1;
Chris Mason593060d2008-03-25 16:50:33 -04003398
3399 found = __find_space_info(info, flags);
3400 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04003401 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003402 found->total_bytes += total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003403 found->disk_total += total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003404 found->bytes_used += bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003405 found->disk_used += bytes_used * factor;
Chris Mason8f18cf12008-04-25 16:53:30 -04003406 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04003407 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04003408 *space_info = found;
3409 return 0;
3410 }
Yan Zhengc146afa2008-11-12 14:34:12 -05003411 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04003412 if (!found)
3413 return -ENOMEM;
3414
Josef Bacikb150a4f2013-06-19 15:00:04 -04003415 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3416 if (ret) {
3417 kfree(found);
3418 return ret;
3419 }
3420
Yan, Zhengb742bb82010-05-16 10:46:24 -04003421 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3422 INIT_LIST_HEAD(&found->block_groups[i]);
Josef Bacik80eb2342008-10-29 14:49:05 -04003423 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003424 spin_lock_init(&found->lock);
Ilya Dryomov52ba6922012-01-16 22:04:47 +02003425 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
Chris Mason593060d2008-03-25 16:50:33 -04003426 found->total_bytes = total_bytes;
Josef Bacik89a55892010-10-14 14:52:27 -04003427 found->disk_total = total_bytes * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003428 found->bytes_used = bytes_used;
Yan, Zhengb742bb82010-05-16 10:46:24 -04003429 found->disk_used = bytes_used * factor;
Chris Mason593060d2008-03-25 16:50:33 -04003430 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04003431 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003432 found->bytes_readonly = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04003433 found->bytes_may_use = 0;
Chris Mason593060d2008-03-25 16:50:33 -04003434 found->full = 0;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003435 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
Josef Bacik6d741192011-04-11 20:20:11 -04003436 found->chunk_alloc = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04003437 found->flush = 0;
3438 init_waitqueue_head(&found->wait);
Chris Mason593060d2008-03-25 16:50:33 -04003439 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04003440 list_add_rcu(&found->list, &info->space_info);
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003441 if (flags & BTRFS_BLOCK_GROUP_DATA)
3442 info->data_sinfo = found;
Chris Mason593060d2008-03-25 16:50:33 -04003443 return 0;
3444}
3445
Chris Mason8790d502008-04-03 16:29:03 -04003446static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3447{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03003448 u64 extra_flags = chunk_to_extended(flags) &
3449 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003450
Miao Xiede98ced2013-01-29 10:13:12 +00003451 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003452 if (flags & BTRFS_BLOCK_GROUP_DATA)
3453 fs_info->avail_data_alloc_bits |= extra_flags;
3454 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3455 fs_info->avail_metadata_alloc_bits |= extra_flags;
3456 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3457 fs_info->avail_system_alloc_bits |= extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00003458 write_sequnlock(&fs_info->profiles_lock);
Chris Mason8790d502008-04-03 16:29:03 -04003459}
Chris Mason593060d2008-03-25 16:50:33 -04003460
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003461/*
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003462 * returns target flags in extended format or 0 if restripe for this
3463 * chunk_type is not in progress
Ilya Dryomovc6664b42012-04-12 16:03:56 -04003464 *
3465 * should be called with either volume_mutex or balance_lock held
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003466 */
3467static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3468{
3469 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3470 u64 target = 0;
3471
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003472 if (!bctl)
3473 return 0;
3474
3475 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3476 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3477 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3478 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3479 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3480 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3481 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3482 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3483 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3484 }
3485
3486 return target;
3487}
3488
3489/*
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003490 * @flags: available profiles in extended format (see ctree.h)
3491 *
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003492 * Returns reduced profile in chunk format. If profile changing is in
3493 * progress (either running or paused) picks the target profile (if it's
3494 * already available), otherwise falls back to plain reducing.
Ilya Dryomova46d11a2012-01-16 22:04:47 +02003495 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003496static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04003497{
Chris Masoncd02dca2010-12-13 14:56:23 -05003498 /*
3499 * we add in the count of missing devices because we want
3500 * to make sure that any RAID levels on a degraded FS
3501 * continue to be honored.
3502 */
3503 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3504 root->fs_info->fs_devices->missing_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003505 u64 target;
David Woodhouse53b381b2013-01-29 18:40:14 -05003506 u64 tmp;
Chris Masona061fc82008-05-07 11:43:44 -04003507
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003508 /*
3509 * see if restripe for this chunk_type is in progress, if so
3510 * try to reduce to the target profile
3511 */
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003512 spin_lock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003513 target = get_restripe_target(root->fs_info, flags);
3514 if (target) {
3515 /* pick target profile only if it's already available */
3516 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003517 spin_unlock(&root->fs_info->balance_lock);
Ilya Dryomovfc67c452012-03-27 17:09:17 +03003518 return extended_to_chunk(target);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02003519 }
3520 }
3521 spin_unlock(&root->fs_info->balance_lock);
3522
David Woodhouse53b381b2013-01-29 18:40:14 -05003523 /* First, mask out the RAID levels which aren't possible */
Chris Masona061fc82008-05-07 11:43:44 -04003524 if (num_devices == 1)
David Woodhouse53b381b2013-01-29 18:40:14 -05003525 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3526 BTRFS_BLOCK_GROUP_RAID5);
3527 if (num_devices < 3)
3528 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
Chris Masona061fc82008-05-07 11:43:44 -04003529 if (num_devices < 4)
3530 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3531
David Woodhouse53b381b2013-01-29 18:40:14 -05003532 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3533 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3534 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3535 flags &= ~tmp;
Chris Masonec44a352008-04-28 15:29:52 -04003536
David Woodhouse53b381b2013-01-29 18:40:14 -05003537 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3538 tmp = BTRFS_BLOCK_GROUP_RAID6;
3539 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3540 tmp = BTRFS_BLOCK_GROUP_RAID5;
3541 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3542 tmp = BTRFS_BLOCK_GROUP_RAID10;
3543 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3544 tmp = BTRFS_BLOCK_GROUP_RAID1;
3545 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3546 tmp = BTRFS_BLOCK_GROUP_RAID0;
Chris Masonec44a352008-04-28 15:29:52 -04003547
David Woodhouse53b381b2013-01-29 18:40:14 -05003548 return extended_to_chunk(flags | tmp);
Chris Masonec44a352008-04-28 15:29:52 -04003549}
3550
Yan, Zhengb742bb82010-05-16 10:46:24 -04003551static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
Josef Bacik6a632092009-02-20 11:00:09 -05003552{
Miao Xiede98ced2013-01-29 10:13:12 +00003553 unsigned seq;
3554
3555 do {
3556 seq = read_seqbegin(&root->fs_info->profiles_lock);
3557
3558 if (flags & BTRFS_BLOCK_GROUP_DATA)
3559 flags |= root->fs_info->avail_data_alloc_bits;
3560 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3561 flags |= root->fs_info->avail_system_alloc_bits;
3562 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3563 flags |= root->fs_info->avail_metadata_alloc_bits;
3564 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
Ilya Dryomov6fef8df2012-01-16 22:04:47 +02003565
Yan, Zhengb742bb82010-05-16 10:46:24 -04003566 return btrfs_reduce_alloc_profile(root, flags);
3567}
Josef Bacik6a632092009-02-20 11:00:09 -05003568
Miao Xie6d07bce2011-01-05 10:07:31 +00003569u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
Yan, Zhengb742bb82010-05-16 10:46:24 -04003570{
3571 u64 flags;
David Woodhouse53b381b2013-01-29 18:40:14 -05003572 u64 ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003573
Yan, Zhengb742bb82010-05-16 10:46:24 -04003574 if (data)
3575 flags = BTRFS_BLOCK_GROUP_DATA;
3576 else if (root == root->fs_info->chunk_root)
3577 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3578 else
3579 flags = BTRFS_BLOCK_GROUP_METADATA;
3580
David Woodhouse53b381b2013-01-29 18:40:14 -05003581 ret = get_alloc_profile(root, flags);
3582 return ret;
Josef Bacik6a632092009-02-20 11:00:09 -05003583}
3584
Josef Bacik6a632092009-02-20 11:00:09 -05003585/*
3586 * This will check the space that the inode allocates from to make sure we have
3587 * enough space for bytes.
3588 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003589int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003590{
3591 struct btrfs_space_info *data_sinfo;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003592 struct btrfs_root *root = BTRFS_I(inode)->root;
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003593 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikab6e24102010-03-19 14:38:13 +00003594 u64 used;
Josef Bacik0af3d002010-06-21 14:48:16 -04003595 int ret = 0, committed = 0, alloc_chunk = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003596
3597 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003598 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003599
Li Zefan82d59022011-04-20 10:33:24 +08003600 if (root == root->fs_info->tree_root ||
3601 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
Josef Bacik0af3d002010-06-21 14:48:16 -04003602 alloc_chunk = 0;
3603 committed = 1;
3604 }
3605
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003606 data_sinfo = fs_info->data_sinfo;
Chris Mason33b4d472009-09-22 14:45:50 -04003607 if (!data_sinfo)
3608 goto alloc;
3609
Josef Bacik6a632092009-02-20 11:00:09 -05003610again:
3611 /* make sure we have enough space to handle the data first */
3612 spin_lock(&data_sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04003613 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3614 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3615 data_sinfo->bytes_may_use;
Josef Bacikab6e24102010-03-19 14:38:13 +00003616
3617 if (used + bytes > data_sinfo->total_bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003618 struct btrfs_trans_handle *trans;
3619
Josef Bacik6a632092009-02-20 11:00:09 -05003620 /*
3621 * if we don't have enough free bytes in this space then we need
3622 * to alloc a new chunk.
3623 */
Josef Bacik0af3d002010-06-21 14:48:16 -04003624 if (!data_sinfo->full && alloc_chunk) {
Josef Bacik6a632092009-02-20 11:00:09 -05003625 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05003626
Chris Mason0e4f8f82011-04-15 16:05:44 -04003627 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik6a632092009-02-20 11:00:09 -05003628 spin_unlock(&data_sinfo->lock);
Chris Mason33b4d472009-09-22 14:45:50 -04003629alloc:
Josef Bacik6a632092009-02-20 11:00:09 -05003630 alloc_target = btrfs_get_alloc_profile(root, 1);
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003631 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003632 if (IS_ERR(trans))
3633 return PTR_ERR(trans);
Josef Bacik6a632092009-02-20 11:00:09 -05003634
3635 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0e4f8f82011-04-15 16:05:44 -04003636 alloc_target,
3637 CHUNK_ALLOC_NO_FORCE);
Josef Bacik6a632092009-02-20 11:00:09 -05003638 btrfs_end_transaction(trans, root);
Miao Xied52a5b52011-01-05 10:07:18 +00003639 if (ret < 0) {
3640 if (ret != -ENOSPC)
3641 return ret;
3642 else
3643 goto commit_trans;
3644 }
Chris Mason33b4d472009-09-22 14:45:50 -04003645
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003646 if (!data_sinfo)
3647 data_sinfo = fs_info->data_sinfo;
3648
Josef Bacik6a632092009-02-20 11:00:09 -05003649 goto again;
3650 }
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003651
3652 /*
Josef Bacikb150a4f2013-06-19 15:00:04 -04003653 * If we don't have enough pinned space to deal with this
3654 * allocation don't bother committing the transaction.
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003655 */
Josef Bacikb150a4f2013-06-19 15:00:04 -04003656 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3657 bytes) < 0)
Josef Bacikf2bb8f52011-05-25 13:10:16 -04003658 committed = 1;
Josef Bacik6a632092009-02-20 11:00:09 -05003659 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003660
3661 /* commit the current transaction and try again */
Miao Xied52a5b52011-01-05 10:07:18 +00003662commit_trans:
Josef Bacika4abeea2011-04-11 17:25:13 -04003663 if (!committed &&
3664 !atomic_read(&root->fs_info->open_ioctl_trans)) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003665 committed = 1;
Josef Bacikb150a4f2013-06-19 15:00:04 -04003666
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003667 trans = btrfs_join_transaction(root);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 if (IS_ERR(trans))
3669 return PTR_ERR(trans);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05003670 ret = btrfs_commit_transaction(trans, root);
3671 if (ret)
3672 return ret;
3673 goto again;
3674 }
3675
Josef Bacik6a632092009-02-20 11:00:09 -05003676 return -ENOSPC;
3677 }
3678 data_sinfo->bytes_may_use += bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003679 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003680 data_sinfo->flags, bytes, 1);
Josef Bacik6a632092009-02-20 11:00:09 -05003681 spin_unlock(&data_sinfo->lock);
3682
Josef Bacik9ed74f22009-09-11 16:12:44 -04003683 return 0;
Josef Bacik6a632092009-02-20 11:00:09 -05003684}
3685
3686/*
Josef Bacikfb25e912011-07-26 17:00:46 -04003687 * Called if we need to clear a data reservation for this inode.
Josef Bacik6a632092009-02-20 11:00:09 -05003688 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003689void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
Josef Bacik6a632092009-02-20 11:00:09 -05003690{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04003691 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik6a632092009-02-20 11:00:09 -05003692 struct btrfs_space_info *data_sinfo;
3693
3694 /* make sure bytes are sectorsize aligned */
Qu Wenruofda28322013-02-26 08:10:22 +00003695 bytes = ALIGN(bytes, root->sectorsize);
Josef Bacik6a632092009-02-20 11:00:09 -05003696
Li Zefanb4d7c3c2012-07-09 20:21:07 -06003697 data_sinfo = root->fs_info->data_sinfo;
Josef Bacik6a632092009-02-20 11:00:09 -05003698 spin_lock(&data_sinfo->lock);
Josef Bacik7ee9e442013-06-21 16:37:03 -04003699 WARN_ON(data_sinfo->bytes_may_use < bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05003700 data_sinfo->bytes_may_use -= bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05003701 trace_btrfs_space_reservation(root->fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04003702 data_sinfo->flags, bytes, 0);
Josef Bacik6a632092009-02-20 11:00:09 -05003703 spin_unlock(&data_sinfo->lock);
3704}
3705
Josef Bacik97e728d2009-04-21 17:40:57 -04003706static void force_metadata_allocation(struct btrfs_fs_info *info)
3707{
3708 struct list_head *head = &info->space_info;
3709 struct btrfs_space_info *found;
3710
3711 rcu_read_lock();
3712 list_for_each_entry_rcu(found, head, list) {
3713 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003714 found->force_alloc = CHUNK_ALLOC_FORCE;
Josef Bacik97e728d2009-04-21 17:40:57 -04003715 }
3716 rcu_read_unlock();
3717}
3718
Miao Xie3c76cd82013-04-25 10:12:38 +00003719static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3720{
3721 return (global->size << 1);
3722}
3723
Chris Masone5bc2452010-10-26 13:37:56 -04003724static int should_alloc_chunk(struct btrfs_root *root,
Josef Bacik698d0082012-09-12 14:08:47 -04003725 struct btrfs_space_info *sinfo, int force)
Yan, Zheng424499d2010-05-16 10:46:25 -04003726{
Josef Bacikfb25e912011-07-26 17:00:46 -04003727 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zheng424499d2010-05-16 10:46:25 -04003728 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
Chris Mason0e4f8f82011-04-15 16:05:44 -04003729 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
Chris Masone5bc2452010-10-26 13:37:56 -04003730 u64 thresh;
Yan, Zheng424499d2010-05-16 10:46:25 -04003731
Chris Mason0e4f8f82011-04-15 16:05:44 -04003732 if (force == CHUNK_ALLOC_FORCE)
3733 return 1;
3734
3735 /*
Josef Bacikfb25e912011-07-26 17:00:46 -04003736 * We need to take into account the global rsv because for all intents
3737 * and purposes it's used space. Don't worry about locking the
3738 * global_rsv, it doesn't change except when the transaction commits.
3739 */
Josef Bacik54338b52012-08-14 16:20:52 -04003740 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
Miao Xie3c76cd82013-04-25 10:12:38 +00003741 num_allocated += calc_global_rsv_need_space(global_rsv);
Josef Bacikfb25e912011-07-26 17:00:46 -04003742
3743 /*
Chris Mason0e4f8f82011-04-15 16:05:44 -04003744 * in limited mode, we want to have some free space up to
3745 * about 1% of the FS size.
3746 */
3747 if (force == CHUNK_ALLOC_LIMITED) {
David Sterba6c417612011-04-13 15:41:04 +02003748 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
Chris Mason0e4f8f82011-04-15 16:05:44 -04003749 thresh = max_t(u64, 64 * 1024 * 1024,
3750 div_factor_fine(thresh, 1));
3751
3752 if (num_bytes - num_allocated < thresh)
3753 return 1;
3754 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003755
Josef Bacik698d0082012-09-12 14:08:47 -04003756 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
Josef Bacik14ed0ca2010-10-15 15:23:48 -04003757 return 0;
Yan, Zheng424499d2010-05-16 10:46:25 -04003758 return 1;
3759}
3760
Liu Bo15d1ff82012-03-29 09:57:44 -04003761static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3762{
3763 u64 num_dev;
3764
David Woodhouse53b381b2013-01-29 18:40:14 -05003765 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3766 BTRFS_BLOCK_GROUP_RAID0 |
3767 BTRFS_BLOCK_GROUP_RAID5 |
3768 BTRFS_BLOCK_GROUP_RAID6))
Liu Bo15d1ff82012-03-29 09:57:44 -04003769 num_dev = root->fs_info->fs_devices->rw_devices;
3770 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3771 num_dev = 2;
3772 else
3773 num_dev = 1; /* DUP or single */
3774
3775 /* metadata for updaing devices and chunk tree */
3776 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3777}
3778
3779static void check_system_chunk(struct btrfs_trans_handle *trans,
3780 struct btrfs_root *root, u64 type)
3781{
3782 struct btrfs_space_info *info;
3783 u64 left;
3784 u64 thresh;
3785
3786 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3787 spin_lock(&info->lock);
3788 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3789 info->bytes_reserved - info->bytes_readonly;
3790 spin_unlock(&info->lock);
3791
3792 thresh = get_system_chunk_thresh(root, type);
3793 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003794 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3795 left, thresh, type);
Liu Bo15d1ff82012-03-29 09:57:44 -04003796 dump_space_info(info, 0, 0);
3797 }
3798
3799 if (left < thresh) {
3800 u64 flags;
3801
3802 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3803 btrfs_alloc_chunk(trans, root, flags);
3804 }
3805}
3806
Chris Mason6324fbf2008-03-24 15:01:59 -04003807static int do_chunk_alloc(struct btrfs_trans_handle *trans,
Josef Bacik698d0082012-09-12 14:08:47 -04003808 struct btrfs_root *extent_root, u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04003809{
3810 struct btrfs_space_info *space_info;
Josef Bacik97e728d2009-04-21 17:40:57 -04003811 struct btrfs_fs_info *fs_info = extent_root->fs_info;
Josef Bacik6d741192011-04-11 20:20:11 -04003812 int wait_for_alloc = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05003813 int ret = 0;
3814
Josef Bacikc6b305a2012-12-18 09:16:16 -05003815 /* Don't re-enter if we're already allocating a chunk */
3816 if (trans->allocating_chunk)
3817 return -ENOSPC;
3818
Chris Mason6324fbf2008-03-24 15:01:59 -04003819 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04003820 if (!space_info) {
3821 ret = update_space_info(extent_root->fs_info, flags,
3822 0, 0, &space_info);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003823 BUG_ON(ret); /* -ENOMEM */
Chris Mason593060d2008-03-25 16:50:33 -04003824 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003825 BUG_ON(!space_info); /* Logic error */
Chris Mason6324fbf2008-03-24 15:01:59 -04003826
Josef Bacik6d741192011-04-11 20:20:11 -04003827again:
Josef Bacik25179202008-10-29 14:49:05 -04003828 spin_lock(&space_info->lock);
Miao Xie9e622d62012-01-26 15:01:12 -05003829 if (force < space_info->force_alloc)
Chris Mason0e4f8f82011-04-15 16:05:44 -04003830 force = space_info->force_alloc;
Josef Bacik25179202008-10-29 14:49:05 -04003831 if (space_info->full) {
3832 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003833 return 0;
Josef Bacik25179202008-10-29 14:49:05 -04003834 }
Chris Mason6324fbf2008-03-24 15:01:59 -04003835
Josef Bacik698d0082012-09-12 14:08:47 -04003836 if (!should_alloc_chunk(extent_root, space_info, force)) {
Josef Bacik25179202008-10-29 14:49:05 -04003837 spin_unlock(&space_info->lock);
Josef Bacik6d741192011-04-11 20:20:11 -04003838 return 0;
3839 } else if (space_info->chunk_alloc) {
3840 wait_for_alloc = 1;
3841 } else {
3842 space_info->chunk_alloc = 1;
Josef Bacik25179202008-10-29 14:49:05 -04003843 }
Chris Mason0e4f8f82011-04-15 16:05:44 -04003844
Josef Bacik25179202008-10-29 14:49:05 -04003845 spin_unlock(&space_info->lock);
3846
Josef Bacik6d741192011-04-11 20:20:11 -04003847 mutex_lock(&fs_info->chunk_mutex);
3848
3849 /*
3850 * The chunk_mutex is held throughout the entirety of a chunk
3851 * allocation, so once we've acquired the chunk_mutex we know that the
3852 * other guy is done and we need to recheck and see if we should
3853 * allocate.
3854 */
3855 if (wait_for_alloc) {
3856 mutex_unlock(&fs_info->chunk_mutex);
3857 wait_for_alloc = 0;
3858 goto again;
3859 }
3860
Josef Bacikc6b305a2012-12-18 09:16:16 -05003861 trans->allocating_chunk = true;
3862
Josef Bacik97e728d2009-04-21 17:40:57 -04003863 /*
Josef Bacik67377732010-09-16 16:19:09 -04003864 * If we have mixed data/metadata chunks we want to make sure we keep
3865 * allocating mixed chunks instead of individual chunks.
3866 */
3867 if (btrfs_mixed_space_info(space_info))
3868 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3869
3870 /*
Josef Bacik97e728d2009-04-21 17:40:57 -04003871 * if we're doing a data chunk, go ahead and make sure that
3872 * we keep a reasonable number of metadata chunks allocated in the
3873 * FS as well.
3874 */
Josef Bacik9ed74f22009-09-11 16:12:44 -04003875 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
Josef Bacik97e728d2009-04-21 17:40:57 -04003876 fs_info->data_chunk_allocations++;
3877 if (!(fs_info->data_chunk_allocations %
3878 fs_info->metadata_ratio))
3879 force_metadata_allocation(fs_info);
3880 }
3881
Liu Bo15d1ff82012-03-29 09:57:44 -04003882 /*
3883 * Check if we have enough space in SYSTEM chunk because we may need
3884 * to update devices.
3885 */
3886 check_system_chunk(trans, extent_root, flags);
3887
Yan Zheng2b820322008-11-17 21:11:30 -05003888 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacikc6b305a2012-12-18 09:16:16 -05003889 trans->allocating_chunk = false;
Mark Fasheh92b8e8972011-07-12 10:57:59 -07003890
Josef Bacik9ed74f22009-09-11 16:12:44 -04003891 spin_lock(&space_info->lock);
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003892 if (ret < 0 && ret != -ENOSPC)
3893 goto out;
Chris Masond3977122009-01-05 21:25:51 -05003894 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04003895 space_info->full = 1;
Yan, Zheng424499d2010-05-16 10:46:25 -04003896 else
3897 ret = 1;
Josef Bacik6d741192011-04-11 20:20:11 -04003898
Chris Mason0e4f8f82011-04-15 16:05:44 -04003899 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
Alexandre Olivaa81cb9a2013-02-21 21:15:14 +00003900out:
Josef Bacik6d741192011-04-11 20:20:11 -04003901 space_info->chunk_alloc = 0;
Josef Bacik9ed74f22009-09-11 16:12:44 -04003902 spin_unlock(&space_info->lock);
Dan Carpentera25c75d2012-04-18 09:59:29 +03003903 mutex_unlock(&fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003904 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04003905}
3906
Josef Bacika80c8dc2012-09-06 16:59:33 -04003907static int can_overcommit(struct btrfs_root *root,
3908 struct btrfs_space_info *space_info, u64 bytes,
Miao Xie08e007d2012-10-16 11:33:38 +00003909 enum btrfs_reserve_flush_enum flush)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003910{
Josef Bacik96f1bb52013-01-30 17:02:51 -05003911 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003912 u64 profile = btrfs_get_alloc_profile(root, 0);
Miao Xie3c76cd82013-04-25 10:12:38 +00003913 u64 space_size;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003914 u64 avail;
3915 u64 used;
Josef Bacik70afa392013-02-06 13:53:19 -05003916 u64 to_add;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003917
3918 used = space_info->bytes_used + space_info->bytes_reserved +
Josef Bacik96f1bb52013-01-30 17:02:51 -05003919 space_info->bytes_pinned + space_info->bytes_readonly;
3920
Josef Bacik96f1bb52013-01-30 17:02:51 -05003921 /*
3922 * We only want to allow over committing if we have lots of actual space
3923 * free, but if we don't have enough space to handle the global reserve
3924 * space then we could end up having a real enospc problem when trying
3925 * to allocate a chunk or some other such important allocation.
3926 */
Miao Xie3c76cd82013-04-25 10:12:38 +00003927 spin_lock(&global_rsv->lock);
3928 space_size = calc_global_rsv_need_space(global_rsv);
3929 spin_unlock(&global_rsv->lock);
3930 if (used + space_size >= space_info->total_bytes)
Josef Bacik96f1bb52013-01-30 17:02:51 -05003931 return 0;
3932
3933 used += space_info->bytes_may_use;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003934
3935 spin_lock(&root->fs_info->free_chunk_lock);
3936 avail = root->fs_info->free_chunk_space;
3937 spin_unlock(&root->fs_info->free_chunk_lock);
3938
3939 /*
3940 * If we have dup, raid1 or raid10 then only half of the free
David Woodhouse53b381b2013-01-29 18:40:14 -05003941 * space is actually useable. For raid56, the space info used
3942 * doesn't include the parity drive, so we don't have to
3943 * change the math
Josef Bacika80c8dc2012-09-06 16:59:33 -04003944 */
3945 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3946 BTRFS_BLOCK_GROUP_RAID1 |
3947 BTRFS_BLOCK_GROUP_RAID10))
3948 avail >>= 1;
3949
Josef Bacik70afa392013-02-06 13:53:19 -05003950 to_add = space_info->total_bytes;
3951
Josef Bacika80c8dc2012-09-06 16:59:33 -04003952 /*
Miao Xie561c2942012-10-16 11:32:18 +00003953 * If we aren't flushing all things, let us overcommit up to
3954 * 1/2th of the space. If we can flush, don't let us overcommit
3955 * too much, let it overcommit up to 1/8 of the space.
Josef Bacika80c8dc2012-09-06 16:59:33 -04003956 */
Miao Xie08e007d2012-10-16 11:33:38 +00003957 if (flush == BTRFS_RESERVE_FLUSH_ALL)
Josef Bacik70afa392013-02-06 13:53:19 -05003958 to_add >>= 3;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003959 else
Josef Bacik70afa392013-02-06 13:53:19 -05003960 to_add >>= 1;
Josef Bacika80c8dc2012-09-06 16:59:33 -04003961
Josef Bacik70afa392013-02-06 13:53:19 -05003962 /*
3963 * Limit the overcommit to the amount of free space we could possibly
3964 * allocate for chunks.
3965 */
3966 to_add = min(avail, to_add);
3967
3968 if (used + bytes < space_info->total_bytes + to_add)
Josef Bacika80c8dc2012-09-06 16:59:33 -04003969 return 1;
3970 return 0;
3971}
3972
Eric Sandeen48a3b632013-04-25 20:41:01 +00003973static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
3974 unsigned long nr_pages)
Miao Xieda633a42012-12-20 11:19:09 +00003975{
3976 struct super_block *sb = root->fs_info->sb;
Miao Xieda633a42012-12-20 11:19:09 +00003977
Josef Bacik925a6ef2013-06-20 12:31:27 -04003978 if (down_read_trylock(&sb->s_umount)) {
3979 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
3980 up_read(&sb->s_umount);
3981 } else {
Miao Xieda633a42012-12-20 11:19:09 +00003982 /*
3983 * We needn't worry the filesystem going from r/w to r/o though
3984 * we don't acquire ->s_umount mutex, because the filesystem
3985 * should guarantee the delalloc inodes list be empty after
3986 * the filesystem is readonly(all dirty pages are written to
3987 * the disk).
3988 */
Miao Xieeb73c1b2013-05-15 07:48:22 +00003989 btrfs_start_all_delalloc_inodes(root->fs_info, 0);
Josef Bacik98ad69c2013-04-04 11:55:49 -04003990 if (!current->journal_info)
Miao Xie199c2a92013-05-15 07:48:23 +00003991 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Miao Xieda633a42012-12-20 11:19:09 +00003992 }
3993}
3994
Yan, Zheng5da9d012010-05-16 10:46:25 -04003995/*
3996 * shrink metadata reservation for delalloc
3997 */
Josef Bacikf4c738c2012-07-02 17:10:51 -04003998static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
3999 bool wait_ordered)
Yan, Zheng5da9d012010-05-16 10:46:25 -04004000{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004001 struct btrfs_block_rsv *block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004002 struct btrfs_space_info *space_info;
Josef Bacik663350a2011-11-03 22:54:25 -04004003 struct btrfs_trans_handle *trans;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004004 u64 delalloc_bytes;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004005 u64 max_reclaim;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004006 long time_left;
Josef Bacik877da172011-10-14 14:02:10 -04004007 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
Josef Bacikb1953bc2011-01-21 21:10:01 +00004008 int loops = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004009 enum btrfs_reserve_flush_enum flush;
Yan, Zheng5da9d012010-05-16 10:46:25 -04004010
Josef Bacik663350a2011-11-03 22:54:25 -04004011 trans = (struct btrfs_trans_handle *)current->journal_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004012 block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik0019f102010-10-15 15:18:40 -04004013 space_info = block_rsv->space_info;
Chris Masonbf9022e2010-10-26 13:40:45 -04004014
4015 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004016 delalloc_bytes = percpu_counter_sum_positive(
4017 &root->fs_info->delalloc_bytes);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004018 if (delalloc_bytes == 0) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004019 if (trans)
Josef Bacikf4c738c2012-07-02 17:10:51 -04004020 return;
Miao Xie199c2a92013-05-15 07:48:23 +00004021 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf4c738c2012-07-02 17:10:51 -04004022 return;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004023 }
4024
Josef Bacikf4c738c2012-07-02 17:10:51 -04004025 while (delalloc_bytes && loops < 3) {
4026 max_reclaim = min(delalloc_bytes, to_reclaim);
4027 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
Miao Xieda633a42012-12-20 11:19:09 +00004028 btrfs_writeback_inodes_sb_nr(root, nr_pages);
Josef Bacikdea31f52012-09-06 16:47:00 -04004029 /*
4030 * We need to wait for the async pages to actually start before
4031 * we do anything.
4032 */
4033 wait_event(root->fs_info->async_submit_wait,
4034 !atomic_read(&root->fs_info->async_delalloc_pages));
4035
Miao Xie08e007d2012-10-16 11:33:38 +00004036 if (!trans)
4037 flush = BTRFS_RESERVE_FLUSH_ALL;
4038 else
4039 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacik0019f102010-10-15 15:18:40 -04004040 spin_lock(&space_info->lock);
Miao Xie08e007d2012-10-16 11:33:38 +00004041 if (can_overcommit(root, space_info, orig, flush)) {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004042 spin_unlock(&space_info->lock);
4043 break;
4044 }
Josef Bacik0019f102010-10-15 15:18:40 -04004045 spin_unlock(&space_info->lock);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004046
Chris Mason36e39c42011-03-12 07:08:42 -05004047 loops++;
Josef Bacikf104d042011-10-14 13:56:58 -04004048 if (wait_ordered && !trans) {
Miao Xie199c2a92013-05-15 07:48:23 +00004049 btrfs_wait_all_ordered_extents(root->fs_info, 0);
Josef Bacikf104d042011-10-14 13:56:58 -04004050 } else {
Josef Bacikf4c738c2012-07-02 17:10:51 -04004051 time_left = schedule_timeout_killable(1);
Josef Bacikf104d042011-10-14 13:56:58 -04004052 if (time_left)
4053 break;
4054 }
Josef Bacikf4c738c2012-07-02 17:10:51 -04004055 smp_mb();
Miao Xie963d6782013-01-29 10:10:51 +00004056 delalloc_bytes = percpu_counter_sum_positive(
4057 &root->fs_info->delalloc_bytes);
Yan, Zheng5da9d012010-05-16 10:46:25 -04004058 }
Yan, Zheng5da9d012010-05-16 10:46:25 -04004059}
4060
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004061/**
Josef Bacik663350a2011-11-03 22:54:25 -04004062 * maybe_commit_transaction - possibly commit the transaction if its ok to
4063 * @root - the root we're allocating for
4064 * @bytes - the number of bytes we want to reserve
4065 * @force - force the commit
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004066 *
Josef Bacik663350a2011-11-03 22:54:25 -04004067 * This will check to make sure that committing the transaction will actually
4068 * get us somewhere and then commit the transaction if it does. Otherwise it
4069 * will return -ENOSPC.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004070 */
Josef Bacik663350a2011-11-03 22:54:25 -04004071static int may_commit_transaction(struct btrfs_root *root,
4072 struct btrfs_space_info *space_info,
4073 u64 bytes, int force)
4074{
4075 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4076 struct btrfs_trans_handle *trans;
4077
4078 trans = (struct btrfs_trans_handle *)current->journal_info;
4079 if (trans)
4080 return -EAGAIN;
4081
4082 if (force)
4083 goto commit;
4084
4085 /* See if there is enough pinned space to make this reservation */
4086 spin_lock(&space_info->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004087 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4088 bytes) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004089 spin_unlock(&space_info->lock);
4090 goto commit;
4091 }
4092 spin_unlock(&space_info->lock);
4093
4094 /*
4095 * See if there is some space in the delayed insertion reservation for
4096 * this reservation.
4097 */
4098 if (space_info != delayed_rsv->space_info)
4099 return -ENOSPC;
4100
Liu Bod9b02182012-02-16 18:34:39 +08004101 spin_lock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004102 spin_lock(&delayed_rsv->lock);
Josef Bacikb150a4f2013-06-19 15:00:04 -04004103 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4104 bytes - delayed_rsv->size) >= 0) {
Josef Bacik663350a2011-11-03 22:54:25 -04004105 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004106 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004107 return -ENOSPC;
4108 }
4109 spin_unlock(&delayed_rsv->lock);
Liu Bod9b02182012-02-16 18:34:39 +08004110 spin_unlock(&space_info->lock);
Josef Bacik663350a2011-11-03 22:54:25 -04004111
4112commit:
4113 trans = btrfs_join_transaction(root);
4114 if (IS_ERR(trans))
4115 return -ENOSPC;
4116
4117 return btrfs_commit_transaction(trans, root);
4118}
4119
Josef Bacik96c3f432012-06-21 14:05:49 -04004120enum flush_state {
Josef Bacik67b0fd62012-09-24 13:42:00 -04004121 FLUSH_DELAYED_ITEMS_NR = 1,
4122 FLUSH_DELAYED_ITEMS = 2,
4123 FLUSH_DELALLOC = 3,
4124 FLUSH_DELALLOC_WAIT = 4,
Josef Bacikea658ba2012-09-11 16:57:25 -04004125 ALLOC_CHUNK = 5,
4126 COMMIT_TRANS = 6,
Josef Bacik96c3f432012-06-21 14:05:49 -04004127};
4128
4129static int flush_space(struct btrfs_root *root,
4130 struct btrfs_space_info *space_info, u64 num_bytes,
4131 u64 orig_bytes, int state)
4132{
4133 struct btrfs_trans_handle *trans;
4134 int nr;
Josef Bacikf4c738c2012-07-02 17:10:51 -04004135 int ret = 0;
Josef Bacik96c3f432012-06-21 14:05:49 -04004136
4137 switch (state) {
Josef Bacik96c3f432012-06-21 14:05:49 -04004138 case FLUSH_DELAYED_ITEMS_NR:
4139 case FLUSH_DELAYED_ITEMS:
4140 if (state == FLUSH_DELAYED_ITEMS_NR) {
4141 u64 bytes = btrfs_calc_trans_metadata_size(root, 1);
4142
4143 nr = (int)div64_u64(num_bytes, bytes);
4144 if (!nr)
4145 nr = 1;
4146 nr *= 2;
4147 } else {
4148 nr = -1;
4149 }
4150 trans = btrfs_join_transaction(root);
4151 if (IS_ERR(trans)) {
4152 ret = PTR_ERR(trans);
4153 break;
4154 }
4155 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4156 btrfs_end_transaction(trans, root);
4157 break;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004158 case FLUSH_DELALLOC:
4159 case FLUSH_DELALLOC_WAIT:
4160 shrink_delalloc(root, num_bytes, orig_bytes,
4161 state == FLUSH_DELALLOC_WAIT);
4162 break;
Josef Bacikea658ba2012-09-11 16:57:25 -04004163 case ALLOC_CHUNK:
4164 trans = btrfs_join_transaction(root);
4165 if (IS_ERR(trans)) {
4166 ret = PTR_ERR(trans);
4167 break;
4168 }
4169 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Josef Bacikea658ba2012-09-11 16:57:25 -04004170 btrfs_get_alloc_profile(root, 0),
4171 CHUNK_ALLOC_NO_FORCE);
4172 btrfs_end_transaction(trans, root);
4173 if (ret == -ENOSPC)
4174 ret = 0;
4175 break;
Josef Bacik96c3f432012-06-21 14:05:49 -04004176 case COMMIT_TRANS:
4177 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4178 break;
4179 default:
4180 ret = -ENOSPC;
4181 break;
4182 }
4183
4184 return ret;
4185}
Josef Bacik663350a2011-11-03 22:54:25 -04004186/**
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004187 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4188 * @root - the root we're allocating for
4189 * @block_rsv - the block_rsv we're allocating for
4190 * @orig_bytes - the number of bytes we want
Adam Buchbinder48fc7f72012-09-19 21:48:00 -04004191 * @flush - whether or not we can flush to make our reservation
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004192 *
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004193 * This will reserve orgi_bytes number of bytes from the space info associated
4194 * with the block_rsv. If there is not enough space it will make an attempt to
4195 * flush out space to make room. It will do this by flushing delalloc if
4196 * possible or committing the transaction. If flush is 0 then no attempts to
4197 * regain reservations will be made and this will fail if there is not enough
4198 * space already.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004199 */
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004200static int reserve_metadata_bytes(struct btrfs_root *root,
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004201 struct btrfs_block_rsv *block_rsv,
Miao Xie08e007d2012-10-16 11:33:38 +00004202 u64 orig_bytes,
4203 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004204{
4205 struct btrfs_space_info *space_info = block_rsv->space_info;
Josef Bacik2bf64752011-09-26 17:12:22 -04004206 u64 used;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004207 u64 num_bytes = orig_bytes;
Josef Bacik67b0fd62012-09-24 13:42:00 -04004208 int flush_state = FLUSH_DELAYED_ITEMS_NR;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004209 int ret = 0;
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004210 bool flushing = false;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004211
4212again:
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004213 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004214 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004215 /*
Miao Xie08e007d2012-10-16 11:33:38 +00004216 * We only want to wait if somebody other than us is flushing and we
4217 * are actually allowed to flush all things.
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004218 */
Miao Xie08e007d2012-10-16 11:33:38 +00004219 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4220 space_info->flush) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004221 spin_unlock(&space_info->lock);
4222 /*
4223 * If we have a trans handle we can't wait because the flusher
4224 * may have to commit the transaction, which would mean we would
4225 * deadlock since we are waiting for the flusher to finish, but
4226 * hold the current transaction open.
4227 */
Josef Bacik663350a2011-11-03 22:54:25 -04004228 if (current->journal_info)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004229 return -EAGAIN;
Arne Jansenb9688bb2012-04-18 10:27:16 +02004230 ret = wait_event_killable(space_info->wait, !space_info->flush);
4231 /* Must have been killed, return */
4232 if (ret)
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004233 return -EINTR;
4234
4235 spin_lock(&space_info->lock);
4236 }
4237
4238 ret = -ENOSPC;
Josef Bacik2bf64752011-09-26 17:12:22 -04004239 used = space_info->bytes_used + space_info->bytes_reserved +
4240 space_info->bytes_pinned + space_info->bytes_readonly +
4241 space_info->bytes_may_use;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004242
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004243 /*
4244 * The idea here is that we've not already over-reserved the block group
4245 * then we can go ahead and save our reservation first and then start
4246 * flushing if we need to. Otherwise if we've already overcommitted
4247 * lets start flushing stuff first and then come back and try to make
4248 * our reservation.
4249 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004250 if (used <= space_info->total_bytes) {
4251 if (used + orig_bytes <= space_info->total_bytes) {
Josef Bacikfb25e912011-07-26 17:00:46 -04004252 space_info->bytes_may_use += orig_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004253 trace_btrfs_space_reservation(root->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04004254 "space_info", space_info->flags, orig_bytes, 1);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004255 ret = 0;
4256 } else {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004257 /*
4258 * Ok set num_bytes to orig_bytes since we aren't
4259 * overocmmitted, this way we only try and reclaim what
4260 * we need.
4261 */
4262 num_bytes = orig_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004263 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004264 } else {
4265 /*
4266 * Ok we're over committed, set num_bytes to the overcommitted
4267 * amount plus the amount of bytes that we need for this
4268 * reservation.
4269 */
Josef Bacik2bf64752011-09-26 17:12:22 -04004270 num_bytes = used - space_info->total_bytes +
Josef Bacik96c3f432012-06-21 14:05:49 -04004271 (orig_bytes * 2);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004272 }
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004273
Josef Bacik44734ed2012-09-28 16:04:19 -04004274 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4275 space_info->bytes_may_use += orig_bytes;
4276 trace_btrfs_space_reservation(root->fs_info, "space_info",
4277 space_info->flags, orig_bytes,
4278 1);
4279 ret = 0;
Josef Bacik2bf64752011-09-26 17:12:22 -04004280 }
4281
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004282 /*
4283 * Couldn't make our reservation, save our place so while we're trying
4284 * to reclaim space we can actually use it instead of somebody else
4285 * stealing it from us.
Miao Xie08e007d2012-10-16 11:33:38 +00004286 *
4287 * We make the other tasks wait for the flush only when we can flush
4288 * all things.
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004289 */
Josef Bacik72bcd992012-12-18 15:16:34 -05004290 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004291 flushing = true;
4292 space_info->flush = 1;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004293 }
4294
Yan, Zhengf0486c62010-05-16 10:46:25 -04004295 spin_unlock(&space_info->lock);
4296
Miao Xie08e007d2012-10-16 11:33:38 +00004297 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004298 goto out;
4299
Josef Bacik96c3f432012-06-21 14:05:49 -04004300 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4301 flush_state);
4302 flush_state++;
Miao Xie08e007d2012-10-16 11:33:38 +00004303
4304 /*
4305 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4306 * would happen. So skip delalloc flush.
4307 */
4308 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4309 (flush_state == FLUSH_DELALLOC ||
4310 flush_state == FLUSH_DELALLOC_WAIT))
4311 flush_state = ALLOC_CHUNK;
4312
Josef Bacik96c3f432012-06-21 14:05:49 -04004313 if (!ret)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004314 goto again;
Miao Xie08e007d2012-10-16 11:33:38 +00004315 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4316 flush_state < COMMIT_TRANS)
4317 goto again;
4318 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4319 flush_state <= COMMIT_TRANS)
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004320 goto again;
4321
4322out:
Josef Bacik5d803662013-02-07 16:06:02 -05004323 if (ret == -ENOSPC &&
4324 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4325 struct btrfs_block_rsv *global_rsv =
4326 &root->fs_info->global_block_rsv;
4327
4328 if (block_rsv != global_rsv &&
4329 !block_rsv_use_bytes(global_rsv, orig_bytes))
4330 ret = 0;
4331 }
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004332 if (flushing) {
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004333 spin_lock(&space_info->lock);
Josef Bacikfdb5eff2011-06-07 16:07:44 -04004334 space_info->flush = 0;
4335 wake_up_all(&space_info->wait);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004336 spin_unlock(&space_info->lock);
4337 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04004338 return ret;
4339}
4340
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004341static struct btrfs_block_rsv *get_block_rsv(
4342 const struct btrfs_trans_handle *trans,
4343 const struct btrfs_root *root)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004344{
Josef Bacik4c13d752011-08-30 11:31:29 -04004345 struct btrfs_block_rsv *block_rsv = NULL;
4346
Josef Bacik0e721102012-06-26 16:13:18 -04004347 if (root->ref_cows)
4348 block_rsv = trans->block_rsv;
4349
4350 if (root == root->fs_info->csum_root && trans->adding_csums)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004351 block_rsv = trans->block_rsv;
Josef Bacik4c13d752011-08-30 11:31:29 -04004352
4353 if (!block_rsv)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004354 block_rsv = root->block_rsv;
4355
4356 if (!block_rsv)
4357 block_rsv = &root->fs_info->empty_block_rsv;
4358
4359 return block_rsv;
4360}
4361
4362static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4363 u64 num_bytes)
4364{
4365 int ret = -ENOSPC;
4366 spin_lock(&block_rsv->lock);
4367 if (block_rsv->reserved >= num_bytes) {
4368 block_rsv->reserved -= num_bytes;
4369 if (block_rsv->reserved < block_rsv->size)
4370 block_rsv->full = 0;
4371 ret = 0;
4372 }
4373 spin_unlock(&block_rsv->lock);
4374 return ret;
4375}
4376
4377static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4378 u64 num_bytes, int update_size)
4379{
4380 spin_lock(&block_rsv->lock);
4381 block_rsv->reserved += num_bytes;
4382 if (update_size)
4383 block_rsv->size += num_bytes;
4384 else if (block_rsv->reserved >= block_rsv->size)
4385 block_rsv->full = 1;
4386 spin_unlock(&block_rsv->lock);
4387}
4388
Josef Bacikd52be812013-05-29 14:54:47 -04004389int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4390 struct btrfs_block_rsv *dest, u64 num_bytes,
4391 int min_factor)
4392{
4393 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4394 u64 min_bytes;
4395
4396 if (global_rsv->space_info != dest->space_info)
4397 return -ENOSPC;
4398
4399 spin_lock(&global_rsv->lock);
4400 min_bytes = div_factor(global_rsv->size, min_factor);
4401 if (global_rsv->reserved < min_bytes + num_bytes) {
4402 spin_unlock(&global_rsv->lock);
4403 return -ENOSPC;
4404 }
4405 global_rsv->reserved -= num_bytes;
4406 if (global_rsv->reserved < global_rsv->size)
4407 global_rsv->full = 0;
4408 spin_unlock(&global_rsv->lock);
4409
4410 block_rsv_add_bytes(dest, num_bytes, 1);
4411 return 0;
4412}
4413
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004414static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4415 struct btrfs_block_rsv *block_rsv,
David Sterba62a45b62011-04-20 15:52:26 +02004416 struct btrfs_block_rsv *dest, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004417{
4418 struct btrfs_space_info *space_info = block_rsv->space_info;
4419
4420 spin_lock(&block_rsv->lock);
4421 if (num_bytes == (u64)-1)
4422 num_bytes = block_rsv->size;
4423 block_rsv->size -= num_bytes;
4424 if (block_rsv->reserved >= block_rsv->size) {
4425 num_bytes = block_rsv->reserved - block_rsv->size;
4426 block_rsv->reserved = block_rsv->size;
4427 block_rsv->full = 1;
4428 } else {
4429 num_bytes = 0;
4430 }
4431 spin_unlock(&block_rsv->lock);
4432
4433 if (num_bytes > 0) {
4434 if (dest) {
Josef Bacike9e22892011-01-24 21:43:19 +00004435 spin_lock(&dest->lock);
4436 if (!dest->full) {
4437 u64 bytes_to_add;
4438
4439 bytes_to_add = dest->size - dest->reserved;
4440 bytes_to_add = min(num_bytes, bytes_to_add);
4441 dest->reserved += bytes_to_add;
4442 if (dest->reserved >= dest->size)
4443 dest->full = 1;
4444 num_bytes -= bytes_to_add;
4445 }
4446 spin_unlock(&dest->lock);
4447 }
4448 if (num_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04004449 spin_lock(&space_info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04004450 space_info->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004451 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004452 space_info->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004453 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004454 spin_unlock(&space_info->lock);
4455 }
4456 }
4457}
4458
4459static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4460 struct btrfs_block_rsv *dst, u64 num_bytes)
4461{
4462 int ret;
4463
4464 ret = block_rsv_use_bytes(src, num_bytes);
4465 if (ret)
4466 return ret;
4467
4468 block_rsv_add_bytes(dst, num_bytes, 1);
4469 return 0;
4470}
4471
Miao Xie66d8f3d2012-09-06 04:02:28 -06004472void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004473{
4474 memset(rsv, 0, sizeof(*rsv));
4475 spin_lock_init(&rsv->lock);
Miao Xie66d8f3d2012-09-06 04:02:28 -06004476 rsv->type = type;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004477}
4478
Miao Xie66d8f3d2012-09-06 04:02:28 -06004479struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4480 unsigned short type)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004481{
4482 struct btrfs_block_rsv *block_rsv;
4483 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004484
4485 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4486 if (!block_rsv)
4487 return NULL;
4488
Miao Xie66d8f3d2012-09-06 04:02:28 -06004489 btrfs_init_block_rsv(block_rsv, type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004490 block_rsv->space_info = __find_space_info(fs_info,
4491 BTRFS_BLOCK_GROUP_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004492 return block_rsv;
4493}
4494
4495void btrfs_free_block_rsv(struct btrfs_root *root,
4496 struct btrfs_block_rsv *rsv)
4497{
Josef Bacik2aaa6652012-08-29 14:27:18 -04004498 if (!rsv)
4499 return;
Josef Bacikdabdb642011-08-08 12:50:18 -04004500 btrfs_block_rsv_release(root, rsv, (u64)-1);
4501 kfree(rsv);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004502}
4503
Miao Xie08e007d2012-10-16 11:33:38 +00004504int btrfs_block_rsv_add(struct btrfs_root *root,
4505 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4506 enum btrfs_reserve_flush_enum flush)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004507{
4508 int ret;
4509
4510 if (num_bytes == 0)
4511 return 0;
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004512
Miao Xie61b520a2011-11-10 20:45:05 -05004513 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004514 if (!ret) {
4515 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4516 return 0;
4517 }
4518
Yan, Zhengf0486c62010-05-16 10:46:25 -04004519 return ret;
4520}
4521
Josef Bacik4a92b1b2011-08-30 12:34:28 -04004522int btrfs_block_rsv_check(struct btrfs_root *root,
Josef Bacik36ba0222011-10-18 12:15:48 -04004523 struct btrfs_block_rsv *block_rsv, int min_factor)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004524{
4525 u64 num_bytes = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004526 int ret = -ENOSPC;
4527
4528 if (!block_rsv)
4529 return 0;
4530
4531 spin_lock(&block_rsv->lock);
Josef Bacik36ba0222011-10-18 12:15:48 -04004532 num_bytes = div_factor(block_rsv->size, min_factor);
4533 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004534 ret = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004535 spin_unlock(&block_rsv->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004536
Josef Bacik36ba0222011-10-18 12:15:48 -04004537 return ret;
4538}
4539
Miao Xie08e007d2012-10-16 11:33:38 +00004540int btrfs_block_rsv_refill(struct btrfs_root *root,
4541 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4542 enum btrfs_reserve_flush_enum flush)
Josef Bacik36ba0222011-10-18 12:15:48 -04004543{
4544 u64 num_bytes = 0;
4545 int ret = -ENOSPC;
4546
4547 if (!block_rsv)
4548 return 0;
4549
4550 spin_lock(&block_rsv->lock);
4551 num_bytes = min_reserved;
Josef Bacik13553e52011-08-08 13:33:21 -04004552 if (block_rsv->reserved >= num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04004553 ret = 0;
Josef Bacik13553e52011-08-08 13:33:21 -04004554 else
Yan, Zhengf0486c62010-05-16 10:46:25 -04004555 num_bytes -= block_rsv->reserved;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004556 spin_unlock(&block_rsv->lock);
Josef Bacik13553e52011-08-08 13:33:21 -04004557
Yan, Zhengf0486c62010-05-16 10:46:25 -04004558 if (!ret)
4559 return 0;
4560
Miao Xieaa38a712011-11-18 17:43:00 +08004561 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
Josef Bacikdabdb642011-08-08 12:50:18 -04004562 if (!ret) {
4563 block_rsv_add_bytes(block_rsv, num_bytes, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004564 return 0;
4565 }
4566
Josef Bacik13553e52011-08-08 13:33:21 -04004567 return ret;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004568}
4569
4570int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4571 struct btrfs_block_rsv *dst_rsv,
4572 u64 num_bytes)
4573{
4574 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4575}
4576
4577void btrfs_block_rsv_release(struct btrfs_root *root,
4578 struct btrfs_block_rsv *block_rsv,
4579 u64 num_bytes)
4580{
4581 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4582 if (global_rsv->full || global_rsv == block_rsv ||
4583 block_rsv->space_info != global_rsv->space_info)
4584 global_rsv = NULL;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004585 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4586 num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04004587}
4588
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004589/*
4590 * helper to calculate size of global block reservation.
4591 * the desired value is sum of space used by extent tree,
4592 * checksum tree and root tree
4593 */
4594static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4595{
4596 struct btrfs_space_info *sinfo;
4597 u64 num_bytes;
4598 u64 meta_used;
4599 u64 data_used;
David Sterba6c417612011-04-13 15:41:04 +02004600 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004601
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004602 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4603 spin_lock(&sinfo->lock);
4604 data_used = sinfo->bytes_used;
4605 spin_unlock(&sinfo->lock);
4606
4607 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4608 spin_lock(&sinfo->lock);
Josef Bacik6d487552010-10-15 15:13:32 -04004609 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4610 data_used = 0;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004611 meta_used = sinfo->bytes_used;
4612 spin_unlock(&sinfo->lock);
4613
4614 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4615 csum_size * 2;
4616 num_bytes += div64_u64(data_used + meta_used, 50);
4617
4618 if (num_bytes * 3 > meta_used)
Chris Mason8e62c2d2012-04-12 13:46:48 -04004619 num_bytes = div64_u64(meta_used, 3);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004620
4621 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4622}
4623
4624static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4625{
4626 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4627 struct btrfs_space_info *sinfo = block_rsv->space_info;
4628 u64 num_bytes;
4629
4630 num_bytes = calc_global_metadata_size(fs_info);
4631
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004632 spin_lock(&sinfo->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004633 spin_lock(&block_rsv->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004634
Josef Bacikfdf30d12013-03-26 15:31:45 -04004635 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004636
4637 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
Josef Bacik6d487552010-10-15 15:13:32 -04004638 sinfo->bytes_reserved + sinfo->bytes_readonly +
4639 sinfo->bytes_may_use;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004640
4641 if (sinfo->total_bytes > num_bytes) {
4642 num_bytes = sinfo->total_bytes - num_bytes;
4643 block_rsv->reserved += num_bytes;
Josef Bacikfb25e912011-07-26 17:00:46 -04004644 sinfo->bytes_may_use += num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004645 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004646 sinfo->flags, num_bytes, 1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004647 }
4648
4649 if (block_rsv->reserved >= block_rsv->size) {
4650 num_bytes = block_rsv->reserved - block_rsv->size;
Josef Bacikfb25e912011-07-26 17:00:46 -04004651 sinfo->bytes_may_use -= num_bytes;
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004652 trace_btrfs_space_reservation(fs_info, "space_info",
Liu Bo2bcc0322012-03-29 09:57:44 -04004653 sinfo->flags, num_bytes, 0);
Chris Mason36e39c42011-03-12 07:08:42 -05004654 sinfo->reservation_progress++;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004655 block_rsv->reserved = block_rsv->size;
4656 block_rsv->full = 1;
4657 }
David Sterba182608c2011-05-05 13:13:16 +02004658
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004659 spin_unlock(&block_rsv->lock);
Stefan Behrens1f699d32012-04-27 12:41:46 -04004660 spin_unlock(&sinfo->lock);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004661}
4662
Yan, Zhengf0486c62010-05-16 10:46:25 -04004663static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4664{
4665 struct btrfs_space_info *space_info;
4666
4667 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4668 fs_info->chunk_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004669
4670 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004671 fs_info->global_block_rsv.space_info = space_info;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004672 fs_info->delalloc_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004673 fs_info->trans_block_rsv.space_info = space_info;
4674 fs_info->empty_block_rsv.space_info = space_info;
Josef Bacik6d668dd2011-11-03 22:54:25 -04004675 fs_info->delayed_block_rsv.space_info = space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004676
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004677 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4678 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4679 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4680 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
Stefan Behrens3a6cad92013-05-16 14:48:19 +00004681 if (fs_info->quota_root)
4682 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04004683 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004684
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004685 update_global_block_rsv(fs_info);
4686}
4687
4688static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4689{
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004690 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4691 (u64)-1);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04004692 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4693 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4694 WARN_ON(fs_info->trans_block_rsv.size > 0);
4695 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4696 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4697 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
Josef Bacik6d668dd2011-11-03 22:54:25 -04004698 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4699 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
Josef Bacikfcb80c22011-05-03 10:40:22 -04004700}
4701
Yan, Zhenga22285a2010-05-16 10:48:46 -04004702void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4703 struct btrfs_root *root)
4704{
Josef Bacik0e721102012-06-26 16:13:18 -04004705 if (!trans->block_rsv)
4706 return;
4707
Yan, Zhenga22285a2010-05-16 10:48:46 -04004708 if (!trans->bytes_reserved)
4709 return;
4710
Chris Masone77266e2012-02-24 10:39:05 -05004711 trace_btrfs_space_reservation(root->fs_info, "transaction",
Liu Bo2bcc0322012-03-29 09:57:44 -04004712 trans->transid, trans->bytes_reserved, 0);
Josef Bacikb24e03d2011-10-14 14:40:17 -04004713 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004714 trans->bytes_reserved = 0;
4715}
4716
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004717/* Can only return 0 or -ENOSPC */
Yan, Zhengd68fc572010-05-16 10:49:58 -04004718int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4719 struct inode *inode)
4720{
4721 struct btrfs_root *root = BTRFS_I(inode)->root;
4722 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4723 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4724
4725 /*
Josef Bacikfcb80c22011-05-03 10:40:22 -04004726 * We need to hold space in order to delete our orphan item once we've
4727 * added it, so this takes the reservation so we can release it later
4728 * when we are truly done with the orphan item.
Yan, Zhengd68fc572010-05-16 10:49:58 -04004729 */
Chris Masonff5714c2011-05-28 07:00:39 -04004730 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004731 trace_btrfs_space_reservation(root->fs_info, "orphan",
4732 btrfs_ino(inode), num_bytes, 1);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004733 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4734}
4735
4736void btrfs_orphan_release_metadata(struct inode *inode)
4737{
4738 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonff5714c2011-05-28 07:00:39 -04004739 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004740 trace_btrfs_space_reservation(root->fs_info, "orphan",
4741 btrfs_ino(inode), num_bytes, 0);
Yan, Zhengd68fc572010-05-16 10:49:58 -04004742 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4743}
4744
Miao Xied5c12072013-02-28 10:04:33 +00004745/*
4746 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4747 * root: the root of the parent directory
4748 * rsv: block reservation
4749 * items: the number of items that we need do reservation
4750 * qgroup_reserved: used to return the reserved size in qgroup
4751 *
4752 * This function is used to reserve the space for snapshot/subvolume
4753 * creation and deletion. Those operations are different with the
4754 * common file/directory operations, they change two fs/file trees
4755 * and root tree, the number of items that the qgroup reserves is
4756 * different with the free space reservation. So we can not use
4757 * the space reseravtion mechanism in start_transaction().
4758 */
4759int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4760 struct btrfs_block_rsv *rsv,
4761 int items,
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004762 u64 *qgroup_reserved,
4763 bool use_global_rsv)
Yan, Zhenga22285a2010-05-16 10:48:46 -04004764{
Miao Xied5c12072013-02-28 10:04:33 +00004765 u64 num_bytes;
4766 int ret;
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004767 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Miao Xied5c12072013-02-28 10:04:33 +00004768
4769 if (root->fs_info->quota_enabled) {
4770 /* One for parent inode, two for dir entries */
4771 num_bytes = 3 * root->leafsize;
4772 ret = btrfs_qgroup_reserve(root, num_bytes);
4773 if (ret)
4774 return ret;
4775 } else {
4776 num_bytes = 0;
4777 }
4778
4779 *qgroup_reserved = num_bytes;
4780
4781 num_bytes = btrfs_calc_trans_metadata_size(root, items);
4782 rsv->space_info = __find_space_info(root->fs_info,
4783 BTRFS_BLOCK_GROUP_METADATA);
4784 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
4785 BTRFS_RESERVE_FLUSH_ALL);
Jeff Mahoneyee3441b2013-07-09 16:37:21 -04004786
4787 if (ret == -ENOSPC && use_global_rsv)
4788 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
4789
Miao Xied5c12072013-02-28 10:04:33 +00004790 if (ret) {
4791 if (*qgroup_reserved)
4792 btrfs_qgroup_free(root, *qgroup_reserved);
4793 }
4794
4795 return ret;
4796}
4797
4798void btrfs_subvolume_release_metadata(struct btrfs_root *root,
4799 struct btrfs_block_rsv *rsv,
4800 u64 qgroup_reserved)
4801{
4802 btrfs_block_rsv_release(root, rsv, (u64)-1);
4803 if (qgroup_reserved)
4804 btrfs_qgroup_free(root, qgroup_reserved);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004805}
4806
Josef Bacik7709cde2011-08-04 10:25:02 -04004807/**
4808 * drop_outstanding_extent - drop an outstanding extent
4809 * @inode: the inode we're dropping the extent for
4810 *
4811 * This is called when we are freeing up an outstanding extent, either called
4812 * after an error or after an extent is written. This will return the number of
4813 * reserved extents that need to be freed. This must be called with
4814 * BTRFS_I(inode)->lock held.
4815 */
Josef Bacik9e0baf62011-07-15 15:16:44 +00004816static unsigned drop_outstanding_extent(struct inode *inode)
4817{
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004818 unsigned drop_inode_space = 0;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004819 unsigned dropped_extents = 0;
4820
Josef Bacik9e0baf62011-07-15 15:16:44 +00004821 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4822 BTRFS_I(inode)->outstanding_extents--;
4823
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004824 if (BTRFS_I(inode)->outstanding_extents == 0 &&
Josef Bacik72ac3c02012-05-23 14:13:11 -04004825 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4826 &BTRFS_I(inode)->runtime_flags))
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004827 drop_inode_space = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004828
Josef Bacik9e0baf62011-07-15 15:16:44 +00004829 /*
4830 * If we have more or the same amount of outsanding extents than we have
4831 * reserved then we need to leave the reserved extents count alone.
4832 */
4833 if (BTRFS_I(inode)->outstanding_extents >=
4834 BTRFS_I(inode)->reserved_extents)
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004835 return drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004836
4837 dropped_extents = BTRFS_I(inode)->reserved_extents -
4838 BTRFS_I(inode)->outstanding_extents;
4839 BTRFS_I(inode)->reserved_extents -= dropped_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004840 return dropped_extents + drop_inode_space;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004841}
4842
Josef Bacik7709cde2011-08-04 10:25:02 -04004843/**
4844 * calc_csum_metadata_size - return the amount of metada space that must be
4845 * reserved/free'd for the given bytes.
4846 * @inode: the inode we're manipulating
4847 * @num_bytes: the number of bytes in question
4848 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4849 *
4850 * This adjusts the number of csum_bytes in the inode and then returns the
4851 * correct amount of metadata that must either be reserved or freed. We
4852 * calculate how many checksums we can fit into one leaf and then divide the
4853 * number of bytes that will need to be checksumed by this value to figure out
4854 * how many checksums will be required. If we are adding bytes then the number
4855 * may go up and we will return the number of additional bytes that must be
4856 * reserved. If it is going down we will return the number of bytes that must
4857 * be freed.
4858 *
4859 * This must be called with BTRFS_I(inode)->lock held.
4860 */
4861static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4862 int reserve)
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004863{
Josef Bacik7709cde2011-08-04 10:25:02 -04004864 struct btrfs_root *root = BTRFS_I(inode)->root;
4865 u64 csum_size;
4866 int num_csums_per_leaf;
4867 int num_csums;
4868 int old_csums;
4869
4870 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4871 BTRFS_I(inode)->csum_bytes == 0)
4872 return 0;
4873
4874 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4875 if (reserve)
4876 BTRFS_I(inode)->csum_bytes += num_bytes;
4877 else
4878 BTRFS_I(inode)->csum_bytes -= num_bytes;
4879 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4880 num_csums_per_leaf = (int)div64_u64(csum_size,
4881 sizeof(struct btrfs_csum_item) +
4882 sizeof(struct btrfs_disk_key));
4883 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4884 num_csums = num_csums + num_csums_per_leaf - 1;
4885 num_csums = num_csums / num_csums_per_leaf;
4886
4887 old_csums = old_csums + num_csums_per_leaf - 1;
4888 old_csums = old_csums / num_csums_per_leaf;
4889
4890 /* No change, no need to reserve more */
4891 if (old_csums == num_csums)
4892 return 0;
4893
4894 if (reserve)
4895 return btrfs_calc_trans_metadata_size(root,
4896 num_csums - old_csums);
4897
4898 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004899}
4900
4901int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4902{
4903 struct btrfs_root *root = BTRFS_I(inode)->root;
4904 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004905 u64 to_reserve = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004906 u64 csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004907 unsigned nr_extents = 0;
Josef Bacik660d3f62011-12-09 11:18:51 -05004908 int extra_reserve = 0;
Miao Xie08e007d2012-10-16 11:33:38 +00004909 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
Jan Schmidteb6b88d2013-01-27 23:26:00 -07004910 int ret = 0;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004911 bool delalloc_lock = true;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004912 u64 to_free = 0;
4913 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004914
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004915 /* If we are a free space inode we need to not flush since we will be in
4916 * the middle of a transaction commit. We also don't need the delalloc
4917 * mutex since we won't race with anybody. We need this mostly to make
4918 * lockdep shut its filthy mouth.
4919 */
4920 if (btrfs_is_free_space_inode(inode)) {
Miao Xie08e007d2012-10-16 11:33:38 +00004921 flush = BTRFS_RESERVE_NO_FLUSH;
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004922 delalloc_lock = false;
4923 }
Josef Bacikc09544e2011-08-30 10:19:10 -04004924
Miao Xie08e007d2012-10-16 11:33:38 +00004925 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4926 btrfs_transaction_in_commit(root->fs_info))
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004927 schedule_timeout(1);
4928
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004929 if (delalloc_lock)
4930 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4931
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004932 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik8bb8ab22010-10-15 16:52:49 -04004933
Josef Bacik9e0baf62011-07-15 15:16:44 +00004934 spin_lock(&BTRFS_I(inode)->lock);
4935 BTRFS_I(inode)->outstanding_extents++;
Josef Bacik57a45ced2011-01-25 16:30:38 -05004936
Josef Bacik9e0baf62011-07-15 15:16:44 +00004937 if (BTRFS_I(inode)->outstanding_extents >
Josef Bacik660d3f62011-12-09 11:18:51 -05004938 BTRFS_I(inode)->reserved_extents)
Josef Bacik9e0baf62011-07-15 15:16:44 +00004939 nr_extents = BTRFS_I(inode)->outstanding_extents -
4940 BTRFS_I(inode)->reserved_extents;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004941
4942 /*
4943 * Add an item to reserve for updating the inode when we complete the
4944 * delalloc io.
4945 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04004946 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4947 &BTRFS_I(inode)->runtime_flags)) {
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004948 nr_extents++;
Josef Bacik660d3f62011-12-09 11:18:51 -05004949 extra_reserve = 1;
Josef Bacik7fd2ae22011-11-08 15:47:34 -05004950 }
4951
4952 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04004953 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
Josef Bacik660d3f62011-12-09 11:18:51 -05004954 csum_bytes = BTRFS_I(inode)->csum_bytes;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004955 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik57a45ced2011-01-25 16:30:38 -05004956
Wang Shilong88e081bf2013-03-01 11:36:01 +00004957 if (root->fs_info->quota_enabled) {
Arne Jansenc5567232011-09-14 15:44:05 +02004958 ret = btrfs_qgroup_reserve(root, num_bytes +
4959 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004960 if (ret)
4961 goto out_fail;
Wang Shilonga9870c02013-03-01 11:33:01 +00004962 }
Arne Jansenc5567232011-09-14 15:44:05 +02004963
Wang Shilong88e081bf2013-03-01 11:36:01 +00004964 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4965 if (unlikely(ret)) {
4966 if (root->fs_info->quota_enabled)
Miao Xie4b5829a2012-12-05 10:53:25 +00004967 btrfs_qgroup_free(root, num_bytes +
4968 nr_extents * root->leafsize);
Wang Shilong88e081bf2013-03-01 11:36:01 +00004969 goto out_fail;
Josef Bacik9e0baf62011-07-15 15:16:44 +00004970 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004971
Josef Bacik660d3f62011-12-09 11:18:51 -05004972 spin_lock(&BTRFS_I(inode)->lock);
4973 if (extra_reserve) {
Josef Bacik72ac3c02012-05-23 14:13:11 -04004974 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4975 &BTRFS_I(inode)->runtime_flags);
Josef Bacik660d3f62011-12-09 11:18:51 -05004976 nr_extents--;
4977 }
4978 BTRFS_I(inode)->reserved_extents += nr_extents;
4979 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacikc64c2bd2012-12-14 13:48:14 -05004980
4981 if (delalloc_lock)
4982 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
Josef Bacik660d3f62011-12-09 11:18:51 -05004983
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05004984 if (to_reserve)
4985 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4986 btrfs_ino(inode), to_reserve, 1);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004987 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4988
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04004989 return 0;
Wang Shilong88e081bf2013-03-01 11:36:01 +00004990
4991out_fail:
4992 spin_lock(&BTRFS_I(inode)->lock);
4993 dropped = drop_outstanding_extent(inode);
4994 /*
4995 * If the inodes csum_bytes is the same as the original
4996 * csum_bytes then we know we haven't raced with any free()ers
4997 * so we can just reduce our inodes csum bytes and carry on.
Wang Shilong88e081bf2013-03-01 11:36:01 +00004998 */
Josef Bacikf4881bc2013-03-25 16:03:35 -04004999 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
Wang Shilong88e081bf2013-03-01 11:36:01 +00005000 calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacikf4881bc2013-03-25 16:03:35 -04005001 } else {
5002 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5003 u64 bytes;
5004
5005 /*
5006 * This is tricky, but first we need to figure out how much we
5007 * free'd from any free-ers that occured during this
5008 * reservation, so we reset ->csum_bytes to the csum_bytes
5009 * before we dropped our lock, and then call the free for the
5010 * number of bytes that were freed while we were trying our
5011 * reservation.
5012 */
5013 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5014 BTRFS_I(inode)->csum_bytes = csum_bytes;
5015 to_free = calc_csum_metadata_size(inode, bytes, 0);
5016
5017
5018 /*
5019 * Now we need to see how much we would have freed had we not
5020 * been making this reservation and our ->csum_bytes were not
5021 * artificially inflated.
5022 */
5023 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5024 bytes = csum_bytes - orig_csum_bytes;
5025 bytes = calc_csum_metadata_size(inode, bytes, 0);
5026
5027 /*
5028 * Now reset ->csum_bytes to what it should be. If bytes is
5029 * more than to_free then we would have free'd more space had we
5030 * not had an artificially high ->csum_bytes, so we need to free
5031 * the remainder. If bytes is the same or less then we don't
5032 * need to do anything, the other free-ers did the correct
5033 * thing.
5034 */
5035 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5036 if (bytes > to_free)
5037 to_free = bytes - to_free;
5038 else
5039 to_free = 0;
5040 }
Wang Shilong88e081bf2013-03-01 11:36:01 +00005041 spin_unlock(&BTRFS_I(inode)->lock);
5042 if (dropped)
5043 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5044
5045 if (to_free) {
5046 btrfs_block_rsv_release(root, block_rsv, to_free);
5047 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5048 btrfs_ino(inode), to_free, 0);
5049 }
5050 if (delalloc_lock)
5051 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5052 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005053}
5054
Josef Bacik7709cde2011-08-04 10:25:02 -04005055/**
5056 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5057 * @inode: the inode to release the reservation for
5058 * @num_bytes: the number of bytes we're releasing
5059 *
5060 * This will release the metadata reservation for an inode. This can be called
5061 * once we complete IO for a given set of bytes to release their metadata
5062 * reservations.
5063 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005064void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5065{
5066 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik9e0baf62011-07-15 15:16:44 +00005067 u64 to_free = 0;
5068 unsigned dropped;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005069
5070 num_bytes = ALIGN(num_bytes, root->sectorsize);
Josef Bacik7709cde2011-08-04 10:25:02 -04005071 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005072 dropped = drop_outstanding_extent(inode);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005073
Miao Xie09348562013-02-07 10:12:07 +00005074 if (num_bytes)
5075 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
Josef Bacik7709cde2011-08-04 10:25:02 -04005076 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9e0baf62011-07-15 15:16:44 +00005077 if (dropped > 0)
5078 to_free += btrfs_calc_trans_metadata_size(root, dropped);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005079
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005080 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5081 btrfs_ino(inode), to_free, 0);
Arne Jansenc5567232011-09-14 15:44:05 +02005082 if (root->fs_info->quota_enabled) {
5083 btrfs_qgroup_free(root, num_bytes +
5084 dropped * root->leafsize);
5085 }
5086
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005087 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5088 to_free);
5089}
5090
Josef Bacik7709cde2011-08-04 10:25:02 -04005091/**
5092 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5093 * @inode: inode we're writing to
5094 * @num_bytes: the number of bytes we want to allocate
5095 *
5096 * This will do the following things
5097 *
5098 * o reserve space in the data space info for num_bytes
5099 * o reserve space in the metadata space info based on number of outstanding
5100 * extents and how much csums will be needed
5101 * o add to the inodes ->delalloc_bytes
5102 * o add it to the fs_info's delalloc inodes list.
5103 *
5104 * This will return 0 for success and -ENOSPC if there is no space left.
5105 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005106int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5107{
5108 int ret;
5109
5110 ret = btrfs_check_data_free_space(inode, num_bytes);
5111 if (ret)
5112 return ret;
5113
5114 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5115 if (ret) {
5116 btrfs_free_reserved_data_space(inode, num_bytes);
5117 return ret;
5118 }
5119
5120 return 0;
5121}
5122
Josef Bacik7709cde2011-08-04 10:25:02 -04005123/**
5124 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5125 * @inode: inode we're releasing space for
5126 * @num_bytes: the number of bytes we want to free up
5127 *
5128 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5129 * called in the case that we don't need the metadata AND data reservations
5130 * anymore. So if there is an error or we insert an inline extent.
5131 *
5132 * This function will release the metadata space that was not used and will
5133 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5134 * list if there are no delalloc bytes left.
5135 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04005136void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5137{
5138 btrfs_delalloc_release_metadata(inode, num_bytes);
5139 btrfs_free_reserved_data_space(inode, num_bytes);
5140}
5141
Liu Boc53d6132012-12-27 09:01:19 +00005142static int update_block_group(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04005143 u64 bytenr, u64 num_bytes, int alloc)
Chris Mason9078a3e2007-04-26 16:46:15 -04005144{
Josef Bacik0af3d002010-06-21 14:48:16 -04005145 struct btrfs_block_group_cache *cache = NULL;
Chris Mason9078a3e2007-04-26 16:46:15 -04005146 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04005147 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005148 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04005149 u64 byte_in_group;
Josef Bacik0af3d002010-06-21 14:48:16 -04005150 int factor;
Chris Mason3e1ad542007-05-07 20:03:49 -04005151
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005152 /* block accounting for super block */
Miao Xieeb73c1b2013-05-15 07:48:22 +00005153 spin_lock(&info->delalloc_root_lock);
David Sterba6c417612011-04-13 15:41:04 +02005154 old_val = btrfs_super_bytes_used(info->super_copy);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005155 if (alloc)
5156 old_val += num_bytes;
5157 else
5158 old_val -= num_bytes;
David Sterba6c417612011-04-13 15:41:04 +02005159 btrfs_set_super_bytes_used(info->super_copy, old_val);
Miao Xieeb73c1b2013-05-15 07:48:22 +00005160 spin_unlock(&info->delalloc_root_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005161
Chris Masond3977122009-01-05 21:25:51 -05005162 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04005163 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005164 if (!cache)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005165 return -ENOENT;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005166 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5167 BTRFS_BLOCK_GROUP_RAID1 |
5168 BTRFS_BLOCK_GROUP_RAID10))
5169 factor = 2;
5170 else
5171 factor = 1;
Josef Bacik9d66e232010-08-25 16:54:15 -04005172 /*
5173 * If this block group has free space cache written out, we
5174 * need to make sure to load it if we are removing space. This
5175 * is because we need the unpinning stage to actually add the
5176 * space back to the block group, otherwise we will leak space.
5177 */
5178 if (!alloc && cache->cached == BTRFS_CACHE_NO)
Liu Bof6373bf2012-12-27 09:01:18 +00005179 cache_block_group(cache, 1);
Josef Bacik0af3d002010-06-21 14:48:16 -04005180
Chris Masondb945352007-10-15 16:15:53 -04005181 byte_in_group = bytenr - cache->key.objectid;
5182 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04005183
Josef Bacik25179202008-10-29 14:49:05 -04005184 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005185 spin_lock(&cache->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -04005186
Josef Bacik73bc1872011-10-03 14:07:49 -04005187 if (btrfs_test_opt(root, SPACE_CACHE) &&
Josef Bacik0af3d002010-06-21 14:48:16 -04005188 cache->disk_cache_state < BTRFS_DC_CLEAR)
5189 cache->disk_cache_state = BTRFS_DC_CLEAR;
5190
Josef Bacik0f9dd462008-09-23 13:14:11 -04005191 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04005192 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04005193 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04005194 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04005195 old_val += num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005196 btrfs_set_block_group_used(&cache->item, old_val);
5197 cache->reserved -= num_bytes;
Yan Zheng11833d62009-09-11 16:11:19 -04005198 cache->space_info->bytes_reserved -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005199 cache->space_info->bytes_used += num_bytes;
5200 cache->space_info->disk_used += num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005201 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005202 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04005203 } else {
Chris Masondb945352007-10-15 16:15:53 -04005204 old_val -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04005205 btrfs_set_block_group_used(&cache->item, old_val);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005206 cache->pinned += num_bytes;
5207 cache->space_info->bytes_pinned += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005208 cache->space_info->bytes_used -= num_bytes;
Yan, Zhengb742bb82010-05-16 10:46:24 -04005209 cache->space_info->disk_used -= num_bytes * factor;
Chris Masonc286ac42008-07-22 23:06:41 -04005210 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005211 spin_unlock(&cache->space_info->lock);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005212
Yan, Zhengf0486c62010-05-16 10:46:25 -04005213 set_extent_dirty(info->pinned_extents,
5214 bytenr, bytenr + num_bytes - 1,
5215 GFP_NOFS | __GFP_NOFAIL);
Chris Masoncd1bc462007-04-27 10:08:34 -04005216 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04005217 btrfs_put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04005218 total -= num_bytes;
5219 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04005220 }
5221 return 0;
5222}
Chris Mason6324fbf2008-03-24 15:01:59 -04005223
Chris Masona061fc82008-05-07 11:43:44 -04005224static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5225{
Josef Bacik0f9dd462008-09-23 13:14:11 -04005226 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005227 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005228
Liu Boa1897fd2012-12-27 09:01:23 +00005229 spin_lock(&root->fs_info->block_group_cache_lock);
5230 bytenr = root->fs_info->first_logical_byte;
5231 spin_unlock(&root->fs_info->block_group_cache_lock);
5232
5233 if (bytenr < (u64)-1)
5234 return bytenr;
5235
Josef Bacik0f9dd462008-09-23 13:14:11 -04005236 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5237 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04005238 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04005239
Yan Zhengd2fb3432008-12-11 16:30:39 -05005240 bytenr = cache->key.objectid;
Chris Masonfa9c0d792009-04-03 09:47:43 -04005241 btrfs_put_block_group(cache);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005242
5243 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04005244}
5245
Yan, Zhengf0486c62010-05-16 10:46:25 -04005246static int pin_down_extent(struct btrfs_root *root,
5247 struct btrfs_block_group_cache *cache,
5248 u64 bytenr, u64 num_bytes, int reserved)
Yan324ae4d2007-11-16 14:57:08 -05005249{
Yan Zheng11833d62009-09-11 16:11:19 -04005250 spin_lock(&cache->space_info->lock);
5251 spin_lock(&cache->lock);
5252 cache->pinned += num_bytes;
5253 cache->space_info->bytes_pinned += num_bytes;
5254 if (reserved) {
5255 cache->reserved -= num_bytes;
5256 cache->space_info->bytes_reserved -= num_bytes;
Yan324ae4d2007-11-16 14:57:08 -05005257 }
Yan Zheng11833d62009-09-11 16:11:19 -04005258 spin_unlock(&cache->lock);
5259 spin_unlock(&cache->space_info->lock);
5260
Yan, Zhengf0486c62010-05-16 10:46:25 -04005261 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5262 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
Yan324ae4d2007-11-16 14:57:08 -05005263 return 0;
5264}
Chris Mason9078a3e2007-04-26 16:46:15 -04005265
Yan, Zhengf0486c62010-05-16 10:46:25 -04005266/*
5267 * this function must be called within transaction
5268 */
5269int btrfs_pin_extent(struct btrfs_root *root,
5270 u64 bytenr, u64 num_bytes, int reserved)
Zheng Yane8569812008-09-26 10:05:48 -04005271{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005272 struct btrfs_block_group_cache *cache;
5273
5274 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005275 BUG_ON(!cache); /* Logic error */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005276
5277 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5278
5279 btrfs_put_block_group(cache);
Yan Zheng11833d62009-09-11 16:11:19 -04005280 return 0;
5281}
Zheng Yane8569812008-09-26 10:05:48 -04005282
Yan, Zhengf0486c62010-05-16 10:46:25 -04005283/*
Chris Masone688b7252011-10-31 20:52:39 -04005284 * this function must be called within transaction
Yan, Zhengf0486c62010-05-16 10:46:25 -04005285 */
Liu Bodcfac412012-12-27 09:01:20 +00005286int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
Chris Masone688b7252011-10-31 20:52:39 -04005287 u64 bytenr, u64 num_bytes)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005288{
Chris Masone688b7252011-10-31 20:52:39 -04005289 struct btrfs_block_group_cache *cache;
Josef Bacikb50c6e22013-04-25 15:55:30 -04005290 int ret;
Chris Masone688b7252011-10-31 20:52:39 -04005291
5292 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005293 if (!cache)
5294 return -EINVAL;
Chris Masone688b7252011-10-31 20:52:39 -04005295
5296 /*
5297 * pull in the free space cache (if any) so that our pin
5298 * removes the free space from the cache. We have load_only set
5299 * to one because the slow code to read in the free extents does check
5300 * the pinned extents.
5301 */
Liu Bof6373bf2012-12-27 09:01:18 +00005302 cache_block_group(cache, 1);
Chris Masone688b7252011-10-31 20:52:39 -04005303
5304 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5305
5306 /* remove us from the free space cache (if we're there at all) */
Josef Bacikb50c6e22013-04-25 15:55:30 -04005307 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
Chris Masone688b7252011-10-31 20:52:39 -04005308 btrfs_put_block_group(cache);
Josef Bacikb50c6e22013-04-25 15:55:30 -04005309 return ret;
Chris Masone688b7252011-10-31 20:52:39 -04005310}
5311
Josef Bacik8c2a1a32013-06-06 13:19:32 -04005312static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5313{
5314 int ret;
5315 struct btrfs_block_group_cache *block_group;
5316 struct btrfs_caching_control *caching_ctl;
5317
5318 block_group = btrfs_lookup_block_group(root->fs_info, start);
5319 if (!block_group)
5320 return -EINVAL;
5321
5322 cache_block_group(block_group, 0);
5323 caching_ctl = get_caching_control(block_group);
5324
5325 if (!caching_ctl) {
5326 /* Logic error */
5327 BUG_ON(!block_group_cache_done(block_group));
5328 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5329 } else {
5330 mutex_lock(&caching_ctl->mutex);
5331
5332 if (start >= caching_ctl->progress) {
5333 ret = add_excluded_extent(root, start, num_bytes);
5334 } else if (start + num_bytes <= caching_ctl->progress) {
5335 ret = btrfs_remove_free_space(block_group,
5336 start, num_bytes);
5337 } else {
5338 num_bytes = caching_ctl->progress - start;
5339 ret = btrfs_remove_free_space(block_group,
5340 start, num_bytes);
5341 if (ret)
5342 goto out_lock;
5343
5344 num_bytes = (start + num_bytes) -
5345 caching_ctl->progress;
5346 start = caching_ctl->progress;
5347 ret = add_excluded_extent(root, start, num_bytes);
5348 }
5349out_lock:
5350 mutex_unlock(&caching_ctl->mutex);
5351 put_caching_control(caching_ctl);
5352 }
5353 btrfs_put_block_group(block_group);
5354 return ret;
5355}
5356
5357int btrfs_exclude_logged_extents(struct btrfs_root *log,
5358 struct extent_buffer *eb)
5359{
5360 struct btrfs_file_extent_item *item;
5361 struct btrfs_key key;
5362 int found_type;
5363 int i;
5364
5365 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5366 return 0;
5367
5368 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5369 btrfs_item_key_to_cpu(eb, &key, i);
5370 if (key.type != BTRFS_EXTENT_DATA_KEY)
5371 continue;
5372 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5373 found_type = btrfs_file_extent_type(eb, item);
5374 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5375 continue;
5376 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5377 continue;
5378 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5379 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5380 __exclude_logged_extent(log, key.objectid, key.offset);
5381 }
5382
5383 return 0;
5384}
5385
Josef Bacikfb25e912011-07-26 17:00:46 -04005386/**
5387 * btrfs_update_reserved_bytes - update the block_group and space info counters
5388 * @cache: The cache we are manipulating
5389 * @num_bytes: The number of bytes in question
5390 * @reserve: One of the reservation enums
5391 *
5392 * This is called by the allocator when it reserves space, or by somebody who is
5393 * freeing space that was never actually used on disk. For example if you
5394 * reserve some space for a new leaf in transaction A and before transaction A
5395 * commits you free that leaf, you call this with reserve set to 0 in order to
5396 * clear the reservation.
5397 *
5398 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5399 * ENOSPC accounting. For data we handle the reservation through clearing the
5400 * delalloc bits in the io_tree. We have to do this since we could end up
5401 * allocating less disk space for the amount of data we have reserved in the
5402 * case of compression.
5403 *
5404 * If this is a reservation and the block group has become read only we cannot
5405 * make the reservation and return -EAGAIN, otherwise this function always
5406 * succeeds.
Yan, Zhengf0486c62010-05-16 10:46:25 -04005407 */
Josef Bacikfb25e912011-07-26 17:00:46 -04005408static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5409 u64 num_bytes, int reserve)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005410{
Josef Bacikfb25e912011-07-26 17:00:46 -04005411 struct btrfs_space_info *space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005412 int ret = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005413
Josef Bacikfb25e912011-07-26 17:00:46 -04005414 spin_lock(&space_info->lock);
5415 spin_lock(&cache->lock);
5416 if (reserve != RESERVE_FREE) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04005417 if (cache->ro) {
5418 ret = -EAGAIN;
5419 } else {
Josef Bacikfb25e912011-07-26 17:00:46 -04005420 cache->reserved += num_bytes;
5421 space_info->bytes_reserved += num_bytes;
5422 if (reserve == RESERVE_ALLOC) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05005423 trace_btrfs_space_reservation(cache->fs_info,
Liu Bo2bcc0322012-03-29 09:57:44 -04005424 "space_info", space_info->flags,
5425 num_bytes, 0);
Josef Bacikfb25e912011-07-26 17:00:46 -04005426 space_info->bytes_may_use -= num_bytes;
5427 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04005428 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005429 } else {
5430 if (cache->ro)
5431 space_info->bytes_readonly += num_bytes;
5432 cache->reserved -= num_bytes;
5433 space_info->bytes_reserved -= num_bytes;
5434 space_info->reservation_progress++;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005435 }
Josef Bacikfb25e912011-07-26 17:00:46 -04005436 spin_unlock(&cache->lock);
5437 spin_unlock(&space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005438 return ret;
5439}
5440
Jeff Mahoney143bede2012-03-01 14:56:26 +01005441void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005442 struct btrfs_root *root)
5443{
5444 struct btrfs_fs_info *fs_info = root->fs_info;
5445 struct btrfs_caching_control *next;
5446 struct btrfs_caching_control *caching_ctl;
5447 struct btrfs_block_group_cache *cache;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005448 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04005449
5450 down_write(&fs_info->extent_commit_sem);
5451
5452 list_for_each_entry_safe(caching_ctl, next,
5453 &fs_info->caching_block_groups, list) {
5454 cache = caching_ctl->block_group;
5455 if (block_group_cache_done(cache)) {
5456 cache->last_byte_to_unpin = (u64)-1;
5457 list_del_init(&caching_ctl->list);
5458 put_caching_control(caching_ctl);
5459 } else {
5460 cache->last_byte_to_unpin = caching_ctl->progress;
5461 }
5462 }
5463
5464 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5465 fs_info->pinned_extents = &fs_info->freed_extents[1];
5466 else
5467 fs_info->pinned_extents = &fs_info->freed_extents[0];
5468
5469 up_write(&fs_info->extent_commit_sem);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005470
Josef Bacikb150a4f2013-06-19 15:00:04 -04005471 list_for_each_entry_rcu(space_info, &fs_info->space_info, list)
5472 percpu_counter_set(&space_info->total_bytes_pinned, 0);
5473
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005474 update_global_block_rsv(fs_info);
Yan Zheng11833d62009-09-11 16:11:19 -04005475}
5476
5477static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5478{
5479 struct btrfs_fs_info *fs_info = root->fs_info;
5480 struct btrfs_block_group_cache *cache = NULL;
Josef Bacik7b398f82012-10-22 15:52:28 -04005481 struct btrfs_space_info *space_info;
5482 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Yan Zheng11833d62009-09-11 16:11:19 -04005483 u64 len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005484 bool readonly;
Yan Zheng11833d62009-09-11 16:11:19 -04005485
5486 while (start <= end) {
Josef Bacik7b398f82012-10-22 15:52:28 -04005487 readonly = false;
Yan Zheng11833d62009-09-11 16:11:19 -04005488 if (!cache ||
5489 start >= cache->key.objectid + cache->key.offset) {
5490 if (cache)
5491 btrfs_put_block_group(cache);
5492 cache = btrfs_lookup_block_group(fs_info, start);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005493 BUG_ON(!cache); /* Logic error */
Yan Zheng11833d62009-09-11 16:11:19 -04005494 }
5495
5496 len = cache->key.objectid + cache->key.offset - start;
5497 len = min(len, end + 1 - start);
5498
5499 if (start < cache->last_byte_to_unpin) {
5500 len = min(len, cache->last_byte_to_unpin - start);
5501 btrfs_add_free_space(cache, start, len);
5502 }
Josef Bacik25179202008-10-29 14:49:05 -04005503
Yan, Zhengf0486c62010-05-16 10:46:25 -04005504 start += len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005505 space_info = cache->space_info;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005506
Josef Bacik7b398f82012-10-22 15:52:28 -04005507 spin_lock(&space_info->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005508 spin_lock(&cache->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005509 cache->pinned -= len;
Josef Bacik7b398f82012-10-22 15:52:28 -04005510 space_info->bytes_pinned -= len;
5511 if (cache->ro) {
5512 space_info->bytes_readonly += len;
5513 readonly = true;
5514 }
Josef Bacik25179202008-10-29 14:49:05 -04005515 spin_unlock(&cache->lock);
Josef Bacik7b398f82012-10-22 15:52:28 -04005516 if (!readonly && global_rsv->space_info == space_info) {
5517 spin_lock(&global_rsv->lock);
5518 if (!global_rsv->full) {
5519 len = min(len, global_rsv->size -
5520 global_rsv->reserved);
5521 global_rsv->reserved += len;
5522 space_info->bytes_may_use += len;
5523 if (global_rsv->reserved >= global_rsv->size)
5524 global_rsv->full = 1;
5525 }
5526 spin_unlock(&global_rsv->lock);
5527 }
5528 spin_unlock(&space_info->lock);
Yan Zheng11833d62009-09-11 16:11:19 -04005529 }
5530
5531 if (cache)
Chris Masonfa9c0d792009-04-03 09:47:43 -04005532 btrfs_put_block_group(cache);
Chris Masonccd467d2007-06-28 15:57:36 -04005533 return 0;
5534}
5535
5536int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
Yan Zheng11833d62009-09-11 16:11:19 -04005537 struct btrfs_root *root)
Chris Masona28ec192007-03-06 20:08:01 -05005538{
Yan Zheng11833d62009-09-11 16:11:19 -04005539 struct btrfs_fs_info *fs_info = root->fs_info;
5540 struct extent_io_tree *unpin;
Chris Mason1a5bc1672007-10-15 16:15:26 -04005541 u64 start;
5542 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05005543 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05005544
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005545 if (trans->aborted)
5546 return 0;
5547
Yan Zheng11833d62009-09-11 16:11:19 -04005548 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5549 unpin = &fs_info->freed_extents[1];
5550 else
5551 unpin = &fs_info->freed_extents[0];
5552
Chris Masond3977122009-01-05 21:25:51 -05005553 while (1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04005554 ret = find_first_extent_bit(unpin, 0, &start, &end,
Josef Bacike6138872012-09-27 17:07:30 -04005555 EXTENT_DIRTY, NULL);
Chris Mason1a5bc1672007-10-15 16:15:26 -04005556 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05005557 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05005558
Li Dongyang5378e602011-03-24 10:24:27 +00005559 if (btrfs_test_opt(root, DISCARD))
5560 ret = btrfs_discard_extent(root, start,
5561 end + 1 - start, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05005562
Chris Mason1a5bc1672007-10-15 16:15:26 -04005563 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Yan Zheng11833d62009-09-11 16:11:19 -04005564 unpin_extent_range(root, start, end);
Chris Masonb9473432009-03-13 11:00:37 -04005565 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05005566 }
Josef Bacik817d52f2009-07-13 21:29:25 -04005567
Chris Masone20d96d2007-03-22 12:13:20 -04005568 return 0;
5569}
5570
Josef Bacikb150a4f2013-06-19 15:00:04 -04005571static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5572 u64 owner, u64 root_objectid)
5573{
5574 struct btrfs_space_info *space_info;
5575 u64 flags;
5576
5577 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5578 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5579 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5580 else
5581 flags = BTRFS_BLOCK_GROUP_METADATA;
5582 } else {
5583 flags = BTRFS_BLOCK_GROUP_DATA;
5584 }
5585
5586 space_info = __find_space_info(fs_info, flags);
5587 BUG_ON(!space_info); /* Logic bug */
5588 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5589}
5590
5591
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005592static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5593 struct btrfs_root *root,
5594 u64 bytenr, u64 num_bytes, u64 parent,
5595 u64 root_objectid, u64 owner_objectid,
5596 u64 owner_offset, int refs_to_drop,
5597 struct btrfs_delayed_extent_op *extent_op)
Chris Masona28ec192007-03-06 20:08:01 -05005598{
Chris Masone2fa7222007-03-12 16:22:34 -04005599 struct btrfs_key key;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005600 struct btrfs_path *path;
Chris Mason1261ec42007-03-20 20:35:03 -04005601 struct btrfs_fs_info *info = root->fs_info;
5602 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04005603 struct extent_buffer *leaf;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005604 struct btrfs_extent_item *ei;
5605 struct btrfs_extent_inline_ref *iref;
Chris Masona28ec192007-03-06 20:08:01 -05005606 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005607 int is_data;
Chris Mason952fcca2008-02-18 16:33:44 -05005608 int extent_slot = 0;
5609 int found_extent = 0;
5610 int num_to_del = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005611 u32 item_size;
5612 u64 refs;
Josef Bacik3173a182013-03-07 14:22:04 -05005613 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5614 SKINNY_METADATA);
Chris Mason037e6392007-03-07 11:50:24 -05005615
Chris Mason5caf2a02007-04-02 11:20:42 -04005616 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04005617 if (!path)
5618 return -ENOMEM;
5619
Chris Mason3c12ac72008-04-21 12:01:38 -04005620 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04005621 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005622
5623 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5624 BUG_ON(!is_data && refs_to_drop != 1);
5625
Josef Bacik3173a182013-03-07 14:22:04 -05005626 if (is_data)
5627 skinny_metadata = 0;
5628
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005629 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5630 bytenr, num_bytes, parent,
5631 root_objectid, owner_objectid,
5632 owner_offset);
Chris Mason7bb86312007-12-11 09:25:06 -05005633 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05005634 extent_slot = path->slots[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005635 while (extent_slot >= 0) {
5636 btrfs_item_key_to_cpu(path->nodes[0], &key,
Chris Mason952fcca2008-02-18 16:33:44 -05005637 extent_slot);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005638 if (key.objectid != bytenr)
Chris Mason952fcca2008-02-18 16:33:44 -05005639 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005640 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5641 key.offset == num_bytes) {
Chris Mason952fcca2008-02-18 16:33:44 -05005642 found_extent = 1;
5643 break;
5644 }
Josef Bacik3173a182013-03-07 14:22:04 -05005645 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5646 key.offset == owner_objectid) {
5647 found_extent = 1;
5648 break;
5649 }
Chris Mason952fcca2008-02-18 16:33:44 -05005650 if (path->slots[0] - extent_slot > 5)
5651 break;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005652 extent_slot--;
Chris Mason952fcca2008-02-18 16:33:44 -05005653 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005654#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5655 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5656 if (found_extent && item_size < sizeof(*ei))
5657 found_extent = 0;
5658#endif
Zheng Yan31840ae2008-09-23 13:14:14 -04005659 if (!found_extent) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005660 BUG_ON(iref);
Chris Mason56bec292009-03-13 10:10:06 -04005661 ret = remove_extent_backref(trans, extent_root, path,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005662 NULL, refs_to_drop,
5663 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005664 if (ret) {
5665 btrfs_abort_transaction(trans, extent_root, ret);
5666 goto out;
5667 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005668 btrfs_release_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04005669 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005670
5671 key.objectid = bytenr;
5672 key.type = BTRFS_EXTENT_ITEM_KEY;
5673 key.offset = num_bytes;
5674
Josef Bacik3173a182013-03-07 14:22:04 -05005675 if (!is_data && skinny_metadata) {
5676 key.type = BTRFS_METADATA_ITEM_KEY;
5677 key.offset = owner_objectid;
5678 }
5679
Zheng Yan31840ae2008-09-23 13:14:14 -04005680 ret = btrfs_search_slot(trans, extent_root,
5681 &key, path, -1, 1);
Josef Bacik3173a182013-03-07 14:22:04 -05005682 if (ret > 0 && skinny_metadata && path->slots[0]) {
5683 /*
5684 * Couldn't find our skinny metadata item,
5685 * see if we have ye olde extent item.
5686 */
5687 path->slots[0]--;
5688 btrfs_item_key_to_cpu(path->nodes[0], &key,
5689 path->slots[0]);
5690 if (key.objectid == bytenr &&
5691 key.type == BTRFS_EXTENT_ITEM_KEY &&
5692 key.offset == num_bytes)
5693 ret = 0;
5694 }
5695
5696 if (ret > 0 && skinny_metadata) {
5697 skinny_metadata = false;
5698 key.type = BTRFS_EXTENT_ITEM_KEY;
5699 key.offset = num_bytes;
5700 btrfs_release_path(path);
5701 ret = btrfs_search_slot(trans, extent_root,
5702 &key, path, -1, 1);
5703 }
5704
Josef Bacikf3465ca2008-11-12 14:19:50 -05005705 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005706 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5707 ret, (unsigned long long)bytenr);
Josef Bacikb783e622011-07-13 15:03:50 +00005708 if (ret > 0)
5709 btrfs_print_leaf(extent_root,
5710 path->nodes[0]);
Josef Bacikf3465ca2008-11-12 14:19:50 -05005711 }
David Sterba005d6422012-09-18 07:52:32 -06005712 if (ret < 0) {
5713 btrfs_abort_transaction(trans, extent_root, ret);
5714 goto out;
5715 }
Zheng Yan31840ae2008-09-23 13:14:14 -04005716 extent_slot = path->slots[0];
5717 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005718 } else if (ret == -ENOENT) {
Chris Mason7bb86312007-12-11 09:25:06 -05005719 btrfs_print_leaf(extent_root, path->nodes[0]);
5720 WARN_ON(1);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005721 btrfs_err(info,
5722 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5723 (unsigned long long)bytenr,
5724 (unsigned long long)parent,
5725 (unsigned long long)root_objectid,
5726 (unsigned long long)owner_objectid,
5727 (unsigned long long)owner_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005728 } else {
David Sterba005d6422012-09-18 07:52:32 -06005729 btrfs_abort_transaction(trans, extent_root, ret);
5730 goto out;
Chris Mason7bb86312007-12-11 09:25:06 -05005731 }
Chris Mason5f39d392007-10-15 16:14:19 -04005732
5733 leaf = path->nodes[0];
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005734 item_size = btrfs_item_size_nr(leaf, extent_slot);
5735#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5736 if (item_size < sizeof(*ei)) {
5737 BUG_ON(found_extent || extent_slot != path->slots[0]);
5738 ret = convert_extent_item_v0(trans, extent_root, path,
5739 owner_objectid, 0);
David Sterba005d6422012-09-18 07:52:32 -06005740 if (ret < 0) {
5741 btrfs_abort_transaction(trans, extent_root, ret);
5742 goto out;
5743 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005744
David Sterbab3b4aa72011-04-21 01:20:15 +02005745 btrfs_release_path(path);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005746 path->leave_spinning = 1;
5747
5748 key.objectid = bytenr;
5749 key.type = BTRFS_EXTENT_ITEM_KEY;
5750 key.offset = num_bytes;
5751
5752 ret = btrfs_search_slot(trans, extent_root, &key, path,
5753 -1, 1);
5754 if (ret) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00005755 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5756 ret, (unsigned long long)bytenr);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005757 btrfs_print_leaf(extent_root, path->nodes[0]);
5758 }
David Sterba005d6422012-09-18 07:52:32 -06005759 if (ret < 0) {
5760 btrfs_abort_transaction(trans, extent_root, ret);
5761 goto out;
5762 }
5763
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005764 extent_slot = path->slots[0];
5765 leaf = path->nodes[0];
5766 item_size = btrfs_item_size_nr(leaf, extent_slot);
5767 }
5768#endif
5769 BUG_ON(item_size < sizeof(*ei));
Chris Mason952fcca2008-02-18 16:33:44 -05005770 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04005771 struct btrfs_extent_item);
Josef Bacik3173a182013-03-07 14:22:04 -05005772 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
5773 key.type == BTRFS_EXTENT_ITEM_KEY) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005774 struct btrfs_tree_block_info *bi;
5775 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
5776 bi = (struct btrfs_tree_block_info *)(ei + 1);
5777 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
Chris Mason952fcca2008-02-18 16:33:44 -05005778 }
5779
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005780 refs = btrfs_extent_refs(leaf, ei);
Josef Bacik32b02532013-04-24 16:38:50 -04005781 if (refs < refs_to_drop) {
5782 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
5783 "for bytenr %Lu\n", refs_to_drop, refs, bytenr);
5784 ret = -EINVAL;
5785 btrfs_abort_transaction(trans, extent_root, ret);
5786 goto out;
5787 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005788 refs -= refs_to_drop;
5789
5790 if (refs > 0) {
5791 if (extent_op)
5792 __run_delayed_extent_op(extent_op, leaf, ei);
5793 /*
5794 * In the case of inline back ref, reference count will
5795 * be updated by remove_extent_backref
5796 */
5797 if (iref) {
5798 BUG_ON(!found_extent);
5799 } else {
5800 btrfs_set_extent_refs(leaf, ei, refs);
5801 btrfs_mark_buffer_dirty(leaf);
5802 }
5803 if (found_extent) {
5804 ret = remove_extent_backref(trans, extent_root, path,
5805 iref, refs_to_drop,
5806 is_data);
David Sterba005d6422012-09-18 07:52:32 -06005807 if (ret) {
5808 btrfs_abort_transaction(trans, extent_root, ret);
5809 goto out;
5810 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005811 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04005812 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
5813 root_objectid);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005814 } else {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005815 if (found_extent) {
5816 BUG_ON(is_data && refs_to_drop !=
5817 extent_data_ref_count(root, path, iref));
5818 if (iref) {
5819 BUG_ON(path->slots[0] != extent_slot);
5820 } else {
5821 BUG_ON(path->slots[0] != extent_slot + 1);
5822 path->slots[0] = extent_slot;
5823 num_to_del = 2;
5824 }
Chris Mason78fae272007-03-25 11:35:08 -04005825 }
Chris Masonb9473432009-03-13 11:00:37 -04005826
Chris Mason952fcca2008-02-18 16:33:44 -05005827 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5828 num_to_del);
David Sterba005d6422012-09-18 07:52:32 -06005829 if (ret) {
5830 btrfs_abort_transaction(trans, extent_root, ret);
5831 goto out;
5832 }
David Sterbab3b4aa72011-04-21 01:20:15 +02005833 btrfs_release_path(path);
David Woodhouse21af8042008-08-12 14:13:26 +01005834
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005835 if (is_data) {
Chris Mason459931e2008-12-10 09:10:46 -05005836 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
David Sterba005d6422012-09-18 07:52:32 -06005837 if (ret) {
5838 btrfs_abort_transaction(trans, extent_root, ret);
5839 goto out;
5840 }
Chris Mason459931e2008-12-10 09:10:46 -05005841 }
5842
Liu Boc53d6132012-12-27 09:01:19 +00005843 ret = update_block_group(root, bytenr, num_bytes, 0);
David Sterba005d6422012-09-18 07:52:32 -06005844 if (ret) {
5845 btrfs_abort_transaction(trans, extent_root, ret);
5846 goto out;
5847 }
Chris Masona28ec192007-03-06 20:08:01 -05005848 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005849out:
Chris Mason5caf2a02007-04-02 11:20:42 -04005850 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05005851 return ret;
5852}
5853
5854/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04005855 * when we free an block, it is possible (and likely) that we free the last
Chris Mason1887be62009-03-13 10:11:24 -04005856 * delayed ref for that extent as well. This searches the delayed ref tree for
5857 * a given extent, and if there are no other delayed refs to be processed, it
5858 * removes it from the tree.
5859 */
5860static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
5861 struct btrfs_root *root, u64 bytenr)
5862{
5863 struct btrfs_delayed_ref_head *head;
5864 struct btrfs_delayed_ref_root *delayed_refs;
5865 struct btrfs_delayed_ref_node *ref;
5866 struct rb_node *node;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005867 int ret = 0;
Chris Mason1887be62009-03-13 10:11:24 -04005868
5869 delayed_refs = &trans->transaction->delayed_refs;
5870 spin_lock(&delayed_refs->lock);
5871 head = btrfs_find_delayed_ref_head(trans, bytenr);
5872 if (!head)
5873 goto out;
5874
5875 node = rb_prev(&head->node.rb_node);
5876 if (!node)
5877 goto out;
5878
5879 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
5880
5881 /* there are still entries for this ref, we can't drop it */
5882 if (ref->bytenr == bytenr)
5883 goto out;
5884
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005885 if (head->extent_op) {
5886 if (!head->must_insert_reserved)
5887 goto out;
Miao Xie78a61842012-11-21 02:21:28 +00005888 btrfs_free_delayed_extent_op(head->extent_op);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005889 head->extent_op = NULL;
5890 }
5891
Chris Mason1887be62009-03-13 10:11:24 -04005892 /*
5893 * waiting for the lock here would deadlock. If someone else has it
5894 * locked they are already in the process of dropping it anyway
5895 */
5896 if (!mutex_trylock(&head->mutex))
5897 goto out;
5898
5899 /*
5900 * at this point we have a head with no other entries. Go
5901 * ahead and process it.
5902 */
5903 head->node.in_tree = 0;
5904 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04005905
Chris Mason1887be62009-03-13 10:11:24 -04005906 delayed_refs->num_entries--;
5907
5908 /*
5909 * we don't take a ref on the node because we're removing it from the
5910 * tree, so we just steal the ref the tree was holding.
5911 */
Chris Masonc3e69d52009-03-13 10:17:05 -04005912 delayed_refs->num_heads--;
5913 if (list_empty(&head->cluster))
5914 delayed_refs->num_heads_ready--;
5915
5916 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04005917 spin_unlock(&delayed_refs->lock);
5918
Yan, Zhengf0486c62010-05-16 10:46:25 -04005919 BUG_ON(head->extent_op);
5920 if (head->must_insert_reserved)
5921 ret = 1;
5922
5923 mutex_unlock(&head->mutex);
Chris Mason1887be62009-03-13 10:11:24 -04005924 btrfs_put_delayed_ref(&head->node);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005925 return ret;
Chris Mason1887be62009-03-13 10:11:24 -04005926out:
5927 spin_unlock(&delayed_refs->lock);
5928 return 0;
5929}
5930
Yan, Zhengf0486c62010-05-16 10:46:25 -04005931void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5932 struct btrfs_root *root,
5933 struct extent_buffer *buf,
Jan Schmidt5581a512012-05-16 17:04:52 +02005934 u64 parent, int last_ref)
Yan, Zhengf0486c62010-05-16 10:46:25 -04005935{
Yan, Zhengf0486c62010-05-16 10:46:25 -04005936 struct btrfs_block_group_cache *cache = NULL;
Josef Bacikb150a4f2013-06-19 15:00:04 -04005937 int pin = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005938 int ret;
5939
5940 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005941 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
5942 buf->start, buf->len,
5943 parent, root->root_key.objectid,
5944 btrfs_header_level(buf),
Jan Schmidt5581a512012-05-16 17:04:52 +02005945 BTRFS_DROP_DELAYED_REF, NULL, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005946 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04005947 }
5948
5949 if (!last_ref)
5950 return;
5951
Yan, Zhengf0486c62010-05-16 10:46:25 -04005952 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005953
5954 if (btrfs_header_generation(buf) == trans->transid) {
5955 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5956 ret = check_ref_cleanup(trans, root, buf->start);
5957 if (!ret)
Josef Bacik37be25b2011-08-05 10:25:38 -04005958 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005959 }
5960
5961 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5962 pin_down_extent(root, cache, buf->start, buf->len, 1);
Josef Bacik37be25b2011-08-05 10:25:38 -04005963 goto out;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005964 }
5965
5966 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5967
5968 btrfs_add_free_space(cache, buf->start, buf->len);
Josef Bacikfb25e912011-07-26 17:00:46 -04005969 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
Josef Bacikb150a4f2013-06-19 15:00:04 -04005970 pin = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04005971 }
5972out:
Josef Bacikb150a4f2013-06-19 15:00:04 -04005973 if (pin)
5974 add_pinned_bytes(root->fs_info, buf->len,
5975 btrfs_header_level(buf),
5976 root->root_key.objectid);
5977
Josef Bacika826d6d2011-03-16 13:42:43 -04005978 /*
5979 * Deleting the buffer, clear the corrupt flag since it doesn't matter
5980 * anymore.
5981 */
5982 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
Yan, Zhengf0486c62010-05-16 10:46:25 -04005983 btrfs_put_block_group(cache);
5984}
5985
Jeff Mahoney79787ea2012-03-12 16:03:00 +01005986/* Can return -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005987int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5988 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
5989 u64 owner, u64 offset, int for_cow)
Chris Mason925baed2008-06-25 16:01:30 -04005990{
5991 int ret;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02005992 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04005993
Josef Bacikb150a4f2013-06-19 15:00:04 -04005994 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
5995
Chris Mason56bec292009-03-13 10:10:06 -04005996 /*
5997 * tree log blocks never actually go into the extent allocation
5998 * tree, just update pinning info and exit early.
Chris Mason56bec292009-03-13 10:10:06 -04005999 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006000 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6001 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
Chris Masonb9473432009-03-13 11:00:37 -04006002 /* unlocks the pinned mutex */
Yan Zheng11833d62009-09-11 16:11:19 -04006003 btrfs_pin_extent(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04006004 ret = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006005 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006006 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6007 num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006008 parent, root_objectid, (int)owner,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006009 BTRFS_DROP_DELAYED_REF, NULL, for_cow);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006010 } else {
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006011 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6012 num_bytes,
6013 parent, root_objectid, owner,
6014 offset, BTRFS_DROP_DELAYED_REF,
6015 NULL, for_cow);
Chris Mason56bec292009-03-13 10:10:06 -04006016 }
Chris Mason925baed2008-06-25 16:01:30 -04006017 return ret;
6018}
6019
David Woodhouse53b381b2013-01-29 18:40:14 -05006020static u64 stripe_align(struct btrfs_root *root,
6021 struct btrfs_block_group_cache *cache,
6022 u64 val, u64 num_bytes)
Chris Mason87ee04e2007-11-30 11:30:34 -05006023{
Qu Wenruofda28322013-02-26 08:10:22 +00006024 u64 ret = ALIGN(val, root->stripesize);
Chris Mason87ee04e2007-11-30 11:30:34 -05006025 return ret;
6026}
6027
Chris Masonfec577f2007-02-26 10:40:21 -05006028/*
Josef Bacik817d52f2009-07-13 21:29:25 -04006029 * when we wait for progress in the block group caching, its because
6030 * our allocation attempt failed at least once. So, we must sleep
6031 * and let some progress happen before we try again.
6032 *
6033 * This function will sleep at least once waiting for new free space to
6034 * show up, and then it will check the block group free space numbers
6035 * for our min num_bytes. Another option is to have it go ahead
6036 * and look in the rbtree for a free extent of a given size, but this
6037 * is a good start.
6038 */
6039static noinline int
6040wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6041 u64 num_bytes)
6042{
Yan Zheng11833d62009-09-11 16:11:19 -04006043 struct btrfs_caching_control *caching_ctl;
Josef Bacik817d52f2009-07-13 21:29:25 -04006044
Yan Zheng11833d62009-09-11 16:11:19 -04006045 caching_ctl = get_caching_control(cache);
6046 if (!caching_ctl)
Josef Bacik817d52f2009-07-13 21:29:25 -04006047 return 0;
Josef Bacik817d52f2009-07-13 21:29:25 -04006048
Yan Zheng11833d62009-09-11 16:11:19 -04006049 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
Li Zefan34d52cb2011-03-29 13:46:06 +08006050 (cache->free_space_ctl->free_space >= num_bytes));
Yan Zheng11833d62009-09-11 16:11:19 -04006051
6052 put_caching_control(caching_ctl);
6053 return 0;
6054}
6055
6056static noinline int
6057wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6058{
6059 struct btrfs_caching_control *caching_ctl;
Yan Zheng11833d62009-09-11 16:11:19 -04006060
6061 caching_ctl = get_caching_control(cache);
6062 if (!caching_ctl)
6063 return 0;
6064
6065 wait_event(caching_ctl->wait, block_group_cache_done(cache));
6066
6067 put_caching_control(caching_ctl);
Josef Bacik817d52f2009-07-13 21:29:25 -04006068 return 0;
6069}
6070
Liu Bo31e50222012-11-21 14:18:10 +00006071int __get_raid_index(u64 flags)
Yan, Zhengb742bb82010-05-16 10:46:24 -04006072{
Ilya Dryomov7738a532012-03-27 17:09:17 +03006073 if (flags & BTRFS_BLOCK_GROUP_RAID10)
Miao Xiee6ec7162013-01-17 05:38:51 +00006074 return BTRFS_RAID_RAID10;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006075 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
Miao Xiee6ec7162013-01-17 05:38:51 +00006076 return BTRFS_RAID_RAID1;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006077 else if (flags & BTRFS_BLOCK_GROUP_DUP)
Miao Xiee6ec7162013-01-17 05:38:51 +00006078 return BTRFS_RAID_DUP;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006079 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
Miao Xiee6ec7162013-01-17 05:38:51 +00006080 return BTRFS_RAID_RAID0;
David Woodhouse53b381b2013-01-29 18:40:14 -05006081 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
Chris Masone942f882013-02-20 14:06:05 -05006082 return BTRFS_RAID_RAID5;
David Woodhouse53b381b2013-01-29 18:40:14 -05006083 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
Chris Masone942f882013-02-20 14:06:05 -05006084 return BTRFS_RAID_RAID6;
Ilya Dryomov7738a532012-03-27 17:09:17 +03006085
Chris Masone942f882013-02-20 14:06:05 -05006086 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
Yan, Zhengb742bb82010-05-16 10:46:24 -04006087}
6088
Ilya Dryomov7738a532012-03-27 17:09:17 +03006089static int get_block_group_index(struct btrfs_block_group_cache *cache)
6090{
Liu Bo31e50222012-11-21 14:18:10 +00006091 return __get_raid_index(cache->flags);
Ilya Dryomov7738a532012-03-27 17:09:17 +03006092}
6093
Josef Bacik817d52f2009-07-13 21:29:25 -04006094enum btrfs_loop_type {
Josef Bacik285ff5a2012-01-13 15:27:45 -05006095 LOOP_CACHING_NOWAIT = 0,
6096 LOOP_CACHING_WAIT = 1,
6097 LOOP_ALLOC_CHUNK = 2,
6098 LOOP_NO_EMPTY_SIZE = 3,
Josef Bacik817d52f2009-07-13 21:29:25 -04006099};
6100
6101/*
Chris Masonfec577f2007-02-26 10:40:21 -05006102 * walks the btree of allocated extents and find a hole of a given size.
6103 * The key ins is changed to record the hole:
6104 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04006105 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05006106 * ins->offset == number of blocks
6107 * Any available blocks before search_start are skipped.
6108 */
Chris Masond3977122009-01-05 21:25:51 -05006109static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05006110 struct btrfs_root *orig_root,
6111 u64 num_bytes, u64 empty_size,
Chris Mason98ed5172008-01-03 10:01:48 -05006112 u64 hint_byte, struct btrfs_key *ins,
David Sterbab6919a52013-04-29 13:39:40 +00006113 u64 flags)
Chris Masonfec577f2007-02-26 10:40:21 -05006114{
Josef Bacik80eb2342008-10-29 14:49:05 -04006115 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05006116 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006117 struct btrfs_free_cluster *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04006118 struct btrfs_block_group_cache *block_group = NULL;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006119 struct btrfs_block_group_cache *used_block_group;
Josef Bacik81c9ad22012-01-18 10:56:06 -05006120 u64 search_start = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04006121 int empty_cluster = 2 * 1024 * 1024;
Josef Bacik80eb2342008-10-29 14:49:05 -04006122 struct btrfs_space_info *space_info;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006123 int loop = 0;
David Sterbab6919a52013-04-29 13:39:40 +00006124 int index = __get_raid_index(flags);
6125 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
Josef Bacikfb25e912011-07-26 17:00:46 -04006126 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
Josef Bacik817d52f2009-07-13 21:29:25 -04006127 bool found_uncached_bg = false;
Josef Bacik0a243252009-09-11 16:11:20 -04006128 bool failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006129 bool failed_alloc = false;
Josef Bacik67377732010-09-16 16:19:09 -04006130 bool use_cluster = true;
Miao Xie60d2adb2011-09-09 17:34:35 +08006131 bool have_caching_bg = false;
Chris Masonfec577f2007-02-26 10:40:21 -05006132
Chris Masondb945352007-10-15 16:15:53 -04006133 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04006134 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04006135 ins->objectid = 0;
6136 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04006137
David Sterbab6919a52013-04-29 13:39:40 +00006138 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
Josef Bacik3f7de032011-11-10 08:29:20 -05006139
David Sterbab6919a52013-04-29 13:39:40 +00006140 space_info = __find_space_info(root->fs_info, flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006141 if (!space_info) {
David Sterbab6919a52013-04-29 13:39:40 +00006142 btrfs_err(root->fs_info, "No space info for %llu", flags);
Josef Bacik1b1d1f62010-03-19 20:49:55 +00006143 return -ENOSPC;
6144 }
Josef Bacik2552d172009-04-03 10:14:19 -04006145
Josef Bacik67377732010-09-16 16:19:09 -04006146 /*
6147 * If the space info is for both data and metadata it means we have a
6148 * small filesystem and we can't use the clustering stuff.
6149 */
6150 if (btrfs_mixed_space_info(space_info))
6151 use_cluster = false;
6152
David Sterbab6919a52013-04-29 13:39:40 +00006153 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006154 last_ptr = &root->fs_info->meta_alloc_cluster;
Chris Mason536ac8a2009-02-12 09:41:38 -05006155 if (!btrfs_test_opt(root, SSD))
6156 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04006157 }
6158
David Sterbab6919a52013-04-29 13:39:40 +00006159 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
Josef Bacik67377732010-09-16 16:19:09 -04006160 btrfs_test_opt(root, SSD)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006161 last_ptr = &root->fs_info->data_alloc_cluster;
6162 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006163
Chris Mason239b14b2008-03-24 15:02:07 -04006164 if (last_ptr) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006165 spin_lock(&last_ptr->lock);
6166 if (last_ptr->block_group)
6167 hint_byte = last_ptr->window_start;
6168 spin_unlock(&last_ptr->lock);
Chris Mason239b14b2008-03-24 15:02:07 -04006169 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006170
Chris Masona061fc82008-05-07 11:43:44 -04006171 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04006172 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04006173
Josef Bacik817d52f2009-07-13 21:29:25 -04006174 if (!last_ptr)
Chris Masonfa9c0d792009-04-03 09:47:43 -04006175 empty_cluster = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006176
Josef Bacik2552d172009-04-03 10:14:19 -04006177 if (search_start == hint_byte) {
Josef Bacik2552d172009-04-03 10:14:19 -04006178 block_group = btrfs_lookup_block_group(root->fs_info,
6179 search_start);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006180 used_block_group = block_group;
Josef Bacik817d52f2009-07-13 21:29:25 -04006181 /*
6182 * we don't want to use the block group if it doesn't match our
6183 * allocation bits, or if its not cached.
Josef Bacikccf0e722009-11-10 21:23:48 -05006184 *
6185 * However if we are re-searching with an ideal block group
6186 * picked out then we don't care that the block group is cached.
Josef Bacik817d52f2009-07-13 21:29:25 -04006187 */
David Sterbab6919a52013-04-29 13:39:40 +00006188 if (block_group && block_group_bits(block_group, flags) &&
Josef Bacik285ff5a2012-01-13 15:27:45 -05006189 block_group->cached != BTRFS_CACHE_NO) {
Josef Bacik2552d172009-04-03 10:14:19 -04006190 down_read(&space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04006191 if (list_empty(&block_group->list) ||
6192 block_group->ro) {
6193 /*
6194 * someone is removing this block group,
6195 * we can't jump into the have_block_group
6196 * target because our list pointers are not
6197 * valid
6198 */
6199 btrfs_put_block_group(block_group);
6200 up_read(&space_info->groups_sem);
Josef Bacikccf0e722009-11-10 21:23:48 -05006201 } else {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006202 index = get_block_group_index(block_group);
Chris Mason44fb5512009-06-04 15:34:51 -04006203 goto have_block_group;
Josef Bacikccf0e722009-11-10 21:23:48 -05006204 }
Josef Bacik2552d172009-04-03 10:14:19 -04006205 } else if (block_group) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006206 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006207 }
Chris Mason42e70e72008-11-07 18:17:11 -05006208 }
Josef Bacik2552d172009-04-03 10:14:19 -04006209search:
Miao Xie60d2adb2011-09-09 17:34:35 +08006210 have_caching_bg = false;
Josef Bacik80eb2342008-10-29 14:49:05 -04006211 down_read(&space_info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006212 list_for_each_entry(block_group, &space_info->block_groups[index],
6213 list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006214 u64 offset;
Josef Bacik817d52f2009-07-13 21:29:25 -04006215 int cached;
Chris Mason8a1413a2008-11-10 16:13:54 -05006216
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006217 used_block_group = block_group;
Josef Bacik11dfe352009-11-13 20:12:59 +00006218 btrfs_get_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006219 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05006220
Chris Mason83a50de2010-12-13 15:06:46 -05006221 /*
6222 * this can happen if we end up cycling through all the
6223 * raid types, but we want to make sure we only allocate
6224 * for the proper type.
6225 */
David Sterbab6919a52013-04-29 13:39:40 +00006226 if (!block_group_bits(block_group, flags)) {
Chris Mason83a50de2010-12-13 15:06:46 -05006227 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6228 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05006229 BTRFS_BLOCK_GROUP_RAID5 |
6230 BTRFS_BLOCK_GROUP_RAID6 |
Chris Mason83a50de2010-12-13 15:06:46 -05006231 BTRFS_BLOCK_GROUP_RAID10;
6232
6233 /*
6234 * if they asked for extra copies and this block group
6235 * doesn't provide them, bail. This does allow us to
6236 * fill raid0 from raid1.
6237 */
David Sterbab6919a52013-04-29 13:39:40 +00006238 if ((flags & extra) && !(block_group->flags & extra))
Chris Mason83a50de2010-12-13 15:06:46 -05006239 goto loop;
6240 }
6241
Josef Bacik2552d172009-04-03 10:14:19 -04006242have_block_group:
Josef Bacik291c7d22011-11-14 13:52:14 -05006243 cached = block_group_cache_done(block_group);
6244 if (unlikely(!cached)) {
Josef Bacik291c7d22011-11-14 13:52:14 -05006245 found_uncached_bg = true;
Liu Bof6373bf2012-12-27 09:01:18 +00006246 ret = cache_block_group(block_group, 0);
Chris Mason1d4284b2012-03-28 20:31:37 -04006247 BUG_ON(ret < 0);
6248 ret = 0;
Josef Bacikea6a4782008-11-20 12:16:16 -05006249 }
6250
Josef Bacikea6a4782008-11-20 12:16:16 -05006251 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04006252 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006253
Josef Bacik0a243252009-09-11 16:11:20 -04006254 /*
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006255 * Ok we want to try and use the cluster allocator, so
6256 * lets look there
Josef Bacik0a243252009-09-11 16:11:20 -04006257 */
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006258 if (last_ptr) {
Chris Mason8de972b2013-01-04 15:39:43 -05006259 unsigned long aligned_cluster;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006260 /*
6261 * the refill lock keeps out other
6262 * people trying to start a new cluster
6263 */
6264 spin_lock(&last_ptr->refill_lock);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006265 used_block_group = last_ptr->block_group;
6266 if (used_block_group != block_group &&
6267 (!used_block_group ||
6268 used_block_group->ro ||
David Sterbab6919a52013-04-29 13:39:40 +00006269 !block_group_bits(used_block_group, flags))) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006270 used_block_group = block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04006271 goto refill_cluster;
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006272 }
Chris Mason44fb5512009-06-04 15:34:51 -04006273
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006274 if (used_block_group != block_group)
6275 btrfs_get_block_group(used_block_group);
6276
6277 offset = btrfs_alloc_from_cluster(used_block_group,
6278 last_ptr, num_bytes, used_block_group->key.objectid);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006279 if (offset) {
6280 /* we have a block, we're done */
6281 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006282 trace_btrfs_reserve_extent_cluster(root,
6283 block_group, search_start, num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006284 goto checks;
6285 }
6286
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006287 WARN_ON(last_ptr->block_group != used_block_group);
6288 if (used_block_group != block_group) {
6289 btrfs_put_block_group(used_block_group);
6290 used_block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006291 }
Chris Mason44fb5512009-06-04 15:34:51 -04006292refill_cluster:
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006293 BUG_ON(used_block_group != block_group);
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006294 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6295 * set up a new clusters, so lets just skip it
6296 * and let the allocator find whatever block
6297 * it can find. If we reach this point, we
6298 * will have tried the cluster allocator
6299 * plenty of times and not have found
6300 * anything, so we are likely way too
6301 * fragmented for the clustering stuff to find
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006302 * anything.
6303 *
6304 * However, if the cluster is taken from the
6305 * current block group, release the cluster
6306 * first, so that we stand a better chance of
6307 * succeeding in the unclustered
6308 * allocation. */
6309 if (loop >= LOOP_NO_EMPTY_SIZE &&
6310 last_ptr->block_group != block_group) {
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006311 spin_unlock(&last_ptr->refill_lock);
6312 goto unclustered_alloc;
6313 }
6314
Chris Masonfa9c0d792009-04-03 09:47:43 -04006315 /*
6316 * this cluster didn't work out, free it and
6317 * start over
6318 */
6319 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6320
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006321 if (loop >= LOOP_NO_EMPTY_SIZE) {
6322 spin_unlock(&last_ptr->refill_lock);
6323 goto unclustered_alloc;
6324 }
6325
Chris Mason8de972b2013-01-04 15:39:43 -05006326 aligned_cluster = max_t(unsigned long,
6327 empty_cluster + empty_size,
6328 block_group->full_stripe_len);
6329
Chris Masonfa9c0d792009-04-03 09:47:43 -04006330 /* allocate a cluster in this block group */
Chris Mason451d7582009-06-09 20:28:34 -04006331 ret = btrfs_find_space_cluster(trans, root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04006332 block_group, last_ptr,
Alexandre Oliva1b22bad2011-11-30 13:43:00 -05006333 search_start, num_bytes,
Chris Mason8de972b2013-01-04 15:39:43 -05006334 aligned_cluster);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006335 if (ret == 0) {
6336 /*
6337 * now pull our allocation out of this
6338 * cluster
6339 */
6340 offset = btrfs_alloc_from_cluster(block_group,
6341 last_ptr, num_bytes,
6342 search_start);
6343 if (offset) {
6344 /* we found one, proceed */
6345 spin_unlock(&last_ptr->refill_lock);
Josef Bacik3f7de032011-11-10 08:29:20 -05006346 trace_btrfs_reserve_extent_cluster(root,
6347 block_group, search_start,
6348 num_bytes);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006349 goto checks;
6350 }
Josef Bacik0a243252009-09-11 16:11:20 -04006351 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6352 && !failed_cluster_refill) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006353 spin_unlock(&last_ptr->refill_lock);
6354
Josef Bacik0a243252009-09-11 16:11:20 -04006355 failed_cluster_refill = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006356 wait_block_group_cache_progress(block_group,
6357 num_bytes + empty_cluster + empty_size);
6358 goto have_block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006359 }
Josef Bacik817d52f2009-07-13 21:29:25 -04006360
Chris Masonfa9c0d792009-04-03 09:47:43 -04006361 /*
6362 * at this point we either didn't find a cluster
6363 * or we weren't able to allocate a block from our
6364 * cluster. Free the cluster we've been trying
6365 * to use, and go to the next block group
6366 */
Josef Bacik0a243252009-09-11 16:11:20 -04006367 btrfs_return_cluster_to_free_space(NULL, last_ptr);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006368 spin_unlock(&last_ptr->refill_lock);
Josef Bacik0a243252009-09-11 16:11:20 -04006369 goto loop;
Chris Masonfa9c0d792009-04-03 09:47:43 -04006370 }
6371
Alexandre Oliva062c05c2011-12-07 19:50:42 -05006372unclustered_alloc:
Alexandre Olivaa5f6f712011-12-12 04:48:19 -02006373 spin_lock(&block_group->free_space_ctl->tree_lock);
6374 if (cached &&
6375 block_group->free_space_ctl->free_space <
6376 num_bytes + empty_cluster + empty_size) {
6377 spin_unlock(&block_group->free_space_ctl->tree_lock);
6378 goto loop;
6379 }
6380 spin_unlock(&block_group->free_space_ctl->tree_lock);
6381
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006382 offset = btrfs_find_space_for_alloc(block_group, search_start,
6383 num_bytes, empty_size);
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006384 /*
6385 * If we didn't find a chunk, and we haven't failed on this
6386 * block group before, and this block group is in the middle of
6387 * caching and we are ok with waiting, then go ahead and wait
6388 * for progress to be made, and set failed_alloc to true.
6389 *
6390 * If failed_alloc is true then we've already waited on this
6391 * block group once and should move on to the next block group.
6392 */
6393 if (!offset && !failed_alloc && !cached &&
6394 loop > LOOP_CACHING_NOWAIT) {
Josef Bacik817d52f2009-07-13 21:29:25 -04006395 wait_block_group_cache_progress(block_group,
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006396 num_bytes + empty_size);
6397 failed_alloc = true;
Josef Bacik817d52f2009-07-13 21:29:25 -04006398 goto have_block_group;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006399 } else if (!offset) {
Miao Xie60d2adb2011-09-09 17:34:35 +08006400 if (!cached)
6401 have_caching_bg = true;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006402 goto loop;
Josef Bacik817d52f2009-07-13 21:29:25 -04006403 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006404checks:
David Woodhouse53b381b2013-01-29 18:40:14 -05006405 search_start = stripe_align(root, used_block_group,
6406 offset, num_bytes);
Chris Masone37c9e62007-05-09 20:13:14 -04006407
Josef Bacik2552d172009-04-03 10:14:19 -04006408 /* move on to the next group */
6409 if (search_start + num_bytes >
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006410 used_block_group->key.objectid + used_block_group->key.offset) {
6411 btrfs_add_free_space(used_block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04006412 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006413 }
Josef Bacik80eb2342008-10-29 14:49:05 -04006414
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006415 if (offset < search_start)
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006416 btrfs_add_free_space(used_block_group, offset,
Josef Bacik6226cb0a2009-04-03 10:14:18 -04006417 search_start - offset);
6418 BUG_ON(offset > search_start);
6419
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006420 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes,
Josef Bacikfb25e912011-07-26 17:00:46 -04006421 alloc_type);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006422 if (ret == -EAGAIN) {
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006423 btrfs_add_free_space(used_block_group, offset, num_bytes);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006424 goto loop;
6425 }
Yan Zheng11833d62009-09-11 16:11:19 -04006426
Josef Bacik2552d172009-04-03 10:14:19 -04006427 /* we are all good, lets return */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006428 ins->objectid = search_start;
6429 ins->offset = num_bytes;
6430
Josef Bacik3f7de032011-11-10 08:29:20 -05006431 trace_btrfs_reserve_extent(orig_root, block_group,
6432 search_start, num_bytes);
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006433 if (used_block_group != block_group)
6434 btrfs_put_block_group(used_block_group);
Josef Bacikd82a6f12011-05-11 15:26:06 -04006435 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006436 break;
6437loop:
Josef Bacik0a243252009-09-11 16:11:20 -04006438 failed_cluster_refill = false;
Josef Bacik1cdda9b2009-10-06 10:04:28 -04006439 failed_alloc = false;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006440 BUG_ON(index != get_block_group_index(block_group));
Alexandre Oliva274bd4f2011-12-07 20:08:40 -05006441 if (used_block_group != block_group)
6442 btrfs_put_block_group(used_block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04006443 btrfs_put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04006444 }
6445 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05006446
Miao Xie60d2adb2011-09-09 17:34:35 +08006447 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6448 goto search;
6449
Yan, Zhengb742bb82010-05-16 10:46:24 -04006450 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6451 goto search;
6452
Josef Bacik285ff5a2012-01-13 15:27:45 -05006453 /*
Josef Bacikccf0e722009-11-10 21:23:48 -05006454 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6455 * caching kthreads as we move along
Josef Bacik817d52f2009-07-13 21:29:25 -04006456 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6457 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6458 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6459 * again
Chris Masonfa9c0d792009-04-03 09:47:43 -04006460 */
Josef Bacik723bda22011-05-27 16:11:38 -04006461 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
Yan, Zhengb742bb82010-05-16 10:46:24 -04006462 index = 0;
Josef Bacik723bda22011-05-27 16:11:38 -04006463 loop++;
Josef Bacik817d52f2009-07-13 21:29:25 -04006464 if (loop == LOOP_ALLOC_CHUNK) {
David Sterbab6919a52013-04-29 13:39:40 +00006465 ret = do_chunk_alloc(trans, root, flags,
Josef Bacikea658ba2012-09-11 16:57:25 -04006466 CHUNK_ALLOC_FORCE);
6467 /*
6468 * Do not bail out on ENOSPC since we
6469 * can do more things.
6470 */
6471 if (ret < 0 && ret != -ENOSPC) {
6472 btrfs_abort_transaction(trans,
6473 root, ret);
6474 goto out;
Josef Bacik723bda22011-05-27 16:11:38 -04006475 }
Josef Bacik723bda22011-05-27 16:11:38 -04006476 }
6477
6478 if (loop == LOOP_NO_EMPTY_SIZE) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04006479 empty_size = 0;
6480 empty_cluster = 0;
6481 }
Chris Mason42e70e72008-11-07 18:17:11 -05006482
Josef Bacik723bda22011-05-27 16:11:38 -04006483 goto search;
Josef Bacik2552d172009-04-03 10:14:19 -04006484 } else if (!ins->objectid) {
6485 ret = -ENOSPC;
Josef Bacikd82a6f12011-05-11 15:26:06 -04006486 } else if (ins->objectid) {
Josef Bacik2552d172009-04-03 10:14:19 -04006487 ret = 0;
6488 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006489out:
Chris Mason0b86a832008-03-24 15:01:56 -04006490
Chris Mason0f70abe2007-02-28 16:46:22 -05006491 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05006492}
Chris Masonec44a352008-04-28 15:29:52 -04006493
Josef Bacik9ed74f22009-09-11 16:12:44 -04006494static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6495 int dump_block_groups)
Josef Bacik0f9dd462008-09-23 13:14:11 -04006496{
6497 struct btrfs_block_group_cache *cache;
Yan, Zhengb742bb82010-05-16 10:46:24 -04006498 int index = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006499
Josef Bacik9ed74f22009-09-11 16:12:44 -04006500 spin_lock(&info->lock);
Josef Bacikfb25e912011-07-26 17:00:46 -04006501 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
6502 (unsigned long long)info->flags,
Chris Masond3977122009-01-05 21:25:51 -05006503 (unsigned long long)(info->total_bytes - info->bytes_used -
Josef Bacik9ed74f22009-09-11 16:12:44 -04006504 info->bytes_pinned - info->bytes_reserved -
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006505 info->bytes_readonly),
Chris Masond3977122009-01-05 21:25:51 -05006506 (info->full) ? "" : "not ");
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006507 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
6508 "reserved=%llu, may_use=%llu, readonly=%llu\n",
Joel Becker21380932009-04-21 12:38:29 -07006509 (unsigned long long)info->total_bytes,
Josef Bacik9ed74f22009-09-11 16:12:44 -04006510 (unsigned long long)info->bytes_used,
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04006511 (unsigned long long)info->bytes_pinned,
6512 (unsigned long long)info->bytes_reserved,
6513 (unsigned long long)info->bytes_may_use,
6514 (unsigned long long)info->bytes_readonly);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006515 spin_unlock(&info->lock);
6516
6517 if (!dump_block_groups)
6518 return;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006519
Josef Bacik80eb2342008-10-29 14:49:05 -04006520 down_read(&info->groups_sem);
Yan, Zhengb742bb82010-05-16 10:46:24 -04006521again:
6522 list_for_each_entry(cache, &info->block_groups[index], list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04006523 spin_lock(&cache->lock);
Liu Bo799ffc32012-07-06 03:31:35 -06006524 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n",
Chris Masond3977122009-01-05 21:25:51 -05006525 (unsigned long long)cache->key.objectid,
6526 (unsigned long long)cache->key.offset,
6527 (unsigned long long)btrfs_block_group_used(&cache->item),
6528 (unsigned long long)cache->pinned,
Liu Bo799ffc32012-07-06 03:31:35 -06006529 (unsigned long long)cache->reserved,
6530 cache->ro ? "[readonly]" : "");
Josef Bacik0f9dd462008-09-23 13:14:11 -04006531 btrfs_dump_free_space(cache, bytes);
6532 spin_unlock(&cache->lock);
6533 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04006534 if (++index < BTRFS_NR_RAID_TYPES)
6535 goto again;
Josef Bacik80eb2342008-10-29 14:49:05 -04006536 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006537}
Zheng Yane8569812008-09-26 10:05:48 -04006538
Yan Zheng11833d62009-09-11 16:11:19 -04006539int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
6540 struct btrfs_root *root,
6541 u64 num_bytes, u64 min_alloc_size,
6542 u64 empty_size, u64 hint_byte,
David Sterbab6919a52013-04-29 13:39:40 +00006543 struct btrfs_key *ins, int is_data)
Chris Masonfec577f2007-02-26 10:40:21 -05006544{
Miao Xie9e622d62012-01-26 15:01:12 -05006545 bool final_tried = false;
David Sterbab6919a52013-04-29 13:39:40 +00006546 u64 flags;
Chris Masonfec577f2007-02-26 10:40:21 -05006547 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04006548
David Sterbab6919a52013-04-29 13:39:40 +00006549 flags = btrfs_get_alloc_profile(root, is_data);
Chris Mason98d20f62008-04-14 09:46:10 -04006550again:
Chris Masondb945352007-10-15 16:15:53 -04006551 WARN_ON(num_bytes < root->sectorsize);
6552 ret = find_free_extent(trans, root, num_bytes, empty_size,
David Sterbab6919a52013-04-29 13:39:40 +00006553 hint_byte, ins, flags);
Chris Mason3b951512008-04-17 11:29:12 -04006554
Miao Xie9e622d62012-01-26 15:01:12 -05006555 if (ret == -ENOSPC) {
6556 if (!final_tried) {
6557 num_bytes = num_bytes >> 1;
Zach Brown24542bf2012-11-16 00:04:43 +00006558 num_bytes = round_down(num_bytes, root->sectorsize);
Miao Xie9e622d62012-01-26 15:01:12 -05006559 num_bytes = max(num_bytes, min_alloc_size);
Miao Xie9e622d62012-01-26 15:01:12 -05006560 if (num_bytes == min_alloc_size)
6561 final_tried = true;
6562 goto again;
6563 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6564 struct btrfs_space_info *sinfo;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006565
David Sterbab6919a52013-04-29 13:39:40 +00006566 sinfo = __find_space_info(root->fs_info, flags);
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006567 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
David Sterbab6919a52013-04-29 13:39:40 +00006568 (unsigned long long)flags,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006569 (unsigned long long)num_bytes);
Jeff Mahoney53804282012-03-01 14:56:28 +01006570 if (sinfo)
6571 dump_space_info(sinfo, num_bytes, 1);
Miao Xie9e622d62012-01-26 15:01:12 -05006572 }
Chris Mason925baed2008-06-25 16:01:30 -04006573 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006574
liubo1abe9b82011-03-24 11:18:59 +00006575 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
6576
Josef Bacik0f9dd462008-09-23 13:14:11 -04006577 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006578}
6579
Chris Masone688b7252011-10-31 20:52:39 -04006580static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6581 u64 start, u64 len, int pin)
Chris Mason65b51a02008-08-01 15:11:20 -04006582{
Josef Bacik0f9dd462008-09-23 13:14:11 -04006583 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05006584 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04006585
Josef Bacik0f9dd462008-09-23 13:14:11 -04006586 cache = btrfs_lookup_block_group(root->fs_info, start);
6587 if (!cache) {
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006588 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6589 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006590 return -ENOSPC;
6591 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05006592
Li Dongyang5378e602011-03-24 10:24:27 +00006593 if (btrfs_test_opt(root, DISCARD))
6594 ret = btrfs_discard_extent(root, start, len, NULL);
Liu Hui1f3c79a2009-01-05 15:57:51 -05006595
Chris Masone688b7252011-10-31 20:52:39 -04006596 if (pin)
6597 pin_down_extent(root, cache, start, len, 1);
6598 else {
6599 btrfs_add_free_space(cache, start, len);
6600 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6601 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04006602 btrfs_put_block_group(cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04006603
liubo1abe9b82011-03-24 11:18:59 +00006604 trace_btrfs_reserved_extent_free(root, start, len);
6605
Chris Masone6dcd2d2008-07-17 12:53:50 -04006606 return ret;
6607}
6608
Chris Masone688b7252011-10-31 20:52:39 -04006609int btrfs_free_reserved_extent(struct btrfs_root *root,
6610 u64 start, u64 len)
6611{
6612 return __btrfs_free_reserved_extent(root, start, len, 0);
6613}
6614
6615int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6616 u64 start, u64 len)
6617{
6618 return __btrfs_free_reserved_extent(root, start, len, 1);
6619}
6620
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006621static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6622 struct btrfs_root *root,
6623 u64 parent, u64 root_objectid,
6624 u64 flags, u64 owner, u64 offset,
6625 struct btrfs_key *ins, int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006626{
6627 int ret;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006628 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006629 struct btrfs_extent_item *extent_item;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006630 struct btrfs_extent_inline_ref *iref;
Chris Masone6dcd2d2008-07-17 12:53:50 -04006631 struct btrfs_path *path;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006632 struct extent_buffer *leaf;
6633 int type;
6634 u32 size;
Chris Masonf2654de2007-06-26 12:20:46 -04006635
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006636 if (parent > 0)
6637 type = BTRFS_SHARED_DATA_REF_KEY;
6638 else
6639 type = BTRFS_EXTENT_DATA_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04006640
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006641 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
Chris Mason7bb86312007-12-11 09:25:06 -05006642
6643 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00006644 if (!path)
6645 return -ENOMEM;
Chris Mason47e4bb92008-02-01 14:51:59 -05006646
Chris Masonb9473432009-03-13 11:00:37 -04006647 path->leave_spinning = 1;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006648 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6649 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006650 if (ret) {
6651 btrfs_free_path(path);
6652 return ret;
6653 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04006654
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006655 leaf = path->nodes[0];
6656 extent_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason47e4bb92008-02-01 14:51:59 -05006657 struct btrfs_extent_item);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006658 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
6659 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6660 btrfs_set_extent_flags(leaf, extent_item,
6661 flags | BTRFS_EXTENT_FLAG_DATA);
Chris Mason47e4bb92008-02-01 14:51:59 -05006662
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006663 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6664 btrfs_set_extent_inline_ref_type(leaf, iref, type);
6665 if (parent > 0) {
6666 struct btrfs_shared_data_ref *ref;
6667 ref = (struct btrfs_shared_data_ref *)(iref + 1);
6668 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6669 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
6670 } else {
6671 struct btrfs_extent_data_ref *ref;
6672 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
6673 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
6674 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
6675 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
6676 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
6677 }
Chris Mason47e4bb92008-02-01 14:51:59 -05006678
6679 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason7bb86312007-12-11 09:25:06 -05006680 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04006681
Liu Boc53d6132012-12-27 09:01:19 +00006682 ret = update_block_group(root, ins->objectid, ins->offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006683 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006684 btrfs_err(fs_info, "update block group failed for %llu %llu",
6685 (unsigned long long)ins->objectid,
6686 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05006687 BUG();
6688 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04006689 return ret;
6690}
6691
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006692static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6693 struct btrfs_root *root,
6694 u64 parent, u64 root_objectid,
6695 u64 flags, struct btrfs_disk_key *key,
6696 int level, struct btrfs_key *ins)
6697{
6698 int ret;
6699 struct btrfs_fs_info *fs_info = root->fs_info;
6700 struct btrfs_extent_item *extent_item;
6701 struct btrfs_tree_block_info *block_info;
6702 struct btrfs_extent_inline_ref *iref;
6703 struct btrfs_path *path;
6704 struct extent_buffer *leaf;
Josef Bacik3173a182013-03-07 14:22:04 -05006705 u32 size = sizeof(*extent_item) + sizeof(*iref);
6706 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6707 SKINNY_METADATA);
6708
6709 if (!skinny_metadata)
6710 size += sizeof(*block_info);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006711
6712 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006713 if (!path)
6714 return -ENOMEM;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006715
6716 path->leave_spinning = 1;
6717 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6718 ins, size);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006719 if (ret) {
6720 btrfs_free_path(path);
6721 return ret;
6722 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006723
6724 leaf = path->nodes[0];
6725 extent_item = btrfs_item_ptr(leaf, path->slots[0],
6726 struct btrfs_extent_item);
6727 btrfs_set_extent_refs(leaf, extent_item, 1);
6728 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
6729 btrfs_set_extent_flags(leaf, extent_item,
6730 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006731
Josef Bacik3173a182013-03-07 14:22:04 -05006732 if (skinny_metadata) {
6733 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
6734 } else {
6735 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
6736 btrfs_set_tree_block_key(leaf, block_info, key);
6737 btrfs_set_tree_block_level(leaf, block_info, level);
6738 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
6739 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006740
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006741 if (parent > 0) {
6742 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
6743 btrfs_set_extent_inline_ref_type(leaf, iref,
6744 BTRFS_SHARED_BLOCK_REF_KEY);
6745 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
6746 } else {
6747 btrfs_set_extent_inline_ref_type(leaf, iref,
6748 BTRFS_TREE_BLOCK_REF_KEY);
6749 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
6750 }
6751
6752 btrfs_mark_buffer_dirty(leaf);
6753 btrfs_free_path(path);
6754
Josef Bacik3173a182013-03-07 14:22:04 -05006755 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006756 if (ret) { /* -ENOENT, logic error */
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00006757 btrfs_err(fs_info, "update block group failed for %llu %llu",
6758 (unsigned long long)ins->objectid,
6759 (unsigned long long)ins->offset);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006760 BUG();
6761 }
6762 return ret;
6763}
6764
6765int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6766 struct btrfs_root *root,
6767 u64 root_objectid, u64 owner,
6768 u64 offset, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04006769{
6770 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04006771
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006772 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
Chris Mason56bec292009-03-13 10:10:06 -04006773
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006774 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
6775 ins->offset, 0,
6776 root_objectid, owner, offset,
6777 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04006778 return ret;
6779}
Chris Masone02119d2008-09-05 16:13:11 -04006780
6781/*
6782 * this is used by the tree logging recovery code. It records that
6783 * an extent has been allocated and makes sure to clear the free
6784 * space cache bits as well
6785 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006786int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6787 struct btrfs_root *root,
6788 u64 root_objectid, u64 owner, u64 offset,
6789 struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04006790{
6791 int ret;
6792 struct btrfs_block_group_cache *block_group;
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006793
6794 /*
6795 * Mixed block groups will exclude before processing the log so we only
6796 * need to do the exlude dance if this fs isn't mixed.
6797 */
6798 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
6799 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
6800 if (ret)
6801 return ret;
6802 }
Chris Masone02119d2008-09-05 16:13:11 -04006803
Chris Masone02119d2008-09-05 16:13:11 -04006804 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik8c2a1a32013-06-06 13:19:32 -04006805 if (!block_group)
6806 return -EINVAL;
Yan Zheng11833d62009-09-11 16:11:19 -04006807
Josef Bacikfb25e912011-07-26 17:00:46 -04006808 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
6809 RESERVE_ALLOC_NO_ACCOUNT);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006810 BUG_ON(ret); /* logic error */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006811 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6812 0, owner, offset, ins, 1);
Josef Bacikb50c6e22013-04-25 15:55:30 -04006813 btrfs_put_block_group(block_group);
Chris Masone02119d2008-09-05 16:13:11 -04006814 return ret;
6815}
6816
Eric Sandeen48a3b632013-04-25 20:41:01 +00006817static struct extent_buffer *
6818btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6819 u64 bytenr, u32 blocksize, int level)
Chris Mason65b51a02008-08-01 15:11:20 -04006820{
6821 struct extent_buffer *buf;
6822
6823 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
6824 if (!buf)
6825 return ERR_PTR(-ENOMEM);
6826 btrfs_set_header_generation(buf, trans->transid);
Chris Mason85d4e462011-07-26 16:11:19 -04006827 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04006828 btrfs_tree_lock(buf);
6829 clean_tree_block(trans, root, buf);
Josef Bacik3083ee22012-03-09 16:01:49 -05006830 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006831
6832 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04006833 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05006834
Chris Masond0c803c2008-09-11 16:17:57 -04006835 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan, Zheng8cef4e12009-11-12 09:33:26 +00006836 /*
6837 * we allow two log transactions at a time, use different
6838 * EXENT bit to differentiate dirty pages.
6839 */
6840 if (root->log_transid % 2 == 0)
6841 set_extent_dirty(&root->dirty_log_pages, buf->start,
6842 buf->start + buf->len - 1, GFP_NOFS);
6843 else
6844 set_extent_new(&root->dirty_log_pages, buf->start,
6845 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04006846 } else {
6847 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
6848 buf->start + buf->len - 1, GFP_NOFS);
6849 }
Chris Mason65b51a02008-08-01 15:11:20 -04006850 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05006851 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04006852 return buf;
6853}
6854
Yan, Zhengf0486c62010-05-16 10:46:25 -04006855static struct btrfs_block_rsv *
6856use_block_rsv(struct btrfs_trans_handle *trans,
6857 struct btrfs_root *root, u32 blocksize)
6858{
6859 struct btrfs_block_rsv *block_rsv;
Josef Bacik68a82272011-01-24 21:43:20 +00006860 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006861 int ret;
Miao Xied88033d2013-05-13 13:55:12 +00006862 bool global_updated = false;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006863
6864 block_rsv = get_block_rsv(trans, root);
6865
Miao Xieb586b322013-05-13 13:55:10 +00006866 if (unlikely(block_rsv->size == 0))
6867 goto try_reserve;
Miao Xied88033d2013-05-13 13:55:12 +00006868again:
Yan, Zhengf0486c62010-05-16 10:46:25 -04006869 ret = block_rsv_use_bytes(block_rsv, blocksize);
6870 if (!ret)
6871 return block_rsv;
6872
Miao Xieb586b322013-05-13 13:55:10 +00006873 if (block_rsv->failfast)
6874 return ERR_PTR(ret);
6875
Miao Xied88033d2013-05-13 13:55:12 +00006876 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
6877 global_updated = true;
6878 update_global_block_rsv(root->fs_info);
6879 goto again;
6880 }
6881
Miao Xieb586b322013-05-13 13:55:10 +00006882 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6883 static DEFINE_RATELIMIT_STATE(_rs,
6884 DEFAULT_RATELIMIT_INTERVAL * 10,
6885 /*DEFAULT_RATELIMIT_BURST*/ 1);
6886 if (__ratelimit(&_rs))
6887 WARN(1, KERN_DEBUG
6888 "btrfs: block rsv returned %d\n", ret);
6889 }
6890try_reserve:
6891 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6892 BTRFS_RESERVE_NO_FLUSH);
6893 if (!ret)
6894 return block_rsv;
6895 /*
6896 * If we couldn't reserve metadata bytes try and use some from
Miao Xie5881cfc2013-05-13 13:55:11 +00006897 * the global reserve if its space type is the same as the global
6898 * reservation.
Miao Xieb586b322013-05-13 13:55:10 +00006899 */
Miao Xie5881cfc2013-05-13 13:55:11 +00006900 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
6901 block_rsv->space_info == global_rsv->space_info) {
Miao Xieb586b322013-05-13 13:55:10 +00006902 ret = block_rsv_use_bytes(global_rsv, blocksize);
6903 if (!ret)
6904 return global_rsv;
6905 }
6906 return ERR_PTR(ret);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006907}
6908
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006909static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
6910 struct btrfs_block_rsv *block_rsv, u32 blocksize)
Yan, Zhengf0486c62010-05-16 10:46:25 -04006911{
6912 block_rsv_add_bytes(block_rsv, blocksize, 0);
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006913 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006914}
6915
Chris Masonfec577f2007-02-26 10:40:21 -05006916/*
Yan, Zhengf0486c62010-05-16 10:46:25 -04006917 * finds a free extent and does all the dirty work required for allocation
6918 * returns the key for the extent through ins, and a tree buffer for
6919 * the first block of the extent through buf.
6920 *
Chris Masonfec577f2007-02-26 10:40:21 -05006921 * returns the tree buffer or NULL.
6922 */
Chris Mason5f39d392007-10-15 16:14:19 -04006923struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006924 struct btrfs_root *root, u32 blocksize,
6925 u64 parent, u64 root_objectid,
6926 struct btrfs_disk_key *key, int level,
Jan Schmidt5581a512012-05-16 17:04:52 +02006927 u64 hint, u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05006928{
Chris Masone2fa7222007-03-12 16:22:34 -04006929 struct btrfs_key ins;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006930 struct btrfs_block_rsv *block_rsv;
Chris Mason5f39d392007-10-15 16:14:19 -04006931 struct extent_buffer *buf;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006932 u64 flags = 0;
6933 int ret;
Josef Bacik3173a182013-03-07 14:22:04 -05006934 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
6935 SKINNY_METADATA);
Yan, Zhengf0486c62010-05-16 10:46:25 -04006936
6937 block_rsv = use_block_rsv(trans, root, blocksize);
6938 if (IS_ERR(block_rsv))
6939 return ERR_CAST(block_rsv);
6940
6941 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
Josef Bacik81c9ad22012-01-18 10:56:06 -05006942 empty_size, hint, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05006943 if (ret) {
Josef Bacik8c2a3ca2012-01-10 10:31:31 -05006944 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04006945 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05006946 }
Chris Mason55c69072008-01-09 15:55:33 -05006947
Chris Mason4008c042009-02-12 14:09:45 -05006948 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6949 blocksize, level);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006950 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006951
6952 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6953 if (parent == 0)
6954 parent = ins.objectid;
6955 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
6956 } else
6957 BUG_ON(parent > 0);
6958
6959 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6960 struct btrfs_delayed_extent_op *extent_op;
Miao Xie78a61842012-11-21 02:21:28 +00006961 extent_op = btrfs_alloc_delayed_extent_op();
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006962 BUG_ON(!extent_op); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006963 if (key)
6964 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6965 else
6966 memset(&extent_op->key, 0, sizeof(extent_op->key));
6967 extent_op->flags_to_set = flags;
Josef Bacik3173a182013-03-07 14:22:04 -05006968 if (skinny_metadata)
6969 extent_op->update_key = 0;
6970 else
6971 extent_op->update_key = 1;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006972 extent_op->update_flags = 1;
6973 extent_op->is_data = 0;
Josef Bacikb1c79e02013-05-09 13:49:30 -04006974 extent_op->level = level;
Yan, Zhengf0486c62010-05-16 10:46:25 -04006975
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006976 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6977 ins.objectid,
Yan, Zhengf0486c62010-05-16 10:46:25 -04006978 ins.offset, parent, root_objectid,
6979 level, BTRFS_ADD_DELAYED_EXTENT,
Jan Schmidt5581a512012-05-16 17:04:52 +02006980 extent_op, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006981 BUG_ON(ret); /* -ENOMEM */
Yan, Zhengf0486c62010-05-16 10:46:25 -04006982 }
Chris Masonfec577f2007-02-26 10:40:21 -05006983 return buf;
6984}
Chris Masona28ec192007-03-06 20:08:01 -05006985
Yan Zheng2c47e6052009-06-27 21:07:35 -04006986struct walk_control {
6987 u64 refs[BTRFS_MAX_LEVEL];
6988 u64 flags[BTRFS_MAX_LEVEL];
6989 struct btrfs_key update_progress;
6990 int stage;
6991 int level;
6992 int shared_level;
6993 int update_ref;
6994 int keep_locks;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04006995 int reada_slot;
6996 int reada_count;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02006997 int for_reloc;
Yan Zheng2c47e6052009-06-27 21:07:35 -04006998};
6999
7000#define DROP_REFERENCE 1
7001#define UPDATE_BACKREF 2
7002
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007003static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7004 struct btrfs_root *root,
7005 struct walk_control *wc,
7006 struct btrfs_path *path)
7007{
7008 u64 bytenr;
7009 u64 generation;
7010 u64 refs;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007011 u64 flags;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007012 u32 nritems;
7013 u32 blocksize;
7014 struct btrfs_key key;
7015 struct extent_buffer *eb;
7016 int ret;
7017 int slot;
7018 int nread = 0;
7019
7020 if (path->slots[wc->level] < wc->reada_slot) {
7021 wc->reada_count = wc->reada_count * 2 / 3;
7022 wc->reada_count = max(wc->reada_count, 2);
7023 } else {
7024 wc->reada_count = wc->reada_count * 3 / 2;
7025 wc->reada_count = min_t(int, wc->reada_count,
7026 BTRFS_NODEPTRS_PER_BLOCK(root));
7027 }
7028
7029 eb = path->nodes[wc->level];
7030 nritems = btrfs_header_nritems(eb);
7031 blocksize = btrfs_level_size(root, wc->level - 1);
7032
7033 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7034 if (nread >= wc->reada_count)
7035 break;
7036
7037 cond_resched();
7038 bytenr = btrfs_node_blockptr(eb, slot);
7039 generation = btrfs_node_ptr_generation(eb, slot);
7040
7041 if (slot == path->slots[wc->level])
7042 goto reada;
7043
7044 if (wc->stage == UPDATE_BACKREF &&
7045 generation <= root->root_key.offset)
7046 continue;
7047
Yan, Zheng94fcca92009-10-09 09:25:16 -04007048 /* We don't lock the tree block, it's OK to be racy here */
Josef Bacik3173a182013-03-07 14:22:04 -05007049 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7050 wc->level - 1, 1, &refs,
7051 &flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007052 /* We don't care about errors in readahead. */
7053 if (ret < 0)
7054 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007055 BUG_ON(refs == 0);
7056
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007057 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007058 if (refs == 1)
7059 goto reada;
7060
Yan, Zheng94fcca92009-10-09 09:25:16 -04007061 if (wc->level == 1 &&
7062 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7063 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007064 if (!wc->update_ref ||
7065 generation <= root->root_key.offset)
7066 continue;
7067 btrfs_node_key_to_cpu(eb, &key, slot);
7068 ret = btrfs_comp_cpu_keys(&key,
7069 &wc->update_progress);
7070 if (ret < 0)
7071 continue;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007072 } else {
7073 if (wc->level == 1 &&
7074 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7075 continue;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007076 }
7077reada:
7078 ret = readahead_tree_block(root, bytenr, blocksize,
7079 generation);
7080 if (ret)
7081 break;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007082 nread++;
7083 }
7084 wc->reada_slot = slot;
7085}
7086
Chris Mason9aca1d52007-03-13 11:09:37 -04007087/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007088 * helper to process tree block while walking down the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007089 *
Yan Zheng2c47e6052009-06-27 21:07:35 -04007090 * when wc->stage == UPDATE_BACKREF, this function updates
7091 * back refs for pointers in the block.
7092 *
7093 * NOTE: return value 1 means we should stop walking down.
Yan Zhengf82d02d2008-10-29 14:49:05 -04007094 */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007095static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7096 struct btrfs_root *root,
7097 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007098 struct walk_control *wc, int lookup_info)
Yan Zheng2c47e6052009-06-27 21:07:35 -04007099{
7100 int level = wc->level;
7101 struct extent_buffer *eb = path->nodes[level];
Yan Zheng2c47e6052009-06-27 21:07:35 -04007102 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7103 int ret;
7104
7105 if (wc->stage == UPDATE_BACKREF &&
7106 btrfs_header_owner(eb) != root->root_key.objectid)
7107 return 1;
7108
7109 /*
7110 * when reference count of tree block is 1, it won't increase
7111 * again. once full backref flag is set, we never clear it.
7112 */
Yan, Zheng94fcca92009-10-09 09:25:16 -04007113 if (lookup_info &&
7114 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7115 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007116 BUG_ON(!path->locks[level]);
7117 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007118 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007119 &wc->refs[level],
7120 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007121 BUG_ON(ret == -ENOMEM);
7122 if (ret)
7123 return ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007124 BUG_ON(wc->refs[level] == 0);
7125 }
7126
Yan Zheng2c47e6052009-06-27 21:07:35 -04007127 if (wc->stage == DROP_REFERENCE) {
7128 if (wc->refs[level] > 1)
7129 return 1;
7130
7131 if (path->locks[level] && !wc->keep_locks) {
Chris Masonbd681512011-07-16 15:23:14 -04007132 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007133 path->locks[level] = 0;
7134 }
7135 return 0;
7136 }
7137
7138 /* wc->stage == UPDATE_BACKREF */
7139 if (!(wc->flags[level] & flag)) {
7140 BUG_ON(!path->locks[level]);
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007141 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007142 BUG_ON(ret); /* -ENOMEM */
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007143 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007144 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007145 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
Josef Bacikb1c79e02013-05-09 13:49:30 -04007146 eb->len, flag,
7147 btrfs_header_level(eb), 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007148 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007149 wc->flags[level] |= flag;
7150 }
7151
7152 /*
7153 * the block is shared by multiple trees, so it's not good to
7154 * keep the tree lock
7155 */
7156 if (path->locks[level] && level > 0) {
Chris Masonbd681512011-07-16 15:23:14 -04007157 btrfs_tree_unlock_rw(eb, path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007158 path->locks[level] = 0;
7159 }
7160 return 0;
7161}
7162
7163/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007164 * helper to process tree block pointer.
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007165 *
7166 * when wc->stage == DROP_REFERENCE, this function checks
7167 * reference count of the block pointed to. if the block
7168 * is shared and we need update back refs for the subtree
7169 * rooted at the block, this function changes wc->stage to
7170 * UPDATE_BACKREF. if the block is shared and there is no
7171 * need to update back, this function drops the reference
7172 * to the block.
7173 *
7174 * NOTE: return value 1 means we should stop walking down.
7175 */
7176static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7177 struct btrfs_root *root,
7178 struct btrfs_path *path,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007179 struct walk_control *wc, int *lookup_info)
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007180{
7181 u64 bytenr;
7182 u64 generation;
7183 u64 parent;
7184 u32 blocksize;
7185 struct btrfs_key key;
7186 struct extent_buffer *next;
7187 int level = wc->level;
7188 int reada = 0;
7189 int ret = 0;
7190
7191 generation = btrfs_node_ptr_generation(path->nodes[level],
7192 path->slots[level]);
7193 /*
7194 * if the lower level block was created before the snapshot
7195 * was created, we know there is no need to update back refs
7196 * for the subtree
7197 */
7198 if (wc->stage == UPDATE_BACKREF &&
Yan, Zheng94fcca92009-10-09 09:25:16 -04007199 generation <= root->root_key.offset) {
7200 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007201 return 1;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007202 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007203
7204 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7205 blocksize = btrfs_level_size(root, level - 1);
7206
7207 next = btrfs_find_tree_block(root, bytenr, blocksize);
7208 if (!next) {
7209 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
Miao Xie90d2c51d2010-03-25 12:37:12 +00007210 if (!next)
7211 return -ENOMEM;
Josef Bacikb2aaaa32013-07-05 17:05:38 -04007212 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7213 level - 1);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007214 reada = 1;
7215 }
7216 btrfs_tree_lock(next);
7217 btrfs_set_lock_blocking(next);
7218
Josef Bacik3173a182013-03-07 14:22:04 -05007219 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
Yan, Zheng94fcca92009-10-09 09:25:16 -04007220 &wc->refs[level - 1],
7221 &wc->flags[level - 1]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007222 if (ret < 0) {
7223 btrfs_tree_unlock(next);
7224 return ret;
7225 }
7226
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00007227 if (unlikely(wc->refs[level - 1] == 0)) {
7228 btrfs_err(root->fs_info, "Missing references.");
7229 BUG();
7230 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007231 *lookup_info = 0;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007232
Yan, Zheng94fcca92009-10-09 09:25:16 -04007233 if (wc->stage == DROP_REFERENCE) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007234 if (wc->refs[level - 1] > 1) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007235 if (level == 1 &&
7236 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7237 goto skip;
7238
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007239 if (!wc->update_ref ||
7240 generation <= root->root_key.offset)
7241 goto skip;
7242
7243 btrfs_node_key_to_cpu(path->nodes[level], &key,
7244 path->slots[level]);
7245 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7246 if (ret < 0)
7247 goto skip;
7248
7249 wc->stage = UPDATE_BACKREF;
7250 wc->shared_level = level - 1;
7251 }
Yan, Zheng94fcca92009-10-09 09:25:16 -04007252 } else {
7253 if (level == 1 &&
7254 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7255 goto skip;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007256 }
7257
Chris Masonb9fab912012-05-06 07:23:47 -04007258 if (!btrfs_buffer_uptodate(next, generation, 0)) {
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007259 btrfs_tree_unlock(next);
7260 free_extent_buffer(next);
7261 next = NULL;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007262 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007263 }
7264
7265 if (!next) {
7266 if (reada && level == 1)
7267 reada_walk_down(trans, root, wc, path);
7268 next = read_tree_block(root, bytenr, blocksize, generation);
Josef Bacik416bc652013-04-23 14:17:42 -04007269 if (!next || !extent_buffer_uptodate(next)) {
7270 free_extent_buffer(next);
Tsutomu Itoh97d9a8a2011-03-24 06:33:21 +00007271 return -EIO;
Josef Bacik416bc652013-04-23 14:17:42 -04007272 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007273 btrfs_tree_lock(next);
7274 btrfs_set_lock_blocking(next);
7275 }
7276
7277 level--;
7278 BUG_ON(level != btrfs_header_level(next));
7279 path->nodes[level] = next;
7280 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007281 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007282 wc->level = level;
7283 if (wc->level == 1)
7284 wc->reada_slot = 0;
7285 return 0;
7286skip:
7287 wc->refs[level - 1] = 0;
7288 wc->flags[level - 1] = 0;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007289 if (wc->stage == DROP_REFERENCE) {
7290 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7291 parent = path->nodes[level]->start;
7292 } else {
7293 BUG_ON(root->root_key.objectid !=
7294 btrfs_header_owner(path->nodes[level]));
7295 parent = 0;
7296 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007297
Yan, Zheng94fcca92009-10-09 09:25:16 -04007298 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007299 root->root_key.objectid, level - 1, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007300 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007301 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007302 btrfs_tree_unlock(next);
7303 free_extent_buffer(next);
Yan, Zheng94fcca92009-10-09 09:25:16 -04007304 *lookup_info = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007305 return 1;
7306}
7307
7308/*
Liu Bo2c016dc2012-12-26 15:32:17 +08007309 * helper to process tree block while walking up the tree.
Yan Zheng2c47e6052009-06-27 21:07:35 -04007310 *
7311 * when wc->stage == DROP_REFERENCE, this function drops
7312 * reference count on the block.
7313 *
7314 * when wc->stage == UPDATE_BACKREF, this function changes
7315 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7316 * to UPDATE_BACKREF previously while processing the block.
7317 *
7318 * NOTE: return value 1 means we should stop walking up.
7319 */
7320static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7321 struct btrfs_root *root,
7322 struct btrfs_path *path,
7323 struct walk_control *wc)
7324{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007325 int ret;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007326 int level = wc->level;
7327 struct extent_buffer *eb = path->nodes[level];
7328 u64 parent = 0;
7329
7330 if (wc->stage == UPDATE_BACKREF) {
7331 BUG_ON(wc->shared_level < level);
7332 if (level < wc->shared_level)
7333 goto out;
7334
Yan Zheng2c47e6052009-06-27 21:07:35 -04007335 ret = find_next_key(path, level + 1, &wc->update_progress);
7336 if (ret > 0)
7337 wc->update_ref = 0;
7338
7339 wc->stage = DROP_REFERENCE;
7340 wc->shared_level = -1;
7341 path->slots[level] = 0;
7342
7343 /*
7344 * check reference count again if the block isn't locked.
7345 * we should start walking down the tree again if reference
7346 * count is one.
7347 */
7348 if (!path->locks[level]) {
7349 BUG_ON(level == 0);
7350 btrfs_tree_lock(eb);
7351 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007352 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007353
7354 ret = btrfs_lookup_extent_info(trans, root,
Josef Bacik3173a182013-03-07 14:22:04 -05007355 eb->start, level, 1,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007356 &wc->refs[level],
7357 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007358 if (ret < 0) {
7359 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007360 path->locks[level] = 0;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007361 return ret;
7362 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007363 BUG_ON(wc->refs[level] == 0);
7364 if (wc->refs[level] == 1) {
Chris Masonbd681512011-07-16 15:23:14 -04007365 btrfs_tree_unlock_rw(eb, path->locks[level]);
Liu Bo3268a242012-12-28 09:33:19 +00007366 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007367 return 1;
7368 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007369 }
7370 }
7371
7372 /* wc->stage == DROP_REFERENCE */
7373 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7374
7375 if (wc->refs[level] == 1) {
7376 if (level == 0) {
7377 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007378 ret = btrfs_dec_ref(trans, root, eb, 1,
7379 wc->for_reloc);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007380 else
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007381 ret = btrfs_dec_ref(trans, root, eb, 0,
7382 wc->for_reloc);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007383 BUG_ON(ret); /* -ENOMEM */
Yan Zheng2c47e6052009-06-27 21:07:35 -04007384 }
7385 /* make block locked assertion in clean_tree_block happy */
7386 if (!path->locks[level] &&
7387 btrfs_header_generation(eb) == trans->transid) {
7388 btrfs_tree_lock(eb);
7389 btrfs_set_lock_blocking(eb);
Chris Masonbd681512011-07-16 15:23:14 -04007390 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007391 }
7392 clean_tree_block(trans, root, eb);
7393 }
7394
7395 if (eb == root->node) {
7396 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7397 parent = eb->start;
7398 else
7399 BUG_ON(root->root_key.objectid !=
7400 btrfs_header_owner(eb));
7401 } else {
7402 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7403 parent = path->nodes[level + 1]->start;
7404 else
7405 BUG_ON(root->root_key.objectid !=
7406 btrfs_header_owner(path->nodes[level + 1]));
7407 }
7408
Jan Schmidt5581a512012-05-16 17:04:52 +02007409 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007410out:
7411 wc->refs[level] = 0;
7412 wc->flags[level] = 0;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007413 return 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007414}
7415
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007416static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
7417 struct btrfs_root *root,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007418 struct btrfs_path *path,
7419 struct walk_control *wc)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007420{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007421 int level = wc->level;
Yan, Zheng94fcca92009-10-09 09:25:16 -04007422 int lookup_info = 1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007423 int ret;
7424
Yan Zheng2c47e6052009-06-27 21:07:35 -04007425 while (level >= 0) {
Yan, Zheng94fcca92009-10-09 09:25:16 -04007426 ret = walk_down_proc(trans, root, path, wc, lookup_info);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007427 if (ret > 0)
Yan Zhengf82d02d2008-10-29 14:49:05 -04007428 break;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007429
Yan Zheng2c47e6052009-06-27 21:07:35 -04007430 if (level == 0)
7431 break;
7432
Yan, Zheng7a7965f2010-02-01 02:41:17 +00007433 if (path->slots[level] >=
7434 btrfs_header_nritems(path->nodes[level]))
7435 break;
7436
Yan, Zheng94fcca92009-10-09 09:25:16 -04007437 ret = do_walk_down(trans, root, path, wc, &lookup_info);
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007438 if (ret > 0) {
7439 path->slots[level]++;
7440 continue;
Miao Xie90d2c51d2010-03-25 12:37:12 +00007441 } else if (ret < 0)
7442 return ret;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007443 level = wc->level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007444 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007445 return 0;
7446}
7447
Chris Masond3977122009-01-05 21:25:51 -05007448static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05007449 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04007450 struct btrfs_path *path,
Yan Zheng2c47e6052009-06-27 21:07:35 -04007451 struct walk_control *wc, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05007452{
Yan Zheng2c47e6052009-06-27 21:07:35 -04007453 int level = wc->level;
Chris Mason20524f02007-03-10 06:35:47 -05007454 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04007455
Yan Zheng2c47e6052009-06-27 21:07:35 -04007456 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
7457 while (level < max_level && path->nodes[level]) {
7458 wc->level = level;
7459 if (path->slots[level] + 1 <
7460 btrfs_header_nritems(path->nodes[level])) {
7461 path->slots[level]++;
Chris Mason20524f02007-03-10 06:35:47 -05007462 return 0;
7463 } else {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007464 ret = walk_up_proc(trans, root, path, wc);
7465 if (ret > 0)
7466 return 0;
Chris Masonbd56b302009-02-04 09:27:02 -05007467
Yan Zheng2c47e6052009-06-27 21:07:35 -04007468 if (path->locks[level]) {
Chris Masonbd681512011-07-16 15:23:14 -04007469 btrfs_tree_unlock_rw(path->nodes[level],
7470 path->locks[level]);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007471 path->locks[level] = 0;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007472 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007473 free_extent_buffer(path->nodes[level]);
7474 path->nodes[level] = NULL;
7475 level++;
Chris Mason20524f02007-03-10 06:35:47 -05007476 }
7477 }
7478 return 1;
7479}
7480
Chris Mason9aca1d52007-03-13 11:09:37 -04007481/*
Yan Zheng2c47e6052009-06-27 21:07:35 -04007482 * drop a subvolume tree.
7483 *
7484 * this function traverses the tree freeing any blocks that only
7485 * referenced by the tree.
7486 *
7487 * when a shared tree block is found. this function decreases its
7488 * reference count by one. if update_ref is true, this function
7489 * also make sure backrefs for the shared block and all lower level
7490 * blocks are properly updated.
David Sterba9d1a2a32013-03-12 15:13:28 +00007491 *
7492 * If called with for_reloc == 0, may exit early with -EAGAIN
Chris Mason9aca1d52007-03-13 11:09:37 -04007493 */
Jeff Mahoney2c536792011-10-03 23:22:41 -04007494int btrfs_drop_snapshot(struct btrfs_root *root,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007495 struct btrfs_block_rsv *block_rsv, int update_ref,
7496 int for_reloc)
Chris Mason20524f02007-03-10 06:35:47 -05007497{
Chris Mason5caf2a02007-04-02 11:20:42 -04007498 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007499 struct btrfs_trans_handle *trans;
7500 struct btrfs_root *tree_root = root->fs_info->tree_root;
Chris Mason9f3a7422007-08-07 15:52:19 -04007501 struct btrfs_root_item *root_item = &root->root_item;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007502 struct walk_control *wc;
7503 struct btrfs_key key;
7504 int err = 0;
7505 int ret;
7506 int level;
Josef Bacikd29a9f62013-07-17 19:30:20 -04007507 bool root_dropped = false;
Chris Mason20524f02007-03-10 06:35:47 -05007508
Chris Mason5caf2a02007-04-02 11:20:42 -04007509 path = btrfs_alloc_path();
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007510 if (!path) {
7511 err = -ENOMEM;
7512 goto out;
7513 }
Chris Mason20524f02007-03-10 06:35:47 -05007514
Yan Zheng2c47e6052009-06-27 21:07:35 -04007515 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Mark Fasheh38a1a912011-07-13 10:59:59 -07007516 if (!wc) {
7517 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007518 err = -ENOMEM;
7519 goto out;
Mark Fasheh38a1a912011-07-13 10:59:59 -07007520 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007521
Yan, Zhenga22285a2010-05-16 10:48:46 -04007522 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007523 if (IS_ERR(trans)) {
7524 err = PTR_ERR(trans);
7525 goto out_free;
7526 }
Tsutomu Itoh98d5dc12011-01-20 06:19:37 +00007527
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007528 if (block_rsv)
7529 trans->block_rsv = block_rsv;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007530
Chris Mason9f3a7422007-08-07 15:52:19 -04007531 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007532 level = btrfs_header_level(root->node);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007533 path->nodes[level] = btrfs_lock_root_node(root);
7534 btrfs_set_lock_blocking(path->nodes[level]);
Chris Mason9f3a7422007-08-07 15:52:19 -04007535 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007536 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007537 memset(&wc->update_progress, 0,
7538 sizeof(wc->update_progress));
Chris Mason9f3a7422007-08-07 15:52:19 -04007539 } else {
Chris Mason9f3a7422007-08-07 15:52:19 -04007540 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007541 memcpy(&wc->update_progress, &key,
7542 sizeof(wc->update_progress));
7543
Chris Mason6702ed42007-08-07 16:15:09 -04007544 level = root_item->drop_level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007545 BUG_ON(level == 0);
Chris Mason6702ed42007-08-07 16:15:09 -04007546 path->lowest_level = level;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007547 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
7548 path->lowest_level = 0;
7549 if (ret < 0) {
7550 err = ret;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007551 goto out_end_trans;
Chris Mason9f3a7422007-08-07 15:52:19 -04007552 }
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007553 WARN_ON(ret > 0);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007554
Chris Mason7d9eb122008-07-08 14:19:17 -04007555 /*
7556 * unlock our path, this is safe because only this
7557 * function is allowed to delete this snapshot
7558 */
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007559 btrfs_unlock_up_safe(path, 0);
Chris Mason9aca1d52007-03-13 11:09:37 -04007560
Yan Zheng2c47e6052009-06-27 21:07:35 -04007561 level = btrfs_header_level(root->node);
7562 while (1) {
7563 btrfs_tree_lock(path->nodes[level]);
7564 btrfs_set_lock_blocking(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007565 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007566
7567 ret = btrfs_lookup_extent_info(trans, root,
7568 path->nodes[level]->start,
Josef Bacik3173a182013-03-07 14:22:04 -05007569 level, 1, &wc->refs[level],
Yan Zheng2c47e6052009-06-27 21:07:35 -04007570 &wc->flags[level]);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007571 if (ret < 0) {
7572 err = ret;
7573 goto out_end_trans;
7574 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007575 BUG_ON(wc->refs[level] == 0);
7576
7577 if (level == root_item->drop_level)
7578 break;
7579
7580 btrfs_tree_unlock(path->nodes[level]);
Josef Bacikfec386a2013-07-15 12:41:42 -04007581 path->locks[level] = 0;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007582 WARN_ON(wc->refs[level] != 1);
7583 level--;
7584 }
7585 }
7586
7587 wc->level = level;
7588 wc->shared_level = -1;
7589 wc->stage = DROP_REFERENCE;
7590 wc->update_ref = update_ref;
7591 wc->keep_locks = 0;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007592 wc->for_reloc = for_reloc;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007593 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zheng2c47e6052009-06-27 21:07:35 -04007594
7595 while (1) {
David Sterba9d1a2a32013-03-12 15:13:28 +00007596
Yan Zheng2c47e6052009-06-27 21:07:35 -04007597 ret = walk_down_tree(trans, root, path, wc);
7598 if (ret < 0) {
7599 err = ret;
Chris Masone7a84562008-06-25 16:01:31 -04007600 break;
7601 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007602
7603 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
7604 if (ret < 0) {
7605 err = ret;
7606 break;
7607 }
7608
7609 if (ret > 0) {
7610 BUG_ON(wc->stage != DROP_REFERENCE);
7611 break;
7612 }
7613
7614 if (wc->stage == DROP_REFERENCE) {
7615 level = wc->level;
7616 btrfs_node_key(path->nodes[level],
7617 &root_item->drop_progress,
7618 path->slots[level]);
7619 root_item->drop_level = level;
7620 }
7621
7622 BUG_ON(wc->level == 0);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007623 if (btrfs_should_end_transaction(trans, tree_root) ||
7624 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007625 ret = btrfs_update_root(trans, tree_root,
7626 &root->root_key,
7627 root_item);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007628 if (ret) {
7629 btrfs_abort_transaction(trans, tree_root, ret);
7630 err = ret;
7631 goto out_end_trans;
7632 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007633
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007634 btrfs_end_transaction_throttle(trans, tree_root);
Josef Bacik3c8f2422013-07-15 11:57:06 -04007635 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
7636 pr_debug("btrfs: drop snapshot early exit\n");
7637 err = -EAGAIN;
7638 goto out_free;
7639 }
7640
Yan, Zhenga22285a2010-05-16 10:48:46 -04007641 trans = btrfs_start_transaction(tree_root, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007642 if (IS_ERR(trans)) {
7643 err = PTR_ERR(trans);
7644 goto out_free;
7645 }
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007646 if (block_rsv)
7647 trans->block_rsv = block_rsv;
Chris Masonc3e69d52009-03-13 10:17:05 -04007648 }
Chris Mason20524f02007-03-10 06:35:47 -05007649 }
David Sterbab3b4aa72011-04-21 01:20:15 +02007650 btrfs_release_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007651 if (err)
7652 goto out_end_trans;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007653
7654 ret = btrfs_del_root(trans, tree_root, &root->root_key);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007655 if (ret) {
7656 btrfs_abort_transaction(trans, tree_root, ret);
7657 goto out_end_trans;
7658 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007659
Yan, Zheng76dda932009-09-21 16:00:26 -04007660 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007661 ret = btrfs_find_root(tree_root, &root->root_key, path,
7662 NULL, NULL);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007663 if (ret < 0) {
7664 btrfs_abort_transaction(trans, tree_root, ret);
7665 err = ret;
7666 goto out_end_trans;
7667 } else if (ret > 0) {
Josef Bacik84cd9482010-12-08 12:24:01 -05007668 /* if we fail to delete the orphan item this time
7669 * around, it'll get picked up the next time.
7670 *
7671 * The most common failure here is just -ENOENT.
7672 */
7673 btrfs_del_orphan_item(trans, tree_root,
7674 root->root_key.objectid);
Yan, Zheng76dda932009-09-21 16:00:26 -04007675 }
7676 }
7677
7678 if (root->in_radix) {
Miao Xiecb517ea2013-05-15 07:48:19 +00007679 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007680 } else {
7681 free_extent_buffer(root->node);
7682 free_extent_buffer(root->commit_root);
Miao Xieb0feb9d2013-05-15 07:48:20 +00007683 btrfs_put_fs_root(root);
Yan, Zheng76dda932009-09-21 16:00:26 -04007684 }
Josef Bacikd29a9f62013-07-17 19:30:20 -04007685 root_dropped = true;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007686out_end_trans:
Yan, Zheng3fd0a552010-05-16 10:49:59 -04007687 btrfs_end_transaction_throttle(trans, tree_root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007688out_free:
Yan Zheng2c47e6052009-06-27 21:07:35 -04007689 kfree(wc);
Chris Mason5caf2a02007-04-02 11:20:42 -04007690 btrfs_free_path(path);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007691out:
Josef Bacikd29a9f62013-07-17 19:30:20 -04007692 /*
7693 * So if we need to stop dropping the snapshot for whatever reason we
7694 * need to make sure to add it back to the dead root list so that we
7695 * keep trying to do the work later. This also cleans up roots if we
7696 * don't have it in the radix (like when we recover after a power fail
7697 * or unmount) so we don't leak memory.
7698 */
7699 if (root_dropped == false)
7700 btrfs_add_dead_root(root);
Tsutomu Itohcb1b69f2011-08-09 07:11:13 +00007701 if (err)
7702 btrfs_std_error(root->fs_info, err);
Jeff Mahoney2c536792011-10-03 23:22:41 -04007703 return err;
Chris Mason20524f02007-03-10 06:35:47 -05007704}
Chris Mason9078a3e2007-04-26 16:46:15 -04007705
Yan Zheng2c47e6052009-06-27 21:07:35 -04007706/*
7707 * drop subtree rooted at tree block 'node'.
7708 *
7709 * NOTE: this function will unlock and release tree block 'node'
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007710 * only used by relocation code
Yan Zheng2c47e6052009-06-27 21:07:35 -04007711 */
Yan Zhengf82d02d2008-10-29 14:49:05 -04007712int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7713 struct btrfs_root *root,
7714 struct extent_buffer *node,
7715 struct extent_buffer *parent)
7716{
7717 struct btrfs_path *path;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007718 struct walk_control *wc;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007719 int level;
7720 int parent_level;
7721 int ret = 0;
7722 int wret;
7723
Yan Zheng2c47e6052009-06-27 21:07:35 -04007724 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7725
Yan Zhengf82d02d2008-10-29 14:49:05 -04007726 path = btrfs_alloc_path();
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007727 if (!path)
7728 return -ENOMEM;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007729
Yan Zheng2c47e6052009-06-27 21:07:35 -04007730 wc = kzalloc(sizeof(*wc), GFP_NOFS);
Tsutomu Itohdb5b4932011-03-23 08:14:16 +00007731 if (!wc) {
7732 btrfs_free_path(path);
7733 return -ENOMEM;
7734 }
Yan Zheng2c47e6052009-06-27 21:07:35 -04007735
Chris Masonb9447ef2009-03-09 11:45:38 -04007736 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007737 parent_level = btrfs_header_level(parent);
7738 extent_buffer_get(parent);
7739 path->nodes[parent_level] = parent;
7740 path->slots[parent_level] = btrfs_header_nritems(parent);
7741
Chris Masonb9447ef2009-03-09 11:45:38 -04007742 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007743 level = btrfs_header_level(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007744 path->nodes[level] = node;
7745 path->slots[level] = 0;
Chris Masonbd681512011-07-16 15:23:14 -04007746 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007747
7748 wc->refs[parent_level] = 1;
7749 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7750 wc->level = level;
7751 wc->shared_level = -1;
7752 wc->stage = DROP_REFERENCE;
7753 wc->update_ref = 0;
7754 wc->keep_locks = 1;
Arne Jansen66d7e7f2011-09-12 15:26:38 +02007755 wc->for_reloc = 1;
Yan, Zheng1c4850e2009-09-21 15:55:59 -04007756 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007757
7758 while (1) {
Yan Zheng2c47e6052009-06-27 21:07:35 -04007759 wret = walk_down_tree(trans, root, path, wc);
7760 if (wret < 0) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04007761 ret = wret;
Yan Zhengf82d02d2008-10-29 14:49:05 -04007762 break;
Yan Zheng2c47e6052009-06-27 21:07:35 -04007763 }
Yan Zhengf82d02d2008-10-29 14:49:05 -04007764
Yan Zheng2c47e6052009-06-27 21:07:35 -04007765 wret = walk_up_tree(trans, root, path, wc, parent_level);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007766 if (wret < 0)
7767 ret = wret;
7768 if (wret != 0)
7769 break;
7770 }
7771
Yan Zheng2c47e6052009-06-27 21:07:35 -04007772 kfree(wc);
Yan Zhengf82d02d2008-10-29 14:49:05 -04007773 btrfs_free_path(path);
7774 return ret;
7775}
7776
Chris Masonec44a352008-04-28 15:29:52 -04007777static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7778{
7779 u64 num_devices;
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007780 u64 stripped;
Chris Masonec44a352008-04-28 15:29:52 -04007781
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007782 /*
7783 * if restripe for this chunk_type is on pick target profile and
7784 * return, otherwise do the usual balance
7785 */
7786 stripped = get_restripe_target(root->fs_info, flags);
7787 if (stripped)
7788 return extended_to_chunk(stripped);
Ilya Dryomove4d8ec02012-01-16 22:04:48 +02007789
Chris Masoncd02dca2010-12-13 14:56:23 -05007790 /*
7791 * we add in the count of missing devices because we want
7792 * to make sure that any RAID levels on a degraded FS
7793 * continue to be honored.
7794 */
7795 num_devices = root->fs_info->fs_devices->rw_devices +
7796 root->fs_info->fs_devices->missing_devices;
7797
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007798 stripped = BTRFS_BLOCK_GROUP_RAID0 |
David Woodhouse53b381b2013-01-29 18:40:14 -05007799 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
Ilya Dryomovfc67c452012-03-27 17:09:17 +03007800 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7801
Chris Masonec44a352008-04-28 15:29:52 -04007802 if (num_devices == 1) {
7803 stripped |= BTRFS_BLOCK_GROUP_DUP;
7804 stripped = flags & ~stripped;
7805
7806 /* turn raid0 into single device chunks */
7807 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7808 return stripped;
7809
7810 /* turn mirroring into duplication */
7811 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7812 BTRFS_BLOCK_GROUP_RAID10))
7813 return stripped | BTRFS_BLOCK_GROUP_DUP;
Chris Masonec44a352008-04-28 15:29:52 -04007814 } else {
7815 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04007816 if (flags & stripped)
7817 return flags;
7818
7819 stripped |= BTRFS_BLOCK_GROUP_DUP;
7820 stripped = flags & ~stripped;
7821
7822 /* switch duplicated blocks with raid1 */
7823 if (flags & BTRFS_BLOCK_GROUP_DUP)
7824 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7825
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007826 /* this is drive concat, leave it alone */
Chris Masonec44a352008-04-28 15:29:52 -04007827 }
Ilya Dryomove3176ca2012-03-27 17:09:16 +03007828
Chris Masonec44a352008-04-28 15:29:52 -04007829 return flags;
7830}
7831
Miao Xie199c36e2011-07-15 10:34:36 +00007832static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
Chris Mason0ef3e662008-05-24 14:04:53 -04007833{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007834 struct btrfs_space_info *sinfo = cache->space_info;
7835 u64 num_bytes;
Miao Xie199c36e2011-07-15 10:34:36 +00007836 u64 min_allocable_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007837 int ret = -ENOSPC;
Chris Mason0ef3e662008-05-24 14:04:53 -04007838
Chris Masonc286ac42008-07-22 23:06:41 -04007839
Miao Xie199c36e2011-07-15 10:34:36 +00007840 /*
7841 * We need some metadata space and system metadata space for
7842 * allocating chunks in some corner cases until we force to set
7843 * it to be readonly.
7844 */
7845 if ((sinfo->flags &
7846 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
7847 !force)
7848 min_allocable_bytes = 1 * 1024 * 1024;
7849 else
7850 min_allocable_bytes = 0;
7851
Yan, Zhengf0486c62010-05-16 10:46:25 -04007852 spin_lock(&sinfo->lock);
7853 spin_lock(&cache->lock);
WuBo61cfea92011-07-26 03:30:11 +00007854
7855 if (cache->ro) {
7856 ret = 0;
7857 goto out;
7858 }
7859
Yan, Zhengf0486c62010-05-16 10:46:25 -04007860 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7861 cache->bytes_super - btrfs_block_group_used(&cache->item);
Chris Mason7d9eb122008-07-08 14:19:17 -04007862
Yan, Zhengf0486c62010-05-16 10:46:25 -04007863 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
Josef Bacik37be25b2011-08-05 10:25:38 -04007864 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
7865 min_allocable_bytes <= sinfo->total_bytes) {
Yan, Zhengf0486c62010-05-16 10:46:25 -04007866 sinfo->bytes_readonly += num_bytes;
Yan, Zhengf0486c62010-05-16 10:46:25 -04007867 cache->ro = 1;
7868 ret = 0;
7869 }
WuBo61cfea92011-07-26 03:30:11 +00007870out:
Yan, Zhengf0486c62010-05-16 10:46:25 -04007871 spin_unlock(&cache->lock);
7872 spin_unlock(&sinfo->lock);
7873 return ret;
Chris Mason0ef3e662008-05-24 14:04:53 -04007874}
7875
Yan, Zhengf0486c62010-05-16 10:46:25 -04007876int btrfs_set_block_group_ro(struct btrfs_root *root,
7877 struct btrfs_block_group_cache *cache)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007878
7879{
Yan, Zhengf0486c62010-05-16 10:46:25 -04007880 struct btrfs_trans_handle *trans;
7881 u64 alloc_flags;
7882 int ret;
7883
7884 BUG_ON(cache->ro);
7885
Josef Bacik7a7eaa42011-04-13 12:54:33 -04007886 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007887 if (IS_ERR(trans))
7888 return PTR_ERR(trans);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007889
7890 alloc_flags = update_block_group_flags(root, cache->flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007891 if (alloc_flags != cache->flags) {
Josef Bacik698d0082012-09-12 14:08:47 -04007892 ret = do_chunk_alloc(trans, root, alloc_flags,
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007893 CHUNK_ALLOC_FORCE);
7894 if (ret < 0)
7895 goto out;
7896 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04007897
Miao Xie199c36e2011-07-15 10:34:36 +00007898 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007899 if (!ret)
7900 goto out;
7901 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
Josef Bacik698d0082012-09-12 14:08:47 -04007902 ret = do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007903 CHUNK_ALLOC_FORCE);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007904 if (ret < 0)
7905 goto out;
Miao Xie199c36e2011-07-15 10:34:36 +00007906 ret = set_block_group_ro(cache, 0);
Yan, Zhengf0486c62010-05-16 10:46:25 -04007907out:
7908 btrfs_end_transaction(trans, root);
7909 return ret;
7910}
7911
Chris Masonc87f08c2011-02-16 13:57:04 -05007912int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7913 struct btrfs_root *root, u64 type)
7914{
7915 u64 alloc_flags = get_alloc_profile(root, type);
Josef Bacik698d0082012-09-12 14:08:47 -04007916 return do_chunk_alloc(trans, root, alloc_flags,
Chris Mason0e4f8f82011-04-15 16:05:44 -04007917 CHUNK_ALLOC_FORCE);
Chris Masonc87f08c2011-02-16 13:57:04 -05007918}
7919
Miao Xie6d07bce2011-01-05 10:07:31 +00007920/*
7921 * helper to account the unused space of all the readonly block group in the
7922 * list. takes mirrors into account.
7923 */
7924static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
7925{
7926 struct btrfs_block_group_cache *block_group;
7927 u64 free_bytes = 0;
7928 int factor;
7929
7930 list_for_each_entry(block_group, groups_list, list) {
7931 spin_lock(&block_group->lock);
7932
7933 if (!block_group->ro) {
7934 spin_unlock(&block_group->lock);
7935 continue;
7936 }
7937
7938 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
7939 BTRFS_BLOCK_GROUP_RAID10 |
7940 BTRFS_BLOCK_GROUP_DUP))
7941 factor = 2;
7942 else
7943 factor = 1;
7944
7945 free_bytes += (block_group->key.offset -
7946 btrfs_block_group_used(&block_group->item)) *
7947 factor;
7948
7949 spin_unlock(&block_group->lock);
7950 }
7951
7952 return free_bytes;
7953}
7954
7955/*
7956 * helper to account the unused space of all the readonly block group in the
7957 * space_info. takes mirrors into account.
7958 */
7959u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7960{
7961 int i;
7962 u64 free_bytes = 0;
7963
7964 spin_lock(&sinfo->lock);
7965
7966 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
7967 if (!list_empty(&sinfo->block_groups[i]))
7968 free_bytes += __btrfs_get_ro_block_group_free_space(
7969 &sinfo->block_groups[i]);
7970
7971 spin_unlock(&sinfo->lock);
7972
7973 return free_bytes;
7974}
7975
Jeff Mahoney143bede2012-03-01 14:56:26 +01007976void btrfs_set_block_group_rw(struct btrfs_root *root,
Yan, Zhengf0486c62010-05-16 10:46:25 -04007977 struct btrfs_block_group_cache *cache)
7978{
7979 struct btrfs_space_info *sinfo = cache->space_info;
7980 u64 num_bytes;
7981
7982 BUG_ON(!cache->ro);
7983
7984 spin_lock(&sinfo->lock);
7985 spin_lock(&cache->lock);
7986 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7987 cache->bytes_super - btrfs_block_group_used(&cache->item);
7988 sinfo->bytes_readonly -= num_bytes;
7989 cache->ro = 0;
7990 spin_unlock(&cache->lock);
7991 spin_unlock(&sinfo->lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04007992}
7993
Josef Bacikba1bf482009-09-11 16:11:19 -04007994/*
7995 * checks to see if its even possible to relocate this block group.
7996 *
7997 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7998 * ok to go ahead and try.
7999 */
8000int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
Zheng Yan1a40e232008-09-26 10:09:34 -04008001{
Zheng Yan1a40e232008-09-26 10:09:34 -04008002 struct btrfs_block_group_cache *block_group;
Josef Bacikba1bf482009-09-11 16:11:19 -04008003 struct btrfs_space_info *space_info;
8004 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8005 struct btrfs_device *device;
Josef Bacik6df9a952013-06-27 13:22:46 -04008006 struct btrfs_trans_handle *trans;
liubocdcb7252011-08-03 10:15:25 +00008007 u64 min_free;
Josef Bacik6719db62011-08-20 08:29:51 -04008008 u64 dev_min = 1;
8009 u64 dev_nr = 0;
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008010 u64 target;
liubocdcb7252011-08-03 10:15:25 +00008011 int index;
Josef Bacikba1bf482009-09-11 16:11:19 -04008012 int full = 0;
8013 int ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05008014
Josef Bacikba1bf482009-09-11 16:11:19 -04008015 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04008016
Josef Bacikba1bf482009-09-11 16:11:19 -04008017 /* odd, couldn't find the block group, leave it alone */
8018 if (!block_group)
8019 return -1;
Chris Masonedbd8d42007-12-21 16:27:24 -05008020
liubocdcb7252011-08-03 10:15:25 +00008021 min_free = btrfs_block_group_used(&block_group->item);
8022
Josef Bacikba1bf482009-09-11 16:11:19 -04008023 /* no bytes used, we're good */
liubocdcb7252011-08-03 10:15:25 +00008024 if (!min_free)
Josef Bacikba1bf482009-09-11 16:11:19 -04008025 goto out;
Chris Mason323da792008-05-09 11:46:48 -04008026
Josef Bacikba1bf482009-09-11 16:11:19 -04008027 space_info = block_group->space_info;
8028 spin_lock(&space_info->lock);
Chris Mason323da792008-05-09 11:46:48 -04008029
Josef Bacikba1bf482009-09-11 16:11:19 -04008030 full = space_info->full;
Zheng Yan1a40e232008-09-26 10:09:34 -04008031
Josef Bacikba1bf482009-09-11 16:11:19 -04008032 /*
8033 * if this is the last block group we have in this space, we can't
Chris Mason7ce618d2009-09-22 14:48:44 -04008034 * relocate it unless we're able to allocate a new chunk below.
8035 *
8036 * Otherwise, we need to make sure we have room in the space to handle
8037 * all of the extents from this block group. If we can, we're good
Josef Bacikba1bf482009-09-11 16:11:19 -04008038 */
Chris Mason7ce618d2009-09-22 14:48:44 -04008039 if ((space_info->total_bytes != block_group->key.offset) &&
liubocdcb7252011-08-03 10:15:25 +00008040 (space_info->bytes_used + space_info->bytes_reserved +
8041 space_info->bytes_pinned + space_info->bytes_readonly +
8042 min_free < space_info->total_bytes)) {
Josef Bacikba1bf482009-09-11 16:11:19 -04008043 spin_unlock(&space_info->lock);
8044 goto out;
8045 }
8046 spin_unlock(&space_info->lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008047
Josef Bacikba1bf482009-09-11 16:11:19 -04008048 /*
8049 * ok we don't have enough space, but maybe we have free space on our
8050 * devices to allocate new chunks for relocation, so loop through our
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008051 * alloc devices and guess if we have enough space. if this block
8052 * group is going to be restriped, run checks against the target
8053 * profile instead of the current one.
Josef Bacikba1bf482009-09-11 16:11:19 -04008054 */
8055 ret = -1;
Chris Mason4313b392008-01-03 09:08:48 -05008056
liubocdcb7252011-08-03 10:15:25 +00008057 /*
8058 * index:
8059 * 0: raid10
8060 * 1: raid1
8061 * 2: dup
8062 * 3: raid0
8063 * 4: single
8064 */
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008065 target = get_restripe_target(root->fs_info, block_group->flags);
8066 if (target) {
Liu Bo31e50222012-11-21 14:18:10 +00008067 index = __get_raid_index(extended_to_chunk(target));
Ilya Dryomov4a5e98f2012-03-27 17:09:17 +03008068 } else {
8069 /*
8070 * this is just a balance, so if we were marked as full
8071 * we know there is no space for a new chunk
8072 */
8073 if (full)
8074 goto out;
8075
8076 index = get_block_group_index(block_group);
8077 }
8078
Miao Xiee6ec7162013-01-17 05:38:51 +00008079 if (index == BTRFS_RAID_RAID10) {
liubocdcb7252011-08-03 10:15:25 +00008080 dev_min = 4;
Josef Bacik6719db62011-08-20 08:29:51 -04008081 /* Divide by 2 */
8082 min_free >>= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008083 } else if (index == BTRFS_RAID_RAID1) {
liubocdcb7252011-08-03 10:15:25 +00008084 dev_min = 2;
Miao Xiee6ec7162013-01-17 05:38:51 +00008085 } else if (index == BTRFS_RAID_DUP) {
Josef Bacik6719db62011-08-20 08:29:51 -04008086 /* Multiply by 2 */
8087 min_free <<= 1;
Miao Xiee6ec7162013-01-17 05:38:51 +00008088 } else if (index == BTRFS_RAID_RAID0) {
liubocdcb7252011-08-03 10:15:25 +00008089 dev_min = fs_devices->rw_devices;
Josef Bacik6719db62011-08-20 08:29:51 -04008090 do_div(min_free, dev_min);
liubocdcb7252011-08-03 10:15:25 +00008091 }
8092
Josef Bacik6df9a952013-06-27 13:22:46 -04008093 /* We need to do this so that we can look at pending chunks */
8094 trans = btrfs_join_transaction(root);
8095 if (IS_ERR(trans)) {
8096 ret = PTR_ERR(trans);
8097 goto out;
8098 }
8099
Josef Bacikba1bf482009-09-11 16:11:19 -04008100 mutex_lock(&root->fs_info->chunk_mutex);
8101 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
Miao Xie7bfc8372011-01-05 10:07:26 +00008102 u64 dev_offset;
Chris Masonea8c2812008-08-04 23:17:27 -04008103
Josef Bacikba1bf482009-09-11 16:11:19 -04008104 /*
8105 * check to make sure we can actually find a chunk with enough
8106 * space to fit our block group in.
8107 */
Stefan Behrens63a212a2012-11-05 18:29:28 +01008108 if (device->total_bytes > device->bytes_used + min_free &&
8109 !device->is_tgtdev_for_dev_replace) {
Josef Bacik6df9a952013-06-27 13:22:46 -04008110 ret = find_free_dev_extent(trans, device, min_free,
Miao Xie7bfc8372011-01-05 10:07:26 +00008111 &dev_offset, NULL);
Josef Bacikba1bf482009-09-11 16:11:19 -04008112 if (!ret)
liubocdcb7252011-08-03 10:15:25 +00008113 dev_nr++;
8114
8115 if (dev_nr >= dev_min)
Yan73e48b22008-01-03 14:14:39 -05008116 break;
liubocdcb7252011-08-03 10:15:25 +00008117
Josef Bacikba1bf482009-09-11 16:11:19 -04008118 ret = -1;
Yan73e48b22008-01-03 14:14:39 -05008119 }
Chris Masonedbd8d42007-12-21 16:27:24 -05008120 }
Josef Bacikba1bf482009-09-11 16:11:19 -04008121 mutex_unlock(&root->fs_info->chunk_mutex);
Josef Bacik6df9a952013-06-27 13:22:46 -04008122 btrfs_end_transaction(trans, root);
Chris Masonedbd8d42007-12-21 16:27:24 -05008123out:
Josef Bacikba1bf482009-09-11 16:11:19 -04008124 btrfs_put_block_group(block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05008125 return ret;
8126}
8127
Christoph Hellwigb2950862008-12-02 09:54:17 -05008128static int find_first_block_group(struct btrfs_root *root,
8129 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04008130{
Chris Mason925baed2008-06-25 16:01:30 -04008131 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008132 struct btrfs_key found_key;
8133 struct extent_buffer *leaf;
8134 int slot;
8135
8136 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8137 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008138 goto out;
8139
Chris Masond3977122009-01-05 21:25:51 -05008140 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008141 slot = path->slots[0];
8142 leaf = path->nodes[0];
8143 if (slot >= btrfs_header_nritems(leaf)) {
8144 ret = btrfs_next_leaf(root, path);
8145 if (ret == 0)
8146 continue;
8147 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04008148 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04008149 break;
8150 }
8151 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8152
8153 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04008154 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8155 ret = 0;
8156 goto out;
8157 }
Chris Mason0b86a832008-03-24 15:01:56 -04008158 path->slots[0]++;
8159 }
Chris Mason925baed2008-06-25 16:01:30 -04008160out:
Chris Mason0b86a832008-03-24 15:01:56 -04008161 return ret;
8162}
8163
Josef Bacik0af3d002010-06-21 14:48:16 -04008164void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8165{
8166 struct btrfs_block_group_cache *block_group;
8167 u64 last = 0;
8168
8169 while (1) {
8170 struct inode *inode;
8171
8172 block_group = btrfs_lookup_first_block_group(info, last);
8173 while (block_group) {
8174 spin_lock(&block_group->lock);
8175 if (block_group->iref)
8176 break;
8177 spin_unlock(&block_group->lock);
8178 block_group = next_block_group(info->tree_root,
8179 block_group);
8180 }
8181 if (!block_group) {
8182 if (last == 0)
8183 break;
8184 last = 0;
8185 continue;
8186 }
8187
8188 inode = block_group->inode;
8189 block_group->iref = 0;
8190 block_group->inode = NULL;
8191 spin_unlock(&block_group->lock);
8192 iput(inode);
8193 last = block_group->key.objectid + block_group->key.offset;
8194 btrfs_put_block_group(block_group);
8195 }
8196}
8197
Zheng Yan1a40e232008-09-26 10:09:34 -04008198int btrfs_free_block_groups(struct btrfs_fs_info *info)
8199{
8200 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04008201 struct btrfs_space_info *space_info;
Yan Zheng11833d62009-09-11 16:11:19 -04008202 struct btrfs_caching_control *caching_ctl;
Zheng Yan1a40e232008-09-26 10:09:34 -04008203 struct rb_node *n;
8204
Yan Zheng11833d62009-09-11 16:11:19 -04008205 down_write(&info->extent_commit_sem);
8206 while (!list_empty(&info->caching_block_groups)) {
8207 caching_ctl = list_entry(info->caching_block_groups.next,
8208 struct btrfs_caching_control, list);
8209 list_del(&caching_ctl->list);
8210 put_caching_control(caching_ctl);
8211 }
8212 up_write(&info->extent_commit_sem);
8213
Zheng Yan1a40e232008-09-26 10:09:34 -04008214 spin_lock(&info->block_group_cache_lock);
8215 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8216 block_group = rb_entry(n, struct btrfs_block_group_cache,
8217 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04008218 rb_erase(&block_group->cache_node,
8219 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04008220 spin_unlock(&info->block_group_cache_lock);
8221
Josef Bacik80eb2342008-10-29 14:49:05 -04008222 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008223 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04008224 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05008225
Josef Bacik817d52f2009-07-13 21:29:25 -04008226 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008227 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008228
Josef Bacik3c148742011-02-02 15:53:47 +00008229 /*
8230 * We haven't cached this block group, which means we could
8231 * possibly have excluded extents on this block group.
8232 */
8233 if (block_group->cached == BTRFS_CACHE_NO)
8234 free_excluded_extents(info->extent_root, block_group);
8235
Josef Bacik817d52f2009-07-13 21:29:25 -04008236 btrfs_remove_free_space_cache(block_group);
Josef Bacik11dfe352009-11-13 20:12:59 +00008237 btrfs_put_block_group(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04008238
8239 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008240 }
8241 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04008242
8243 /* now that all the block groups are freed, go through and
8244 * free all the space_info structs. This is only called during
8245 * the final stages of unmount, and so we know nobody is
8246 * using them. We call synchronize_rcu() once before we start,
8247 * just to be on the safe side.
8248 */
8249 synchronize_rcu();
8250
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04008251 release_global_block_rsv(info);
8252
Chris Mason4184ea72009-03-10 12:39:20 -04008253 while(!list_empty(&info->space_info)) {
8254 space_info = list_entry(info->space_info.next,
8255 struct btrfs_space_info,
8256 list);
David Sterbab069e0c2013-02-08 21:28:17 +00008257 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8258 if (space_info->bytes_pinned > 0 ||
8259 space_info->bytes_reserved > 0 ||
8260 space_info->bytes_may_use > 0) {
8261 WARN_ON(1);
8262 dump_space_info(space_info, 0, 0);
8263 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008264 }
Josef Bacikb150a4f2013-06-19 15:00:04 -04008265 percpu_counter_destroy(&space_info->total_bytes_pinned);
Chris Mason4184ea72009-03-10 12:39:20 -04008266 list_del(&space_info->list);
8267 kfree(space_info);
8268 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008269 return 0;
8270}
8271
Yan, Zhengb742bb82010-05-16 10:46:24 -04008272static void __link_block_group(struct btrfs_space_info *space_info,
8273 struct btrfs_block_group_cache *cache)
8274{
8275 int index = get_block_group_index(cache);
8276
8277 down_write(&space_info->groups_sem);
8278 list_add_tail(&cache->list, &space_info->block_groups[index]);
8279 up_write(&space_info->groups_sem);
8280}
8281
Chris Mason9078a3e2007-04-26 16:46:15 -04008282int btrfs_read_block_groups(struct btrfs_root *root)
8283{
8284 struct btrfs_path *path;
8285 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008286 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04008287 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04008288 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04008289 struct btrfs_key key;
8290 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04008291 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -04008292 int need_clear = 0;
8293 u64 cache_gen;
Chris Mason96b51792007-10-15 16:15:19 -04008294
Chris Masonbe744172007-05-06 10:15:01 -04008295 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04008296 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04008297 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04008298 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04008299 path = btrfs_alloc_path();
8300 if (!path)
8301 return -ENOMEM;
Josef Bacik026fd312011-05-13 10:32:11 -04008302 path->reada = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04008303
David Sterba6c417612011-04-13 15:41:04 +02008304 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
Josef Bacik73bc1872011-10-03 14:07:49 -04008305 if (btrfs_test_opt(root, SPACE_CACHE) &&
David Sterba6c417612011-04-13 15:41:04 +02008306 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
Josef Bacik0af3d002010-06-21 14:48:16 -04008307 need_clear = 1;
Josef Bacik88c2ba32010-09-21 14:21:34 -04008308 if (btrfs_test_opt(root, CLEAR_CACHE))
8309 need_clear = 1;
Josef Bacik0af3d002010-06-21 14:48:16 -04008310
Chris Masond3977122009-01-05 21:25:51 -05008311 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04008312 ret = find_first_block_group(root, path, &key);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008313 if (ret > 0)
8314 break;
Chris Mason0b86a832008-03-24 15:01:56 -04008315 if (ret != 0)
8316 goto error;
Chris Mason5f39d392007-10-15 16:14:19 -04008317 leaf = path->nodes[0];
8318 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04008319 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04008320 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04008321 ret = -ENOMEM;
Yan, Zhengf0486c62010-05-16 10:46:25 -04008322 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04008323 }
Li Zefan34d52cb2011-03-29 13:46:06 +08008324 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8325 GFP_NOFS);
8326 if (!cache->free_space_ctl) {
8327 kfree(cache);
8328 ret = -ENOMEM;
8329 goto error;
8330 }
Chris Mason3e1ad542007-05-07 20:03:49 -04008331
Yan Zhengd2fb3432008-12-11 16:30:39 -05008332 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008333 spin_lock_init(&cache->lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008334 cache->fs_info = info;
Josef Bacik0f9dd462008-09-23 13:14:11 -04008335 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008336 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacik96303082009-07-13 21:29:25 -04008337
Liu Bocf7c1ef2012-07-06 03:31:34 -06008338 if (need_clear) {
8339 /*
8340 * When we mount with old space cache, we need to
8341 * set BTRFS_DC_CLEAR and set dirty flag.
8342 *
8343 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
8344 * truncate the old free space cache inode and
8345 * setup a new one.
8346 * b) Setting 'dirty flag' makes sure that we flush
8347 * the new space cache info onto disk.
8348 */
Josef Bacik0af3d002010-06-21 14:48:16 -04008349 cache->disk_cache_state = BTRFS_DC_CLEAR;
Liu Bocf7c1ef2012-07-06 03:31:34 -06008350 if (btrfs_test_opt(root, SPACE_CACHE))
8351 cache->dirty = 1;
8352 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008353
Chris Mason5f39d392007-10-15 16:14:19 -04008354 read_extent_buffer(leaf, &cache->item,
8355 btrfs_item_ptr_offset(leaf, path->slots[0]),
8356 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04008357 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04008358
Chris Mason9078a3e2007-04-26 16:46:15 -04008359 key.objectid = found_key.objectid + found_key.offset;
David Sterbab3b4aa72011-04-21 01:20:15 +02008360 btrfs_release_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008361 cache->flags = btrfs_block_group_flags(&cache->item);
Josef Bacik817d52f2009-07-13 21:29:25 -04008362 cache->sectorsize = root->sectorsize;
David Woodhouse53b381b2013-01-29 18:40:14 -05008363 cache->full_stripe_len = btrfs_full_stripe_len(root,
8364 &root->fs_info->mapping_tree,
8365 found_key.objectid);
Li Zefan34d52cb2011-03-29 13:46:06 +08008366 btrfs_init_free_space_ctl(cache);
8367
Josef Bacik817d52f2009-07-13 21:29:25 -04008368 /*
Josef Bacik3c148742011-02-02 15:53:47 +00008369 * We need to exclude the super stripes now so that the space
8370 * info has super bytes accounted for, otherwise we'll think
8371 * we have more space than we actually do.
8372 */
Josef Bacik835d9742013-03-19 12:13:25 -04008373 ret = exclude_super_stripes(root, cache);
8374 if (ret) {
8375 /*
8376 * We may have excluded something, so call this just in
8377 * case.
8378 */
8379 free_excluded_extents(root, cache);
8380 kfree(cache->free_space_ctl);
8381 kfree(cache);
8382 goto error;
8383 }
Josef Bacik3c148742011-02-02 15:53:47 +00008384
8385 /*
Josef Bacik817d52f2009-07-13 21:29:25 -04008386 * check for two cases, either we are full, and therefore
8387 * don't need to bother with the caching work since we won't
8388 * find any space, or we are empty, and we can just add all
8389 * the space in and be done with it. This saves us _alot_ of
8390 * time, particularly in the full case.
8391 */
8392 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
Yan Zheng11833d62009-09-11 16:11:19 -04008393 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008394 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik1b2da372009-09-11 16:11:20 -04008395 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008396 } else if (btrfs_block_group_used(&cache->item) == 0) {
Yan Zheng11833d62009-09-11 16:11:19 -04008397 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008398 cache->cached = BTRFS_CACHE_FINISHED;
8399 add_new_free_space(cache, root->fs_info,
8400 found_key.objectid,
8401 found_key.objectid +
8402 found_key.offset);
Yan Zheng11833d62009-09-11 16:11:19 -04008403 free_excluded_extents(root, cache);
Josef Bacik817d52f2009-07-13 21:29:25 -04008404 }
Chris Mason96b51792007-10-15 16:15:19 -04008405
Josef Bacik8c579fe2013-04-02 12:40:42 -04008406 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8407 if (ret) {
8408 btrfs_remove_free_space_cache(cache);
8409 btrfs_put_block_group(cache);
8410 goto error;
8411 }
8412
Chris Mason6324fbf2008-03-24 15:01:59 -04008413 ret = update_space_info(info, cache->flags, found_key.offset,
8414 btrfs_block_group_used(&cache->item),
8415 &space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008416 if (ret) {
8417 btrfs_remove_free_space_cache(cache);
8418 spin_lock(&info->block_group_cache_lock);
8419 rb_erase(&cache->cache_node,
8420 &info->block_group_cache_tree);
8421 spin_unlock(&info->block_group_cache_lock);
8422 btrfs_put_block_group(cache);
8423 goto error;
8424 }
8425
Chris Mason6324fbf2008-03-24 15:01:59 -04008426 cache->space_info = space_info;
Josef Bacik1b2da372009-09-11 16:11:20 -04008427 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008428 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008429 spin_unlock(&cache->space_info->lock);
8430
Yan, Zhengb742bb82010-05-16 10:46:24 -04008431 __link_block_group(space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008432
Chris Mason75ccf472008-09-30 19:24:06 -04008433 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05008434 if (btrfs_chunk_readonly(root, cache->key.objectid))
Miao Xie199c36e2011-07-15 10:34:36 +00008435 set_block_group_ro(cache, 1);
Chris Mason9078a3e2007-04-26 16:46:15 -04008436 }
Yan, Zhengb742bb82010-05-16 10:46:24 -04008437
8438 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8439 if (!(get_alloc_profile(root, space_info->flags) &
8440 (BTRFS_BLOCK_GROUP_RAID10 |
8441 BTRFS_BLOCK_GROUP_RAID1 |
David Woodhouse53b381b2013-01-29 18:40:14 -05008442 BTRFS_BLOCK_GROUP_RAID5 |
8443 BTRFS_BLOCK_GROUP_RAID6 |
Yan, Zhengb742bb82010-05-16 10:46:24 -04008444 BTRFS_BLOCK_GROUP_DUP)))
8445 continue;
8446 /*
8447 * avoid allocating from un-mirrored block group if there are
8448 * mirrored block groups.
8449 */
chandan1095cc02013-07-16 12:28:56 +05308450 list_for_each_entry(cache,
8451 &space_info->block_groups[BTRFS_RAID_RAID0],
8452 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008453 set_block_group_ro(cache, 1);
chandan1095cc02013-07-16 12:28:56 +05308454 list_for_each_entry(cache,
8455 &space_info->block_groups[BTRFS_RAID_SINGLE],
8456 list)
Miao Xie199c36e2011-07-15 10:34:36 +00008457 set_block_group_ro(cache, 1);
Yan, Zhengb742bb82010-05-16 10:46:24 -04008458 }
Yan, Zhengf0486c62010-05-16 10:46:25 -04008459
8460 init_global_block_rsv(info);
Chris Mason0b86a832008-03-24 15:01:56 -04008461 ret = 0;
8462error:
Chris Mason9078a3e2007-04-26 16:46:15 -04008463 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04008464 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04008465}
Chris Mason6324fbf2008-03-24 15:01:59 -04008466
Josef Bacikea658ba2012-09-11 16:57:25 -04008467void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
8468 struct btrfs_root *root)
8469{
8470 struct btrfs_block_group_cache *block_group, *tmp;
8471 struct btrfs_root *extent_root = root->fs_info->extent_root;
8472 struct btrfs_block_group_item item;
8473 struct btrfs_key key;
8474 int ret = 0;
8475
8476 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
8477 new_bg_list) {
8478 list_del_init(&block_group->new_bg_list);
8479
8480 if (ret)
8481 continue;
8482
8483 spin_lock(&block_group->lock);
8484 memcpy(&item, &block_group->item, sizeof(item));
8485 memcpy(&key, &block_group->key, sizeof(key));
8486 spin_unlock(&block_group->lock);
8487
8488 ret = btrfs_insert_item(trans, extent_root, &key, &item,
8489 sizeof(item));
8490 if (ret)
8491 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacik6df9a952013-06-27 13:22:46 -04008492 ret = btrfs_finish_chunk_alloc(trans, extent_root,
8493 key.objectid, key.offset);
8494 if (ret)
8495 btrfs_abort_transaction(trans, extent_root, ret);
Josef Bacikea658ba2012-09-11 16:57:25 -04008496 }
8497}
8498
Chris Mason6324fbf2008-03-24 15:01:59 -04008499int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8500 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04008501 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04008502 u64 size)
8503{
8504 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04008505 struct btrfs_root *extent_root;
8506 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04008507
8508 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04008509
Chris Mason12fcfd22009-03-24 10:24:20 -04008510 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04008511
Chris Mason8f18cf12008-04-25 16:53:30 -04008512 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008513 if (!cache)
8514 return -ENOMEM;
Li Zefan34d52cb2011-03-29 13:46:06 +08008515 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8516 GFP_NOFS);
8517 if (!cache->free_space_ctl) {
8518 kfree(cache);
8519 return -ENOMEM;
8520 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04008521
Chris Masone17cade2008-04-15 15:41:47 -04008522 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04008523 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05008524 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
Josef Bacik96303082009-07-13 21:29:25 -04008525 cache->sectorsize = root->sectorsize;
Josef Bacik0af3d002010-06-21 14:48:16 -04008526 cache->fs_info = root->fs_info;
David Woodhouse53b381b2013-01-29 18:40:14 -05008527 cache->full_stripe_len = btrfs_full_stripe_len(root,
8528 &root->fs_info->mapping_tree,
8529 chunk_offset);
Josef Bacik96303082009-07-13 21:29:25 -04008530
Yan Zhengd2fb3432008-12-11 16:30:39 -05008531 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04008532 spin_lock_init(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04008533 INIT_LIST_HEAD(&cache->list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04008534 INIT_LIST_HEAD(&cache->cluster_list);
Josef Bacikea658ba2012-09-11 16:57:25 -04008535 INIT_LIST_HEAD(&cache->new_bg_list);
Chris Mason0ef3e662008-05-24 14:04:53 -04008536
Li Zefan34d52cb2011-03-29 13:46:06 +08008537 btrfs_init_free_space_ctl(cache);
8538
Chris Mason6324fbf2008-03-24 15:01:59 -04008539 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04008540 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8541 cache->flags = type;
8542 btrfs_set_block_group_flags(&cache->item, type);
8543
Yan Zheng11833d62009-09-11 16:11:19 -04008544 cache->last_byte_to_unpin = (u64)-1;
Josef Bacik817d52f2009-07-13 21:29:25 -04008545 cache->cached = BTRFS_CACHE_FINISHED;
Josef Bacik835d9742013-03-19 12:13:25 -04008546 ret = exclude_super_stripes(root, cache);
8547 if (ret) {
8548 /*
8549 * We may have excluded something, so call this just in
8550 * case.
8551 */
8552 free_excluded_extents(root, cache);
8553 kfree(cache->free_space_ctl);
8554 kfree(cache);
8555 return ret;
8556 }
Josef Bacik96303082009-07-13 21:29:25 -04008557
Josef Bacik817d52f2009-07-13 21:29:25 -04008558 add_new_free_space(cache, root->fs_info, chunk_offset,
8559 chunk_offset + size);
8560
Yan Zheng11833d62009-09-11 16:11:19 -04008561 free_excluded_extents(root, cache);
8562
Josef Bacik8c579fe2013-04-02 12:40:42 -04008563 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8564 if (ret) {
8565 btrfs_remove_free_space_cache(cache);
8566 btrfs_put_block_group(cache);
8567 return ret;
8568 }
8569
Chris Mason6324fbf2008-03-24 15:01:59 -04008570 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8571 &cache->space_info);
Josef Bacik8c579fe2013-04-02 12:40:42 -04008572 if (ret) {
8573 btrfs_remove_free_space_cache(cache);
8574 spin_lock(&root->fs_info->block_group_cache_lock);
8575 rb_erase(&cache->cache_node,
8576 &root->fs_info->block_group_cache_tree);
8577 spin_unlock(&root->fs_info->block_group_cache_lock);
8578 btrfs_put_block_group(cache);
8579 return ret;
8580 }
Li Zefanc7c144d2011-12-07 10:39:22 +08008581 update_global_block_rsv(root->fs_info);
Josef Bacik1b2da372009-09-11 16:11:20 -04008582
8583 spin_lock(&cache->space_info->lock);
Yan, Zhengf0486c62010-05-16 10:46:25 -04008584 cache->space_info->bytes_readonly += cache->bytes_super;
Josef Bacik1b2da372009-09-11 16:11:20 -04008585 spin_unlock(&cache->space_info->lock);
8586
Yan, Zhengb742bb82010-05-16 10:46:24 -04008587 __link_block_group(cache->space_info, cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04008588
Josef Bacikea658ba2012-09-11 16:57:25 -04008589 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
Chris Mason6324fbf2008-03-24 15:01:59 -04008590
Chris Masond18a2c42008-04-04 15:40:00 -04008591 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04008592
Chris Mason6324fbf2008-03-24 15:01:59 -04008593 return 0;
8594}
Zheng Yan1a40e232008-09-26 10:09:34 -04008595
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008596static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
8597{
Ilya Dryomov899c81e2012-03-27 17:09:16 +03008598 u64 extra_flags = chunk_to_extended(flags) &
8599 BTRFS_EXTENDED_PROFILE_MASK;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008600
Miao Xiede98ced2013-01-29 10:13:12 +00008601 write_seqlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008602 if (flags & BTRFS_BLOCK_GROUP_DATA)
8603 fs_info->avail_data_alloc_bits &= ~extra_flags;
8604 if (flags & BTRFS_BLOCK_GROUP_METADATA)
8605 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
8606 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
8607 fs_info->avail_system_alloc_bits &= ~extra_flags;
Miao Xiede98ced2013-01-29 10:13:12 +00008608 write_sequnlock(&fs_info->profiles_lock);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008609}
8610
Zheng Yan1a40e232008-09-26 10:09:34 -04008611int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8612 struct btrfs_root *root, u64 group_start)
8613{
8614 struct btrfs_path *path;
8615 struct btrfs_block_group_cache *block_group;
Chris Mason44fb5512009-06-04 15:34:51 -04008616 struct btrfs_free_cluster *cluster;
Josef Bacik0af3d002010-06-21 14:48:16 -04008617 struct btrfs_root *tree_root = root->fs_info->tree_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04008618 struct btrfs_key key;
Josef Bacik0af3d002010-06-21 14:48:16 -04008619 struct inode *inode;
Zheng Yan1a40e232008-09-26 10:09:34 -04008620 int ret;
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008621 int index;
Josef Bacik89a55892010-10-14 14:52:27 -04008622 int factor;
Zheng Yan1a40e232008-09-26 10:09:34 -04008623
Zheng Yan1a40e232008-09-26 10:09:34 -04008624 root = root->fs_info->extent_root;
8625
8626 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8627 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05008628 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04008629
liubo9f7c43c2011-03-07 02:13:33 +00008630 /*
8631 * Free the reserved super bytes from this block group before
8632 * remove it.
8633 */
8634 free_excluded_extents(root, block_group);
8635
Zheng Yan1a40e232008-09-26 10:09:34 -04008636 memcpy(&key, &block_group->key, sizeof(key));
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008637 index = get_block_group_index(block_group);
Josef Bacik89a55892010-10-14 14:52:27 -04008638 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8639 BTRFS_BLOCK_GROUP_RAID1 |
8640 BTRFS_BLOCK_GROUP_RAID10))
8641 factor = 2;
8642 else
8643 factor = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04008644
Chris Mason44fb5512009-06-04 15:34:51 -04008645 /* make sure this block group isn't part of an allocation cluster */
8646 cluster = &root->fs_info->data_alloc_cluster;
8647 spin_lock(&cluster->refill_lock);
8648 btrfs_return_cluster_to_free_space(block_group, cluster);
8649 spin_unlock(&cluster->refill_lock);
8650
8651 /*
8652 * make sure this block group isn't part of a metadata
8653 * allocation cluster
8654 */
8655 cluster = &root->fs_info->meta_alloc_cluster;
8656 spin_lock(&cluster->refill_lock);
8657 btrfs_return_cluster_to_free_space(block_group, cluster);
8658 spin_unlock(&cluster->refill_lock);
8659
Zheng Yan1a40e232008-09-26 10:09:34 -04008660 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07008661 if (!path) {
8662 ret = -ENOMEM;
8663 goto out;
8664 }
Zheng Yan1a40e232008-09-26 10:09:34 -04008665
Ilya Dryomov10b2f342011-10-02 13:56:53 +03008666 inode = lookup_free_space_inode(tree_root, block_group, path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008667 if (!IS_ERR(inode)) {
Tsutomu Itohb5324022011-07-19 07:27:20 +00008668 ret = btrfs_orphan_add(trans, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01008669 if (ret) {
8670 btrfs_add_delayed_iput(inode);
8671 goto out;
8672 }
Josef Bacik0af3d002010-06-21 14:48:16 -04008673 clear_nlink(inode);
8674 /* One for the block groups ref */
8675 spin_lock(&block_group->lock);
8676 if (block_group->iref) {
8677 block_group->iref = 0;
8678 block_group->inode = NULL;
8679 spin_unlock(&block_group->lock);
8680 iput(inode);
8681 } else {
8682 spin_unlock(&block_group->lock);
8683 }
8684 /* One for our lookup ref */
Josef Bacik455757c2011-09-19 12:26:24 -04008685 btrfs_add_delayed_iput(inode);
Josef Bacik0af3d002010-06-21 14:48:16 -04008686 }
8687
8688 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8689 key.offset = block_group->key.objectid;
8690 key.type = 0;
8691
8692 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8693 if (ret < 0)
8694 goto out;
8695 if (ret > 0)
David Sterbab3b4aa72011-04-21 01:20:15 +02008696 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008697 if (ret == 0) {
8698 ret = btrfs_del_item(trans, tree_root, path);
8699 if (ret)
8700 goto out;
David Sterbab3b4aa72011-04-21 01:20:15 +02008701 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -04008702 }
8703
Yan Zheng3dfdb932009-01-21 10:49:16 -05008704 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04008705 rb_erase(&block_group->cache_node,
8706 &root->fs_info->block_group_cache_tree);
Liu Boa1897fd2012-12-27 09:01:23 +00008707
8708 if (root->fs_info->first_logical_byte == block_group->key.objectid)
8709 root->fs_info->first_logical_byte = (u64)-1;
Yan Zheng3dfdb932009-01-21 10:49:16 -05008710 spin_unlock(&root->fs_info->block_group_cache_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04008711
Josef Bacik80eb2342008-10-29 14:49:05 -04008712 down_write(&block_group->space_info->groups_sem);
Chris Mason44fb5512009-06-04 15:34:51 -04008713 /*
8714 * we must use list_del_init so people can check to see if they
8715 * are still on the list after taking the semaphore
8716 */
8717 list_del_init(&block_group->list);
Ilya Dryomov10ea00f2012-01-16 22:04:47 +02008718 if (list_empty(&block_group->space_info->block_groups[index]))
8719 clear_avail_alloc_bits(root->fs_info, block_group->flags);
Josef Bacik80eb2342008-10-29 14:49:05 -04008720 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04008721
Josef Bacik817d52f2009-07-13 21:29:25 -04008722 if (block_group->cached == BTRFS_CACHE_STARTED)
Yan Zheng11833d62009-09-11 16:11:19 -04008723 wait_block_group_cache_done(block_group);
Josef Bacik817d52f2009-07-13 21:29:25 -04008724
8725 btrfs_remove_free_space_cache(block_group);
8726
Yan Zhengc146afa2008-11-12 14:34:12 -05008727 spin_lock(&block_group->space_info->lock);
8728 block_group->space_info->total_bytes -= block_group->key.offset;
8729 block_group->space_info->bytes_readonly -= block_group->key.offset;
Josef Bacik89a55892010-10-14 14:52:27 -04008730 block_group->space_info->disk_total -= block_group->key.offset * factor;
Yan Zhengc146afa2008-11-12 14:34:12 -05008731 spin_unlock(&block_group->space_info->lock);
Chris Mason283bb192009-07-24 16:30:55 -04008732
Josef Bacik0af3d002010-06-21 14:48:16 -04008733 memcpy(&key, &block_group->key, sizeof(key));
8734
Chris Mason283bb192009-07-24 16:30:55 -04008735 btrfs_clear_space_info_full(root->fs_info);
Yan Zhengc146afa2008-11-12 14:34:12 -05008736
Chris Masonfa9c0d792009-04-03 09:47:43 -04008737 btrfs_put_block_group(block_group);
8738 btrfs_put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04008739
8740 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8741 if (ret > 0)
8742 ret = -EIO;
8743 if (ret < 0)
8744 goto out;
8745
8746 ret = btrfs_del_item(trans, root, path);
8747out:
8748 btrfs_free_path(path);
8749 return ret;
8750}
liuboacce9522011-01-06 19:30:25 +08008751
liuboc59021f2011-03-07 02:13:14 +00008752int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8753{
8754 struct btrfs_space_info *space_info;
liubo1aba86d2011-04-08 08:44:37 +00008755 struct btrfs_super_block *disk_super;
8756 u64 features;
8757 u64 flags;
8758 int mixed = 0;
liuboc59021f2011-03-07 02:13:14 +00008759 int ret;
8760
David Sterba6c417612011-04-13 15:41:04 +02008761 disk_super = fs_info->super_copy;
liubo1aba86d2011-04-08 08:44:37 +00008762 if (!btrfs_super_root(disk_super))
8763 return 1;
liuboc59021f2011-03-07 02:13:14 +00008764
liubo1aba86d2011-04-08 08:44:37 +00008765 features = btrfs_super_incompat_flags(disk_super);
8766 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8767 mixed = 1;
liuboc59021f2011-03-07 02:13:14 +00008768
liubo1aba86d2011-04-08 08:44:37 +00008769 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8770 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
liuboc59021f2011-03-07 02:13:14 +00008771 if (ret)
liubo1aba86d2011-04-08 08:44:37 +00008772 goto out;
liuboc59021f2011-03-07 02:13:14 +00008773
liubo1aba86d2011-04-08 08:44:37 +00008774 if (mixed) {
8775 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8776 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8777 } else {
8778 flags = BTRFS_BLOCK_GROUP_METADATA;
8779 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8780 if (ret)
8781 goto out;
8782
8783 flags = BTRFS_BLOCK_GROUP_DATA;
8784 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8785 }
8786out:
liuboc59021f2011-03-07 02:13:14 +00008787 return ret;
8788}
8789
liuboacce9522011-01-06 19:30:25 +08008790int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8791{
8792 return unpin_extent_range(root, start, end);
8793}
8794
8795int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
Li Dongyang5378e602011-03-24 10:24:27 +00008796 u64 num_bytes, u64 *actual_bytes)
liuboacce9522011-01-06 19:30:25 +08008797{
Li Dongyang5378e602011-03-24 10:24:27 +00008798 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
liuboacce9522011-01-06 19:30:25 +08008799}
Li Dongyangf7039b12011-03-24 10:24:28 +00008800
8801int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8802{
8803 struct btrfs_fs_info *fs_info = root->fs_info;
8804 struct btrfs_block_group_cache *cache = NULL;
8805 u64 group_trimmed;
8806 u64 start;
8807 u64 end;
8808 u64 trimmed = 0;
Liu Bo2cac13e2012-02-09 18:17:41 +08008809 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
Li Dongyangf7039b12011-03-24 10:24:28 +00008810 int ret = 0;
8811
Liu Bo2cac13e2012-02-09 18:17:41 +08008812 /*
8813 * try to trim all FS space, our block group may start from non-zero.
8814 */
8815 if (range->len == total_bytes)
8816 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8817 else
8818 cache = btrfs_lookup_block_group(fs_info, range->start);
Li Dongyangf7039b12011-03-24 10:24:28 +00008819
8820 while (cache) {
8821 if (cache->key.objectid >= (range->start + range->len)) {
8822 btrfs_put_block_group(cache);
8823 break;
8824 }
8825
8826 start = max(range->start, cache->key.objectid);
8827 end = min(range->start + range->len,
8828 cache->key.objectid + cache->key.offset);
8829
8830 if (end - start >= range->minlen) {
8831 if (!block_group_cache_done(cache)) {
Liu Bof6373bf2012-12-27 09:01:18 +00008832 ret = cache_block_group(cache, 0);
Josef Bacik1be41b72013-06-12 13:56:06 -04008833 if (ret) {
8834 btrfs_put_block_group(cache);
8835 break;
8836 }
8837 ret = wait_block_group_cache_done(cache);
8838 if (ret) {
8839 btrfs_put_block_group(cache);
8840 break;
8841 }
Li Dongyangf7039b12011-03-24 10:24:28 +00008842 }
8843 ret = btrfs_trim_block_group(cache,
8844 &group_trimmed,
8845 start,
8846 end,
8847 range->minlen);
8848
8849 trimmed += group_trimmed;
8850 if (ret) {
8851 btrfs_put_block_group(cache);
8852 break;
8853 }
8854 }
8855
8856 cache = next_block_group(fs_info->tree_root, cache);
8857 }
8858
8859 range->len = trimmed;
8860 return ret;
8861}