Btrfs: put the block group cache after we commit the super
[deliverable/linux.git] / fs / btrfs / extent-tree.c
CommitLineData
6cbd5570
CM
1/*
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 */
ec6b910f 18#include <linux/sched.h>
edbd8d4e 19#include <linux/pagemap.h>
ec44a35c 20#include <linux/writeback.h>
21af804c 21#include <linux/blkdev.h>
b7a9f29f 22#include <linux/sort.h>
4184ea7f 23#include <linux/rcupdate.h>
817d52f8 24#include <linux/kthread.h>
5a0e3ad6 25#include <linux/slab.h>
4b4e25f2 26#include "compat.h"
74493f7a 27#include "hash.h"
fec577fb
CM
28#include "ctree.h"
29#include "disk-io.h"
30#include "print-tree.h"
e089f05c 31#include "transaction.h"
0b86a832 32#include "volumes.h"
925baedd 33#include "locking.h"
fa9c0d79 34#include "free-space-cache.h"
fec577fb 35
0e4f8f88
CM
36/* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
47 *
48 */
49enum {
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
53};
54
fb25e914
JB
55/*
56 * Control how reservations are dealt with.
57 *
58 * RESERVE_FREE - freeing a reservation.
59 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
60 * ENOSPC accounting
61 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
62 * bytes_may_use as the ENOSPC accounting is done elsewhere
63 */
64enum {
65 RESERVE_FREE = 0,
66 RESERVE_ALLOC = 1,
67 RESERVE_ALLOC_NO_ACCOUNT = 2,
68};
69
f3465ca4
JB
70static int update_block_group(struct btrfs_trans_handle *trans,
71 struct btrfs_root *root,
f0486c68 72 u64 bytenr, u64 num_bytes, int alloc);
5d4f98a2
YZ
73static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
74 struct btrfs_root *root,
75 u64 bytenr, u64 num_bytes, u64 parent,
76 u64 root_objectid, u64 owner_objectid,
77 u64 owner_offset, int refs_to_drop,
78 struct btrfs_delayed_extent_op *extra_op);
79static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
80 struct extent_buffer *leaf,
81 struct btrfs_extent_item *ei);
82static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
83 struct btrfs_root *root,
84 u64 parent, u64 root_objectid,
85 u64 flags, u64 owner, u64 offset,
86 struct btrfs_key *ins, int ref_mod);
87static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
88 struct btrfs_root *root,
89 u64 parent, u64 root_objectid,
90 u64 flags, struct btrfs_disk_key *key,
91 int level, struct btrfs_key *ins);
6a63209f
JB
92static int do_chunk_alloc(struct btrfs_trans_handle *trans,
93 struct btrfs_root *extent_root, u64 alloc_bytes,
94 u64 flags, int force);
11833d66
YZ
95static int find_next_key(struct btrfs_path *path, int level,
96 struct btrfs_key *key);
9ed74f2d
JB
97static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
98 int dump_block_groups);
fb25e914
JB
99static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
100 u64 num_bytes, int reserve);
6a63209f 101
817d52f8
JB
102static noinline int
103block_group_cache_done(struct btrfs_block_group_cache *cache)
104{
105 smp_mb();
106 return cache->cached == BTRFS_CACHE_FINISHED;
107}
108
0f9dd46c
JB
109static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
110{
111 return (cache->flags & bits) == bits;
112}
113
62a45b60 114static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
11dfe35a
JB
115{
116 atomic_inc(&cache->count);
117}
118
119void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
120{
f0486c68
YZ
121 if (atomic_dec_and_test(&cache->count)) {
122 WARN_ON(cache->pinned > 0);
123 WARN_ON(cache->reserved > 0);
34d52cb6 124 kfree(cache->free_space_ctl);
11dfe35a 125 kfree(cache);
f0486c68 126 }
11dfe35a
JB
127}
128
0f9dd46c
JB
129/*
130 * this adds the block group to the fs_info rb tree for the block group
131 * cache
132 */
b2950863 133static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
0f9dd46c
JB
134 struct btrfs_block_group_cache *block_group)
135{
136 struct rb_node **p;
137 struct rb_node *parent = NULL;
138 struct btrfs_block_group_cache *cache;
139
140 spin_lock(&info->block_group_cache_lock);
141 p = &info->block_group_cache_tree.rb_node;
142
143 while (*p) {
144 parent = *p;
145 cache = rb_entry(parent, struct btrfs_block_group_cache,
146 cache_node);
147 if (block_group->key.objectid < cache->key.objectid) {
148 p = &(*p)->rb_left;
149 } else if (block_group->key.objectid > cache->key.objectid) {
150 p = &(*p)->rb_right;
151 } else {
152 spin_unlock(&info->block_group_cache_lock);
153 return -EEXIST;
154 }
155 }
156
157 rb_link_node(&block_group->cache_node, parent, p);
158 rb_insert_color(&block_group->cache_node,
159 &info->block_group_cache_tree);
160 spin_unlock(&info->block_group_cache_lock);
161
162 return 0;
163}
164
165/*
166 * This will return the block group at or after bytenr if contains is 0, else
167 * it will return the block group that contains the bytenr
168 */
169static struct btrfs_block_group_cache *
170block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
171 int contains)
172{
173 struct btrfs_block_group_cache *cache, *ret = NULL;
174 struct rb_node *n;
175 u64 end, start;
176
177 spin_lock(&info->block_group_cache_lock);
178 n = info->block_group_cache_tree.rb_node;
179
180 while (n) {
181 cache = rb_entry(n, struct btrfs_block_group_cache,
182 cache_node);
183 end = cache->key.objectid + cache->key.offset - 1;
184 start = cache->key.objectid;
185
186 if (bytenr < start) {
187 if (!contains && (!ret || start < ret->key.objectid))
188 ret = cache;
189 n = n->rb_left;
190 } else if (bytenr > start) {
191 if (contains && bytenr <= end) {
192 ret = cache;
193 break;
194 }
195 n = n->rb_right;
196 } else {
197 ret = cache;
198 break;
199 }
200 }
d2fb3437 201 if (ret)
11dfe35a 202 btrfs_get_block_group(ret);
0f9dd46c
JB
203 spin_unlock(&info->block_group_cache_lock);
204
205 return ret;
206}
207
11833d66
YZ
208static int add_excluded_extent(struct btrfs_root *root,
209 u64 start, u64 num_bytes)
817d52f8 210{
11833d66
YZ
211 u64 end = start + num_bytes - 1;
212 set_extent_bits(&root->fs_info->freed_extents[0],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
214 set_extent_bits(&root->fs_info->freed_extents[1],
215 start, end, EXTENT_UPTODATE, GFP_NOFS);
216 return 0;
217}
817d52f8 218
11833d66
YZ
219static void free_excluded_extents(struct btrfs_root *root,
220 struct btrfs_block_group_cache *cache)
221{
222 u64 start, end;
817d52f8 223
11833d66
YZ
224 start = cache->key.objectid;
225 end = start + cache->key.offset - 1;
226
227 clear_extent_bits(&root->fs_info->freed_extents[0],
228 start, end, EXTENT_UPTODATE, GFP_NOFS);
229 clear_extent_bits(&root->fs_info->freed_extents[1],
230 start, end, EXTENT_UPTODATE, GFP_NOFS);
817d52f8
JB
231}
232
11833d66
YZ
233static int exclude_super_stripes(struct btrfs_root *root,
234 struct btrfs_block_group_cache *cache)
817d52f8 235{
817d52f8
JB
236 u64 bytenr;
237 u64 *logical;
238 int stripe_len;
239 int i, nr, ret;
240
06b2331f
YZ
241 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
242 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
243 cache->bytes_super += stripe_len;
244 ret = add_excluded_extent(root, cache->key.objectid,
245 stripe_len);
246 BUG_ON(ret);
247 }
248
817d52f8
JB
249 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
250 bytenr = btrfs_sb_offset(i);
251 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
252 cache->key.objectid, bytenr,
253 0, &logical, &nr, &stripe_len);
254 BUG_ON(ret);
11833d66 255
817d52f8 256 while (nr--) {
1b2da372 257 cache->bytes_super += stripe_len;
11833d66
YZ
258 ret = add_excluded_extent(root, logical[nr],
259 stripe_len);
260 BUG_ON(ret);
817d52f8 261 }
11833d66 262
817d52f8
JB
263 kfree(logical);
264 }
817d52f8
JB
265 return 0;
266}
267
11833d66
YZ
268static struct btrfs_caching_control *
269get_caching_control(struct btrfs_block_group_cache *cache)
270{
271 struct btrfs_caching_control *ctl;
272
273 spin_lock(&cache->lock);
274 if (cache->cached != BTRFS_CACHE_STARTED) {
275 spin_unlock(&cache->lock);
276 return NULL;
277 }
278
dde5abee
JB
279 /* We're loading it the fast way, so we don't have a caching_ctl. */
280 if (!cache->caching_ctl) {
281 spin_unlock(&cache->lock);
11833d66
YZ
282 return NULL;
283 }
284
285 ctl = cache->caching_ctl;
286 atomic_inc(&ctl->count);
287 spin_unlock(&cache->lock);
288 return ctl;
289}
290
291static void put_caching_control(struct btrfs_caching_control *ctl)
292{
293 if (atomic_dec_and_test(&ctl->count))
294 kfree(ctl);
295}
296
0f9dd46c
JB
297/*
298 * this is only called by cache_block_group, since we could have freed extents
299 * we need to check the pinned_extents for any extents that can't be used yet
300 * since their free space will be released as soon as the transaction commits.
301 */
817d52f8 302static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
0f9dd46c
JB
303 struct btrfs_fs_info *info, u64 start, u64 end)
304{
817d52f8 305 u64 extent_start, extent_end, size, total_added = 0;
0f9dd46c
JB
306 int ret;
307
308 while (start < end) {
11833d66 309 ret = find_first_extent_bit(info->pinned_extents, start,
0f9dd46c 310 &extent_start, &extent_end,
11833d66 311 EXTENT_DIRTY | EXTENT_UPTODATE);
0f9dd46c
JB
312 if (ret)
313 break;
314
06b2331f 315 if (extent_start <= start) {
0f9dd46c
JB
316 start = extent_end + 1;
317 } else if (extent_start > start && extent_start < end) {
318 size = extent_start - start;
817d52f8 319 total_added += size;
ea6a478e
JB
320 ret = btrfs_add_free_space(block_group, start,
321 size);
0f9dd46c
JB
322 BUG_ON(ret);
323 start = extent_end + 1;
324 } else {
325 break;
326 }
327 }
328
329 if (start < end) {
330 size = end - start;
817d52f8 331 total_added += size;
ea6a478e 332 ret = btrfs_add_free_space(block_group, start, size);
0f9dd46c
JB
333 BUG_ON(ret);
334 }
335
817d52f8 336 return total_added;
0f9dd46c
JB
337}
338
bab39bf9 339static noinline void caching_thread(struct btrfs_work *work)
e37c9e69 340{
bab39bf9
JB
341 struct btrfs_block_group_cache *block_group;
342 struct btrfs_fs_info *fs_info;
343 struct btrfs_caching_control *caching_ctl;
344 struct btrfs_root *extent_root;
e37c9e69 345 struct btrfs_path *path;
5f39d397 346 struct extent_buffer *leaf;
11833d66 347 struct btrfs_key key;
817d52f8 348 u64 total_found = 0;
11833d66
YZ
349 u64 last = 0;
350 u32 nritems;
351 int ret = 0;
f510cfec 352
bab39bf9
JB
353 caching_ctl = container_of(work, struct btrfs_caching_control, work);
354 block_group = caching_ctl->block_group;
355 fs_info = block_group->fs_info;
356 extent_root = fs_info->extent_root;
357
e37c9e69
CM
358 path = btrfs_alloc_path();
359 if (!path)
bab39bf9 360 goto out;
7d7d6068 361
817d52f8 362 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
11833d66 363
5cd57b2c 364 /*
817d52f8
JB
365 * We don't want to deadlock with somebody trying to allocate a new
366 * extent for the extent root while also trying to search the extent
367 * root to add free space. So we skip locking and search the commit
368 * root, since its read-only
5cd57b2c
CM
369 */
370 path->skip_locking = 1;
817d52f8 371 path->search_commit_root = 1;
026fd317 372 path->reada = 1;
817d52f8 373
e4404d6e 374 key.objectid = last;
e37c9e69 375 key.offset = 0;
11833d66 376 key.type = BTRFS_EXTENT_ITEM_KEY;
013f1b12 377again:
11833d66 378 mutex_lock(&caching_ctl->mutex);
013f1b12
CM
379 /* need to make sure the commit_root doesn't disappear */
380 down_read(&fs_info->extent_commit_sem);
381
11833d66 382 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
e37c9e69 383 if (ret < 0)
ef8bbdfe 384 goto err;
a512bbf8 385
11833d66
YZ
386 leaf = path->nodes[0];
387 nritems = btrfs_header_nritems(leaf);
388
d397712b 389 while (1) {
7841cb28 390 if (btrfs_fs_closing(fs_info) > 1) {
f25784b3 391 last = (u64)-1;
817d52f8 392 break;
f25784b3 393 }
817d52f8 394
11833d66
YZ
395 if (path->slots[0] < nritems) {
396 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
397 } else {
398 ret = find_next_key(path, 0, &key);
399 if (ret)
e37c9e69 400 break;
817d52f8 401
589d8ade
JB
402 if (need_resched() ||
403 btrfs_next_leaf(extent_root, path)) {
404 caching_ctl->progress = last;
ff5714cc 405 btrfs_release_path(path);
589d8ade
JB
406 up_read(&fs_info->extent_commit_sem);
407 mutex_unlock(&caching_ctl->mutex);
11833d66 408 cond_resched();
589d8ade
JB
409 goto again;
410 }
411 leaf = path->nodes[0];
412 nritems = btrfs_header_nritems(leaf);
413 continue;
11833d66 414 }
817d52f8 415
11833d66
YZ
416 if (key.objectid < block_group->key.objectid) {
417 path->slots[0]++;
817d52f8 418 continue;
e37c9e69 419 }
0f9dd46c 420
e37c9e69 421 if (key.objectid >= block_group->key.objectid +
0f9dd46c 422 block_group->key.offset)
e37c9e69 423 break;
7d7d6068 424
11833d66 425 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
817d52f8
JB
426 total_found += add_new_free_space(block_group,
427 fs_info, last,
428 key.objectid);
7d7d6068 429 last = key.objectid + key.offset;
817d52f8 430
11833d66
YZ
431 if (total_found > (1024 * 1024 * 2)) {
432 total_found = 0;
433 wake_up(&caching_ctl->wait);
434 }
817d52f8 435 }
e37c9e69
CM
436 path->slots[0]++;
437 }
817d52f8 438 ret = 0;
e37c9e69 439
817d52f8
JB
440 total_found += add_new_free_space(block_group, fs_info, last,
441 block_group->key.objectid +
442 block_group->key.offset);
11833d66 443 caching_ctl->progress = (u64)-1;
817d52f8
JB
444
445 spin_lock(&block_group->lock);
11833d66 446 block_group->caching_ctl = NULL;
817d52f8
JB
447 block_group->cached = BTRFS_CACHE_FINISHED;
448 spin_unlock(&block_group->lock);
0f9dd46c 449
54aa1f4d 450err:
e37c9e69 451 btrfs_free_path(path);
276e680d 452 up_read(&fs_info->extent_commit_sem);
817d52f8 453
11833d66
YZ
454 free_excluded_extents(extent_root, block_group);
455
456 mutex_unlock(&caching_ctl->mutex);
bab39bf9 457out:
11833d66
YZ
458 wake_up(&caching_ctl->wait);
459
460 put_caching_control(caching_ctl);
11dfe35a 461 btrfs_put_block_group(block_group);
817d52f8
JB
462}
463
9d66e233
JB
464static int cache_block_group(struct btrfs_block_group_cache *cache,
465 struct btrfs_trans_handle *trans,
b8399dee 466 struct btrfs_root *root,
9d66e233 467 int load_cache_only)
817d52f8 468{
11833d66
YZ
469 struct btrfs_fs_info *fs_info = cache->fs_info;
470 struct btrfs_caching_control *caching_ctl;
817d52f8
JB
471 int ret = 0;
472
11833d66
YZ
473 smp_mb();
474 if (cache->cached != BTRFS_CACHE_NO)
475 return 0;
476
9d66e233
JB
477 /*
478 * We can't do the read from on-disk cache during a commit since we need
b8399dee
JB
479 * to have the normal tree locking. Also if we are currently trying to
480 * allocate blocks for the tree root we can't do the fast caching since
481 * we likely hold important locks.
9d66e233 482 */
f7039b1d 483 if (trans && (!trans->transaction->in_commit) &&
b8399dee 484 (root && root != root->fs_info->tree_root)) {
9d66e233
JB
485 spin_lock(&cache->lock);
486 if (cache->cached != BTRFS_CACHE_NO) {
487 spin_unlock(&cache->lock);
488 return 0;
489 }
490 cache->cached = BTRFS_CACHE_STARTED;
491 spin_unlock(&cache->lock);
492
493 ret = load_free_space_cache(fs_info, cache);
494
495 spin_lock(&cache->lock);
496 if (ret == 1) {
497 cache->cached = BTRFS_CACHE_FINISHED;
498 cache->last_byte_to_unpin = (u64)-1;
499 } else {
500 cache->cached = BTRFS_CACHE_NO;
501 }
502 spin_unlock(&cache->lock);
3c14874a
JB
503 if (ret == 1) {
504 free_excluded_extents(fs_info->extent_root, cache);
9d66e233 505 return 0;
3c14874a 506 }
9d66e233
JB
507 }
508
509 if (load_cache_only)
510 return 0;
511
fc0e4a31 512 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
11833d66
YZ
513 BUG_ON(!caching_ctl);
514
515 INIT_LIST_HEAD(&caching_ctl->list);
516 mutex_init(&caching_ctl->mutex);
517 init_waitqueue_head(&caching_ctl->wait);
518 caching_ctl->block_group = cache;
519 caching_ctl->progress = cache->key.objectid;
520 /* one for caching kthread, one for caching block group list */
521 atomic_set(&caching_ctl->count, 2);
bab39bf9 522 caching_ctl->work.func = caching_thread;
11833d66 523
817d52f8
JB
524 spin_lock(&cache->lock);
525 if (cache->cached != BTRFS_CACHE_NO) {
526 spin_unlock(&cache->lock);
11833d66
YZ
527 kfree(caching_ctl);
528 return 0;
817d52f8 529 }
11833d66 530 cache->caching_ctl = caching_ctl;
817d52f8
JB
531 cache->cached = BTRFS_CACHE_STARTED;
532 spin_unlock(&cache->lock);
533
11833d66
YZ
534 down_write(&fs_info->extent_commit_sem);
535 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
536 up_write(&fs_info->extent_commit_sem);
537
11dfe35a 538 btrfs_get_block_group(cache);
11833d66 539
bab39bf9 540 btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work);
817d52f8 541
ef8bbdfe 542 return ret;
e37c9e69
CM
543}
544
0f9dd46c
JB
545/*
546 * return the block group that starts at or after bytenr
547 */
d397712b
CM
548static struct btrfs_block_group_cache *
549btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
0ef3e66b 550{
0f9dd46c 551 struct btrfs_block_group_cache *cache;
0ef3e66b 552
0f9dd46c 553 cache = block_group_cache_tree_search(info, bytenr, 0);
0ef3e66b 554
0f9dd46c 555 return cache;
0ef3e66b
CM
556}
557
0f9dd46c 558/*
9f55684c 559 * return the block group that contains the given bytenr
0f9dd46c 560 */
d397712b
CM
561struct btrfs_block_group_cache *btrfs_lookup_block_group(
562 struct btrfs_fs_info *info,
563 u64 bytenr)
be744175 564{
0f9dd46c 565 struct btrfs_block_group_cache *cache;
be744175 566
0f9dd46c 567 cache = block_group_cache_tree_search(info, bytenr, 1);
96b5179d 568
0f9dd46c 569 return cache;
be744175 570}
0b86a832 571
0f9dd46c
JB
572static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
573 u64 flags)
6324fbf3 574{
0f9dd46c 575 struct list_head *head = &info->space_info;
0f9dd46c 576 struct btrfs_space_info *found;
4184ea7f 577
b742bb82
YZ
578 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
579 BTRFS_BLOCK_GROUP_METADATA;
580
4184ea7f
CM
581 rcu_read_lock();
582 list_for_each_entry_rcu(found, head, list) {
67377734 583 if (found->flags & flags) {
4184ea7f 584 rcu_read_unlock();
0f9dd46c 585 return found;
4184ea7f 586 }
0f9dd46c 587 }
4184ea7f 588 rcu_read_unlock();
0f9dd46c 589 return NULL;
6324fbf3
CM
590}
591
4184ea7f
CM
592/*
593 * after adding space to the filesystem, we need to clear the full flags
594 * on all the space infos.
595 */
596void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
597{
598 struct list_head *head = &info->space_info;
599 struct btrfs_space_info *found;
600
601 rcu_read_lock();
602 list_for_each_entry_rcu(found, head, list)
603 found->full = 0;
604 rcu_read_unlock();
605}
606
80eb234a
JB
607static u64 div_factor(u64 num, int factor)
608{
609 if (factor == 10)
610 return num;
611 num *= factor;
612 do_div(num, 10);
613 return num;
614}
615
e5bc2458
CM
616static u64 div_factor_fine(u64 num, int factor)
617{
618 if (factor == 100)
619 return num;
620 num *= factor;
621 do_div(num, 100);
622 return num;
623}
624
d2fb3437
YZ
625u64 btrfs_find_block_group(struct btrfs_root *root,
626 u64 search_start, u64 search_hint, int owner)
cd1bc465 627{
96b5179d 628 struct btrfs_block_group_cache *cache;
cd1bc465 629 u64 used;
d2fb3437
YZ
630 u64 last = max(search_hint, search_start);
631 u64 group_start = 0;
31f3c99b 632 int full_search = 0;
d2fb3437 633 int factor = 9;
0ef3e66b 634 int wrapped = 0;
31f3c99b 635again:
e8569813
ZY
636 while (1) {
637 cache = btrfs_lookup_first_block_group(root->fs_info, last);
0f9dd46c
JB
638 if (!cache)
639 break;
96b5179d 640
c286ac48 641 spin_lock(&cache->lock);
96b5179d
CM
642 last = cache->key.objectid + cache->key.offset;
643 used = btrfs_block_group_used(&cache->item);
644
d2fb3437
YZ
645 if ((full_search || !cache->ro) &&
646 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
e8569813 647 if (used + cache->pinned + cache->reserved <
d2fb3437
YZ
648 div_factor(cache->key.offset, factor)) {
649 group_start = cache->key.objectid;
c286ac48 650 spin_unlock(&cache->lock);
fa9c0d79 651 btrfs_put_block_group(cache);
8790d502
CM
652 goto found;
653 }
6324fbf3 654 }
c286ac48 655 spin_unlock(&cache->lock);
fa9c0d79 656 btrfs_put_block_group(cache);
de428b63 657 cond_resched();
cd1bc465 658 }
0ef3e66b
CM
659 if (!wrapped) {
660 last = search_start;
661 wrapped = 1;
662 goto again;
663 }
664 if (!full_search && factor < 10) {
be744175 665 last = search_start;
31f3c99b 666 full_search = 1;
0ef3e66b 667 factor = 10;
31f3c99b
CM
668 goto again;
669 }
be744175 670found:
d2fb3437 671 return group_start;
925baedd 672}
0f9dd46c 673
e02119d5 674/* simple helper to search for an existing extent at a given offset */
31840ae1 675int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
e02119d5
CM
676{
677 int ret;
678 struct btrfs_key key;
31840ae1 679 struct btrfs_path *path;
e02119d5 680
31840ae1 681 path = btrfs_alloc_path();
d8926bb3
MF
682 if (!path)
683 return -ENOMEM;
684
e02119d5
CM
685 key.objectid = start;
686 key.offset = len;
687 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
688 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
689 0, 0);
31840ae1 690 btrfs_free_path(path);
7bb86316
CM
691 return ret;
692}
693
a22285a6
YZ
694/*
695 * helper function to lookup reference count and flags of extent.
696 *
697 * the head node for delayed ref is used to store the sum of all the
698 * reference count modifications queued up in the rbtree. the head
699 * node may also store the extent flags to set. This way you can check
700 * to see what the reference count and extent flags would be if all of
701 * the delayed refs are not processed.
702 */
703int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
704 struct btrfs_root *root, u64 bytenr,
705 u64 num_bytes, u64 *refs, u64 *flags)
706{
707 struct btrfs_delayed_ref_head *head;
708 struct btrfs_delayed_ref_root *delayed_refs;
709 struct btrfs_path *path;
710 struct btrfs_extent_item *ei;
711 struct extent_buffer *leaf;
712 struct btrfs_key key;
713 u32 item_size;
714 u64 num_refs;
715 u64 extent_flags;
716 int ret;
717
718 path = btrfs_alloc_path();
719 if (!path)
720 return -ENOMEM;
721
722 key.objectid = bytenr;
723 key.type = BTRFS_EXTENT_ITEM_KEY;
724 key.offset = num_bytes;
725 if (!trans) {
726 path->skip_locking = 1;
727 path->search_commit_root = 1;
728 }
729again:
730 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
731 &key, path, 0, 0);
732 if (ret < 0)
733 goto out_free;
734
735 if (ret == 0) {
736 leaf = path->nodes[0];
737 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
738 if (item_size >= sizeof(*ei)) {
739 ei = btrfs_item_ptr(leaf, path->slots[0],
740 struct btrfs_extent_item);
741 num_refs = btrfs_extent_refs(leaf, ei);
742 extent_flags = btrfs_extent_flags(leaf, ei);
743 } else {
744#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
745 struct btrfs_extent_item_v0 *ei0;
746 BUG_ON(item_size != sizeof(*ei0));
747 ei0 = btrfs_item_ptr(leaf, path->slots[0],
748 struct btrfs_extent_item_v0);
749 num_refs = btrfs_extent_refs_v0(leaf, ei0);
750 /* FIXME: this isn't correct for data */
751 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
752#else
753 BUG();
754#endif
755 }
756 BUG_ON(num_refs == 0);
757 } else {
758 num_refs = 0;
759 extent_flags = 0;
760 ret = 0;
761 }
762
763 if (!trans)
764 goto out;
765
766 delayed_refs = &trans->transaction->delayed_refs;
767 spin_lock(&delayed_refs->lock);
768 head = btrfs_find_delayed_ref_head(trans, bytenr);
769 if (head) {
770 if (!mutex_trylock(&head->mutex)) {
771 atomic_inc(&head->node.refs);
772 spin_unlock(&delayed_refs->lock);
773
b3b4aa74 774 btrfs_release_path(path);
a22285a6 775
8cc33e5c
DS
776 /*
777 * Mutex was contended, block until it's released and try
778 * again
779 */
a22285a6
YZ
780 mutex_lock(&head->mutex);
781 mutex_unlock(&head->mutex);
782 btrfs_put_delayed_ref(&head->node);
783 goto again;
784 }
785 if (head->extent_op && head->extent_op->update_flags)
786 extent_flags |= head->extent_op->flags_to_set;
787 else
788 BUG_ON(num_refs == 0);
789
790 num_refs += head->node.ref_mod;
791 mutex_unlock(&head->mutex);
792 }
793 spin_unlock(&delayed_refs->lock);
794out:
795 WARN_ON(num_refs == 0);
796 if (refs)
797 *refs = num_refs;
798 if (flags)
799 *flags = extent_flags;
800out_free:
801 btrfs_free_path(path);
802 return ret;
803}
804
d8d5f3e1
CM
805/*
806 * Back reference rules. Back refs have three main goals:
807 *
808 * 1) differentiate between all holders of references to an extent so that
809 * when a reference is dropped we can make sure it was a valid reference
810 * before freeing the extent.
811 *
812 * 2) Provide enough information to quickly find the holders of an extent
813 * if we notice a given block is corrupted or bad.
814 *
815 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
816 * maintenance. This is actually the same as #2, but with a slightly
817 * different use case.
818 *
5d4f98a2
YZ
819 * There are two kinds of back refs. The implicit back refs is optimized
820 * for pointers in non-shared tree blocks. For a given pointer in a block,
821 * back refs of this kind provide information about the block's owner tree
822 * and the pointer's key. These information allow us to find the block by
823 * b-tree searching. The full back refs is for pointers in tree blocks not
824 * referenced by their owner trees. The location of tree block is recorded
825 * in the back refs. Actually the full back refs is generic, and can be
826 * used in all cases the implicit back refs is used. The major shortcoming
827 * of the full back refs is its overhead. Every time a tree block gets
828 * COWed, we have to update back refs entry for all pointers in it.
829 *
830 * For a newly allocated tree block, we use implicit back refs for
831 * pointers in it. This means most tree related operations only involve
832 * implicit back refs. For a tree block created in old transaction, the
833 * only way to drop a reference to it is COW it. So we can detect the
834 * event that tree block loses its owner tree's reference and do the
835 * back refs conversion.
836 *
837 * When a tree block is COW'd through a tree, there are four cases:
838 *
839 * The reference count of the block is one and the tree is the block's
840 * owner tree. Nothing to do in this case.
841 *
842 * The reference count of the block is one and the tree is not the
843 * block's owner tree. In this case, full back refs is used for pointers
844 * in the block. Remove these full back refs, add implicit back refs for
845 * every pointers in the new block.
846 *
847 * The reference count of the block is greater than one and the tree is
848 * the block's owner tree. In this case, implicit back refs is used for
849 * pointers in the block. Add full back refs for every pointers in the
850 * block, increase lower level extents' reference counts. The original
851 * implicit back refs are entailed to the new block.
852 *
853 * The reference count of the block is greater than one and the tree is
854 * not the block's owner tree. Add implicit back refs for every pointer in
855 * the new block, increase lower level extents' reference count.
856 *
857 * Back Reference Key composing:
858 *
859 * The key objectid corresponds to the first byte in the extent,
860 * The key type is used to differentiate between types of back refs.
861 * There are different meanings of the key offset for different types
862 * of back refs.
863 *
d8d5f3e1
CM
864 * File extents can be referenced by:
865 *
866 * - multiple snapshots, subvolumes, or different generations in one subvol
31840ae1 867 * - different files inside a single subvolume
d8d5f3e1
CM
868 * - different offsets inside a file (bookend extents in file.c)
869 *
5d4f98a2 870 * The extent ref structure for the implicit back refs has fields for:
d8d5f3e1
CM
871 *
872 * - Objectid of the subvolume root
d8d5f3e1 873 * - objectid of the file holding the reference
5d4f98a2
YZ
874 * - original offset in the file
875 * - how many bookend extents
d8d5f3e1 876 *
5d4f98a2
YZ
877 * The key offset for the implicit back refs is hash of the first
878 * three fields.
d8d5f3e1 879 *
5d4f98a2 880 * The extent ref structure for the full back refs has field for:
d8d5f3e1 881 *
5d4f98a2 882 * - number of pointers in the tree leaf
d8d5f3e1 883 *
5d4f98a2
YZ
884 * The key offset for the implicit back refs is the first byte of
885 * the tree leaf
d8d5f3e1 886 *
5d4f98a2
YZ
887 * When a file extent is allocated, The implicit back refs is used.
888 * the fields are filled in:
d8d5f3e1 889 *
5d4f98a2 890 * (root_key.objectid, inode objectid, offset in file, 1)
d8d5f3e1 891 *
5d4f98a2
YZ
892 * When a file extent is removed file truncation, we find the
893 * corresponding implicit back refs and check the following fields:
d8d5f3e1 894 *
5d4f98a2 895 * (btrfs_header_owner(leaf), inode objectid, offset in file)
d8d5f3e1 896 *
5d4f98a2 897 * Btree extents can be referenced by:
d8d5f3e1 898 *
5d4f98a2 899 * - Different subvolumes
d8d5f3e1 900 *
5d4f98a2
YZ
901 * Both the implicit back refs and the full back refs for tree blocks
902 * only consist of key. The key offset for the implicit back refs is
903 * objectid of block's owner tree. The key offset for the full back refs
904 * is the first byte of parent block.
d8d5f3e1 905 *
5d4f98a2
YZ
906 * When implicit back refs is used, information about the lowest key and
907 * level of the tree block are required. These information are stored in
908 * tree block info structure.
d8d5f3e1 909 */
31840ae1 910
5d4f98a2
YZ
911#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
912static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
913 struct btrfs_root *root,
914 struct btrfs_path *path,
915 u64 owner, u32 extra_size)
7bb86316 916{
5d4f98a2
YZ
917 struct btrfs_extent_item *item;
918 struct btrfs_extent_item_v0 *ei0;
919 struct btrfs_extent_ref_v0 *ref0;
920 struct btrfs_tree_block_info *bi;
921 struct extent_buffer *leaf;
7bb86316 922 struct btrfs_key key;
5d4f98a2
YZ
923 struct btrfs_key found_key;
924 u32 new_size = sizeof(*item);
925 u64 refs;
926 int ret;
927
928 leaf = path->nodes[0];
929 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
930
931 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
932 ei0 = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_extent_item_v0);
934 refs = btrfs_extent_refs_v0(leaf, ei0);
935
936 if (owner == (u64)-1) {
937 while (1) {
938 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
939 ret = btrfs_next_leaf(root, path);
940 if (ret < 0)
941 return ret;
942 BUG_ON(ret > 0);
943 leaf = path->nodes[0];
944 }
945 btrfs_item_key_to_cpu(leaf, &found_key,
946 path->slots[0]);
947 BUG_ON(key.objectid != found_key.objectid);
948 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
949 path->slots[0]++;
950 continue;
951 }
952 ref0 = btrfs_item_ptr(leaf, path->slots[0],
953 struct btrfs_extent_ref_v0);
954 owner = btrfs_ref_objectid_v0(leaf, ref0);
955 break;
956 }
957 }
b3b4aa74 958 btrfs_release_path(path);
5d4f98a2
YZ
959
960 if (owner < BTRFS_FIRST_FREE_OBJECTID)
961 new_size += sizeof(*bi);
962
963 new_size -= sizeof(*ei0);
964 ret = btrfs_search_slot(trans, root, &key, path,
965 new_size + extra_size, 1);
966 if (ret < 0)
967 return ret;
968 BUG_ON(ret);
969
970 ret = btrfs_extend_item(trans, root, path, new_size);
5d4f98a2
YZ
971
972 leaf = path->nodes[0];
973 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
974 btrfs_set_extent_refs(leaf, item, refs);
975 /* FIXME: get real generation */
976 btrfs_set_extent_generation(leaf, item, 0);
977 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
978 btrfs_set_extent_flags(leaf, item,
979 BTRFS_EXTENT_FLAG_TREE_BLOCK |
980 BTRFS_BLOCK_FLAG_FULL_BACKREF);
981 bi = (struct btrfs_tree_block_info *)(item + 1);
982 /* FIXME: get first key of the block */
983 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
984 btrfs_set_tree_block_level(leaf, bi, (int)owner);
985 } else {
986 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
987 }
988 btrfs_mark_buffer_dirty(leaf);
989 return 0;
990}
991#endif
992
993static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
994{
995 u32 high_crc = ~(u32)0;
996 u32 low_crc = ~(u32)0;
997 __le64 lenum;
998
999 lenum = cpu_to_le64(root_objectid);
163e783e 1000 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
5d4f98a2 1001 lenum = cpu_to_le64(owner);
163e783e 1002 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
5d4f98a2 1003 lenum = cpu_to_le64(offset);
163e783e 1004 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
5d4f98a2
YZ
1005
1006 return ((u64)high_crc << 31) ^ (u64)low_crc;
1007}
1008
1009static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1010 struct btrfs_extent_data_ref *ref)
1011{
1012 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1013 btrfs_extent_data_ref_objectid(leaf, ref),
1014 btrfs_extent_data_ref_offset(leaf, ref));
1015}
1016
1017static int match_extent_data_ref(struct extent_buffer *leaf,
1018 struct btrfs_extent_data_ref *ref,
1019 u64 root_objectid, u64 owner, u64 offset)
1020{
1021 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1022 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1023 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1024 return 0;
1025 return 1;
1026}
1027
1028static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1029 struct btrfs_root *root,
1030 struct btrfs_path *path,
1031 u64 bytenr, u64 parent,
1032 u64 root_objectid,
1033 u64 owner, u64 offset)
1034{
1035 struct btrfs_key key;
1036 struct btrfs_extent_data_ref *ref;
31840ae1 1037 struct extent_buffer *leaf;
5d4f98a2 1038 u32 nritems;
74493f7a 1039 int ret;
5d4f98a2
YZ
1040 int recow;
1041 int err = -ENOENT;
74493f7a 1042
31840ae1 1043 key.objectid = bytenr;
5d4f98a2
YZ
1044 if (parent) {
1045 key.type = BTRFS_SHARED_DATA_REF_KEY;
1046 key.offset = parent;
1047 } else {
1048 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1049 key.offset = hash_extent_data_ref(root_objectid,
1050 owner, offset);
1051 }
1052again:
1053 recow = 0;
1054 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1055 if (ret < 0) {
1056 err = ret;
1057 goto fail;
1058 }
31840ae1 1059
5d4f98a2
YZ
1060 if (parent) {
1061 if (!ret)
1062 return 0;
1063#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1064 key.type = BTRFS_EXTENT_REF_V0_KEY;
b3b4aa74 1065 btrfs_release_path(path);
5d4f98a2
YZ
1066 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1067 if (ret < 0) {
1068 err = ret;
1069 goto fail;
1070 }
1071 if (!ret)
1072 return 0;
1073#endif
1074 goto fail;
31840ae1
ZY
1075 }
1076
1077 leaf = path->nodes[0];
5d4f98a2
YZ
1078 nritems = btrfs_header_nritems(leaf);
1079 while (1) {
1080 if (path->slots[0] >= nritems) {
1081 ret = btrfs_next_leaf(root, path);
1082 if (ret < 0)
1083 err = ret;
1084 if (ret)
1085 goto fail;
1086
1087 leaf = path->nodes[0];
1088 nritems = btrfs_header_nritems(leaf);
1089 recow = 1;
1090 }
1091
1092 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1093 if (key.objectid != bytenr ||
1094 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1095 goto fail;
1096
1097 ref = btrfs_item_ptr(leaf, path->slots[0],
1098 struct btrfs_extent_data_ref);
1099
1100 if (match_extent_data_ref(leaf, ref, root_objectid,
1101 owner, offset)) {
1102 if (recow) {
b3b4aa74 1103 btrfs_release_path(path);
5d4f98a2
YZ
1104 goto again;
1105 }
1106 err = 0;
1107 break;
1108 }
1109 path->slots[0]++;
31840ae1 1110 }
5d4f98a2
YZ
1111fail:
1112 return err;
31840ae1
ZY
1113}
1114
5d4f98a2
YZ
1115static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1116 struct btrfs_root *root,
1117 struct btrfs_path *path,
1118 u64 bytenr, u64 parent,
1119 u64 root_objectid, u64 owner,
1120 u64 offset, int refs_to_add)
31840ae1
ZY
1121{
1122 struct btrfs_key key;
1123 struct extent_buffer *leaf;
5d4f98a2 1124 u32 size;
31840ae1
ZY
1125 u32 num_refs;
1126 int ret;
74493f7a 1127
74493f7a 1128 key.objectid = bytenr;
5d4f98a2
YZ
1129 if (parent) {
1130 key.type = BTRFS_SHARED_DATA_REF_KEY;
1131 key.offset = parent;
1132 size = sizeof(struct btrfs_shared_data_ref);
1133 } else {
1134 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1135 key.offset = hash_extent_data_ref(root_objectid,
1136 owner, offset);
1137 size = sizeof(struct btrfs_extent_data_ref);
1138 }
74493f7a 1139
5d4f98a2
YZ
1140 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1141 if (ret && ret != -EEXIST)
1142 goto fail;
1143
1144 leaf = path->nodes[0];
1145 if (parent) {
1146 struct btrfs_shared_data_ref *ref;
31840ae1 1147 ref = btrfs_item_ptr(leaf, path->slots[0],
5d4f98a2
YZ
1148 struct btrfs_shared_data_ref);
1149 if (ret == 0) {
1150 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1151 } else {
1152 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1153 num_refs += refs_to_add;
1154 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
31840ae1 1155 }
5d4f98a2
YZ
1156 } else {
1157 struct btrfs_extent_data_ref *ref;
1158 while (ret == -EEXIST) {
1159 ref = btrfs_item_ptr(leaf, path->slots[0],
1160 struct btrfs_extent_data_ref);
1161 if (match_extent_data_ref(leaf, ref, root_objectid,
1162 owner, offset))
1163 break;
b3b4aa74 1164 btrfs_release_path(path);
5d4f98a2
YZ
1165 key.offset++;
1166 ret = btrfs_insert_empty_item(trans, root, path, &key,
1167 size);
1168 if (ret && ret != -EEXIST)
1169 goto fail;
31840ae1 1170
5d4f98a2
YZ
1171 leaf = path->nodes[0];
1172 }
1173 ref = btrfs_item_ptr(leaf, path->slots[0],
1174 struct btrfs_extent_data_ref);
1175 if (ret == 0) {
1176 btrfs_set_extent_data_ref_root(leaf, ref,
1177 root_objectid);
1178 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1179 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1180 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1181 } else {
1182 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1183 num_refs += refs_to_add;
1184 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
31840ae1 1185 }
31840ae1 1186 }
5d4f98a2
YZ
1187 btrfs_mark_buffer_dirty(leaf);
1188 ret = 0;
1189fail:
b3b4aa74 1190 btrfs_release_path(path);
7bb86316 1191 return ret;
74493f7a
CM
1192}
1193
5d4f98a2
YZ
1194static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root,
1196 struct btrfs_path *path,
1197 int refs_to_drop)
31840ae1 1198{
5d4f98a2
YZ
1199 struct btrfs_key key;
1200 struct btrfs_extent_data_ref *ref1 = NULL;
1201 struct btrfs_shared_data_ref *ref2 = NULL;
31840ae1 1202 struct extent_buffer *leaf;
5d4f98a2 1203 u32 num_refs = 0;
31840ae1
ZY
1204 int ret = 0;
1205
1206 leaf = path->nodes[0];
5d4f98a2
YZ
1207 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1208
1209 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1210 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1211 struct btrfs_extent_data_ref);
1212 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1213 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1214 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1215 struct btrfs_shared_data_ref);
1216 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1217#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1218 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1219 struct btrfs_extent_ref_v0 *ref0;
1220 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1221 struct btrfs_extent_ref_v0);
1222 num_refs = btrfs_ref_count_v0(leaf, ref0);
1223#endif
1224 } else {
1225 BUG();
1226 }
1227
56bec294
CM
1228 BUG_ON(num_refs < refs_to_drop);
1229 num_refs -= refs_to_drop;
5d4f98a2 1230
31840ae1
ZY
1231 if (num_refs == 0) {
1232 ret = btrfs_del_item(trans, root, path);
1233 } else {
5d4f98a2
YZ
1234 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1235 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1236 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1237 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1238#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1239 else {
1240 struct btrfs_extent_ref_v0 *ref0;
1241 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1242 struct btrfs_extent_ref_v0);
1243 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1244 }
1245#endif
31840ae1
ZY
1246 btrfs_mark_buffer_dirty(leaf);
1247 }
31840ae1
ZY
1248 return ret;
1249}
1250
5d4f98a2
YZ
1251static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1252 struct btrfs_path *path,
1253 struct btrfs_extent_inline_ref *iref)
15916de8 1254{
5d4f98a2
YZ
1255 struct btrfs_key key;
1256 struct extent_buffer *leaf;
1257 struct btrfs_extent_data_ref *ref1;
1258 struct btrfs_shared_data_ref *ref2;
1259 u32 num_refs = 0;
1260
1261 leaf = path->nodes[0];
1262 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1263 if (iref) {
1264 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1265 BTRFS_EXTENT_DATA_REF_KEY) {
1266 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1267 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1268 } else {
1269 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1270 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1271 }
1272 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1273 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1274 struct btrfs_extent_data_ref);
1275 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1276 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1277 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1278 struct btrfs_shared_data_ref);
1279 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1280#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1281 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1282 struct btrfs_extent_ref_v0 *ref0;
1283 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1284 struct btrfs_extent_ref_v0);
1285 num_refs = btrfs_ref_count_v0(leaf, ref0);
4b4e25f2 1286#endif
5d4f98a2
YZ
1287 } else {
1288 WARN_ON(1);
1289 }
1290 return num_refs;
1291}
15916de8 1292
5d4f98a2
YZ
1293static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1294 struct btrfs_root *root,
1295 struct btrfs_path *path,
1296 u64 bytenr, u64 parent,
1297 u64 root_objectid)
1f3c79a2 1298{
5d4f98a2 1299 struct btrfs_key key;
1f3c79a2 1300 int ret;
1f3c79a2 1301
5d4f98a2
YZ
1302 key.objectid = bytenr;
1303 if (parent) {
1304 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1305 key.offset = parent;
1306 } else {
1307 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1308 key.offset = root_objectid;
1f3c79a2
LH
1309 }
1310
5d4f98a2
YZ
1311 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1312 if (ret > 0)
1313 ret = -ENOENT;
1314#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1315 if (ret == -ENOENT && parent) {
b3b4aa74 1316 btrfs_release_path(path);
5d4f98a2
YZ
1317 key.type = BTRFS_EXTENT_REF_V0_KEY;
1318 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1319 if (ret > 0)
1320 ret = -ENOENT;
1321 }
1f3c79a2 1322#endif
5d4f98a2 1323 return ret;
1f3c79a2
LH
1324}
1325
5d4f98a2
YZ
1326static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1328 struct btrfs_path *path,
1329 u64 bytenr, u64 parent,
1330 u64 root_objectid)
31840ae1 1331{
5d4f98a2 1332 struct btrfs_key key;
31840ae1 1333 int ret;
31840ae1 1334
5d4f98a2
YZ
1335 key.objectid = bytenr;
1336 if (parent) {
1337 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1338 key.offset = parent;
1339 } else {
1340 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1341 key.offset = root_objectid;
1342 }
1343
1344 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
b3b4aa74 1345 btrfs_release_path(path);
31840ae1
ZY
1346 return ret;
1347}
1348
5d4f98a2 1349static inline int extent_ref_type(u64 parent, u64 owner)
31840ae1 1350{
5d4f98a2
YZ
1351 int type;
1352 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1353 if (parent > 0)
1354 type = BTRFS_SHARED_BLOCK_REF_KEY;
1355 else
1356 type = BTRFS_TREE_BLOCK_REF_KEY;
1357 } else {
1358 if (parent > 0)
1359 type = BTRFS_SHARED_DATA_REF_KEY;
1360 else
1361 type = BTRFS_EXTENT_DATA_REF_KEY;
1362 }
1363 return type;
31840ae1 1364}
56bec294 1365
2c47e605
YZ
1366static int find_next_key(struct btrfs_path *path, int level,
1367 struct btrfs_key *key)
56bec294 1368
02217ed2 1369{
2c47e605 1370 for (; level < BTRFS_MAX_LEVEL; level++) {
5d4f98a2
YZ
1371 if (!path->nodes[level])
1372 break;
5d4f98a2
YZ
1373 if (path->slots[level] + 1 >=
1374 btrfs_header_nritems(path->nodes[level]))
1375 continue;
1376 if (level == 0)
1377 btrfs_item_key_to_cpu(path->nodes[level], key,
1378 path->slots[level] + 1);
1379 else
1380 btrfs_node_key_to_cpu(path->nodes[level], key,
1381 path->slots[level] + 1);
1382 return 0;
1383 }
1384 return 1;
1385}
037e6390 1386
5d4f98a2
YZ
1387/*
1388 * look for inline back ref. if back ref is found, *ref_ret is set
1389 * to the address of inline back ref, and 0 is returned.
1390 *
1391 * if back ref isn't found, *ref_ret is set to the address where it
1392 * should be inserted, and -ENOENT is returned.
1393 *
1394 * if insert is true and there are too many inline back refs, the path
1395 * points to the extent item, and -EAGAIN is returned.
1396 *
1397 * NOTE: inline back refs are ordered in the same way that back ref
1398 * items in the tree are ordered.
1399 */
1400static noinline_for_stack
1401int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 struct btrfs_path *path,
1404 struct btrfs_extent_inline_ref **ref_ret,
1405 u64 bytenr, u64 num_bytes,
1406 u64 parent, u64 root_objectid,
1407 u64 owner, u64 offset, int insert)
1408{
1409 struct btrfs_key key;
1410 struct extent_buffer *leaf;
1411 struct btrfs_extent_item *ei;
1412 struct btrfs_extent_inline_ref *iref;
1413 u64 flags;
1414 u64 item_size;
1415 unsigned long ptr;
1416 unsigned long end;
1417 int extra_size;
1418 int type;
1419 int want;
1420 int ret;
1421 int err = 0;
26b8003f 1422
db94535d 1423 key.objectid = bytenr;
31840ae1 1424 key.type = BTRFS_EXTENT_ITEM_KEY;
56bec294 1425 key.offset = num_bytes;
31840ae1 1426
5d4f98a2
YZ
1427 want = extent_ref_type(parent, owner);
1428 if (insert) {
1429 extra_size = btrfs_extent_inline_ref_size(want);
85d4198e 1430 path->keep_locks = 1;
5d4f98a2
YZ
1431 } else
1432 extra_size = -1;
1433 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
b9473439 1434 if (ret < 0) {
5d4f98a2
YZ
1435 err = ret;
1436 goto out;
1437 }
1438 BUG_ON(ret);
1439
1440 leaf = path->nodes[0];
1441 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1442#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1443 if (item_size < sizeof(*ei)) {
1444 if (!insert) {
1445 err = -ENOENT;
1446 goto out;
1447 }
1448 ret = convert_extent_item_v0(trans, root, path, owner,
1449 extra_size);
1450 if (ret < 0) {
1451 err = ret;
1452 goto out;
1453 }
1454 leaf = path->nodes[0];
1455 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1456 }
1457#endif
1458 BUG_ON(item_size < sizeof(*ei));
1459
5d4f98a2
YZ
1460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1461 flags = btrfs_extent_flags(leaf, ei);
1462
1463 ptr = (unsigned long)(ei + 1);
1464 end = (unsigned long)ei + item_size;
1465
1466 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1467 ptr += sizeof(struct btrfs_tree_block_info);
1468 BUG_ON(ptr > end);
1469 } else {
1470 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1471 }
1472
1473 err = -ENOENT;
1474 while (1) {
1475 if (ptr >= end) {
1476 WARN_ON(ptr > end);
1477 break;
1478 }
1479 iref = (struct btrfs_extent_inline_ref *)ptr;
1480 type = btrfs_extent_inline_ref_type(leaf, iref);
1481 if (want < type)
1482 break;
1483 if (want > type) {
1484 ptr += btrfs_extent_inline_ref_size(type);
1485 continue;
1486 }
1487
1488 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1489 struct btrfs_extent_data_ref *dref;
1490 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1491 if (match_extent_data_ref(leaf, dref, root_objectid,
1492 owner, offset)) {
1493 err = 0;
1494 break;
1495 }
1496 if (hash_extent_data_ref_item(leaf, dref) <
1497 hash_extent_data_ref(root_objectid, owner, offset))
1498 break;
1499 } else {
1500 u64 ref_offset;
1501 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1502 if (parent > 0) {
1503 if (parent == ref_offset) {
1504 err = 0;
1505 break;
1506 }
1507 if (ref_offset < parent)
1508 break;
1509 } else {
1510 if (root_objectid == ref_offset) {
1511 err = 0;
1512 break;
1513 }
1514 if (ref_offset < root_objectid)
1515 break;
1516 }
1517 }
1518 ptr += btrfs_extent_inline_ref_size(type);
1519 }
1520 if (err == -ENOENT && insert) {
1521 if (item_size + extra_size >=
1522 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1523 err = -EAGAIN;
1524 goto out;
1525 }
1526 /*
1527 * To add new inline back ref, we have to make sure
1528 * there is no corresponding back ref item.
1529 * For simplicity, we just do not add new inline back
1530 * ref if there is any kind of item for this block
1531 */
2c47e605
YZ
1532 if (find_next_key(path, 0, &key) == 0 &&
1533 key.objectid == bytenr &&
85d4198e 1534 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
5d4f98a2
YZ
1535 err = -EAGAIN;
1536 goto out;
1537 }
1538 }
1539 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1540out:
85d4198e 1541 if (insert) {
5d4f98a2
YZ
1542 path->keep_locks = 0;
1543 btrfs_unlock_up_safe(path, 1);
1544 }
1545 return err;
1546}
1547
1548/*
1549 * helper to add new inline back ref
1550 */
1551static noinline_for_stack
1552int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1553 struct btrfs_root *root,
1554 struct btrfs_path *path,
1555 struct btrfs_extent_inline_ref *iref,
1556 u64 parent, u64 root_objectid,
1557 u64 owner, u64 offset, int refs_to_add,
1558 struct btrfs_delayed_extent_op *extent_op)
1559{
1560 struct extent_buffer *leaf;
1561 struct btrfs_extent_item *ei;
1562 unsigned long ptr;
1563 unsigned long end;
1564 unsigned long item_offset;
1565 u64 refs;
1566 int size;
1567 int type;
1568 int ret;
1569
1570 leaf = path->nodes[0];
1571 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1572 item_offset = (unsigned long)iref - (unsigned long)ei;
1573
1574 type = extent_ref_type(parent, owner);
1575 size = btrfs_extent_inline_ref_size(type);
1576
1577 ret = btrfs_extend_item(trans, root, path, size);
5d4f98a2
YZ
1578
1579 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1580 refs = btrfs_extent_refs(leaf, ei);
1581 refs += refs_to_add;
1582 btrfs_set_extent_refs(leaf, ei, refs);
1583 if (extent_op)
1584 __run_delayed_extent_op(extent_op, leaf, ei);
1585
1586 ptr = (unsigned long)ei + item_offset;
1587 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1588 if (ptr < end - size)
1589 memmove_extent_buffer(leaf, ptr + size, ptr,
1590 end - size - ptr);
1591
1592 iref = (struct btrfs_extent_inline_ref *)ptr;
1593 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1594 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1595 struct btrfs_extent_data_ref *dref;
1596 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1597 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1598 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1599 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1600 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1601 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1602 struct btrfs_shared_data_ref *sref;
1603 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1604 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1605 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1606 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1607 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1608 } else {
1609 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1610 }
1611 btrfs_mark_buffer_dirty(leaf);
1612 return 0;
1613}
1614
1615static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1616 struct btrfs_root *root,
1617 struct btrfs_path *path,
1618 struct btrfs_extent_inline_ref **ref_ret,
1619 u64 bytenr, u64 num_bytes, u64 parent,
1620 u64 root_objectid, u64 owner, u64 offset)
1621{
1622 int ret;
1623
1624 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1625 bytenr, num_bytes, parent,
1626 root_objectid, owner, offset, 0);
1627 if (ret != -ENOENT)
54aa1f4d 1628 return ret;
5d4f98a2 1629
b3b4aa74 1630 btrfs_release_path(path);
5d4f98a2
YZ
1631 *ref_ret = NULL;
1632
1633 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1634 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1635 root_objectid);
1636 } else {
1637 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1638 root_objectid, owner, offset);
b9473439 1639 }
5d4f98a2
YZ
1640 return ret;
1641}
31840ae1 1642
5d4f98a2
YZ
1643/*
1644 * helper to update/remove inline back ref
1645 */
1646static noinline_for_stack
1647int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1648 struct btrfs_root *root,
1649 struct btrfs_path *path,
1650 struct btrfs_extent_inline_ref *iref,
1651 int refs_to_mod,
1652 struct btrfs_delayed_extent_op *extent_op)
1653{
1654 struct extent_buffer *leaf;
1655 struct btrfs_extent_item *ei;
1656 struct btrfs_extent_data_ref *dref = NULL;
1657 struct btrfs_shared_data_ref *sref = NULL;
1658 unsigned long ptr;
1659 unsigned long end;
1660 u32 item_size;
1661 int size;
1662 int type;
1663 int ret;
1664 u64 refs;
1665
1666 leaf = path->nodes[0];
1667 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1668 refs = btrfs_extent_refs(leaf, ei);
1669 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1670 refs += refs_to_mod;
1671 btrfs_set_extent_refs(leaf, ei, refs);
1672 if (extent_op)
1673 __run_delayed_extent_op(extent_op, leaf, ei);
1674
1675 type = btrfs_extent_inline_ref_type(leaf, iref);
1676
1677 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1678 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1679 refs = btrfs_extent_data_ref_count(leaf, dref);
1680 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1681 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1682 refs = btrfs_shared_data_ref_count(leaf, sref);
1683 } else {
1684 refs = 1;
1685 BUG_ON(refs_to_mod != -1);
56bec294 1686 }
31840ae1 1687
5d4f98a2
YZ
1688 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1689 refs += refs_to_mod;
1690
1691 if (refs > 0) {
1692 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1693 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1694 else
1695 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1696 } else {
1697 size = btrfs_extent_inline_ref_size(type);
1698 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1699 ptr = (unsigned long)iref;
1700 end = (unsigned long)ei + item_size;
1701 if (ptr + size < end)
1702 memmove_extent_buffer(leaf, ptr, ptr + size,
1703 end - ptr - size);
1704 item_size -= size;
1705 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
5d4f98a2
YZ
1706 }
1707 btrfs_mark_buffer_dirty(leaf);
1708 return 0;
1709}
1710
1711static noinline_for_stack
1712int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1713 struct btrfs_root *root,
1714 struct btrfs_path *path,
1715 u64 bytenr, u64 num_bytes, u64 parent,
1716 u64 root_objectid, u64 owner,
1717 u64 offset, int refs_to_add,
1718 struct btrfs_delayed_extent_op *extent_op)
1719{
1720 struct btrfs_extent_inline_ref *iref;
1721 int ret;
1722
1723 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1724 bytenr, num_bytes, parent,
1725 root_objectid, owner, offset, 1);
1726 if (ret == 0) {
1727 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1728 ret = update_inline_extent_backref(trans, root, path, iref,
1729 refs_to_add, extent_op);
1730 } else if (ret == -ENOENT) {
1731 ret = setup_inline_extent_backref(trans, root, path, iref,
1732 parent, root_objectid,
1733 owner, offset, refs_to_add,
1734 extent_op);
771ed689 1735 }
5d4f98a2
YZ
1736 return ret;
1737}
31840ae1 1738
5d4f98a2
YZ
1739static int insert_extent_backref(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root,
1741 struct btrfs_path *path,
1742 u64 bytenr, u64 parent, u64 root_objectid,
1743 u64 owner, u64 offset, int refs_to_add)
1744{
1745 int ret;
1746 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1747 BUG_ON(refs_to_add != 1);
1748 ret = insert_tree_block_ref(trans, root, path, bytenr,
1749 parent, root_objectid);
1750 } else {
1751 ret = insert_extent_data_ref(trans, root, path, bytenr,
1752 parent, root_objectid,
1753 owner, offset, refs_to_add);
1754 }
1755 return ret;
1756}
56bec294 1757
5d4f98a2
YZ
1758static int remove_extent_backref(struct btrfs_trans_handle *trans,
1759 struct btrfs_root *root,
1760 struct btrfs_path *path,
1761 struct btrfs_extent_inline_ref *iref,
1762 int refs_to_drop, int is_data)
1763{
1764 int ret;
b9473439 1765
5d4f98a2
YZ
1766 BUG_ON(!is_data && refs_to_drop != 1);
1767 if (iref) {
1768 ret = update_inline_extent_backref(trans, root, path, iref,
1769 -refs_to_drop, NULL);
1770 } else if (is_data) {
1771 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1772 } else {
1773 ret = btrfs_del_item(trans, root, path);
1774 }
1775 return ret;
1776}
1777
5378e607 1778static int btrfs_issue_discard(struct block_device *bdev,
5d4f98a2
YZ
1779 u64 start, u64 len)
1780{
5378e607 1781 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
5d4f98a2 1782}
5d4f98a2
YZ
1783
1784static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
5378e607 1785 u64 num_bytes, u64 *actual_bytes)
5d4f98a2 1786{
5d4f98a2 1787 int ret;
5378e607 1788 u64 discarded_bytes = 0;
5d4f98a2
YZ
1789 struct btrfs_multi_bio *multi = NULL;
1790
e244a0ae 1791
5d4f98a2 1792 /* Tell the block device(s) that the sectors can be discarded */
5378e607
LD
1793 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1794 bytenr, &num_bytes, &multi, 0);
5d4f98a2
YZ
1795 if (!ret) {
1796 struct btrfs_bio_stripe *stripe = multi->stripes;
1797 int i;
1798
5d4f98a2
YZ
1799
1800 for (i = 0; i < multi->num_stripes; i++, stripe++) {
d5e2003c
JB
1801 if (!stripe->dev->can_discard)
1802 continue;
1803
5378e607
LD
1804 ret = btrfs_issue_discard(stripe->dev->bdev,
1805 stripe->physical,
1806 stripe->length);
1807 if (!ret)
1808 discarded_bytes += stripe->length;
1809 else if (ret != -EOPNOTSUPP)
1810 break;
d5e2003c
JB
1811
1812 /*
1813 * Just in case we get back EOPNOTSUPP for some reason,
1814 * just ignore the return value so we don't screw up
1815 * people calling discard_extent.
1816 */
1817 ret = 0;
5d4f98a2
YZ
1818 }
1819 kfree(multi);
1820 }
5378e607
LD
1821
1822 if (actual_bytes)
1823 *actual_bytes = discarded_bytes;
1824
5d4f98a2
YZ
1825
1826 return ret;
5d4f98a2
YZ
1827}
1828
1829int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 u64 bytenr, u64 num_bytes, u64 parent,
1832 u64 root_objectid, u64 owner, u64 offset)
1833{
1834 int ret;
1835 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1836 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1837
1838 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1839 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1840 parent, root_objectid, (int)owner,
1841 BTRFS_ADD_DELAYED_REF, NULL);
1842 } else {
1843 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1844 parent, root_objectid, owner, offset,
1845 BTRFS_ADD_DELAYED_REF, NULL);
1846 }
1847 return ret;
1848}
1849
1850static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1851 struct btrfs_root *root,
1852 u64 bytenr, u64 num_bytes,
1853 u64 parent, u64 root_objectid,
1854 u64 owner, u64 offset, int refs_to_add,
1855 struct btrfs_delayed_extent_op *extent_op)
1856{
1857 struct btrfs_path *path;
1858 struct extent_buffer *leaf;
1859 struct btrfs_extent_item *item;
1860 u64 refs;
1861 int ret;
1862 int err = 0;
1863
1864 path = btrfs_alloc_path();
1865 if (!path)
1866 return -ENOMEM;
1867
1868 path->reada = 1;
1869 path->leave_spinning = 1;
1870 /* this will setup the path even if it fails to insert the back ref */
1871 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1872 path, bytenr, num_bytes, parent,
1873 root_objectid, owner, offset,
1874 refs_to_add, extent_op);
1875 if (ret == 0)
1876 goto out;
1877
1878 if (ret != -EAGAIN) {
1879 err = ret;
1880 goto out;
1881 }
1882
1883 leaf = path->nodes[0];
1884 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1885 refs = btrfs_extent_refs(leaf, item);
1886 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1887 if (extent_op)
1888 __run_delayed_extent_op(extent_op, leaf, item);
56bec294 1889
5d4f98a2 1890 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 1891 btrfs_release_path(path);
56bec294
CM
1892
1893 path->reada = 1;
b9473439
CM
1894 path->leave_spinning = 1;
1895
56bec294
CM
1896 /* now insert the actual backref */
1897 ret = insert_extent_backref(trans, root->fs_info->extent_root,
5d4f98a2
YZ
1898 path, bytenr, parent, root_objectid,
1899 owner, offset, refs_to_add);
56bec294 1900 BUG_ON(ret);
5d4f98a2 1901out:
56bec294 1902 btrfs_free_path(path);
5d4f98a2 1903 return err;
56bec294
CM
1904}
1905
5d4f98a2
YZ
1906static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1907 struct btrfs_root *root,
1908 struct btrfs_delayed_ref_node *node,
1909 struct btrfs_delayed_extent_op *extent_op,
1910 int insert_reserved)
56bec294 1911{
5d4f98a2
YZ
1912 int ret = 0;
1913 struct btrfs_delayed_data_ref *ref;
1914 struct btrfs_key ins;
1915 u64 parent = 0;
1916 u64 ref_root = 0;
1917 u64 flags = 0;
1918
1919 ins.objectid = node->bytenr;
1920 ins.offset = node->num_bytes;
1921 ins.type = BTRFS_EXTENT_ITEM_KEY;
1922
1923 ref = btrfs_delayed_node_to_data_ref(node);
1924 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1925 parent = ref->parent;
1926 else
1927 ref_root = ref->root;
1928
1929 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1930 if (extent_op) {
1931 BUG_ON(extent_op->update_key);
1932 flags |= extent_op->flags_to_set;
1933 }
1934 ret = alloc_reserved_file_extent(trans, root,
1935 parent, ref_root, flags,
1936 ref->objectid, ref->offset,
1937 &ins, node->ref_mod);
5d4f98a2
YZ
1938 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1939 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1940 node->num_bytes, parent,
1941 ref_root, ref->objectid,
1942 ref->offset, node->ref_mod,
1943 extent_op);
1944 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1945 ret = __btrfs_free_extent(trans, root, node->bytenr,
1946 node->num_bytes, parent,
1947 ref_root, ref->objectid,
1948 ref->offset, node->ref_mod,
1949 extent_op);
1950 } else {
1951 BUG();
1952 }
1953 return ret;
1954}
1955
1956static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1957 struct extent_buffer *leaf,
1958 struct btrfs_extent_item *ei)
1959{
1960 u64 flags = btrfs_extent_flags(leaf, ei);
1961 if (extent_op->update_flags) {
1962 flags |= extent_op->flags_to_set;
1963 btrfs_set_extent_flags(leaf, ei, flags);
1964 }
1965
1966 if (extent_op->update_key) {
1967 struct btrfs_tree_block_info *bi;
1968 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1969 bi = (struct btrfs_tree_block_info *)(ei + 1);
1970 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1971 }
1972}
1973
1974static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1975 struct btrfs_root *root,
1976 struct btrfs_delayed_ref_node *node,
1977 struct btrfs_delayed_extent_op *extent_op)
1978{
1979 struct btrfs_key key;
1980 struct btrfs_path *path;
1981 struct btrfs_extent_item *ei;
1982 struct extent_buffer *leaf;
1983 u32 item_size;
56bec294 1984 int ret;
5d4f98a2
YZ
1985 int err = 0;
1986
1987 path = btrfs_alloc_path();
1988 if (!path)
1989 return -ENOMEM;
1990
1991 key.objectid = node->bytenr;
1992 key.type = BTRFS_EXTENT_ITEM_KEY;
1993 key.offset = node->num_bytes;
1994
1995 path->reada = 1;
1996 path->leave_spinning = 1;
1997 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1998 path, 0, 1);
1999 if (ret < 0) {
2000 err = ret;
2001 goto out;
2002 }
2003 if (ret > 0) {
2004 err = -EIO;
2005 goto out;
2006 }
2007
2008 leaf = path->nodes[0];
2009 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2010#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2011 if (item_size < sizeof(*ei)) {
2012 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2013 path, (u64)-1, 0);
2014 if (ret < 0) {
2015 err = ret;
2016 goto out;
2017 }
2018 leaf = path->nodes[0];
2019 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2020 }
2021#endif
2022 BUG_ON(item_size < sizeof(*ei));
2023 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2024 __run_delayed_extent_op(extent_op, leaf, ei);
56bec294 2025
5d4f98a2
YZ
2026 btrfs_mark_buffer_dirty(leaf);
2027out:
2028 btrfs_free_path(path);
2029 return err;
56bec294
CM
2030}
2031
5d4f98a2
YZ
2032static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2033 struct btrfs_root *root,
2034 struct btrfs_delayed_ref_node *node,
2035 struct btrfs_delayed_extent_op *extent_op,
2036 int insert_reserved)
56bec294
CM
2037{
2038 int ret = 0;
5d4f98a2
YZ
2039 struct btrfs_delayed_tree_ref *ref;
2040 struct btrfs_key ins;
2041 u64 parent = 0;
2042 u64 ref_root = 0;
56bec294 2043
5d4f98a2
YZ
2044 ins.objectid = node->bytenr;
2045 ins.offset = node->num_bytes;
2046 ins.type = BTRFS_EXTENT_ITEM_KEY;
56bec294 2047
5d4f98a2
YZ
2048 ref = btrfs_delayed_node_to_tree_ref(node);
2049 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2050 parent = ref->parent;
2051 else
2052 ref_root = ref->root;
2053
2054 BUG_ON(node->ref_mod != 1);
2055 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2056 BUG_ON(!extent_op || !extent_op->update_flags ||
2057 !extent_op->update_key);
2058 ret = alloc_reserved_tree_block(trans, root,
2059 parent, ref_root,
2060 extent_op->flags_to_set,
2061 &extent_op->key,
2062 ref->level, &ins);
5d4f98a2
YZ
2063 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2064 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2065 node->num_bytes, parent, ref_root,
2066 ref->level, 0, 1, extent_op);
2067 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2068 ret = __btrfs_free_extent(trans, root, node->bytenr,
2069 node->num_bytes, parent, ref_root,
2070 ref->level, 0, 1, extent_op);
2071 } else {
2072 BUG();
2073 }
56bec294
CM
2074 return ret;
2075}
2076
2077/* helper function to actually process a single delayed ref entry */
5d4f98a2
YZ
2078static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2079 struct btrfs_root *root,
2080 struct btrfs_delayed_ref_node *node,
2081 struct btrfs_delayed_extent_op *extent_op,
2082 int insert_reserved)
56bec294
CM
2083{
2084 int ret;
5d4f98a2 2085 if (btrfs_delayed_ref_is_head(node)) {
56bec294
CM
2086 struct btrfs_delayed_ref_head *head;
2087 /*
2088 * we've hit the end of the chain and we were supposed
2089 * to insert this extent into the tree. But, it got
2090 * deleted before we ever needed to insert it, so all
2091 * we have to do is clean up the accounting
2092 */
5d4f98a2
YZ
2093 BUG_ON(extent_op);
2094 head = btrfs_delayed_node_to_head(node);
56bec294 2095 if (insert_reserved) {
f0486c68
YZ
2096 btrfs_pin_extent(root, node->bytenr,
2097 node->num_bytes, 1);
5d4f98a2
YZ
2098 if (head->is_data) {
2099 ret = btrfs_del_csums(trans, root,
2100 node->bytenr,
2101 node->num_bytes);
2102 BUG_ON(ret);
2103 }
56bec294 2104 }
56bec294
CM
2105 mutex_unlock(&head->mutex);
2106 return 0;
2107 }
2108
5d4f98a2
YZ
2109 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2110 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2111 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2112 insert_reserved);
2113 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2114 node->type == BTRFS_SHARED_DATA_REF_KEY)
2115 ret = run_delayed_data_ref(trans, root, node, extent_op,
2116 insert_reserved);
2117 else
2118 BUG();
2119 return ret;
56bec294
CM
2120}
2121
2122static noinline struct btrfs_delayed_ref_node *
2123select_delayed_ref(struct btrfs_delayed_ref_head *head)
2124{
2125 struct rb_node *node;
2126 struct btrfs_delayed_ref_node *ref;
2127 int action = BTRFS_ADD_DELAYED_REF;
2128again:
2129 /*
2130 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2131 * this prevents ref count from going down to zero when
2132 * there still are pending delayed ref.
2133 */
2134 node = rb_prev(&head->node.rb_node);
2135 while (1) {
2136 if (!node)
2137 break;
2138 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2139 rb_node);
2140 if (ref->bytenr != head->node.bytenr)
2141 break;
5d4f98a2 2142 if (ref->action == action)
56bec294
CM
2143 return ref;
2144 node = rb_prev(node);
2145 }
2146 if (action == BTRFS_ADD_DELAYED_REF) {
2147 action = BTRFS_DROP_DELAYED_REF;
2148 goto again;
2149 }
2150 return NULL;
2151}
2152
c3e69d58
CM
2153static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2154 struct btrfs_root *root,
2155 struct list_head *cluster)
56bec294 2156{
56bec294
CM
2157 struct btrfs_delayed_ref_root *delayed_refs;
2158 struct btrfs_delayed_ref_node *ref;
2159 struct btrfs_delayed_ref_head *locked_ref = NULL;
5d4f98a2 2160 struct btrfs_delayed_extent_op *extent_op;
56bec294 2161 int ret;
c3e69d58 2162 int count = 0;
56bec294 2163 int must_insert_reserved = 0;
56bec294
CM
2164
2165 delayed_refs = &trans->transaction->delayed_refs;
56bec294
CM
2166 while (1) {
2167 if (!locked_ref) {
c3e69d58
CM
2168 /* pick a new head ref from the cluster list */
2169 if (list_empty(cluster))
56bec294 2170 break;
56bec294 2171
c3e69d58
CM
2172 locked_ref = list_entry(cluster->next,
2173 struct btrfs_delayed_ref_head, cluster);
2174
2175 /* grab the lock that says we are going to process
2176 * all the refs for this head */
2177 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2178
2179 /*
2180 * we may have dropped the spin lock to get the head
2181 * mutex lock, and that might have given someone else
2182 * time to free the head. If that's true, it has been
2183 * removed from our list and we can move on.
2184 */
2185 if (ret == -EAGAIN) {
2186 locked_ref = NULL;
2187 count++;
2188 continue;
56bec294
CM
2189 }
2190 }
a28ec197 2191
56bec294
CM
2192 /*
2193 * record the must insert reserved flag before we
2194 * drop the spin lock.
2195 */
2196 must_insert_reserved = locked_ref->must_insert_reserved;
2197 locked_ref->must_insert_reserved = 0;
7bb86316 2198
5d4f98a2
YZ
2199 extent_op = locked_ref->extent_op;
2200 locked_ref->extent_op = NULL;
2201
56bec294
CM
2202 /*
2203 * locked_ref is the head node, so we have to go one
2204 * node back for any delayed ref updates
2205 */
56bec294
CM
2206 ref = select_delayed_ref(locked_ref);
2207 if (!ref) {
2208 /* All delayed refs have been processed, Go ahead
2209 * and send the head node to run_one_delayed_ref,
2210 * so that any accounting fixes can happen
2211 */
2212 ref = &locked_ref->node;
5d4f98a2
YZ
2213
2214 if (extent_op && must_insert_reserved) {
2215 kfree(extent_op);
2216 extent_op = NULL;
2217 }
2218
2219 if (extent_op) {
2220 spin_unlock(&delayed_refs->lock);
2221
2222 ret = run_delayed_extent_op(trans, root,
2223 ref, extent_op);
2224 BUG_ON(ret);
2225 kfree(extent_op);
2226
2227 cond_resched();
2228 spin_lock(&delayed_refs->lock);
2229 continue;
2230 }
2231
c3e69d58 2232 list_del_init(&locked_ref->cluster);
56bec294
CM
2233 locked_ref = NULL;
2234 }
02217ed2 2235
56bec294
CM
2236 ref->in_tree = 0;
2237 rb_erase(&ref->rb_node, &delayed_refs->root);
2238 delayed_refs->num_entries--;
5d4f98a2 2239
56bec294 2240 spin_unlock(&delayed_refs->lock);
925baedd 2241
5d4f98a2 2242 ret = run_one_delayed_ref(trans, root, ref, extent_op,
56bec294
CM
2243 must_insert_reserved);
2244 BUG_ON(ret);
eb099670 2245
5d4f98a2
YZ
2246 btrfs_put_delayed_ref(ref);
2247 kfree(extent_op);
c3e69d58 2248 count++;
5d4f98a2 2249
c3e69d58
CM
2250 cond_resched();
2251 spin_lock(&delayed_refs->lock);
2252 }
2253 return count;
2254}
2255
2256/*
2257 * this starts processing the delayed reference count updates and
2258 * extent insertions we have queued up so far. count can be
2259 * 0, which means to process everything in the tree at the start
2260 * of the run (but not newly added entries), or it can be some target
2261 * number you'd like to process.
2262 */
2263int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2264 struct btrfs_root *root, unsigned long count)
2265{
2266 struct rb_node *node;
2267 struct btrfs_delayed_ref_root *delayed_refs;
2268 struct btrfs_delayed_ref_node *ref;
2269 struct list_head cluster;
2270 int ret;
2271 int run_all = count == (unsigned long)-1;
2272 int run_most = 0;
2273
2274 if (root == root->fs_info->extent_root)
2275 root = root->fs_info->tree_root;
2276
2277 delayed_refs = &trans->transaction->delayed_refs;
2278 INIT_LIST_HEAD(&cluster);
2279again:
2280 spin_lock(&delayed_refs->lock);
2281 if (count == 0) {
2282 count = delayed_refs->num_entries * 2;
2283 run_most = 1;
2284 }
2285 while (1) {
2286 if (!(run_all || run_most) &&
2287 delayed_refs->num_heads_ready < 64)
2288 break;
eb099670 2289
56bec294 2290 /*
c3e69d58
CM
2291 * go find something we can process in the rbtree. We start at
2292 * the beginning of the tree, and then build a cluster
2293 * of refs to process starting at the first one we are able to
2294 * lock
56bec294 2295 */
c3e69d58
CM
2296 ret = btrfs_find_ref_cluster(trans, &cluster,
2297 delayed_refs->run_delayed_start);
2298 if (ret)
56bec294
CM
2299 break;
2300
c3e69d58
CM
2301 ret = run_clustered_refs(trans, root, &cluster);
2302 BUG_ON(ret < 0);
2303
2304 count -= min_t(unsigned long, ret, count);
2305
2306 if (count == 0)
2307 break;
eb099670 2308 }
c3e69d58 2309
56bec294 2310 if (run_all) {
56bec294 2311 node = rb_first(&delayed_refs->root);
c3e69d58 2312 if (!node)
56bec294 2313 goto out;
c3e69d58 2314 count = (unsigned long)-1;
e9d0b13b 2315
56bec294
CM
2316 while (node) {
2317 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2318 rb_node);
2319 if (btrfs_delayed_ref_is_head(ref)) {
2320 struct btrfs_delayed_ref_head *head;
5caf2a00 2321
56bec294
CM
2322 head = btrfs_delayed_node_to_head(ref);
2323 atomic_inc(&ref->refs);
2324
2325 spin_unlock(&delayed_refs->lock);
8cc33e5c
DS
2326 /*
2327 * Mutex was contended, block until it's
2328 * released and try again
2329 */
56bec294
CM
2330 mutex_lock(&head->mutex);
2331 mutex_unlock(&head->mutex);
2332
2333 btrfs_put_delayed_ref(ref);
1887be66 2334 cond_resched();
56bec294
CM
2335 goto again;
2336 }
2337 node = rb_next(node);
2338 }
2339 spin_unlock(&delayed_refs->lock);
56bec294
CM
2340 schedule_timeout(1);
2341 goto again;
5f39d397 2342 }
54aa1f4d 2343out:
c3e69d58 2344 spin_unlock(&delayed_refs->lock);
a28ec197
CM
2345 return 0;
2346}
2347
5d4f98a2
YZ
2348int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2349 struct btrfs_root *root,
2350 u64 bytenr, u64 num_bytes, u64 flags,
2351 int is_data)
2352{
2353 struct btrfs_delayed_extent_op *extent_op;
2354 int ret;
2355
2356 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2357 if (!extent_op)
2358 return -ENOMEM;
2359
2360 extent_op->flags_to_set = flags;
2361 extent_op->update_flags = 1;
2362 extent_op->update_key = 0;
2363 extent_op->is_data = is_data ? 1 : 0;
2364
2365 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2366 if (ret)
2367 kfree(extent_op);
2368 return ret;
2369}
2370
2371static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2372 struct btrfs_root *root,
2373 struct btrfs_path *path,
2374 u64 objectid, u64 offset, u64 bytenr)
2375{
2376 struct btrfs_delayed_ref_head *head;
2377 struct btrfs_delayed_ref_node *ref;
2378 struct btrfs_delayed_data_ref *data_ref;
2379 struct btrfs_delayed_ref_root *delayed_refs;
2380 struct rb_node *node;
2381 int ret = 0;
2382
2383 ret = -ENOENT;
2384 delayed_refs = &trans->transaction->delayed_refs;
2385 spin_lock(&delayed_refs->lock);
2386 head = btrfs_find_delayed_ref_head(trans, bytenr);
2387 if (!head)
2388 goto out;
2389
2390 if (!mutex_trylock(&head->mutex)) {
2391 atomic_inc(&head->node.refs);
2392 spin_unlock(&delayed_refs->lock);
2393
b3b4aa74 2394 btrfs_release_path(path);
5d4f98a2 2395
8cc33e5c
DS
2396 /*
2397 * Mutex was contended, block until it's released and let
2398 * caller try again
2399 */
5d4f98a2
YZ
2400 mutex_lock(&head->mutex);
2401 mutex_unlock(&head->mutex);
2402 btrfs_put_delayed_ref(&head->node);
2403 return -EAGAIN;
2404 }
2405
2406 node = rb_prev(&head->node.rb_node);
2407 if (!node)
2408 goto out_unlock;
2409
2410 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2411
2412 if (ref->bytenr != bytenr)
2413 goto out_unlock;
2414
2415 ret = 1;
2416 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2417 goto out_unlock;
2418
2419 data_ref = btrfs_delayed_node_to_data_ref(ref);
2420
2421 node = rb_prev(node);
2422 if (node) {
2423 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2424 if (ref->bytenr == bytenr)
2425 goto out_unlock;
2426 }
2427
2428 if (data_ref->root != root->root_key.objectid ||
2429 data_ref->objectid != objectid || data_ref->offset != offset)
2430 goto out_unlock;
2431
2432 ret = 0;
2433out_unlock:
2434 mutex_unlock(&head->mutex);
2435out:
2436 spin_unlock(&delayed_refs->lock);
2437 return ret;
2438}
2439
2440static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2441 struct btrfs_root *root,
2442 struct btrfs_path *path,
2443 u64 objectid, u64 offset, u64 bytenr)
be20aa9d
CM
2444{
2445 struct btrfs_root *extent_root = root->fs_info->extent_root;
f321e491 2446 struct extent_buffer *leaf;
5d4f98a2
YZ
2447 struct btrfs_extent_data_ref *ref;
2448 struct btrfs_extent_inline_ref *iref;
2449 struct btrfs_extent_item *ei;
f321e491 2450 struct btrfs_key key;
5d4f98a2 2451 u32 item_size;
be20aa9d 2452 int ret;
925baedd 2453
be20aa9d 2454 key.objectid = bytenr;
31840ae1 2455 key.offset = (u64)-1;
f321e491 2456 key.type = BTRFS_EXTENT_ITEM_KEY;
be20aa9d 2457
be20aa9d
CM
2458 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2459 if (ret < 0)
2460 goto out;
2461 BUG_ON(ret == 0);
80ff3856
YZ
2462
2463 ret = -ENOENT;
2464 if (path->slots[0] == 0)
31840ae1 2465 goto out;
be20aa9d 2466
31840ae1 2467 path->slots[0]--;
f321e491 2468 leaf = path->nodes[0];
5d4f98a2 2469 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
be20aa9d 2470
5d4f98a2 2471 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
be20aa9d 2472 goto out;
f321e491 2473
5d4f98a2
YZ
2474 ret = 1;
2475 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2476#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2477 if (item_size < sizeof(*ei)) {
2478 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2479 goto out;
2480 }
2481#endif
2482 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
bd09835d 2483
5d4f98a2
YZ
2484 if (item_size != sizeof(*ei) +
2485 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2486 goto out;
be20aa9d 2487
5d4f98a2
YZ
2488 if (btrfs_extent_generation(leaf, ei) <=
2489 btrfs_root_last_snapshot(&root->root_item))
2490 goto out;
2491
2492 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2493 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2494 BTRFS_EXTENT_DATA_REF_KEY)
2495 goto out;
2496
2497 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2498 if (btrfs_extent_refs(leaf, ei) !=
2499 btrfs_extent_data_ref_count(leaf, ref) ||
2500 btrfs_extent_data_ref_root(leaf, ref) !=
2501 root->root_key.objectid ||
2502 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2503 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2504 goto out;
2505
2506 ret = 0;
2507out:
2508 return ret;
2509}
2510
2511int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2512 struct btrfs_root *root,
2513 u64 objectid, u64 offset, u64 bytenr)
2514{
2515 struct btrfs_path *path;
2516 int ret;
2517 int ret2;
2518
2519 path = btrfs_alloc_path();
2520 if (!path)
2521 return -ENOENT;
2522
2523 do {
2524 ret = check_committed_ref(trans, root, path, objectid,
2525 offset, bytenr);
2526 if (ret && ret != -ENOENT)
f321e491 2527 goto out;
80ff3856 2528
5d4f98a2
YZ
2529 ret2 = check_delayed_ref(trans, root, path, objectid,
2530 offset, bytenr);
2531 } while (ret2 == -EAGAIN);
2532
2533 if (ret2 && ret2 != -ENOENT) {
2534 ret = ret2;
2535 goto out;
f321e491 2536 }
5d4f98a2
YZ
2537
2538 if (ret != -ENOENT || ret2 != -ENOENT)
2539 ret = 0;
be20aa9d 2540out:
80ff3856 2541 btrfs_free_path(path);
f0486c68
YZ
2542 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2543 WARN_ON(ret > 0);
f321e491 2544 return ret;
be20aa9d 2545}
c5739bba 2546
5d4f98a2 2547static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
b7a9f29f 2548 struct btrfs_root *root,
5d4f98a2
YZ
2549 struct extent_buffer *buf,
2550 int full_backref, int inc)
31840ae1
ZY
2551{
2552 u64 bytenr;
5d4f98a2
YZ
2553 u64 num_bytes;
2554 u64 parent;
31840ae1 2555 u64 ref_root;
31840ae1 2556 u32 nritems;
31840ae1
ZY
2557 struct btrfs_key key;
2558 struct btrfs_file_extent_item *fi;
2559 int i;
2560 int level;
2561 int ret = 0;
31840ae1 2562 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
5d4f98a2 2563 u64, u64, u64, u64, u64, u64);
31840ae1
ZY
2564
2565 ref_root = btrfs_header_owner(buf);
31840ae1
ZY
2566 nritems = btrfs_header_nritems(buf);
2567 level = btrfs_header_level(buf);
2568
5d4f98a2
YZ
2569 if (!root->ref_cows && level == 0)
2570 return 0;
31840ae1 2571
5d4f98a2
YZ
2572 if (inc)
2573 process_func = btrfs_inc_extent_ref;
2574 else
2575 process_func = btrfs_free_extent;
31840ae1 2576
5d4f98a2
YZ
2577 if (full_backref)
2578 parent = buf->start;
2579 else
2580 parent = 0;
2581
2582 for (i = 0; i < nritems; i++) {
31840ae1 2583 if (level == 0) {
5d4f98a2 2584 btrfs_item_key_to_cpu(buf, &key, i);
31840ae1
ZY
2585 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2586 continue;
5d4f98a2 2587 fi = btrfs_item_ptr(buf, i,
31840ae1
ZY
2588 struct btrfs_file_extent_item);
2589 if (btrfs_file_extent_type(buf, fi) ==
2590 BTRFS_FILE_EXTENT_INLINE)
2591 continue;
2592 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2593 if (bytenr == 0)
2594 continue;
5d4f98a2
YZ
2595
2596 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2597 key.offset -= btrfs_file_extent_offset(buf, fi);
2598 ret = process_func(trans, root, bytenr, num_bytes,
2599 parent, ref_root, key.objectid,
2600 key.offset);
31840ae1
ZY
2601 if (ret)
2602 goto fail;
2603 } else {
5d4f98a2
YZ
2604 bytenr = btrfs_node_blockptr(buf, i);
2605 num_bytes = btrfs_level_size(root, level - 1);
2606 ret = process_func(trans, root, bytenr, num_bytes,
2607 parent, ref_root, level - 1, 0);
31840ae1
ZY
2608 if (ret)
2609 goto fail;
2610 }
2611 }
2612 return 0;
2613fail:
5d4f98a2
YZ
2614 BUG();
2615 return ret;
2616}
2617
2618int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2619 struct extent_buffer *buf, int full_backref)
2620{
2621 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2622}
2623
2624int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2625 struct extent_buffer *buf, int full_backref)
2626{
2627 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
31840ae1
ZY
2628}
2629
9078a3e1
CM
2630static int write_one_cache_group(struct btrfs_trans_handle *trans,
2631 struct btrfs_root *root,
2632 struct btrfs_path *path,
2633 struct btrfs_block_group_cache *cache)
2634{
2635 int ret;
9078a3e1 2636 struct btrfs_root *extent_root = root->fs_info->extent_root;
5f39d397
CM
2637 unsigned long bi;
2638 struct extent_buffer *leaf;
9078a3e1 2639
9078a3e1 2640 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
54aa1f4d
CM
2641 if (ret < 0)
2642 goto fail;
9078a3e1 2643 BUG_ON(ret);
5f39d397
CM
2644
2645 leaf = path->nodes[0];
2646 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2647 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2648 btrfs_mark_buffer_dirty(leaf);
b3b4aa74 2649 btrfs_release_path(path);
54aa1f4d 2650fail:
9078a3e1
CM
2651 if (ret)
2652 return ret;
9078a3e1
CM
2653 return 0;
2654
2655}
2656
4a8c9a62
YZ
2657static struct btrfs_block_group_cache *
2658next_block_group(struct btrfs_root *root,
2659 struct btrfs_block_group_cache *cache)
2660{
2661 struct rb_node *node;
2662 spin_lock(&root->fs_info->block_group_cache_lock);
2663 node = rb_next(&cache->cache_node);
2664 btrfs_put_block_group(cache);
2665 if (node) {
2666 cache = rb_entry(node, struct btrfs_block_group_cache,
2667 cache_node);
11dfe35a 2668 btrfs_get_block_group(cache);
4a8c9a62
YZ
2669 } else
2670 cache = NULL;
2671 spin_unlock(&root->fs_info->block_group_cache_lock);
2672 return cache;
2673}
2674
0af3d00b
JB
2675static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2676 struct btrfs_trans_handle *trans,
2677 struct btrfs_path *path)
2678{
2679 struct btrfs_root *root = block_group->fs_info->tree_root;
2680 struct inode *inode = NULL;
2681 u64 alloc_hint = 0;
2b20982e 2682 int dcs = BTRFS_DC_ERROR;
0af3d00b
JB
2683 int num_pages = 0;
2684 int retries = 0;
2685 int ret = 0;
2686
2687 /*
2688 * If this block group is smaller than 100 megs don't bother caching the
2689 * block group.
2690 */
2691 if (block_group->key.offset < (100 * 1024 * 1024)) {
2692 spin_lock(&block_group->lock);
2693 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2694 spin_unlock(&block_group->lock);
2695 return 0;
2696 }
2697
2698again:
2699 inode = lookup_free_space_inode(root, block_group, path);
2700 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2701 ret = PTR_ERR(inode);
b3b4aa74 2702 btrfs_release_path(path);
0af3d00b
JB
2703 goto out;
2704 }
2705
2706 if (IS_ERR(inode)) {
2707 BUG_ON(retries);
2708 retries++;
2709
2710 if (block_group->ro)
2711 goto out_free;
2712
2713 ret = create_free_space_inode(root, trans, block_group, path);
2714 if (ret)
2715 goto out_free;
2716 goto again;
2717 }
2718
2719 /*
2720 * We want to set the generation to 0, that way if anything goes wrong
2721 * from here on out we know not to trust this cache when we load up next
2722 * time.
2723 */
2724 BTRFS_I(inode)->generation = 0;
2725 ret = btrfs_update_inode(trans, root, inode);
2726 WARN_ON(ret);
2727
2728 if (i_size_read(inode) > 0) {
2729 ret = btrfs_truncate_free_space_cache(root, trans, path,
2730 inode);
2731 if (ret)
2732 goto out_put;
2733 }
2734
2735 spin_lock(&block_group->lock);
2736 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2b20982e
JB
2737 /* We're not cached, don't bother trying to write stuff out */
2738 dcs = BTRFS_DC_WRITTEN;
0af3d00b
JB
2739 spin_unlock(&block_group->lock);
2740 goto out_put;
2741 }
2742 spin_unlock(&block_group->lock);
2743
2744 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2745 if (!num_pages)
2746 num_pages = 1;
2747
2748 /*
2749 * Just to make absolutely sure we have enough space, we're going to
2750 * preallocate 12 pages worth of space for each block group. In
2751 * practice we ought to use at most 8, but we need extra space so we can
2752 * add our header and have a terminator between the extents and the
2753 * bitmaps.
2754 */
2755 num_pages *= 16;
2756 num_pages *= PAGE_CACHE_SIZE;
2757
2758 ret = btrfs_check_data_free_space(inode, num_pages);
2759 if (ret)
2760 goto out_put;
2761
2762 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2763 num_pages, num_pages,
2764 &alloc_hint);
2b20982e
JB
2765 if (!ret)
2766 dcs = BTRFS_DC_SETUP;
0af3d00b
JB
2767 btrfs_free_reserved_data_space(inode, num_pages);
2768out_put:
2769 iput(inode);
2770out_free:
b3b4aa74 2771 btrfs_release_path(path);
0af3d00b
JB
2772out:
2773 spin_lock(&block_group->lock);
2b20982e 2774 block_group->disk_cache_state = dcs;
0af3d00b
JB
2775 spin_unlock(&block_group->lock);
2776
2777 return ret;
2778}
2779
96b5179d
CM
2780int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2781 struct btrfs_root *root)
9078a3e1 2782{
4a8c9a62 2783 struct btrfs_block_group_cache *cache;
9078a3e1 2784 int err = 0;
9078a3e1 2785 struct btrfs_path *path;
96b5179d 2786 u64 last = 0;
9078a3e1
CM
2787
2788 path = btrfs_alloc_path();
2789 if (!path)
2790 return -ENOMEM;
2791
0af3d00b
JB
2792again:
2793 while (1) {
2794 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2795 while (cache) {
2796 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2797 break;
2798 cache = next_block_group(root, cache);
2799 }
2800 if (!cache) {
2801 if (last == 0)
2802 break;
2803 last = 0;
2804 continue;
2805 }
2806 err = cache_save_setup(cache, trans, path);
2807 last = cache->key.objectid + cache->key.offset;
2808 btrfs_put_block_group(cache);
2809 }
2810
d397712b 2811 while (1) {
4a8c9a62
YZ
2812 if (last == 0) {
2813 err = btrfs_run_delayed_refs(trans, root,
2814 (unsigned long)-1);
2815 BUG_ON(err);
0f9dd46c 2816 }
54aa1f4d 2817
4a8c9a62
YZ
2818 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2819 while (cache) {
0af3d00b
JB
2820 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2821 btrfs_put_block_group(cache);
2822 goto again;
2823 }
2824
4a8c9a62
YZ
2825 if (cache->dirty)
2826 break;
2827 cache = next_block_group(root, cache);
2828 }
2829 if (!cache) {
2830 if (last == 0)
2831 break;
2832 last = 0;
2833 continue;
2834 }
0f9dd46c 2835
0cb59c99
JB
2836 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2837 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
e8569813 2838 cache->dirty = 0;
4a8c9a62 2839 last = cache->key.objectid + cache->key.offset;
0f9dd46c 2840
4a8c9a62
YZ
2841 err = write_one_cache_group(trans, root, path, cache);
2842 BUG_ON(err);
2843 btrfs_put_block_group(cache);
9078a3e1 2844 }
4a8c9a62 2845
0cb59c99
JB
2846 while (1) {
2847 /*
2848 * I don't think this is needed since we're just marking our
2849 * preallocated extent as written, but just in case it can't
2850 * hurt.
2851 */
2852 if (last == 0) {
2853 err = btrfs_run_delayed_refs(trans, root,
2854 (unsigned long)-1);
2855 BUG_ON(err);
2856 }
2857
2858 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2859 while (cache) {
2860 /*
2861 * Really this shouldn't happen, but it could if we
2862 * couldn't write the entire preallocated extent and
2863 * splitting the extent resulted in a new block.
2864 */
2865 if (cache->dirty) {
2866 btrfs_put_block_group(cache);
2867 goto again;
2868 }
2869 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2870 break;
2871 cache = next_block_group(root, cache);
2872 }
2873 if (!cache) {
2874 if (last == 0)
2875 break;
2876 last = 0;
2877 continue;
2878 }
2879
2880 btrfs_write_out_cache(root, trans, cache, path);
2881
2882 /*
2883 * If we didn't have an error then the cache state is still
2884 * NEED_WRITE, so we can set it to WRITTEN.
2885 */
2886 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2887 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2888 last = cache->key.objectid + cache->key.offset;
2889 btrfs_put_block_group(cache);
2890 }
2891
9078a3e1 2892 btrfs_free_path(path);
4a8c9a62 2893 return 0;
9078a3e1
CM
2894}
2895
d2fb3437
YZ
2896int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2897{
2898 struct btrfs_block_group_cache *block_group;
2899 int readonly = 0;
2900
2901 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2902 if (!block_group || block_group->ro)
2903 readonly = 1;
2904 if (block_group)
fa9c0d79 2905 btrfs_put_block_group(block_group);
d2fb3437
YZ
2906 return readonly;
2907}
2908
593060d7
CM
2909static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2910 u64 total_bytes, u64 bytes_used,
2911 struct btrfs_space_info **space_info)
2912{
2913 struct btrfs_space_info *found;
b742bb82
YZ
2914 int i;
2915 int factor;
2916
2917 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2918 BTRFS_BLOCK_GROUP_RAID10))
2919 factor = 2;
2920 else
2921 factor = 1;
593060d7
CM
2922
2923 found = __find_space_info(info, flags);
2924 if (found) {
25179201 2925 spin_lock(&found->lock);
593060d7 2926 found->total_bytes += total_bytes;
89a55897 2927 found->disk_total += total_bytes * factor;
593060d7 2928 found->bytes_used += bytes_used;
b742bb82 2929 found->disk_used += bytes_used * factor;
8f18cf13 2930 found->full = 0;
25179201 2931 spin_unlock(&found->lock);
593060d7
CM
2932 *space_info = found;
2933 return 0;
2934 }
c146afad 2935 found = kzalloc(sizeof(*found), GFP_NOFS);
593060d7
CM
2936 if (!found)
2937 return -ENOMEM;
2938
b742bb82
YZ
2939 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2940 INIT_LIST_HEAD(&found->block_groups[i]);
80eb234a 2941 init_rwsem(&found->groups_sem);
0f9dd46c 2942 spin_lock_init(&found->lock);
b742bb82
YZ
2943 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2944 BTRFS_BLOCK_GROUP_SYSTEM |
2945 BTRFS_BLOCK_GROUP_METADATA);
593060d7 2946 found->total_bytes = total_bytes;
89a55897 2947 found->disk_total = total_bytes * factor;
593060d7 2948 found->bytes_used = bytes_used;
b742bb82 2949 found->disk_used = bytes_used * factor;
593060d7 2950 found->bytes_pinned = 0;
e8569813 2951 found->bytes_reserved = 0;
c146afad 2952 found->bytes_readonly = 0;
f0486c68 2953 found->bytes_may_use = 0;
593060d7 2954 found->full = 0;
0e4f8f88 2955 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
6d74119f 2956 found->chunk_alloc = 0;
fdb5effd
JB
2957 found->flush = 0;
2958 init_waitqueue_head(&found->wait);
593060d7 2959 *space_info = found;
4184ea7f 2960 list_add_rcu(&found->list, &info->space_info);
593060d7
CM
2961 return 0;
2962}
2963
8790d502
CM
2964static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2965{
2966 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
611f0e00 2967 BTRFS_BLOCK_GROUP_RAID1 |
321aecc6 2968 BTRFS_BLOCK_GROUP_RAID10 |
611f0e00 2969 BTRFS_BLOCK_GROUP_DUP);
8790d502
CM
2970 if (extra_flags) {
2971 if (flags & BTRFS_BLOCK_GROUP_DATA)
2972 fs_info->avail_data_alloc_bits |= extra_flags;
2973 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2974 fs_info->avail_metadata_alloc_bits |= extra_flags;
2975 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2976 fs_info->avail_system_alloc_bits |= extra_flags;
2977 }
2978}
593060d7 2979
2b82032c 2980u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
ec44a35c 2981{
cd02dca5
CM
2982 /*
2983 * we add in the count of missing devices because we want
2984 * to make sure that any RAID levels on a degraded FS
2985 * continue to be honored.
2986 */
2987 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2988 root->fs_info->fs_devices->missing_devices;
a061fc8d
CM
2989
2990 if (num_devices == 1)
2991 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2992 if (num_devices < 4)
2993 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2994
ec44a35c
CM
2995 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2996 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
a061fc8d 2997 BTRFS_BLOCK_GROUP_RAID10))) {
ec44a35c 2998 flags &= ~BTRFS_BLOCK_GROUP_DUP;
a061fc8d 2999 }
ec44a35c
CM
3000
3001 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
a061fc8d 3002 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
ec44a35c 3003 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
a061fc8d 3004 }
ec44a35c
CM
3005
3006 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3007 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3008 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3009 (flags & BTRFS_BLOCK_GROUP_DUP)))
3010 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3011 return flags;
3012}
3013
b742bb82 3014static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
6a63209f 3015{
b742bb82
YZ
3016 if (flags & BTRFS_BLOCK_GROUP_DATA)
3017 flags |= root->fs_info->avail_data_alloc_bits &
3018 root->fs_info->data_alloc_profile;
3019 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3020 flags |= root->fs_info->avail_system_alloc_bits &
3021 root->fs_info->system_alloc_profile;
3022 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3023 flags |= root->fs_info->avail_metadata_alloc_bits &
3024 root->fs_info->metadata_alloc_profile;
3025 return btrfs_reduce_alloc_profile(root, flags);
6a63209f
JB
3026}
3027
6d07bcec 3028u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
9ed74f2d 3029{
b742bb82 3030 u64 flags;
9ed74f2d 3031
b742bb82
YZ
3032 if (data)
3033 flags = BTRFS_BLOCK_GROUP_DATA;
3034 else if (root == root->fs_info->chunk_root)
3035 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9ed74f2d 3036 else
b742bb82 3037 flags = BTRFS_BLOCK_GROUP_METADATA;
9ed74f2d 3038
b742bb82 3039 return get_alloc_profile(root, flags);
6a63209f 3040}
9ed74f2d 3041
6a63209f
JB
3042void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3043{
6a63209f 3044 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
f0486c68 3045 BTRFS_BLOCK_GROUP_DATA);
9ed74f2d
JB
3046}
3047
6a63209f 3048/*
6a63209f
JB
3049 * This will check the space that the inode allocates from to make sure we have
3050 * enough space for bytes.
6a63209f 3051 */
0ca1f7ce 3052int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
6a63209f 3053{
6a63209f 3054 struct btrfs_space_info *data_sinfo;
0ca1f7ce 3055 struct btrfs_root *root = BTRFS_I(inode)->root;
ab6e2410 3056 u64 used;
0af3d00b 3057 int ret = 0, committed = 0, alloc_chunk = 1;
6a63209f 3058
6a63209f
JB
3059 /* make sure bytes are sectorsize aligned */
3060 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
6a63209f 3061
82d5902d
LZ
3062 if (root == root->fs_info->tree_root ||
3063 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
0af3d00b
JB
3064 alloc_chunk = 0;
3065 committed = 1;
3066 }
3067
6a63209f 3068 data_sinfo = BTRFS_I(inode)->space_info;
33b4d47f
CM
3069 if (!data_sinfo)
3070 goto alloc;
9ed74f2d 3071
6a63209f
JB
3072again:
3073 /* make sure we have enough space to handle the data first */
3074 spin_lock(&data_sinfo->lock);
8929ecfa
YZ
3075 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3076 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3077 data_sinfo->bytes_may_use;
ab6e2410
JB
3078
3079 if (used + bytes > data_sinfo->total_bytes) {
4e06bdd6 3080 struct btrfs_trans_handle *trans;
9ed74f2d 3081
6a63209f
JB
3082 /*
3083 * if we don't have enough free bytes in this space then we need
3084 * to alloc a new chunk.
3085 */
0af3d00b 3086 if (!data_sinfo->full && alloc_chunk) {
6a63209f 3087 u64 alloc_target;
9ed74f2d 3088
0e4f8f88 3089 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
6a63209f 3090 spin_unlock(&data_sinfo->lock);
33b4d47f 3091alloc:
6a63209f 3092 alloc_target = btrfs_get_alloc_profile(root, 1);
7a7eaa40 3093 trans = btrfs_join_transaction(root);
a22285a6
YZ
3094 if (IS_ERR(trans))
3095 return PTR_ERR(trans);
9ed74f2d 3096
6a63209f
JB
3097 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3098 bytes + 2 * 1024 * 1024,
0e4f8f88
CM
3099 alloc_target,
3100 CHUNK_ALLOC_NO_FORCE);
6a63209f 3101 btrfs_end_transaction(trans, root);
d52a5b5f
MX
3102 if (ret < 0) {
3103 if (ret != -ENOSPC)
3104 return ret;
3105 else
3106 goto commit_trans;
3107 }
9ed74f2d 3108
33b4d47f
CM
3109 if (!data_sinfo) {
3110 btrfs_set_inode_space_info(root, inode);
3111 data_sinfo = BTRFS_I(inode)->space_info;
3112 }
6a63209f
JB
3113 goto again;
3114 }
f2bb8f5c
JB
3115
3116 /*
3117 * If we have less pinned bytes than we want to allocate then
3118 * don't bother committing the transaction, it won't help us.
3119 */
3120 if (data_sinfo->bytes_pinned < bytes)
3121 committed = 1;
6a63209f 3122 spin_unlock(&data_sinfo->lock);
6a63209f 3123
4e06bdd6 3124 /* commit the current transaction and try again */
d52a5b5f 3125commit_trans:
a4abeea4
JB
3126 if (!committed &&
3127 !atomic_read(&root->fs_info->open_ioctl_trans)) {
4e06bdd6 3128 committed = 1;
7a7eaa40 3129 trans = btrfs_join_transaction(root);
a22285a6
YZ
3130 if (IS_ERR(trans))
3131 return PTR_ERR(trans);
4e06bdd6
JB
3132 ret = btrfs_commit_transaction(trans, root);
3133 if (ret)
3134 return ret;
3135 goto again;
3136 }
9ed74f2d 3137
6a63209f
JB
3138 return -ENOSPC;
3139 }
3140 data_sinfo->bytes_may_use += bytes;
6a63209f 3141 spin_unlock(&data_sinfo->lock);
6a63209f 3142
9ed74f2d 3143 return 0;
9ed74f2d 3144}
6a63209f 3145
6a63209f 3146/*
fb25e914 3147 * Called if we need to clear a data reservation for this inode.
6a63209f 3148 */
0ca1f7ce 3149void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
e3ccfa98 3150{
0ca1f7ce 3151 struct btrfs_root *root = BTRFS_I(inode)->root;
6a63209f 3152 struct btrfs_space_info *data_sinfo;
e3ccfa98 3153
6a63209f
JB
3154 /* make sure bytes are sectorsize aligned */
3155 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
e3ccfa98 3156
6a63209f
JB
3157 data_sinfo = BTRFS_I(inode)->space_info;
3158 spin_lock(&data_sinfo->lock);
3159 data_sinfo->bytes_may_use -= bytes;
6a63209f 3160 spin_unlock(&data_sinfo->lock);
e3ccfa98
JB
3161}
3162
97e728d4 3163static void force_metadata_allocation(struct btrfs_fs_info *info)
e3ccfa98 3164{
97e728d4
JB
3165 struct list_head *head = &info->space_info;
3166 struct btrfs_space_info *found;
e3ccfa98 3167
97e728d4
JB
3168 rcu_read_lock();
3169 list_for_each_entry_rcu(found, head, list) {
3170 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
0e4f8f88 3171 found->force_alloc = CHUNK_ALLOC_FORCE;
e3ccfa98 3172 }
97e728d4 3173 rcu_read_unlock();
e3ccfa98
JB
3174}
3175
e5bc2458 3176static int should_alloc_chunk(struct btrfs_root *root,
0e4f8f88
CM
3177 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3178 int force)
32c00aff 3179{
fb25e914 3180 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
424499db 3181 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
0e4f8f88 3182 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
e5bc2458 3183 u64 thresh;
e3ccfa98 3184
0e4f8f88
CM
3185 if (force == CHUNK_ALLOC_FORCE)
3186 return 1;
3187
fb25e914
JB
3188 /*
3189 * We need to take into account the global rsv because for all intents
3190 * and purposes it's used space. Don't worry about locking the
3191 * global_rsv, it doesn't change except when the transaction commits.
3192 */
3193 num_allocated += global_rsv->size;
3194
0e4f8f88
CM
3195 /*
3196 * in limited mode, we want to have some free space up to
3197 * about 1% of the FS size.
3198 */
3199 if (force == CHUNK_ALLOC_LIMITED) {
3200 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3201 thresh = max_t(u64, 64 * 1024 * 1024,
3202 div_factor_fine(thresh, 1));
3203
3204 if (num_bytes - num_allocated < thresh)
3205 return 1;
3206 }
3207
3208 /*
3209 * we have two similar checks here, one based on percentage
3210 * and once based on a hard number of 256MB. The idea
3211 * is that if we have a good amount of free
3212 * room, don't allocate a chunk. A good mount is
3213 * less than 80% utilized of the chunks we have allocated,
3214 * or more than 256MB free
3215 */
3216 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
424499db 3217 return 0;
e3ccfa98 3218
0e4f8f88 3219 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
424499db 3220 return 0;
32c00aff 3221
e5bc2458 3222 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
0e4f8f88
CM
3223
3224 /* 256MB or 5% of the FS */
e5bc2458
CM
3225 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3226
3227 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
14ed0ca6 3228 return 0;
424499db 3229 return 1;
32c00aff
JB
3230}
3231
6324fbf3
CM
3232static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3233 struct btrfs_root *extent_root, u64 alloc_bytes,
0ef3e66b 3234 u64 flags, int force)
9ed74f2d 3235{
6324fbf3 3236 struct btrfs_space_info *space_info;
97e728d4 3237 struct btrfs_fs_info *fs_info = extent_root->fs_info;
6d74119f 3238 int wait_for_alloc = 0;
9ed74f2d 3239 int ret = 0;
9ed74f2d 3240
2b82032c 3241 flags = btrfs_reduce_alloc_profile(extent_root, flags);
ec44a35c 3242
6324fbf3 3243 space_info = __find_space_info(extent_root->fs_info, flags);
593060d7
CM
3244 if (!space_info) {
3245 ret = update_space_info(extent_root->fs_info, flags,
3246 0, 0, &space_info);
3247 BUG_ON(ret);
9ed74f2d 3248 }
6324fbf3 3249 BUG_ON(!space_info);
9ed74f2d 3250
6d74119f 3251again:
25179201 3252 spin_lock(&space_info->lock);
9ed74f2d 3253 if (space_info->force_alloc)
0e4f8f88 3254 force = space_info->force_alloc;
25179201
JB
3255 if (space_info->full) {
3256 spin_unlock(&space_info->lock);
6d74119f 3257 return 0;
9ed74f2d
JB
3258 }
3259
0e4f8f88 3260 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
25179201 3261 spin_unlock(&space_info->lock);
6d74119f
JB
3262 return 0;
3263 } else if (space_info->chunk_alloc) {
3264 wait_for_alloc = 1;
3265 } else {
3266 space_info->chunk_alloc = 1;
9ed74f2d 3267 }
0e4f8f88 3268
25179201 3269 spin_unlock(&space_info->lock);
9ed74f2d 3270
6d74119f
JB
3271 mutex_lock(&fs_info->chunk_mutex);
3272
3273 /*
3274 * The chunk_mutex is held throughout the entirety of a chunk
3275 * allocation, so once we've acquired the chunk_mutex we know that the
3276 * other guy is done and we need to recheck and see if we should
3277 * allocate.
3278 */
3279 if (wait_for_alloc) {
3280 mutex_unlock(&fs_info->chunk_mutex);
3281 wait_for_alloc = 0;
3282 goto again;
3283 }
3284
67377734
JB
3285 /*
3286 * If we have mixed data/metadata chunks we want to make sure we keep
3287 * allocating mixed chunks instead of individual chunks.
3288 */
3289 if (btrfs_mixed_space_info(space_info))
3290 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3291
97e728d4
JB
3292 /*
3293 * if we're doing a data chunk, go ahead and make sure that
3294 * we keep a reasonable number of metadata chunks allocated in the
3295 * FS as well.
3296 */
9ed74f2d 3297 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
97e728d4
JB
3298 fs_info->data_chunk_allocations++;
3299 if (!(fs_info->data_chunk_allocations %
3300 fs_info->metadata_ratio))
3301 force_metadata_allocation(fs_info);
9ed74f2d
JB
3302 }
3303
2b82032c 3304 ret = btrfs_alloc_chunk(trans, extent_root, flags);
92b8e897
MF
3305 if (ret < 0 && ret != -ENOSPC)
3306 goto out;
3307
9ed74f2d 3308 spin_lock(&space_info->lock);
9ed74f2d 3309 if (ret)
6324fbf3 3310 space_info->full = 1;
424499db
YZ
3311 else
3312 ret = 1;
6d74119f 3313
0e4f8f88 3314 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
6d74119f 3315 space_info->chunk_alloc = 0;
9ed74f2d 3316 spin_unlock(&space_info->lock);
92b8e897 3317out:
c146afad 3318 mutex_unlock(&extent_root->fs_info->chunk_mutex);
0f9dd46c 3319 return ret;
6324fbf3 3320}
9ed74f2d 3321
9ed74f2d 3322/*
5da9d01b 3323 * shrink metadata reservation for delalloc
9ed74f2d 3324 */
5da9d01b 3325static int shrink_delalloc(struct btrfs_trans_handle *trans,
0019f10d 3326 struct btrfs_root *root, u64 to_reclaim, int sync)
5da9d01b 3327{
0ca1f7ce 3328 struct btrfs_block_rsv *block_rsv;
0019f10d 3329 struct btrfs_space_info *space_info;
5da9d01b
YZ
3330 u64 reserved;
3331 u64 max_reclaim;
3332 u64 reclaimed = 0;
b1953bce 3333 long time_left;
bf9022e0 3334 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
b1953bce 3335 int loops = 0;
36e39c40 3336 unsigned long progress;
5da9d01b 3337
0ca1f7ce 3338 block_rsv = &root->fs_info->delalloc_block_rsv;
0019f10d 3339 space_info = block_rsv->space_info;
bf9022e0
CM
3340
3341 smp_mb();
fb25e914 3342 reserved = space_info->bytes_may_use;
36e39c40 3343 progress = space_info->reservation_progress;
5da9d01b
YZ
3344
3345 if (reserved == 0)
3346 return 0;
c4f675cd 3347
fdb5effd
JB
3348 smp_mb();
3349 if (root->fs_info->delalloc_bytes == 0) {
3350 if (trans)
3351 return 0;
3352 btrfs_wait_ordered_extents(root, 0, 0);
3353 return 0;
3354 }
3355
5da9d01b
YZ
3356 max_reclaim = min(reserved, to_reclaim);
3357
b1953bce 3358 while (loops < 1024) {
bf9022e0
CM
3359 /* have the flusher threads jump in and do some IO */
3360 smp_mb();
3361 nr_pages = min_t(unsigned long, nr_pages,
3362 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3363 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
5da9d01b 3364
0019f10d 3365 spin_lock(&space_info->lock);
fb25e914
JB
3366 if (reserved > space_info->bytes_may_use)
3367 reclaimed += reserved - space_info->bytes_may_use;
3368 reserved = space_info->bytes_may_use;
0019f10d 3369 spin_unlock(&space_info->lock);
5da9d01b 3370
36e39c40
CM
3371 loops++;
3372
5da9d01b
YZ
3373 if (reserved == 0 || reclaimed >= max_reclaim)
3374 break;
3375
3376 if (trans && trans->transaction->blocked)
3377 return -EAGAIN;
bf9022e0 3378
36e39c40 3379 time_left = schedule_timeout_interruptible(1);
b1953bce
JB
3380
3381 /* We were interrupted, exit */
3382 if (time_left)
3383 break;
3384
36e39c40
CM
3385 /* we've kicked the IO a few times, if anything has been freed,
3386 * exit. There is no sense in looping here for a long time
3387 * when we really need to commit the transaction, or there are
3388 * just too many writers without enough free space
3389 */
3390
3391 if (loops > 3) {
3392 smp_mb();
3393 if (progress != space_info->reservation_progress)
3394 break;
3395 }
bf9022e0 3396
5da9d01b 3397 }
fdb5effd
JB
3398 if (reclaimed >= to_reclaim && !trans)
3399 btrfs_wait_ordered_extents(root, 0, 0);
5da9d01b
YZ
3400 return reclaimed >= to_reclaim;
3401}
3402
8bb8ab2e
JB
3403/*
3404 * Retries tells us how many times we've called reserve_metadata_bytes. The
3405 * idea is if this is the first call (retries == 0) then we will add to our
3406 * reserved count if we can't make the allocation in order to hold our place
3407 * while we go and try and free up space. That way for retries > 1 we don't try
3408 * and add space, we just check to see if the amount of unused space is >= the
3409 * total space, meaning that our reservation is valid.
3410 *
3411 * However if we don't intend to retry this reservation, pass -1 as retries so
3412 * that it short circuits this logic.
3413 */
3414static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root,
3416 struct btrfs_block_rsv *block_rsv,
3417 u64 orig_bytes, int flush)
9ed74f2d 3418{
f0486c68 3419 struct btrfs_space_info *space_info = block_rsv->space_info;
8bb8ab2e
JB
3420 u64 unused;
3421 u64 num_bytes = orig_bytes;
3422 int retries = 0;
3423 int ret = 0;
38227933 3424 bool committed = false;
fdb5effd 3425 bool flushing = false;
8bb8ab2e 3426again:
fdb5effd 3427 ret = 0;
8bb8ab2e 3428 spin_lock(&space_info->lock);
fdb5effd
JB
3429 /*
3430 * We only want to wait if somebody other than us is flushing and we are
3431 * actually alloed to flush.
3432 */
3433 while (flush && !flushing && space_info->flush) {
3434 spin_unlock(&space_info->lock);
3435 /*
3436 * If we have a trans handle we can't wait because the flusher
3437 * may have to commit the transaction, which would mean we would
3438 * deadlock since we are waiting for the flusher to finish, but
3439 * hold the current transaction open.
3440 */
3441 if (trans)
3442 return -EAGAIN;
3443 ret = wait_event_interruptible(space_info->wait,
3444 !space_info->flush);
3445 /* Must have been interrupted, return */
3446 if (ret)
3447 return -EINTR;
3448
3449 spin_lock(&space_info->lock);
3450 }
3451
3452 ret = -ENOSPC;
8bb8ab2e
JB
3453 unused = space_info->bytes_used + space_info->bytes_reserved +
3454 space_info->bytes_pinned + space_info->bytes_readonly +
3455 space_info->bytes_may_use;
9ed74f2d 3456
8bb8ab2e
JB
3457 /*
3458 * The idea here is that we've not already over-reserved the block group
3459 * then we can go ahead and save our reservation first and then start
3460 * flushing if we need to. Otherwise if we've already overcommitted
3461 * lets start flushing stuff first and then come back and try to make
3462 * our reservation.
3463 */
3464 if (unused <= space_info->total_bytes) {
6f334348 3465 unused = space_info->total_bytes - unused;
8bb8ab2e 3466 if (unused >= num_bytes) {
fb25e914 3467 space_info->bytes_may_use += orig_bytes;
8bb8ab2e
JB
3468 ret = 0;
3469 } else {
3470 /*
3471 * Ok set num_bytes to orig_bytes since we aren't
3472 * overocmmitted, this way we only try and reclaim what
3473 * we need.
3474 */
3475 num_bytes = orig_bytes;
3476 }
3477 } else {
3478 /*
3479 * Ok we're over committed, set num_bytes to the overcommitted
3480 * amount plus the amount of bytes that we need for this
3481 * reservation.
3482 */
3483 num_bytes = unused - space_info->total_bytes +
3484 (orig_bytes * (retries + 1));
3485 }
9ed74f2d 3486
8bb8ab2e
JB
3487 /*
3488 * Couldn't make our reservation, save our place so while we're trying
3489 * to reclaim space we can actually use it instead of somebody else
3490 * stealing it from us.
3491 */
fdb5effd
JB
3492 if (ret && flush) {
3493 flushing = true;
3494 space_info->flush = 1;
8bb8ab2e 3495 }
9ed74f2d 3496
f0486c68 3497 spin_unlock(&space_info->lock);
9ed74f2d 3498
fdb5effd 3499 if (!ret || !flush)
8bb8ab2e 3500 goto out;
f0486c68 3501
8bb8ab2e
JB
3502 /*
3503 * We do synchronous shrinking since we don't actually unreserve
3504 * metadata until after the IO is completed.
3505 */
3506 ret = shrink_delalloc(trans, root, num_bytes, 1);
fdb5effd 3507 if (ret < 0)
8bb8ab2e 3508 goto out;
f0486c68 3509
75c195a2
CM
3510 ret = 0;
3511
8bb8ab2e
JB
3512 /*
3513 * So if we were overcommitted it's possible that somebody else flushed
3514 * out enough space and we simply didn't have enough space to reclaim,
3515 * so go back around and try again.
3516 */
3517 if (retries < 2) {
3518 retries++;
3519 goto again;
3520 }
f0486c68 3521
8bb8ab2e
JB
3522 /*
3523 * Not enough space to be reclaimed, don't bother committing the
3524 * transaction.
3525 */
fdb5effd 3526 spin_lock(&space_info->lock);
8bb8ab2e
JB
3527 if (space_info->bytes_pinned < orig_bytes)
3528 ret = -ENOSPC;
3529 spin_unlock(&space_info->lock);
3530 if (ret)
3531 goto out;
f0486c68 3532
8bb8ab2e 3533 ret = -EAGAIN;
75c195a2 3534 if (trans)
8bb8ab2e 3535 goto out;
f0486c68 3536
8bb8ab2e 3537 ret = -ENOSPC;
75c195a2
CM
3538 if (committed)
3539 goto out;
3540
7a7eaa40 3541 trans = btrfs_join_transaction(root);
8bb8ab2e
JB
3542 if (IS_ERR(trans))
3543 goto out;
3544 ret = btrfs_commit_transaction(trans, root);
38227933
JB
3545 if (!ret) {
3546 trans = NULL;
3547 committed = true;
8bb8ab2e 3548 goto again;
38227933 3549 }
8bb8ab2e
JB
3550
3551out:
fdb5effd 3552 if (flushing) {
8bb8ab2e 3553 spin_lock(&space_info->lock);
fdb5effd
JB
3554 space_info->flush = 0;
3555 wake_up_all(&space_info->wait);
8bb8ab2e 3556 spin_unlock(&space_info->lock);
f0486c68 3557 }
f0486c68
YZ
3558 return ret;
3559}
3560
3561static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3562 struct btrfs_root *root)
3563{
3564 struct btrfs_block_rsv *block_rsv;
3565 if (root->ref_cows)
3566 block_rsv = trans->block_rsv;
3567 else
3568 block_rsv = root->block_rsv;
3569
3570 if (!block_rsv)
3571 block_rsv = &root->fs_info->empty_block_rsv;
3572
3573 return block_rsv;
3574}
3575
3576static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3577 u64 num_bytes)
3578{
3579 int ret = -ENOSPC;
3580 spin_lock(&block_rsv->lock);
3581 if (block_rsv->reserved >= num_bytes) {
3582 block_rsv->reserved -= num_bytes;
3583 if (block_rsv->reserved < block_rsv->size)
3584 block_rsv->full = 0;
3585 ret = 0;
3586 }
3587 spin_unlock(&block_rsv->lock);
3588 return ret;
3589}
3590
3591static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3592 u64 num_bytes, int update_size)
3593{
3594 spin_lock(&block_rsv->lock);
3595 block_rsv->reserved += num_bytes;
3596 if (update_size)
3597 block_rsv->size += num_bytes;
3598 else if (block_rsv->reserved >= block_rsv->size)
3599 block_rsv->full = 1;
3600 spin_unlock(&block_rsv->lock);
3601}
3602
62a45b60
DS
3603static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3604 struct btrfs_block_rsv *dest, u64 num_bytes)
f0486c68
YZ
3605{
3606 struct btrfs_space_info *space_info = block_rsv->space_info;
3607
3608 spin_lock(&block_rsv->lock);
3609 if (num_bytes == (u64)-1)
3610 num_bytes = block_rsv->size;
3611 block_rsv->size -= num_bytes;
3612 if (block_rsv->reserved >= block_rsv->size) {
3613 num_bytes = block_rsv->reserved - block_rsv->size;
3614 block_rsv->reserved = block_rsv->size;
3615 block_rsv->full = 1;
3616 } else {
3617 num_bytes = 0;
3618 }
3619 spin_unlock(&block_rsv->lock);
3620
3621 if (num_bytes > 0) {
3622 if (dest) {
e9e22899
JB
3623 spin_lock(&dest->lock);
3624 if (!dest->full) {
3625 u64 bytes_to_add;
3626
3627 bytes_to_add = dest->size - dest->reserved;
3628 bytes_to_add = min(num_bytes, bytes_to_add);
3629 dest->reserved += bytes_to_add;
3630 if (dest->reserved >= dest->size)
3631 dest->full = 1;
3632 num_bytes -= bytes_to_add;
3633 }
3634 spin_unlock(&dest->lock);
3635 }
3636 if (num_bytes) {
f0486c68 3637 spin_lock(&space_info->lock);
fb25e914 3638 space_info->bytes_may_use -= num_bytes;
36e39c40 3639 space_info->reservation_progress++;
f0486c68 3640 spin_unlock(&space_info->lock);
4e06bdd6 3641 }
9ed74f2d 3642 }
f0486c68 3643}
4e06bdd6 3644
f0486c68
YZ
3645static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3646 struct btrfs_block_rsv *dst, u64 num_bytes)
3647{
3648 int ret;
9ed74f2d 3649
f0486c68
YZ
3650 ret = block_rsv_use_bytes(src, num_bytes);
3651 if (ret)
3652 return ret;
9ed74f2d 3653
f0486c68 3654 block_rsv_add_bytes(dst, num_bytes, 1);
9ed74f2d
JB
3655 return 0;
3656}
3657
f0486c68 3658void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
9ed74f2d 3659{
f0486c68
YZ
3660 memset(rsv, 0, sizeof(*rsv));
3661 spin_lock_init(&rsv->lock);
f0486c68
YZ
3662}
3663
3664struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3665{
3666 struct btrfs_block_rsv *block_rsv;
3667 struct btrfs_fs_info *fs_info = root->fs_info;
9ed74f2d 3668
f0486c68
YZ
3669 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3670 if (!block_rsv)
3671 return NULL;
9ed74f2d 3672
f0486c68 3673 btrfs_init_block_rsv(block_rsv);
f0486c68
YZ
3674 block_rsv->space_info = __find_space_info(fs_info,
3675 BTRFS_BLOCK_GROUP_METADATA);
f0486c68
YZ
3676 return block_rsv;
3677}
9ed74f2d 3678
f0486c68
YZ
3679void btrfs_free_block_rsv(struct btrfs_root *root,
3680 struct btrfs_block_rsv *rsv)
3681{
dabdb640
JB
3682 btrfs_block_rsv_release(root, rsv, (u64)-1);
3683 kfree(rsv);
9ed74f2d
JB
3684}
3685
f0486c68
YZ
3686int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3687 struct btrfs_root *root,
3688 struct btrfs_block_rsv *block_rsv,
8bb8ab2e 3689 u64 num_bytes)
f0486c68
YZ
3690{
3691 int ret;
9ed74f2d 3692
f0486c68
YZ
3693 if (num_bytes == 0)
3694 return 0;
8bb8ab2e
JB
3695
3696 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
f0486c68
YZ
3697 if (!ret) {
3698 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3699 return 0;
3700 }
9ed74f2d 3701
f0486c68
YZ
3702 return ret;
3703}
9ed74f2d 3704
f0486c68
YZ
3705int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3706 struct btrfs_root *root,
3707 struct btrfs_block_rsv *block_rsv,
482e6dc5 3708 u64 min_reserved, int min_factor, int flush)
f0486c68
YZ
3709{
3710 u64 num_bytes = 0;
f0486c68 3711 int ret = -ENOSPC;
9ed74f2d 3712
f0486c68
YZ
3713 if (!block_rsv)
3714 return 0;
9ed74f2d 3715
f0486c68
YZ
3716 spin_lock(&block_rsv->lock);
3717 if (min_factor > 0)
3718 num_bytes = div_factor(block_rsv->size, min_factor);
3719 if (min_reserved > num_bytes)
3720 num_bytes = min_reserved;
9ed74f2d 3721
13553e52 3722 if (block_rsv->reserved >= num_bytes)
f0486c68 3723 ret = 0;
13553e52 3724 else
f0486c68 3725 num_bytes -= block_rsv->reserved;
f0486c68 3726 spin_unlock(&block_rsv->lock);
13553e52 3727
f0486c68
YZ
3728 if (!ret)
3729 return 0;
3730
482e6dc5 3731 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, flush);
dabdb640
JB
3732 if (!ret) {
3733 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3734 return 0;
f0486c68 3735 }
9ed74f2d 3736
13553e52 3737 return ret;
f0486c68
YZ
3738}
3739
3740int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3741 struct btrfs_block_rsv *dst_rsv,
3742 u64 num_bytes)
3743{
3744 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3745}
3746
3747void btrfs_block_rsv_release(struct btrfs_root *root,
3748 struct btrfs_block_rsv *block_rsv,
3749 u64 num_bytes)
3750{
3751 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3752 if (global_rsv->full || global_rsv == block_rsv ||
3753 block_rsv->space_info != global_rsv->space_info)
3754 global_rsv = NULL;
3755 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
6a63209f
JB
3756}
3757
3758/*
8929ecfa
YZ
3759 * helper to calculate size of global block reservation.
3760 * the desired value is sum of space used by extent tree,
3761 * checksum tree and root tree
6a63209f 3762 */
8929ecfa 3763static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
6a63209f 3764{
8929ecfa
YZ
3765 struct btrfs_space_info *sinfo;
3766 u64 num_bytes;
3767 u64 meta_used;
3768 u64 data_used;
3769 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
6a63209f 3770
8929ecfa
YZ
3771 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3772 spin_lock(&sinfo->lock);
3773 data_used = sinfo->bytes_used;
3774 spin_unlock(&sinfo->lock);
33b4d47f 3775
8929ecfa
YZ
3776 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3777 spin_lock(&sinfo->lock);
6d48755d
JB
3778 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3779 data_used = 0;
8929ecfa
YZ
3780 meta_used = sinfo->bytes_used;
3781 spin_unlock(&sinfo->lock);
ab6e2410 3782
8929ecfa
YZ
3783 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3784 csum_size * 2;
3785 num_bytes += div64_u64(data_used + meta_used, 50);
4e06bdd6 3786
8929ecfa
YZ
3787 if (num_bytes * 3 > meta_used)
3788 num_bytes = div64_u64(meta_used, 3);
ab6e2410 3789
8929ecfa
YZ
3790 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3791}
6a63209f 3792
8929ecfa
YZ
3793static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3794{
3795 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3796 struct btrfs_space_info *sinfo = block_rsv->space_info;
3797 u64 num_bytes;
6a63209f 3798
8929ecfa 3799 num_bytes = calc_global_metadata_size(fs_info);
33b4d47f 3800
8929ecfa
YZ
3801 spin_lock(&block_rsv->lock);
3802 spin_lock(&sinfo->lock);
4e06bdd6 3803
8929ecfa 3804 block_rsv->size = num_bytes;
4e06bdd6 3805
8929ecfa 3806 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
6d48755d
JB
3807 sinfo->bytes_reserved + sinfo->bytes_readonly +
3808 sinfo->bytes_may_use;
8929ecfa
YZ
3809
3810 if (sinfo->total_bytes > num_bytes) {
3811 num_bytes = sinfo->total_bytes - num_bytes;
3812 block_rsv->reserved += num_bytes;
fb25e914 3813 sinfo->bytes_may_use += num_bytes;
6a63209f 3814 }
6a63209f 3815
8929ecfa
YZ
3816 if (block_rsv->reserved >= block_rsv->size) {
3817 num_bytes = block_rsv->reserved - block_rsv->size;
fb25e914 3818 sinfo->bytes_may_use -= num_bytes;
36e39c40 3819 sinfo->reservation_progress++;
8929ecfa
YZ
3820 block_rsv->reserved = block_rsv->size;
3821 block_rsv->full = 1;
3822 }
182608c8 3823
8929ecfa
YZ
3824 spin_unlock(&sinfo->lock);
3825 spin_unlock(&block_rsv->lock);
6a63209f
JB
3826}
3827
f0486c68 3828static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
6a63209f 3829{
f0486c68 3830 struct btrfs_space_info *space_info;
6a63209f 3831
f0486c68
YZ
3832 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3833 fs_info->chunk_block_rsv.space_info = space_info;
6a63209f 3834
f0486c68 3835 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
8929ecfa 3836 fs_info->global_block_rsv.space_info = space_info;
8929ecfa 3837 fs_info->delalloc_block_rsv.space_info = space_info;
f0486c68
YZ
3838 fs_info->trans_block_rsv.space_info = space_info;
3839 fs_info->empty_block_rsv.space_info = space_info;
f0486c68 3840
8929ecfa
YZ
3841 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3842 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3843 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3844 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
f0486c68 3845 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
8929ecfa 3846
8929ecfa 3847 update_global_block_rsv(fs_info);
6a63209f
JB
3848}
3849
8929ecfa 3850static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
6a63209f 3851{
8929ecfa
YZ
3852 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3853 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3854 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3855 WARN_ON(fs_info->trans_block_rsv.size > 0);
3856 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3857 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3858 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
f0486c68 3859}
6a63209f 3860
a22285a6
YZ
3861void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root)
6a63209f 3863{
a22285a6
YZ
3864 if (!trans->bytes_reserved)
3865 return;
6a63209f 3866
a22285a6
YZ
3867 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3868 btrfs_block_rsv_release(root, trans->block_rsv,
3869 trans->bytes_reserved);
3870 trans->bytes_reserved = 0;
3871}
6a63209f 3872
d68fc57b
YZ
3873int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3874 struct inode *inode)
3875{
3876 struct btrfs_root *root = BTRFS_I(inode)->root;
3877 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3878 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3879
3880 /*
fcb80c2a
JB
3881 * We need to hold space in order to delete our orphan item once we've
3882 * added it, so this takes the reservation so we can release it later
3883 * when we are truly done with the orphan item.
d68fc57b 3884 */
ff5714cc 3885 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
d68fc57b 3886 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
6a63209f
JB
3887}
3888
d68fc57b 3889void btrfs_orphan_release_metadata(struct inode *inode)
97e728d4 3890{
d68fc57b 3891 struct btrfs_root *root = BTRFS_I(inode)->root;
ff5714cc 3892 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
d68fc57b
YZ
3893 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3894}
97e728d4 3895
a22285a6
YZ
3896int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3897 struct btrfs_pending_snapshot *pending)
3898{
3899 struct btrfs_root *root = pending->root;
3900 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3901 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3902 /*
3903 * two for root back/forward refs, two for directory entries
3904 * and one for root of the snapshot.
3905 */
16cdcec7 3906 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
a22285a6
YZ
3907 dst_rsv->space_info = src_rsv->space_info;
3908 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
97e728d4
JB
3909}
3910
7709cde3
JB
3911/**
3912 * drop_outstanding_extent - drop an outstanding extent
3913 * @inode: the inode we're dropping the extent for
3914 *
3915 * This is called when we are freeing up an outstanding extent, either called
3916 * after an error or after an extent is written. This will return the number of
3917 * reserved extents that need to be freed. This must be called with
3918 * BTRFS_I(inode)->lock held.
3919 */
9e0baf60
JB
3920static unsigned drop_outstanding_extent(struct inode *inode)
3921{
3922 unsigned dropped_extents = 0;
3923
9e0baf60
JB
3924 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
3925 BTRFS_I(inode)->outstanding_extents--;
3926
3927 /*
3928 * If we have more or the same amount of outsanding extents than we have
3929 * reserved then we need to leave the reserved extents count alone.
3930 */
3931 if (BTRFS_I(inode)->outstanding_extents >=
3932 BTRFS_I(inode)->reserved_extents)
7709cde3 3933 return 0;
9e0baf60
JB
3934
3935 dropped_extents = BTRFS_I(inode)->reserved_extents -
3936 BTRFS_I(inode)->outstanding_extents;
3937 BTRFS_I(inode)->reserved_extents -= dropped_extents;
9e0baf60
JB
3938 return dropped_extents;
3939}
3940
7709cde3
JB
3941/**
3942 * calc_csum_metadata_size - return the amount of metada space that must be
3943 * reserved/free'd for the given bytes.
3944 * @inode: the inode we're manipulating
3945 * @num_bytes: the number of bytes in question
3946 * @reserve: 1 if we are reserving space, 0 if we are freeing space
3947 *
3948 * This adjusts the number of csum_bytes in the inode and then returns the
3949 * correct amount of metadata that must either be reserved or freed. We
3950 * calculate how many checksums we can fit into one leaf and then divide the
3951 * number of bytes that will need to be checksumed by this value to figure out
3952 * how many checksums will be required. If we are adding bytes then the number
3953 * may go up and we will return the number of additional bytes that must be
3954 * reserved. If it is going down we will return the number of bytes that must
3955 * be freed.
3956 *
3957 * This must be called with BTRFS_I(inode)->lock held.
3958 */
3959static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
3960 int reserve)
6324fbf3 3961{
7709cde3
JB
3962 struct btrfs_root *root = BTRFS_I(inode)->root;
3963 u64 csum_size;
3964 int num_csums_per_leaf;
3965 int num_csums;
3966 int old_csums;
3967
3968 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
3969 BTRFS_I(inode)->csum_bytes == 0)
3970 return 0;
3971
3972 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
3973 if (reserve)
3974 BTRFS_I(inode)->csum_bytes += num_bytes;
3975 else
3976 BTRFS_I(inode)->csum_bytes -= num_bytes;
3977 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
3978 num_csums_per_leaf = (int)div64_u64(csum_size,
3979 sizeof(struct btrfs_csum_item) +
3980 sizeof(struct btrfs_disk_key));
3981 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
3982 num_csums = num_csums + num_csums_per_leaf - 1;
3983 num_csums = num_csums / num_csums_per_leaf;
3984
3985 old_csums = old_csums + num_csums_per_leaf - 1;
3986 old_csums = old_csums / num_csums_per_leaf;
3987
3988 /* No change, no need to reserve more */
3989 if (old_csums == num_csums)
3990 return 0;
3991
3992 if (reserve)
3993 return btrfs_calc_trans_metadata_size(root,
3994 num_csums - old_csums);
3995
3996 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
0ca1f7ce 3997}
c146afad 3998
0ca1f7ce
YZ
3999int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4000{
4001 struct btrfs_root *root = BTRFS_I(inode)->root;
4002 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
9e0baf60
JB
4003 u64 to_reserve = 0;
4004 unsigned nr_extents = 0;
0ca1f7ce 4005 int ret;
6324fbf3 4006
0ca1f7ce
YZ
4007 if (btrfs_transaction_in_commit(root->fs_info))
4008 schedule_timeout(1);
ec44a35c 4009
0ca1f7ce 4010 num_bytes = ALIGN(num_bytes, root->sectorsize);
8bb8ab2e 4011
9e0baf60
JB
4012 spin_lock(&BTRFS_I(inode)->lock);
4013 BTRFS_I(inode)->outstanding_extents++;
4014
4015 if (BTRFS_I(inode)->outstanding_extents >
4016 BTRFS_I(inode)->reserved_extents) {
4017 nr_extents = BTRFS_I(inode)->outstanding_extents -
4018 BTRFS_I(inode)->reserved_extents;
4019 BTRFS_I(inode)->reserved_extents += nr_extents;
57a45ced 4020
16cdcec7 4021 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
593060d7 4022 }
7709cde3 4023 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
9e0baf60 4024 spin_unlock(&BTRFS_I(inode)->lock);
57a45ced 4025
8bb8ab2e 4026 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
9e0baf60 4027 if (ret) {
7ed49f18 4028 u64 to_free = 0;
9e0baf60 4029 unsigned dropped;
7ed49f18 4030
7709cde3 4031 spin_lock(&BTRFS_I(inode)->lock);
9e0baf60 4032 dropped = drop_outstanding_extent(inode);
7ed49f18 4033 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
7709cde3 4034 spin_unlock(&BTRFS_I(inode)->lock);
7ed49f18
JB
4035 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4036
4037 /*
4038 * Somebody could have come in and twiddled with the
4039 * reservation, so if we have to free more than we would have
4040 * reserved from this reservation go ahead and release those
4041 * bytes.
4042 */
4043 to_free -= to_reserve;
4044 if (to_free)
4045 btrfs_block_rsv_release(root, block_rsv, to_free);
0ca1f7ce 4046 return ret;
9e0baf60 4047 }
25179201 4048
0ca1f7ce
YZ
4049 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4050
0ca1f7ce
YZ
4051 return 0;
4052}
4053
7709cde3
JB
4054/**
4055 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4056 * @inode: the inode to release the reservation for
4057 * @num_bytes: the number of bytes we're releasing
4058 *
4059 * This will release the metadata reservation for an inode. This can be called
4060 * once we complete IO for a given set of bytes to release their metadata
4061 * reservations.
4062 */
0ca1f7ce
YZ
4063void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4064{
4065 struct btrfs_root *root = BTRFS_I(inode)->root;
9e0baf60
JB
4066 u64 to_free = 0;
4067 unsigned dropped;
0ca1f7ce
YZ
4068
4069 num_bytes = ALIGN(num_bytes, root->sectorsize);
7709cde3 4070 spin_lock(&BTRFS_I(inode)->lock);
9e0baf60 4071 dropped = drop_outstanding_extent(inode);
97e728d4 4072
7709cde3
JB
4073 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4074 spin_unlock(&BTRFS_I(inode)->lock);
9e0baf60
JB
4075 if (dropped > 0)
4076 to_free += btrfs_calc_trans_metadata_size(root, dropped);
0ca1f7ce
YZ
4077
4078 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4079 to_free);
4080}
4081
7709cde3
JB
4082/**
4083 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4084 * @inode: inode we're writing to
4085 * @num_bytes: the number of bytes we want to allocate
4086 *
4087 * This will do the following things
4088 *
4089 * o reserve space in the data space info for num_bytes
4090 * o reserve space in the metadata space info based on number of outstanding
4091 * extents and how much csums will be needed
4092 * o add to the inodes ->delalloc_bytes
4093 * o add it to the fs_info's delalloc inodes list.
4094 *
4095 * This will return 0 for success and -ENOSPC if there is no space left.
4096 */
0ca1f7ce
YZ
4097int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4098{
4099 int ret;
4100
4101 ret = btrfs_check_data_free_space(inode, num_bytes);
d397712b 4102 if (ret)
0ca1f7ce
YZ
4103 return ret;
4104
4105 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4106 if (ret) {
4107 btrfs_free_reserved_data_space(inode, num_bytes);
4108 return ret;
4109 }
4110
4111 return 0;
4112}
4113
7709cde3
JB
4114/**
4115 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4116 * @inode: inode we're releasing space for
4117 * @num_bytes: the number of bytes we want to free up
4118 *
4119 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4120 * called in the case that we don't need the metadata AND data reservations
4121 * anymore. So if there is an error or we insert an inline extent.
4122 *
4123 * This function will release the metadata space that was not used and will
4124 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4125 * list if there are no delalloc bytes left.
4126 */
0ca1f7ce
YZ
4127void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4128{
4129 btrfs_delalloc_release_metadata(inode, num_bytes);
4130 btrfs_free_reserved_data_space(inode, num_bytes);
6324fbf3
CM
4131}
4132
9078a3e1
CM
4133static int update_block_group(struct btrfs_trans_handle *trans,
4134 struct btrfs_root *root,
f0486c68 4135 u64 bytenr, u64 num_bytes, int alloc)
9078a3e1 4136{
0af3d00b 4137 struct btrfs_block_group_cache *cache = NULL;
9078a3e1 4138 struct btrfs_fs_info *info = root->fs_info;
db94535d 4139 u64 total = num_bytes;
9078a3e1 4140 u64 old_val;
db94535d 4141 u64 byte_in_group;
0af3d00b 4142 int factor;
3e1ad54f 4143
5d4f98a2
YZ
4144 /* block accounting for super block */
4145 spin_lock(&info->delalloc_lock);
4146 old_val = btrfs_super_bytes_used(&info->super_copy);
4147 if (alloc)
4148 old_val += num_bytes;
4149 else
4150 old_val -= num_bytes;
4151 btrfs_set_super_bytes_used(&info->super_copy, old_val);
5d4f98a2
YZ
4152 spin_unlock(&info->delalloc_lock);
4153
d397712b 4154 while (total) {
db94535d 4155 cache = btrfs_lookup_block_group(info, bytenr);
f3465ca4 4156 if (!cache)
9078a3e1 4157 return -1;
b742bb82
YZ
4158 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4159 BTRFS_BLOCK_GROUP_RAID1 |
4160 BTRFS_BLOCK_GROUP_RAID10))
4161 factor = 2;
4162 else
4163 factor = 1;
9d66e233
JB
4164 /*
4165 * If this block group has free space cache written out, we
4166 * need to make sure to load it if we are removing space. This
4167 * is because we need the unpinning stage to actually add the
4168 * space back to the block group, otherwise we will leak space.
4169 */
4170 if (!alloc && cache->cached == BTRFS_CACHE_NO)
b8399dee 4171 cache_block_group(cache, trans, NULL, 1);
0af3d00b 4172
db94535d
CM
4173 byte_in_group = bytenr - cache->key.objectid;
4174 WARN_ON(byte_in_group > cache->key.offset);
9078a3e1 4175
25179201 4176 spin_lock(&cache->space_info->lock);
c286ac48 4177 spin_lock(&cache->lock);
0af3d00b
JB
4178
4179 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4180 cache->disk_cache_state < BTRFS_DC_CLEAR)
4181 cache->disk_cache_state = BTRFS_DC_CLEAR;
4182
0f9dd46c 4183 cache->dirty = 1;
9078a3e1 4184 old_val = btrfs_block_group_used(&cache->item);
db94535d 4185 num_bytes = min(total, cache->key.offset - byte_in_group);
cd1bc465 4186 if (alloc) {
db94535d 4187 old_val += num_bytes;
11833d66
YZ
4188 btrfs_set_block_group_used(&cache->item, old_val);
4189 cache->reserved -= num_bytes;
11833d66 4190 cache->space_info->bytes_reserved -= num_bytes;
b742bb82
YZ
4191 cache->space_info->bytes_used += num_bytes;
4192 cache->space_info->disk_used += num_bytes * factor;
c286ac48 4193 spin_unlock(&cache->lock);
25179201 4194 spin_unlock(&cache->space_info->lock);
cd1bc465 4195 } else {
db94535d 4196 old_val -= num_bytes;
c286ac48 4197 btrfs_set_block_group_used(&cache->item, old_val);
f0486c68
YZ
4198 cache->pinned += num_bytes;
4199 cache->space_info->bytes_pinned += num_bytes;
6324fbf3 4200 cache->space_info->bytes_used -= num_bytes;
b742bb82 4201 cache->space_info->disk_used -= num_bytes * factor;
c286ac48 4202 spin_unlock(&cache->lock);
25179201 4203 spin_unlock(&cache->space_info->lock);
1f3c79a2 4204
f0486c68
YZ
4205 set_extent_dirty(info->pinned_extents,
4206 bytenr, bytenr + num_bytes - 1,
4207 GFP_NOFS | __GFP_NOFAIL);
cd1bc465 4208 }
fa9c0d79 4209 btrfs_put_block_group(cache);
db94535d
CM
4210 total -= num_bytes;
4211 bytenr += num_bytes;
9078a3e1
CM
4212 }
4213 return 0;
4214}
6324fbf3 4215
a061fc8d
CM
4216static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4217{
0f9dd46c 4218 struct btrfs_block_group_cache *cache;
d2fb3437 4219 u64 bytenr;
0f9dd46c
JB
4220
4221 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4222 if (!cache)
a061fc8d 4223 return 0;
0f9dd46c 4224
d2fb3437 4225 bytenr = cache->key.objectid;
fa9c0d79 4226 btrfs_put_block_group(cache);
d2fb3437
YZ
4227
4228 return bytenr;
a061fc8d
CM
4229}
4230
f0486c68
YZ
4231static int pin_down_extent(struct btrfs_root *root,
4232 struct btrfs_block_group_cache *cache,
4233 u64 bytenr, u64 num_bytes, int reserved)
324ae4df 4234{
11833d66
YZ
4235 spin_lock(&cache->space_info->lock);
4236 spin_lock(&cache->lock);
4237 cache->pinned += num_bytes;
4238 cache->space_info->bytes_pinned += num_bytes;
4239 if (reserved) {
4240 cache->reserved -= num_bytes;
4241 cache->space_info->bytes_reserved -= num_bytes;
4242 }
4243 spin_unlock(&cache->lock);
4244 spin_unlock(&cache->space_info->lock);
68b38550 4245
f0486c68
YZ
4246 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4247 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
4248 return 0;
4249}
68b38550 4250
f0486c68
YZ
4251/*
4252 * this function must be called within transaction
4253 */
4254int btrfs_pin_extent(struct btrfs_root *root,
4255 u64 bytenr, u64 num_bytes, int reserved)
4256{
4257 struct btrfs_block_group_cache *cache;
68b38550 4258
f0486c68
YZ
4259 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4260 BUG_ON(!cache);
4261
4262 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4263
4264 btrfs_put_block_group(cache);
11833d66
YZ
4265 return 0;
4266}
4267
fb25e914
JB
4268/**
4269 * btrfs_update_reserved_bytes - update the block_group and space info counters
4270 * @cache: The cache we are manipulating
4271 * @num_bytes: The number of bytes in question
4272 * @reserve: One of the reservation enums
4273 *
4274 * This is called by the allocator when it reserves space, or by somebody who is
4275 * freeing space that was never actually used on disk. For example if you
4276 * reserve some space for a new leaf in transaction A and before transaction A
4277 * commits you free that leaf, you call this with reserve set to 0 in order to
4278 * clear the reservation.
4279 *
4280 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4281 * ENOSPC accounting. For data we handle the reservation through clearing the
4282 * delalloc bits in the io_tree. We have to do this since we could end up
4283 * allocating less disk space for the amount of data we have reserved in the
4284 * case of compression.
4285 *
4286 * If this is a reservation and the block group has become read only we cannot
4287 * make the reservation and return -EAGAIN, otherwise this function always
4288 * succeeds.
f0486c68 4289 */
fb25e914
JB
4290static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4291 u64 num_bytes, int reserve)
11833d66 4292{
fb25e914 4293 struct btrfs_space_info *space_info = cache->space_info;
f0486c68 4294 int ret = 0;
fb25e914
JB
4295 spin_lock(&space_info->lock);
4296 spin_lock(&cache->lock);
4297 if (reserve != RESERVE_FREE) {
f0486c68
YZ
4298 if (cache->ro) {
4299 ret = -EAGAIN;
4300 } else {
fb25e914
JB
4301 cache->reserved += num_bytes;
4302 space_info->bytes_reserved += num_bytes;
4303 if (reserve == RESERVE_ALLOC) {
4304 BUG_ON(space_info->bytes_may_use < num_bytes);
4305 space_info->bytes_may_use -= num_bytes;
4306 }
f0486c68 4307 }
fb25e914
JB
4308 } else {
4309 if (cache->ro)
4310 space_info->bytes_readonly += num_bytes;
4311 cache->reserved -= num_bytes;
4312 space_info->bytes_reserved -= num_bytes;
4313 space_info->reservation_progress++;
324ae4df 4314 }
fb25e914
JB
4315 spin_unlock(&cache->lock);
4316 spin_unlock(&space_info->lock);
f0486c68 4317 return ret;
324ae4df 4318}
9078a3e1 4319
11833d66
YZ
4320int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4321 struct btrfs_root *root)
e8569813 4322{
e8569813 4323 struct btrfs_fs_info *fs_info = root->fs_info;
11833d66
YZ
4324 struct btrfs_caching_control *next;
4325 struct btrfs_caching_control *caching_ctl;
4326 struct btrfs_block_group_cache *cache;
e8569813 4327
11833d66 4328 down_write(&fs_info->extent_commit_sem);
25179201 4329
11833d66
YZ
4330 list_for_each_entry_safe(caching_ctl, next,
4331 &fs_info->caching_block_groups, list) {
4332 cache = caching_ctl->block_group;
4333 if (block_group_cache_done(cache)) {
4334 cache->last_byte_to_unpin = (u64)-1;
4335 list_del_init(&caching_ctl->list);
4336 put_caching_control(caching_ctl);
e8569813 4337 } else {
11833d66 4338 cache->last_byte_to_unpin = caching_ctl->progress;
e8569813 4339 }
e8569813 4340 }
11833d66
YZ
4341
4342 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4343 fs_info->pinned_extents = &fs_info->freed_extents[1];
4344 else
4345 fs_info->pinned_extents = &fs_info->freed_extents[0];
4346
4347 up_write(&fs_info->extent_commit_sem);
8929ecfa
YZ
4348
4349 update_global_block_rsv(fs_info);
e8569813
ZY
4350 return 0;
4351}
4352
11833d66 4353static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
ccd467d6 4354{
11833d66
YZ
4355 struct btrfs_fs_info *fs_info = root->fs_info;
4356 struct btrfs_block_group_cache *cache = NULL;
4357 u64 len;
ccd467d6 4358
11833d66
YZ
4359 while (start <= end) {
4360 if (!cache ||
4361 start >= cache->key.objectid + cache->key.offset) {
4362 if (cache)
4363 btrfs_put_block_group(cache);
4364 cache = btrfs_lookup_block_group(fs_info, start);
4365 BUG_ON(!cache);
4366 }
4367
4368 len = cache->key.objectid + cache->key.offset - start;
4369 len = min(len, end + 1 - start);
4370
4371 if (start < cache->last_byte_to_unpin) {
4372 len = min(len, cache->last_byte_to_unpin - start);
4373 btrfs_add_free_space(cache, start, len);
4374 }
4375
f0486c68
YZ
4376 start += len;
4377
11833d66
YZ
4378 spin_lock(&cache->space_info->lock);
4379 spin_lock(&cache->lock);
4380 cache->pinned -= len;
4381 cache->space_info->bytes_pinned -= len;
37be25bc 4382 if (cache->ro)
f0486c68 4383 cache->space_info->bytes_readonly += len;
11833d66
YZ
4384 spin_unlock(&cache->lock);
4385 spin_unlock(&cache->space_info->lock);
ccd467d6 4386 }
11833d66
YZ
4387
4388 if (cache)
4389 btrfs_put_block_group(cache);
ccd467d6
CM
4390 return 0;
4391}
4392
4393int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
11833d66 4394 struct btrfs_root *root)
a28ec197 4395{
11833d66
YZ
4396 struct btrfs_fs_info *fs_info = root->fs_info;
4397 struct extent_io_tree *unpin;
1a5bc167
CM
4398 u64 start;
4399 u64 end;
a28ec197 4400 int ret;
a28ec197 4401
11833d66
YZ
4402 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4403 unpin = &fs_info->freed_extents[1];
4404 else
4405 unpin = &fs_info->freed_extents[0];
4406
d397712b 4407 while (1) {
1a5bc167
CM
4408 ret = find_first_extent_bit(unpin, 0, &start, &end,
4409 EXTENT_DIRTY);
4410 if (ret)
a28ec197 4411 break;
1f3c79a2 4412
5378e607
LD
4413 if (btrfs_test_opt(root, DISCARD))
4414 ret = btrfs_discard_extent(root, start,
4415 end + 1 - start, NULL);
1f3c79a2 4416
1a5bc167 4417 clear_extent_dirty(unpin, start, end, GFP_NOFS);
11833d66 4418 unpin_extent_range(root, start, end);
b9473439 4419 cond_resched();
a28ec197 4420 }
817d52f8 4421
e20d96d6
CM
4422 return 0;
4423}
4424
5d4f98a2
YZ
4425static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4426 struct btrfs_root *root,
4427 u64 bytenr, u64 num_bytes, u64 parent,
4428 u64 root_objectid, u64 owner_objectid,
4429 u64 owner_offset, int refs_to_drop,
4430 struct btrfs_delayed_extent_op *extent_op)
a28ec197 4431{
e2fa7227 4432 struct btrfs_key key;
5d4f98a2 4433 struct btrfs_path *path;
1261ec42
CM
4434 struct btrfs_fs_info *info = root->fs_info;
4435 struct btrfs_root *extent_root = info->extent_root;
5f39d397 4436 struct extent_buffer *leaf;
5d4f98a2
YZ
4437 struct btrfs_extent_item *ei;
4438 struct btrfs_extent_inline_ref *iref;
a28ec197 4439 int ret;
5d4f98a2 4440 int is_data;
952fccac
CM
4441 int extent_slot = 0;
4442 int found_extent = 0;
4443 int num_to_del = 1;
5d4f98a2
YZ
4444 u32 item_size;
4445 u64 refs;
037e6390 4446
5caf2a00 4447 path = btrfs_alloc_path();
54aa1f4d
CM
4448 if (!path)
4449 return -ENOMEM;
5f26f772 4450
3c12ac72 4451 path->reada = 1;
b9473439 4452 path->leave_spinning = 1;
5d4f98a2
YZ
4453
4454 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4455 BUG_ON(!is_data && refs_to_drop != 1);
4456
4457 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4458 bytenr, num_bytes, parent,
4459 root_objectid, owner_objectid,
4460 owner_offset);
7bb86316 4461 if (ret == 0) {
952fccac 4462 extent_slot = path->slots[0];
5d4f98a2
YZ
4463 while (extent_slot >= 0) {
4464 btrfs_item_key_to_cpu(path->nodes[0], &key,
952fccac 4465 extent_slot);
5d4f98a2 4466 if (key.objectid != bytenr)
952fccac 4467 break;
5d4f98a2
YZ
4468 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4469 key.offset == num_bytes) {
952fccac
CM
4470 found_extent = 1;
4471 break;
4472 }
4473 if (path->slots[0] - extent_slot > 5)
4474 break;
5d4f98a2 4475 extent_slot--;
952fccac 4476 }
5d4f98a2
YZ
4477#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4478 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4479 if (found_extent && item_size < sizeof(*ei))
4480 found_extent = 0;
4481#endif
31840ae1 4482 if (!found_extent) {
5d4f98a2 4483 BUG_ON(iref);
56bec294 4484 ret = remove_extent_backref(trans, extent_root, path,
5d4f98a2
YZ
4485 NULL, refs_to_drop,
4486 is_data);
31840ae1 4487 BUG_ON(ret);
b3b4aa74 4488 btrfs_release_path(path);
b9473439 4489 path->leave_spinning = 1;
5d4f98a2
YZ
4490
4491 key.objectid = bytenr;
4492 key.type = BTRFS_EXTENT_ITEM_KEY;
4493 key.offset = num_bytes;
4494
31840ae1
ZY
4495 ret = btrfs_search_slot(trans, extent_root,
4496 &key, path, -1, 1);
f3465ca4
JB
4497 if (ret) {
4498 printk(KERN_ERR "umm, got %d back from search"
d397712b
CM
4499 ", was looking for %llu\n", ret,
4500 (unsigned long long)bytenr);
b783e62d
JB
4501 if (ret > 0)
4502 btrfs_print_leaf(extent_root,
4503 path->nodes[0]);
f3465ca4 4504 }
31840ae1
ZY
4505 BUG_ON(ret);
4506 extent_slot = path->slots[0];
4507 }
7bb86316
CM
4508 } else {
4509 btrfs_print_leaf(extent_root, path->nodes[0]);
4510 WARN_ON(1);
d397712b 4511 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
5d4f98a2 4512 "parent %llu root %llu owner %llu offset %llu\n",
d397712b 4513 (unsigned long long)bytenr,
56bec294 4514 (unsigned long long)parent,
d397712b 4515 (unsigned long long)root_objectid,
5d4f98a2
YZ
4516 (unsigned long long)owner_objectid,
4517 (unsigned long long)owner_offset);
7bb86316 4518 }
5f39d397
CM
4519
4520 leaf = path->nodes[0];
5d4f98a2
YZ
4521 item_size = btrfs_item_size_nr(leaf, extent_slot);
4522#ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4523 if (item_size < sizeof(*ei)) {
4524 BUG_ON(found_extent || extent_slot != path->slots[0]);
4525 ret = convert_extent_item_v0(trans, extent_root, path,
4526 owner_objectid, 0);
4527 BUG_ON(ret < 0);
4528
b3b4aa74 4529 btrfs_release_path(path);
5d4f98a2
YZ
4530 path->leave_spinning = 1;
4531
4532 key.objectid = bytenr;
4533 key.type = BTRFS_EXTENT_ITEM_KEY;
4534 key.offset = num_bytes;
4535
4536 ret = btrfs_search_slot(trans, extent_root, &key, path,
4537 -1, 1);
4538 if (ret) {
4539 printk(KERN_ERR "umm, got %d back from search"
4540 ", was looking for %llu\n", ret,
4541 (unsigned long long)bytenr);
4542 btrfs_print_leaf(extent_root, path->nodes[0]);
4543 }
4544 BUG_ON(ret);
4545 extent_slot = path->slots[0];
4546 leaf = path->nodes[0];
4547 item_size = btrfs_item_size_nr(leaf, extent_slot);
4548 }
4549#endif
4550 BUG_ON(item_size < sizeof(*ei));
952fccac 4551 ei = btrfs_item_ptr(leaf, extent_slot,
123abc88 4552 struct btrfs_extent_item);
5d4f98a2
YZ
4553 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4554 struct btrfs_tree_block_info *bi;
4555 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4556 bi = (struct btrfs_tree_block_info *)(ei + 1);
4557 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4558 }
56bec294 4559
5d4f98a2 4560 refs = btrfs_extent_refs(leaf, ei);
56bec294
CM
4561 BUG_ON(refs < refs_to_drop);
4562 refs -= refs_to_drop;
5f39d397 4563
5d4f98a2
YZ
4564 if (refs > 0) {
4565 if (extent_op)
4566 __run_delayed_extent_op(extent_op, leaf, ei);
4567 /*
4568 * In the case of inline back ref, reference count will
4569 * be updated by remove_extent_backref
952fccac 4570 */
5d4f98a2
YZ
4571 if (iref) {
4572 BUG_ON(!found_extent);
4573 } else {
4574 btrfs_set_extent_refs(leaf, ei, refs);
4575 btrfs_mark_buffer_dirty(leaf);
4576 }
4577 if (found_extent) {
4578 ret = remove_extent_backref(trans, extent_root, path,
4579 iref, refs_to_drop,
4580 is_data);
952fccac
CM
4581 BUG_ON(ret);
4582 }
5d4f98a2 4583 } else {
5d4f98a2
YZ
4584 if (found_extent) {
4585 BUG_ON(is_data && refs_to_drop !=
4586 extent_data_ref_count(root, path, iref));
4587 if (iref) {
4588 BUG_ON(path->slots[0] != extent_slot);
4589 } else {
4590 BUG_ON(path->slots[0] != extent_slot + 1);
4591 path->slots[0] = extent_slot;
4592 num_to_del = 2;
4593 }
78fae27e 4594 }
b9473439 4595
952fccac
CM
4596 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4597 num_to_del);
31840ae1 4598 BUG_ON(ret);
b3b4aa74 4599 btrfs_release_path(path);
21af804c 4600
5d4f98a2 4601 if (is_data) {
459931ec
CM
4602 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4603 BUG_ON(ret);
d57e62b8
CM
4604 } else {
4605 invalidate_mapping_pages(info->btree_inode->i_mapping,
4606 bytenr >> PAGE_CACHE_SHIFT,
4607 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
459931ec
CM
4608 }
4609
f0486c68 4610 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
dcbdd4dc 4611 BUG_ON(ret);
a28ec197 4612 }
5caf2a00 4613 btrfs_free_path(path);
a28ec197
CM
4614 return ret;
4615}
4616
1887be66 4617/*
f0486c68 4618 * when we free an block, it is possible (and likely) that we free the last
1887be66
CM
4619 * delayed ref for that extent as well. This searches the delayed ref tree for
4620 * a given extent, and if there are no other delayed refs to be processed, it
4621 * removes it from the tree.
4622 */
4623static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4624 struct btrfs_root *root, u64 bytenr)
4625{
4626 struct btrfs_delayed_ref_head *head;
4627 struct btrfs_delayed_ref_root *delayed_refs;
4628 struct btrfs_delayed_ref_node *ref;
4629 struct rb_node *node;
f0486c68 4630 int ret = 0;
1887be66
CM
4631
4632 delayed_refs = &trans->transaction->delayed_refs;
4633 spin_lock(&delayed_refs->lock);
4634 head = btrfs_find_delayed_ref_head(trans, bytenr);
4635 if (!head)
4636 goto out;
4637
4638 node = rb_prev(&head->node.rb_node);
4639 if (!node)
4640 goto out;
4641
4642 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4643
4644 /* there are still entries for this ref, we can't drop it */
4645 if (ref->bytenr == bytenr)
4646 goto out;
4647
5d4f98a2
YZ
4648 if (head->extent_op) {
4649 if (!head->must_insert_reserved)
4650 goto out;
4651 kfree(head->extent_op);
4652 head->extent_op = NULL;
4653 }
4654
1887be66
CM
4655 /*
4656 * waiting for the lock here would deadlock. If someone else has it
4657 * locked they are already in the process of dropping it anyway
4658 */
4659 if (!mutex_trylock(&head->mutex))
4660 goto out;
4661
4662 /*
4663 * at this point we have a head with no other entries. Go
4664 * ahead and process it.
4665 */
4666 head->node.in_tree = 0;
4667 rb_erase(&head->node.rb_node, &delayed_refs->root);
c3e69d58 4668
1887be66
CM
4669 delayed_refs->num_entries--;
4670
4671 /*
4672 * we don't take a ref on the node because we're removing it from the
4673 * tree, so we just steal the ref the tree was holding.
4674 */
c3e69d58
CM
4675 delayed_refs->num_heads--;
4676 if (list_empty(&head->cluster))
4677 delayed_refs->num_heads_ready--;
4678
4679 list_del_init(&head->cluster);
1887be66
CM
4680 spin_unlock(&delayed_refs->lock);
4681
f0486c68
YZ
4682 BUG_ON(head->extent_op);
4683 if (head->must_insert_reserved)
4684 ret = 1;
4685
4686 mutex_unlock(&head->mutex);
1887be66 4687 btrfs_put_delayed_ref(&head->node);
f0486c68 4688 return ret;
1887be66
CM
4689out:
4690 spin_unlock(&delayed_refs->lock);
4691 return 0;
4692}
4693
f0486c68
YZ
4694void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4695 struct btrfs_root *root,
4696 struct extent_buffer *buf,
4697 u64 parent, int last_ref)
4698{
4699 struct btrfs_block_rsv *block_rsv;
4700 struct btrfs_block_group_cache *cache = NULL;
4701 int ret;
4702
4703 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4704 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4705 parent, root->root_key.objectid,
4706 btrfs_header_level(buf),
4707 BTRFS_DROP_DELAYED_REF, NULL);
4708 BUG_ON(ret);
4709 }
4710
4711 if (!last_ref)
4712 return;
4713
4714 block_rsv = get_block_rsv(trans, root);
4715 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
3bf84a5a
YZ
4716 if (block_rsv->space_info != cache->space_info)
4717 goto out;
f0486c68
YZ
4718
4719 if (btrfs_header_generation(buf) == trans->transid) {
4720 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4721 ret = check_ref_cleanup(trans, root, buf->start);
4722 if (!ret)
37be25bc 4723 goto out;
f0486c68
YZ
4724 }
4725
4726 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4727 pin_down_extent(root, cache, buf->start, buf->len, 1);
37be25bc 4728 goto out;
f0486c68
YZ
4729 }
4730
4731 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4732
4733 btrfs_add_free_space(cache, buf->start, buf->len);
fb25e914 4734 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE);
f0486c68
YZ
4735 }
4736out:
a826d6dc
JB
4737 /*
4738 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4739 * anymore.
4740 */
4741 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
f0486c68
YZ
4742 btrfs_put_block_group(cache);
4743}
4744
925baedd 4745int btrfs_free_extent(struct btrfs_trans_handle *trans,
31840ae1
ZY
4746 struct btrfs_root *root,
4747 u64 bytenr, u64 num_bytes, u64 parent,
5d4f98a2 4748 u64 root_objectid, u64 owner, u64 offset)
925baedd
CM
4749{
4750 int ret;
4751
56bec294
CM
4752 /*
4753 * tree log blocks never actually go into the extent allocation
4754 * tree, just update pinning info and exit early.
56bec294 4755 */
5d4f98a2
YZ
4756 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4757 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
b9473439 4758 /* unlocks the pinned mutex */
11833d66 4759 btrfs_pin_extent(root, bytenr, num_bytes, 1);
56bec294 4760 ret = 0;
5d4f98a2
YZ
4761 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4762 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4763 parent, root_objectid, (int)owner,
4764 BTRFS_DROP_DELAYED_REF, NULL);
1887be66 4765 BUG_ON(ret);
5d4f98a2
YZ
4766 } else {
4767 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4768 parent, root_objectid, owner,
4769 offset, BTRFS_DROP_DELAYED_REF, NULL);
4770 BUG_ON(ret);
56bec294 4771 }
925baedd
CM
4772 return ret;
4773}
4774
87ee04eb
CM
4775static u64 stripe_align(struct btrfs_root *root, u64 val)
4776{
4777 u64 mask = ((u64)root->stripesize - 1);
4778 u64 ret = (val + mask) & ~mask;
4779 return ret;
4780}
4781
817d52f8
JB
4782/*
4783 * when we wait for progress in the block group caching, its because
4784 * our allocation attempt failed at least once. So, we must sleep
4785 * and let some progress happen before we try again.
4786 *
4787 * This function will sleep at least once waiting for new free space to
4788 * show up, and then it will check the block group free space numbers
4789 * for our min num_bytes. Another option is to have it go ahead
4790 * and look in the rbtree for a free extent of a given size, but this
4791 * is a good start.
4792 */
4793static noinline int
4794wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4795 u64 num_bytes)
4796{
11833d66 4797 struct btrfs_caching_control *caching_ctl;
817d52f8
JB
4798 DEFINE_WAIT(wait);
4799
11833d66
YZ
4800 caching_ctl = get_caching_control(cache);
4801 if (!caching_ctl)
817d52f8 4802 return 0;
817d52f8 4803
11833d66 4804 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
34d52cb6 4805 (cache->free_space_ctl->free_space >= num_bytes));
11833d66
YZ
4806
4807 put_caching_control(caching_ctl);
4808 return 0;
4809}
4810
4811static noinline int
4812wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4813{
4814 struct btrfs_caching_control *caching_ctl;
4815 DEFINE_WAIT(wait);
4816
4817 caching_ctl = get_caching_control(cache);
4818 if (!caching_ctl)
4819 return 0;
4820
4821 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4822
4823 put_caching_control(caching_ctl);
817d52f8
JB
4824 return 0;
4825}
4826
b742bb82
YZ
4827static int get_block_group_index(struct btrfs_block_group_cache *cache)
4828{
4829 int index;
4830 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4831 index = 0;
4832 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4833 index = 1;
4834 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4835 index = 2;
4836 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4837 index = 3;
4838 else
4839 index = 4;
4840 return index;
4841}
4842
817d52f8 4843enum btrfs_loop_type {
ccf0e725 4844 LOOP_FIND_IDEAL = 0,
817d52f8
JB
4845 LOOP_CACHING_NOWAIT = 1,
4846 LOOP_CACHING_WAIT = 2,
4847 LOOP_ALLOC_CHUNK = 3,
4848 LOOP_NO_EMPTY_SIZE = 4,
4849};
4850
fec577fb
CM
4851/*
4852 * walks the btree of allocated extents and find a hole of a given size.
4853 * The key ins is changed to record the hole:
4854 * ins->objectid == block start
62e2749e 4855 * ins->flags = BTRFS_EXTENT_ITEM_KEY
fec577fb
CM
4856 * ins->offset == number of blocks
4857 * Any available blocks before search_start are skipped.
4858 */
d397712b 4859static noinline int find_free_extent(struct btrfs_trans_handle *trans,
98ed5174
CM
4860 struct btrfs_root *orig_root,
4861 u64 num_bytes, u64 empty_size,
4862 u64 search_start, u64 search_end,
4863 u64 hint_byte, struct btrfs_key *ins,
e0f54067 4864 u64 data)
fec577fb 4865{
80eb234a 4866 int ret = 0;
d397712b 4867 struct btrfs_root *root = orig_root->fs_info->extent_root;
fa9c0d79 4868 struct btrfs_free_cluster *last_ptr = NULL;
80eb234a 4869 struct btrfs_block_group_cache *block_group = NULL;
239b14b3 4870 int empty_cluster = 2 * 1024 * 1024;
0ef3e66b 4871 int allowed_chunk_alloc = 0;
ccf0e725 4872 int done_chunk_alloc = 0;
80eb234a 4873 struct btrfs_space_info *space_info;
fa9c0d79
CM
4874 int last_ptr_loop = 0;
4875 int loop = 0;
f0486c68 4876 int index = 0;
fb25e914
JB
4877 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
4878 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
817d52f8 4879 bool found_uncached_bg = false;
0a24325e 4880 bool failed_cluster_refill = false;
1cdda9b8 4881 bool failed_alloc = false;
67377734 4882 bool use_cluster = true;
ccf0e725
JB
4883 u64 ideal_cache_percent = 0;
4884 u64 ideal_cache_offset = 0;
fec577fb 4885
db94535d 4886 WARN_ON(num_bytes < root->sectorsize);
b1a4d965 4887 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
80eb234a
JB
4888 ins->objectid = 0;
4889 ins->offset = 0;
b1a4d965 4890
2552d17e 4891 space_info = __find_space_info(root->fs_info, data);
1b1d1f66 4892 if (!space_info) {
e0f54067 4893 printk(KERN_ERR "No space info for %llu\n", data);
1b1d1f66
JB
4894 return -ENOSPC;
4895 }
2552d17e 4896
67377734
JB
4897 /*
4898 * If the space info is for both data and metadata it means we have a
4899 * small filesystem and we can't use the clustering stuff.
4900 */
4901 if (btrfs_mixed_space_info(space_info))
4902 use_cluster = false;
4903
0ef3e66b
CM
4904 if (orig_root->ref_cows || empty_size)
4905 allowed_chunk_alloc = 1;
4906
67377734 4907 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
fa9c0d79 4908 last_ptr = &root->fs_info->meta_alloc_cluster;
536ac8ae
CM
4909 if (!btrfs_test_opt(root, SSD))
4910 empty_cluster = 64 * 1024;
239b14b3
CM
4911 }
4912
67377734
JB
4913 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4914 btrfs_test_opt(root, SSD)) {
fa9c0d79
CM
4915 last_ptr = &root->fs_info->data_alloc_cluster;
4916 }
0f9dd46c 4917
239b14b3 4918 if (last_ptr) {
fa9c0d79
CM
4919 spin_lock(&last_ptr->lock);
4920 if (last_ptr->block_group)
4921 hint_byte = last_ptr->window_start;
4922 spin_unlock(&last_ptr->lock);
239b14b3 4923 }
fa9c0d79 4924
a061fc8d 4925 search_start = max(search_start, first_logical_byte(root, 0));
239b14b3 4926 search_start = max(search_start, hint_byte);
0b86a832 4927
817d52f8 4928 if (!last_ptr)
fa9c0d79 4929 empty_cluster = 0;
fa9c0d79 4930
2552d17e 4931 if (search_start == hint_byte) {
ccf0e725 4932ideal_cache:
2552d17e
JB
4933 block_group = btrfs_lookup_block_group(root->fs_info,
4934 search_start);
817d52f8
JB
4935 /*
4936 * we don't want to use the block group if it doesn't match our
4937 * allocation bits, or if its not cached.
ccf0e725
JB
4938 *
4939 * However if we are re-searching with an ideal block group
4940 * picked out then we don't care that the block group is cached.
817d52f8
JB
4941 */
4942 if (block_group && block_group_bits(block_group, data) &&
ccf0e725
JB
4943 (block_group->cached != BTRFS_CACHE_NO ||
4944 search_start == ideal_cache_offset)) {
2552d17e 4945 down_read(&space_info->groups_sem);
44fb5511
CM
4946 if (list_empty(&block_group->list) ||
4947 block_group->ro) {
4948 /*
4949 * someone is removing this block group,
4950 * we can't jump into the have_block_group
4951 * target because our list pointers are not
4952 * valid
4953 */
4954 btrfs_put_block_group(block_group);
4955 up_read(&space_info->groups_sem);
ccf0e725 4956 } else {
b742bb82 4957 index = get_block_group_index(block_group);
44fb5511 4958 goto have_block_group;
ccf0e725 4959 }
2552d17e 4960 } else if (block_group) {
fa9c0d79 4961 btrfs_put_block_group(block_group);
2552d17e 4962 }
42e70e7a 4963 }
2552d17e 4964search:
80eb234a 4965 down_read(&space_info->groups_sem);
b742bb82
YZ
4966 list_for_each_entry(block_group, &space_info->block_groups[index],
4967 list) {
6226cb0a 4968 u64 offset;
817d52f8 4969 int cached;
8a1413a2 4970
11dfe35a 4971 btrfs_get_block_group(block_group);
2552d17e 4972 search_start = block_group->key.objectid;
42e70e7a 4973
83a50de9
CM
4974 /*
4975 * this can happen if we end up cycling through all the
4976 * raid types, but we want to make sure we only allocate
4977 * for the proper type.
4978 */
4979 if (!block_group_bits(block_group, data)) {
4980 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4981 BTRFS_BLOCK_GROUP_RAID1 |
4982 BTRFS_BLOCK_GROUP_RAID10;
4983
4984 /*
4985 * if they asked for extra copies and this block group
4986 * doesn't provide them, bail. This does allow us to
4987 * fill raid0 from raid1.
4988 */
4989 if ((data & extra) && !(block_group->flags & extra))
4990 goto loop;
4991 }
4992
2552d17e 4993have_block_group:
817d52f8 4994 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
ccf0e725
JB
4995 u64 free_percent;
4996
b8399dee
JB
4997 ret = cache_block_group(block_group, trans,
4998 orig_root, 1);
9d66e233
JB
4999 if (block_group->cached == BTRFS_CACHE_FINISHED)
5000 goto have_block_group;
5001
ccf0e725
JB
5002 free_percent = btrfs_block_group_used(&block_group->item);
5003 free_percent *= 100;
5004 free_percent = div64_u64(free_percent,
5005 block_group->key.offset);
5006 free_percent = 100 - free_percent;
5007 if (free_percent > ideal_cache_percent &&
5008 likely(!block_group->ro)) {
5009 ideal_cache_offset = block_group->key.objectid;
5010 ideal_cache_percent = free_percent;
5011 }
5012
817d52f8 5013 /*
bab39bf9
JB
5014 * The caching workers are limited to 2 threads, so we
5015 * can queue as much work as we care to.
817d52f8 5016 */
bab39bf9 5017 if (loop > LOOP_FIND_IDEAL) {
b8399dee
JB
5018 ret = cache_block_group(block_group, trans,
5019 orig_root, 0);
817d52f8 5020 BUG_ON(ret);
2552d17e 5021 }
817d52f8
JB
5022 found_uncached_bg = true;
5023
ccf0e725
JB
5024 /*
5025 * If loop is set for cached only, try the next block
5026 * group.
5027 */
5028 if (loop == LOOP_FIND_IDEAL)
817d52f8
JB
5029 goto loop;
5030 }
5031
ccf0e725
JB
5032 cached = block_group_cache_done(block_group);
5033 if (unlikely(!cached))
5034 found_uncached_bg = true;
5035
ea6a478e 5036 if (unlikely(block_group->ro))
2552d17e 5037 goto loop;
0f9dd46c 5038
ff5714cc 5039 spin_lock(&block_group->free_space_ctl->tree_lock);
cca1c81f 5040 if (cached &&
ff5714cc
CM
5041 block_group->free_space_ctl->free_space <
5042 num_bytes + empty_size) {
5043 spin_unlock(&block_group->free_space_ctl->tree_lock);
cca1c81f
JB
5044 goto loop;
5045 }
ff5714cc 5046 spin_unlock(&block_group->free_space_ctl->tree_lock);
cca1c81f 5047
0a24325e
JB
5048 /*
5049 * Ok we want to try and use the cluster allocator, so lets look
5050 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5051 * have tried the cluster allocator plenty of times at this
5052 * point and not have found anything, so we are likely way too
5053 * fragmented for the clustering stuff to find anything, so lets
5054 * just skip it and let the allocator find whatever block it can
5055 * find
5056 */
5057 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
fa9c0d79
CM
5058 /*
5059 * the refill lock keeps out other
5060 * people trying to start a new cluster
5061 */
5062 spin_lock(&last_ptr->refill_lock);
44fb5511
CM
5063 if (last_ptr->block_group &&
5064 (last_ptr->block_group->ro ||
5065 !block_group_bits(last_ptr->block_group, data))) {
5066 offset = 0;
5067 goto refill_cluster;
5068 }
5069
fa9c0d79
CM
5070 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5071 num_bytes, search_start);
5072 if (offset) {
5073 /* we have a block, we're done */
5074 spin_unlock(&last_ptr->refill_lock);
5075 goto checks;
5076 }
5077
5078 spin_lock(&last_ptr->lock);
5079 /*
5080 * whoops, this cluster doesn't actually point to
5081 * this block group. Get a ref on the block
5082 * group is does point to and try again
5083 */
5084 if (!last_ptr_loop && last_ptr->block_group &&
ff1f2b44 5085 last_ptr->block_group != block_group &&
5086 index <=
5087 get_block_group_index(last_ptr->block_group)) {
fa9c0d79
CM
5088
5089 btrfs_put_block_group(block_group);
5090 block_group = last_ptr->block_group;
11dfe35a 5091 btrfs_get_block_group(block_group);
fa9c0d79
CM
5092 spin_unlock(&last_ptr->lock);
5093 spin_unlock(&last_ptr->refill_lock);
5094
5095 last_ptr_loop = 1;
5096 search_start = block_group->key.objectid;
44fb5511
CM
5097 /*
5098 * we know this block group is properly
5099 * in the list because
5100 * btrfs_remove_block_group, drops the
5101 * cluster before it removes the block
5102 * group from the list
5103 */
fa9c0d79
CM
5104 goto have_block_group;
5105 }
5106 spin_unlock(&last_ptr->lock);
44fb5511 5107refill_cluster:
fa9c0d79
CM
5108 /*
5109 * this cluster didn't work out, free it and
5110 * start over
5111 */
5112 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5113
5114 last_ptr_loop = 0;
5115
5116 /* allocate a cluster in this block group */
451d7585 5117 ret = btrfs_find_space_cluster(trans, root,
fa9c0d79
CM
5118 block_group, last_ptr,
5119 offset, num_bytes,
5120 empty_cluster + empty_size);
5121 if (ret == 0) {
5122 /*
5123 * now pull our allocation out of this
5124 * cluster
5125 */
5126 offset = btrfs_alloc_from_cluster(block_group,
5127 last_ptr, num_bytes,
5128 search_start);
5129 if (offset) {
5130 /* we found one, proceed */
5131 spin_unlock(&last_ptr->refill_lock);
5132 goto checks;
5133 }
0a24325e
JB
5134 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5135 && !failed_cluster_refill) {
817d52f8
JB
5136 spin_unlock(&last_ptr->refill_lock);
5137
0a24325e 5138 failed_cluster_refill = true;
817d52f8
JB
5139 wait_block_group_cache_progress(block_group,
5140 num_bytes + empty_cluster + empty_size);
5141 goto have_block_group;
fa9c0d79 5142 }
817d52f8 5143
fa9c0d79
CM
5144 /*
5145 * at this point we either didn't find a cluster
5146 * or we weren't able to allocate a block from our
5147 * cluster. Free the cluster we've been trying
5148 * to use, and go to the next block group
5149 */
0a24325e 5150 btrfs_return_cluster_to_free_space(NULL, last_ptr);
fa9c0d79 5151 spin_unlock(&last_ptr->refill_lock);
0a24325e 5152 goto loop;
fa9c0d79
CM
5153 }
5154
6226cb0a
JB
5155 offset = btrfs_find_space_for_alloc(block_group, search_start,
5156 num_bytes, empty_size);
1cdda9b8
JB
5157 /*
5158 * If we didn't find a chunk, and we haven't failed on this
5159 * block group before, and this block group is in the middle of
5160 * caching and we are ok with waiting, then go ahead and wait
5161 * for progress to be made, and set failed_alloc to true.
5162 *
5163 * If failed_alloc is true then we've already waited on this
5164 * block group once and should move on to the next block group.
5165 */
5166 if (!offset && !failed_alloc && !cached &&
5167 loop > LOOP_CACHING_NOWAIT) {
817d52f8 5168 wait_block_group_cache_progress(block_group,
1cdda9b8
JB
5169 num_bytes + empty_size);
5170 failed_alloc = true;
817d52f8 5171 goto have_block_group;
1cdda9b8
JB
5172 } else if (!offset) {
5173 goto loop;
817d52f8 5174 }
fa9c0d79 5175checks:
6226cb0a 5176 search_start = stripe_align(root, offset);
2552d17e 5177 /* move on to the next group */
6226cb0a
JB
5178 if (search_start + num_bytes >= search_end) {
5179 btrfs_add_free_space(block_group, offset, num_bytes);
2552d17e 5180 goto loop;
6226cb0a 5181 }
25179201 5182
2552d17e
JB
5183 /* move on to the next group */
5184 if (search_start + num_bytes >
6226cb0a
JB
5185 block_group->key.objectid + block_group->key.offset) {
5186 btrfs_add_free_space(block_group, offset, num_bytes);
2552d17e 5187 goto loop;
6226cb0a 5188 }
f5a31e16 5189
f0486c68
YZ
5190 ins->objectid = search_start;
5191 ins->offset = num_bytes;
2552d17e 5192
f0486c68
YZ
5193 if (offset < search_start)
5194 btrfs_add_free_space(block_group, offset,
5195 search_start - offset);
5196 BUG_ON(offset > search_start);
2552d17e 5197
fb25e914
JB
5198 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
5199 alloc_type);
f0486c68 5200 if (ret == -EAGAIN) {
6226cb0a 5201 btrfs_add_free_space(block_group, offset, num_bytes);
2552d17e 5202 goto loop;
0f9dd46c 5203 }
0b86a832 5204
f0486c68 5205 /* we are all good, lets return */
2552d17e
JB
5206 ins->objectid = search_start;
5207 ins->offset = num_bytes;
d2fb3437 5208
6226cb0a
JB
5209 if (offset < search_start)
5210 btrfs_add_free_space(block_group, offset,
5211 search_start - offset);
5212 BUG_ON(offset > search_start);
d82a6f1d 5213 btrfs_put_block_group(block_group);
2552d17e
JB
5214 break;
5215loop:
0a24325e 5216 failed_cluster_refill = false;
1cdda9b8 5217 failed_alloc = false;
b742bb82 5218 BUG_ON(index != get_block_group_index(block_group));
fa9c0d79 5219 btrfs_put_block_group(block_group);
2552d17e
JB
5220 }
5221 up_read(&space_info->groups_sem);
5222
b742bb82
YZ
5223 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5224 goto search;
5225
ccf0e725
JB
5226 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5227 * for them to make caching progress. Also
5228 * determine the best possible bg to cache
5229 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5230 * caching kthreads as we move along
817d52f8
JB
5231 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5232 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5233 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5234 * again
fa9c0d79 5235 */
723bda20 5236 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
b742bb82 5237 index = 0;
ccf0e725 5238 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
817d52f8 5239 found_uncached_bg = false;
ccf0e725 5240 loop++;
bab39bf9 5241 if (!ideal_cache_percent)
817d52f8 5242 goto search;
ccf0e725
JB
5243
5244 /*
5245 * 1 of the following 2 things have happened so far
5246 *
5247 * 1) We found an ideal block group for caching that
5248 * is mostly full and will cache quickly, so we might
5249 * as well wait for it.
5250 *
5251 * 2) We searched for cached only and we didn't find
5252 * anything, and we didn't start any caching kthreads
5253 * either, so chances are we will loop through and
5254 * start a couple caching kthreads, and then come back
5255 * around and just wait for them. This will be slower
5256 * because we will have 2 caching kthreads reading at
5257 * the same time when we could have just started one
5258 * and waited for it to get far enough to give us an
5259 * allocation, so go ahead and go to the wait caching
5260 * loop.
5261 */
5262 loop = LOOP_CACHING_WAIT;
5263 search_start = ideal_cache_offset;
5264 ideal_cache_percent = 0;
5265 goto ideal_cache;
5266 } else if (loop == LOOP_FIND_IDEAL) {
5267 /*
5268 * Didn't find a uncached bg, wait on anything we find
5269 * next.
5270 */
5271 loop = LOOP_CACHING_WAIT;
5272 goto search;
5273 }
5274
723bda20 5275 loop++;
817d52f8
JB
5276
5277 if (loop == LOOP_ALLOC_CHUNK) {
723bda20
JB
5278 if (allowed_chunk_alloc) {
5279 ret = do_chunk_alloc(trans, root, num_bytes +
5280 2 * 1024 * 1024, data,
5281 CHUNK_ALLOC_LIMITED);
5282 allowed_chunk_alloc = 0;
5283 if (ret == 1)
5284 done_chunk_alloc = 1;
5285 } else if (!done_chunk_alloc &&
5286 space_info->force_alloc ==
5287 CHUNK_ALLOC_NO_FORCE) {
5288 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5289 }
2552d17e 5290
723bda20
JB
5291 /*
5292 * We didn't allocate a chunk, go ahead and drop the
5293 * empty size and loop again.
5294 */
5295 if (!done_chunk_alloc)
5296 loop = LOOP_NO_EMPTY_SIZE;
2552d17e
JB
5297 }
5298
723bda20
JB
5299 if (loop == LOOP_NO_EMPTY_SIZE) {
5300 empty_size = 0;
5301 empty_cluster = 0;
fa9c0d79 5302 }
723bda20
JB
5303
5304 goto search;
2552d17e
JB
5305 } else if (!ins->objectid) {
5306 ret = -ENOSPC;
d82a6f1d 5307 } else if (ins->objectid) {
80eb234a 5308 ret = 0;
be744175 5309 }
be744175 5310
0f70abe2 5311 return ret;
fec577fb 5312}
ec44a35c 5313
9ed74f2d
JB
5314static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5315 int dump_block_groups)
0f9dd46c
JB
5316{
5317 struct btrfs_block_group_cache *cache;
b742bb82 5318 int index = 0;
0f9dd46c 5319
9ed74f2d 5320 spin_lock(&info->lock);
fb25e914
JB
5321 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n",
5322 (unsigned long long)info->flags,
d397712b 5323 (unsigned long long)(info->total_bytes - info->bytes_used -
9ed74f2d 5324 info->bytes_pinned - info->bytes_reserved -
8929ecfa 5325 info->bytes_readonly),
d397712b 5326 (info->full) ? "" : "not ");
8929ecfa
YZ
5327 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5328 "reserved=%llu, may_use=%llu, readonly=%llu\n",
21380931 5329 (unsigned long long)info->total_bytes,
8929ecfa 5330 (unsigned long long)info->bytes_used,
21380931 5331 (unsigned long long)info->bytes_pinned,
8929ecfa 5332 (unsigned long long)info->bytes_reserved,
21380931 5333 (unsigned long long)info->bytes_may_use,
8929ecfa 5334 (unsigned long long)info->bytes_readonly);
9ed74f2d
JB
5335 spin_unlock(&info->lock);
5336
5337 if (!dump_block_groups)
5338 return;
0f9dd46c 5339
80eb234a 5340 down_read(&info->groups_sem);
b742bb82
YZ
5341again:
5342 list_for_each_entry(cache, &info->block_groups[index], list) {
0f9dd46c 5343 spin_lock(&cache->lock);
d397712b
CM
5344 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5345 "%llu pinned %llu reserved\n",
5346 (unsigned long long)cache->key.objectid,
5347 (unsigned long long)cache->key.offset,
5348 (unsigned long long)btrfs_block_group_used(&cache->item),
5349 (unsigned long long)cache->pinned,
5350 (unsigned long long)cache->reserved);
0f9dd46c
JB
5351 btrfs_dump_free_space(cache, bytes);
5352 spin_unlock(&cache->lock);
5353 }
b742bb82
YZ
5354 if (++index < BTRFS_NR_RAID_TYPES)
5355 goto again;
80eb234a 5356 up_read(&info->groups_sem);
0f9dd46c 5357}
e8569813 5358
11833d66
YZ
5359int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5360 struct btrfs_root *root,
5361 u64 num_bytes, u64 min_alloc_size,
5362 u64 empty_size, u64 hint_byte,
5363 u64 search_end, struct btrfs_key *ins,
5364 u64 data)
fec577fb
CM
5365{
5366 int ret;
fbdc762b 5367 u64 search_start = 0;
925baedd 5368
6a63209f 5369 data = btrfs_get_alloc_profile(root, data);
98d20f67 5370again:
0ef3e66b
CM
5371 /*
5372 * the only place that sets empty_size is btrfs_realloc_node, which
5373 * is not called recursively on allocations
5374 */
83d3c969 5375 if (empty_size || root->ref_cows)
6324fbf3 5376 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
0e4f8f88
CM
5377 num_bytes + 2 * 1024 * 1024, data,
5378 CHUNK_ALLOC_NO_FORCE);
0b86a832 5379
db94535d
CM
5380 WARN_ON(num_bytes < root->sectorsize);
5381 ret = find_free_extent(trans, root, num_bytes, empty_size,
f0486c68
YZ
5382 search_start, search_end, hint_byte,
5383 ins, data);
3b951516 5384
98d20f67
CM
5385 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5386 num_bytes = num_bytes >> 1;
0f9dd46c 5387 num_bytes = num_bytes & ~(root->sectorsize - 1);
98d20f67 5388 num_bytes = max(num_bytes, min_alloc_size);
0ef3e66b 5389 do_chunk_alloc(trans, root->fs_info->extent_root,
0e4f8f88 5390 num_bytes, data, CHUNK_ALLOC_FORCE);
98d20f67
CM
5391 goto again;
5392 }
91435650 5393 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
0f9dd46c
JB
5394 struct btrfs_space_info *sinfo;
5395
5396 sinfo = __find_space_info(root->fs_info, data);
d397712b
CM
5397 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5398 "wanted %llu\n", (unsigned long long)data,
5399 (unsigned long long)num_bytes);
9ed74f2d 5400 dump_space_info(sinfo, num_bytes, 1);
925baedd 5401 }
0f9dd46c 5402
1abe9b8a 5403 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5404
0f9dd46c 5405 return ret;
e6dcd2dc
CM
5406}
5407
65b51a00
CM
5408int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5409{
0f9dd46c 5410 struct btrfs_block_group_cache *cache;
1f3c79a2 5411 int ret = 0;
0f9dd46c 5412
0f9dd46c
JB
5413 cache = btrfs_lookup_block_group(root->fs_info, start);
5414 if (!cache) {
d397712b
CM
5415 printk(KERN_ERR "Unable to find block group for %llu\n",
5416 (unsigned long long)start);
0f9dd46c
JB
5417 return -ENOSPC;
5418 }
1f3c79a2 5419
5378e607
LD
5420 if (btrfs_test_opt(root, DISCARD))
5421 ret = btrfs_discard_extent(root, start, len, NULL);
1f3c79a2 5422
0f9dd46c 5423 btrfs_add_free_space(cache, start, len);
fb25e914 5424 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
fa9c0d79 5425 btrfs_put_block_group(cache);
817d52f8 5426
1abe9b8a 5427 trace_btrfs_reserved_extent_free(root, start, len);
5428
e6dcd2dc
CM
5429 return ret;
5430}
5431
5d4f98a2
YZ
5432static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5433 struct btrfs_root *root,
5434 u64 parent, u64 root_objectid,
5435 u64 flags, u64 owner, u64 offset,
5436 struct btrfs_key *ins, int ref_mod)
e6dcd2dc
CM
5437{
5438 int ret;
5d4f98a2 5439 struct btrfs_fs_info *fs_info = root->fs_info;
e6dcd2dc 5440 struct btrfs_extent_item *extent_item;
5d4f98a2 5441 struct btrfs_extent_inline_ref *iref;
e6dcd2dc 5442 struct btrfs_path *path;
5d4f98a2
YZ
5443 struct extent_buffer *leaf;
5444 int type;
5445 u32 size;
26b8003f 5446
5d4f98a2
YZ
5447 if (parent > 0)
5448 type = BTRFS_SHARED_DATA_REF_KEY;
5449 else
5450 type = BTRFS_EXTENT_DATA_REF_KEY;
58176a96 5451
5d4f98a2 5452 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
7bb86316
CM
5453
5454 path = btrfs_alloc_path();
db5b493a
TI
5455 if (!path)
5456 return -ENOMEM;
47e4bb98 5457
b9473439 5458 path->leave_spinning = 1;
5d4f98a2
YZ
5459 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5460 ins, size);
ccd467d6 5461 BUG_ON(ret);
0f9dd46c 5462
5d4f98a2
YZ
5463 leaf = path->nodes[0];
5464 extent_item = btrfs_item_ptr(leaf, path->slots[0],
47e4bb98 5465 struct btrfs_extent_item);
5d4f98a2
YZ
5466 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5467 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5468 btrfs_set_extent_flags(leaf, extent_item,
5469 flags | BTRFS_EXTENT_FLAG_DATA);
5470
5471 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5472 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5473 if (parent > 0) {
5474 struct btrfs_shared_data_ref *ref;
5475 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5476 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5477 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5478 } else {
5479 struct btrfs_extent_data_ref *ref;
5480 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5481 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5482 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5483 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5484 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5485 }
47e4bb98
CM
5486
5487 btrfs_mark_buffer_dirty(path->nodes[0]);
7bb86316 5488 btrfs_free_path(path);
f510cfec 5489
f0486c68 5490 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
f5947066 5491 if (ret) {
d397712b
CM
5492 printk(KERN_ERR "btrfs update block group failed for %llu "
5493 "%llu\n", (unsigned long long)ins->objectid,
5494 (unsigned long long)ins->offset);
f5947066
CM
5495 BUG();
5496 }
e6dcd2dc
CM
5497 return ret;
5498}
5499
5d4f98a2
YZ
5500static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5501 struct btrfs_root *root,
5502 u64 parent, u64 root_objectid,
5503 u64 flags, struct btrfs_disk_key *key,
5504 int level, struct btrfs_key *ins)
e6dcd2dc
CM
5505{
5506 int ret;
5d4f98a2
YZ
5507 struct btrfs_fs_info *fs_info = root->fs_info;
5508 struct btrfs_extent_item *extent_item;
5509 struct btrfs_tree_block_info *block_info;
5510 struct btrfs_extent_inline_ref *iref;
5511 struct btrfs_path *path;
5512 struct extent_buffer *leaf;
5513 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
1c2308f8 5514
5d4f98a2 5515 path = btrfs_alloc_path();
d8926bb3
MF
5516 if (!path)
5517 return -ENOMEM;
56bec294 5518
5d4f98a2
YZ
5519 path->leave_spinning = 1;
5520 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5521 ins, size);
56bec294 5522 BUG_ON(ret);
5d4f98a2
YZ
5523
5524 leaf = path->nodes[0];
5525 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5526 struct btrfs_extent_item);
5527 btrfs_set_extent_refs(leaf, extent_item, 1);
5528 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5529 btrfs_set_extent_flags(leaf, extent_item,
5530 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5531 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5532
5533 btrfs_set_tree_block_key(leaf, block_info, key);
5534 btrfs_set_tree_block_level(leaf, block_info, level);
5535
5536 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5537 if (parent > 0) {
5538 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5539 btrfs_set_extent_inline_ref_type(leaf, iref,
5540 BTRFS_SHARED_BLOCK_REF_KEY);
5541 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5542 } else {
5543 btrfs_set_extent_inline_ref_type(leaf, iref,
5544 BTRFS_TREE_BLOCK_REF_KEY);
5545 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5546 }
5547
5548 btrfs_mark_buffer_dirty(leaf);
5549 btrfs_free_path(path);
5550
f0486c68 5551 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5d4f98a2
YZ
5552 if (ret) {
5553 printk(KERN_ERR "btrfs update block group failed for %llu "
5554 "%llu\n", (unsigned long long)ins->objectid,
5555 (unsigned long long)ins->offset);
5556 BUG();
5557 }
5558 return ret;
5559}
5560
5561int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5562 struct btrfs_root *root,
5563 u64 root_objectid, u64 owner,
5564 u64 offset, struct btrfs_key *ins)
5565{
5566 int ret;
5567
5568 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5569
5570 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5571 0, root_objectid, owner, offset,
5572 BTRFS_ADD_DELAYED_EXTENT, NULL);
e6dcd2dc
CM
5573 return ret;
5574}
e02119d5
CM
5575
5576/*
5577 * this is used by the tree logging recovery code. It records that
5578 * an extent has been allocated and makes sure to clear the free
5579 * space cache bits as well
5580 */
5d4f98a2
YZ
5581int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5582 struct btrfs_root *root,
5583 u64 root_objectid, u64 owner, u64 offset,
5584 struct btrfs_key *ins)
e02119d5
CM
5585{
5586 int ret;
5587 struct btrfs_block_group_cache *block_group;
11833d66
YZ
5588 struct btrfs_caching_control *caching_ctl;
5589 u64 start = ins->objectid;
5590 u64 num_bytes = ins->offset;
e02119d5 5591
e02119d5 5592 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
b8399dee 5593 cache_block_group(block_group, trans, NULL, 0);
11833d66 5594 caching_ctl = get_caching_control(block_group);
e02119d5 5595
11833d66
YZ
5596 if (!caching_ctl) {
5597 BUG_ON(!block_group_cache_done(block_group));
5598 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5599 BUG_ON(ret);
5600 } else {
5601 mutex_lock(&caching_ctl->mutex);
5602
5603 if (start >= caching_ctl->progress) {
5604 ret = add_excluded_extent(root, start, num_bytes);
5605 BUG_ON(ret);
5606 } else if (start + num_bytes <= caching_ctl->progress) {
5607 ret = btrfs_remove_free_space(block_group,
5608 start, num_bytes);
5609 BUG_ON(ret);
5610 } else {
5611 num_bytes = caching_ctl->progress - start;
5612 ret = btrfs_remove_free_space(block_group,
5613 start, num_bytes);
5614 BUG_ON(ret);
5615
5616 start = caching_ctl->progress;
5617 num_bytes = ins->objectid + ins->offset -
5618 caching_ctl->progress;
5619 ret = add_excluded_extent(root, start, num_bytes);
5620 BUG_ON(ret);
5621 }
5622
5623 mutex_unlock(&caching_ctl->mutex);
5624 put_caching_control(caching_ctl);
5625 }
5626
fb25e914
JB
5627 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
5628 RESERVE_ALLOC_NO_ACCOUNT);
f0486c68 5629 BUG_ON(ret);
fa9c0d79 5630 btrfs_put_block_group(block_group);
5d4f98a2
YZ
5631 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5632 0, owner, offset, ins, 1);
e02119d5
CM
5633 return ret;
5634}
5635
65b51a00
CM
5636struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5637 struct btrfs_root *root,
4008c04a
CM
5638 u64 bytenr, u32 blocksize,
5639 int level)
65b51a00
CM
5640{
5641 struct extent_buffer *buf;
5642
5643 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5644 if (!buf)
5645 return ERR_PTR(-ENOMEM);
5646 btrfs_set_header_generation(buf, trans->transid);
85d4e461 5647 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
65b51a00
CM
5648 btrfs_tree_lock(buf);
5649 clean_tree_block(trans, root, buf);
b4ce94de
CM
5650
5651 btrfs_set_lock_blocking(buf);
65b51a00 5652 btrfs_set_buffer_uptodate(buf);
b4ce94de 5653
d0c803c4 5654 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
8cef4e16
YZ
5655 /*
5656 * we allow two log transactions at a time, use different
5657 * EXENT bit to differentiate dirty pages.
5658 */
5659 if (root->log_transid % 2 == 0)
5660 set_extent_dirty(&root->dirty_log_pages, buf->start,
5661 buf->start + buf->len - 1, GFP_NOFS);
5662 else
5663 set_extent_new(&root->dirty_log_pages, buf->start,
5664 buf->start + buf->len - 1, GFP_NOFS);
d0c803c4
CM
5665 } else {
5666 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
65b51a00 5667 buf->start + buf->len - 1, GFP_NOFS);
d0c803c4 5668 }
65b51a00 5669 trans->blocks_used++;
b4ce94de 5670 /* this returns a buffer locked for blocking */
65b51a00
CM
5671 return buf;
5672}
5673
f0486c68
YZ
5674static struct btrfs_block_rsv *
5675use_block_rsv(struct btrfs_trans_handle *trans,
5676 struct btrfs_root *root, u32 blocksize)
5677{
5678 struct btrfs_block_rsv *block_rsv;
68a82277 5679 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
f0486c68
YZ
5680 int ret;
5681
5682 block_rsv = get_block_rsv(trans, root);
5683
5684 if (block_rsv->size == 0) {
8bb8ab2e
JB
5685 ret = reserve_metadata_bytes(trans, root, block_rsv,
5686 blocksize, 0);
68a82277
JB
5687 /*
5688 * If we couldn't reserve metadata bytes try and use some from
5689 * the global reserve.
5690 */
5691 if (ret && block_rsv != global_rsv) {
5692 ret = block_rsv_use_bytes(global_rsv, blocksize);
5693 if (!ret)
5694 return global_rsv;
f0486c68 5695 return ERR_PTR(ret);
68a82277 5696 } else if (ret) {
f0486c68 5697 return ERR_PTR(ret);
68a82277 5698 }
f0486c68
YZ
5699 return block_rsv;
5700 }
5701
5702 ret = block_rsv_use_bytes(block_rsv, blocksize);
5703 if (!ret)
5704 return block_rsv;
68a82277
JB
5705 if (ret) {
5706 WARN_ON(1);
5707 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5708 0);
5709 if (!ret) {
68a82277
JB
5710 return block_rsv;
5711 } else if (ret && block_rsv != global_rsv) {
5712 ret = block_rsv_use_bytes(global_rsv, blocksize);
5713 if (!ret)
5714 return global_rsv;
5715 }
5716 }
f0486c68 5717
f0486c68
YZ
5718 return ERR_PTR(-ENOSPC);
5719}
5720
5721static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5722{
5723 block_rsv_add_bytes(block_rsv, blocksize, 0);
5724 block_rsv_release_bytes(block_rsv, NULL, 0);
5725}
5726
fec577fb 5727/*
f0486c68
YZ
5728 * finds a free extent and does all the dirty work required for allocation
5729 * returns the key for the extent through ins, and a tree buffer for
5730 * the first block of the extent through buf.
5731 *
fec577fb
CM
5732 * returns the tree buffer or NULL.
5733 */
5f39d397 5734struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5d4f98a2
YZ
5735 struct btrfs_root *root, u32 blocksize,
5736 u64 parent, u64 root_objectid,
5737 struct btrfs_disk_key *key, int level,
5738 u64 hint, u64 empty_size)
fec577fb 5739{
e2fa7227 5740 struct btrfs_key ins;
f0486c68 5741 struct btrfs_block_rsv *block_rsv;
5f39d397 5742 struct extent_buffer *buf;
f0486c68
YZ
5743 u64 flags = 0;
5744 int ret;
5745
fec577fb 5746
f0486c68
YZ
5747 block_rsv = use_block_rsv(trans, root, blocksize);
5748 if (IS_ERR(block_rsv))
5749 return ERR_CAST(block_rsv);
5750
5751 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5752 empty_size, hint, (u64)-1, &ins, 0);
fec577fb 5753 if (ret) {
f0486c68 5754 unuse_block_rsv(block_rsv, blocksize);
54aa1f4d 5755 return ERR_PTR(ret);
fec577fb 5756 }
55c69072 5757
4008c04a
CM
5758 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5759 blocksize, level);
f0486c68
YZ
5760 BUG_ON(IS_ERR(buf));
5761
5762 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5763 if (parent == 0)
5764 parent = ins.objectid;
5765 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5766 } else
5767 BUG_ON(parent > 0);
5768
5769 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5770 struct btrfs_delayed_extent_op *extent_op;
5771 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5772 BUG_ON(!extent_op);
5773 if (key)
5774 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5775 else
5776 memset(&extent_op->key, 0, sizeof(extent_op->key));
5777 extent_op->flags_to_set = flags;
5778 extent_op->update_key = 1;
5779 extent_op->update_flags = 1;
5780 extent_op->is_data = 0;
5781
5782 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5783 ins.offset, parent, root_objectid,
5784 level, BTRFS_ADD_DELAYED_EXTENT,
5785 extent_op);
5786 BUG_ON(ret);
5787 }
fec577fb
CM
5788 return buf;
5789}
a28ec197 5790
2c47e605
YZ
5791struct walk_control {
5792 u64 refs[BTRFS_MAX_LEVEL];
5793 u64 flags[BTRFS_MAX_LEVEL];
5794 struct btrfs_key update_progress;
5795 int stage;
5796 int level;
5797 int shared_level;
5798 int update_ref;
5799 int keep_locks;
1c4850e2
YZ
5800 int reada_slot;
5801 int reada_count;
2c47e605
YZ
5802};
5803
5804#define DROP_REFERENCE 1
5805#define UPDATE_BACKREF 2
5806
1c4850e2
YZ
5807static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5808 struct btrfs_root *root,
5809 struct walk_control *wc,
5810 struct btrfs_path *path)
6407bf6d 5811{
1c4850e2
YZ
5812 u64 bytenr;
5813 u64 generation;
5814 u64 refs;
94fcca9f 5815 u64 flags;
5d4f98a2 5816 u32 nritems;
1c4850e2
YZ
5817 u32 blocksize;
5818 struct btrfs_key key;
5819 struct extent_buffer *eb;
6407bf6d 5820 int ret;
1c4850e2
YZ
5821 int slot;
5822 int nread = 0;
6407bf6d 5823
1c4850e2
YZ
5824 if (path->slots[wc->level] < wc->reada_slot) {
5825 wc->reada_count = wc->reada_count * 2 / 3;
5826 wc->reada_count = max(wc->reada_count, 2);
5827 } else {
5828 wc->reada_count = wc->reada_count * 3 / 2;
5829 wc->reada_count = min_t(int, wc->reada_count,
5830 BTRFS_NODEPTRS_PER_BLOCK(root));
5831 }
7bb86316 5832
1c4850e2
YZ
5833 eb = path->nodes[wc->level];
5834 nritems = btrfs_header_nritems(eb);
5835 blocksize = btrfs_level_size(root, wc->level - 1);
bd56b302 5836
1c4850e2
YZ
5837 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5838 if (nread >= wc->reada_count)
5839 break;
bd56b302 5840
2dd3e67b 5841 cond_resched();
1c4850e2
YZ
5842 bytenr = btrfs_node_blockptr(eb, slot);
5843 generation = btrfs_node_ptr_generation(eb, slot);
2dd3e67b 5844
1c4850e2
YZ
5845 if (slot == path->slots[wc->level])
5846 goto reada;
5d4f98a2 5847
1c4850e2
YZ
5848 if (wc->stage == UPDATE_BACKREF &&
5849 generation <= root->root_key.offset)
bd56b302
CM
5850 continue;
5851
94fcca9f
YZ
5852 /* We don't lock the tree block, it's OK to be racy here */
5853 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5854 &refs, &flags);
5855 BUG_ON(ret);
5856 BUG_ON(refs == 0);
5857
1c4850e2 5858 if (wc->stage == DROP_REFERENCE) {
1c4850e2
YZ
5859 if (refs == 1)
5860 goto reada;
bd56b302 5861
94fcca9f
YZ
5862 if (wc->level == 1 &&
5863 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5864 continue;
1c4850e2
YZ
5865 if (!wc->update_ref ||
5866 generation <= root->root_key.offset)
5867 continue;
5868 btrfs_node_key_to_cpu(eb, &key, slot);
5869 ret = btrfs_comp_cpu_keys(&key,
5870 &wc->update_progress);
5871 if (ret < 0)
5872 continue;
94fcca9f
YZ
5873 } else {
5874 if (wc->level == 1 &&
5875 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5876 continue;
6407bf6d 5877 }
1c4850e2
YZ
5878reada:
5879 ret = readahead_tree_block(root, bytenr, blocksize,
5880 generation);
5881 if (ret)
bd56b302 5882 break;
1c4850e2 5883 nread++;
20524f02 5884 }
1c4850e2 5885 wc->reada_slot = slot;
20524f02 5886}
2c47e605 5887
f82d02d9 5888/*
2c47e605
YZ
5889 * hepler to process tree block while walking down the tree.
5890 *
2c47e605
YZ
5891 * when wc->stage == UPDATE_BACKREF, this function updates
5892 * back refs for pointers in the block.
5893 *
5894 * NOTE: return value 1 means we should stop walking down.
f82d02d9 5895 */
2c47e605 5896static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5d4f98a2 5897 struct btrfs_root *root,
2c47e605 5898 struct btrfs_path *path,
94fcca9f 5899 struct walk_control *wc, int lookup_info)
f82d02d9 5900{
2c47e605
YZ
5901 int level = wc->level;
5902 struct extent_buffer *eb = path->nodes[level];
2c47e605 5903 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
f82d02d9
YZ
5904 int ret;
5905
2c47e605
YZ
5906 if (wc->stage == UPDATE_BACKREF &&
5907 btrfs_header_owner(eb) != root->root_key.objectid)
5908 return 1;
f82d02d9 5909
2c47e605
YZ
5910 /*
5911 * when reference count of tree block is 1, it won't increase
5912 * again. once full backref flag is set, we never clear it.
5913 */
94fcca9f
YZ
5914 if (lookup_info &&
5915 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5916 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
2c47e605
YZ
5917 BUG_ON(!path->locks[level]);
5918 ret = btrfs_lookup_extent_info(trans, root,
5919 eb->start, eb->len,
5920 &wc->refs[level],
5921 &wc->flags[level]);
5922 BUG_ON(ret);
5923 BUG_ON(wc->refs[level] == 0);
5924 }
5d4f98a2 5925
2c47e605
YZ
5926 if (wc->stage == DROP_REFERENCE) {
5927 if (wc->refs[level] > 1)
5928 return 1;
f82d02d9 5929
2c47e605 5930 if (path->locks[level] && !wc->keep_locks) {
bd681513 5931 btrfs_tree_unlock_rw(eb, path->locks[level]);
2c47e605
YZ
5932 path->locks[level] = 0;
5933 }
5934 return 0;
5935 }
f82d02d9 5936
2c47e605
YZ
5937 /* wc->stage == UPDATE_BACKREF */
5938 if (!(wc->flags[level] & flag)) {
5939 BUG_ON(!path->locks[level]);
5940 ret = btrfs_inc_ref(trans, root, eb, 1);
f82d02d9 5941 BUG_ON(ret);
2c47e605
YZ
5942 ret = btrfs_dec_ref(trans, root, eb, 0);
5943 BUG_ON(ret);
5944 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5945 eb->len, flag, 0);
5946 BUG_ON(ret);
5947 wc->flags[level] |= flag;
5948 }
5949
5950 /*
5951 * the block is shared by multiple trees, so it's not good to
5952 * keep the tree lock
5953 */
5954 if (path->locks[level] && level > 0) {
bd681513 5955 btrfs_tree_unlock_rw(eb, path->locks[level]);
2c47e605
YZ
5956 path->locks[level] = 0;
5957 }
5958 return 0;
5959}
5960
1c4850e2
YZ
5961/*
5962 * hepler to process tree block pointer.
5963 *
5964 * when wc->stage == DROP_REFERENCE, this function checks
5965 * reference count of the block pointed to. if the block
5966 * is shared and we need update back refs for the subtree
5967 * rooted at the block, this function changes wc->stage to
5968 * UPDATE_BACKREF. if the block is shared and there is no
5969 * need to update back, this function drops the reference
5970 * to the block.
5971 *
5972 * NOTE: return value 1 means we should stop walking down.
5973 */
5974static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5975 struct btrfs_root *root,
5976 struct btrfs_path *path,
94fcca9f 5977 struct walk_control *wc, int *lookup_info)
1c4850e2
YZ
5978{
5979 u64 bytenr;
5980 u64 generation;
5981 u64 parent;
5982 u32 blocksize;
5983 struct btrfs_key key;
5984 struct extent_buffer *next;
5985 int level = wc->level;
5986 int reada = 0;
5987 int ret = 0;
5988
5989 generation = btrfs_node_ptr_generation(path->nodes[level],
5990 path->slots[level]);
5991 /*
5992 * if the lower level block was created before the snapshot
5993 * was created, we know there is no need to update back refs
5994 * for the subtree
5995 */
5996 if (wc->stage == UPDATE_BACKREF &&
94fcca9f
YZ
5997 generation <= root->root_key.offset) {
5998 *lookup_info = 1;
1c4850e2 5999 return 1;
94fcca9f 6000 }
1c4850e2
YZ
6001
6002 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6003 blocksize = btrfs_level_size(root, level - 1);
6004
6005 next = btrfs_find_tree_block(root, bytenr, blocksize);
6006 if (!next) {
6007 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
90d2c51d
MX
6008 if (!next)
6009 return -ENOMEM;
1c4850e2
YZ
6010 reada = 1;
6011 }
6012 btrfs_tree_lock(next);
6013 btrfs_set_lock_blocking(next);
6014
94fcca9f
YZ
6015 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6016 &wc->refs[level - 1],
6017 &wc->flags[level - 1]);
6018 BUG_ON(ret);
6019 BUG_ON(wc->refs[level - 1] == 0);
6020 *lookup_info = 0;
1c4850e2 6021
94fcca9f 6022 if (wc->stage == DROP_REFERENCE) {
1c4850e2 6023 if (wc->refs[level - 1] > 1) {
94fcca9f
YZ
6024 if (level == 1 &&
6025 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6026 goto skip;
6027
1c4850e2
YZ
6028 if (!wc->update_ref ||
6029 generation <= root->root_key.offset)
6030 goto skip;
6031
6032 btrfs_node_key_to_cpu(path->nodes[level], &key,
6033 path->slots[level]);
6034 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6035 if (ret < 0)
6036 goto skip;
6037
6038 wc->stage = UPDATE_BACKREF;
6039 wc->shared_level = level - 1;
6040 }
94fcca9f
YZ
6041 } else {
6042 if (level == 1 &&
6043 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6044 goto skip;
1c4850e2
YZ
6045 }
6046
6047 if (!btrfs_buffer_uptodate(next, generation)) {
6048 btrfs_tree_unlock(next);
6049 free_extent_buffer(next);
6050 next = NULL;
94fcca9f 6051 *lookup_info = 1;
1c4850e2
YZ
6052 }
6053
6054 if (!next) {
6055 if (reada && level == 1)
6056 reada_walk_down(trans, root, wc, path);
6057 next = read_tree_block(root, bytenr, blocksize, generation);
97d9a8a4
TI
6058 if (!next)
6059 return -EIO;
1c4850e2
YZ
6060 btrfs_tree_lock(next);
6061 btrfs_set_lock_blocking(next);
6062 }
6063
6064 level--;
6065 BUG_ON(level != btrfs_header_level(next));
6066 path->nodes[level] = next;
6067 path->slots[level] = 0;
bd681513 6068 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
1c4850e2
YZ
6069 wc->level = level;
6070 if (wc->level == 1)
6071 wc->reada_slot = 0;
6072 return 0;
6073skip:
6074 wc->refs[level - 1] = 0;
6075 wc->flags[level - 1] = 0;
94fcca9f
YZ
6076 if (wc->stage == DROP_REFERENCE) {
6077 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6078 parent = path->nodes[level]->start;
6079 } else {
6080 BUG_ON(root->root_key.objectid !=
6081 btrfs_header_owner(path->nodes[level]));
6082 parent = 0;
6083 }
1c4850e2 6084
94fcca9f
YZ
6085 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6086 root->root_key.objectid, level - 1, 0);
6087 BUG_ON(ret);
1c4850e2 6088 }
1c4850e2
YZ
6089 btrfs_tree_unlock(next);
6090 free_extent_buffer(next);
94fcca9f 6091 *lookup_info = 1;
1c4850e2
YZ
6092 return 1;
6093}
6094
2c47e605
YZ
6095/*
6096 * hepler to process tree block while walking up the tree.
6097 *
6098 * when wc->stage == DROP_REFERENCE, this function drops
6099 * reference count on the block.
6100 *
6101 * when wc->stage == UPDATE_BACKREF, this function changes
6102 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6103 * to UPDATE_BACKREF previously while processing the block.
6104 *
6105 * NOTE: return value 1 means we should stop walking up.
6106 */
6107static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6108 struct btrfs_root *root,
6109 struct btrfs_path *path,
6110 struct walk_control *wc)
6111{
f0486c68 6112 int ret;
2c47e605
YZ
6113 int level = wc->level;
6114 struct extent_buffer *eb = path->nodes[level];
6115 u64 parent = 0;
6116
6117 if (wc->stage == UPDATE_BACKREF) {
6118 BUG_ON(wc->shared_level < level);
6119 if (level < wc->shared_level)
6120 goto out;
6121
2c47e605
YZ
6122 ret = find_next_key(path, level + 1, &wc->update_progress);
6123 if (ret > 0)
6124 wc->update_ref = 0;
6125
6126 wc->stage = DROP_REFERENCE;
6127 wc->shared_level = -1;
6128 path->slots[level] = 0;
6129
6130 /*
6131 * check reference count again if the block isn't locked.
6132 * we should start walking down the tree again if reference
6133 * count is one.
6134 */
6135 if (!path->locks[level]) {
6136 BUG_ON(level == 0);
6137 btrfs_tree_lock(eb);
6138 btrfs_set_lock_blocking(eb);
bd681513 6139 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
2c47e605
YZ
6140
6141 ret = btrfs_lookup_extent_info(trans, root,
6142 eb->start, eb->len,
6143 &wc->refs[level],
6144 &wc->flags[level]);
f82d02d9 6145 BUG_ON(ret);
2c47e605
YZ
6146 BUG_ON(wc->refs[level] == 0);
6147 if (wc->refs[level] == 1) {
bd681513 6148 btrfs_tree_unlock_rw(eb, path->locks[level]);
2c47e605
YZ
6149 return 1;
6150 }
f82d02d9 6151 }
2c47e605 6152 }
f82d02d9 6153
2c47e605
YZ
6154 /* wc->stage == DROP_REFERENCE */
6155 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5d4f98a2 6156
2c47e605
YZ
6157 if (wc->refs[level] == 1) {
6158 if (level == 0) {
6159 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6160 ret = btrfs_dec_ref(trans, root, eb, 1);
6161 else
6162 ret = btrfs_dec_ref(trans, root, eb, 0);
6163 BUG_ON(ret);
6164 }
6165 /* make block locked assertion in clean_tree_block happy */
6166 if (!path->locks[level] &&
6167 btrfs_header_generation(eb) == trans->transid) {
6168 btrfs_tree_lock(eb);
6169 btrfs_set_lock_blocking(eb);
bd681513 6170 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
2c47e605
YZ
6171 }
6172 clean_tree_block(trans, root, eb);
6173 }
6174
6175 if (eb == root->node) {
6176 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6177 parent = eb->start;
6178 else
6179 BUG_ON(root->root_key.objectid !=
6180 btrfs_header_owner(eb));
6181 } else {
6182 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6183 parent = path->nodes[level + 1]->start;
6184 else
6185 BUG_ON(root->root_key.objectid !=
6186 btrfs_header_owner(path->nodes[level + 1]));
f82d02d9 6187 }
f82d02d9 6188
f0486c68 6189 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
2c47e605
YZ
6190out:
6191 wc->refs[level] = 0;
6192 wc->flags[level] = 0;
f0486c68 6193 return 0;
2c47e605
YZ
6194}
6195
6196static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6197 struct btrfs_root *root,
6198 struct btrfs_path *path,
6199 struct walk_control *wc)
6200{
2c47e605 6201 int level = wc->level;
94fcca9f 6202 int lookup_info = 1;
2c47e605
YZ
6203 int ret;
6204
6205 while (level >= 0) {
94fcca9f 6206 ret = walk_down_proc(trans, root, path, wc, lookup_info);
2c47e605
YZ
6207 if (ret > 0)
6208 break;
6209
6210 if (level == 0)
6211 break;
6212
7a7965f8
YZ
6213 if (path->slots[level] >=
6214 btrfs_header_nritems(path->nodes[level]))
6215 break;
6216
94fcca9f 6217 ret = do_walk_down(trans, root, path, wc, &lookup_info);
1c4850e2
YZ
6218 if (ret > 0) {
6219 path->slots[level]++;
6220 continue;
90d2c51d
MX
6221 } else if (ret < 0)
6222 return ret;
1c4850e2 6223 level = wc->level;
f82d02d9 6224 }
f82d02d9
YZ
6225 return 0;
6226}
6227
d397712b 6228static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
98ed5174 6229 struct btrfs_root *root,
f82d02d9 6230 struct btrfs_path *path,
2c47e605 6231 struct walk_control *wc, int max_level)
20524f02 6232{
2c47e605 6233 int level = wc->level;
20524f02 6234 int ret;
9f3a7427 6235
2c47e605
YZ
6236 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6237 while (level < max_level && path->nodes[level]) {
6238 wc->level = level;
6239 if (path->slots[level] + 1 <
6240 btrfs_header_nritems(path->nodes[level])) {
6241 path->slots[level]++;
20524f02
CM
6242 return 0;
6243 } else {
2c47e605
YZ
6244 ret = walk_up_proc(trans, root, path, wc);
6245 if (ret > 0)
6246 return 0;
bd56b302 6247
2c47e605 6248 if (path->locks[level]) {
bd681513
CM
6249 btrfs_tree_unlock_rw(path->nodes[level],
6250 path->locks[level]);
2c47e605 6251 path->locks[level] = 0;
f82d02d9 6252 }
2c47e605
YZ
6253 free_extent_buffer(path->nodes[level]);
6254 path->nodes[level] = NULL;
6255 level++;
20524f02
CM
6256 }
6257 }
6258 return 1;
6259}
6260
9aca1d51 6261/*
2c47e605
YZ
6262 * drop a subvolume tree.
6263 *
6264 * this function traverses the tree freeing any blocks that only
6265 * referenced by the tree.
6266 *
6267 * when a shared tree block is found. this function decreases its
6268 * reference count by one. if update_ref is true, this function
6269 * also make sure backrefs for the shared block and all lower level
6270 * blocks are properly updated.
9aca1d51 6271 */
cb1b69f4
TI
6272void btrfs_drop_snapshot(struct btrfs_root *root,
6273 struct btrfs_block_rsv *block_rsv, int update_ref)
20524f02 6274{
5caf2a00 6275 struct btrfs_path *path;
2c47e605
YZ
6276 struct btrfs_trans_handle *trans;
6277 struct btrfs_root *tree_root = root->fs_info->tree_root;
9f3a7427 6278 struct btrfs_root_item *root_item = &root->root_item;
2c47e605
YZ
6279 struct walk_control *wc;
6280 struct btrfs_key key;
6281 int err = 0;
6282 int ret;
6283 int level;
20524f02 6284
5caf2a00 6285 path = btrfs_alloc_path();
cb1b69f4
TI
6286 if (!path) {
6287 err = -ENOMEM;
6288 goto out;
6289 }
20524f02 6290
2c47e605 6291 wc = kzalloc(sizeof(*wc), GFP_NOFS);
38a1a919
MF
6292 if (!wc) {
6293 btrfs_free_path(path);
cb1b69f4
TI
6294 err = -ENOMEM;
6295 goto out;
38a1a919 6296 }
2c47e605 6297
a22285a6 6298 trans = btrfs_start_transaction(tree_root, 0);
98d5dc13
TI
6299 BUG_ON(IS_ERR(trans));
6300
3fd0a558
YZ
6301 if (block_rsv)
6302 trans->block_rsv = block_rsv;
2c47e605 6303
9f3a7427 6304 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2c47e605 6305 level = btrfs_header_level(root->node);
5d4f98a2
YZ
6306 path->nodes[level] = btrfs_lock_root_node(root);
6307 btrfs_set_lock_blocking(path->nodes[level]);
9f3a7427 6308 path->slots[level] = 0;
bd681513 6309 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
2c47e605
YZ
6310 memset(&wc->update_progress, 0,
6311 sizeof(wc->update_progress));
9f3a7427 6312 } else {
9f3a7427 6313 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2c47e605
YZ
6314 memcpy(&wc->update_progress, &key,
6315 sizeof(wc->update_progress));
6316
6702ed49 6317 level = root_item->drop_level;
2c47e605 6318 BUG_ON(level == 0);
6702ed49 6319 path->lowest_level = level;
2c47e605
YZ
6320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6321 path->lowest_level = 0;
6322 if (ret < 0) {
6323 err = ret;
cb1b69f4 6324 goto out_free;
9f3a7427 6325 }
1c4850e2 6326 WARN_ON(ret > 0);
2c47e605 6327
7d9eb12c
CM
6328 /*
6329 * unlock our path, this is safe because only this
6330 * function is allowed to delete this snapshot
6331 */
5d4f98a2 6332 btrfs_unlock_up_safe(path, 0);
2c47e605
YZ
6333
6334 level = btrfs_header_level(root->node);
6335 while (1) {
6336 btrfs_tree_lock(path->nodes[level]);
6337 btrfs_set_lock_blocking(path->nodes[level]);
6338
6339 ret = btrfs_lookup_extent_info(trans, root,
6340 path->nodes[level]->start,
6341 path->nodes[level]->len,
6342 &wc->refs[level],
6343 &wc->flags[level]);
6344 BUG_ON(ret);
6345 BUG_ON(wc->refs[level] == 0);
6346
6347 if (level == root_item->drop_level)
6348 break;
6349
6350 btrfs_tree_unlock(path->nodes[level]);
6351 WARN_ON(wc->refs[level] != 1);
6352 level--;
6353 }
9f3a7427 6354 }
2c47e605
YZ
6355
6356 wc->level = level;
6357 wc->shared_level = -1;
6358 wc->stage = DROP_REFERENCE;
6359 wc->update_ref = update_ref;
6360 wc->keep_locks = 0;
1c4850e2 6361 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
2c47e605 6362
d397712b 6363 while (1) {
2c47e605
YZ
6364 ret = walk_down_tree(trans, root, path, wc);
6365 if (ret < 0) {
6366 err = ret;
20524f02 6367 break;
2c47e605 6368 }
9aca1d51 6369
2c47e605
YZ
6370 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6371 if (ret < 0) {
6372 err = ret;
20524f02 6373 break;
2c47e605
YZ
6374 }
6375
6376 if (ret > 0) {
6377 BUG_ON(wc->stage != DROP_REFERENCE);
e7a84565
CM
6378 break;
6379 }
2c47e605
YZ
6380
6381 if (wc->stage == DROP_REFERENCE) {
6382 level = wc->level;
6383 btrfs_node_key(path->nodes[level],
6384 &root_item->drop_progress,
6385 path->slots[level]);
6386 root_item->drop_level = level;
6387 }
6388
6389 BUG_ON(wc->level == 0);
3fd0a558 6390 if (btrfs_should_end_transaction(trans, tree_root)) {
2c47e605
YZ
6391 ret = btrfs_update_root(trans, tree_root,
6392 &root->root_key,
6393 root_item);
6394 BUG_ON(ret);
6395
3fd0a558 6396 btrfs_end_transaction_throttle(trans, tree_root);
a22285a6 6397 trans = btrfs_start_transaction(tree_root, 0);
98d5dc13 6398 BUG_ON(IS_ERR(trans));
3fd0a558
YZ
6399 if (block_rsv)
6400 trans->block_rsv = block_rsv;
c3e69d58 6401 }
20524f02 6402 }
b3b4aa74 6403 btrfs_release_path(path);
2c47e605
YZ
6404 BUG_ON(err);
6405
6406 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6407 BUG_ON(ret);
6408
76dda93c
YZ
6409 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6410 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6411 NULL, NULL);
6412 BUG_ON(ret < 0);
6413 if (ret > 0) {
84cd948c
JB
6414 /* if we fail to delete the orphan item this time
6415 * around, it'll get picked up the next time.
6416 *
6417 * The most common failure here is just -ENOENT.
6418 */
6419 btrfs_del_orphan_item(trans, tree_root,
6420 root->root_key.objectid);
76dda93c
YZ
6421 }
6422 }
6423
6424 if (root->in_radix) {
6425 btrfs_free_fs_root(tree_root->fs_info, root);
6426 } else {
6427 free_extent_buffer(root->node);
6428 free_extent_buffer(root->commit_root);
6429 kfree(root);
6430 }
cb1b69f4 6431out_free:
3fd0a558 6432 btrfs_end_transaction_throttle(trans, tree_root);
2c47e605 6433 kfree(wc);
5caf2a00 6434 btrfs_free_path(path);
cb1b69f4
TI
6435out:
6436 if (err)
6437 btrfs_std_error(root->fs_info, err);
6438 return;
20524f02 6439}
9078a3e1 6440
2c47e605
YZ
6441/*
6442 * drop subtree rooted at tree block 'node'.
6443 *
6444 * NOTE: this function will unlock and release tree block 'node'
6445 */
f82d02d9
YZ
6446int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6447 struct btrfs_root *root,
6448 struct extent_buffer *node,
6449 struct extent_buffer *parent)
6450{
6451 struct btrfs_path *path;
2c47e605 6452 struct walk_control *wc;
f82d02d9
YZ
6453 int level;
6454 int parent_level;
6455 int ret = 0;
6456 int wret;
6457
2c47e605
YZ
6458 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6459
f82d02d9 6460 path = btrfs_alloc_path();
db5b493a
TI
6461 if (!path)
6462 return -ENOMEM;
f82d02d9 6463
2c47e605 6464 wc = kzalloc(sizeof(*wc), GFP_NOFS);
db5b493a
TI
6465 if (!wc) {
6466 btrfs_free_path(path);
6467 return -ENOMEM;
6468 }
2c47e605 6469
b9447ef8 6470 btrfs_assert_tree_locked(parent);
f82d02d9
YZ
6471 parent_level = btrfs_header_level(parent);
6472 extent_buffer_get(parent);
6473 path->nodes[parent_level] = parent;
6474 path->slots[parent_level] = btrfs_header_nritems(parent);
6475
b9447ef8 6476 btrfs_assert_tree_locked(node);
f82d02d9 6477 level = btrfs_header_level(node);
f82d02d9
YZ
6478 path->nodes[level] = node;
6479 path->slots[level] = 0;
bd681513 6480 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
2c47e605
YZ
6481
6482 wc->refs[parent_level] = 1;
6483 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6484 wc->level = level;
6485 wc->shared_level = -1;
6486 wc->stage = DROP_REFERENCE;
6487 wc->update_ref = 0;
6488 wc->keep_locks = 1;
1c4850e2 6489 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
f82d02d9
YZ
6490
6491 while (1) {
2c47e605
YZ
6492 wret = walk_down_tree(trans, root, path, wc);
6493 if (wret < 0) {
f82d02d9 6494 ret = wret;
f82d02d9 6495 break;
2c47e605 6496 }
f82d02d9 6497
2c47e605 6498 wret = walk_up_tree(trans, root, path, wc, parent_level);
f82d02d9
YZ
6499 if (wret < 0)
6500 ret = wret;
6501 if (wret != 0)
6502 break;
6503 }
6504
2c47e605 6505 kfree(wc);
f82d02d9
YZ
6506 btrfs_free_path(path);
6507 return ret;
6508}
6509
ec44a35c
CM
6510static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6511{
6512 u64 num_devices;
6513 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6514 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6515
cd02dca5
CM
6516 /*
6517 * we add in the count of missing devices because we want
6518 * to make sure that any RAID levels on a degraded FS
6519 * continue to be honored.
6520 */
6521 num_devices = root->fs_info->fs_devices->rw_devices +
6522 root->fs_info->fs_devices->missing_devices;
6523
ec44a35c
CM
6524 if (num_devices == 1) {
6525 stripped |= BTRFS_BLOCK_GROUP_DUP;
6526 stripped = flags & ~stripped;
6527
6528 /* turn raid0 into single device chunks */
6529 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6530 return stripped;
6531
6532 /* turn mirroring into duplication */
6533 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6534 BTRFS_BLOCK_GROUP_RAID10))
6535 return stripped | BTRFS_BLOCK_GROUP_DUP;
6536 return flags;
6537 } else {
6538 /* they already had raid on here, just return */
ec44a35c
CM
6539 if (flags & stripped)
6540 return flags;
6541
6542 stripped |= BTRFS_BLOCK_GROUP_DUP;
6543 stripped = flags & ~stripped;
6544
6545 /* switch duplicated blocks with raid1 */
6546 if (flags & BTRFS_BLOCK_GROUP_DUP)
6547 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6548
6549 /* turn single device chunks into raid0 */
6550 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6551 }
6552 return flags;
6553}
6554
199c36ea 6555static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
0ef3e66b 6556{
f0486c68
YZ
6557 struct btrfs_space_info *sinfo = cache->space_info;
6558 u64 num_bytes;
199c36ea 6559 u64 min_allocable_bytes;
f0486c68 6560 int ret = -ENOSPC;
0ef3e66b 6561
c286ac48 6562
199c36ea
MX
6563 /*
6564 * We need some metadata space and system metadata space for
6565 * allocating chunks in some corner cases until we force to set
6566 * it to be readonly.
6567 */
6568 if ((sinfo->flags &
6569 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
6570 !force)
6571 min_allocable_bytes = 1 * 1024 * 1024;
6572 else
6573 min_allocable_bytes = 0;
6574
f0486c68
YZ
6575 spin_lock(&sinfo->lock);
6576 spin_lock(&cache->lock);
61cfea9b
W
6577
6578 if (cache->ro) {
6579 ret = 0;
6580 goto out;
6581 }
6582
f0486c68
YZ
6583 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6584 cache->bytes_super - btrfs_block_group_used(&cache->item);
6585
6586 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
37be25bc
JB
6587 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
6588 min_allocable_bytes <= sinfo->total_bytes) {
f0486c68 6589 sinfo->bytes_readonly += num_bytes;
f0486c68
YZ
6590 cache->ro = 1;
6591 ret = 0;
6592 }
61cfea9b 6593out:
f0486c68
YZ
6594 spin_unlock(&cache->lock);
6595 spin_unlock(&sinfo->lock);
6596 return ret;
6597}
7d9eb12c 6598
f0486c68
YZ
6599int btrfs_set_block_group_ro(struct btrfs_root *root,
6600 struct btrfs_block_group_cache *cache)
c286ac48 6601
f0486c68
YZ
6602{
6603 struct btrfs_trans_handle *trans;
6604 u64 alloc_flags;
6605 int ret;
7d9eb12c 6606
f0486c68 6607 BUG_ON(cache->ro);
0ef3e66b 6608
ff5714cc 6609 trans = btrfs_join_transaction(root);
f0486c68 6610 BUG_ON(IS_ERR(trans));
5d4f98a2 6611
f0486c68
YZ
6612 alloc_flags = update_block_group_flags(root, cache->flags);
6613 if (alloc_flags != cache->flags)
0e4f8f88
CM
6614 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6615 CHUNK_ALLOC_FORCE);
5d4f98a2 6616
199c36ea 6617 ret = set_block_group_ro(cache, 0);
f0486c68
YZ
6618 if (!ret)
6619 goto out;
6620 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
0e4f8f88
CM
6621 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6622 CHUNK_ALLOC_FORCE);
f0486c68
YZ
6623 if (ret < 0)
6624 goto out;
199c36ea 6625 ret = set_block_group_ro(cache, 0);
f0486c68
YZ
6626out:
6627 btrfs_end_transaction(trans, root);
6628 return ret;
6629}
5d4f98a2 6630
c87f08ca
CM
6631int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6632 struct btrfs_root *root, u64 type)
6633{
6634 u64 alloc_flags = get_alloc_profile(root, type);
0e4f8f88
CM
6635 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6636 CHUNK_ALLOC_FORCE);
c87f08ca
CM
6637}
6638
6d07bcec
MX
6639/*
6640 * helper to account the unused space of all the readonly block group in the
6641 * list. takes mirrors into account.
6642 */
6643static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6644{
6645 struct btrfs_block_group_cache *block_group;
6646 u64 free_bytes = 0;
6647 int factor;
6648
6649 list_for_each_entry(block_group, groups_list, list) {
6650 spin_lock(&block_group->lock);
6651
6652 if (!block_group->ro) {
6653 spin_unlock(&block_group->lock);
6654 continue;
6655 }
6656
6657 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6658 BTRFS_BLOCK_GROUP_RAID10 |
6659 BTRFS_BLOCK_GROUP_DUP))
6660 factor = 2;
6661 else
6662 factor = 1;
6663
6664 free_bytes += (block_group->key.offset -
6665 btrfs_block_group_used(&block_group->item)) *
6666 factor;
6667
6668 spin_unlock(&block_group->lock);
6669 }
6670
6671 return free_bytes;
6672}
6673
6674/*
6675 * helper to account the unused space of all the readonly block group in the
6676 * space_info. takes mirrors into account.
6677 */
6678u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6679{
6680 int i;
6681 u64 free_bytes = 0;
6682
6683 spin_lock(&sinfo->lock);
6684
6685 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6686 if (!list_empty(&sinfo->block_groups[i]))
6687 free_bytes += __btrfs_get_ro_block_group_free_space(
6688 &sinfo->block_groups[i]);
6689
6690 spin_unlock(&sinfo->lock);
6691
6692 return free_bytes;
6693}
6694
f0486c68
YZ
6695int btrfs_set_block_group_rw(struct btrfs_root *root,
6696 struct btrfs_block_group_cache *cache)
5d4f98a2 6697{
f0486c68
YZ
6698 struct btrfs_space_info *sinfo = cache->space_info;
6699 u64 num_bytes;
6700
6701 BUG_ON(!cache->ro);
6702
6703 spin_lock(&sinfo->lock);
6704 spin_lock(&cache->lock);
6705 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6706 cache->bytes_super - btrfs_block_group_used(&cache->item);
6707 sinfo->bytes_readonly -= num_bytes;
6708 cache->ro = 0;
6709 spin_unlock(&cache->lock);
6710 spin_unlock(&sinfo->lock);
5d4f98a2
YZ
6711 return 0;
6712}
6713
ba1bf481
JB
6714/*
6715 * checks to see if its even possible to relocate this block group.
6716 *
6717 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6718 * ok to go ahead and try.
6719 */
6720int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
1a40e23b 6721{
ba1bf481
JB
6722 struct btrfs_block_group_cache *block_group;
6723 struct btrfs_space_info *space_info;
6724 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6725 struct btrfs_device *device;
cdcb725c 6726 u64 min_free;
6719db6a
JB
6727 u64 dev_min = 1;
6728 u64 dev_nr = 0;
cdcb725c 6729 int index;
ba1bf481
JB
6730 int full = 0;
6731 int ret = 0;
1a40e23b 6732
ba1bf481 6733 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1a40e23b 6734
ba1bf481
JB
6735 /* odd, couldn't find the block group, leave it alone */
6736 if (!block_group)
6737 return -1;
1a40e23b 6738
cdcb725c 6739 min_free = btrfs_block_group_used(&block_group->item);
6740
ba1bf481 6741 /* no bytes used, we're good */
cdcb725c 6742 if (!min_free)
1a40e23b
ZY
6743 goto out;
6744
ba1bf481
JB
6745 space_info = block_group->space_info;
6746 spin_lock(&space_info->lock);
17d217fe 6747
ba1bf481 6748 full = space_info->full;
17d217fe 6749
ba1bf481
JB
6750 /*
6751 * if this is the last block group we have in this space, we can't
7ce618db
CM
6752 * relocate it unless we're able to allocate a new chunk below.
6753 *
6754 * Otherwise, we need to make sure we have room in the space to handle
6755 * all of the extents from this block group. If we can, we're good
ba1bf481 6756 */
7ce618db 6757 if ((space_info->total_bytes != block_group->key.offset) &&
cdcb725c 6758 (space_info->bytes_used + space_info->bytes_reserved +
6759 space_info->bytes_pinned + space_info->bytes_readonly +
6760 min_free < space_info->total_bytes)) {
ba1bf481
JB
6761 spin_unlock(&space_info->lock);
6762 goto out;
17d217fe 6763 }
ba1bf481 6764 spin_unlock(&space_info->lock);
ea8c2819 6765
ba1bf481
JB
6766 /*
6767 * ok we don't have enough space, but maybe we have free space on our
6768 * devices to allocate new chunks for relocation, so loop through our
6769 * alloc devices and guess if we have enough space. However, if we
6770 * were marked as full, then we know there aren't enough chunks, and we
6771 * can just return.
6772 */
6773 ret = -1;
6774 if (full)
6775 goto out;
ea8c2819 6776
cdcb725c 6777 /*
6778 * index:
6779 * 0: raid10
6780 * 1: raid1
6781 * 2: dup
6782 * 3: raid0
6783 * 4: single
6784 */
6785 index = get_block_group_index(block_group);
6786 if (index == 0) {
6787 dev_min = 4;
6719db6a
JB
6788 /* Divide by 2 */
6789 min_free >>= 1;
cdcb725c 6790 } else if (index == 1) {
6791 dev_min = 2;
6792 } else if (index == 2) {
6719db6a
JB
6793 /* Multiply by 2 */
6794 min_free <<= 1;
cdcb725c 6795 } else if (index == 3) {
6796 dev_min = fs_devices->rw_devices;
6719db6a 6797 do_div(min_free, dev_min);
cdcb725c 6798 }
6799
ba1bf481
JB
6800 mutex_lock(&root->fs_info->chunk_mutex);
6801 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7bfc837d 6802 u64 dev_offset;
56bec294 6803
ba1bf481
JB
6804 /*
6805 * check to make sure we can actually find a chunk with enough
6806 * space to fit our block group in.
6807 */
6808 if (device->total_bytes > device->bytes_used + min_free) {
6809 ret = find_free_dev_extent(NULL, device, min_free,
7bfc837d 6810 &dev_offset, NULL);
ba1bf481 6811 if (!ret)
cdcb725c 6812 dev_nr++;
6813
6814 if (dev_nr >= dev_min)
73e48b27 6815 break;
cdcb725c 6816
ba1bf481 6817 ret = -1;
725c8463 6818 }
edbd8d4e 6819 }
ba1bf481 6820 mutex_unlock(&root->fs_info->chunk_mutex);
edbd8d4e 6821out:
ba1bf481 6822 btrfs_put_block_group(block_group);
edbd8d4e
CM
6823 return ret;
6824}
6825
b2950863
CH
6826static int find_first_block_group(struct btrfs_root *root,
6827 struct btrfs_path *path, struct btrfs_key *key)
0b86a832 6828{
925baedd 6829 int ret = 0;
0b86a832
CM
6830 struct btrfs_key found_key;
6831 struct extent_buffer *leaf;
6832 int slot;
edbd8d4e 6833
0b86a832
CM
6834 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6835 if (ret < 0)
925baedd
CM
6836 goto out;
6837
d397712b 6838 while (1) {
0b86a832 6839 slot = path->slots[0];
edbd8d4e 6840 leaf = path->nodes[0];
0b86a832
CM
6841 if (slot >= btrfs_header_nritems(leaf)) {
6842 ret = btrfs_next_leaf(root, path);
6843 if (ret == 0)
6844 continue;
6845 if (ret < 0)
925baedd 6846 goto out;
0b86a832 6847 break;
edbd8d4e 6848 }
0b86a832 6849 btrfs_item_key_to_cpu(leaf, &found_key, slot);
edbd8d4e 6850
0b86a832 6851 if (found_key.objectid >= key->objectid &&
925baedd
CM
6852 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6853 ret = 0;
6854 goto out;
6855 }
0b86a832 6856 path->slots[0]++;
edbd8d4e 6857 }
925baedd 6858out:
0b86a832 6859 return ret;
edbd8d4e
CM
6860}
6861
0af3d00b
JB
6862void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6863{
6864 struct btrfs_block_group_cache *block_group;
6865 u64 last = 0;
6866
6867 while (1) {
6868 struct inode *inode;
6869
6870 block_group = btrfs_lookup_first_block_group(info, last);
6871 while (block_group) {
6872 spin_lock(&block_group->lock);
6873 if (block_group->iref)
6874 break;
6875 spin_unlock(&block_group->lock);
6876 block_group = next_block_group(info->tree_root,
6877 block_group);
6878 }
6879 if (!block_group) {
6880 if (last == 0)
6881 break;
6882 last = 0;
6883 continue;
6884 }
6885
6886 inode = block_group->inode;
6887 block_group->iref = 0;
6888 block_group->inode = NULL;
6889 spin_unlock(&block_group->lock);
6890 iput(inode);
6891 last = block_group->key.objectid + block_group->key.offset;
6892 btrfs_put_block_group(block_group);
6893 }
6894}
6895
1a40e23b
ZY
6896int btrfs_free_block_groups(struct btrfs_fs_info *info)
6897{
6898 struct btrfs_block_group_cache *block_group;
4184ea7f 6899 struct btrfs_space_info *space_info;
11833d66 6900 struct btrfs_caching_control *caching_ctl;
1a40e23b
ZY
6901 struct rb_node *n;
6902
11833d66
YZ
6903 down_write(&info->extent_commit_sem);
6904 while (!list_empty(&info->caching_block_groups)) {
6905 caching_ctl = list_entry(info->caching_block_groups.next,
6906 struct btrfs_caching_control, list);
6907 list_del(&caching_ctl->list);
6908 put_caching_control(caching_ctl);
6909 }
6910 up_write(&info->extent_commit_sem);
6911
1a40e23b
ZY
6912 spin_lock(&info->block_group_cache_lock);
6913 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6914 block_group = rb_entry(n, struct btrfs_block_group_cache,
6915 cache_node);
1a40e23b
ZY
6916 rb_erase(&block_group->cache_node,
6917 &info->block_group_cache_tree);
d899e052
YZ
6918 spin_unlock(&info->block_group_cache_lock);
6919
80eb234a 6920 down_write(&block_group->space_info->groups_sem);
1a40e23b 6921 list_del(&block_group->list);
80eb234a 6922 up_write(&block_group->space_info->groups_sem);
d2fb3437 6923
817d52f8 6924 if (block_group->cached == BTRFS_CACHE_STARTED)
11833d66 6925 wait_block_group_cache_done(block_group);
817d52f8 6926
3c14874a
JB
6927 /*
6928 * We haven't cached this block group, which means we could
6929 * possibly have excluded extents on this block group.
6930 */
6931 if (block_group->cached == BTRFS_CACHE_NO)
6932 free_excluded_extents(info->extent_root, block_group);
6933
817d52f8 6934 btrfs_remove_free_space_cache(block_group);
11dfe35a 6935 btrfs_put_block_group(block_group);
d899e052
YZ
6936
6937 spin_lock(&info->block_group_cache_lock);
1a40e23b
ZY
6938 }
6939 spin_unlock(&info->block_group_cache_lock);
4184ea7f
CM
6940
6941 /* now that all the block groups are freed, go through and
6942 * free all the space_info structs. This is only called during
6943 * the final stages of unmount, and so we know nobody is
6944 * using them. We call synchronize_rcu() once before we start,
6945 * just to be on the safe side.
6946 */
6947 synchronize_rcu();
6948
8929ecfa
YZ
6949 release_global_block_rsv(info);
6950
4184ea7f
CM
6951 while(!list_empty(&info->space_info)) {
6952 space_info = list_entry(info->space_info.next,
6953 struct btrfs_space_info,
6954 list);
f0486c68 6955 if (space_info->bytes_pinned > 0 ||
fb25e914
JB
6956 space_info->bytes_reserved > 0 ||
6957 space_info->bytes_may_use > 0) {
f0486c68
YZ
6958 WARN_ON(1);
6959 dump_space_info(space_info, 0, 0);
6960 }
4184ea7f
CM
6961 list_del(&space_info->list);
6962 kfree(space_info);
6963 }
1a40e23b
ZY
6964 return 0;
6965}
6966
b742bb82
YZ
6967static void __link_block_group(struct btrfs_space_info *space_info,
6968 struct btrfs_block_group_cache *cache)
6969{
6970 int index = get_block_group_index(cache);
6971
6972 down_write(&space_info->groups_sem);
6973 list_add_tail(&cache->list, &space_info->block_groups[index]);
6974 up_write(&space_info->groups_sem);
6975}
6976
9078a3e1
CM
6977int btrfs_read_block_groups(struct btrfs_root *root)
6978{
6979 struct btrfs_path *path;
6980 int ret;
9078a3e1 6981 struct btrfs_block_group_cache *cache;
be744175 6982 struct btrfs_fs_info *info = root->fs_info;
6324fbf3 6983 struct btrfs_space_info *space_info;
9078a3e1
CM
6984 struct btrfs_key key;
6985 struct btrfs_key found_key;
5f39d397 6986 struct extent_buffer *leaf;
0af3d00b
JB
6987 int need_clear = 0;
6988 u64 cache_gen;
96b5179d 6989
be744175 6990 root = info->extent_root;
9078a3e1 6991 key.objectid = 0;
0b86a832 6992 key.offset = 0;
9078a3e1 6993 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
9078a3e1
CM
6994 path = btrfs_alloc_path();
6995 if (!path)
6996 return -ENOMEM;
026fd317 6997 path->reada = 1;
9078a3e1 6998
0af3d00b
JB
6999 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
7000 if (cache_gen != 0 &&
7001 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
7002 need_clear = 1;
88c2ba3b
JB
7003 if (btrfs_test_opt(root, CLEAR_CACHE))
7004 need_clear = 1;
8216ef86
JB
7005 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
7006 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
0af3d00b 7007
d397712b 7008 while (1) {
0b86a832 7009 ret = find_first_block_group(root, path, &key);
b742bb82
YZ
7010 if (ret > 0)
7011 break;
0b86a832
CM
7012 if (ret != 0)
7013 goto error;
5f39d397
CM
7014 leaf = path->nodes[0];
7015 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8f18cf13 7016 cache = kzalloc(sizeof(*cache), GFP_NOFS);
9078a3e1 7017 if (!cache) {
0b86a832 7018 ret = -ENOMEM;
f0486c68 7019 goto error;
9078a3e1 7020 }
34d52cb6
LZ
7021 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7022 GFP_NOFS);
7023 if (!cache->free_space_ctl) {
7024 kfree(cache);
7025 ret = -ENOMEM;
7026 goto error;
7027 }
3e1ad54f 7028
d2fb3437 7029 atomic_set(&cache->count, 1);
c286ac48 7030 spin_lock_init(&cache->lock);
817d52f8 7031 cache->fs_info = info;
0f9dd46c 7032 INIT_LIST_HEAD(&cache->list);
fa9c0d79 7033 INIT_LIST_HEAD(&cache->cluster_list);
96303081 7034
0af3d00b
JB
7035 if (need_clear)
7036 cache->disk_cache_state = BTRFS_DC_CLEAR;
7037
5f39d397
CM
7038 read_extent_buffer(leaf, &cache->item,
7039 btrfs_item_ptr_offset(leaf, path->slots[0]),
7040 sizeof(cache->item));
9078a3e1 7041 memcpy(&cache->key, &found_key, sizeof(found_key));
0b86a832 7042
9078a3e1 7043 key.objectid = found_key.objectid + found_key.offset;
b3b4aa74 7044 btrfs_release_path(path);
0b86a832 7045 cache->flags = btrfs_block_group_flags(&cache->item);
817d52f8
JB
7046 cache->sectorsize = root->sectorsize;
7047
34d52cb6
LZ
7048 btrfs_init_free_space_ctl(cache);
7049
3c14874a
JB
7050 /*
7051 * We need to exclude the super stripes now so that the space
7052 * info has super bytes accounted for, otherwise we'll think
7053 * we have more space than we actually do.
7054 */
7055 exclude_super_stripes(root, cache);
7056
817d52f8
JB
7057 /*
7058 * check for two cases, either we are full, and therefore
7059 * don't need to bother with the caching work since we won't
7060 * find any space, or we are empty, and we can just add all
7061 * the space in and be done with it. This saves us _alot_ of
7062 * time, particularly in the full case.
7063 */
7064 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
11833d66 7065 cache->last_byte_to_unpin = (u64)-1;
817d52f8 7066 cache->cached = BTRFS_CACHE_FINISHED;
1b2da372 7067 free_excluded_extents(root, cache);
817d52f8 7068 } else if (btrfs_block_group_used(&cache->item) == 0) {
11833d66 7069 cache->last_byte_to_unpin = (u64)-1;
817d52f8
JB
7070 cache->cached = BTRFS_CACHE_FINISHED;
7071 add_new_free_space(cache, root->fs_info,
7072 found_key.objectid,
7073 found_key.objectid +
7074 found_key.offset);
11833d66 7075 free_excluded_extents(root, cache);
817d52f8 7076 }
96b5179d 7077
6324fbf3
CM
7078 ret = update_space_info(info, cache->flags, found_key.offset,
7079 btrfs_block_group_used(&cache->item),
7080 &space_info);
7081 BUG_ON(ret);
7082 cache->space_info = space_info;
1b2da372 7083 spin_lock(&cache->space_info->lock);
f0486c68 7084 cache->space_info->bytes_readonly += cache->bytes_super;
1b2da372
JB
7085 spin_unlock(&cache->space_info->lock);
7086
b742bb82 7087 __link_block_group(space_info, cache);
0f9dd46c
JB
7088
7089 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7090 BUG_ON(ret);
75ccf47d
CM
7091
7092 set_avail_alloc_bits(root->fs_info, cache->flags);
2b82032c 7093 if (btrfs_chunk_readonly(root, cache->key.objectid))
199c36ea 7094 set_block_group_ro(cache, 1);
9078a3e1 7095 }
b742bb82
YZ
7096
7097 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7098 if (!(get_alloc_profile(root, space_info->flags) &
7099 (BTRFS_BLOCK_GROUP_RAID10 |
7100 BTRFS_BLOCK_GROUP_RAID1 |
7101 BTRFS_BLOCK_GROUP_DUP)))
7102 continue;
7103 /*
7104 * avoid allocating from un-mirrored block group if there are
7105 * mirrored block groups.
7106 */
7107 list_for_each_entry(cache, &space_info->block_groups[3], list)
199c36ea 7108 set_block_group_ro(cache, 1);
b742bb82 7109 list_for_each_entry(cache, &space_info->block_groups[4], list)
199c36ea 7110 set_block_group_ro(cache, 1);
9078a3e1 7111 }
f0486c68
YZ
7112
7113 init_global_block_rsv(info);
0b86a832
CM
7114 ret = 0;
7115error:
9078a3e1 7116 btrfs_free_path(path);
0b86a832 7117 return ret;
9078a3e1 7118}
6324fbf3
CM
7119
7120int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7121 struct btrfs_root *root, u64 bytes_used,
e17cade2 7122 u64 type, u64 chunk_objectid, u64 chunk_offset,
6324fbf3
CM
7123 u64 size)
7124{
7125 int ret;
6324fbf3
CM
7126 struct btrfs_root *extent_root;
7127 struct btrfs_block_group_cache *cache;
6324fbf3
CM
7128
7129 extent_root = root->fs_info->extent_root;
6324fbf3 7130
12fcfd22 7131 root->fs_info->last_trans_log_full_commit = trans->transid;
e02119d5 7132
8f18cf13 7133 cache = kzalloc(sizeof(*cache), GFP_NOFS);
0f9dd46c
JB
7134 if (!cache)
7135 return -ENOMEM;
34d52cb6
LZ
7136 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7137 GFP_NOFS);
7138 if (!cache->free_space_ctl) {
7139 kfree(cache);
7140 return -ENOMEM;
7141 }
0f9dd46c 7142
e17cade2 7143 cache->key.objectid = chunk_offset;
6324fbf3 7144 cache->key.offset = size;
d2fb3437 7145 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
96303081 7146 cache->sectorsize = root->sectorsize;
0af3d00b 7147 cache->fs_info = root->fs_info;
96303081 7148
d2fb3437 7149 atomic_set(&cache->count, 1);
c286ac48 7150 spin_lock_init(&cache->lock);
0f9dd46c 7151 INIT_LIST_HEAD(&cache->list);
fa9c0d79 7152 INIT_LIST_HEAD(&cache->cluster_list);
0ef3e66b 7153
34d52cb6
LZ
7154 btrfs_init_free_space_ctl(cache);
7155
6324fbf3 7156 btrfs_set_block_group_used(&cache->item, bytes_used);
6324fbf3
CM
7157 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7158 cache->flags = type;
7159 btrfs_set_block_group_flags(&cache->item, type);
7160
11833d66 7161 cache->last_byte_to_unpin = (u64)-1;
817d52f8 7162 cache->cached = BTRFS_CACHE_FINISHED;
11833d66 7163 exclude_super_stripes(root, cache);
96303081 7164
817d52f8
JB
7165 add_new_free_space(cache, root->fs_info, chunk_offset,
7166 chunk_offset + size);
7167
11833d66
YZ
7168 free_excluded_extents(root, cache);
7169
6324fbf3
CM
7170 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7171 &cache->space_info);
7172 BUG_ON(ret);
1b2da372
JB
7173
7174 spin_lock(&cache->space_info->lock);
f0486c68 7175 cache->space_info->bytes_readonly += cache->bytes_super;
1b2da372
JB
7176 spin_unlock(&cache->space_info->lock);
7177
b742bb82 7178 __link_block_group(cache->space_info, cache);
6324fbf3 7179
0f9dd46c
JB
7180 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7181 BUG_ON(ret);
c286ac48 7182
6324fbf3
CM
7183 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7184 sizeof(cache->item));
7185 BUG_ON(ret);
7186
d18a2c44 7187 set_avail_alloc_bits(extent_root->fs_info, type);
925baedd 7188
6324fbf3
CM
7189 return 0;
7190}
1a40e23b
ZY
7191
7192int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7193 struct btrfs_root *root, u64 group_start)
7194{
7195 struct btrfs_path *path;
7196 struct btrfs_block_group_cache *block_group;
44fb5511 7197 struct btrfs_free_cluster *cluster;
0af3d00b 7198 struct btrfs_root *tree_root = root->fs_info->tree_root;
1a40e23b 7199 struct btrfs_key key;
0af3d00b 7200 struct inode *inode;
1a40e23b 7201 int ret;
89a55897 7202 int factor;
1a40e23b 7203
1a40e23b
ZY
7204 root = root->fs_info->extent_root;
7205
7206 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7207 BUG_ON(!block_group);
c146afad 7208 BUG_ON(!block_group->ro);
1a40e23b 7209
9f7c43c9 7210 /*
7211 * Free the reserved super bytes from this block group before
7212 * remove it.
7213 */
7214 free_excluded_extents(root, block_group);
7215
1a40e23b 7216 memcpy(&key, &block_group->key, sizeof(key));
89a55897
JB
7217 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7218 BTRFS_BLOCK_GROUP_RAID1 |
7219 BTRFS_BLOCK_GROUP_RAID10))
7220 factor = 2;
7221 else
7222 factor = 1;
1a40e23b 7223
44fb5511
CM
7224 /* make sure this block group isn't part of an allocation cluster */
7225 cluster = &root->fs_info->data_alloc_cluster;
7226 spin_lock(&cluster->refill_lock);
7227 btrfs_return_cluster_to_free_space(block_group, cluster);
7228 spin_unlock(&cluster->refill_lock);
7229
7230 /*
7231 * make sure this block group isn't part of a metadata
7232 * allocation cluster
7233 */
7234 cluster = &root->fs_info->meta_alloc_cluster;
7235 spin_lock(&cluster->refill_lock);
7236 btrfs_return_cluster_to_free_space(block_group, cluster);
7237 spin_unlock(&cluster->refill_lock);
7238
1a40e23b 7239 path = btrfs_alloc_path();
d8926bb3
MF
7240 if (!path) {
7241 ret = -ENOMEM;
7242 goto out;
7243 }
1a40e23b 7244
0af3d00b
JB
7245 inode = lookup_free_space_inode(root, block_group, path);
7246 if (!IS_ERR(inode)) {
b532402e
TI
7247 ret = btrfs_orphan_add(trans, inode);
7248 BUG_ON(ret);
0af3d00b
JB
7249 clear_nlink(inode);
7250 /* One for the block groups ref */
7251 spin_lock(&block_group->lock);
7252 if (block_group->iref) {
7253 block_group->iref = 0;
7254 block_group->inode = NULL;
7255 spin_unlock(&block_group->lock);
7256 iput(inode);
7257 } else {
7258 spin_unlock(&block_group->lock);
7259 }
7260 /* One for our lookup ref */
7261 iput(inode);
7262 }
7263
7264 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7265 key.offset = block_group->key.objectid;
7266 key.type = 0;
7267
7268 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7269 if (ret < 0)
7270 goto out;
7271 if (ret > 0)
b3b4aa74 7272 btrfs_release_path(path);
0af3d00b
JB
7273 if (ret == 0) {
7274 ret = btrfs_del_item(trans, tree_root, path);
7275 if (ret)
7276 goto out;
b3b4aa74 7277 btrfs_release_path(path);
0af3d00b
JB
7278 }
7279
3dfdb934 7280 spin_lock(&root->fs_info->block_group_cache_lock);
1a40e23b
ZY
7281 rb_erase(&block_group->cache_node,
7282 &root->fs_info->block_group_cache_tree);
3dfdb934 7283 spin_unlock(&root->fs_info->block_group_cache_lock);
817d52f8 7284
80eb234a 7285 down_write(&block_group->space_info->groups_sem);
44fb5511
CM
7286 /*
7287 * we must use list_del_init so people can check to see if they
7288 * are still on the list after taking the semaphore
7289 */
7290 list_del_init(&block_group->list);
80eb234a 7291 up_write(&block_group->space_info->groups_sem);
1a40e23b 7292
817d52f8 7293 if (block_group->cached == BTRFS_CACHE_STARTED)
11833d66 7294 wait_block_group_cache_done(block_group);
817d52f8
JB
7295
7296 btrfs_remove_free_space_cache(block_group);
7297
c146afad
YZ
7298 spin_lock(&block_group->space_info->lock);
7299 block_group->space_info->total_bytes -= block_group->key.offset;
7300 block_group->space_info->bytes_readonly -= block_group->key.offset;
89a55897 7301 block_group->space_info->disk_total -= block_group->key.offset * factor;
c146afad 7302 spin_unlock(&block_group->space_info->lock);
283bb197 7303
0af3d00b
JB
7304 memcpy(&key, &block_group->key, sizeof(key));
7305
283bb197 7306 btrfs_clear_space_info_full(root->fs_info);
c146afad 7307
fa9c0d79
CM
7308 btrfs_put_block_group(block_group);
7309 btrfs_put_block_group(block_group);
1a40e23b
ZY
7310
7311 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7312 if (ret > 0)
7313 ret = -EIO;
7314 if (ret < 0)
7315 goto out;
7316
7317 ret = btrfs_del_item(trans, root, path);
7318out:
7319 btrfs_free_path(path);
7320 return ret;
7321}
acce952b 7322
c59021f8 7323int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7324{
7325 struct btrfs_space_info *space_info;
1aba86d6 7326 struct btrfs_super_block *disk_super;
7327 u64 features;
7328 u64 flags;
7329 int mixed = 0;
c59021f8 7330 int ret;
7331
1aba86d6 7332 disk_super = &fs_info->super_copy;
7333 if (!btrfs_super_root(disk_super))
7334 return 1;
c59021f8 7335
1aba86d6 7336 features = btrfs_super_incompat_flags(disk_super);
7337 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7338 mixed = 1;
c59021f8 7339
1aba86d6 7340 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7341 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
c59021f8 7342 if (ret)
1aba86d6 7343 goto out;
c59021f8 7344
1aba86d6 7345 if (mixed) {
7346 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7347 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7348 } else {
7349 flags = BTRFS_BLOCK_GROUP_METADATA;
7350 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7351 if (ret)
7352 goto out;
7353
7354 flags = BTRFS_BLOCK_GROUP_DATA;
7355 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7356 }
7357out:
c59021f8 7358 return ret;
7359}
7360
acce952b 7361int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7362{
7363 return unpin_extent_range(root, start, end);
7364}
7365
7366int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
5378e607 7367 u64 num_bytes, u64 *actual_bytes)
acce952b 7368{
5378e607 7369 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
acce952b 7370}
f7039b1d
LD
7371
7372int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7373{
7374 struct btrfs_fs_info *fs_info = root->fs_info;
7375 struct btrfs_block_group_cache *cache = NULL;
7376 u64 group_trimmed;
7377 u64 start;
7378 u64 end;
7379 u64 trimmed = 0;
7380 int ret = 0;
7381
7382 cache = btrfs_lookup_block_group(fs_info, range->start);
7383
7384 while (cache) {
7385 if (cache->key.objectid >= (range->start + range->len)) {
7386 btrfs_put_block_group(cache);
7387 break;
7388 }
7389
7390 start = max(range->start, cache->key.objectid);
7391 end = min(range->start + range->len,
7392 cache->key.objectid + cache->key.offset);
7393
7394 if (end - start >= range->minlen) {
7395 if (!block_group_cache_done(cache)) {
7396 ret = cache_block_group(cache, NULL, root, 0);
7397 if (!ret)
7398 wait_block_group_cache_done(cache);
7399 }
7400 ret = btrfs_trim_block_group(cache,
7401 &group_trimmed,
7402 start,
7403 end,
7404 range->minlen);
7405
7406 trimmed += group_trimmed;
7407 if (ret) {
7408 btrfs_put_block_group(cache);
7409 break;
7410 }
7411 }
7412
7413 cache = next_block_group(fs_info->tree_root, cache);
7414 }
7415
7416 range->len = trimmed;
7417 return ret;
7418}
This page took 0.760185 seconds and 5 git commands to generate.