aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent-tree.c
diff options
context:
space:
mode:
authorJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
committerJonathan Herman <hermanjl@cs.unc.edu>2013-01-17 16:15:55 -0500
commit8dea78da5cee153b8af9c07a2745f6c55057fe12 (patch)
treea8f4d49d63b1ecc92f2fddceba0655b2472c5bd9 /fs/btrfs/extent-tree.c
parent406089d01562f1e2bf9f089fd7637009ebaad589 (diff)
Patched in Tegra support.
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r--fs/btrfs/extent-tree.c2703
1 files changed, 974 insertions, 1729 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 521e9d4424f..f5be06a2462 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -23,7 +23,6 @@
23#include <linux/rcupdate.h> 23#include <linux/rcupdate.h>
24#include <linux/kthread.h> 24#include <linux/kthread.h>
25#include <linux/slab.h> 25#include <linux/slab.h>
26#include <linux/ratelimit.h>
27#include "compat.h" 26#include "compat.h"
28#include "hash.h" 27#include "hash.h"
29#include "ctree.h" 28#include "ctree.h"
@@ -33,43 +32,24 @@
33#include "volumes.h" 32#include "volumes.h"
34#include "locking.h" 33#include "locking.h"
35#include "free-space-cache.h" 34#include "free-space-cache.h"
36#include "math.h"
37 35
38#undef SCRAMBLE_DELAYED_REFS 36/* control flags for do_chunk_alloc's force field
39
40/*
41 * control flags for do_chunk_alloc's force field
42 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk 37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
43 * if we really need one. 38 * if we really need one.
44 * 39 *
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
41 *
45 * CHUNK_ALLOC_LIMITED means to only try and allocate one 42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
46 * if we have very few chunks already allocated. This is 43 * if we have very few chunks already allocated. This is
47 * used as part of the clustering code to help make sure 44 * used as part of the clustering code to help make sure
48 * we have a good pool of storage to cluster in, without 45 * we have a good pool of storage to cluster in, without
49 * filling the FS with empty chunks 46 * filling the FS with empty chunks
50 * 47 *
51 * CHUNK_ALLOC_FORCE means it must try to allocate one
52 *
53 */ 48 */
54enum { 49enum {
55 CHUNK_ALLOC_NO_FORCE = 0, 50 CHUNK_ALLOC_NO_FORCE = 0,
56 CHUNK_ALLOC_LIMITED = 1, 51 CHUNK_ALLOC_FORCE = 1,
57 CHUNK_ALLOC_FORCE = 2, 52 CHUNK_ALLOC_LIMITED = 2,
58};
59
60/*
61 * Control how reservations are dealt with.
62 *
63 * RESERVE_FREE - freeing a reservation.
64 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
65 * ENOSPC accounting
66 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
67 * bytes_may_use as the ENOSPC accounting is done elsewhere
68 */
69enum {
70 RESERVE_FREE = 0,
71 RESERVE_ALLOC = 1,
72 RESERVE_ALLOC_NO_ACCOUNT = 2,
73}; 53};
74 54
75static int update_block_group(struct btrfs_trans_handle *trans, 55static int update_block_group(struct btrfs_trans_handle *trans,
@@ -95,14 +75,12 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
95 u64 flags, struct btrfs_disk_key *key, 75 u64 flags, struct btrfs_disk_key *key,
96 int level, struct btrfs_key *ins); 76 int level, struct btrfs_key *ins);
97static int do_chunk_alloc(struct btrfs_trans_handle *trans, 77static int do_chunk_alloc(struct btrfs_trans_handle *trans,
98 struct btrfs_root *extent_root, u64 flags, 78 struct btrfs_root *extent_root, u64 alloc_bytes,
99 int force); 79 u64 flags, int force);
100static int find_next_key(struct btrfs_path *path, int level, 80static int find_next_key(struct btrfs_path *path, int level,
101 struct btrfs_key *key); 81 struct btrfs_key *key);
102static void dump_space_info(struct btrfs_space_info *info, u64 bytes, 82static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
103 int dump_block_groups); 83 int dump_block_groups);
104static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
105 u64 num_bytes, int reserve);
106 84
107static noinline int 85static noinline int
108block_group_cache_done(struct btrfs_block_group_cache *cache) 86block_group_cache_done(struct btrfs_block_group_cache *cache)
@@ -126,6 +104,7 @@ void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
126 if (atomic_dec_and_test(&cache->count)) { 104 if (atomic_dec_and_test(&cache->count)) {
127 WARN_ON(cache->pinned > 0); 105 WARN_ON(cache->pinned > 0);
128 WARN_ON(cache->reserved > 0); 106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
129 kfree(cache->free_space_ctl); 108 kfree(cache->free_space_ctl);
130 kfree(cache); 109 kfree(cache);
131 } 110 }
@@ -248,7 +227,7 @@ static int exclude_super_stripes(struct btrfs_root *root,
248 cache->bytes_super += stripe_len; 227 cache->bytes_super += stripe_len;
249 ret = add_excluded_extent(root, cache->key.objectid, 228 ret = add_excluded_extent(root, cache->key.objectid,
250 stripe_len); 229 stripe_len);
251 BUG_ON(ret); /* -ENOMEM */ 230 BUG_ON(ret);
252 } 231 }
253 232
254 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { 233 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
@@ -256,13 +235,13 @@ static int exclude_super_stripes(struct btrfs_root *root,
256 ret = btrfs_rmap_block(&root->fs_info->mapping_tree, 235 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
257 cache->key.objectid, bytenr, 236 cache->key.objectid, bytenr,
258 0, &logical, &nr, &stripe_len); 237 0, &logical, &nr, &stripe_len);
259 BUG_ON(ret); /* -ENOMEM */ 238 BUG_ON(ret);
260 239
261 while (nr--) { 240 while (nr--) {
262 cache->bytes_super += stripe_len; 241 cache->bytes_super += stripe_len;
263 ret = add_excluded_extent(root, logical[nr], 242 ret = add_excluded_extent(root, logical[nr],
264 stripe_len); 243 stripe_len);
265 BUG_ON(ret); /* -ENOMEM */ 244 BUG_ON(ret);
266 } 245 }
267 246
268 kfree(logical); 247 kfree(logical);
@@ -313,8 +292,7 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
313 while (start < end) { 292 while (start < end) {
314 ret = find_first_extent_bit(info->pinned_extents, start, 293 ret = find_first_extent_bit(info->pinned_extents, start,
315 &extent_start, &extent_end, 294 &extent_start, &extent_end,
316 EXTENT_DIRTY | EXTENT_UPTODATE, 295 EXTENT_DIRTY | EXTENT_UPTODATE);
317 NULL);
318 if (ret) 296 if (ret)
319 break; 297 break;
320 298
@@ -325,7 +303,7 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
325 total_added += size; 303 total_added += size;
326 ret = btrfs_add_free_space(block_group, start, 304 ret = btrfs_add_free_space(block_group, start,
327 size); 305 size);
328 BUG_ON(ret); /* -ENOMEM or logic error */ 306 BUG_ON(ret);
329 start = extent_end + 1; 307 start = extent_end + 1;
330 } else { 308 } else {
331 break; 309 break;
@@ -336,7 +314,7 @@ static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
336 size = end - start; 314 size = end - start;
337 total_added += size; 315 total_added += size;
338 ret = btrfs_add_free_space(block_group, start, size); 316 ret = btrfs_add_free_space(block_group, start, size);
339 BUG_ON(ret); /* -ENOMEM or logic error */ 317 BUG_ON(ret);
340 } 318 }
341 319
342 return total_added; 320 return total_added;
@@ -472,60 +450,13 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
472 struct btrfs_root *root, 450 struct btrfs_root *root,
473 int load_cache_only) 451 int load_cache_only)
474{ 452{
475 DEFINE_WAIT(wait);
476 struct btrfs_fs_info *fs_info = cache->fs_info; 453 struct btrfs_fs_info *fs_info = cache->fs_info;
477 struct btrfs_caching_control *caching_ctl; 454 struct btrfs_caching_control *caching_ctl;
478 int ret = 0; 455 int ret = 0;
479 456
480 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); 457 smp_mb();
481 if (!caching_ctl) 458 if (cache->cached != BTRFS_CACHE_NO)
482 return -ENOMEM;
483
484 INIT_LIST_HEAD(&caching_ctl->list);
485 mutex_init(&caching_ctl->mutex);
486 init_waitqueue_head(&caching_ctl->wait);
487 caching_ctl->block_group = cache;
488 caching_ctl->progress = cache->key.objectid;
489 atomic_set(&caching_ctl->count, 1);
490 caching_ctl->work.func = caching_thread;
491
492 spin_lock(&cache->lock);
493 /*
494 * This should be a rare occasion, but this could happen I think in the
495 * case where one thread starts to load the space cache info, and then
496 * some other thread starts a transaction commit which tries to do an
497 * allocation while the other thread is still loading the space cache
498 * info. The previous loop should have kept us from choosing this block
499 * group, but if we've moved to the state where we will wait on caching
500 * block groups we need to first check if we're doing a fast load here,
501 * so we can wait for it to finish, otherwise we could end up allocating
502 * from a block group who's cache gets evicted for one reason or
503 * another.
504 */
505 while (cache->cached == BTRFS_CACHE_FAST) {
506 struct btrfs_caching_control *ctl;
507
508 ctl = cache->caching_ctl;
509 atomic_inc(&ctl->count);
510 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
511 spin_unlock(&cache->lock);
512
513 schedule();
514
515 finish_wait(&ctl->wait, &wait);
516 put_caching_control(ctl);
517 spin_lock(&cache->lock);
518 }
519
520 if (cache->cached != BTRFS_CACHE_NO) {
521 spin_unlock(&cache->lock);
522 kfree(caching_ctl);
523 return 0; 459 return 0;
524 }
525 WARN_ON(cache->caching_ctl);
526 cache->caching_ctl = caching_ctl;
527 cache->cached = BTRFS_CACHE_FAST;
528 spin_unlock(&cache->lock);
529 460
530 /* 461 /*
531 * We can't do the read from on-disk cache during a commit since we need 462 * We can't do the read from on-disk cache during a commit since we need
@@ -533,52 +464,58 @@ static int cache_block_group(struct btrfs_block_group_cache *cache,
533 * allocate blocks for the tree root we can't do the fast caching since 464 * allocate blocks for the tree root we can't do the fast caching since
534 * we likely hold important locks. 465 * we likely hold important locks.
535 */ 466 */
536 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { 467 if (trans && (!trans->transaction->in_commit) &&
468 (root && root != root->fs_info->tree_root)) {
469 spin_lock(&cache->lock);
470 if (cache->cached != BTRFS_CACHE_NO) {
471 spin_unlock(&cache->lock);
472 return 0;
473 }
474 cache->cached = BTRFS_CACHE_STARTED;
475 spin_unlock(&cache->lock);
476
537 ret = load_free_space_cache(fs_info, cache); 477 ret = load_free_space_cache(fs_info, cache);
538 478
539 spin_lock(&cache->lock); 479 spin_lock(&cache->lock);
540 if (ret == 1) { 480 if (ret == 1) {
541 cache->caching_ctl = NULL;
542 cache->cached = BTRFS_CACHE_FINISHED; 481 cache->cached = BTRFS_CACHE_FINISHED;
543 cache->last_byte_to_unpin = (u64)-1; 482 cache->last_byte_to_unpin = (u64)-1;
544 } else { 483 } else {
545 if (load_cache_only) { 484 cache->cached = BTRFS_CACHE_NO;
546 cache->caching_ctl = NULL;
547 cache->cached = BTRFS_CACHE_NO;
548 } else {
549 cache->cached = BTRFS_CACHE_STARTED;
550 }
551 } 485 }
552 spin_unlock(&cache->lock); 486 spin_unlock(&cache->lock);
553 wake_up(&caching_ctl->wait);
554 if (ret == 1) { 487 if (ret == 1) {
555 put_caching_control(caching_ctl);
556 free_excluded_extents(fs_info->extent_root, cache); 488 free_excluded_extents(fs_info->extent_root, cache);
557 return 0; 489 return 0;
558 } 490 }
559 } else {
560 /*
561 * We are not going to do the fast caching, set cached to the
562 * appropriate value and wakeup any waiters.
563 */
564 spin_lock(&cache->lock);
565 if (load_cache_only) {
566 cache->caching_ctl = NULL;
567 cache->cached = BTRFS_CACHE_NO;
568 } else {
569 cache->cached = BTRFS_CACHE_STARTED;
570 }
571 spin_unlock(&cache->lock);
572 wake_up(&caching_ctl->wait);
573 } 491 }
574 492
575 if (load_cache_only) { 493 if (load_cache_only)
576 put_caching_control(caching_ctl); 494 return 0;
495
496 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
497 BUG_ON(!caching_ctl);
498
499 INIT_LIST_HEAD(&caching_ctl->list);
500 mutex_init(&caching_ctl->mutex);
501 init_waitqueue_head(&caching_ctl->wait);
502 caching_ctl->block_group = cache;
503 caching_ctl->progress = cache->key.objectid;
504 /* one for caching kthread, one for caching block group list */
505 atomic_set(&caching_ctl->count, 2);
506 caching_ctl->work.func = caching_thread;
507
508 spin_lock(&cache->lock);
509 if (cache->cached != BTRFS_CACHE_NO) {
510 spin_unlock(&cache->lock);
511 kfree(caching_ctl);
577 return 0; 512 return 0;
578 } 513 }
514 cache->caching_ctl = caching_ctl;
515 cache->cached = BTRFS_CACHE_STARTED;
516 spin_unlock(&cache->lock);
579 517
580 down_write(&fs_info->extent_commit_sem); 518 down_write(&fs_info->extent_commit_sem);
581 atomic_inc(&caching_ctl->count);
582 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); 519 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
583 up_write(&fs_info->extent_commit_sem); 520 up_write(&fs_info->extent_commit_sem);
584 521
@@ -622,7 +559,8 @@ static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
622 struct list_head *head = &info->space_info; 559 struct list_head *head = &info->space_info;
623 struct btrfs_space_info *found; 560 struct btrfs_space_info *found;
624 561
625 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; 562 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
563 BTRFS_BLOCK_GROUP_METADATA;
626 564
627 rcu_read_lock(); 565 rcu_read_lock();
628 list_for_each_entry_rcu(found, head, list) { 566 list_for_each_entry_rcu(found, head, list) {
@@ -650,6 +588,24 @@ void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
650 rcu_read_unlock(); 588 rcu_read_unlock();
651} 589}
652 590
591static u64 div_factor(u64 num, int factor)
592{
593 if (factor == 10)
594 return num;
595 num *= factor;
596 do_div(num, 10);
597 return num;
598}
599
600static u64 div_factor_fine(u64 num, int factor)
601{
602 if (factor == 100)
603 return num;
604 num *= factor;
605 do_div(num, 100);
606 return num;
607}
608
653u64 btrfs_find_block_group(struct btrfs_root *root, 609u64 btrfs_find_block_group(struct btrfs_root *root,
654 u64 search_start, u64 search_hint, int owner) 610 u64 search_start, u64 search_hint, int owner)
655{ 611{
@@ -967,7 +923,7 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
967 ret = btrfs_next_leaf(root, path); 923 ret = btrfs_next_leaf(root, path);
968 if (ret < 0) 924 if (ret < 0)
969 return ret; 925 return ret;
970 BUG_ON(ret > 0); /* Corruption */ 926 BUG_ON(ret > 0);
971 leaf = path->nodes[0]; 927 leaf = path->nodes[0];
972 } 928 }
973 btrfs_item_key_to_cpu(leaf, &found_key, 929 btrfs_item_key_to_cpu(leaf, &found_key,
@@ -993,9 +949,9 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
993 new_size + extra_size, 1); 949 new_size + extra_size, 1);
994 if (ret < 0) 950 if (ret < 0)
995 return ret; 951 return ret;
996 BUG_ON(ret); /* Corruption */ 952 BUG_ON(ret);
997 953
998 btrfs_extend_item(trans, root, path, new_size); 954 ret = btrfs_extend_item(trans, root, path, new_size);
999 955
1000 leaf = path->nodes[0]; 956 leaf = path->nodes[0];
1001 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 957 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
@@ -1463,11 +1419,7 @@ int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1463 err = ret; 1419 err = ret;
1464 goto out; 1420 goto out;
1465 } 1421 }
1466 if (ret && !insert) { 1422 BUG_ON(ret);
1467 err = -ENOENT;
1468 goto out;
1469 }
1470 BUG_ON(ret); /* Corruption */
1471 1423
1472 leaf = path->nodes[0]; 1424 leaf = path->nodes[0];
1473 item_size = btrfs_item_size_nr(leaf, path->slots[0]); 1425 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
@@ -1581,13 +1533,13 @@ out:
1581 * helper to add new inline back ref 1533 * helper to add new inline back ref
1582 */ 1534 */
1583static noinline_for_stack 1535static noinline_for_stack
1584void setup_inline_extent_backref(struct btrfs_trans_handle *trans, 1536int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1585 struct btrfs_root *root, 1537 struct btrfs_root *root,
1586 struct btrfs_path *path, 1538 struct btrfs_path *path,
1587 struct btrfs_extent_inline_ref *iref, 1539 struct btrfs_extent_inline_ref *iref,
1588 u64 parent, u64 root_objectid, 1540 u64 parent, u64 root_objectid,
1589 u64 owner, u64 offset, int refs_to_add, 1541 u64 owner, u64 offset, int refs_to_add,
1590 struct btrfs_delayed_extent_op *extent_op) 1542 struct btrfs_delayed_extent_op *extent_op)
1591{ 1543{
1592 struct extent_buffer *leaf; 1544 struct extent_buffer *leaf;
1593 struct btrfs_extent_item *ei; 1545 struct btrfs_extent_item *ei;
@@ -1597,6 +1549,7 @@ void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1597 u64 refs; 1549 u64 refs;
1598 int size; 1550 int size;
1599 int type; 1551 int type;
1552 int ret;
1600 1553
1601 leaf = path->nodes[0]; 1554 leaf = path->nodes[0];
1602 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1555 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
@@ -1605,7 +1558,7 @@ void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1605 type = extent_ref_type(parent, owner); 1558 type = extent_ref_type(parent, owner);
1606 size = btrfs_extent_inline_ref_size(type); 1559 size = btrfs_extent_inline_ref_size(type);
1607 1560
1608 btrfs_extend_item(trans, root, path, size); 1561 ret = btrfs_extend_item(trans, root, path, size);
1609 1562
1610 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); 1563 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1611 refs = btrfs_extent_refs(leaf, ei); 1564 refs = btrfs_extent_refs(leaf, ei);
@@ -1640,6 +1593,7 @@ void setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1640 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); 1593 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1641 } 1594 }
1642 btrfs_mark_buffer_dirty(leaf); 1595 btrfs_mark_buffer_dirty(leaf);
1596 return 0;
1643} 1597}
1644 1598
1645static int lookup_extent_backref(struct btrfs_trans_handle *trans, 1599static int lookup_extent_backref(struct btrfs_trans_handle *trans,
@@ -1674,12 +1628,12 @@ static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1674 * helper to update/remove inline back ref 1628 * helper to update/remove inline back ref
1675 */ 1629 */
1676static noinline_for_stack 1630static noinline_for_stack
1677void update_inline_extent_backref(struct btrfs_trans_handle *trans, 1631int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1678 struct btrfs_root *root, 1632 struct btrfs_root *root,
1679 struct btrfs_path *path, 1633 struct btrfs_path *path,
1680 struct btrfs_extent_inline_ref *iref, 1634 struct btrfs_extent_inline_ref *iref,
1681 int refs_to_mod, 1635 int refs_to_mod,
1682 struct btrfs_delayed_extent_op *extent_op) 1636 struct btrfs_delayed_extent_op *extent_op)
1683{ 1637{
1684 struct extent_buffer *leaf; 1638 struct extent_buffer *leaf;
1685 struct btrfs_extent_item *ei; 1639 struct btrfs_extent_item *ei;
@@ -1690,6 +1644,7 @@ void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1690 u32 item_size; 1644 u32 item_size;
1691 int size; 1645 int size;
1692 int type; 1646 int type;
1647 int ret;
1693 u64 refs; 1648 u64 refs;
1694 1649
1695 leaf = path->nodes[0]; 1650 leaf = path->nodes[0];
@@ -1731,9 +1686,10 @@ void update_inline_extent_backref(struct btrfs_trans_handle *trans,
1731 memmove_extent_buffer(leaf, ptr, ptr + size, 1686 memmove_extent_buffer(leaf, ptr, ptr + size,
1732 end - ptr - size); 1687 end - ptr - size);
1733 item_size -= size; 1688 item_size -= size;
1734 btrfs_truncate_item(trans, root, path, item_size, 1); 1689 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1735 } 1690 }
1736 btrfs_mark_buffer_dirty(leaf); 1691 btrfs_mark_buffer_dirty(leaf);
1692 return 0;
1737} 1693}
1738 1694
1739static noinline_for_stack 1695static noinline_for_stack
@@ -1753,13 +1709,13 @@ int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1753 root_objectid, owner, offset, 1); 1709 root_objectid, owner, offset, 1);
1754 if (ret == 0) { 1710 if (ret == 0) {
1755 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); 1711 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1756 update_inline_extent_backref(trans, root, path, iref, 1712 ret = update_inline_extent_backref(trans, root, path, iref,
1757 refs_to_add, extent_op); 1713 refs_to_add, extent_op);
1758 } else if (ret == -ENOENT) { 1714 } else if (ret == -ENOENT) {
1759 setup_inline_extent_backref(trans, root, path, iref, parent, 1715 ret = setup_inline_extent_backref(trans, root, path, iref,
1760 root_objectid, owner, offset, 1716 parent, root_objectid,
1761 refs_to_add, extent_op); 1717 owner, offset, refs_to_add,
1762 ret = 0; 1718 extent_op);
1763 } 1719 }
1764 return ret; 1720 return ret;
1765} 1721}
@@ -1789,12 +1745,12 @@ static int remove_extent_backref(struct btrfs_trans_handle *trans,
1789 struct btrfs_extent_inline_ref *iref, 1745 struct btrfs_extent_inline_ref *iref,
1790 int refs_to_drop, int is_data) 1746 int refs_to_drop, int is_data)
1791{ 1747{
1792 int ret = 0; 1748 int ret;
1793 1749
1794 BUG_ON(!is_data && refs_to_drop != 1); 1750 BUG_ON(!is_data && refs_to_drop != 1);
1795 if (iref) { 1751 if (iref) {
1796 update_inline_extent_backref(trans, root, path, iref, 1752 ret = update_inline_extent_backref(trans, root, path, iref,
1797 -refs_to_drop, NULL); 1753 -refs_to_drop, NULL);
1798 } else if (is_data) { 1754 } else if (is_data) {
1799 ret = remove_extent_data_ref(trans, root, path, refs_to_drop); 1755 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1800 } else { 1756 } else {
@@ -1814,19 +1770,18 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1814{ 1770{
1815 int ret; 1771 int ret;
1816 u64 discarded_bytes = 0; 1772 u64 discarded_bytes = 0;
1817 struct btrfs_bio *bbio = NULL; 1773 struct btrfs_multi_bio *multi = NULL;
1818 1774
1819 1775
1820 /* Tell the block device(s) that the sectors can be discarded */ 1776 /* Tell the block device(s) that the sectors can be discarded */
1821 ret = btrfs_map_block(root->fs_info, REQ_DISCARD, 1777 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1822 bytenr, &num_bytes, &bbio, 0); 1778 bytenr, &num_bytes, &multi, 0);
1823 /* Error condition is -ENOMEM */
1824 if (!ret) { 1779 if (!ret) {
1825 struct btrfs_bio_stripe *stripe = bbio->stripes; 1780 struct btrfs_bio_stripe *stripe = multi->stripes;
1826 int i; 1781 int i;
1827 1782
1828 1783
1829 for (i = 0; i < bbio->num_stripes; i++, stripe++) { 1784 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1830 if (!stripe->dev->can_discard) 1785 if (!stripe->dev->can_discard)
1831 continue; 1786 continue;
1832 1787
@@ -1836,7 +1791,7 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1836 if (!ret) 1791 if (!ret)
1837 discarded_bytes += stripe->length; 1792 discarded_bytes += stripe->length;
1838 else if (ret != -EOPNOTSUPP) 1793 else if (ret != -EOPNOTSUPP)
1839 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */ 1794 break;
1840 1795
1841 /* 1796 /*
1842 * Just in case we get back EOPNOTSUPP for some reason, 1797 * Just in case we get back EOPNOTSUPP for some reason,
@@ -1845,7 +1800,7 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1845 */ 1800 */
1846 ret = 0; 1801 ret = 0;
1847 } 1802 }
1848 kfree(bbio); 1803 kfree(multi);
1849 } 1804 }
1850 1805
1851 if (actual_bytes) 1806 if (actual_bytes)
@@ -1855,28 +1810,23 @@ static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1855 return ret; 1810 return ret;
1856} 1811}
1857 1812
1858/* Can return -ENOMEM */
1859int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, 1813int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1860 struct btrfs_root *root, 1814 struct btrfs_root *root,
1861 u64 bytenr, u64 num_bytes, u64 parent, 1815 u64 bytenr, u64 num_bytes, u64 parent,
1862 u64 root_objectid, u64 owner, u64 offset, int for_cow) 1816 u64 root_objectid, u64 owner, u64 offset)
1863{ 1817{
1864 int ret; 1818 int ret;
1865 struct btrfs_fs_info *fs_info = root->fs_info;
1866
1867 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && 1819 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1868 root_objectid == BTRFS_TREE_LOG_OBJECTID); 1820 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1869 1821
1870 if (owner < BTRFS_FIRST_FREE_OBJECTID) { 1822 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1871 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, 1823 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1872 num_bytes,
1873 parent, root_objectid, (int)owner, 1824 parent, root_objectid, (int)owner,
1874 BTRFS_ADD_DELAYED_REF, NULL, for_cow); 1825 BTRFS_ADD_DELAYED_REF, NULL);
1875 } else { 1826 } else {
1876 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, 1827 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1877 num_bytes,
1878 parent, root_objectid, owner, offset, 1828 parent, root_objectid, owner, offset,
1879 BTRFS_ADD_DELAYED_REF, NULL, for_cow); 1829 BTRFS_ADD_DELAYED_REF, NULL);
1880 } 1830 }
1881 return ret; 1831 return ret;
1882} 1832}
@@ -1931,8 +1881,7 @@ static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1931 ret = insert_extent_backref(trans, root->fs_info->extent_root, 1881 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1932 path, bytenr, parent, root_objectid, 1882 path, bytenr, parent, root_objectid,
1933 owner, offset, refs_to_add); 1883 owner, offset, refs_to_add);
1934 if (ret) 1884 BUG_ON(ret);
1935 btrfs_abort_transaction(trans, root, ret);
1936out: 1885out:
1937 btrfs_free_path(path); 1886 btrfs_free_path(path);
1938 return err; 1887 return err;
@@ -2019,9 +1968,6 @@ static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2019 int ret; 1968 int ret;
2020 int err = 0; 1969 int err = 0;
2021 1970
2022 if (trans->aborted)
2023 return 0;
2024
2025 path = btrfs_alloc_path(); 1971 path = btrfs_alloc_path();
2026 if (!path) 1972 if (!path)
2027 return -ENOMEM; 1973 return -ENOMEM;
@@ -2119,11 +2065,7 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2119 struct btrfs_delayed_extent_op *extent_op, 2065 struct btrfs_delayed_extent_op *extent_op,
2120 int insert_reserved) 2066 int insert_reserved)
2121{ 2067{
2122 int ret = 0; 2068 int ret;
2123
2124 if (trans->aborted)
2125 return 0;
2126
2127 if (btrfs_delayed_ref_is_head(node)) { 2069 if (btrfs_delayed_ref_is_head(node)) {
2128 struct btrfs_delayed_ref_head *head; 2070 struct btrfs_delayed_ref_head *head;
2129 /* 2071 /*
@@ -2141,10 +2083,11 @@ static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2141 ret = btrfs_del_csums(trans, root, 2083 ret = btrfs_del_csums(trans, root,
2142 node->bytenr, 2084 node->bytenr,
2143 node->num_bytes); 2085 node->num_bytes);
2086 BUG_ON(ret);
2144 } 2087 }
2145 } 2088 }
2146 mutex_unlock(&head->mutex); 2089 mutex_unlock(&head->mutex);
2147 return ret; 2090 return 0;
2148 } 2091 }
2149 2092
2150 if (node->type == BTRFS_TREE_BLOCK_REF_KEY || 2093 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
@@ -2191,10 +2134,6 @@ again:
2191 return NULL; 2134 return NULL;
2192} 2135}
2193 2136
2194/*
2195 * Returns 0 on success or if called with an already aborted transaction.
2196 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2197 */
2198static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, 2137static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2199 struct btrfs_root *root, 2138 struct btrfs_root *root,
2200 struct list_head *cluster) 2139 struct list_head *cluster)
@@ -2203,7 +2142,6 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2203 struct btrfs_delayed_ref_node *ref; 2142 struct btrfs_delayed_ref_node *ref;
2204 struct btrfs_delayed_ref_head *locked_ref = NULL; 2143 struct btrfs_delayed_ref_head *locked_ref = NULL;
2205 struct btrfs_delayed_extent_op *extent_op; 2144 struct btrfs_delayed_extent_op *extent_op;
2206 struct btrfs_fs_info *fs_info = root->fs_info;
2207 int ret; 2145 int ret;
2208 int count = 0; 2146 int count = 0;
2209 int must_insert_reserved = 0; 2147 int must_insert_reserved = 0;
@@ -2236,38 +2174,6 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2236 } 2174 }
2237 2175
2238 /* 2176 /*
2239 * We need to try and merge add/drops of the same ref since we
2240 * can run into issues with relocate dropping the implicit ref
2241 * and then it being added back again before the drop can
2242 * finish. If we merged anything we need to re-loop so we can
2243 * get a good ref.
2244 */
2245 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2246 locked_ref);
2247
2248 /*
2249 * locked_ref is the head node, so we have to go one
2250 * node back for any delayed ref updates
2251 */
2252 ref = select_delayed_ref(locked_ref);
2253
2254 if (ref && ref->seq &&
2255 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
2256 /*
2257 * there are still refs with lower seq numbers in the
2258 * process of being added. Don't run this ref yet.
2259 */
2260 list_del_init(&locked_ref->cluster);
2261 mutex_unlock(&locked_ref->mutex);
2262 locked_ref = NULL;
2263 delayed_refs->num_heads_ready++;
2264 spin_unlock(&delayed_refs->lock);
2265 cond_resched();
2266 spin_lock(&delayed_refs->lock);
2267 continue;
2268 }
2269
2270 /*
2271 * record the must insert reserved flag before we 2177 * record the must insert reserved flag before we
2272 * drop the spin lock. 2178 * drop the spin lock.
2273 */ 2179 */
@@ -2277,6 +2183,11 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2277 extent_op = locked_ref->extent_op; 2183 extent_op = locked_ref->extent_op;
2278 locked_ref->extent_op = NULL; 2184 locked_ref->extent_op = NULL;
2279 2185
2186 /*
2187 * locked_ref is the head node, so we have to go one
2188 * node back for any delayed ref updates
2189 */
2190 ref = select_delayed_ref(locked_ref);
2280 if (!ref) { 2191 if (!ref) {
2281 /* All delayed refs have been processed, Go ahead 2192 /* All delayed refs have been processed, Go ahead
2282 * and send the head node to run_one_delayed_ref, 2193 * and send the head node to run_one_delayed_ref,
@@ -2294,18 +2205,12 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2294 2205
2295 ret = run_delayed_extent_op(trans, root, 2206 ret = run_delayed_extent_op(trans, root,
2296 ref, extent_op); 2207 ref, extent_op);
2208 BUG_ON(ret);
2297 kfree(extent_op); 2209 kfree(extent_op);
2298 2210
2299 if (ret) { 2211 cond_resched();
2300 list_del_init(&locked_ref->cluster); 2212 spin_lock(&delayed_refs->lock);
2301 mutex_unlock(&locked_ref->mutex); 2213 continue;
2302
2303 printk(KERN_DEBUG "btrfs: run_delayed_extent_op returned %d\n", ret);
2304 spin_lock(&delayed_refs->lock);
2305 return ret;
2306 }
2307
2308 goto next;
2309 } 2214 }
2310 2215
2311 list_del_init(&locked_ref->cluster); 2216 list_del_init(&locked_ref->cluster);
@@ -2315,135 +2220,29 @@ static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2315 ref->in_tree = 0; 2220 ref->in_tree = 0;
2316 rb_erase(&ref->rb_node, &delayed_refs->root); 2221 rb_erase(&ref->rb_node, &delayed_refs->root);
2317 delayed_refs->num_entries--; 2222 delayed_refs->num_entries--;
2318 if (locked_ref) { 2223
2319 /*
2320 * when we play the delayed ref, also correct the
2321 * ref_mod on head
2322 */
2323 switch (ref->action) {
2324 case BTRFS_ADD_DELAYED_REF:
2325 case BTRFS_ADD_DELAYED_EXTENT:
2326 locked_ref->node.ref_mod -= ref->ref_mod;
2327 break;
2328 case BTRFS_DROP_DELAYED_REF:
2329 locked_ref->node.ref_mod += ref->ref_mod;
2330 break;
2331 default:
2332 WARN_ON(1);
2333 }
2334 }
2335 spin_unlock(&delayed_refs->lock); 2224 spin_unlock(&delayed_refs->lock);
2336 2225
2337 ret = run_one_delayed_ref(trans, root, ref, extent_op, 2226 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2338 must_insert_reserved); 2227 must_insert_reserved);
2228 BUG_ON(ret);
2339 2229
2340 btrfs_put_delayed_ref(ref); 2230 btrfs_put_delayed_ref(ref);
2341 kfree(extent_op); 2231 kfree(extent_op);
2342 count++; 2232 count++;
2343 2233
2344 if (ret) {
2345 if (locked_ref) {
2346 list_del_init(&locked_ref->cluster);
2347 mutex_unlock(&locked_ref->mutex);
2348 }
2349 printk(KERN_DEBUG "btrfs: run_one_delayed_ref returned %d\n", ret);
2350 spin_lock(&delayed_refs->lock);
2351 return ret;
2352 }
2353
2354next:
2355 cond_resched(); 2234 cond_resched();
2356 spin_lock(&delayed_refs->lock); 2235 spin_lock(&delayed_refs->lock);
2357 } 2236 }
2358 return count; 2237 return count;
2359} 2238}
2360 2239
2361#ifdef SCRAMBLE_DELAYED_REFS
2362/*
2363 * Normally delayed refs get processed in ascending bytenr order. This
2364 * correlates in most cases to the order added. To expose dependencies on this
2365 * order, we start to process the tree in the middle instead of the beginning
2366 */
2367static u64 find_middle(struct rb_root *root)
2368{
2369 struct rb_node *n = root->rb_node;
2370 struct btrfs_delayed_ref_node *entry;
2371 int alt = 1;
2372 u64 middle;
2373 u64 first = 0, last = 0;
2374
2375 n = rb_first(root);
2376 if (n) {
2377 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2378 first = entry->bytenr;
2379 }
2380 n = rb_last(root);
2381 if (n) {
2382 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2383 last = entry->bytenr;
2384 }
2385 n = root->rb_node;
2386
2387 while (n) {
2388 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2389 WARN_ON(!entry->in_tree);
2390
2391 middle = entry->bytenr;
2392
2393 if (alt)
2394 n = n->rb_left;
2395 else
2396 n = n->rb_right;
2397
2398 alt = 1 - alt;
2399 }
2400 return middle;
2401}
2402#endif
2403
2404int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
2405 struct btrfs_fs_info *fs_info)
2406{
2407 struct qgroup_update *qgroup_update;
2408 int ret = 0;
2409
2410 if (list_empty(&trans->qgroup_ref_list) !=
2411 !trans->delayed_ref_elem.seq) {
2412 /* list without seq or seq without list */
2413 printk(KERN_ERR "btrfs: qgroup accounting update error, list is%s empty, seq is %llu\n",
2414 list_empty(&trans->qgroup_ref_list) ? "" : " not",
2415 trans->delayed_ref_elem.seq);
2416 BUG();
2417 }
2418
2419 if (!trans->delayed_ref_elem.seq)
2420 return 0;
2421
2422 while (!list_empty(&trans->qgroup_ref_list)) {
2423 qgroup_update = list_first_entry(&trans->qgroup_ref_list,
2424 struct qgroup_update, list);
2425 list_del(&qgroup_update->list);
2426 if (!ret)
2427 ret = btrfs_qgroup_account_ref(
2428 trans, fs_info, qgroup_update->node,
2429 qgroup_update->extent_op);
2430 kfree(qgroup_update);
2431 }
2432
2433 btrfs_put_tree_mod_seq(fs_info, &trans->delayed_ref_elem);
2434
2435 return ret;
2436}
2437
2438/* 2240/*
2439 * this starts processing the delayed reference count updates and 2241 * this starts processing the delayed reference count updates and
2440 * extent insertions we have queued up so far. count can be 2242 * extent insertions we have queued up so far. count can be
2441 * 0, which means to process everything in the tree at the start 2243 * 0, which means to process everything in the tree at the start
2442 * of the run (but not newly added entries), or it can be some target 2244 * of the run (but not newly added entries), or it can be some target
2443 * number you'd like to process. 2245 * number you'd like to process.
2444 *
2445 * Returns 0 on success or if called with an aborted transaction
2446 * Returns <0 on error and aborts the transaction
2447 */ 2246 */
2448int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, 2247int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2449 struct btrfs_root *root, unsigned long count) 2248 struct btrfs_root *root, unsigned long count)
@@ -2453,30 +2252,16 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2453 struct btrfs_delayed_ref_node *ref; 2252 struct btrfs_delayed_ref_node *ref;
2454 struct list_head cluster; 2253 struct list_head cluster;
2455 int ret; 2254 int ret;
2456 u64 delayed_start;
2457 int run_all = count == (unsigned long)-1; 2255 int run_all = count == (unsigned long)-1;
2458 int run_most = 0; 2256 int run_most = 0;
2459 int loops;
2460
2461 /* We'll clean this up in btrfs_cleanup_transaction */
2462 if (trans->aborted)
2463 return 0;
2464 2257
2465 if (root == root->fs_info->extent_root) 2258 if (root == root->fs_info->extent_root)
2466 root = root->fs_info->tree_root; 2259 root = root->fs_info->tree_root;
2467 2260
2468 btrfs_delayed_refs_qgroup_accounting(trans, root->fs_info);
2469
2470 delayed_refs = &trans->transaction->delayed_refs; 2261 delayed_refs = &trans->transaction->delayed_refs;
2471 INIT_LIST_HEAD(&cluster); 2262 INIT_LIST_HEAD(&cluster);
2472again: 2263again:
2473 loops = 0;
2474 spin_lock(&delayed_refs->lock); 2264 spin_lock(&delayed_refs->lock);
2475
2476#ifdef SCRAMBLE_DELAYED_REFS
2477 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2478#endif
2479
2480 if (count == 0) { 2265 if (count == 0) {
2481 count = delayed_refs->num_entries * 2; 2266 count = delayed_refs->num_entries * 2;
2482 run_most = 1; 2267 run_most = 1;
@@ -2492,54 +2277,21 @@ again:
2492 * of refs to process starting at the first one we are able to 2277 * of refs to process starting at the first one we are able to
2493 * lock 2278 * lock
2494 */ 2279 */
2495 delayed_start = delayed_refs->run_delayed_start;
2496 ret = btrfs_find_ref_cluster(trans, &cluster, 2280 ret = btrfs_find_ref_cluster(trans, &cluster,
2497 delayed_refs->run_delayed_start); 2281 delayed_refs->run_delayed_start);
2498 if (ret) 2282 if (ret)
2499 break; 2283 break;
2500 2284
2501 ret = run_clustered_refs(trans, root, &cluster); 2285 ret = run_clustered_refs(trans, root, &cluster);
2502 if (ret < 0) { 2286 BUG_ON(ret < 0);
2503 spin_unlock(&delayed_refs->lock);
2504 btrfs_abort_transaction(trans, root, ret);
2505 return ret;
2506 }
2507 2287
2508 count -= min_t(unsigned long, ret, count); 2288 count -= min_t(unsigned long, ret, count);
2509 2289
2510 if (count == 0) 2290 if (count == 0)
2511 break; 2291 break;
2512
2513 if (delayed_start >= delayed_refs->run_delayed_start) {
2514 if (loops == 0) {
2515 /*
2516 * btrfs_find_ref_cluster looped. let's do one
2517 * more cycle. if we don't run any delayed ref
2518 * during that cycle (because we can't because
2519 * all of them are blocked), bail out.
2520 */
2521 loops = 1;
2522 } else {
2523 /*
2524 * no runnable refs left, stop trying
2525 */
2526 BUG_ON(run_all);
2527 break;
2528 }
2529 }
2530 if (ret) {
2531 /* refs were run, let's reset staleness detection */
2532 loops = 0;
2533 }
2534 } 2292 }
2535 2293
2536 if (run_all) { 2294 if (run_all) {
2537 if (!list_empty(&trans->new_bgs)) {
2538 spin_unlock(&delayed_refs->lock);
2539 btrfs_create_pending_block_groups(trans, root);
2540 spin_lock(&delayed_refs->lock);
2541 }
2542
2543 node = rb_first(&delayed_refs->root); 2295 node = rb_first(&delayed_refs->root);
2544 if (!node) 2296 if (!node)
2545 goto out; 2297 goto out;
@@ -2574,7 +2326,6 @@ again:
2574 } 2326 }
2575out: 2327out:
2576 spin_unlock(&delayed_refs->lock); 2328 spin_unlock(&delayed_refs->lock);
2577 assert_qgroups_uptodate(trans);
2578 return 0; 2329 return 0;
2579} 2330}
2580 2331
@@ -2595,8 +2346,7 @@ int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2595 extent_op->update_key = 0; 2346 extent_op->update_key = 0;
2596 extent_op->is_data = is_data ? 1 : 0; 2347 extent_op->is_data = is_data ? 1 : 0;
2597 2348
2598 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr, 2349 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2599 num_bytes, extent_op);
2600 if (ret) 2350 if (ret)
2601 kfree(extent_op); 2351 kfree(extent_op);
2602 return ret; 2352 return ret;
@@ -2654,10 +2404,8 @@ static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2654 2404
2655 node = rb_prev(node); 2405 node = rb_prev(node);
2656 if (node) { 2406 if (node) {
2657 int seq = ref->seq;
2658
2659 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); 2407 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2660 if (ref->bytenr == bytenr && ref->seq == seq) 2408 if (ref->bytenr == bytenr)
2661 goto out_unlock; 2409 goto out_unlock;
2662 } 2410 }
2663 2411
@@ -2694,7 +2442,7 @@ static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2694 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); 2442 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2695 if (ret < 0) 2443 if (ret < 0)
2696 goto out; 2444 goto out;
2697 BUG_ON(ret == 0); /* Corruption */ 2445 BUG_ON(ret == 0);
2698 2446
2699 ret = -ENOENT; 2447 ret = -ENOENT;
2700 if (path->slots[0] == 0) 2448 if (path->slots[0] == 0)
@@ -2783,7 +2531,7 @@ out:
2783static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, 2531static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2784 struct btrfs_root *root, 2532 struct btrfs_root *root,
2785 struct extent_buffer *buf, 2533 struct extent_buffer *buf,
2786 int full_backref, int inc, int for_cow) 2534 int full_backref, int inc)
2787{ 2535{
2788 u64 bytenr; 2536 u64 bytenr;
2789 u64 num_bytes; 2537 u64 num_bytes;
@@ -2796,7 +2544,7 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2796 int level; 2544 int level;
2797 int ret = 0; 2545 int ret = 0;
2798 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, 2546 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2799 u64, u64, u64, u64, u64, u64, int); 2547 u64, u64, u64, u64, u64, u64);
2800 2548
2801 ref_root = btrfs_header_owner(buf); 2549 ref_root = btrfs_header_owner(buf);
2802 nritems = btrfs_header_nritems(buf); 2550 nritems = btrfs_header_nritems(buf);
@@ -2833,34 +2581,34 @@ static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2833 key.offset -= btrfs_file_extent_offset(buf, fi); 2581 key.offset -= btrfs_file_extent_offset(buf, fi);
2834 ret = process_func(trans, root, bytenr, num_bytes, 2582 ret = process_func(trans, root, bytenr, num_bytes,
2835 parent, ref_root, key.objectid, 2583 parent, ref_root, key.objectid,
2836 key.offset, for_cow); 2584 key.offset);
2837 if (ret) 2585 if (ret)
2838 goto fail; 2586 goto fail;
2839 } else { 2587 } else {
2840 bytenr = btrfs_node_blockptr(buf, i); 2588 bytenr = btrfs_node_blockptr(buf, i);
2841 num_bytes = btrfs_level_size(root, level - 1); 2589 num_bytes = btrfs_level_size(root, level - 1);
2842 ret = process_func(trans, root, bytenr, num_bytes, 2590 ret = process_func(trans, root, bytenr, num_bytes,
2843 parent, ref_root, level - 1, 0, 2591 parent, ref_root, level - 1, 0);
2844 for_cow);
2845 if (ret) 2592 if (ret)
2846 goto fail; 2593 goto fail;
2847 } 2594 }
2848 } 2595 }
2849 return 0; 2596 return 0;
2850fail: 2597fail:
2598 BUG();
2851 return ret; 2599 return ret;
2852} 2600}
2853 2601
2854int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2602int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2855 struct extent_buffer *buf, int full_backref, int for_cow) 2603 struct extent_buffer *buf, int full_backref)
2856{ 2604{
2857 return __btrfs_mod_ref(trans, root, buf, full_backref, 1, for_cow); 2605 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2858} 2606}
2859 2607
2860int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 2608int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2861 struct extent_buffer *buf, int full_backref, int for_cow) 2609 struct extent_buffer *buf, int full_backref)
2862{ 2610{
2863 return __btrfs_mod_ref(trans, root, buf, full_backref, 0, for_cow); 2611 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2864} 2612}
2865 2613
2866static int write_one_cache_group(struct btrfs_trans_handle *trans, 2614static int write_one_cache_group(struct btrfs_trans_handle *trans,
@@ -2876,7 +2624,7 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
2876 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); 2624 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2877 if (ret < 0) 2625 if (ret < 0)
2878 goto fail; 2626 goto fail;
2879 BUG_ON(ret); /* Corruption */ 2627 BUG_ON(ret);
2880 2628
2881 leaf = path->nodes[0]; 2629 leaf = path->nodes[0];
2882 bi = btrfs_item_ptr_offset(leaf, path->slots[0]); 2630 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
@@ -2884,10 +2632,8 @@ static int write_one_cache_group(struct btrfs_trans_handle *trans,
2884 btrfs_mark_buffer_dirty(leaf); 2632 btrfs_mark_buffer_dirty(leaf);
2885 btrfs_release_path(path); 2633 btrfs_release_path(path);
2886fail: 2634fail:
2887 if (ret) { 2635 if (ret)
2888 btrfs_abort_transaction(trans, root, ret);
2889 return ret; 2636 return ret;
2890 }
2891 return 0; 2637 return 0;
2892 2638
2893} 2639}
@@ -2954,13 +2700,6 @@ again:
2954 goto again; 2700 goto again;
2955 } 2701 }
2956 2702
2957 /* We've already setup this transaction, go ahead and exit */
2958 if (block_group->cache_generation == trans->transid &&
2959 i_size_read(inode)) {
2960 dcs = BTRFS_DC_SETUP;
2961 goto out_put;
2962 }
2963
2964 /* 2703 /*
2965 * We want to set the generation to 0, that way if anything goes wrong 2704 * We want to set the generation to 0, that way if anything goes wrong
2966 * from here on out we know not to trust this cache when we load up next 2705 * from here on out we know not to trust this cache when we load up next
@@ -2978,29 +2717,25 @@ again:
2978 } 2717 }
2979 2718
2980 spin_lock(&block_group->lock); 2719 spin_lock(&block_group->lock);
2981 if (block_group->cached != BTRFS_CACHE_FINISHED || 2720 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2982 !btrfs_test_opt(root, SPACE_CACHE)) { 2721 /* We're not cached, don't bother trying to write stuff out */
2983 /*
2984 * don't bother trying to write stuff out _if_
2985 * a) we're not cached,
2986 * b) we're with nospace_cache mount option.
2987 */
2988 dcs = BTRFS_DC_WRITTEN; 2722 dcs = BTRFS_DC_WRITTEN;
2989 spin_unlock(&block_group->lock); 2723 spin_unlock(&block_group->lock);
2990 goto out_put; 2724 goto out_put;
2991 } 2725 }
2992 spin_unlock(&block_group->lock); 2726 spin_unlock(&block_group->lock);
2993 2727
2994 /* 2728 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2995 * Try to preallocate enough space based on how big the block group is.
2996 * Keep in mind this has to include any pinned space which could end up
2997 * taking up quite a bit since it's not folded into the other space
2998 * cache.
2999 */
3000 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
3001 if (!num_pages) 2729 if (!num_pages)
3002 num_pages = 1; 2730 num_pages = 1;
3003 2731
2732 /*
2733 * Just to make absolutely sure we have enough space, we're going to
2734 * preallocate 12 pages worth of space for each block group. In
2735 * practice we ought to use at most 8, but we need extra space so we can
2736 * add our header and have a terminator between the extents and the
2737 * bitmaps.
2738 */
3004 num_pages *= 16; 2739 num_pages *= 16;
3005 num_pages *= PAGE_CACHE_SIZE; 2740 num_pages *= PAGE_CACHE_SIZE;
3006 2741
@@ -3014,15 +2749,12 @@ again:
3014 if (!ret) 2749 if (!ret)
3015 dcs = BTRFS_DC_SETUP; 2750 dcs = BTRFS_DC_SETUP;
3016 btrfs_free_reserved_data_space(inode, num_pages); 2751 btrfs_free_reserved_data_space(inode, num_pages);
3017
3018out_put: 2752out_put:
3019 iput(inode); 2753 iput(inode);
3020out_free: 2754out_free:
3021 btrfs_release_path(path); 2755 btrfs_release_path(path);
3022out: 2756out:
3023 spin_lock(&block_group->lock); 2757 spin_lock(&block_group->lock);
3024 if (!ret && dcs == BTRFS_DC_SETUP)
3025 block_group->cache_generation = trans->transid;
3026 block_group->disk_cache_state = dcs; 2758 block_group->disk_cache_state = dcs;
3027 spin_unlock(&block_group->lock); 2759 spin_unlock(&block_group->lock);
3028 2760
@@ -3064,8 +2796,7 @@ again:
3064 if (last == 0) { 2796 if (last == 0) {
3065 err = btrfs_run_delayed_refs(trans, root, 2797 err = btrfs_run_delayed_refs(trans, root,
3066 (unsigned long)-1); 2798 (unsigned long)-1);
3067 if (err) /* File system offline */ 2799 BUG_ON(err);
3068 goto out;
3069 } 2800 }
3070 2801
3071 cache = btrfs_lookup_first_block_group(root->fs_info, last); 2802 cache = btrfs_lookup_first_block_group(root->fs_info, last);
@@ -3092,9 +2823,7 @@ again:
3092 last = cache->key.objectid + cache->key.offset; 2823 last = cache->key.objectid + cache->key.offset;
3093 2824
3094 err = write_one_cache_group(trans, root, path, cache); 2825 err = write_one_cache_group(trans, root, path, cache);
3095 if (err) /* File system offline */ 2826 BUG_ON(err);
3096 goto out;
3097
3098 btrfs_put_block_group(cache); 2827 btrfs_put_block_group(cache);
3099 } 2828 }
3100 2829
@@ -3107,8 +2836,7 @@ again:
3107 if (last == 0) { 2836 if (last == 0) {
3108 err = btrfs_run_delayed_refs(trans, root, 2837 err = btrfs_run_delayed_refs(trans, root,
3109 (unsigned long)-1); 2838 (unsigned long)-1);
3110 if (err) /* File system offline */ 2839 BUG_ON(err);
3111 goto out;
3112 } 2840 }
3113 2841
3114 cache = btrfs_lookup_first_block_group(root->fs_info, last); 2842 cache = btrfs_lookup_first_block_group(root->fs_info, last);
@@ -3133,21 +2861,20 @@ again:
3133 continue; 2861 continue;
3134 } 2862 }
3135 2863
3136 err = btrfs_write_out_cache(root, trans, cache, path); 2864 btrfs_write_out_cache(root, trans, cache, path);
3137 2865
3138 /* 2866 /*
3139 * If we didn't have an error then the cache state is still 2867 * If we didn't have an error then the cache state is still
3140 * NEED_WRITE, so we can set it to WRITTEN. 2868 * NEED_WRITE, so we can set it to WRITTEN.
3141 */ 2869 */
3142 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE) 2870 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3143 cache->disk_cache_state = BTRFS_DC_WRITTEN; 2871 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3144 last = cache->key.objectid + cache->key.offset; 2872 last = cache->key.objectid + cache->key.offset;
3145 btrfs_put_block_group(cache); 2873 btrfs_put_block_group(cache);
3146 } 2874 }
3147out:
3148 2875
3149 btrfs_free_path(path); 2876 btrfs_free_path(path);
3150 return err; 2877 return 0;
3151} 2878}
3152 2879
3153int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) 2880int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
@@ -3197,7 +2924,9 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3197 INIT_LIST_HEAD(&found->block_groups[i]); 2924 INIT_LIST_HEAD(&found->block_groups[i]);
3198 init_rwsem(&found->groups_sem); 2925 init_rwsem(&found->groups_sem);
3199 spin_lock_init(&found->lock); 2926 spin_lock_init(&found->lock);
3200 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; 2927 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2928 BTRFS_BLOCK_GROUP_SYSTEM |
2929 BTRFS_BLOCK_GROUP_METADATA);
3201 found->total_bytes = total_bytes; 2930 found->total_bytes = total_bytes;
3202 found->disk_total = total_bytes * factor; 2931 found->disk_total = total_bytes * factor;
3203 found->bytes_used = bytes_used; 2932 found->bytes_used = bytes_used;
@@ -3213,59 +2942,25 @@ static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3213 init_waitqueue_head(&found->wait); 2942 init_waitqueue_head(&found->wait);
3214 *space_info = found; 2943 *space_info = found;
3215 list_add_rcu(&found->list, &info->space_info); 2944 list_add_rcu(&found->list, &info->space_info);
3216 if (flags & BTRFS_BLOCK_GROUP_DATA)
3217 info->data_sinfo = found;
3218 return 0; 2945 return 0;
3219} 2946}
3220 2947
3221static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) 2948static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3222{ 2949{
3223 u64 extra_flags = chunk_to_extended(flags) & 2950 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
3224 BTRFS_EXTENDED_PROFILE_MASK; 2951 BTRFS_BLOCK_GROUP_RAID1 |
3225 2952 BTRFS_BLOCK_GROUP_RAID10 |
3226 if (flags & BTRFS_BLOCK_GROUP_DATA) 2953 BTRFS_BLOCK_GROUP_DUP);
3227 fs_info->avail_data_alloc_bits |= extra_flags; 2954 if (extra_flags) {
3228 if (flags & BTRFS_BLOCK_GROUP_METADATA) 2955 if (flags & BTRFS_BLOCK_GROUP_DATA)
3229 fs_info->avail_metadata_alloc_bits |= extra_flags; 2956 fs_info->avail_data_alloc_bits |= extra_flags;
3230 if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 2957 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3231 fs_info->avail_system_alloc_bits |= extra_flags; 2958 fs_info->avail_metadata_alloc_bits |= extra_flags;
3232} 2959 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3233 2960 fs_info->avail_system_alloc_bits |= extra_flags;
3234/*
3235 * returns target flags in extended format or 0 if restripe for this
3236 * chunk_type is not in progress
3237 *
3238 * should be called with either volume_mutex or balance_lock held
3239 */
3240static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3241{
3242 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3243 u64 target = 0;
3244
3245 if (!bctl)
3246 return 0;
3247
3248 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3249 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3250 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3251 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3252 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3253 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3254 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3255 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3256 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3257 } 2961 }
3258
3259 return target;
3260} 2962}
3261 2963
3262/*
3263 * @flags: available profiles in extended format (see ctree.h)
3264 *
3265 * Returns reduced profile in chunk format. If profile changing is in
3266 * progress (either running or paused) picks the target profile (if it's
3267 * already available), otherwise falls back to plain reducing.
3268 */
3269u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) 2964u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3270{ 2965{
3271 /* 2966 /*
@@ -3275,22 +2970,6 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3275 */ 2970 */
3276 u64 num_devices = root->fs_info->fs_devices->rw_devices + 2971 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3277 root->fs_info->fs_devices->missing_devices; 2972 root->fs_info->fs_devices->missing_devices;
3278 u64 target;
3279
3280 /*
3281 * see if restripe for this chunk_type is in progress, if so
3282 * try to reduce to the target profile
3283 */
3284 spin_lock(&root->fs_info->balance_lock);
3285 target = get_restripe_target(root->fs_info, flags);
3286 if (target) {
3287 /* pick target profile only if it's already available */
3288 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
3289 spin_unlock(&root->fs_info->balance_lock);
3290 return extended_to_chunk(target);
3291 }
3292 }
3293 spin_unlock(&root->fs_info->balance_lock);
3294 2973
3295 if (num_devices == 1) 2974 if (num_devices == 1)
3296 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); 2975 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
@@ -3311,22 +2990,22 @@ u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3311 if ((flags & BTRFS_BLOCK_GROUP_RAID0) && 2990 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3312 ((flags & BTRFS_BLOCK_GROUP_RAID1) | 2991 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3313 (flags & BTRFS_BLOCK_GROUP_RAID10) | 2992 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3314 (flags & BTRFS_BLOCK_GROUP_DUP))) { 2993 (flags & BTRFS_BLOCK_GROUP_DUP)))
3315 flags &= ~BTRFS_BLOCK_GROUP_RAID0; 2994 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3316 } 2995 return flags;
3317
3318 return extended_to_chunk(flags);
3319} 2996}
3320 2997
3321static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) 2998static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
3322{ 2999{
3323 if (flags & BTRFS_BLOCK_GROUP_DATA) 3000 if (flags & BTRFS_BLOCK_GROUP_DATA)
3324 flags |= root->fs_info->avail_data_alloc_bits; 3001 flags |= root->fs_info->avail_data_alloc_bits &
3002 root->fs_info->data_alloc_profile;
3325 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) 3003 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3326 flags |= root->fs_info->avail_system_alloc_bits; 3004 flags |= root->fs_info->avail_system_alloc_bits &
3005 root->fs_info->system_alloc_profile;
3327 else if (flags & BTRFS_BLOCK_GROUP_METADATA) 3006 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3328 flags |= root->fs_info->avail_metadata_alloc_bits; 3007 flags |= root->fs_info->avail_metadata_alloc_bits &
3329 3008 root->fs_info->metadata_alloc_profile;
3330 return btrfs_reduce_alloc_profile(root, flags); 3009 return btrfs_reduce_alloc_profile(root, flags);
3331} 3010}
3332 3011
@@ -3344,6 +3023,12 @@ u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3344 return get_alloc_profile(root, flags); 3023 return get_alloc_profile(root, flags);
3345} 3024}
3346 3025
3026void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3027{
3028 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
3029 BTRFS_BLOCK_GROUP_DATA);
3030}
3031
3347/* 3032/*
3348 * This will check the space that the inode allocates from to make sure we have 3033 * This will check the space that the inode allocates from to make sure we have
3349 * enough space for bytes. 3034 * enough space for bytes.
@@ -3352,7 +3037,6 @@ int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3352{ 3037{
3353 struct btrfs_space_info *data_sinfo; 3038 struct btrfs_space_info *data_sinfo;
3354 struct btrfs_root *root = BTRFS_I(inode)->root; 3039 struct btrfs_root *root = BTRFS_I(inode)->root;
3355 struct btrfs_fs_info *fs_info = root->fs_info;
3356 u64 used; 3040 u64 used;
3357 int ret = 0, committed = 0, alloc_chunk = 1; 3041 int ret = 0, committed = 0, alloc_chunk = 1;
3358 3042
@@ -3365,7 +3049,7 @@ int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3365 committed = 1; 3049 committed = 1;
3366 } 3050 }
3367 3051
3368 data_sinfo = fs_info->data_sinfo; 3052 data_sinfo = BTRFS_I(inode)->space_info;
3369 if (!data_sinfo) 3053 if (!data_sinfo)
3370 goto alloc; 3054 goto alloc;
3371 3055
@@ -3395,6 +3079,7 @@ alloc:
3395 return PTR_ERR(trans); 3079 return PTR_ERR(trans);
3396 3080
3397 ret = do_chunk_alloc(trans, root->fs_info->extent_root, 3081 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3082 bytes + 2 * 1024 * 1024,
3398 alloc_target, 3083 alloc_target,
3399 CHUNK_ALLOC_NO_FORCE); 3084 CHUNK_ALLOC_NO_FORCE);
3400 btrfs_end_transaction(trans, root); 3085 btrfs_end_transaction(trans, root);
@@ -3405,9 +3090,10 @@ alloc:
3405 goto commit_trans; 3090 goto commit_trans;
3406 } 3091 }
3407 3092
3408 if (!data_sinfo) 3093 if (!data_sinfo) {
3409 data_sinfo = fs_info->data_sinfo; 3094 btrfs_set_inode_space_info(root, inode);
3410 3095 data_sinfo = BTRFS_I(inode)->space_info;
3096 }
3411 goto again; 3097 goto again;
3412 } 3098 }
3413 3099
@@ -3436,15 +3122,16 @@ commit_trans:
3436 return -ENOSPC; 3122 return -ENOSPC;
3437 } 3123 }
3438 data_sinfo->bytes_may_use += bytes; 3124 data_sinfo->bytes_may_use += bytes;
3439 trace_btrfs_space_reservation(root->fs_info, "space_info", 3125 BTRFS_I(inode)->reserved_bytes += bytes;
3440 data_sinfo->flags, bytes, 1);
3441 spin_unlock(&data_sinfo->lock); 3126 spin_unlock(&data_sinfo->lock);
3442 3127
3443 return 0; 3128 return 0;
3444} 3129}
3445 3130
3446/* 3131/*
3447 * Called if we need to clear a data reservation for this inode. 3132 * called when we are clearing an delalloc extent from the
3133 * inode's io_tree or there was an error for whatever reason
3134 * after calling btrfs_check_data_free_space
3448 */ 3135 */
3449void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) 3136void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3450{ 3137{
@@ -3454,11 +3141,10 @@ void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3454 /* make sure bytes are sectorsize aligned */ 3141 /* make sure bytes are sectorsize aligned */
3455 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); 3142 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3456 3143
3457 data_sinfo = root->fs_info->data_sinfo; 3144 data_sinfo = BTRFS_I(inode)->space_info;
3458 spin_lock(&data_sinfo->lock); 3145 spin_lock(&data_sinfo->lock);
3459 data_sinfo->bytes_may_use -= bytes; 3146 data_sinfo->bytes_may_use -= bytes;
3460 trace_btrfs_space_reservation(root->fs_info, "space_info", 3147 BTRFS_I(inode)->reserved_bytes -= bytes;
3461 data_sinfo->flags, bytes, 0);
3462 spin_unlock(&data_sinfo->lock); 3148 spin_unlock(&data_sinfo->lock);
3463} 3149}
3464 3150
@@ -3476,9 +3162,9 @@ static void force_metadata_allocation(struct btrfs_fs_info *info)
3476} 3162}
3477 3163
3478static int should_alloc_chunk(struct btrfs_root *root, 3164static int should_alloc_chunk(struct btrfs_root *root,
3479 struct btrfs_space_info *sinfo, int force) 3165 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3166 int force)
3480{ 3167{
3481 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3482 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; 3168 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3483 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; 3169 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3484 u64 thresh; 3170 u64 thresh;
@@ -3487,19 +3173,11 @@ static int should_alloc_chunk(struct btrfs_root *root,
3487 return 1; 3173 return 1;
3488 3174
3489 /* 3175 /*
3490 * We need to take into account the global rsv because for all intents
3491 * and purposes it's used space. Don't worry about locking the
3492 * global_rsv, it doesn't change except when the transaction commits.
3493 */
3494 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3495 num_allocated += global_rsv->size;
3496
3497 /*
3498 * in limited mode, we want to have some free space up to 3176 * in limited mode, we want to have some free space up to
3499 * about 1% of the FS size. 3177 * about 1% of the FS size.
3500 */ 3178 */
3501 if (force == CHUNK_ALLOC_LIMITED) { 3179 if (force == CHUNK_ALLOC_LIMITED) {
3502 thresh = btrfs_super_total_bytes(root->fs_info->super_copy); 3180 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3503 thresh = max_t(u64, 64 * 1024 * 1024, 3181 thresh = max_t(u64, 64 * 1024 * 1024,
3504 div_factor_fine(thresh, 1)); 3182 div_factor_fine(thresh, 1));
3505 3183
@@ -3507,81 +3185,59 @@ static int should_alloc_chunk(struct btrfs_root *root,
3507 return 1; 3185 return 1;
3508 } 3186 }
3509 3187
3510 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8)) 3188 /*
3189 * we have two similar checks here, one based on percentage
3190 * and once based on a hard number of 256MB. The idea
3191 * is that if we have a good amount of free
3192 * room, don't allocate a chunk. A good mount is
3193 * less than 80% utilized of the chunks we have allocated,
3194 * or more than 256MB free
3195 */
3196 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3511 return 0; 3197 return 0;
3512 return 1;
3513}
3514
3515static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3516{
3517 u64 num_dev;
3518
3519 if (type & BTRFS_BLOCK_GROUP_RAID10 ||
3520 type & BTRFS_BLOCK_GROUP_RAID0)
3521 num_dev = root->fs_info->fs_devices->rw_devices;
3522 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3523 num_dev = 2;
3524 else
3525 num_dev = 1; /* DUP or single */
3526 3198
3527 /* metadata for updaing devices and chunk tree */ 3199 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
3528 return btrfs_calc_trans_metadata_size(root, num_dev + 1); 3200 return 0;
3529}
3530
3531static void check_system_chunk(struct btrfs_trans_handle *trans,
3532 struct btrfs_root *root, u64 type)
3533{
3534 struct btrfs_space_info *info;
3535 u64 left;
3536 u64 thresh;
3537
3538 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3539 spin_lock(&info->lock);
3540 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3541 info->bytes_reserved - info->bytes_readonly;
3542 spin_unlock(&info->lock);
3543 3201
3544 thresh = get_system_chunk_thresh(root, type); 3202 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3545 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3546 printk(KERN_INFO "left=%llu, need=%llu, flags=%llu\n",
3547 left, thresh, type);
3548 dump_space_info(info, 0, 0);
3549 }
3550 3203
3551 if (left < thresh) { 3204 /* 256MB or 5% of the FS */
3552 u64 flags; 3205 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3553 3206
3554 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0); 3207 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
3555 btrfs_alloc_chunk(trans, root, flags); 3208 return 0;
3556 } 3209 return 1;
3557} 3210}
3558 3211
3559static int do_chunk_alloc(struct btrfs_trans_handle *trans, 3212static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3560 struct btrfs_root *extent_root, u64 flags, int force) 3213 struct btrfs_root *extent_root, u64 alloc_bytes,
3214 u64 flags, int force)
3561{ 3215{
3562 struct btrfs_space_info *space_info; 3216 struct btrfs_space_info *space_info;
3563 struct btrfs_fs_info *fs_info = extent_root->fs_info; 3217 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3564 int wait_for_alloc = 0; 3218 int wait_for_alloc = 0;
3565 int ret = 0; 3219 int ret = 0;
3566 3220
3221 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3222
3567 space_info = __find_space_info(extent_root->fs_info, flags); 3223 space_info = __find_space_info(extent_root->fs_info, flags);
3568 if (!space_info) { 3224 if (!space_info) {
3569 ret = update_space_info(extent_root->fs_info, flags, 3225 ret = update_space_info(extent_root->fs_info, flags,
3570 0, 0, &space_info); 3226 0, 0, &space_info);
3571 BUG_ON(ret); /* -ENOMEM */ 3227 BUG_ON(ret);
3572 } 3228 }
3573 BUG_ON(!space_info); /* Logic error */ 3229 BUG_ON(!space_info);
3574 3230
3575again: 3231again:
3576 spin_lock(&space_info->lock); 3232 spin_lock(&space_info->lock);
3577 if (force < space_info->force_alloc) 3233 if (space_info->force_alloc)
3578 force = space_info->force_alloc; 3234 force = space_info->force_alloc;
3579 if (space_info->full) { 3235 if (space_info->full) {
3580 spin_unlock(&space_info->lock); 3236 spin_unlock(&space_info->lock);
3581 return 0; 3237 return 0;
3582 } 3238 }
3583 3239
3584 if (!should_alloc_chunk(extent_root, space_info, force)) { 3240 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
3585 spin_unlock(&space_info->lock); 3241 spin_unlock(&space_info->lock);
3586 return 0; 3242 return 0;
3587 } else if (space_info->chunk_alloc) { 3243 } else if (space_info->chunk_alloc) {
@@ -3625,12 +3281,6 @@ again:
3625 force_metadata_allocation(fs_info); 3281 force_metadata_allocation(fs_info);
3626 } 3282 }
3627 3283
3628 /*
3629 * Check if we have enough space in SYSTEM chunk because we may need
3630 * to update devices.
3631 */
3632 check_system_chunk(trans, extent_root, flags);
3633
3634 ret = btrfs_alloc_chunk(trans, extent_root, flags); 3284 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3635 if (ret < 0 && ret != -ENOSPC) 3285 if (ret < 0 && ret != -ENOSPC)
3636 goto out; 3286 goto out;
@@ -3645,289 +3295,123 @@ again:
3645 space_info->chunk_alloc = 0; 3295 space_info->chunk_alloc = 0;
3646 spin_unlock(&space_info->lock); 3296 spin_unlock(&space_info->lock);
3647out: 3297out:
3648 mutex_unlock(&fs_info->chunk_mutex); 3298 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3649 return ret; 3299 return ret;
3650} 3300}
3651 3301
3652static int can_overcommit(struct btrfs_root *root,
3653 struct btrfs_space_info *space_info, u64 bytes,
3654 enum btrfs_reserve_flush_enum flush)
3655{
3656 u64 profile = btrfs_get_alloc_profile(root, 0);
3657 u64 avail;
3658 u64 used;
3659
3660 used = space_info->bytes_used + space_info->bytes_reserved +
3661 space_info->bytes_pinned + space_info->bytes_readonly +
3662 space_info->bytes_may_use;
3663
3664 spin_lock(&root->fs_info->free_chunk_lock);
3665 avail = root->fs_info->free_chunk_space;
3666 spin_unlock(&root->fs_info->free_chunk_lock);
3667
3668 /*
3669 * If we have dup, raid1 or raid10 then only half of the free
3670 * space is actually useable.
3671 */
3672 if (profile & (BTRFS_BLOCK_GROUP_DUP |
3673 BTRFS_BLOCK_GROUP_RAID1 |
3674 BTRFS_BLOCK_GROUP_RAID10))
3675 avail >>= 1;
3676
3677 /*
3678 * If we aren't flushing all things, let us overcommit up to
3679 * 1/2th of the space. If we can flush, don't let us overcommit
3680 * too much, let it overcommit up to 1/8 of the space.
3681 */
3682 if (flush == BTRFS_RESERVE_FLUSH_ALL)
3683 avail >>= 3;
3684 else
3685 avail >>= 1;
3686
3687 if (used + bytes < space_info->total_bytes + avail)
3688 return 1;
3689 return 0;
3690}
3691
3692static int writeback_inodes_sb_nr_if_idle_safe(struct super_block *sb,
3693 unsigned long nr_pages,
3694 enum wb_reason reason)
3695{
3696 if (!writeback_in_progress(sb->s_bdi) &&
3697 down_read_trylock(&sb->s_umount)) {
3698 writeback_inodes_sb_nr(sb, nr_pages, reason);
3699 up_read(&sb->s_umount);
3700 return 1;
3701 }
3702
3703 return 0;
3704}
3705
3706/* 3302/*
3707 * shrink metadata reservation for delalloc 3303 * shrink metadata reservation for delalloc
3708 */ 3304 */
3709static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, 3305static int shrink_delalloc(struct btrfs_trans_handle *trans,
3710 bool wait_ordered) 3306 struct btrfs_root *root, u64 to_reclaim, int sync)
3711{ 3307{
3712 struct btrfs_block_rsv *block_rsv; 3308 struct btrfs_block_rsv *block_rsv;
3713 struct btrfs_space_info *space_info; 3309 struct btrfs_space_info *space_info;
3714 struct btrfs_trans_handle *trans; 3310 u64 reserved;
3715 u64 delalloc_bytes;
3716 u64 max_reclaim; 3311 u64 max_reclaim;
3312 u64 reclaimed = 0;
3717 long time_left; 3313 long time_left;
3718 unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; 3314 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
3719 int loops = 0; 3315 int loops = 0;
3720 enum btrfs_reserve_flush_enum flush; 3316 unsigned long progress;
3721 3317
3722 trans = (struct btrfs_trans_handle *)current->journal_info;
3723 block_rsv = &root->fs_info->delalloc_block_rsv; 3318 block_rsv = &root->fs_info->delalloc_block_rsv;
3724 space_info = block_rsv->space_info; 3319 space_info = block_rsv->space_info;
3725 3320
3726 smp_mb(); 3321 smp_mb();
3727 delalloc_bytes = root->fs_info->delalloc_bytes; 3322 reserved = space_info->bytes_reserved;
3728 if (delalloc_bytes == 0) { 3323 progress = space_info->reservation_progress;
3324
3325 if (reserved == 0)
3326 return 0;
3327
3328 smp_mb();
3329 if (root->fs_info->delalloc_bytes == 0) {
3729 if (trans) 3330 if (trans)
3730 return; 3331 return 0;
3731 btrfs_wait_ordered_extents(root, 0); 3332 btrfs_wait_ordered_extents(root, 0, 0);
3732 return; 3333 return 0;
3733 } 3334 }
3734 3335
3735 while (delalloc_bytes && loops < 3) { 3336 max_reclaim = min(reserved, to_reclaim);
3736 max_reclaim = min(delalloc_bytes, to_reclaim);
3737 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
3738 writeback_inodes_sb_nr_if_idle_safe(root->fs_info->sb,
3739 nr_pages,
3740 WB_REASON_FS_FREE_SPACE);
3741 3337
3742 /* 3338 while (loops < 1024) {
3743 * We need to wait for the async pages to actually start before 3339 /* have the flusher threads jump in and do some IO */
3744 * we do anything. 3340 smp_mb();
3745 */ 3341 nr_pages = min_t(unsigned long, nr_pages,
3746 wait_event(root->fs_info->async_submit_wait, 3342 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3747 !atomic_read(&root->fs_info->async_delalloc_pages)); 3343 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
3748 3344
3749 if (!trans)
3750 flush = BTRFS_RESERVE_FLUSH_ALL;
3751 else
3752 flush = BTRFS_RESERVE_NO_FLUSH;
3753 spin_lock(&space_info->lock); 3345 spin_lock(&space_info->lock);
3754 if (can_overcommit(root, space_info, orig, flush)) { 3346 if (reserved > space_info->bytes_reserved)
3755 spin_unlock(&space_info->lock); 3347 reclaimed += reserved - space_info->bytes_reserved;
3756 break; 3348 reserved = space_info->bytes_reserved;
3757 }
3758 spin_unlock(&space_info->lock); 3349 spin_unlock(&space_info->lock);
3759 3350
3760 loops++; 3351 loops++;
3761 if (wait_ordered && !trans) {
3762 btrfs_wait_ordered_extents(root, 0);
3763 } else {
3764 time_left = schedule_timeout_killable(1);
3765 if (time_left)
3766 break;
3767 }
3768 smp_mb();
3769 delalloc_bytes = root->fs_info->delalloc_bytes;
3770 }
3771}
3772
3773/**
3774 * maybe_commit_transaction - possibly commit the transaction if its ok to
3775 * @root - the root we're allocating for
3776 * @bytes - the number of bytes we want to reserve
3777 * @force - force the commit
3778 *
3779 * This will check to make sure that committing the transaction will actually
3780 * get us somewhere and then commit the transaction if it does. Otherwise it
3781 * will return -ENOSPC.
3782 */
3783static int may_commit_transaction(struct btrfs_root *root,
3784 struct btrfs_space_info *space_info,
3785 u64 bytes, int force)
3786{
3787 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
3788 struct btrfs_trans_handle *trans;
3789
3790 trans = (struct btrfs_trans_handle *)current->journal_info;
3791 if (trans)
3792 return -EAGAIN;
3793
3794 if (force)
3795 goto commit;
3796
3797 /* See if there is enough pinned space to make this reservation */
3798 spin_lock(&space_info->lock);
3799 if (space_info->bytes_pinned >= bytes) {
3800 spin_unlock(&space_info->lock);
3801 goto commit;
3802 }
3803 spin_unlock(&space_info->lock);
3804
3805 /*
3806 * See if there is some space in the delayed insertion reservation for
3807 * this reservation.
3808 */
3809 if (space_info != delayed_rsv->space_info)
3810 return -ENOSPC;
3811
3812 spin_lock(&space_info->lock);
3813 spin_lock(&delayed_rsv->lock);
3814 if (space_info->bytes_pinned + delayed_rsv->size < bytes) {
3815 spin_unlock(&delayed_rsv->lock);
3816 spin_unlock(&space_info->lock);
3817 return -ENOSPC;
3818 }
3819 spin_unlock(&delayed_rsv->lock);
3820 spin_unlock(&space_info->lock);
3821 3352
3822commit: 3353 if (reserved == 0 || reclaimed >= max_reclaim)
3823 trans = btrfs_join_transaction(root); 3354 break;
3824 if (IS_ERR(trans))
3825 return -ENOSPC;
3826 3355
3827 return btrfs_commit_transaction(trans, root); 3356 if (trans && trans->transaction->blocked)
3828} 3357 return -EAGAIN;
3829 3358
3830enum flush_state { 3359 time_left = schedule_timeout_interruptible(1);
3831 FLUSH_DELAYED_ITEMS_NR = 1,
3832 FLUSH_DELAYED_ITEMS = 2,
3833 FLUSH_DELALLOC = 3,
3834 FLUSH_DELALLOC_WAIT = 4,
3835 ALLOC_CHUNK = 5,
3836 COMMIT_TRANS = 6,
3837};
3838 3360
3839static int flush_space(struct btrfs_root *root, 3361 /* We were interrupted, exit */
3840 struct btrfs_space_info *space_info, u64 num_bytes, 3362 if (time_left)
3841 u64 orig_bytes, int state) 3363 break;
3842{
3843 struct btrfs_trans_handle *trans;
3844 int nr;
3845 int ret = 0;
3846 3364
3847 switch (state) { 3365 /* we've kicked the IO a few times, if anything has been freed,
3848 case FLUSH_DELAYED_ITEMS_NR: 3366 * exit. There is no sense in looping here for a long time
3849 case FLUSH_DELAYED_ITEMS: 3367 * when we really need to commit the transaction, or there are
3850 if (state == FLUSH_DELAYED_ITEMS_NR) { 3368 * just too many writers without enough free space
3851 u64 bytes = btrfs_calc_trans_metadata_size(root, 1); 3369 */
3852 3370
3853 nr = (int)div64_u64(num_bytes, bytes); 3371 if (loops > 3) {
3854 if (!nr) 3372 smp_mb();
3855 nr = 1; 3373 if (progress != space_info->reservation_progress)
3856 nr *= 2; 3374 break;
3857 } else {
3858 nr = -1;
3859 }
3860 trans = btrfs_join_transaction(root);
3861 if (IS_ERR(trans)) {
3862 ret = PTR_ERR(trans);
3863 break;
3864 }
3865 ret = btrfs_run_delayed_items_nr(trans, root, nr);
3866 btrfs_end_transaction(trans, root);
3867 break;
3868 case FLUSH_DELALLOC:
3869 case FLUSH_DELALLOC_WAIT:
3870 shrink_delalloc(root, num_bytes, orig_bytes,
3871 state == FLUSH_DELALLOC_WAIT);
3872 break;
3873 case ALLOC_CHUNK:
3874 trans = btrfs_join_transaction(root);
3875 if (IS_ERR(trans)) {
3876 ret = PTR_ERR(trans);
3877 break;
3878 } 3375 }
3879 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3880 btrfs_get_alloc_profile(root, 0),
3881 CHUNK_ALLOC_NO_FORCE);
3882 btrfs_end_transaction(trans, root);
3883 if (ret == -ENOSPC)
3884 ret = 0;
3885 break;
3886 case COMMIT_TRANS:
3887 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
3888 break;
3889 default:
3890 ret = -ENOSPC;
3891 break;
3892 }
3893 3376
3894 return ret; 3377 }
3378 if (reclaimed >= to_reclaim && !trans)
3379 btrfs_wait_ordered_extents(root, 0, 0);
3380 return reclaimed >= to_reclaim;
3895} 3381}
3896/** 3382
3897 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space 3383/*
3898 * @root - the root we're allocating for 3384 * Retries tells us how many times we've called reserve_metadata_bytes. The
3899 * @block_rsv - the block_rsv we're allocating for 3385 * idea is if this is the first call (retries == 0) then we will add to our
3900 * @orig_bytes - the number of bytes we want 3386 * reserved count if we can't make the allocation in order to hold our place
3901 * @flush - whether or not we can flush to make our reservation 3387 * while we go and try and free up space. That way for retries > 1 we don't try
3388 * and add space, we just check to see if the amount of unused space is >= the
3389 * total space, meaning that our reservation is valid.
3902 * 3390 *
3903 * This will reserve orgi_bytes number of bytes from the space info associated 3391 * However if we don't intend to retry this reservation, pass -1 as retries so
3904 * with the block_rsv. If there is not enough space it will make an attempt to 3392 * that it short circuits this logic.
3905 * flush out space to make room. It will do this by flushing delalloc if
3906 * possible or committing the transaction. If flush is 0 then no attempts to
3907 * regain reservations will be made and this will fail if there is not enough
3908 * space already.
3909 */ 3393 */
3910static int reserve_metadata_bytes(struct btrfs_root *root, 3394static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3395 struct btrfs_root *root,
3911 struct btrfs_block_rsv *block_rsv, 3396 struct btrfs_block_rsv *block_rsv,
3912 u64 orig_bytes, 3397 u64 orig_bytes, int flush)
3913 enum btrfs_reserve_flush_enum flush)
3914{ 3398{
3915 struct btrfs_space_info *space_info = block_rsv->space_info; 3399 struct btrfs_space_info *space_info = block_rsv->space_info;
3916 u64 used; 3400 u64 unused;
3917 u64 num_bytes = orig_bytes; 3401 u64 num_bytes = orig_bytes;
3918 int flush_state = FLUSH_DELAYED_ITEMS_NR; 3402 int retries = 0;
3919 int ret = 0; 3403 int ret = 0;
3404 bool committed = false;
3920 bool flushing = false; 3405 bool flushing = false;
3921 3406
3922again: 3407again:
3923 ret = 0; 3408 ret = 0;
3924 spin_lock(&space_info->lock); 3409 spin_lock(&space_info->lock);
3925 /* 3410 /*
3926 * We only want to wait if somebody other than us is flushing and we 3411 * We only want to wait if somebody other than us is flushing and we are
3927 * are actually allowed to flush all things. 3412 * actually alloed to flush.
3928 */ 3413 */
3929 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing && 3414 while (flush && !flushing && space_info->flush) {
3930 space_info->flush) {
3931 spin_unlock(&space_info->lock); 3415 spin_unlock(&space_info->lock);
3932 /* 3416 /*
3933 * If we have a trans handle we can't wait because the flusher 3417 * If we have a trans handle we can't wait because the flusher
@@ -3935,10 +3419,11 @@ again:
3935 * deadlock since we are waiting for the flusher to finish, but 3419 * deadlock since we are waiting for the flusher to finish, but
3936 * hold the current transaction open. 3420 * hold the current transaction open.
3937 */ 3421 */
3938 if (current->journal_info) 3422 if (trans)
3939 return -EAGAIN; 3423 return -EAGAIN;
3940 ret = wait_event_killable(space_info->wait, !space_info->flush); 3424 ret = wait_event_interruptible(space_info->wait,
3941 /* Must have been killed, return */ 3425 !space_info->flush);
3426 /* Must have been interrupted, return */
3942 if (ret) 3427 if (ret)
3943 return -EINTR; 3428 return -EINTR;
3944 3429
@@ -3946,9 +3431,9 @@ again:
3946 } 3431 }
3947 3432
3948 ret = -ENOSPC; 3433 ret = -ENOSPC;
3949 used = space_info->bytes_used + space_info->bytes_reserved + 3434 unused = space_info->bytes_used + space_info->bytes_reserved +
3950 space_info->bytes_pinned + space_info->bytes_readonly + 3435 space_info->bytes_pinned + space_info->bytes_readonly +
3951 space_info->bytes_may_use; 3436 space_info->bytes_may_use;
3952 3437
3953 /* 3438 /*
3954 * The idea here is that we've not already over-reserved the block group 3439 * The idea here is that we've not already over-reserved the block group
@@ -3957,11 +3442,10 @@ again:
3957 * lets start flushing stuff first and then come back and try to make 3442 * lets start flushing stuff first and then come back and try to make
3958 * our reservation. 3443 * our reservation.
3959 */ 3444 */
3960 if (used <= space_info->total_bytes) { 3445 if (unused <= space_info->total_bytes) {
3961 if (used + orig_bytes <= space_info->total_bytes) { 3446 unused = space_info->total_bytes - unused;
3962 space_info->bytes_may_use += orig_bytes; 3447 if (unused >= num_bytes) {
3963 trace_btrfs_space_reservation(root->fs_info, 3448 space_info->bytes_reserved += orig_bytes;
3964 "space_info", space_info->flags, orig_bytes, 1);
3965 ret = 0; 3449 ret = 0;
3966 } else { 3450 } else {
3967 /* 3451 /*
@@ -3977,57 +3461,73 @@ again:
3977 * amount plus the amount of bytes that we need for this 3461 * amount plus the amount of bytes that we need for this
3978 * reservation. 3462 * reservation.
3979 */ 3463 */
3980 num_bytes = used - space_info->total_bytes + 3464 num_bytes = unused - space_info->total_bytes +
3981 (orig_bytes * 2); 3465 (orig_bytes * (retries + 1));
3982 }
3983
3984 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
3985 space_info->bytes_may_use += orig_bytes;
3986 trace_btrfs_space_reservation(root->fs_info, "space_info",
3987 space_info->flags, orig_bytes,
3988 1);
3989 ret = 0;
3990 } 3466 }
3991 3467
3992 /* 3468 /*
3993 * Couldn't make our reservation, save our place so while we're trying 3469 * Couldn't make our reservation, save our place so while we're trying
3994 * to reclaim space we can actually use it instead of somebody else 3470 * to reclaim space we can actually use it instead of somebody else
3995 * stealing it from us. 3471 * stealing it from us.
3996 *
3997 * We make the other tasks wait for the flush only when we can flush
3998 * all things.
3999 */ 3472 */
4000 if (ret && flush == BTRFS_RESERVE_FLUSH_ALL) { 3473 if (ret && flush) {
4001 flushing = true; 3474 flushing = true;
4002 space_info->flush = 1; 3475 space_info->flush = 1;
4003 } 3476 }
4004 3477
4005 spin_unlock(&space_info->lock); 3478 spin_unlock(&space_info->lock);
4006 3479
4007 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) 3480 if (!ret || !flush)
4008 goto out; 3481 goto out;
4009 3482
4010 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4011 flush_state);
4012 flush_state++;
4013
4014 /* 3483 /*
4015 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock 3484 * We do synchronous shrinking since we don't actually unreserve
4016 * would happen. So skip delalloc flush. 3485 * metadata until after the IO is completed.
4017 */ 3486 */
4018 if (flush == BTRFS_RESERVE_FLUSH_LIMIT && 3487 ret = shrink_delalloc(trans, root, num_bytes, 1);
4019 (flush_state == FLUSH_DELALLOC || 3488 if (ret < 0)
4020 flush_state == FLUSH_DELALLOC_WAIT)) 3489 goto out;
4021 flush_state = ALLOC_CHUNK;
4022 3490
4023 if (!ret) 3491 ret = 0;
4024 goto again; 3492
4025 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT && 3493 /*
4026 flush_state < COMMIT_TRANS) 3494 * So if we were overcommitted it's possible that somebody else flushed
3495 * out enough space and we simply didn't have enough space to reclaim,
3496 * so go back around and try again.
3497 */
3498 if (retries < 2) {
3499 retries++;
4027 goto again; 3500 goto again;
4028 else if (flush == BTRFS_RESERVE_FLUSH_ALL && 3501 }
4029 flush_state <= COMMIT_TRANS) 3502
3503 /*
3504 * Not enough space to be reclaimed, don't bother committing the
3505 * transaction.
3506 */
3507 spin_lock(&space_info->lock);
3508 if (space_info->bytes_pinned < orig_bytes)
3509 ret = -ENOSPC;
3510 spin_unlock(&space_info->lock);
3511 if (ret)
3512 goto out;
3513
3514 ret = -EAGAIN;
3515 if (trans)
3516 goto out;
3517
3518 ret = -ENOSPC;
3519 if (committed)
3520 goto out;
3521
3522 trans = btrfs_join_transaction(root);
3523 if (IS_ERR(trans))
3524 goto out;
3525 ret = btrfs_commit_transaction(trans, root);
3526 if (!ret) {
3527 trans = NULL;
3528 committed = true;
4030 goto again; 3529 goto again;
3530 }
4031 3531
4032out: 3532out:
4033 if (flushing) { 3533 if (flushing) {
@@ -4039,19 +3539,13 @@ out:
4039 return ret; 3539 return ret;
4040} 3540}
4041 3541
4042static struct btrfs_block_rsv *get_block_rsv( 3542static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
4043 const struct btrfs_trans_handle *trans, 3543 struct btrfs_root *root)
4044 const struct btrfs_root *root)
4045{ 3544{
4046 struct btrfs_block_rsv *block_rsv = NULL; 3545 struct btrfs_block_rsv *block_rsv;
4047
4048 if (root->ref_cows) 3546 if (root->ref_cows)
4049 block_rsv = trans->block_rsv; 3547 block_rsv = trans->block_rsv;
4050 3548 else
4051 if (root == root->fs_info->csum_root && trans->adding_csums)
4052 block_rsv = trans->block_rsv;
4053
4054 if (!block_rsv)
4055 block_rsv = root->block_rsv; 3549 block_rsv = root->block_rsv;
4056 3550
4057 if (!block_rsv) 3551 if (!block_rsv)
@@ -4087,8 +3581,7 @@ static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4087 spin_unlock(&block_rsv->lock); 3581 spin_unlock(&block_rsv->lock);
4088} 3582}
4089 3583
4090static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, 3584static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
4091 struct btrfs_block_rsv *block_rsv,
4092 struct btrfs_block_rsv *dest, u64 num_bytes) 3585 struct btrfs_block_rsv *dest, u64 num_bytes)
4093{ 3586{
4094 struct btrfs_space_info *space_info = block_rsv->space_info; 3587 struct btrfs_space_info *space_info = block_rsv->space_info;
@@ -4123,9 +3616,7 @@ static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4123 } 3616 }
4124 if (num_bytes) { 3617 if (num_bytes) {
4125 spin_lock(&space_info->lock); 3618 spin_lock(&space_info->lock);
4126 space_info->bytes_may_use -= num_bytes; 3619 space_info->bytes_reserved -= num_bytes;
4127 trace_btrfs_space_reservation(fs_info, "space_info",
4128 space_info->flags, num_bytes, 0);
4129 space_info->reservation_progress++; 3620 space_info->reservation_progress++;
4130 spin_unlock(&space_info->lock); 3621 spin_unlock(&space_info->lock);
4131 } 3622 }
@@ -4145,15 +3636,16 @@ static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4145 return 0; 3636 return 0;
4146} 3637}
4147 3638
4148void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type) 3639void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
4149{ 3640{
4150 memset(rsv, 0, sizeof(*rsv)); 3641 memset(rsv, 0, sizeof(*rsv));
4151 spin_lock_init(&rsv->lock); 3642 spin_lock_init(&rsv->lock);
4152 rsv->type = type; 3643 atomic_set(&rsv->usage, 1);
3644 rsv->priority = 6;
3645 INIT_LIST_HEAD(&rsv->list);
4153} 3646}
4154 3647
4155struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, 3648struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
4156 unsigned short type)
4157{ 3649{
4158 struct btrfs_block_rsv *block_rsv; 3650 struct btrfs_block_rsv *block_rsv;
4159 struct btrfs_fs_info *fs_info = root->fs_info; 3651 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4162,7 +3654,7 @@ struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4162 if (!block_rsv) 3654 if (!block_rsv)
4163 return NULL; 3655 return NULL;
4164 3656
4165 btrfs_init_block_rsv(block_rsv, type); 3657 btrfs_init_block_rsv(block_rsv);
4166 block_rsv->space_info = __find_space_info(fs_info, 3658 block_rsv->space_info = __find_space_info(fs_info,
4167 BTRFS_BLOCK_GROUP_METADATA); 3659 BTRFS_BLOCK_GROUP_METADATA);
4168 return block_rsv; 3660 return block_rsv;
@@ -4171,22 +3663,38 @@ struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4171void btrfs_free_block_rsv(struct btrfs_root *root, 3663void btrfs_free_block_rsv(struct btrfs_root *root,
4172 struct btrfs_block_rsv *rsv) 3664 struct btrfs_block_rsv *rsv)
4173{ 3665{
4174 if (!rsv) 3666 if (rsv && atomic_dec_and_test(&rsv->usage)) {
4175 return; 3667 btrfs_block_rsv_release(root, rsv, (u64)-1);
4176 btrfs_block_rsv_release(root, rsv, (u64)-1); 3668 if (!rsv->durable)
4177 kfree(rsv); 3669 kfree(rsv);
3670 }
4178} 3671}
4179 3672
4180int btrfs_block_rsv_add(struct btrfs_root *root, 3673/*
4181 struct btrfs_block_rsv *block_rsv, u64 num_bytes, 3674 * make the block_rsv struct be able to capture freed space.
4182 enum btrfs_reserve_flush_enum flush) 3675 * the captured space will re-add to the the block_rsv struct
3676 * after transaction commit
3677 */
3678void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3679 struct btrfs_block_rsv *block_rsv)
3680{
3681 block_rsv->durable = 1;
3682 mutex_lock(&fs_info->durable_block_rsv_mutex);
3683 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3684 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3685}
3686
3687int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3688 struct btrfs_root *root,
3689 struct btrfs_block_rsv *block_rsv,
3690 u64 num_bytes)
4183{ 3691{
4184 int ret; 3692 int ret;
4185 3693
4186 if (num_bytes == 0) 3694 if (num_bytes == 0)
4187 return 0; 3695 return 0;
4188 3696
4189 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); 3697 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
4190 if (!ret) { 3698 if (!ret) {
4191 block_rsv_add_bytes(block_rsv, num_bytes, 1); 3699 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4192 return 0; 3700 return 0;
@@ -4195,52 +3703,55 @@ int btrfs_block_rsv_add(struct btrfs_root *root,
4195 return ret; 3703 return ret;
4196} 3704}
4197 3705
4198int btrfs_block_rsv_check(struct btrfs_root *root, 3706int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
4199 struct btrfs_block_rsv *block_rsv, int min_factor) 3707 struct btrfs_root *root,
3708 struct btrfs_block_rsv *block_rsv,
3709 u64 min_reserved, int min_factor)
4200{ 3710{
4201 u64 num_bytes = 0; 3711 u64 num_bytes = 0;
3712 int commit_trans = 0;
4202 int ret = -ENOSPC; 3713 int ret = -ENOSPC;
4203 3714
4204 if (!block_rsv) 3715 if (!block_rsv)
4205 return 0; 3716 return 0;
4206 3717
4207 spin_lock(&block_rsv->lock); 3718 spin_lock(&block_rsv->lock);
4208 num_bytes = div_factor(block_rsv->size, min_factor); 3719 if (min_factor > 0)
4209 if (block_rsv->reserved >= num_bytes) 3720 num_bytes = div_factor(block_rsv->size, min_factor);
4210 ret = 0; 3721 if (min_reserved > num_bytes)
4211 spin_unlock(&block_rsv->lock); 3722 num_bytes = min_reserved;
4212
4213 return ret;
4214}
4215
4216int btrfs_block_rsv_refill(struct btrfs_root *root,
4217 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4218 enum btrfs_reserve_flush_enum flush)
4219{
4220 u64 num_bytes = 0;
4221 int ret = -ENOSPC;
4222 3723
4223 if (!block_rsv) 3724 if (block_rsv->reserved >= num_bytes) {
4224 return 0;
4225
4226 spin_lock(&block_rsv->lock);
4227 num_bytes = min_reserved;
4228 if (block_rsv->reserved >= num_bytes)
4229 ret = 0; 3725 ret = 0;
4230 else 3726 } else {
4231 num_bytes -= block_rsv->reserved; 3727 num_bytes -= block_rsv->reserved;
3728 if (block_rsv->durable &&
3729 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3730 commit_trans = 1;
3731 }
4232 spin_unlock(&block_rsv->lock); 3732 spin_unlock(&block_rsv->lock);
4233
4234 if (!ret) 3733 if (!ret)
4235 return 0; 3734 return 0;
4236 3735
4237 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); 3736 if (block_rsv->refill_used) {
4238 if (!ret) { 3737 ret = reserve_metadata_bytes(trans, root, block_rsv,
4239 block_rsv_add_bytes(block_rsv, num_bytes, 0); 3738 num_bytes, 0);
3739 if (!ret) {
3740 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3741 return 0;
3742 }
3743 }
3744
3745 if (commit_trans) {
3746 if (trans)
3747 return -EAGAIN;
3748 trans = btrfs_join_transaction(root);
3749 BUG_ON(IS_ERR(trans));
3750 ret = btrfs_commit_transaction(trans, root);
4240 return 0; 3751 return 0;
4241 } 3752 }
4242 3753
4243 return ret; 3754 return -ENOSPC;
4244} 3755}
4245 3756
4246int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, 3757int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
@@ -4258,8 +3769,7 @@ void btrfs_block_rsv_release(struct btrfs_root *root,
4258 if (global_rsv->full || global_rsv == block_rsv || 3769 if (global_rsv->full || global_rsv == block_rsv ||
4259 block_rsv->space_info != global_rsv->space_info) 3770 block_rsv->space_info != global_rsv->space_info)
4260 global_rsv = NULL; 3771 global_rsv = NULL;
4261 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv, 3772 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
4262 num_bytes);
4263} 3773}
4264 3774
4265/* 3775/*
@@ -4273,7 +3783,7 @@ static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4273 u64 num_bytes; 3783 u64 num_bytes;
4274 u64 meta_used; 3784 u64 meta_used;
4275 u64 data_used; 3785 u64 data_used;
4276 int csum_size = btrfs_super_csum_size(fs_info->super_copy); 3786 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
4277 3787
4278 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); 3788 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4279 spin_lock(&sinfo->lock); 3789 spin_lock(&sinfo->lock);
@@ -4305,8 +3815,8 @@ static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4305 3815
4306 num_bytes = calc_global_metadata_size(fs_info); 3816 num_bytes = calc_global_metadata_size(fs_info);
4307 3817
4308 spin_lock(&sinfo->lock);
4309 spin_lock(&block_rsv->lock); 3818 spin_lock(&block_rsv->lock);
3819 spin_lock(&sinfo->lock);
4310 3820
4311 block_rsv->size = num_bytes; 3821 block_rsv->size = num_bytes;
4312 3822
@@ -4317,23 +3827,19 @@ static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4317 if (sinfo->total_bytes > num_bytes) { 3827 if (sinfo->total_bytes > num_bytes) {
4318 num_bytes = sinfo->total_bytes - num_bytes; 3828 num_bytes = sinfo->total_bytes - num_bytes;
4319 block_rsv->reserved += num_bytes; 3829 block_rsv->reserved += num_bytes;
4320 sinfo->bytes_may_use += num_bytes; 3830 sinfo->bytes_reserved += num_bytes;
4321 trace_btrfs_space_reservation(fs_info, "space_info",
4322 sinfo->flags, num_bytes, 1);
4323 } 3831 }
4324 3832
4325 if (block_rsv->reserved >= block_rsv->size) { 3833 if (block_rsv->reserved >= block_rsv->size) {
4326 num_bytes = block_rsv->reserved - block_rsv->size; 3834 num_bytes = block_rsv->reserved - block_rsv->size;
4327 sinfo->bytes_may_use -= num_bytes; 3835 sinfo->bytes_reserved -= num_bytes;
4328 trace_btrfs_space_reservation(fs_info, "space_info",
4329 sinfo->flags, num_bytes, 0);
4330 sinfo->reservation_progress++; 3836 sinfo->reservation_progress++;
4331 block_rsv->reserved = block_rsv->size; 3837 block_rsv->reserved = block_rsv->size;
4332 block_rsv->full = 1; 3838 block_rsv->full = 1;
4333 } 3839 }
4334 3840
4335 spin_unlock(&block_rsv->lock);
4336 spin_unlock(&sinfo->lock); 3841 spin_unlock(&sinfo->lock);
3842 spin_unlock(&block_rsv->lock);
4337} 3843}
4338 3844
4339static void init_global_block_rsv(struct btrfs_fs_info *fs_info) 3845static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
@@ -4342,13 +3848,16 @@ static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4342 3848
4343 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); 3849 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4344 fs_info->chunk_block_rsv.space_info = space_info; 3850 fs_info->chunk_block_rsv.space_info = space_info;
3851 fs_info->chunk_block_rsv.priority = 10;
4345 3852
4346 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); 3853 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4347 fs_info->global_block_rsv.space_info = space_info; 3854 fs_info->global_block_rsv.space_info = space_info;
3855 fs_info->global_block_rsv.priority = 10;
3856 fs_info->global_block_rsv.refill_used = 1;
4348 fs_info->delalloc_block_rsv.space_info = space_info; 3857 fs_info->delalloc_block_rsv.space_info = space_info;
4349 fs_info->trans_block_rsv.space_info = space_info; 3858 fs_info->trans_block_rsv.space_info = space_info;
4350 fs_info->empty_block_rsv.space_info = space_info; 3859 fs_info->empty_block_rsv.space_info = space_info;
4351 fs_info->delayed_block_rsv.space_info = space_info; 3860 fs_info->empty_block_rsv.priority = 10;
4352 3861
4353 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; 3862 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4354 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; 3863 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
@@ -4356,39 +3865,67 @@ static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4356 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; 3865 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
4357 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; 3866 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
4358 3867
3868 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3869
3870 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3871
4359 update_global_block_rsv(fs_info); 3872 update_global_block_rsv(fs_info);
4360} 3873}
4361 3874
4362static void release_global_block_rsv(struct btrfs_fs_info *fs_info) 3875static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4363{ 3876{
4364 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, 3877 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
4365 (u64)-1);
4366 WARN_ON(fs_info->delalloc_block_rsv.size > 0); 3878 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4367 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); 3879 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4368 WARN_ON(fs_info->trans_block_rsv.size > 0); 3880 WARN_ON(fs_info->trans_block_rsv.size > 0);
4369 WARN_ON(fs_info->trans_block_rsv.reserved > 0); 3881 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4370 WARN_ON(fs_info->chunk_block_rsv.size > 0); 3882 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4371 WARN_ON(fs_info->chunk_block_rsv.reserved > 0); 3883 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
4372 WARN_ON(fs_info->delayed_block_rsv.size > 0); 3884}
4373 WARN_ON(fs_info->delayed_block_rsv.reserved > 0); 3885
3886int btrfs_truncate_reserve_metadata(struct btrfs_trans_handle *trans,
3887 struct btrfs_root *root,
3888 struct btrfs_block_rsv *rsv)
3889{
3890 struct btrfs_block_rsv *trans_rsv = &root->fs_info->trans_block_rsv;
3891 u64 num_bytes;
3892 int ret;
3893
3894 /*
3895 * Truncate should be freeing data, but give us 2 items just in case it
3896 * needs to use some space. We may want to be smarter about this in the
3897 * future.
3898 */
3899 num_bytes = btrfs_calc_trans_metadata_size(root, 2);
3900
3901 /* We already have enough bytes, just return */
3902 if (rsv->reserved >= num_bytes)
3903 return 0;
3904
3905 num_bytes -= rsv->reserved;
3906
3907 /*
3908 * You should have reserved enough space before hand to do this, so this
3909 * should not fail.
3910 */
3911 ret = block_rsv_migrate_bytes(trans_rsv, rsv, num_bytes);
3912 BUG_ON(ret);
3913
3914 return 0;
4374} 3915}
4375 3916
4376void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, 3917void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4377 struct btrfs_root *root) 3918 struct btrfs_root *root)
4378{ 3919{
4379 if (!trans->block_rsv)
4380 return;
4381
4382 if (!trans->bytes_reserved) 3920 if (!trans->bytes_reserved)
4383 return; 3921 return;
4384 3922
4385 trace_btrfs_space_reservation(root->fs_info, "transaction", 3923 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4386 trans->transid, trans->bytes_reserved, 0); 3924 btrfs_block_rsv_release(root, trans->block_rsv,
4387 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); 3925 trans->bytes_reserved);
4388 trans->bytes_reserved = 0; 3926 trans->bytes_reserved = 0;
4389} 3927}
4390 3928
4391/* Can only return 0 or -ENOSPC */
4392int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, 3929int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4393 struct inode *inode) 3930 struct inode *inode)
4394{ 3931{
@@ -4402,8 +3939,6 @@ int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4402 * when we are truly done with the orphan item. 3939 * when we are truly done with the orphan item.
4403 */ 3940 */
4404 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); 3941 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4405 trace_btrfs_space_reservation(root->fs_info, "orphan",
4406 btrfs_ino(inode), num_bytes, 1);
4407 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); 3942 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4408} 3943}
4409 3944
@@ -4411,8 +3946,6 @@ void btrfs_orphan_release_metadata(struct inode *inode)
4411{ 3946{
4412 struct btrfs_root *root = BTRFS_I(inode)->root; 3947 struct btrfs_root *root = BTRFS_I(inode)->root;
4413 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); 3948 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4414 trace_btrfs_space_reservation(root->fs_info, "orphan",
4415 btrfs_ino(inode), num_bytes, 0);
4416 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); 3949 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4417} 3950}
4418 3951
@@ -4423,106 +3956,41 @@ int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4423 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); 3956 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4424 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; 3957 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4425 /* 3958 /*
4426 * two for root back/forward refs, two for directory entries, 3959 * two for root back/forward refs, two for directory entries
4427 * one for root of the snapshot and one for parent inode. 3960 * and one for root of the snapshot.
4428 */ 3961 */
4429 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 6); 3962 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
4430 dst_rsv->space_info = src_rsv->space_info; 3963 dst_rsv->space_info = src_rsv->space_info;
4431 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); 3964 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4432} 3965}
4433 3966
4434/**
4435 * drop_outstanding_extent - drop an outstanding extent
4436 * @inode: the inode we're dropping the extent for
4437 *
4438 * This is called when we are freeing up an outstanding extent, either called
4439 * after an error or after an extent is written. This will return the number of
4440 * reserved extents that need to be freed. This must be called with
4441 * BTRFS_I(inode)->lock held.
4442 */
4443static unsigned drop_outstanding_extent(struct inode *inode) 3967static unsigned drop_outstanding_extent(struct inode *inode)
4444{ 3968{
4445 unsigned drop_inode_space = 0;
4446 unsigned dropped_extents = 0; 3969 unsigned dropped_extents = 0;
4447 3970
3971 spin_lock(&BTRFS_I(inode)->lock);
4448 BUG_ON(!BTRFS_I(inode)->outstanding_extents); 3972 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
4449 BTRFS_I(inode)->outstanding_extents--; 3973 BTRFS_I(inode)->outstanding_extents--;
4450 3974
4451 if (BTRFS_I(inode)->outstanding_extents == 0 &&
4452 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4453 &BTRFS_I(inode)->runtime_flags))
4454 drop_inode_space = 1;
4455
4456 /* 3975 /*
4457 * If we have more or the same amount of outsanding extents than we have 3976 * If we have more or the same amount of outsanding extents than we have
4458 * reserved then we need to leave the reserved extents count alone. 3977 * reserved then we need to leave the reserved extents count alone.
4459 */ 3978 */
4460 if (BTRFS_I(inode)->outstanding_extents >= 3979 if (BTRFS_I(inode)->outstanding_extents >=
4461 BTRFS_I(inode)->reserved_extents) 3980 BTRFS_I(inode)->reserved_extents)
4462 return drop_inode_space; 3981 goto out;
4463 3982
4464 dropped_extents = BTRFS_I(inode)->reserved_extents - 3983 dropped_extents = BTRFS_I(inode)->reserved_extents -
4465 BTRFS_I(inode)->outstanding_extents; 3984 BTRFS_I(inode)->outstanding_extents;
4466 BTRFS_I(inode)->reserved_extents -= dropped_extents; 3985 BTRFS_I(inode)->reserved_extents -= dropped_extents;
4467 return dropped_extents + drop_inode_space; 3986out:
3987 spin_unlock(&BTRFS_I(inode)->lock);
3988 return dropped_extents;
4468} 3989}
4469 3990
4470/** 3991static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4471 * calc_csum_metadata_size - return the amount of metada space that must be
4472 * reserved/free'd for the given bytes.
4473 * @inode: the inode we're manipulating
4474 * @num_bytes: the number of bytes in question
4475 * @reserve: 1 if we are reserving space, 0 if we are freeing space
4476 *
4477 * This adjusts the number of csum_bytes in the inode and then returns the
4478 * correct amount of metadata that must either be reserved or freed. We
4479 * calculate how many checksums we can fit into one leaf and then divide the
4480 * number of bytes that will need to be checksumed by this value to figure out
4481 * how many checksums will be required. If we are adding bytes then the number
4482 * may go up and we will return the number of additional bytes that must be
4483 * reserved. If it is going down we will return the number of bytes that must
4484 * be freed.
4485 *
4486 * This must be called with BTRFS_I(inode)->lock held.
4487 */
4488static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
4489 int reserve)
4490{ 3992{
4491 struct btrfs_root *root = BTRFS_I(inode)->root; 3993 return num_bytes >>= 3;
4492 u64 csum_size;
4493 int num_csums_per_leaf;
4494 int num_csums;
4495 int old_csums;
4496
4497 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
4498 BTRFS_I(inode)->csum_bytes == 0)
4499 return 0;
4500
4501 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4502 if (reserve)
4503 BTRFS_I(inode)->csum_bytes += num_bytes;
4504 else
4505 BTRFS_I(inode)->csum_bytes -= num_bytes;
4506 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
4507 num_csums_per_leaf = (int)div64_u64(csum_size,
4508 sizeof(struct btrfs_csum_item) +
4509 sizeof(struct btrfs_disk_key));
4510 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
4511 num_csums = num_csums + num_csums_per_leaf - 1;
4512 num_csums = num_csums / num_csums_per_leaf;
4513
4514 old_csums = old_csums + num_csums_per_leaf - 1;
4515 old_csums = old_csums / num_csums_per_leaf;
4516
4517 /* No change, no need to reserve more */
4518 if (old_csums == num_csums)
4519 return 0;
4520
4521 if (reserve)
4522 return btrfs_calc_trans_metadata_size(root,
4523 num_csums - old_csums);
4524
4525 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
4526} 3994}
4527 3995
4528int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) 3996int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
@@ -4530,136 +3998,45 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4530 struct btrfs_root *root = BTRFS_I(inode)->root; 3998 struct btrfs_root *root = BTRFS_I(inode)->root;
4531 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; 3999 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4532 u64 to_reserve = 0; 4000 u64 to_reserve = 0;
4533 u64 csum_bytes;
4534 unsigned nr_extents = 0; 4001 unsigned nr_extents = 0;
4535 int extra_reserve = 0;
4536 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
4537 int ret; 4002 int ret;
4538 bool delalloc_lock = true;
4539 4003
4540 /* If we are a free space inode we need to not flush since we will be in 4004 if (btrfs_transaction_in_commit(root->fs_info))
4541 * the middle of a transaction commit. We also don't need the delalloc
4542 * mutex since we won't race with anybody. We need this mostly to make
4543 * lockdep shut its filthy mouth.
4544 */
4545 if (btrfs_is_free_space_inode(inode)) {
4546 flush = BTRFS_RESERVE_NO_FLUSH;
4547 delalloc_lock = false;
4548 }
4549
4550 if (flush != BTRFS_RESERVE_NO_FLUSH &&
4551 btrfs_transaction_in_commit(root->fs_info))
4552 schedule_timeout(1); 4005 schedule_timeout(1);
4553 4006
4554 if (delalloc_lock)
4555 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
4556
4557 num_bytes = ALIGN(num_bytes, root->sectorsize); 4007 num_bytes = ALIGN(num_bytes, root->sectorsize);
4558 4008
4559 spin_lock(&BTRFS_I(inode)->lock); 4009 spin_lock(&BTRFS_I(inode)->lock);
4560 BTRFS_I(inode)->outstanding_extents++; 4010 BTRFS_I(inode)->outstanding_extents++;
4561 4011
4562 if (BTRFS_I(inode)->outstanding_extents > 4012 if (BTRFS_I(inode)->outstanding_extents >
4563 BTRFS_I(inode)->reserved_extents) 4013 BTRFS_I(inode)->reserved_extents) {
4564 nr_extents = BTRFS_I(inode)->outstanding_extents - 4014 nr_extents = BTRFS_I(inode)->outstanding_extents -
4565 BTRFS_I(inode)->reserved_extents; 4015 BTRFS_I(inode)->reserved_extents;
4016 BTRFS_I(inode)->reserved_extents += nr_extents;
4566 4017
4567 /* 4018 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
4568 * Add an item to reserve for updating the inode when we complete the
4569 * delalloc io.
4570 */
4571 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4572 &BTRFS_I(inode)->runtime_flags)) {
4573 nr_extents++;
4574 extra_reserve = 1;
4575 } 4019 }
4576
4577 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
4578 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
4579 csum_bytes = BTRFS_I(inode)->csum_bytes;
4580 spin_unlock(&BTRFS_I(inode)->lock); 4020 spin_unlock(&BTRFS_I(inode)->lock);
4581 4021
4582 if (root->fs_info->quota_enabled) { 4022 to_reserve += calc_csum_metadata_size(inode, num_bytes);
4583 ret = btrfs_qgroup_reserve(root, num_bytes + 4023 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4584 nr_extents * root->leafsize);
4585 if (ret) {
4586 spin_lock(&BTRFS_I(inode)->lock);
4587 calc_csum_metadata_size(inode, num_bytes, 0);
4588 spin_unlock(&BTRFS_I(inode)->lock);
4589 if (delalloc_lock)
4590 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4591 return ret;
4592 }
4593 }
4594
4595 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
4596 if (ret) { 4024 if (ret) {
4597 u64 to_free = 0;
4598 unsigned dropped; 4025 unsigned dropped;
4599
4600 spin_lock(&BTRFS_I(inode)->lock);
4601 dropped = drop_outstanding_extent(inode);
4602 /* 4026 /*
4603 * If the inodes csum_bytes is the same as the original 4027 * We don't need the return value since our reservation failed,
4604 * csum_bytes then we know we haven't raced with any free()ers 4028 * we just need to clean up our counter.
4605 * so we can just reduce our inodes csum bytes and carry on.
4606 * Otherwise we have to do the normal free thing to account for
4607 * the case that the free side didn't free up its reserve
4608 * because of this outstanding reservation.
4609 */ 4029 */
4610 if (BTRFS_I(inode)->csum_bytes == csum_bytes) 4030 dropped = drop_outstanding_extent(inode);
4611 calc_csum_metadata_size(inode, num_bytes, 0); 4031 WARN_ON(dropped > 1);
4612 else
4613 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
4614 spin_unlock(&BTRFS_I(inode)->lock);
4615 if (dropped)
4616 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4617
4618 if (to_free) {
4619 btrfs_block_rsv_release(root, block_rsv, to_free);
4620 trace_btrfs_space_reservation(root->fs_info,
4621 "delalloc",
4622 btrfs_ino(inode),
4623 to_free, 0);
4624 }
4625 if (root->fs_info->quota_enabled) {
4626 btrfs_qgroup_free(root, num_bytes +
4627 nr_extents * root->leafsize);
4628 }
4629 if (delalloc_lock)
4630 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4631 return ret; 4032 return ret;
4632 } 4033 }
4633 4034
4634 spin_lock(&BTRFS_I(inode)->lock);
4635 if (extra_reserve) {
4636 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
4637 &BTRFS_I(inode)->runtime_flags);
4638 nr_extents--;
4639 }
4640 BTRFS_I(inode)->reserved_extents += nr_extents;
4641 spin_unlock(&BTRFS_I(inode)->lock);
4642
4643 if (delalloc_lock)
4644 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
4645
4646 if (to_reserve)
4647 trace_btrfs_space_reservation(root->fs_info,"delalloc",
4648 btrfs_ino(inode), to_reserve, 1);
4649 block_rsv_add_bytes(block_rsv, to_reserve, 1); 4035 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4650 4036
4651 return 0; 4037 return 0;
4652} 4038}
4653 4039
4654/**
4655 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
4656 * @inode: the inode to release the reservation for
4657 * @num_bytes: the number of bytes we're releasing
4658 *
4659 * This will release the metadata reservation for an inode. This can be called
4660 * once we complete IO for a given set of bytes to release their metadata
4661 * reservations.
4662 */
4663void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) 4040void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4664{ 4041{
4665 struct btrfs_root *root = BTRFS_I(inode)->root; 4042 struct btrfs_root *root = BTRFS_I(inode)->root;
@@ -4667,40 +4044,16 @@ void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4667 unsigned dropped; 4044 unsigned dropped;
4668 4045
4669 num_bytes = ALIGN(num_bytes, root->sectorsize); 4046 num_bytes = ALIGN(num_bytes, root->sectorsize);
4670 spin_lock(&BTRFS_I(inode)->lock);
4671 dropped = drop_outstanding_extent(inode); 4047 dropped = drop_outstanding_extent(inode);
4672 4048
4673 to_free = calc_csum_metadata_size(inode, num_bytes, 0); 4049 to_free = calc_csum_metadata_size(inode, num_bytes);
4674 spin_unlock(&BTRFS_I(inode)->lock);
4675 if (dropped > 0) 4050 if (dropped > 0)
4676 to_free += btrfs_calc_trans_metadata_size(root, dropped); 4051 to_free += btrfs_calc_trans_metadata_size(root, dropped);
4677 4052
4678 trace_btrfs_space_reservation(root->fs_info, "delalloc",
4679 btrfs_ino(inode), to_free, 0);
4680 if (root->fs_info->quota_enabled) {
4681 btrfs_qgroup_free(root, num_bytes +
4682 dropped * root->leafsize);
4683 }
4684
4685 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, 4053 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4686 to_free); 4054 to_free);
4687} 4055}
4688 4056
4689/**
4690 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
4691 * @inode: inode we're writing to
4692 * @num_bytes: the number of bytes we want to allocate
4693 *
4694 * This will do the following things
4695 *
4696 * o reserve space in the data space info for num_bytes
4697 * o reserve space in the metadata space info based on number of outstanding
4698 * extents and how much csums will be needed
4699 * o add to the inodes ->delalloc_bytes
4700 * o add it to the fs_info's delalloc inodes list.
4701 *
4702 * This will return 0 for success and -ENOSPC if there is no space left.
4703 */
4704int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) 4057int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4705{ 4058{
4706 int ret; 4059 int ret;
@@ -4718,19 +4071,6 @@ int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4718 return 0; 4071 return 0;
4719} 4072}
4720 4073
4721/**
4722 * btrfs_delalloc_release_space - release data and metadata space for delalloc
4723 * @inode: inode we're releasing space for
4724 * @num_bytes: the number of bytes we want to free up
4725 *
4726 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
4727 * called in the case that we don't need the metadata AND data reservations
4728 * anymore. So if there is an error or we insert an inline extent.
4729 *
4730 * This function will release the metadata space that was not used and will
4731 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
4732 * list if there are no delalloc bytes left.
4733 */
4734void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) 4074void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4735{ 4075{
4736 btrfs_delalloc_release_metadata(inode, num_bytes); 4076 btrfs_delalloc_release_metadata(inode, num_bytes);
@@ -4750,18 +4090,18 @@ static int update_block_group(struct btrfs_trans_handle *trans,
4750 4090
4751 /* block accounting for super block */ 4091 /* block accounting for super block */
4752 spin_lock(&info->delalloc_lock); 4092 spin_lock(&info->delalloc_lock);
4753 old_val = btrfs_super_bytes_used(info->super_copy); 4093 old_val = btrfs_super_bytes_used(&info->super_copy);
4754 if (alloc) 4094 if (alloc)
4755 old_val += num_bytes; 4095 old_val += num_bytes;
4756 else 4096 else
4757 old_val -= num_bytes; 4097 old_val -= num_bytes;
4758 btrfs_set_super_bytes_used(info->super_copy, old_val); 4098 btrfs_set_super_bytes_used(&info->super_copy, old_val);
4759 spin_unlock(&info->delalloc_lock); 4099 spin_unlock(&info->delalloc_lock);
4760 4100
4761 while (total) { 4101 while (total) {
4762 cache = btrfs_lookup_block_group(info, bytenr); 4102 cache = btrfs_lookup_block_group(info, bytenr);
4763 if (!cache) 4103 if (!cache)
4764 return -ENOENT; 4104 return -1;
4765 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | 4105 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4766 BTRFS_BLOCK_GROUP_RAID1 | 4106 BTRFS_BLOCK_GROUP_RAID1 |
4767 BTRFS_BLOCK_GROUP_RAID10)) 4107 BTRFS_BLOCK_GROUP_RAID10))
@@ -4783,7 +4123,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
4783 spin_lock(&cache->space_info->lock); 4123 spin_lock(&cache->space_info->lock);
4784 spin_lock(&cache->lock); 4124 spin_lock(&cache->lock);
4785 4125
4786 if (btrfs_test_opt(root, SPACE_CACHE) && 4126 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4787 cache->disk_cache_state < BTRFS_DC_CLEAR) 4127 cache->disk_cache_state < BTRFS_DC_CLEAR)
4788 cache->disk_cache_state = BTRFS_DC_CLEAR; 4128 cache->disk_cache_state = BTRFS_DC_CLEAR;
4789 4129
@@ -4795,6 +4135,7 @@ static int update_block_group(struct btrfs_trans_handle *trans,
4795 btrfs_set_block_group_used(&cache->item, old_val); 4135 btrfs_set_block_group_used(&cache->item, old_val);
4796 cache->reserved -= num_bytes; 4136 cache->reserved -= num_bytes;
4797 cache->space_info->bytes_reserved -= num_bytes; 4137 cache->space_info->bytes_reserved -= num_bytes;
4138 cache->space_info->reservation_progress++;
4798 cache->space_info->bytes_used += num_bytes; 4139 cache->space_info->bytes_used += num_bytes;
4799 cache->space_info->disk_used += num_bytes * factor; 4140 cache->space_info->disk_used += num_bytes * factor;
4800 spin_unlock(&cache->lock); 4141 spin_unlock(&cache->lock);
@@ -4846,6 +4187,7 @@ static int pin_down_extent(struct btrfs_root *root,
4846 if (reserved) { 4187 if (reserved) {
4847 cache->reserved -= num_bytes; 4188 cache->reserved -= num_bytes;
4848 cache->space_info->bytes_reserved -= num_bytes; 4189 cache->space_info->bytes_reserved -= num_bytes;
4190 cache->space_info->reservation_progress++;
4849 } 4191 }
4850 spin_unlock(&cache->lock); 4192 spin_unlock(&cache->lock);
4851 spin_unlock(&cache->space_info->lock); 4193 spin_unlock(&cache->space_info->lock);
@@ -4864,7 +4206,7 @@ int btrfs_pin_extent(struct btrfs_root *root,
4864 struct btrfs_block_group_cache *cache; 4206 struct btrfs_block_group_cache *cache;
4865 4207
4866 cache = btrfs_lookup_block_group(root->fs_info, bytenr); 4208 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4867 BUG_ON(!cache); /* Logic error */ 4209 BUG_ON(!cache);
4868 4210
4869 pin_down_extent(root, cache, bytenr, num_bytes, reserved); 4211 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4870 4212
@@ -4873,89 +4215,49 @@ int btrfs_pin_extent(struct btrfs_root *root,
4873} 4215}
4874 4216
4875/* 4217/*
4876 * this function must be called within transaction 4218 * update size of reserved extents. this function may return -EAGAIN
4877 */ 4219 * if 'reserve' is true or 'sinfo' is false.
4878int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
4879 struct btrfs_root *root,
4880 u64 bytenr, u64 num_bytes)
4881{
4882 struct btrfs_block_group_cache *cache;
4883
4884 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4885 BUG_ON(!cache); /* Logic error */
4886
4887 /*
4888 * pull in the free space cache (if any) so that our pin
4889 * removes the free space from the cache. We have load_only set
4890 * to one because the slow code to read in the free extents does check
4891 * the pinned extents.
4892 */
4893 cache_block_group(cache, trans, root, 1);
4894
4895 pin_down_extent(root, cache, bytenr, num_bytes, 0);
4896
4897 /* remove us from the free space cache (if we're there at all) */
4898 btrfs_remove_free_space(cache, bytenr, num_bytes);
4899 btrfs_put_block_group(cache);
4900 return 0;
4901}
4902
4903/**
4904 * btrfs_update_reserved_bytes - update the block_group and space info counters
4905 * @cache: The cache we are manipulating
4906 * @num_bytes: The number of bytes in question
4907 * @reserve: One of the reservation enums
4908 *
4909 * This is called by the allocator when it reserves space, or by somebody who is
4910 * freeing space that was never actually used on disk. For example if you
4911 * reserve some space for a new leaf in transaction A and before transaction A
4912 * commits you free that leaf, you call this with reserve set to 0 in order to
4913 * clear the reservation.
4914 *
4915 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
4916 * ENOSPC accounting. For data we handle the reservation through clearing the
4917 * delalloc bits in the io_tree. We have to do this since we could end up
4918 * allocating less disk space for the amount of data we have reserved in the
4919 * case of compression.
4920 *
4921 * If this is a reservation and the block group has become read only we cannot
4922 * make the reservation and return -EAGAIN, otherwise this function always
4923 * succeeds.
4924 */ 4220 */
4925static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 4221int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4926 u64 num_bytes, int reserve) 4222 u64 num_bytes, int reserve, int sinfo)
4927{ 4223{
4928 struct btrfs_space_info *space_info = cache->space_info;
4929 int ret = 0; 4224 int ret = 0;
4930 4225 if (sinfo) {
4931 spin_lock(&space_info->lock); 4226 struct btrfs_space_info *space_info = cache->space_info;
4932 spin_lock(&cache->lock); 4227 spin_lock(&space_info->lock);
4933 if (reserve != RESERVE_FREE) { 4228 spin_lock(&cache->lock);
4229 if (reserve) {
4230 if (cache->ro) {
4231 ret = -EAGAIN;
4232 } else {
4233 cache->reserved += num_bytes;
4234 space_info->bytes_reserved += num_bytes;
4235 }
4236 } else {
4237 if (cache->ro)
4238 space_info->bytes_readonly += num_bytes;
4239 cache->reserved -= num_bytes;
4240 space_info->bytes_reserved -= num_bytes;
4241 space_info->reservation_progress++;
4242 }
4243 spin_unlock(&cache->lock);
4244 spin_unlock(&space_info->lock);
4245 } else {
4246 spin_lock(&cache->lock);
4934 if (cache->ro) { 4247 if (cache->ro) {
4935 ret = -EAGAIN; 4248 ret = -EAGAIN;
4936 } else { 4249 } else {
4937 cache->reserved += num_bytes; 4250 if (reserve)
4938 space_info->bytes_reserved += num_bytes; 4251 cache->reserved += num_bytes;
4939 if (reserve == RESERVE_ALLOC) { 4252 else
4940 trace_btrfs_space_reservation(cache->fs_info, 4253 cache->reserved -= num_bytes;
4941 "space_info", space_info->flags,
4942 num_bytes, 0);
4943 space_info->bytes_may_use -= num_bytes;
4944 }
4945 } 4254 }
4946 } else { 4255 spin_unlock(&cache->lock);
4947 if (cache->ro)
4948 space_info->bytes_readonly += num_bytes;
4949 cache->reserved -= num_bytes;
4950 space_info->bytes_reserved -= num_bytes;
4951 space_info->reservation_progress++;
4952 } 4256 }
4953 spin_unlock(&cache->lock);
4954 spin_unlock(&space_info->lock);
4955 return ret; 4257 return ret;
4956} 4258}
4957 4259
4958void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 4260int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4959 struct btrfs_root *root) 4261 struct btrfs_root *root)
4960{ 4262{
4961 struct btrfs_fs_info *fs_info = root->fs_info; 4263 struct btrfs_fs_info *fs_info = root->fs_info;
@@ -4985,25 +4287,22 @@ void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4985 up_write(&fs_info->extent_commit_sem); 4287 up_write(&fs_info->extent_commit_sem);
4986 4288
4987 update_global_block_rsv(fs_info); 4289 update_global_block_rsv(fs_info);
4290 return 0;
4988} 4291}
4989 4292
4990static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) 4293static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4991{ 4294{
4992 struct btrfs_fs_info *fs_info = root->fs_info; 4295 struct btrfs_fs_info *fs_info = root->fs_info;
4993 struct btrfs_block_group_cache *cache = NULL; 4296 struct btrfs_block_group_cache *cache = NULL;
4994 struct btrfs_space_info *space_info;
4995 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4996 u64 len; 4297 u64 len;
4997 bool readonly;
4998 4298
4999 while (start <= end) { 4299 while (start <= end) {
5000 readonly = false;
5001 if (!cache || 4300 if (!cache ||
5002 start >= cache->key.objectid + cache->key.offset) { 4301 start >= cache->key.objectid + cache->key.offset) {
5003 if (cache) 4302 if (cache)
5004 btrfs_put_block_group(cache); 4303 btrfs_put_block_group(cache);
5005 cache = btrfs_lookup_block_group(fs_info, start); 4304 cache = btrfs_lookup_block_group(fs_info, start);
5006 BUG_ON(!cache); /* Logic error */ 4305 BUG_ON(!cache);
5007 } 4306 }
5008 4307
5009 len = cache->key.objectid + cache->key.offset - start; 4308 len = cache->key.objectid + cache->key.offset - start;
@@ -5015,30 +4314,20 @@ static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5015 } 4314 }
5016 4315
5017 start += len; 4316 start += len;
5018 space_info = cache->space_info;
5019 4317
5020 spin_lock(&space_info->lock); 4318 spin_lock(&cache->space_info->lock);
5021 spin_lock(&cache->lock); 4319 spin_lock(&cache->lock);
5022 cache->pinned -= len; 4320 cache->pinned -= len;
5023 space_info->bytes_pinned -= len; 4321 cache->space_info->bytes_pinned -= len;
5024 if (cache->ro) { 4322 if (cache->ro) {
5025 space_info->bytes_readonly += len; 4323 cache->space_info->bytes_readonly += len;
5026 readonly = true; 4324 } else if (cache->reserved_pinned > 0) {
4325 len = min(len, cache->reserved_pinned);
4326 cache->reserved_pinned -= len;
4327 cache->space_info->bytes_reserved += len;
5027 } 4328 }
5028 spin_unlock(&cache->lock); 4329 spin_unlock(&cache->lock);
5029 if (!readonly && global_rsv->space_info == space_info) { 4330 spin_unlock(&cache->space_info->lock);
5030 spin_lock(&global_rsv->lock);
5031 if (!global_rsv->full) {
5032 len = min(len, global_rsv->size -
5033 global_rsv->reserved);
5034 global_rsv->reserved += len;
5035 space_info->bytes_may_use += len;
5036 if (global_rsv->reserved >= global_rsv->size)
5037 global_rsv->full = 1;
5038 }
5039 spin_unlock(&global_rsv->lock);
5040 }
5041 spin_unlock(&space_info->lock);
5042 } 4331 }
5043 4332
5044 if (cache) 4333 if (cache)
@@ -5051,13 +4340,13 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
5051{ 4340{
5052 struct btrfs_fs_info *fs_info = root->fs_info; 4341 struct btrfs_fs_info *fs_info = root->fs_info;
5053 struct extent_io_tree *unpin; 4342 struct extent_io_tree *unpin;
4343 struct btrfs_block_rsv *block_rsv;
4344 struct btrfs_block_rsv *next_rsv;
5054 u64 start; 4345 u64 start;
5055 u64 end; 4346 u64 end;
4347 int idx;
5056 int ret; 4348 int ret;
5057 4349
5058 if (trans->aborted)
5059 return 0;
5060
5061 if (fs_info->pinned_extents == &fs_info->freed_extents[0]) 4350 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5062 unpin = &fs_info->freed_extents[1]; 4351 unpin = &fs_info->freed_extents[1];
5063 else 4352 else
@@ -5065,7 +4354,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
5065 4354
5066 while (1) { 4355 while (1) {
5067 ret = find_first_extent_bit(unpin, 0, &start, &end, 4356 ret = find_first_extent_bit(unpin, 0, &start, &end,
5068 EXTENT_DIRTY, NULL); 4357 EXTENT_DIRTY);
5069 if (ret) 4358 if (ret)
5070 break; 4359 break;
5071 4360
@@ -5078,6 +4367,30 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
5078 cond_resched(); 4367 cond_resched();
5079 } 4368 }
5080 4369
4370 mutex_lock(&fs_info->durable_block_rsv_mutex);
4371 list_for_each_entry_safe(block_rsv, next_rsv,
4372 &fs_info->durable_block_rsv_list, list) {
4373
4374 idx = trans->transid & 0x1;
4375 if (block_rsv->freed[idx] > 0) {
4376 block_rsv_add_bytes(block_rsv,
4377 block_rsv->freed[idx], 0);
4378 block_rsv->freed[idx] = 0;
4379 }
4380 if (atomic_read(&block_rsv->usage) == 0) {
4381 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4382
4383 if (block_rsv->freed[0] == 0 &&
4384 block_rsv->freed[1] == 0) {
4385 list_del_init(&block_rsv->list);
4386 kfree(block_rsv);
4387 }
4388 } else {
4389 btrfs_block_rsv_release(root, block_rsv, 0);
4390 }
4391 }
4392 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4393
5081 return 0; 4394 return 0;
5082} 4395}
5083 4396
@@ -5143,10 +4456,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5143 ret = remove_extent_backref(trans, extent_root, path, 4456 ret = remove_extent_backref(trans, extent_root, path,
5144 NULL, refs_to_drop, 4457 NULL, refs_to_drop,
5145 is_data); 4458 is_data);
5146 if (ret) { 4459 BUG_ON(ret);
5147 btrfs_abort_transaction(trans, extent_root, ret);
5148 goto out;
5149 }
5150 btrfs_release_path(path); 4460 btrfs_release_path(path);
5151 path->leave_spinning = 1; 4461 path->leave_spinning = 1;
5152 4462
@@ -5164,13 +4474,10 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5164 btrfs_print_leaf(extent_root, 4474 btrfs_print_leaf(extent_root,
5165 path->nodes[0]); 4475 path->nodes[0]);
5166 } 4476 }
5167 if (ret < 0) { 4477 BUG_ON(ret);
5168 btrfs_abort_transaction(trans, extent_root, ret);
5169 goto out;
5170 }
5171 extent_slot = path->slots[0]; 4478 extent_slot = path->slots[0];
5172 } 4479 }
5173 } else if (ret == -ENOENT) { 4480 } else {
5174 btrfs_print_leaf(extent_root, path->nodes[0]); 4481 btrfs_print_leaf(extent_root, path->nodes[0]);
5175 WARN_ON(1); 4482 WARN_ON(1);
5176 printk(KERN_ERR "btrfs unable to find ref byte nr %llu " 4483 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
@@ -5180,9 +4487,6 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5180 (unsigned long long)root_objectid, 4487 (unsigned long long)root_objectid,
5181 (unsigned long long)owner_objectid, 4488 (unsigned long long)owner_objectid,
5182 (unsigned long long)owner_offset); 4489 (unsigned long long)owner_offset);
5183 } else {
5184 btrfs_abort_transaction(trans, extent_root, ret);
5185 goto out;
5186 } 4490 }
5187 4491
5188 leaf = path->nodes[0]; 4492 leaf = path->nodes[0];
@@ -5192,10 +4496,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5192 BUG_ON(found_extent || extent_slot != path->slots[0]); 4496 BUG_ON(found_extent || extent_slot != path->slots[0]);
5193 ret = convert_extent_item_v0(trans, extent_root, path, 4497 ret = convert_extent_item_v0(trans, extent_root, path,
5194 owner_objectid, 0); 4498 owner_objectid, 0);
5195 if (ret < 0) { 4499 BUG_ON(ret < 0);
5196 btrfs_abort_transaction(trans, extent_root, ret);
5197 goto out;
5198 }
5199 4500
5200 btrfs_release_path(path); 4501 btrfs_release_path(path);
5201 path->leave_spinning = 1; 4502 path->leave_spinning = 1;
@@ -5212,11 +4513,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5212 (unsigned long long)bytenr); 4513 (unsigned long long)bytenr);
5213 btrfs_print_leaf(extent_root, path->nodes[0]); 4514 btrfs_print_leaf(extent_root, path->nodes[0]);
5214 } 4515 }
5215 if (ret < 0) { 4516 BUG_ON(ret);
5216 btrfs_abort_transaction(trans, extent_root, ret);
5217 goto out;
5218 }
5219
5220 extent_slot = path->slots[0]; 4517 extent_slot = path->slots[0];
5221 leaf = path->nodes[0]; 4518 leaf = path->nodes[0];
5222 item_size = btrfs_item_size_nr(leaf, extent_slot); 4519 item_size = btrfs_item_size_nr(leaf, extent_slot);
@@ -5253,10 +4550,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5253 ret = remove_extent_backref(trans, extent_root, path, 4550 ret = remove_extent_backref(trans, extent_root, path,
5254 iref, refs_to_drop, 4551 iref, refs_to_drop,
5255 is_data); 4552 is_data);
5256 if (ret) { 4553 BUG_ON(ret);
5257 btrfs_abort_transaction(trans, extent_root, ret);
5258 goto out;
5259 }
5260 } 4554 }
5261 } else { 4555 } else {
5262 if (found_extent) { 4556 if (found_extent) {
@@ -5273,27 +4567,21 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5273 4567
5274 ret = btrfs_del_items(trans, extent_root, path, path->slots[0], 4568 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
5275 num_to_del); 4569 num_to_del);
5276 if (ret) { 4570 BUG_ON(ret);
5277 btrfs_abort_transaction(trans, extent_root, ret);
5278 goto out;
5279 }
5280 btrfs_release_path(path); 4571 btrfs_release_path(path);
5281 4572
5282 if (is_data) { 4573 if (is_data) {
5283 ret = btrfs_del_csums(trans, root, bytenr, num_bytes); 4574 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
5284 if (ret) { 4575 BUG_ON(ret);
5285 btrfs_abort_transaction(trans, extent_root, ret); 4576 } else {
5286 goto out; 4577 invalidate_mapping_pages(info->btree_inode->i_mapping,
5287 } 4578 bytenr >> PAGE_CACHE_SHIFT,
4579 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
5288 } 4580 }
5289 4581
5290 ret = update_block_group(trans, root, bytenr, num_bytes, 0); 4582 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
5291 if (ret) { 4583 BUG_ON(ret);
5292 btrfs_abort_transaction(trans, extent_root, ret);
5293 goto out;
5294 }
5295 } 4584 }
5296out:
5297 btrfs_free_path(path); 4585 btrfs_free_path(path);
5298 return ret; 4586 return ret;
5299} 4587}
@@ -5380,39 +4668,79 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
5380 struct extent_buffer *buf, 4668 struct extent_buffer *buf,
5381 u64 parent, int last_ref) 4669 u64 parent, int last_ref)
5382{ 4670{
4671 struct btrfs_block_rsv *block_rsv;
5383 struct btrfs_block_group_cache *cache = NULL; 4672 struct btrfs_block_group_cache *cache = NULL;
5384 int ret; 4673 int ret;
5385 4674
5386 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 4675 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5387 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, 4676 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
5388 buf->start, buf->len, 4677 parent, root->root_key.objectid,
5389 parent, root->root_key.objectid, 4678 btrfs_header_level(buf),
5390 btrfs_header_level(buf), 4679 BTRFS_DROP_DELAYED_REF, NULL);
5391 BTRFS_DROP_DELAYED_REF, NULL, 0); 4680 BUG_ON(ret);
5392 BUG_ON(ret); /* -ENOMEM */
5393 } 4681 }
5394 4682
5395 if (!last_ref) 4683 if (!last_ref)
5396 return; 4684 return;
5397 4685
4686 block_rsv = get_block_rsv(trans, root);
5398 cache = btrfs_lookup_block_group(root->fs_info, buf->start); 4687 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4688 if (block_rsv->space_info != cache->space_info)
4689 goto out;
5399 4690
5400 if (btrfs_header_generation(buf) == trans->transid) { 4691 if (btrfs_header_generation(buf) == trans->transid) {
5401 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { 4692 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
5402 ret = check_ref_cleanup(trans, root, buf->start); 4693 ret = check_ref_cleanup(trans, root, buf->start);
5403 if (!ret) 4694 if (!ret)
5404 goto out; 4695 goto pin;
5405 } 4696 }
5406 4697
5407 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { 4698 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
5408 pin_down_extent(root, cache, buf->start, buf->len, 1); 4699 pin_down_extent(root, cache, buf->start, buf->len, 1);
5409 goto out; 4700 goto pin;
5410 } 4701 }
5411 4702
5412 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 4703 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
5413 4704
5414 btrfs_add_free_space(cache, buf->start, buf->len); 4705 btrfs_add_free_space(cache, buf->start, buf->len);
5415 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); 4706 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
4707 if (ret == -EAGAIN) {
4708 /* block group became read-only */
4709 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
4710 goto out;
4711 }
4712
4713 ret = 1;
4714 spin_lock(&block_rsv->lock);
4715 if (block_rsv->reserved < block_rsv->size) {
4716 block_rsv->reserved += buf->len;
4717 ret = 0;
4718 }
4719 spin_unlock(&block_rsv->lock);
4720
4721 if (ret) {
4722 spin_lock(&cache->space_info->lock);
4723 cache->space_info->bytes_reserved -= buf->len;
4724 cache->space_info->reservation_progress++;
4725 spin_unlock(&cache->space_info->lock);
4726 }
4727 goto out;
4728 }
4729pin:
4730 if (block_rsv->durable && !cache->ro) {
4731 ret = 0;
4732 spin_lock(&cache->lock);
4733 if (!cache->ro) {
4734 cache->reserved_pinned += buf->len;
4735 ret = 1;
4736 }
4737 spin_unlock(&cache->lock);
4738
4739 if (ret) {
4740 spin_lock(&block_rsv->lock);
4741 block_rsv->freed[trans->transid & 0x1] += buf->len;
4742 spin_unlock(&block_rsv->lock);
4743 }
5416 } 4744 }
5417out: 4745out:
5418 /* 4746 /*
@@ -5423,13 +4751,12 @@ out:
5423 btrfs_put_block_group(cache); 4751 btrfs_put_block_group(cache);
5424} 4752}
5425 4753
5426/* Can return -ENOMEM */ 4754int btrfs_free_extent(struct btrfs_trans_handle *trans,
5427int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, 4755 struct btrfs_root *root,
5428 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, 4756 u64 bytenr, u64 num_bytes, u64 parent,
5429 u64 owner, u64 offset, int for_cow) 4757 u64 root_objectid, u64 owner, u64 offset)
5430{ 4758{
5431 int ret; 4759 int ret;
5432 struct btrfs_fs_info *fs_info = root->fs_info;
5433 4760
5434 /* 4761 /*
5435 * tree log blocks never actually go into the extent allocation 4762 * tree log blocks never actually go into the extent allocation
@@ -5441,16 +4768,15 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
5441 btrfs_pin_extent(root, bytenr, num_bytes, 1); 4768 btrfs_pin_extent(root, bytenr, num_bytes, 1);
5442 ret = 0; 4769 ret = 0;
5443 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { 4770 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5444 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, 4771 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
5445 num_bytes,
5446 parent, root_objectid, (int)owner, 4772 parent, root_objectid, (int)owner,
5447 BTRFS_DROP_DELAYED_REF, NULL, for_cow); 4773 BTRFS_DROP_DELAYED_REF, NULL);
4774 BUG_ON(ret);
5448 } else { 4775 } else {
5449 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, 4776 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
5450 num_bytes, 4777 parent, root_objectid, owner,
5451 parent, root_objectid, owner, 4778 offset, BTRFS_DROP_DELAYED_REF, NULL);
5452 offset, BTRFS_DROP_DELAYED_REF, 4779 BUG_ON(ret);
5453 NULL, for_cow);
5454 } 4780 }
5455 return ret; 4781 return ret;
5456} 4782}
@@ -5507,34 +4833,28 @@ wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
5507 return 0; 4833 return 0;
5508} 4834}
5509 4835
5510int __get_raid_index(u64 flags) 4836static int get_block_group_index(struct btrfs_block_group_cache *cache)
5511{ 4837{
5512 int index; 4838 int index;
5513 4839 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
5514 if (flags & BTRFS_BLOCK_GROUP_RAID10)
5515 index = 0; 4840 index = 0;
5516 else if (flags & BTRFS_BLOCK_GROUP_RAID1) 4841 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
5517 index = 1; 4842 index = 1;
5518 else if (flags & BTRFS_BLOCK_GROUP_DUP) 4843 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
5519 index = 2; 4844 index = 2;
5520 else if (flags & BTRFS_BLOCK_GROUP_RAID0) 4845 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
5521 index = 3; 4846 index = 3;
5522 else 4847 else
5523 index = 4; 4848 index = 4;
5524
5525 return index; 4849 return index;
5526} 4850}
5527 4851
5528static int get_block_group_index(struct btrfs_block_group_cache *cache)
5529{
5530 return __get_raid_index(cache->flags);
5531}
5532
5533enum btrfs_loop_type { 4852enum btrfs_loop_type {
5534 LOOP_CACHING_NOWAIT = 0, 4853 LOOP_FIND_IDEAL = 0,
5535 LOOP_CACHING_WAIT = 1, 4854 LOOP_CACHING_NOWAIT = 1,
5536 LOOP_ALLOC_CHUNK = 2, 4855 LOOP_CACHING_WAIT = 2,
5537 LOOP_NO_EMPTY_SIZE = 3, 4856 LOOP_ALLOC_CHUNK = 3,
4857 LOOP_NO_EMPTY_SIZE = 4,
5538}; 4858};
5539 4859
5540/* 4860/*
@@ -5548,6 +4868,7 @@ enum btrfs_loop_type {
5548static noinline int find_free_extent(struct btrfs_trans_handle *trans, 4868static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5549 struct btrfs_root *orig_root, 4869 struct btrfs_root *orig_root,
5550 u64 num_bytes, u64 empty_size, 4870 u64 num_bytes, u64 empty_size,
4871 u64 search_start, u64 search_end,
5551 u64 hint_byte, struct btrfs_key *ins, 4872 u64 hint_byte, struct btrfs_key *ins,
5552 u64 data) 4873 u64 data)
5553{ 4874{
@@ -5555,27 +4876,25 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5555 struct btrfs_root *root = orig_root->fs_info->extent_root; 4876 struct btrfs_root *root = orig_root->fs_info->extent_root;
5556 struct btrfs_free_cluster *last_ptr = NULL; 4877 struct btrfs_free_cluster *last_ptr = NULL;
5557 struct btrfs_block_group_cache *block_group = NULL; 4878 struct btrfs_block_group_cache *block_group = NULL;
5558 struct btrfs_block_group_cache *used_block_group;
5559 u64 search_start = 0;
5560 int empty_cluster = 2 * 1024 * 1024; 4879 int empty_cluster = 2 * 1024 * 1024;
4880 int allowed_chunk_alloc = 0;
4881 int done_chunk_alloc = 0;
5561 struct btrfs_space_info *space_info; 4882 struct btrfs_space_info *space_info;
4883 int last_ptr_loop = 0;
5562 int loop = 0; 4884 int loop = 0;
5563 int index = 0; 4885 int index = 0;
5564 int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ?
5565 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
5566 bool found_uncached_bg = false; 4886 bool found_uncached_bg = false;
5567 bool failed_cluster_refill = false; 4887 bool failed_cluster_refill = false;
5568 bool failed_alloc = false; 4888 bool failed_alloc = false;
5569 bool use_cluster = true; 4889 bool use_cluster = true;
5570 bool have_caching_bg = false; 4890 u64 ideal_cache_percent = 0;
4891 u64 ideal_cache_offset = 0;
5571 4892
5572 WARN_ON(num_bytes < root->sectorsize); 4893 WARN_ON(num_bytes < root->sectorsize);
5573 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); 4894 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
5574 ins->objectid = 0; 4895 ins->objectid = 0;
5575 ins->offset = 0; 4896 ins->offset = 0;
5576 4897
5577 trace_find_free_extent(orig_root, num_bytes, empty_size, data);
5578
5579 space_info = __find_space_info(root->fs_info, data); 4898 space_info = __find_space_info(root->fs_info, data);
5580 if (!space_info) { 4899 if (!space_info) {
5581 printk(KERN_ERR "No space info for %llu\n", data); 4900 printk(KERN_ERR "No space info for %llu\n", data);
@@ -5589,6 +4908,9 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5589 if (btrfs_mixed_space_info(space_info)) 4908 if (btrfs_mixed_space_info(space_info))
5590 use_cluster = false; 4909 use_cluster = false;
5591 4910
4911 if (orig_root->ref_cows || empty_size)
4912 allowed_chunk_alloc = 1;
4913
5592 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { 4914 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
5593 last_ptr = &root->fs_info->meta_alloc_cluster; 4915 last_ptr = &root->fs_info->meta_alloc_cluster;
5594 if (!btrfs_test_opt(root, SSD)) 4916 if (!btrfs_test_opt(root, SSD))
@@ -5614,9 +4936,9 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5614 empty_cluster = 0; 4936 empty_cluster = 0;
5615 4937
5616 if (search_start == hint_byte) { 4938 if (search_start == hint_byte) {
4939ideal_cache:
5617 block_group = btrfs_lookup_block_group(root->fs_info, 4940 block_group = btrfs_lookup_block_group(root->fs_info,
5618 search_start); 4941 search_start);
5619 used_block_group = block_group;
5620 /* 4942 /*
5621 * we don't want to use the block group if it doesn't match our 4943 * we don't want to use the block group if it doesn't match our
5622 * allocation bits, or if its not cached. 4944 * allocation bits, or if its not cached.
@@ -5625,7 +4947,8 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5625 * picked out then we don't care that the block group is cached. 4947 * picked out then we don't care that the block group is cached.
5626 */ 4948 */
5627 if (block_group && block_group_bits(block_group, data) && 4949 if (block_group && block_group_bits(block_group, data) &&
5628 block_group->cached != BTRFS_CACHE_NO) { 4950 (block_group->cached != BTRFS_CACHE_NO ||
4951 search_start == ideal_cache_offset)) {
5629 down_read(&space_info->groups_sem); 4952 down_read(&space_info->groups_sem);
5630 if (list_empty(&block_group->list) || 4953 if (list_empty(&block_group->list) ||
5631 block_group->ro) { 4954 block_group->ro) {
@@ -5646,14 +4969,12 @@ static noinline int find_free_extent(struct btrfs_trans_handle *trans,
5646 } 4969 }
5647 } 4970 }
5648search: 4971search:
5649 have_caching_bg = false;
5650 down_read(&space_info->groups_sem); 4972 down_read(&space_info->groups_sem);
5651 list_for_each_entry(block_group, &space_info->block_groups[index], 4973 list_for_each_entry(block_group, &space_info->block_groups[index],
5652 list) { 4974 list) {
5653 u64 offset; 4975 u64 offset;
5654 int cached; 4976 int cached;
5655 4977
5656 used_block_group = block_group;
5657 btrfs_get_block_group(block_group); 4978 btrfs_get_block_group(block_group);
5658 search_start = block_group->key.objectid; 4979 search_start = block_group->key.objectid;
5659 4980
@@ -5677,93 +4998,132 @@ search:
5677 } 4998 }
5678 4999
5679have_block_group: 5000have_block_group:
5680 cached = block_group_cache_done(block_group); 5001 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
5681 if (unlikely(!cached)) { 5002 u64 free_percent;
5682 found_uncached_bg = true; 5003
5683 ret = cache_block_group(block_group, trans, 5004 ret = cache_block_group(block_group, trans,
5684 orig_root, 0); 5005 orig_root, 1);
5685 BUG_ON(ret < 0); 5006 if (block_group->cached == BTRFS_CACHE_FINISHED)
5686 ret = 0; 5007 goto have_block_group;
5008
5009 free_percent = btrfs_block_group_used(&block_group->item);
5010 free_percent *= 100;
5011 free_percent = div64_u64(free_percent,
5012 block_group->key.offset);
5013 free_percent = 100 - free_percent;
5014 if (free_percent > ideal_cache_percent &&
5015 likely(!block_group->ro)) {
5016 ideal_cache_offset = block_group->key.objectid;
5017 ideal_cache_percent = free_percent;
5018 }
5019
5020 /*
5021 * The caching workers are limited to 2 threads, so we
5022 * can queue as much work as we care to.
5023 */
5024 if (loop > LOOP_FIND_IDEAL) {
5025 ret = cache_block_group(block_group, trans,
5026 orig_root, 0);
5027 BUG_ON(ret);
5028 }
5029 found_uncached_bg = true;
5030
5031 /*
5032 * If loop is set for cached only, try the next block
5033 * group.
5034 */
5035 if (loop == LOOP_FIND_IDEAL)
5036 goto loop;
5687 } 5037 }
5688 5038
5039 cached = block_group_cache_done(block_group);
5040 if (unlikely(!cached))
5041 found_uncached_bg = true;
5042
5689 if (unlikely(block_group->ro)) 5043 if (unlikely(block_group->ro))
5690 goto loop; 5044 goto loop;
5691 5045
5046 spin_lock(&block_group->free_space_ctl->tree_lock);
5047 if (cached &&
5048 block_group->free_space_ctl->free_space <
5049 num_bytes + empty_size) {
5050 spin_unlock(&block_group->free_space_ctl->tree_lock);
5051 goto loop;
5052 }
5053 spin_unlock(&block_group->free_space_ctl->tree_lock);
5054
5692 /* 5055 /*
5693 * Ok we want to try and use the cluster allocator, so 5056 * Ok we want to try and use the cluster allocator, so lets look
5694 * lets look there 5057 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5058 * have tried the cluster allocator plenty of times at this
5059 * point and not have found anything, so we are likely way too
5060 * fragmented for the clustering stuff to find anything, so lets
5061 * just skip it and let the allocator find whatever block it can
5062 * find
5695 */ 5063 */
5696 if (last_ptr) { 5064 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
5697 /* 5065 /*
5698 * the refill lock keeps out other 5066 * the refill lock keeps out other
5699 * people trying to start a new cluster 5067 * people trying to start a new cluster
5700 */ 5068 */
5701 spin_lock(&last_ptr->refill_lock); 5069 spin_lock(&last_ptr->refill_lock);
5702 used_block_group = last_ptr->block_group; 5070 if (last_ptr->block_group &&
5703 if (used_block_group != block_group && 5071 (last_ptr->block_group->ro ||
5704 (!used_block_group || 5072 !block_group_bits(last_ptr->block_group, data))) {
5705 used_block_group->ro || 5073 offset = 0;
5706 !block_group_bits(used_block_group, data))) {
5707 used_block_group = block_group;
5708 goto refill_cluster; 5074 goto refill_cluster;
5709 } 5075 }
5710 5076
5711 if (used_block_group != block_group) 5077 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5712 btrfs_get_block_group(used_block_group); 5078 num_bytes, search_start);
5713
5714 offset = btrfs_alloc_from_cluster(used_block_group,
5715 last_ptr, num_bytes, used_block_group->key.objectid);
5716 if (offset) { 5079 if (offset) {
5717 /* we have a block, we're done */ 5080 /* we have a block, we're done */
5718 spin_unlock(&last_ptr->refill_lock); 5081 spin_unlock(&last_ptr->refill_lock);
5719 trace_btrfs_reserve_extent_cluster(root,
5720 block_group, search_start, num_bytes);
5721 goto checks; 5082 goto checks;
5722 } 5083 }
5723 5084
5724 WARN_ON(last_ptr->block_group != used_block_group); 5085 spin_lock(&last_ptr->lock);
5725 if (used_block_group != block_group) { 5086 /*
5726 btrfs_put_block_group(used_block_group); 5087 * whoops, this cluster doesn't actually point to
5727 used_block_group = block_group; 5088 * this block group. Get a ref on the block
5728 } 5089 * group is does point to and try again
5729refill_cluster: 5090 */
5730 BUG_ON(used_block_group != block_group); 5091 if (!last_ptr_loop && last_ptr->block_group &&
5731 /* If we are on LOOP_NO_EMPTY_SIZE, we can't 5092 last_ptr->block_group != block_group &&
5732 * set up a new clusters, so lets just skip it 5093 index <=
5733 * and let the allocator find whatever block 5094 get_block_group_index(last_ptr->block_group)) {
5734 * it can find. If we reach this point, we 5095
5735 * will have tried the cluster allocator 5096 btrfs_put_block_group(block_group);
5736 * plenty of times and not have found 5097 block_group = last_ptr->block_group;
5737 * anything, so we are likely way too 5098 btrfs_get_block_group(block_group);
5738 * fragmented for the clustering stuff to find 5099 spin_unlock(&last_ptr->lock);
5739 * anything.
5740 *
5741 * However, if the cluster is taken from the
5742 * current block group, release the cluster
5743 * first, so that we stand a better chance of
5744 * succeeding in the unclustered
5745 * allocation. */
5746 if (loop >= LOOP_NO_EMPTY_SIZE &&
5747 last_ptr->block_group != block_group) {
5748 spin_unlock(&last_ptr->refill_lock); 5100 spin_unlock(&last_ptr->refill_lock);
5749 goto unclustered_alloc;
5750 }
5751 5101
5102 last_ptr_loop = 1;
5103 search_start = block_group->key.objectid;
5104 /*
5105 * we know this block group is properly
5106 * in the list because
5107 * btrfs_remove_block_group, drops the
5108 * cluster before it removes the block
5109 * group from the list
5110 */
5111 goto have_block_group;
5112 }
5113 spin_unlock(&last_ptr->lock);
5114refill_cluster:
5752 /* 5115 /*
5753 * this cluster didn't work out, free it and 5116 * this cluster didn't work out, free it and
5754 * start over 5117 * start over
5755 */ 5118 */
5756 btrfs_return_cluster_to_free_space(NULL, last_ptr); 5119 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5757 5120
5758 if (loop >= LOOP_NO_EMPTY_SIZE) { 5121 last_ptr_loop = 0;
5759 spin_unlock(&last_ptr->refill_lock);
5760 goto unclustered_alloc;
5761 }
5762 5122
5763 /* allocate a cluster in this block group */ 5123 /* allocate a cluster in this block group */
5764 ret = btrfs_find_space_cluster(trans, root, 5124 ret = btrfs_find_space_cluster(trans, root,
5765 block_group, last_ptr, 5125 block_group, last_ptr,
5766 search_start, num_bytes, 5126 offset, num_bytes,
5767 empty_cluster + empty_size); 5127 empty_cluster + empty_size);
5768 if (ret == 0) { 5128 if (ret == 0) {
5769 /* 5129 /*
@@ -5776,9 +5136,6 @@ refill_cluster:
5776 if (offset) { 5136 if (offset) {
5777 /* we found one, proceed */ 5137 /* we found one, proceed */
5778 spin_unlock(&last_ptr->refill_lock); 5138 spin_unlock(&last_ptr->refill_lock);
5779 trace_btrfs_reserve_extent_cluster(root,
5780 block_group, search_start,
5781 num_bytes);
5782 goto checks; 5139 goto checks;
5783 } 5140 }
5784 } else if (!cached && loop > LOOP_CACHING_NOWAIT 5141 } else if (!cached && loop > LOOP_CACHING_NOWAIT
@@ -5802,16 +5159,6 @@ refill_cluster:
5802 goto loop; 5159 goto loop;
5803 } 5160 }
5804 5161
5805unclustered_alloc:
5806 spin_lock(&block_group->free_space_ctl->tree_lock);
5807 if (cached &&
5808 block_group->free_space_ctl->free_space <
5809 num_bytes + empty_cluster + empty_size) {
5810 spin_unlock(&block_group->free_space_ctl->tree_lock);
5811 goto loop;
5812 }
5813 spin_unlock(&block_group->free_space_ctl->tree_lock);
5814
5815 offset = btrfs_find_space_for_alloc(block_group, search_start, 5162 offset = btrfs_find_space_for_alloc(block_group, search_start,
5816 num_bytes, empty_size); 5163 num_bytes, empty_size);
5817 /* 5164 /*
@@ -5830,29 +5177,35 @@ unclustered_alloc:
5830 failed_alloc = true; 5177 failed_alloc = true;
5831 goto have_block_group; 5178 goto have_block_group;
5832 } else if (!offset) { 5179 } else if (!offset) {
5833 if (!cached)
5834 have_caching_bg = true;
5835 goto loop; 5180 goto loop;
5836 } 5181 }
5837checks: 5182checks:
5838 search_start = stripe_align(root, offset); 5183 search_start = stripe_align(root, offset);
5184 /* move on to the next group */
5185 if (search_start + num_bytes >= search_end) {
5186 btrfs_add_free_space(block_group, offset, num_bytes);
5187 goto loop;
5188 }
5839 5189
5840 /* move on to the next group */ 5190 /* move on to the next group */
5841 if (search_start + num_bytes > 5191 if (search_start + num_bytes >
5842 used_block_group->key.objectid + used_block_group->key.offset) { 5192 block_group->key.objectid + block_group->key.offset) {
5843 btrfs_add_free_space(used_block_group, offset, num_bytes); 5193 btrfs_add_free_space(block_group, offset, num_bytes);
5844 goto loop; 5194 goto loop;
5845 } 5195 }
5846 5196
5197 ins->objectid = search_start;
5198 ins->offset = num_bytes;
5199
5847 if (offset < search_start) 5200 if (offset < search_start)
5848 btrfs_add_free_space(used_block_group, offset, 5201 btrfs_add_free_space(block_group, offset,
5849 search_start - offset); 5202 search_start - offset);
5850 BUG_ON(offset > search_start); 5203 BUG_ON(offset > search_start);
5851 5204
5852 ret = btrfs_update_reserved_bytes(used_block_group, num_bytes, 5205 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
5853 alloc_type); 5206 (data & BTRFS_BLOCK_GROUP_DATA));
5854 if (ret == -EAGAIN) { 5207 if (ret == -EAGAIN) {
5855 btrfs_add_free_space(used_block_group, offset, num_bytes); 5208 btrfs_add_free_space(block_group, offset, num_bytes);
5856 goto loop; 5209 goto loop;
5857 } 5210 }
5858 5211
@@ -5860,29 +5213,26 @@ checks:
5860 ins->objectid = search_start; 5213 ins->objectid = search_start;
5861 ins->offset = num_bytes; 5214 ins->offset = num_bytes;
5862 5215
5863 trace_btrfs_reserve_extent(orig_root, block_group, 5216 if (offset < search_start)
5864 search_start, num_bytes); 5217 btrfs_add_free_space(block_group, offset,
5865 if (used_block_group != block_group) 5218 search_start - offset);
5866 btrfs_put_block_group(used_block_group); 5219 BUG_ON(offset > search_start);
5867 btrfs_put_block_group(block_group); 5220 btrfs_put_block_group(block_group);
5868 break; 5221 break;
5869loop: 5222loop:
5870 failed_cluster_refill = false; 5223 failed_cluster_refill = false;
5871 failed_alloc = false; 5224 failed_alloc = false;
5872 BUG_ON(index != get_block_group_index(block_group)); 5225 BUG_ON(index != get_block_group_index(block_group));
5873 if (used_block_group != block_group)
5874 btrfs_put_block_group(used_block_group);
5875 btrfs_put_block_group(block_group); 5226 btrfs_put_block_group(block_group);
5876 } 5227 }
5877 up_read(&space_info->groups_sem); 5228 up_read(&space_info->groups_sem);
5878 5229
5879 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
5880 goto search;
5881
5882 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) 5230 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5883 goto search; 5231 goto search;
5884 5232
5885 /* 5233 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5234 * for them to make caching progress. Also
5235 * determine the best possible bg to cache
5886 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking 5236 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5887 * caching kthreads as we move along 5237 * caching kthreads as we move along
5888 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching 5238 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
@@ -5892,19 +5242,65 @@ loop:
5892 */ 5242 */
5893 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { 5243 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
5894 index = 0; 5244 index = 0;
5895 loop++; 5245 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5896 if (loop == LOOP_ALLOC_CHUNK) { 5246 found_uncached_bg = false;
5897 ret = do_chunk_alloc(trans, root, data, 5247 loop++;
5898 CHUNK_ALLOC_FORCE); 5248 if (!ideal_cache_percent)
5249 goto search;
5250
5899 /* 5251 /*
5900 * Do not bail out on ENOSPC since we 5252 * 1 of the following 2 things have happened so far
5901 * can do more things. 5253 *
5254 * 1) We found an ideal block group for caching that
5255 * is mostly full and will cache quickly, so we might
5256 * as well wait for it.
5257 *
5258 * 2) We searched for cached only and we didn't find
5259 * anything, and we didn't start any caching kthreads
5260 * either, so chances are we will loop through and
5261 * start a couple caching kthreads, and then come back
5262 * around and just wait for them. This will be slower
5263 * because we will have 2 caching kthreads reading at
5264 * the same time when we could have just started one
5265 * and waited for it to get far enough to give us an
5266 * allocation, so go ahead and go to the wait caching
5267 * loop.
5902 */ 5268 */
5903 if (ret < 0 && ret != -ENOSPC) { 5269 loop = LOOP_CACHING_WAIT;
5904 btrfs_abort_transaction(trans, 5270 search_start = ideal_cache_offset;
5905 root, ret); 5271 ideal_cache_percent = 0;
5906 goto out; 5272 goto ideal_cache;
5273 } else if (loop == LOOP_FIND_IDEAL) {
5274 /*
5275 * Didn't find a uncached bg, wait on anything we find
5276 * next.
5277 */
5278 loop = LOOP_CACHING_WAIT;
5279 goto search;
5280 }
5281
5282 loop++;
5283
5284 if (loop == LOOP_ALLOC_CHUNK) {
5285 if (allowed_chunk_alloc) {
5286 ret = do_chunk_alloc(trans, root, num_bytes +
5287 2 * 1024 * 1024, data,
5288 CHUNK_ALLOC_LIMITED);
5289 allowed_chunk_alloc = 0;
5290 if (ret == 1)
5291 done_chunk_alloc = 1;
5292 } else if (!done_chunk_alloc &&
5293 space_info->force_alloc ==
5294 CHUNK_ALLOC_NO_FORCE) {
5295 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5907 } 5296 }
5297
5298 /*
5299 * We didn't allocate a chunk, go ahead and drop the
5300 * empty size and loop again.
5301 */
5302 if (!done_chunk_alloc)
5303 loop = LOOP_NO_EMPTY_SIZE;
5908 } 5304 }
5909 5305
5910 if (loop == LOOP_NO_EMPTY_SIZE) { 5306 if (loop == LOOP_NO_EMPTY_SIZE) {
@@ -5918,7 +5314,6 @@ loop:
5918 } else if (ins->objectid) { 5314 } else if (ins->objectid) {
5919 ret = 0; 5315 ret = 0;
5920 } 5316 }
5921out:
5922 5317
5923 return ret; 5318 return ret;
5924} 5319}
@@ -5930,8 +5325,7 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5930 int index = 0; 5325 int index = 0;
5931 5326
5932 spin_lock(&info->lock); 5327 spin_lock(&info->lock);
5933 printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", 5328 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5934 (unsigned long long)info->flags,
5935 (unsigned long long)(info->total_bytes - info->bytes_used - 5329 (unsigned long long)(info->total_bytes - info->bytes_used -
5936 info->bytes_pinned - info->bytes_reserved - 5330 info->bytes_pinned - info->bytes_reserved -
5937 info->bytes_readonly), 5331 info->bytes_readonly),
@@ -5953,13 +5347,13 @@ static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5953again: 5347again:
5954 list_for_each_entry(cache, &info->block_groups[index], list) { 5348 list_for_each_entry(cache, &info->block_groups[index], list) {
5955 spin_lock(&cache->lock); 5349 spin_lock(&cache->lock);
5956 printk(KERN_INFO "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s\n", 5350 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5351 "%llu pinned %llu reserved\n",
5957 (unsigned long long)cache->key.objectid, 5352 (unsigned long long)cache->key.objectid,
5958 (unsigned long long)cache->key.offset, 5353 (unsigned long long)cache->key.offset,
5959 (unsigned long long)btrfs_block_group_used(&cache->item), 5354 (unsigned long long)btrfs_block_group_used(&cache->item),
5960 (unsigned long long)cache->pinned, 5355 (unsigned long long)cache->pinned,
5961 (unsigned long long)cache->reserved, 5356 (unsigned long long)cache->reserved);
5962 cache->ro ? "[readonly]" : "");
5963 btrfs_dump_free_space(cache, bytes); 5357 btrfs_dump_free_space(cache, bytes);
5964 spin_unlock(&cache->lock); 5358 spin_unlock(&cache->lock);
5965 } 5359 }
@@ -5972,35 +5366,44 @@ int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5972 struct btrfs_root *root, 5366 struct btrfs_root *root,
5973 u64 num_bytes, u64 min_alloc_size, 5367 u64 num_bytes, u64 min_alloc_size,
5974 u64 empty_size, u64 hint_byte, 5368 u64 empty_size, u64 hint_byte,
5975 struct btrfs_key *ins, u64 data) 5369 u64 search_end, struct btrfs_key *ins,
5370 u64 data)
5976{ 5371{
5977 bool final_tried = false;
5978 int ret; 5372 int ret;
5373 u64 search_start = 0;
5979 5374
5980 data = btrfs_get_alloc_profile(root, data); 5375 data = btrfs_get_alloc_profile(root, data);
5981again: 5376again:
5377 /*
5378 * the only place that sets empty_size is btrfs_realloc_node, which
5379 * is not called recursively on allocations
5380 */
5381 if (empty_size || root->ref_cows)
5382 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5383 num_bytes + 2 * 1024 * 1024, data,
5384 CHUNK_ALLOC_NO_FORCE);
5385
5982 WARN_ON(num_bytes < root->sectorsize); 5386 WARN_ON(num_bytes < root->sectorsize);
5983 ret = find_free_extent(trans, root, num_bytes, empty_size, 5387 ret = find_free_extent(trans, root, num_bytes, empty_size,
5984 hint_byte, ins, data); 5388 search_start, search_end, hint_byte,
5985 5389 ins, data);
5986 if (ret == -ENOSPC) { 5390
5987 if (!final_tried) { 5391 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5988 num_bytes = num_bytes >> 1; 5392 num_bytes = num_bytes >> 1;
5989 num_bytes = num_bytes & ~(root->sectorsize - 1); 5393 num_bytes = num_bytes & ~(root->sectorsize - 1);
5990 num_bytes = max(num_bytes, min_alloc_size); 5394 num_bytes = max(num_bytes, min_alloc_size);
5991 if (num_bytes == min_alloc_size) 5395 do_chunk_alloc(trans, root->fs_info->extent_root,
5992 final_tried = true; 5396 num_bytes, data, CHUNK_ALLOC_FORCE);
5993 goto again; 5397 goto again;
5994 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) { 5398 }
5995 struct btrfs_space_info *sinfo; 5399 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
5996 5400 struct btrfs_space_info *sinfo;
5997 sinfo = __find_space_info(root->fs_info, data); 5401
5998 printk(KERN_ERR "btrfs allocation failed flags %llu, " 5402 sinfo = __find_space_info(root->fs_info, data);
5999 "wanted %llu\n", (unsigned long long)data, 5403 printk(KERN_ERR "btrfs allocation failed flags %llu, "
6000 (unsigned long long)num_bytes); 5404 "wanted %llu\n", (unsigned long long)data,
6001 if (sinfo) 5405 (unsigned long long)num_bytes);
6002 dump_space_info(sinfo, num_bytes, 1); 5406 dump_space_info(sinfo, num_bytes, 1);
6003 }
6004 } 5407 }
6005 5408
6006 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); 5409 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
@@ -6008,8 +5411,7 @@ again:
6008 return ret; 5411 return ret;
6009} 5412}
6010 5413
6011static int __btrfs_free_reserved_extent(struct btrfs_root *root, 5414int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
6012 u64 start, u64 len, int pin)
6013{ 5415{
6014 struct btrfs_block_group_cache *cache; 5416 struct btrfs_block_group_cache *cache;
6015 int ret = 0; 5417 int ret = 0;
@@ -6024,12 +5426,8 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6024 if (btrfs_test_opt(root, DISCARD)) 5426 if (btrfs_test_opt(root, DISCARD))
6025 ret = btrfs_discard_extent(root, start, len, NULL); 5427 ret = btrfs_discard_extent(root, start, len, NULL);
6026 5428
6027 if (pin) 5429 btrfs_add_free_space(cache, start, len);
6028 pin_down_extent(root, cache, start, len, 1); 5430 btrfs_update_reserved_bytes(cache, len, 0, 1);
6029 else {
6030 btrfs_add_free_space(cache, start, len);
6031 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE);
6032 }
6033 btrfs_put_block_group(cache); 5431 btrfs_put_block_group(cache);
6034 5432
6035 trace_btrfs_reserved_extent_free(root, start, len); 5433 trace_btrfs_reserved_extent_free(root, start, len);
@@ -6037,18 +5435,6 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6037 return ret; 5435 return ret;
6038} 5436}
6039 5437
6040int btrfs_free_reserved_extent(struct btrfs_root *root,
6041 u64 start, u64 len)
6042{
6043 return __btrfs_free_reserved_extent(root, start, len, 0);
6044}
6045
6046int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6047 u64 start, u64 len)
6048{
6049 return __btrfs_free_reserved_extent(root, start, len, 1);
6050}
6051
6052static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 5438static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6053 struct btrfs_root *root, 5439 struct btrfs_root *root,
6054 u64 parent, u64 root_objectid, 5440 u64 parent, u64 root_objectid,
@@ -6078,10 +5464,7 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6078 path->leave_spinning = 1; 5464 path->leave_spinning = 1;
6079 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, 5465 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6080 ins, size); 5466 ins, size);
6081 if (ret) { 5467 BUG_ON(ret);
6082 btrfs_free_path(path);
6083 return ret;
6084 }
6085 5468
6086 leaf = path->nodes[0]; 5469 leaf = path->nodes[0];
6087 extent_item = btrfs_item_ptr(leaf, path->slots[0], 5470 extent_item = btrfs_item_ptr(leaf, path->slots[0],
@@ -6111,7 +5494,7 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6111 btrfs_free_path(path); 5494 btrfs_free_path(path);
6112 5495
6113 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); 5496 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
6114 if (ret) { /* -ENOENT, logic error */ 5497 if (ret) {
6115 printk(KERN_ERR "btrfs update block group failed for %llu " 5498 printk(KERN_ERR "btrfs update block group failed for %llu "
6116 "%llu\n", (unsigned long long)ins->objectid, 5499 "%llu\n", (unsigned long long)ins->objectid,
6117 (unsigned long long)ins->offset); 5500 (unsigned long long)ins->offset);
@@ -6142,10 +5525,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6142 path->leave_spinning = 1; 5525 path->leave_spinning = 1;
6143 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, 5526 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
6144 ins, size); 5527 ins, size);
6145 if (ret) { 5528 BUG_ON(ret);
6146 btrfs_free_path(path);
6147 return ret;
6148 }
6149 5529
6150 leaf = path->nodes[0]; 5530 leaf = path->nodes[0];
6151 extent_item = btrfs_item_ptr(leaf, path->slots[0], 5531 extent_item = btrfs_item_ptr(leaf, path->slots[0],
@@ -6175,7 +5555,7 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
6175 btrfs_free_path(path); 5555 btrfs_free_path(path);
6176 5556
6177 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); 5557 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
6178 if (ret) { /* -ENOENT, logic error */ 5558 if (ret) {
6179 printk(KERN_ERR "btrfs update block group failed for %llu " 5559 printk(KERN_ERR "btrfs update block group failed for %llu "
6180 "%llu\n", (unsigned long long)ins->objectid, 5560 "%llu\n", (unsigned long long)ins->objectid,
6181 (unsigned long long)ins->offset); 5561 (unsigned long long)ins->offset);
@@ -6193,10 +5573,9 @@ int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6193 5573
6194 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); 5574 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
6195 5575
6196 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid, 5576 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
6197 ins->offset, 0, 5577 0, root_objectid, owner, offset,
6198 root_objectid, owner, offset, 5578 BTRFS_ADD_DELAYED_EXTENT, NULL);
6199 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
6200 return ret; 5579 return ret;
6201} 5580}
6202 5581
@@ -6223,37 +5602,36 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
6223 if (!caching_ctl) { 5602 if (!caching_ctl) {
6224 BUG_ON(!block_group_cache_done(block_group)); 5603 BUG_ON(!block_group_cache_done(block_group));
6225 ret = btrfs_remove_free_space(block_group, start, num_bytes); 5604 ret = btrfs_remove_free_space(block_group, start, num_bytes);
6226 BUG_ON(ret); /* -ENOMEM */ 5605 BUG_ON(ret);
6227 } else { 5606 } else {
6228 mutex_lock(&caching_ctl->mutex); 5607 mutex_lock(&caching_ctl->mutex);
6229 5608
6230 if (start >= caching_ctl->progress) { 5609 if (start >= caching_ctl->progress) {
6231 ret = add_excluded_extent(root, start, num_bytes); 5610 ret = add_excluded_extent(root, start, num_bytes);
6232 BUG_ON(ret); /* -ENOMEM */ 5611 BUG_ON(ret);
6233 } else if (start + num_bytes <= caching_ctl->progress) { 5612 } else if (start + num_bytes <= caching_ctl->progress) {
6234 ret = btrfs_remove_free_space(block_group, 5613 ret = btrfs_remove_free_space(block_group,
6235 start, num_bytes); 5614 start, num_bytes);
6236 BUG_ON(ret); /* -ENOMEM */ 5615 BUG_ON(ret);
6237 } else { 5616 } else {
6238 num_bytes = caching_ctl->progress - start; 5617 num_bytes = caching_ctl->progress - start;
6239 ret = btrfs_remove_free_space(block_group, 5618 ret = btrfs_remove_free_space(block_group,
6240 start, num_bytes); 5619 start, num_bytes);
6241 BUG_ON(ret); /* -ENOMEM */ 5620 BUG_ON(ret);
6242 5621
6243 start = caching_ctl->progress; 5622 start = caching_ctl->progress;
6244 num_bytes = ins->objectid + ins->offset - 5623 num_bytes = ins->objectid + ins->offset -
6245 caching_ctl->progress; 5624 caching_ctl->progress;
6246 ret = add_excluded_extent(root, start, num_bytes); 5625 ret = add_excluded_extent(root, start, num_bytes);
6247 BUG_ON(ret); /* -ENOMEM */ 5626 BUG_ON(ret);
6248 } 5627 }
6249 5628
6250 mutex_unlock(&caching_ctl->mutex); 5629 mutex_unlock(&caching_ctl->mutex);
6251 put_caching_control(caching_ctl); 5630 put_caching_control(caching_ctl);
6252 } 5631 }
6253 5632
6254 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 5633 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
6255 RESERVE_ALLOC_NO_ACCOUNT); 5634 BUG_ON(ret);
6256 BUG_ON(ret); /* logic error */
6257 btrfs_put_block_group(block_group); 5635 btrfs_put_block_group(block_group);
6258 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 5636 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
6259 0, owner, offset, ins, 1); 5637 0, owner, offset, ins, 1);
@@ -6274,7 +5652,6 @@ struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
6274 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); 5652 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
6275 btrfs_tree_lock(buf); 5653 btrfs_tree_lock(buf);
6276 clean_tree_block(trans, root, buf); 5654 clean_tree_block(trans, root, buf);
6277 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
6278 5655
6279 btrfs_set_lock_blocking(buf); 5656 btrfs_set_lock_blocking(buf);
6280 btrfs_set_buffer_uptodate(buf); 5657 btrfs_set_buffer_uptodate(buf);
@@ -6310,8 +5687,8 @@ use_block_rsv(struct btrfs_trans_handle *trans,
6310 block_rsv = get_block_rsv(trans, root); 5687 block_rsv = get_block_rsv(trans, root);
6311 5688
6312 if (block_rsv->size == 0) { 5689 if (block_rsv->size == 0) {
6313 ret = reserve_metadata_bytes(root, block_rsv, blocksize, 5690 ret = reserve_metadata_bytes(trans, root, block_rsv,
6314 BTRFS_RESERVE_NO_FLUSH); 5691 blocksize, 0);
6315 /* 5692 /*
6316 * If we couldn't reserve metadata bytes try and use some from 5693 * If we couldn't reserve metadata bytes try and use some from
6317 * the global reserve. 5694 * the global reserve.
@@ -6330,16 +5707,14 @@ use_block_rsv(struct btrfs_trans_handle *trans,
6330 ret = block_rsv_use_bytes(block_rsv, blocksize); 5707 ret = block_rsv_use_bytes(block_rsv, blocksize);
6331 if (!ret) 5708 if (!ret)
6332 return block_rsv; 5709 return block_rsv;
6333 if (ret && !block_rsv->failfast) { 5710 if (ret) {
6334 static DEFINE_RATELIMIT_STATE(_rs, 5711 WARN_ON(1);
6335 DEFAULT_RATELIMIT_INTERVAL, 5712 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
6336 /*DEFAULT_RATELIMIT_BURST*/ 2); 5713 0);
6337 if (__ratelimit(&_rs))
6338 WARN(1, KERN_DEBUG "btrfs: block rsv returned %d\n",
6339 ret);
6340 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
6341 BTRFS_RESERVE_NO_FLUSH);
6342 if (!ret) { 5714 if (!ret) {
5715 spin_lock(&block_rsv->lock);
5716 block_rsv->size += blocksize;
5717 spin_unlock(&block_rsv->lock);
6343 return block_rsv; 5718 return block_rsv;
6344 } else if (ret && block_rsv != global_rsv) { 5719 } else if (ret && block_rsv != global_rsv) {
6345 ret = block_rsv_use_bytes(global_rsv, blocksize); 5720 ret = block_rsv_use_bytes(global_rsv, blocksize);
@@ -6351,11 +5726,10 @@ use_block_rsv(struct btrfs_trans_handle *trans,
6351 return ERR_PTR(-ENOSPC); 5726 return ERR_PTR(-ENOSPC);
6352} 5727}
6353 5728
6354static void unuse_block_rsv(struct btrfs_fs_info *fs_info, 5729static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
6355 struct btrfs_block_rsv *block_rsv, u32 blocksize)
6356{ 5730{
6357 block_rsv_add_bytes(block_rsv, blocksize, 0); 5731 block_rsv_add_bytes(block_rsv, blocksize, 0);
6358 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); 5732 block_rsv_release_bytes(block_rsv, NULL, 0);
6359} 5733}
6360 5734
6361/* 5735/*
@@ -6383,15 +5757,15 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6383 return ERR_CAST(block_rsv); 5757 return ERR_CAST(block_rsv);
6384 5758
6385 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, 5759 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
6386 empty_size, hint, &ins, 0); 5760 empty_size, hint, (u64)-1, &ins, 0);
6387 if (ret) { 5761 if (ret) {
6388 unuse_block_rsv(root->fs_info, block_rsv, blocksize); 5762 unuse_block_rsv(block_rsv, blocksize);
6389 return ERR_PTR(ret); 5763 return ERR_PTR(ret);
6390 } 5764 }
6391 5765
6392 buf = btrfs_init_new_buffer(trans, root, ins.objectid, 5766 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
6393 blocksize, level); 5767 blocksize, level);
6394 BUG_ON(IS_ERR(buf)); /* -ENOMEM */ 5768 BUG_ON(IS_ERR(buf));
6395 5769
6396 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { 5770 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
6397 if (parent == 0) 5771 if (parent == 0)
@@ -6403,7 +5777,7 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6403 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { 5777 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
6404 struct btrfs_delayed_extent_op *extent_op; 5778 struct btrfs_delayed_extent_op *extent_op;
6405 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); 5779 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
6406 BUG_ON(!extent_op); /* -ENOMEM */ 5780 BUG_ON(!extent_op);
6407 if (key) 5781 if (key)
6408 memcpy(&extent_op->key, key, sizeof(extent_op->key)); 5782 memcpy(&extent_op->key, key, sizeof(extent_op->key));
6409 else 5783 else
@@ -6413,12 +5787,11 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
6413 extent_op->update_flags = 1; 5787 extent_op->update_flags = 1;
6414 extent_op->is_data = 0; 5788 extent_op->is_data = 0;
6415 5789
6416 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans, 5790 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
6417 ins.objectid,
6418 ins.offset, parent, root_objectid, 5791 ins.offset, parent, root_objectid,
6419 level, BTRFS_ADD_DELAYED_EXTENT, 5792 level, BTRFS_ADD_DELAYED_EXTENT,
6420 extent_op, 0); 5793 extent_op);
6421 BUG_ON(ret); /* -ENOMEM */ 5794 BUG_ON(ret);
6422 } 5795 }
6423 return buf; 5796 return buf;
6424} 5797}
@@ -6434,7 +5807,6 @@ struct walk_control {
6434 int keep_locks; 5807 int keep_locks;
6435 int reada_slot; 5808 int reada_slot;
6436 int reada_count; 5809 int reada_count;
6437 int for_reloc;
6438}; 5810};
6439 5811
6440#define DROP_REFERENCE 1 5812#define DROP_REFERENCE 1
@@ -6488,9 +5860,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
6488 /* We don't lock the tree block, it's OK to be racy here */ 5860 /* We don't lock the tree block, it's OK to be racy here */
6489 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 5861 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6490 &refs, &flags); 5862 &refs, &flags);
6491 /* We don't care about errors in readahead. */ 5863 BUG_ON(ret);
6492 if (ret < 0)
6493 continue;
6494 BUG_ON(refs == 0); 5864 BUG_ON(refs == 0);
6495 5865
6496 if (wc->stage == DROP_REFERENCE) { 5866 if (wc->stage == DROP_REFERENCE) {
@@ -6557,9 +5927,7 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6557 eb->start, eb->len, 5927 eb->start, eb->len,
6558 &wc->refs[level], 5928 &wc->refs[level],
6559 &wc->flags[level]); 5929 &wc->flags[level]);
6560 BUG_ON(ret == -ENOMEM); 5930 BUG_ON(ret);
6561 if (ret)
6562 return ret;
6563 BUG_ON(wc->refs[level] == 0); 5931 BUG_ON(wc->refs[level] == 0);
6564 } 5932 }
6565 5933
@@ -6577,13 +5945,13 @@ static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
6577 /* wc->stage == UPDATE_BACKREF */ 5945 /* wc->stage == UPDATE_BACKREF */
6578 if (!(wc->flags[level] & flag)) { 5946 if (!(wc->flags[level] & flag)) {
6579 BUG_ON(!path->locks[level]); 5947 BUG_ON(!path->locks[level]);
6580 ret = btrfs_inc_ref(trans, root, eb, 1, wc->for_reloc); 5948 ret = btrfs_inc_ref(trans, root, eb, 1);
6581 BUG_ON(ret); /* -ENOMEM */ 5949 BUG_ON(ret);
6582 ret = btrfs_dec_ref(trans, root, eb, 0, wc->for_reloc); 5950 ret = btrfs_dec_ref(trans, root, eb, 0);
6583 BUG_ON(ret); /* -ENOMEM */ 5951 BUG_ON(ret);
6584 ret = btrfs_set_disk_extent_flags(trans, root, eb->start, 5952 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6585 eb->len, flag, 0); 5953 eb->len, flag, 0);
6586 BUG_ON(ret); /* -ENOMEM */ 5954 BUG_ON(ret);
6587 wc->flags[level] |= flag; 5955 wc->flags[level] |= flag;
6588 } 5956 }
6589 5957
@@ -6655,11 +6023,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6655 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, 6023 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6656 &wc->refs[level - 1], 6024 &wc->refs[level - 1],
6657 &wc->flags[level - 1]); 6025 &wc->flags[level - 1]);
6658 if (ret < 0) { 6026 BUG_ON(ret);
6659 btrfs_tree_unlock(next);
6660 return ret;
6661 }
6662
6663 BUG_ON(wc->refs[level - 1] == 0); 6027 BUG_ON(wc->refs[level - 1] == 0);
6664 *lookup_info = 0; 6028 *lookup_info = 0;
6665 6029
@@ -6688,7 +6052,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6688 goto skip; 6052 goto skip;
6689 } 6053 }
6690 6054
6691 if (!btrfs_buffer_uptodate(next, generation, 0)) { 6055 if (!btrfs_buffer_uptodate(next, generation)) {
6692 btrfs_tree_unlock(next); 6056 btrfs_tree_unlock(next);
6693 free_extent_buffer(next); 6057 free_extent_buffer(next);
6694 next = NULL; 6058 next = NULL;
@@ -6727,8 +6091,8 @@ skip:
6727 } 6091 }
6728 6092
6729 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, 6093 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6730 root->root_key.objectid, level - 1, 0, 0); 6094 root->root_key.objectid, level - 1, 0);
6731 BUG_ON(ret); /* -ENOMEM */ 6095 BUG_ON(ret);
6732 } 6096 }
6733 btrfs_tree_unlock(next); 6097 btrfs_tree_unlock(next);
6734 free_extent_buffer(next); 6098 free_extent_buffer(next);
@@ -6786,10 +6150,7 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6786 eb->start, eb->len, 6150 eb->start, eb->len,
6787 &wc->refs[level], 6151 &wc->refs[level],
6788 &wc->flags[level]); 6152 &wc->flags[level]);
6789 if (ret < 0) { 6153 BUG_ON(ret);
6790 btrfs_tree_unlock_rw(eb, path->locks[level]);
6791 return ret;
6792 }
6793 BUG_ON(wc->refs[level] == 0); 6154 BUG_ON(wc->refs[level] == 0);
6794 if (wc->refs[level] == 1) { 6155 if (wc->refs[level] == 1) {
6795 btrfs_tree_unlock_rw(eb, path->locks[level]); 6156 btrfs_tree_unlock_rw(eb, path->locks[level]);
@@ -6804,12 +6165,10 @@ static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6804 if (wc->refs[level] == 1) { 6165 if (wc->refs[level] == 1) {
6805 if (level == 0) { 6166 if (level == 0) {
6806 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) 6167 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6807 ret = btrfs_dec_ref(trans, root, eb, 1, 6168 ret = btrfs_dec_ref(trans, root, eb, 1);
6808 wc->for_reloc);
6809 else 6169 else
6810 ret = btrfs_dec_ref(trans, root, eb, 0, 6170 ret = btrfs_dec_ref(trans, root, eb, 0);
6811 wc->for_reloc); 6171 BUG_ON(ret);
6812 BUG_ON(ret); /* -ENOMEM */
6813 } 6172 }
6814 /* make block locked assertion in clean_tree_block happy */ 6173 /* make block locked assertion in clean_tree_block happy */
6815 if (!path->locks[level] && 6174 if (!path->locks[level] &&
@@ -6918,9 +6277,8 @@ static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6918 * also make sure backrefs for the shared block and all lower level 6277 * also make sure backrefs for the shared block and all lower level
6919 * blocks are properly updated. 6278 * blocks are properly updated.
6920 */ 6279 */
6921int btrfs_drop_snapshot(struct btrfs_root *root, 6280void btrfs_drop_snapshot(struct btrfs_root *root,
6922 struct btrfs_block_rsv *block_rsv, int update_ref, 6281 struct btrfs_block_rsv *block_rsv, int update_ref)
6923 int for_reloc)
6924{ 6282{
6925 struct btrfs_path *path; 6283 struct btrfs_path *path;
6926 struct btrfs_trans_handle *trans; 6284 struct btrfs_trans_handle *trans;
@@ -6946,10 +6304,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6946 } 6304 }
6947 6305
6948 trans = btrfs_start_transaction(tree_root, 0); 6306 trans = btrfs_start_transaction(tree_root, 0);
6949 if (IS_ERR(trans)) { 6307 BUG_ON(IS_ERR(trans));
6950 err = PTR_ERR(trans);
6951 goto out_free;
6952 }
6953 6308
6954 if (block_rsv) 6309 if (block_rsv)
6955 trans->block_rsv = block_rsv; 6310 trans->block_rsv = block_rsv;
@@ -6974,7 +6329,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6974 path->lowest_level = 0; 6329 path->lowest_level = 0;
6975 if (ret < 0) { 6330 if (ret < 0) {
6976 err = ret; 6331 err = ret;
6977 goto out_end_trans; 6332 goto out_free;
6978 } 6333 }
6979 WARN_ON(ret > 0); 6334 WARN_ON(ret > 0);
6980 6335
@@ -6994,10 +6349,7 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
6994 path->nodes[level]->len, 6349 path->nodes[level]->len,
6995 &wc->refs[level], 6350 &wc->refs[level],
6996 &wc->flags[level]); 6351 &wc->flags[level]);
6997 if (ret < 0) { 6352 BUG_ON(ret);
6998 err = ret;
6999 goto out_end_trans;
7000 }
7001 BUG_ON(wc->refs[level] == 0); 6353 BUG_ON(wc->refs[level] == 0);
7002 6354
7003 if (level == root_item->drop_level) 6355 if (level == root_item->drop_level)
@@ -7014,7 +6366,6 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
7014 wc->stage = DROP_REFERENCE; 6366 wc->stage = DROP_REFERENCE;
7015 wc->update_ref = update_ref; 6367 wc->update_ref = update_ref;
7016 wc->keep_locks = 0; 6368 wc->keep_locks = 0;
7017 wc->for_reloc = for_reloc;
7018 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); 6369 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
7019 6370
7020 while (1) { 6371 while (1) {
@@ -7048,40 +6399,26 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
7048 ret = btrfs_update_root(trans, tree_root, 6399 ret = btrfs_update_root(trans, tree_root,
7049 &root->root_key, 6400 &root->root_key,
7050 root_item); 6401 root_item);
7051 if (ret) { 6402 BUG_ON(ret);
7052 btrfs_abort_transaction(trans, tree_root, ret);
7053 err = ret;
7054 goto out_end_trans;
7055 }
7056 6403
7057 btrfs_end_transaction_throttle(trans, tree_root); 6404 btrfs_end_transaction_throttle(trans, tree_root);
7058 trans = btrfs_start_transaction(tree_root, 0); 6405 trans = btrfs_start_transaction(tree_root, 0);
7059 if (IS_ERR(trans)) { 6406 BUG_ON(IS_ERR(trans));
7060 err = PTR_ERR(trans);
7061 goto out_free;
7062 }
7063 if (block_rsv) 6407 if (block_rsv)
7064 trans->block_rsv = block_rsv; 6408 trans->block_rsv = block_rsv;
7065 } 6409 }
7066 } 6410 }
7067 btrfs_release_path(path); 6411 btrfs_release_path(path);
7068 if (err) 6412 BUG_ON(err);
7069 goto out_end_trans;
7070 6413
7071 ret = btrfs_del_root(trans, tree_root, &root->root_key); 6414 ret = btrfs_del_root(trans, tree_root, &root->root_key);
7072 if (ret) { 6415 BUG_ON(ret);
7073 btrfs_abort_transaction(trans, tree_root, ret);
7074 goto out_end_trans;
7075 }
7076 6416
7077 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { 6417 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
7078 ret = btrfs_find_last_root(tree_root, root->root_key.objectid, 6418 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
7079 NULL, NULL); 6419 NULL, NULL);
7080 if (ret < 0) { 6420 BUG_ON(ret < 0);
7081 btrfs_abort_transaction(trans, tree_root, ret); 6421 if (ret > 0) {
7082 err = ret;
7083 goto out_end_trans;
7084 } else if (ret > 0) {
7085 /* if we fail to delete the orphan item this time 6422 /* if we fail to delete the orphan item this time
7086 * around, it'll get picked up the next time. 6423 * around, it'll get picked up the next time.
7087 * 6424 *
@@ -7099,22 +6436,20 @@ int btrfs_drop_snapshot(struct btrfs_root *root,
7099 free_extent_buffer(root->commit_root); 6436 free_extent_buffer(root->commit_root);
7100 kfree(root); 6437 kfree(root);
7101 } 6438 }
7102out_end_trans:
7103 btrfs_end_transaction_throttle(trans, tree_root);
7104out_free: 6439out_free:
6440 btrfs_end_transaction_throttle(trans, tree_root);
7105 kfree(wc); 6441 kfree(wc);
7106 btrfs_free_path(path); 6442 btrfs_free_path(path);
7107out: 6443out:
7108 if (err) 6444 if (err)
7109 btrfs_std_error(root->fs_info, err); 6445 btrfs_std_error(root->fs_info, err);
7110 return err; 6446 return;
7111} 6447}
7112 6448
7113/* 6449/*
7114 * drop subtree rooted at tree block 'node'. 6450 * drop subtree rooted at tree block 'node'.
7115 * 6451 *
7116 * NOTE: this function will unlock and release tree block 'node' 6452 * NOTE: this function will unlock and release tree block 'node'
7117 * only used by relocation code
7118 */ 6453 */
7119int btrfs_drop_subtree(struct btrfs_trans_handle *trans, 6454int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7120 struct btrfs_root *root, 6455 struct btrfs_root *root,
@@ -7159,7 +6494,6 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7159 wc->stage = DROP_REFERENCE; 6494 wc->stage = DROP_REFERENCE;
7160 wc->update_ref = 0; 6495 wc->update_ref = 0;
7161 wc->keep_locks = 1; 6496 wc->keep_locks = 1;
7162 wc->for_reloc = 1;
7163 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); 6497 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
7164 6498
7165 while (1) { 6499 while (1) {
@@ -7184,15 +6518,8 @@ int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
7184static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) 6518static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7185{ 6519{
7186 u64 num_devices; 6520 u64 num_devices;
7187 u64 stripped; 6521 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7188 6522 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7189 /*
7190 * if restripe for this chunk_type is on pick target profile and
7191 * return, otherwise do the usual balance
7192 */
7193 stripped = get_restripe_target(root->fs_info, flags);
7194 if (stripped)
7195 return extended_to_chunk(stripped);
7196 6523
7197 /* 6524 /*
7198 * we add in the count of missing devices because we want 6525 * we add in the count of missing devices because we want
@@ -7202,9 +6529,6 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7202 num_devices = root->fs_info->fs_devices->rw_devices + 6529 num_devices = root->fs_info->fs_devices->rw_devices +
7203 root->fs_info->fs_devices->missing_devices; 6530 root->fs_info->fs_devices->missing_devices;
7204 6531
7205 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7206 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7207
7208 if (num_devices == 1) { 6532 if (num_devices == 1) {
7209 stripped |= BTRFS_BLOCK_GROUP_DUP; 6533 stripped |= BTRFS_BLOCK_GROUP_DUP;
7210 stripped = flags & ~stripped; 6534 stripped = flags & ~stripped;
@@ -7217,6 +6541,7 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7217 if (flags & (BTRFS_BLOCK_GROUP_RAID1 | 6541 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7218 BTRFS_BLOCK_GROUP_RAID10)) 6542 BTRFS_BLOCK_GROUP_RAID10))
7219 return stripped | BTRFS_BLOCK_GROUP_DUP; 6543 return stripped | BTRFS_BLOCK_GROUP_DUP;
6544 return flags;
7220 } else { 6545 } else {
7221 /* they already had raid on here, just return */ 6546 /* they already had raid on here, just return */
7222 if (flags & stripped) 6547 if (flags & stripped)
@@ -7229,9 +6554,9 @@ static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7229 if (flags & BTRFS_BLOCK_GROUP_DUP) 6554 if (flags & BTRFS_BLOCK_GROUP_DUP)
7230 return stripped | BTRFS_BLOCK_GROUP_RAID1; 6555 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7231 6556
7232 /* this is drive concat, leave it alone */ 6557 /* turn single device chunks into raid0 */
6558 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7233 } 6559 }
7234
7235 return flags; 6560 return flags;
7236} 6561}
7237 6562
@@ -7267,9 +6592,12 @@ static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
7267 cache->bytes_super - btrfs_block_group_used(&cache->item); 6592 cache->bytes_super - btrfs_block_group_used(&cache->item);
7268 6593
7269 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + 6594 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7270 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + 6595 sinfo->bytes_may_use + sinfo->bytes_readonly +
7271 min_allocable_bytes <= sinfo->total_bytes) { 6596 cache->reserved_pinned + num_bytes + min_allocable_bytes <=
6597 sinfo->total_bytes) {
7272 sinfo->bytes_readonly += num_bytes; 6598 sinfo->bytes_readonly += num_bytes;
6599 sinfo->bytes_reserved += cache->reserved_pinned;
6600 cache->reserved_pinned = 0;
7273 cache->ro = 1; 6601 cache->ro = 1;
7274 ret = 0; 6602 ret = 0;
7275 } 6603 }
@@ -7290,22 +6618,18 @@ int btrfs_set_block_group_ro(struct btrfs_root *root,
7290 BUG_ON(cache->ro); 6618 BUG_ON(cache->ro);
7291 6619
7292 trans = btrfs_join_transaction(root); 6620 trans = btrfs_join_transaction(root);
7293 if (IS_ERR(trans)) 6621 BUG_ON(IS_ERR(trans));
7294 return PTR_ERR(trans);
7295 6622
7296 alloc_flags = update_block_group_flags(root, cache->flags); 6623 alloc_flags = update_block_group_flags(root, cache->flags);
7297 if (alloc_flags != cache->flags) { 6624 if (alloc_flags != cache->flags)
7298 ret = do_chunk_alloc(trans, root, alloc_flags, 6625 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
7299 CHUNK_ALLOC_FORCE); 6626 CHUNK_ALLOC_FORCE);
7300 if (ret < 0)
7301 goto out;
7302 }
7303 6627
7304 ret = set_block_group_ro(cache, 0); 6628 ret = set_block_group_ro(cache, 0);
7305 if (!ret) 6629 if (!ret)
7306 goto out; 6630 goto out;
7307 alloc_flags = get_alloc_profile(root, cache->space_info->flags); 6631 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7308 ret = do_chunk_alloc(trans, root, alloc_flags, 6632 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
7309 CHUNK_ALLOC_FORCE); 6633 CHUNK_ALLOC_FORCE);
7310 if (ret < 0) 6634 if (ret < 0)
7311 goto out; 6635 goto out;
@@ -7319,7 +6643,7 @@ int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
7319 struct btrfs_root *root, u64 type) 6643 struct btrfs_root *root, u64 type)
7320{ 6644{
7321 u64 alloc_flags = get_alloc_profile(root, type); 6645 u64 alloc_flags = get_alloc_profile(root, type);
7322 return do_chunk_alloc(trans, root, alloc_flags, 6646 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
7323 CHUNK_ALLOC_FORCE); 6647 CHUNK_ALLOC_FORCE);
7324} 6648}
7325 6649
@@ -7379,7 +6703,7 @@ u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
7379 return free_bytes; 6703 return free_bytes;
7380} 6704}
7381 6705
7382void btrfs_set_block_group_rw(struct btrfs_root *root, 6706int btrfs_set_block_group_rw(struct btrfs_root *root,
7383 struct btrfs_block_group_cache *cache) 6707 struct btrfs_block_group_cache *cache)
7384{ 6708{
7385 struct btrfs_space_info *sinfo = cache->space_info; 6709 struct btrfs_space_info *sinfo = cache->space_info;
@@ -7395,6 +6719,7 @@ void btrfs_set_block_group_rw(struct btrfs_root *root,
7395 cache->ro = 0; 6719 cache->ro = 0;
7396 spin_unlock(&cache->lock); 6720 spin_unlock(&cache->lock);
7397 spin_unlock(&sinfo->lock); 6721 spin_unlock(&sinfo->lock);
6722 return 0;
7398} 6723}
7399 6724
7400/* 6725/*
@@ -7412,7 +6737,6 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7412 u64 min_free; 6737 u64 min_free;
7413 u64 dev_min = 1; 6738 u64 dev_min = 1;
7414 u64 dev_nr = 0; 6739 u64 dev_nr = 0;
7415 u64 target;
7416 int index; 6740 int index;
7417 int full = 0; 6741 int full = 0;
7418 int ret = 0; 6742 int ret = 0;
@@ -7453,11 +6777,13 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7453 /* 6777 /*
7454 * ok we don't have enough space, but maybe we have free space on our 6778 * ok we don't have enough space, but maybe we have free space on our
7455 * devices to allocate new chunks for relocation, so loop through our 6779 * devices to allocate new chunks for relocation, so loop through our
7456 * alloc devices and guess if we have enough space. if this block 6780 * alloc devices and guess if we have enough space. However, if we
7457 * group is going to be restriped, run checks against the target 6781 * were marked as full, then we know there aren't enough chunks, and we
7458 * profile instead of the current one. 6782 * can just return.
7459 */ 6783 */
7460 ret = -1; 6784 ret = -1;
6785 if (full)
6786 goto out;
7461 6787
7462 /* 6788 /*
7463 * index: 6789 * index:
@@ -7467,20 +6793,7 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7467 * 3: raid0 6793 * 3: raid0
7468 * 4: single 6794 * 4: single
7469 */ 6795 */
7470 target = get_restripe_target(root->fs_info, block_group->flags); 6796 index = get_block_group_index(block_group);
7471 if (target) {
7472 index = __get_raid_index(extended_to_chunk(target));
7473 } else {
7474 /*
7475 * this is just a balance, so if we were marked as full
7476 * we know there is no space for a new chunk
7477 */
7478 if (full)
7479 goto out;
7480
7481 index = get_block_group_index(block_group);
7482 }
7483
7484 if (index == 0) { 6797 if (index == 0) {
7485 dev_min = 4; 6798 dev_min = 4;
7486 /* Divide by 2 */ 6799 /* Divide by 2 */
@@ -7503,9 +6816,8 @@ int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7503 * check to make sure we can actually find a chunk with enough 6816 * check to make sure we can actually find a chunk with enough
7504 * space to fit our block group in. 6817 * space to fit our block group in.
7505 */ 6818 */
7506 if (device->total_bytes > device->bytes_used + min_free && 6819 if (device->total_bytes > device->bytes_used + min_free) {
7507 !device->is_tgtdev_for_dev_replace) { 6820 ret = find_free_dev_extent(NULL, device, min_free,
7508 ret = find_free_dev_extent(device, min_free,
7509 &dev_offset, NULL); 6821 &dev_offset, NULL);
7510 if (!ret) 6822 if (!ret)
7511 dev_nr++; 6823 dev_nr++;
@@ -7652,8 +6964,7 @@ int btrfs_free_block_groups(struct btrfs_fs_info *info)
7652 struct btrfs_space_info, 6964 struct btrfs_space_info,
7653 list); 6965 list);
7654 if (space_info->bytes_pinned > 0 || 6966 if (space_info->bytes_pinned > 0 ||
7655 space_info->bytes_reserved > 0 || 6967 space_info->bytes_reserved > 0) {
7656 space_info->bytes_may_use > 0) {
7657 WARN_ON(1); 6968 WARN_ON(1);
7658 dump_space_info(space_info, 0, 0); 6969 dump_space_info(space_info, 0, 0);
7659 } 6970 }
@@ -7695,12 +7006,14 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7695 return -ENOMEM; 7006 return -ENOMEM;
7696 path->reada = 1; 7007 path->reada = 1;
7697 7008
7698 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); 7009 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
7699 if (btrfs_test_opt(root, SPACE_CACHE) && 7010 if (cache_gen != 0 &&
7700 btrfs_super_generation(root->fs_info->super_copy) != cache_gen) 7011 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
7701 need_clear = 1; 7012 need_clear = 1;
7702 if (btrfs_test_opt(root, CLEAR_CACHE)) 7013 if (btrfs_test_opt(root, CLEAR_CACHE))
7703 need_clear = 1; 7014 need_clear = 1;
7015 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
7016 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
7704 7017
7705 while (1) { 7018 while (1) {
7706 ret = find_first_block_group(root, path, &key); 7019 ret = find_first_block_group(root, path, &key);
@@ -7729,21 +7042,8 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7729 INIT_LIST_HEAD(&cache->list); 7042 INIT_LIST_HEAD(&cache->list);
7730 INIT_LIST_HEAD(&cache->cluster_list); 7043 INIT_LIST_HEAD(&cache->cluster_list);
7731 7044
7732 if (need_clear) { 7045 if (need_clear)
7733 /*
7734 * When we mount with old space cache, we need to
7735 * set BTRFS_DC_CLEAR and set dirty flag.
7736 *
7737 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
7738 * truncate the old free space cache inode and
7739 * setup a new one.
7740 * b) Setting 'dirty flag' makes sure that we flush
7741 * the new space cache info onto disk.
7742 */
7743 cache->disk_cache_state = BTRFS_DC_CLEAR; 7046 cache->disk_cache_state = BTRFS_DC_CLEAR;
7744 if (btrfs_test_opt(root, SPACE_CACHE))
7745 cache->dirty = 1;
7746 }
7747 7047
7748 read_extent_buffer(leaf, &cache->item, 7048 read_extent_buffer(leaf, &cache->item,
7749 btrfs_item_ptr_offset(leaf, path->slots[0]), 7049 btrfs_item_ptr_offset(leaf, path->slots[0]),
@@ -7788,7 +7088,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7788 ret = update_space_info(info, cache->flags, found_key.offset, 7088 ret = update_space_info(info, cache->flags, found_key.offset,
7789 btrfs_block_group_used(&cache->item), 7089 btrfs_block_group_used(&cache->item),
7790 &space_info); 7090 &space_info);
7791 BUG_ON(ret); /* -ENOMEM */ 7091 BUG_ON(ret);
7792 cache->space_info = space_info; 7092 cache->space_info = space_info;
7793 spin_lock(&cache->space_info->lock); 7093 spin_lock(&cache->space_info->lock);
7794 cache->space_info->bytes_readonly += cache->bytes_super; 7094 cache->space_info->bytes_readonly += cache->bytes_super;
@@ -7797,7 +7097,7 @@ int btrfs_read_block_groups(struct btrfs_root *root)
7797 __link_block_group(space_info, cache); 7097 __link_block_group(space_info, cache);
7798 7098
7799 ret = btrfs_add_block_group_cache(root->fs_info, cache); 7099 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7800 BUG_ON(ret); /* Logic error */ 7100 BUG_ON(ret);
7801 7101
7802 set_avail_alloc_bits(root->fs_info, cache->flags); 7102 set_avail_alloc_bits(root->fs_info, cache->flags);
7803 if (btrfs_chunk_readonly(root, cache->key.objectid)) 7103 if (btrfs_chunk_readonly(root, cache->key.objectid))
@@ -7827,34 +7127,6 @@ error:
7827 return ret; 7127 return ret;
7828} 7128}
7829 7129
7830void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
7831 struct btrfs_root *root)
7832{
7833 struct btrfs_block_group_cache *block_group, *tmp;
7834 struct btrfs_root *extent_root = root->fs_info->extent_root;
7835 struct btrfs_block_group_item item;
7836 struct btrfs_key key;
7837 int ret = 0;
7838
7839 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
7840 new_bg_list) {
7841 list_del_init(&block_group->new_bg_list);
7842
7843 if (ret)
7844 continue;
7845
7846 spin_lock(&block_group->lock);
7847 memcpy(&item, &block_group->item, sizeof(item));
7848 memcpy(&key, &block_group->key, sizeof(key));
7849 spin_unlock(&block_group->lock);
7850
7851 ret = btrfs_insert_item(trans, extent_root, &key, &item,
7852 sizeof(item));
7853 if (ret)
7854 btrfs_abort_transaction(trans, extent_root, ret);
7855 }
7856}
7857
7858int btrfs_make_block_group(struct btrfs_trans_handle *trans, 7130int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7859 struct btrfs_root *root, u64 bytes_used, 7131 struct btrfs_root *root, u64 bytes_used,
7860 u64 type, u64 chunk_objectid, u64 chunk_offset, 7132 u64 type, u64 chunk_objectid, u64 chunk_offset,
@@ -7888,7 +7160,6 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7888 spin_lock_init(&cache->lock); 7160 spin_lock_init(&cache->lock);
7889 INIT_LIST_HEAD(&cache->list); 7161 INIT_LIST_HEAD(&cache->list);
7890 INIT_LIST_HEAD(&cache->cluster_list); 7162 INIT_LIST_HEAD(&cache->cluster_list);
7891 INIT_LIST_HEAD(&cache->new_bg_list);
7892 7163
7893 btrfs_init_free_space_ctl(cache); 7164 btrfs_init_free_space_ctl(cache);
7894 7165
@@ -7908,8 +7179,7 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7908 7179
7909 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, 7180 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7910 &cache->space_info); 7181 &cache->space_info);
7911 BUG_ON(ret); /* -ENOMEM */ 7182 BUG_ON(ret);
7912 update_global_block_rsv(root->fs_info);
7913 7183
7914 spin_lock(&cache->space_info->lock); 7184 spin_lock(&cache->space_info->lock);
7915 cache->space_info->bytes_readonly += cache->bytes_super; 7185 cache->space_info->bytes_readonly += cache->bytes_super;
@@ -7918,28 +7188,17 @@ int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7918 __link_block_group(cache->space_info, cache); 7188 __link_block_group(cache->space_info, cache);
7919 7189
7920 ret = btrfs_add_block_group_cache(root->fs_info, cache); 7190 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7921 BUG_ON(ret); /* Logic error */ 7191 BUG_ON(ret);
7922 7192
7923 list_add_tail(&cache->new_bg_list, &trans->new_bgs); 7193 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7194 sizeof(cache->item));
7195 BUG_ON(ret);
7924 7196
7925 set_avail_alloc_bits(extent_root->fs_info, type); 7197 set_avail_alloc_bits(extent_root->fs_info, type);
7926 7198
7927 return 0; 7199 return 0;
7928} 7200}
7929 7201
7930static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
7931{
7932 u64 extra_flags = chunk_to_extended(flags) &
7933 BTRFS_EXTENDED_PROFILE_MASK;
7934
7935 if (flags & BTRFS_BLOCK_GROUP_DATA)
7936 fs_info->avail_data_alloc_bits &= ~extra_flags;
7937 if (flags & BTRFS_BLOCK_GROUP_METADATA)
7938 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
7939 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
7940 fs_info->avail_system_alloc_bits &= ~extra_flags;
7941}
7942
7943int btrfs_remove_block_group(struct btrfs_trans_handle *trans, 7202int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7944 struct btrfs_root *root, u64 group_start) 7203 struct btrfs_root *root, u64 group_start)
7945{ 7204{
@@ -7950,7 +7209,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7950 struct btrfs_key key; 7209 struct btrfs_key key;
7951 struct inode *inode; 7210 struct inode *inode;
7952 int ret; 7211 int ret;
7953 int index;
7954 int factor; 7212 int factor;
7955 7213
7956 root = root->fs_info->extent_root; 7214 root = root->fs_info->extent_root;
@@ -7966,7 +7224,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7966 free_excluded_extents(root, block_group); 7224 free_excluded_extents(root, block_group);
7967 7225
7968 memcpy(&key, &block_group->key, sizeof(key)); 7226 memcpy(&key, &block_group->key, sizeof(key));
7969 index = get_block_group_index(block_group);
7970 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | 7227 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7971 BTRFS_BLOCK_GROUP_RAID1 | 7228 BTRFS_BLOCK_GROUP_RAID1 |
7972 BTRFS_BLOCK_GROUP_RAID10)) 7229 BTRFS_BLOCK_GROUP_RAID10))
@@ -7995,13 +7252,10 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7995 goto out; 7252 goto out;
7996 } 7253 }
7997 7254
7998 inode = lookup_free_space_inode(tree_root, block_group, path); 7255 inode = lookup_free_space_inode(root, block_group, path);
7999 if (!IS_ERR(inode)) { 7256 if (!IS_ERR(inode)) {
8000 ret = btrfs_orphan_add(trans, inode); 7257 ret = btrfs_orphan_add(trans, inode);
8001 if (ret) { 7258 BUG_ON(ret);
8002 btrfs_add_delayed_iput(inode);
8003 goto out;
8004 }
8005 clear_nlink(inode); 7259 clear_nlink(inode);
8006 /* One for the block groups ref */ 7260 /* One for the block groups ref */
8007 spin_lock(&block_group->lock); 7261 spin_lock(&block_group->lock);
@@ -8014,7 +7268,7 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8014 spin_unlock(&block_group->lock); 7268 spin_unlock(&block_group->lock);
8015 } 7269 }
8016 /* One for our lookup ref */ 7270 /* One for our lookup ref */
8017 btrfs_add_delayed_iput(inode); 7271 iput(inode);
8018 } 7272 }
8019 7273
8020 key.objectid = BTRFS_FREE_SPACE_OBJECTID; 7274 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
@@ -8044,8 +7298,6 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8044 * are still on the list after taking the semaphore 7298 * are still on the list after taking the semaphore
8045 */ 7299 */
8046 list_del_init(&block_group->list); 7300 list_del_init(&block_group->list);
8047 if (list_empty(&block_group->space_info->block_groups[index]))
8048 clear_avail_alloc_bits(root->fs_info, block_group->flags);
8049 up_write(&block_group->space_info->groups_sem); 7301 up_write(&block_group->space_info->groups_sem);
8050 7302
8051 if (block_group->cached == BTRFS_CACHE_STARTED) 7303 if (block_group->cached == BTRFS_CACHE_STARTED)
@@ -8087,7 +7339,7 @@ int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8087 int mixed = 0; 7339 int mixed = 0;
8088 int ret; 7340 int ret;
8089 7341
8090 disk_super = fs_info->super_copy; 7342 disk_super = &fs_info->super_copy;
8091 if (!btrfs_super_root(disk_super)) 7343 if (!btrfs_super_root(disk_super))
8092 return 1; 7344 return 1;
8093 7345
@@ -8135,16 +7387,9 @@ int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8135 u64 start; 7387 u64 start;
8136 u64 end; 7388 u64 end;
8137 u64 trimmed = 0; 7389 u64 trimmed = 0;
8138 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
8139 int ret = 0; 7390 int ret = 0;
8140 7391
8141 /* 7392 cache = btrfs_lookup_block_group(fs_info, range->start);
8142 * try to trim all FS space, our block group may start from non-zero.
8143 */
8144 if (range->len == total_bytes)
8145 cache = btrfs_lookup_first_block_group(fs_info, range->start);
8146 else
8147 cache = btrfs_lookup_block_group(fs_info, range->start);
8148 7393
8149 while (cache) { 7394 while (cache) {
8150 if (cache->key.objectid >= (range->start + range->len)) { 7395 if (cache->key.objectid >= (range->start + range->len)) {