aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/btrfs/ctree.h13
-rw-r--r--fs/btrfs/extent-tree.c143
-rw-r--r--fs/btrfs/extent_io.h1
-rw-r--r--fs/btrfs/extent_map.c2
-rw-r--r--fs/btrfs/extent_map.h1
-rw-r--r--fs/btrfs/free-space-cache.c192
-rw-r--r--fs/btrfs/inode.c41
-rw-r--r--fs/btrfs/locking.c80
-rw-r--r--fs/btrfs/scrub.c19
-rw-r--r--fs/btrfs/volumes.c36
-rw-r--r--fs/btrfs/volumes.h3
11 files changed, 359 insertions, 172 deletions
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index b7e2c1c1ef36..be91397f4e92 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -1259,11 +1259,19 @@ struct btrfs_block_group_cache {
1259 spinlock_t lock; 1259 spinlock_t lock;
1260 u64 pinned; 1260 u64 pinned;
1261 u64 reserved; 1261 u64 reserved;
1262 u64 delalloc_bytes;
1262 u64 bytes_super; 1263 u64 bytes_super;
1263 u64 flags; 1264 u64 flags;
1264 u64 sectorsize; 1265 u64 sectorsize;
1265 u64 cache_generation; 1266 u64 cache_generation;
1266 1267
1268 /*
1269 * It is just used for the delayed data space allocation because
1270 * only the data space allocation and the relative metadata update
1271 * can be done cross the transaction.
1272 */
1273 struct rw_semaphore data_rwsem;
1274
1267 /* for raid56, this is a full stripe, without parity */ 1275 /* for raid56, this is a full stripe, without parity */
1268 unsigned long full_stripe_len; 1276 unsigned long full_stripe_len;
1269 1277
@@ -3316,7 +3324,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
3316 struct btrfs_key *ins); 3324 struct btrfs_key *ins);
3317int btrfs_reserve_extent(struct btrfs_root *root, u64 num_bytes, 3325int btrfs_reserve_extent(struct btrfs_root *root, u64 num_bytes,
3318 u64 min_alloc_size, u64 empty_size, u64 hint_byte, 3326 u64 min_alloc_size, u64 empty_size, u64 hint_byte,
3319 struct btrfs_key *ins, int is_data); 3327 struct btrfs_key *ins, int is_data, int delalloc);
3320int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3328int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3321 struct extent_buffer *buf, int full_backref, int no_quota); 3329 struct extent_buffer *buf, int full_backref, int no_quota);
3322int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, 3330int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
@@ -3330,7 +3338,8 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans,
3330 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, 3338 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
3331 u64 owner, u64 offset, int no_quota); 3339 u64 owner, u64 offset, int no_quota);
3332 3340
3333int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len); 3341int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len,
3342 int delalloc);
3334int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, 3343int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
3335 u64 start, u64 len); 3344 u64 start, u64 len);
3336void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, 3345void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index fafb3e53ecde..99c253918208 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -105,7 +105,8 @@ static int find_next_key(struct btrfs_path *path, int level,
105static void dump_space_info(struct btrfs_space_info *info, u64 bytes, 105static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups); 106 int dump_block_groups);
107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 107static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
108 u64 num_bytes, int reserve); 108 u64 num_bytes, int reserve,
109 int delalloc);
109static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, 110static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
110 u64 num_bytes); 111 u64 num_bytes);
111int btrfs_pin_extent(struct btrfs_root *root, 112int btrfs_pin_extent(struct btrfs_root *root,
@@ -3260,7 +3261,8 @@ again:
3260 3261
3261 spin_lock(&block_group->lock); 3262 spin_lock(&block_group->lock);
3262 if (block_group->cached != BTRFS_CACHE_FINISHED || 3263 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3263 !btrfs_test_opt(root, SPACE_CACHE)) { 3264 !btrfs_test_opt(root, SPACE_CACHE) ||
3265 block_group->delalloc_bytes) {
3264 /* 3266 /*
3265 * don't bother trying to write stuff out _if_ 3267 * don't bother trying to write stuff out _if_
3266 * a) we're not cached, 3268 * a) we're not cached,
@@ -5613,6 +5615,7 @@ int btrfs_exclude_logged_extents(struct btrfs_root *log,
5613 * @cache: The cache we are manipulating 5615 * @cache: The cache we are manipulating
5614 * @num_bytes: The number of bytes in question 5616 * @num_bytes: The number of bytes in question
5615 * @reserve: One of the reservation enums 5617 * @reserve: One of the reservation enums
5618 * @delalloc: The blocks are allocated for the delalloc write
5616 * 5619 *
5617 * This is called by the allocator when it reserves space, or by somebody who is 5620 * This is called by the allocator when it reserves space, or by somebody who is
5618 * freeing space that was never actually used on disk. For example if you 5621 * freeing space that was never actually used on disk. For example if you
@@ -5631,7 +5634,7 @@ int btrfs_exclude_logged_extents(struct btrfs_root *log,
5631 * succeeds. 5634 * succeeds.
5632 */ 5635 */
5633static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, 5636static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5634 u64 num_bytes, int reserve) 5637 u64 num_bytes, int reserve, int delalloc)
5635{ 5638{
5636 struct btrfs_space_info *space_info = cache->space_info; 5639 struct btrfs_space_info *space_info = cache->space_info;
5637 int ret = 0; 5640 int ret = 0;
@@ -5650,12 +5653,18 @@ static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5650 num_bytes, 0); 5653 num_bytes, 0);
5651 space_info->bytes_may_use -= num_bytes; 5654 space_info->bytes_may_use -= num_bytes;
5652 } 5655 }
5656
5657 if (delalloc)
5658 cache->delalloc_bytes += num_bytes;
5653 } 5659 }
5654 } else { 5660 } else {
5655 if (cache->ro) 5661 if (cache->ro)
5656 space_info->bytes_readonly += num_bytes; 5662 space_info->bytes_readonly += num_bytes;
5657 cache->reserved -= num_bytes; 5663 cache->reserved -= num_bytes;
5658 space_info->bytes_reserved -= num_bytes; 5664 space_info->bytes_reserved -= num_bytes;
5665
5666 if (delalloc)
5667 cache->delalloc_bytes -= num_bytes;
5659 } 5668 }
5660 spin_unlock(&cache->lock); 5669 spin_unlock(&cache->lock);
5661 spin_unlock(&space_info->lock); 5670 spin_unlock(&space_info->lock);
@@ -6206,7 +6215,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6206 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); 6215 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6207 6216
6208 btrfs_add_free_space(cache, buf->start, buf->len); 6217 btrfs_add_free_space(cache, buf->start, buf->len);
6209 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); 6218 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
6210 trace_btrfs_reserved_extent_free(root, buf->start, buf->len); 6219 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
6211 pin = 0; 6220 pin = 0;
6212 } 6221 }
@@ -6365,6 +6374,70 @@ enum btrfs_loop_type {
6365 LOOP_NO_EMPTY_SIZE = 3, 6374 LOOP_NO_EMPTY_SIZE = 3,
6366}; 6375};
6367 6376
6377static inline void
6378btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6379 int delalloc)
6380{
6381 if (delalloc)
6382 down_read(&cache->data_rwsem);
6383}
6384
6385static inline void
6386btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6387 int delalloc)
6388{
6389 btrfs_get_block_group(cache);
6390 if (delalloc)
6391 down_read(&cache->data_rwsem);
6392}
6393
6394static struct btrfs_block_group_cache *
6395btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6396 struct btrfs_free_cluster *cluster,
6397 int delalloc)
6398{
6399 struct btrfs_block_group_cache *used_bg;
6400 bool locked = false;
6401again:
6402 spin_lock(&cluster->refill_lock);
6403 if (locked) {
6404 if (used_bg == cluster->block_group)
6405 return used_bg;
6406
6407 up_read(&used_bg->data_rwsem);
6408 btrfs_put_block_group(used_bg);
6409 }
6410
6411 used_bg = cluster->block_group;
6412 if (!used_bg)
6413 return NULL;
6414
6415 if (used_bg == block_group)
6416 return used_bg;
6417
6418 btrfs_get_block_group(used_bg);
6419
6420 if (!delalloc)
6421 return used_bg;
6422
6423 if (down_read_trylock(&used_bg->data_rwsem))
6424 return used_bg;
6425
6426 spin_unlock(&cluster->refill_lock);
6427 down_read(&used_bg->data_rwsem);
6428 locked = true;
6429 goto again;
6430}
6431
6432static inline void
6433btrfs_release_block_group(struct btrfs_block_group_cache *cache,
6434 int delalloc)
6435{
6436 if (delalloc)
6437 up_read(&cache->data_rwsem);
6438 btrfs_put_block_group(cache);
6439}
6440
6368/* 6441/*
6369 * walks the btree of allocated extents and find a hole of a given size. 6442 * walks the btree of allocated extents and find a hole of a given size.
6370 * The key ins is changed to record the hole: 6443 * The key ins is changed to record the hole:
@@ -6379,7 +6452,7 @@ enum btrfs_loop_type {
6379static noinline int find_free_extent(struct btrfs_root *orig_root, 6452static noinline int find_free_extent(struct btrfs_root *orig_root,
6380 u64 num_bytes, u64 empty_size, 6453 u64 num_bytes, u64 empty_size,
6381 u64 hint_byte, struct btrfs_key *ins, 6454 u64 hint_byte, struct btrfs_key *ins,
6382 u64 flags) 6455 u64 flags, int delalloc)
6383{ 6456{
6384 int ret = 0; 6457 int ret = 0;
6385 struct btrfs_root *root = orig_root->fs_info->extent_root; 6458 struct btrfs_root *root = orig_root->fs_info->extent_root;
@@ -6467,6 +6540,7 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
6467 up_read(&space_info->groups_sem); 6540 up_read(&space_info->groups_sem);
6468 } else { 6541 } else {
6469 index = get_block_group_index(block_group); 6542 index = get_block_group_index(block_group);
6543 btrfs_lock_block_group(block_group, delalloc);
6470 goto have_block_group; 6544 goto have_block_group;
6471 } 6545 }
6472 } else if (block_group) { 6546 } else if (block_group) {
@@ -6481,7 +6555,7 @@ search:
6481 u64 offset; 6555 u64 offset;
6482 int cached; 6556 int cached;
6483 6557
6484 btrfs_get_block_group(block_group); 6558 btrfs_grab_block_group(block_group, delalloc);
6485 search_start = block_group->key.objectid; 6559 search_start = block_group->key.objectid;
6486 6560
6487 /* 6561 /*
@@ -6529,16 +6603,16 @@ have_block_group:
6529 * the refill lock keeps out other 6603 * the refill lock keeps out other
6530 * people trying to start a new cluster 6604 * people trying to start a new cluster
6531 */ 6605 */
6532 spin_lock(&last_ptr->refill_lock); 6606 used_block_group = btrfs_lock_cluster(block_group,
6533 used_block_group = last_ptr->block_group; 6607 last_ptr,
6534 if (used_block_group != block_group && 6608 delalloc);
6535 (!used_block_group || 6609 if (!used_block_group)
6536 used_block_group->ro ||
6537 !block_group_bits(used_block_group, flags)))
6538 goto refill_cluster; 6610 goto refill_cluster;
6539 6611
6540 if (used_block_group != block_group) 6612 if (used_block_group != block_group &&
6541 btrfs_get_block_group(used_block_group); 6613 (used_block_group->ro ||
6614 !block_group_bits(used_block_group, flags)))
6615 goto release_cluster;
6542 6616
6543 offset = btrfs_alloc_from_cluster(used_block_group, 6617 offset = btrfs_alloc_from_cluster(used_block_group,
6544 last_ptr, 6618 last_ptr,
@@ -6552,16 +6626,15 @@ have_block_group:
6552 used_block_group, 6626 used_block_group,
6553 search_start, num_bytes); 6627 search_start, num_bytes);
6554 if (used_block_group != block_group) { 6628 if (used_block_group != block_group) {
6555 btrfs_put_block_group(block_group); 6629 btrfs_release_block_group(block_group,
6630 delalloc);
6556 block_group = used_block_group; 6631 block_group = used_block_group;
6557 } 6632 }
6558 goto checks; 6633 goto checks;
6559 } 6634 }
6560 6635
6561 WARN_ON(last_ptr->block_group != used_block_group); 6636 WARN_ON(last_ptr->block_group != used_block_group);
6562 if (used_block_group != block_group) 6637release_cluster:
6563 btrfs_put_block_group(used_block_group);
6564refill_cluster:
6565 /* If we are on LOOP_NO_EMPTY_SIZE, we can't 6638 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6566 * set up a new clusters, so lets just skip it 6639 * set up a new clusters, so lets just skip it
6567 * and let the allocator find whatever block 6640 * and let the allocator find whatever block
@@ -6578,8 +6651,10 @@ refill_cluster:
6578 * succeeding in the unclustered 6651 * succeeding in the unclustered
6579 * allocation. */ 6652 * allocation. */
6580 if (loop >= LOOP_NO_EMPTY_SIZE && 6653 if (loop >= LOOP_NO_EMPTY_SIZE &&
6581 last_ptr->block_group != block_group) { 6654 used_block_group != block_group) {
6582 spin_unlock(&last_ptr->refill_lock); 6655 spin_unlock(&last_ptr->refill_lock);
6656 btrfs_release_block_group(used_block_group,
6657 delalloc);
6583 goto unclustered_alloc; 6658 goto unclustered_alloc;
6584 } 6659 }
6585 6660
@@ -6589,6 +6664,10 @@ refill_cluster:
6589 */ 6664 */
6590 btrfs_return_cluster_to_free_space(NULL, last_ptr); 6665 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6591 6666
6667 if (used_block_group != block_group)
6668 btrfs_release_block_group(used_block_group,
6669 delalloc);
6670refill_cluster:
6592 if (loop >= LOOP_NO_EMPTY_SIZE) { 6671 if (loop >= LOOP_NO_EMPTY_SIZE) {
6593 spin_unlock(&last_ptr->refill_lock); 6672 spin_unlock(&last_ptr->refill_lock);
6594 goto unclustered_alloc; 6673 goto unclustered_alloc;
@@ -6696,7 +6775,7 @@ checks:
6696 BUG_ON(offset > search_start); 6775 BUG_ON(offset > search_start);
6697 6776
6698 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 6777 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
6699 alloc_type); 6778 alloc_type, delalloc);
6700 if (ret == -EAGAIN) { 6779 if (ret == -EAGAIN) {
6701 btrfs_add_free_space(block_group, offset, num_bytes); 6780 btrfs_add_free_space(block_group, offset, num_bytes);
6702 goto loop; 6781 goto loop;
@@ -6708,13 +6787,13 @@ checks:
6708 6787
6709 trace_btrfs_reserve_extent(orig_root, block_group, 6788 trace_btrfs_reserve_extent(orig_root, block_group,
6710 search_start, num_bytes); 6789 search_start, num_bytes);
6711 btrfs_put_block_group(block_group); 6790 btrfs_release_block_group(block_group, delalloc);
6712 break; 6791 break;
6713loop: 6792loop:
6714 failed_cluster_refill = false; 6793 failed_cluster_refill = false;
6715 failed_alloc = false; 6794 failed_alloc = false;
6716 BUG_ON(index != get_block_group_index(block_group)); 6795 BUG_ON(index != get_block_group_index(block_group));
6717 btrfs_put_block_group(block_group); 6796 btrfs_release_block_group(block_group, delalloc);
6718 } 6797 }
6719 up_read(&space_info->groups_sem); 6798 up_read(&space_info->groups_sem);
6720 6799
@@ -6827,7 +6906,7 @@ again:
6827int btrfs_reserve_extent(struct btrfs_root *root, 6906int btrfs_reserve_extent(struct btrfs_root *root,
6828 u64 num_bytes, u64 min_alloc_size, 6907 u64 num_bytes, u64 min_alloc_size,
6829 u64 empty_size, u64 hint_byte, 6908 u64 empty_size, u64 hint_byte,
6830 struct btrfs_key *ins, int is_data) 6909 struct btrfs_key *ins, int is_data, int delalloc)
6831{ 6910{
6832 bool final_tried = false; 6911 bool final_tried = false;
6833 u64 flags; 6912 u64 flags;
@@ -6837,7 +6916,7 @@ int btrfs_reserve_extent(struct btrfs_root *root,
6837again: 6916again:
6838 WARN_ON(num_bytes < root->sectorsize); 6917 WARN_ON(num_bytes < root->sectorsize);
6839 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins, 6918 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6840 flags); 6919 flags, delalloc);
6841 6920
6842 if (ret == -ENOSPC) { 6921 if (ret == -ENOSPC) {
6843 if (!final_tried && ins->offset) { 6922 if (!final_tried && ins->offset) {
@@ -6862,7 +6941,8 @@ again:
6862} 6941}
6863 6942
6864static int __btrfs_free_reserved_extent(struct btrfs_root *root, 6943static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6865 u64 start, u64 len, int pin) 6944 u64 start, u64 len,
6945 int pin, int delalloc)
6866{ 6946{
6867 struct btrfs_block_group_cache *cache; 6947 struct btrfs_block_group_cache *cache;
6868 int ret = 0; 6948 int ret = 0;
@@ -6881,7 +6961,7 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6881 pin_down_extent(root, cache, start, len, 1); 6961 pin_down_extent(root, cache, start, len, 1);
6882 else { 6962 else {
6883 btrfs_add_free_space(cache, start, len); 6963 btrfs_add_free_space(cache, start, len);
6884 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); 6964 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
6885 } 6965 }
6886 btrfs_put_block_group(cache); 6966 btrfs_put_block_group(cache);
6887 6967
@@ -6891,15 +6971,15 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6891} 6971}
6892 6972
6893int btrfs_free_reserved_extent(struct btrfs_root *root, 6973int btrfs_free_reserved_extent(struct btrfs_root *root,
6894 u64 start, u64 len) 6974 u64 start, u64 len, int delalloc)
6895{ 6975{
6896 return __btrfs_free_reserved_extent(root, start, len, 0); 6976 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
6897} 6977}
6898 6978
6899int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, 6979int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6900 u64 start, u64 len) 6980 u64 start, u64 len)
6901{ 6981{
6902 return __btrfs_free_reserved_extent(root, start, len, 1); 6982 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
6903} 6983}
6904 6984
6905static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, 6985static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
@@ -7114,7 +7194,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7114 return -EINVAL; 7194 return -EINVAL;
7115 7195
7116 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 7196 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
7117 RESERVE_ALLOC_NO_ACCOUNT); 7197 RESERVE_ALLOC_NO_ACCOUNT, 0);
7118 BUG_ON(ret); /* logic error */ 7198 BUG_ON(ret); /* logic error */
7119 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, 7199 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7120 0, owner, offset, ins, 1); 7200 0, owner, offset, ins, 1);
@@ -7256,7 +7336,7 @@ struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
7256 return ERR_CAST(block_rsv); 7336 return ERR_CAST(block_rsv);
7257 7337
7258 ret = btrfs_reserve_extent(root, blocksize, blocksize, 7338 ret = btrfs_reserve_extent(root, blocksize, blocksize,
7259 empty_size, hint, &ins, 0); 7339 empty_size, hint, &ins, 0, 0);
7260 if (ret) { 7340 if (ret) {
7261 unuse_block_rsv(root->fs_info, block_rsv, blocksize); 7341 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
7262 return ERR_PTR(ret); 7342 return ERR_PTR(ret);
@@ -8659,6 +8739,7 @@ btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8659 start); 8739 start);
8660 atomic_set(&cache->count, 1); 8740 atomic_set(&cache->count, 1);
8661 spin_lock_init(&cache->lock); 8741 spin_lock_init(&cache->lock);
8742 init_rwsem(&cache->data_rwsem);
8662 INIT_LIST_HEAD(&cache->list); 8743 INIT_LIST_HEAD(&cache->list);
8663 INIT_LIST_HEAD(&cache->cluster_list); 8744 INIT_LIST_HEAD(&cache->cluster_list);
8664 INIT_LIST_HEAD(&cache->new_bg_list); 8745 INIT_LIST_HEAD(&cache->new_bg_list);
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 15ce5f2a2b62..ccc264e7bde1 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -158,7 +158,6 @@ struct extent_buffer {
158 * to unlock 158 * to unlock
159 */ 159 */
160 wait_queue_head_t read_lock_wq; 160 wait_queue_head_t read_lock_wq;
161 wait_queue_head_t lock_wq;
162 struct page *pages[INLINE_EXTENT_BUFFER_PAGES]; 161 struct page *pages[INLINE_EXTENT_BUFFER_PAGES];
163#ifdef CONFIG_BTRFS_DEBUG 162#ifdef CONFIG_BTRFS_DEBUG
164 struct list_head leak_list; 163 struct list_head leak_list;
diff --git a/fs/btrfs/extent_map.c b/fs/btrfs/extent_map.c
index 1874aee69c86..225302b39afb 100644
--- a/fs/btrfs/extent_map.c
+++ b/fs/btrfs/extent_map.c
@@ -75,6 +75,8 @@ void free_extent_map(struct extent_map *em)
75 if (atomic_dec_and_test(&em->refs)) { 75 if (atomic_dec_and_test(&em->refs)) {
76 WARN_ON(extent_map_in_tree(em)); 76 WARN_ON(extent_map_in_tree(em));
77 WARN_ON(!list_empty(&em->list)); 77 WARN_ON(!list_empty(&em->list));
78 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
79 kfree(em->bdev);
78 kmem_cache_free(extent_map_cache, em); 80 kmem_cache_free(extent_map_cache, em);
79 } 81 }
80} 82}
diff --git a/fs/btrfs/extent_map.h b/fs/btrfs/extent_map.h
index e7fd8a56a140..b2991fd8583e 100644
--- a/fs/btrfs/extent_map.h
+++ b/fs/btrfs/extent_map.h
@@ -15,6 +15,7 @@
15#define EXTENT_FLAG_PREALLOC 3 /* pre-allocated extent */ 15#define EXTENT_FLAG_PREALLOC 3 /* pre-allocated extent */
16#define EXTENT_FLAG_LOGGING 4 /* Logging this extent */ 16#define EXTENT_FLAG_LOGGING 4 /* Logging this extent */
17#define EXTENT_FLAG_FILLING 5 /* Filling in a preallocated extent */ 17#define EXTENT_FLAG_FILLING 5 /* Filling in a preallocated extent */
18#define EXTENT_FLAG_FS_MAPPING 6 /* filesystem extent mapping type */
18 19
19struct extent_map { 20struct extent_map {
20 struct rb_node rb_node; 21 struct rb_node rb_node;
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 372b05ff1943..2b0a627cb5f9 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -274,18 +274,32 @@ struct io_ctl {
274}; 274};
275 275
276static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode, 276static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
277 struct btrfs_root *root) 277 struct btrfs_root *root, int write)
278{ 278{
279 int num_pages;
280 int check_crcs = 0;
281
282 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
283 PAGE_CACHE_SHIFT;
284
285 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
286 check_crcs = 1;
287
288 /* Make sure we can fit our crcs into the first page */
289 if (write && check_crcs &&
290 (num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
291 return -ENOSPC;
292
279 memset(io_ctl, 0, sizeof(struct io_ctl)); 293 memset(io_ctl, 0, sizeof(struct io_ctl));
280 io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> 294
281 PAGE_CACHE_SHIFT; 295 io_ctl->pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
282 io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages,
283 GFP_NOFS);
284 if (!io_ctl->pages) 296 if (!io_ctl->pages)
285 return -ENOMEM; 297 return -ENOMEM;
298
299 io_ctl->num_pages = num_pages;
286 io_ctl->root = root; 300 io_ctl->root = root;
287 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID) 301 io_ctl->check_crcs = check_crcs;
288 io_ctl->check_crcs = 1; 302
289 return 0; 303 return 0;
290} 304}
291 305
@@ -666,6 +680,13 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
666 generation = btrfs_free_space_generation(leaf, header); 680 generation = btrfs_free_space_generation(leaf, header);
667 btrfs_release_path(path); 681 btrfs_release_path(path);
668 682
683 if (!BTRFS_I(inode)->generation) {
684 btrfs_info(root->fs_info,
685 "The free space cache file (%llu) is invalid. skip it\n",
686 offset);
687 return 0;
688 }
689
669 if (BTRFS_I(inode)->generation != generation) { 690 if (BTRFS_I(inode)->generation != generation) {
670 btrfs_err(root->fs_info, 691 btrfs_err(root->fs_info,
671 "free space inode generation (%llu) " 692 "free space inode generation (%llu) "
@@ -677,7 +698,7 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
677 if (!num_entries) 698 if (!num_entries)
678 return 0; 699 return 0;
679 700
680 ret = io_ctl_init(&io_ctl, inode, root); 701 ret = io_ctl_init(&io_ctl, inode, root, 0);
681 if (ret) 702 if (ret)
682 return ret; 703 return ret;
683 704
@@ -957,19 +978,18 @@ fail:
957} 978}
958 979
959static noinline_for_stack int 980static noinline_for_stack int
960add_ioctl_entries(struct btrfs_root *root, 981write_pinned_extent_entries(struct btrfs_root *root,
961 struct inode *inode, 982 struct btrfs_block_group_cache *block_group,
962 struct btrfs_block_group_cache *block_group, 983 struct io_ctl *io_ctl,
963 struct io_ctl *io_ctl, 984 int *entries)
964 struct extent_state **cached_state,
965 struct list_head *bitmap_list,
966 int *entries)
967{ 985{
968 u64 start, extent_start, extent_end, len; 986 u64 start, extent_start, extent_end, len;
969 struct list_head *pos, *n;
970 struct extent_io_tree *unpin = NULL; 987 struct extent_io_tree *unpin = NULL;
971 int ret; 988 int ret;
972 989
990 if (!block_group)
991 return 0;
992
973 /* 993 /*
974 * We want to add any pinned extents to our free space cache 994 * We want to add any pinned extents to our free space cache
975 * so we don't leak the space 995 * so we don't leak the space
@@ -979,23 +999,19 @@ add_ioctl_entries(struct btrfs_root *root,
979 */ 999 */
980 unpin = root->fs_info->pinned_extents; 1000 unpin = root->fs_info->pinned_extents;
981 1001
982 if (block_group) 1002 start = block_group->key.objectid;
983 start = block_group->key.objectid;
984 1003
985 while (block_group && (start < block_group->key.objectid + 1004 while (start < block_group->key.objectid + block_group->key.offset) {
986 block_group->key.offset)) {
987 ret = find_first_extent_bit(unpin, start, 1005 ret = find_first_extent_bit(unpin, start,
988 &extent_start, &extent_end, 1006 &extent_start, &extent_end,
989 EXTENT_DIRTY, NULL); 1007 EXTENT_DIRTY, NULL);
990 if (ret) { 1008 if (ret)
991 ret = 0; 1009 return 0;
992 break;
993 }
994 1010
995 /* This pinned extent is out of our range */ 1011 /* This pinned extent is out of our range */
996 if (extent_start >= block_group->key.objectid + 1012 if (extent_start >= block_group->key.objectid +
997 block_group->key.offset) 1013 block_group->key.offset)
998 break; 1014 return 0;
999 1015
1000 extent_start = max(extent_start, start); 1016 extent_start = max(extent_start, start);
1001 extent_end = min(block_group->key.objectid + 1017 extent_end = min(block_group->key.objectid +
@@ -1005,11 +1021,20 @@ add_ioctl_entries(struct btrfs_root *root,
1005 *entries += 1; 1021 *entries += 1;
1006 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL); 1022 ret = io_ctl_add_entry(io_ctl, extent_start, len, NULL);
1007 if (ret) 1023 if (ret)
1008 goto out_nospc; 1024 return -ENOSPC;
1009 1025
1010 start = extent_end; 1026 start = extent_end;
1011 } 1027 }
1012 1028
1029 return 0;
1030}
1031
1032static noinline_for_stack int
1033write_bitmap_entries(struct io_ctl *io_ctl, struct list_head *bitmap_list)
1034{
1035 struct list_head *pos, *n;
1036 int ret;
1037
1013 /* Write out the bitmaps */ 1038 /* Write out the bitmaps */
1014 list_for_each_safe(pos, n, bitmap_list) { 1039 list_for_each_safe(pos, n, bitmap_list) {
1015 struct btrfs_free_space *entry = 1040 struct btrfs_free_space *entry =
@@ -1017,36 +1042,24 @@ add_ioctl_entries(struct btrfs_root *root,
1017 1042
1018 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap); 1043 ret = io_ctl_add_bitmap(io_ctl, entry->bitmap);
1019 if (ret) 1044 if (ret)
1020 goto out_nospc; 1045 return -ENOSPC;
1021 list_del_init(&entry->list); 1046 list_del_init(&entry->list);
1022 } 1047 }
1023 1048
1024 /* Zero out the rest of the pages just to make sure */ 1049 return 0;
1025 io_ctl_zero_remaining_pages(io_ctl); 1050}
1026
1027 ret = btrfs_dirty_pages(root, inode, io_ctl->pages, io_ctl->num_pages,
1028 0, i_size_read(inode), cached_state);
1029 io_ctl_drop_pages(io_ctl);
1030 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1031 i_size_read(inode) - 1, cached_state, GFP_NOFS);
1032 1051
1033 if (ret) 1052static int flush_dirty_cache(struct inode *inode)
1034 goto fail; 1053{
1054 int ret;
1035 1055
1036 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1); 1056 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
1037 if (ret) { 1057 if (ret)
1038 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1, 1058 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
1039 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL, 1059 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
1040 GFP_NOFS); 1060 GFP_NOFS);
1041 goto fail;
1042 }
1043 return 0;
1044 1061
1045fail: 1062 return ret;
1046 return -1;
1047
1048out_nospc:
1049 return -ENOSPC;
1050} 1063}
1051 1064
1052static void noinline_for_stack 1065static void noinline_for_stack
@@ -1056,6 +1069,7 @@ cleanup_write_cache_enospc(struct inode *inode,
1056 struct list_head *bitmap_list) 1069 struct list_head *bitmap_list)
1057{ 1070{
1058 struct list_head *pos, *n; 1071 struct list_head *pos, *n;
1072
1059 list_for_each_safe(pos, n, bitmap_list) { 1073 list_for_each_safe(pos, n, bitmap_list) {
1060 struct btrfs_free_space *entry = 1074 struct btrfs_free_space *entry =
1061 list_entry(pos, struct btrfs_free_space, list); 1075 list_entry(pos, struct btrfs_free_space, list);
@@ -1088,64 +1102,104 @@ static int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
1088{ 1102{
1089 struct extent_state *cached_state = NULL; 1103 struct extent_state *cached_state = NULL;
1090 struct io_ctl io_ctl; 1104 struct io_ctl io_ctl;
1091 struct list_head bitmap_list; 1105 LIST_HEAD(bitmap_list);
1092 int entries = 0; 1106 int entries = 0;
1093 int bitmaps = 0; 1107 int bitmaps = 0;
1094 int ret; 1108 int ret;
1095 int err = -1;
1096
1097 INIT_LIST_HEAD(&bitmap_list);
1098 1109
1099 if (!i_size_read(inode)) 1110 if (!i_size_read(inode))
1100 return -1; 1111 return -1;
1101 1112
1102 ret = io_ctl_init(&io_ctl, inode, root); 1113 ret = io_ctl_init(&io_ctl, inode, root, 1);
1103 if (ret) 1114 if (ret)
1104 return -1; 1115 return -1;
1105 1116
1117 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA)) {
1118 down_write(&block_group->data_rwsem);
1119 spin_lock(&block_group->lock);
1120 if (block_group->delalloc_bytes) {
1121 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1122 spin_unlock(&block_group->lock);
1123 up_write(&block_group->data_rwsem);
1124 BTRFS_I(inode)->generation = 0;
1125 ret = 0;
1126 goto out;
1127 }
1128 spin_unlock(&block_group->lock);
1129 }
1130
1106 /* Lock all pages first so we can lock the extent safely. */ 1131 /* Lock all pages first so we can lock the extent safely. */
1107 io_ctl_prepare_pages(&io_ctl, inode, 0); 1132 io_ctl_prepare_pages(&io_ctl, inode, 0);
1108 1133
1109 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, 1134 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
1110 0, &cached_state); 1135 0, &cached_state);
1111 1136
1112
1113 /* Make sure we can fit our crcs into the first page */
1114 if (io_ctl.check_crcs &&
1115 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE)
1116 goto out_nospc;
1117
1118 io_ctl_set_generation(&io_ctl, trans->transid); 1137 io_ctl_set_generation(&io_ctl, trans->transid);
1119 1138
1139 /* Write out the extent entries in the free space cache */
1120 ret = write_cache_extent_entries(&io_ctl, ctl, 1140 ret = write_cache_extent_entries(&io_ctl, ctl,
1121 block_group, &entries, &bitmaps, 1141 block_group, &entries, &bitmaps,
1122 &bitmap_list); 1142 &bitmap_list);
1123 if (ret) 1143 if (ret)
1124 goto out_nospc; 1144 goto out_nospc;
1125 1145
1126 ret = add_ioctl_entries(root, inode, block_group, &io_ctl, 1146 /*
1127 &cached_state, &bitmap_list, &entries); 1147 * Some spaces that are freed in the current transaction are pinned,
1148 * they will be added into free space cache after the transaction is
1149 * committed, we shouldn't lose them.
1150 */
1151 ret = write_pinned_extent_entries(root, block_group, &io_ctl, &entries);
1152 if (ret)
1153 goto out_nospc;
1128 1154
1129 if (ret == -ENOSPC) 1155 /* At last, we write out all the bitmaps. */
1156 ret = write_bitmap_entries(&io_ctl, &bitmap_list);
1157 if (ret)
1130 goto out_nospc; 1158 goto out_nospc;
1131 else if (ret) 1159
1160 /* Zero out the rest of the pages just to make sure */
1161 io_ctl_zero_remaining_pages(&io_ctl);
1162
1163 /* Everything is written out, now we dirty the pages in the file. */
1164 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
1165 0, i_size_read(inode), &cached_state);
1166 if (ret)
1167 goto out_nospc;
1168
1169 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1170 up_write(&block_group->data_rwsem);
1171 /*
1172 * Release the pages and unlock the extent, we will flush
1173 * them out later
1174 */
1175 io_ctl_drop_pages(&io_ctl);
1176
1177 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1178 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1179
1180 /* Flush the dirty pages in the cache file. */
1181 ret = flush_dirty_cache(inode);
1182 if (ret)
1132 goto out; 1183 goto out;
1133 1184
1134 err = update_cache_item(trans, root, inode, path, offset, 1185 /* Update the cache item to tell everyone this cache file is valid. */
1186 ret = update_cache_item(trans, root, inode, path, offset,
1135 entries, bitmaps); 1187 entries, bitmaps);
1136
1137out: 1188out:
1138 io_ctl_free(&io_ctl); 1189 io_ctl_free(&io_ctl);
1139 if (err) { 1190 if (ret) {
1140 invalidate_inode_pages2(inode->i_mapping); 1191 invalidate_inode_pages2(inode->i_mapping);
1141 BTRFS_I(inode)->generation = 0; 1192 BTRFS_I(inode)->generation = 0;
1142 } 1193 }
1143 btrfs_update_inode(trans, root, inode); 1194 btrfs_update_inode(trans, root, inode);
1144 return err; 1195 return ret;
1145 1196
1146out_nospc: 1197out_nospc:
1147
1148 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list); 1198 cleanup_write_cache_enospc(inode, &io_ctl, &cached_state, &bitmap_list);
1199
1200 if (block_group && (block_group->flags & BTRFS_BLOCK_GROUP_DATA))
1201 up_write(&block_group->data_rwsem);
1202
1149 goto out; 1203 goto out;
1150} 1204}
1151 1205
@@ -1165,6 +1219,12 @@ int btrfs_write_out_cache(struct btrfs_root *root,
1165 spin_unlock(&block_group->lock); 1219 spin_unlock(&block_group->lock);
1166 return 0; 1220 return 0;
1167 } 1221 }
1222
1223 if (block_group->delalloc_bytes) {
1224 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
1225 spin_unlock(&block_group->lock);
1226 return 0;
1227 }
1168 spin_unlock(&block_group->lock); 1228 spin_unlock(&block_group->lock);
1169 1229
1170 inode = lookup_free_space_inode(root, block_group, path); 1230 inode = lookup_free_space_inode(root, block_group, path);
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 8925f66a1411..3668048e16f8 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -693,7 +693,7 @@ retry:
693 ret = btrfs_reserve_extent(root, 693 ret = btrfs_reserve_extent(root,
694 async_extent->compressed_size, 694 async_extent->compressed_size,
695 async_extent->compressed_size, 695 async_extent->compressed_size,
696 0, alloc_hint, &ins, 1); 696 0, alloc_hint, &ins, 1, 1);
697 if (ret) { 697 if (ret) {
698 int i; 698 int i;
699 699
@@ -794,7 +794,7 @@ retry:
794out: 794out:
795 return ret; 795 return ret;
796out_free_reserve: 796out_free_reserve:
797 btrfs_free_reserved_extent(root, ins.objectid, ins.offset); 797 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
798out_free: 798out_free:
799 extent_clear_unlock_delalloc(inode, async_extent->start, 799 extent_clear_unlock_delalloc(inode, async_extent->start,
800 async_extent->start + 800 async_extent->start +
@@ -917,7 +917,7 @@ static noinline int cow_file_range(struct inode *inode,
917 cur_alloc_size = disk_num_bytes; 917 cur_alloc_size = disk_num_bytes;
918 ret = btrfs_reserve_extent(root, cur_alloc_size, 918 ret = btrfs_reserve_extent(root, cur_alloc_size,
919 root->sectorsize, 0, alloc_hint, 919 root->sectorsize, 0, alloc_hint,
920 &ins, 1); 920 &ins, 1, 1);
921 if (ret < 0) 921 if (ret < 0)
922 goto out_unlock; 922 goto out_unlock;
923 923
@@ -995,7 +995,7 @@ out:
995 return ret; 995 return ret;
996 996
997out_reserve: 997out_reserve:
998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset); 998 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
999out_unlock: 999out_unlock:
1000 extent_clear_unlock_delalloc(inode, start, end, locked_page, 1000 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING | 1001 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
@@ -2599,6 +2599,21 @@ out_kfree:
2599 return NULL; 2599 return NULL;
2600} 2600}
2601 2601
2602static void btrfs_release_delalloc_bytes(struct btrfs_root *root,
2603 u64 start, u64 len)
2604{
2605 struct btrfs_block_group_cache *cache;
2606
2607 cache = btrfs_lookup_block_group(root->fs_info, start);
2608 ASSERT(cache);
2609
2610 spin_lock(&cache->lock);
2611 cache->delalloc_bytes -= len;
2612 spin_unlock(&cache->lock);
2613
2614 btrfs_put_block_group(cache);
2615}
2616
2602/* as ordered data IO finishes, this gets called so we can finish 2617/* as ordered data IO finishes, this gets called so we can finish
2603 * an ordered extent if the range of bytes in the file it covers are 2618 * an ordered extent if the range of bytes in the file it covers are
2604 * fully written. 2619 * fully written.
@@ -2698,6 +2713,10 @@ static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2698 logical_len, logical_len, 2713 logical_len, logical_len,
2699 compress_type, 0, 0, 2714 compress_type, 0, 0,
2700 BTRFS_FILE_EXTENT_REG); 2715 BTRFS_FILE_EXTENT_REG);
2716 if (!ret)
2717 btrfs_release_delalloc_bytes(root,
2718 ordered_extent->start,
2719 ordered_extent->disk_len);
2701 } 2720 }
2702 unpin_extent_cache(&BTRFS_I(inode)->extent_tree, 2721 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2703 ordered_extent->file_offset, ordered_extent->len, 2722 ordered_extent->file_offset, ordered_extent->len,
@@ -2750,7 +2769,7 @@ out:
2750 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && 2769 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2751 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) 2770 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2752 btrfs_free_reserved_extent(root, ordered_extent->start, 2771 btrfs_free_reserved_extent(root, ordered_extent->start,
2753 ordered_extent->disk_len); 2772 ordered_extent->disk_len, 1);
2754 } 2773 }
2755 2774
2756 2775
@@ -6535,21 +6554,21 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6535 6554
6536 alloc_hint = get_extent_allocation_hint(inode, start, len); 6555 alloc_hint = get_extent_allocation_hint(inode, start, len);
6537 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0, 6556 ret = btrfs_reserve_extent(root, len, root->sectorsize, 0,
6538 alloc_hint, &ins, 1); 6557 alloc_hint, &ins, 1, 1);
6539 if (ret) 6558 if (ret)
6540 return ERR_PTR(ret); 6559 return ERR_PTR(ret);
6541 6560
6542 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid, 6561 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
6543 ins.offset, ins.offset, ins.offset, 0); 6562 ins.offset, ins.offset, ins.offset, 0);
6544 if (IS_ERR(em)) { 6563 if (IS_ERR(em)) {
6545 btrfs_free_reserved_extent(root, ins.objectid, ins.offset); 6564 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
6546 return em; 6565 return em;
6547 } 6566 }
6548 6567
6549 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid, 6568 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
6550 ins.offset, ins.offset, 0); 6569 ins.offset, ins.offset, 0);
6551 if (ret) { 6570 if (ret) {
6552 btrfs_free_reserved_extent(root, ins.objectid, ins.offset); 6571 btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 1);
6553 free_extent_map(em); 6572 free_extent_map(em);
6554 return ERR_PTR(ret); 6573 return ERR_PTR(ret);
6555 } 6574 }
@@ -7437,7 +7456,7 @@ free_ordered:
7437 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) && 7456 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
7438 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) 7457 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
7439 btrfs_free_reserved_extent(root, ordered->start, 7458 btrfs_free_reserved_extent(root, ordered->start,
7440 ordered->disk_len); 7459 ordered->disk_len, 1);
7441 btrfs_put_ordered_extent(ordered); 7460 btrfs_put_ordered_extent(ordered);
7442 btrfs_put_ordered_extent(ordered); 7461 btrfs_put_ordered_extent(ordered);
7443 } 7462 }
@@ -8808,7 +8827,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8808 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024); 8827 cur_bytes = min(num_bytes, 256ULL * 1024 * 1024);
8809 cur_bytes = max(cur_bytes, min_size); 8828 cur_bytes = max(cur_bytes, min_size);
8810 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0, 8829 ret = btrfs_reserve_extent(root, cur_bytes, min_size, 0,
8811 *alloc_hint, &ins, 1); 8830 *alloc_hint, &ins, 1, 0);
8812 if (ret) { 8831 if (ret) {
8813 if (own_trans) 8832 if (own_trans)
8814 btrfs_end_transaction(trans, root); 8833 btrfs_end_transaction(trans, root);
@@ -8822,7 +8841,7 @@ static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
8822 BTRFS_FILE_EXTENT_PREALLOC); 8841 BTRFS_FILE_EXTENT_PREALLOC);
8823 if (ret) { 8842 if (ret) {
8824 btrfs_free_reserved_extent(root, ins.objectid, 8843 btrfs_free_reserved_extent(root, ins.objectid,
8825 ins.offset); 8844 ins.offset, 0);
8826 btrfs_abort_transaction(trans, root, ret); 8845 btrfs_abort_transaction(trans, root, ret);
8827 if (own_trans) 8846 if (own_trans)
8828 btrfs_end_transaction(trans, root); 8847 btrfs_end_transaction(trans, root);
diff --git a/fs/btrfs/locking.c b/fs/btrfs/locking.c
index 01277b8f2373..5665d2149249 100644
--- a/fs/btrfs/locking.c
+++ b/fs/btrfs/locking.c
@@ -33,14 +33,14 @@ static void btrfs_assert_tree_read_locked(struct extent_buffer *eb);
33 */ 33 */
34void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw) 34void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw)
35{ 35{
36 if (eb->lock_nested) { 36 /*
37 read_lock(&eb->lock); 37 * no lock is required. The lock owner may change if
38 if (eb->lock_nested && current->pid == eb->lock_owner) { 38 * we have a read lock, but it won't change to or away
39 read_unlock(&eb->lock); 39 * from us. If we have the write lock, we are the owner
40 return; 40 * and it'll never change.
41 } 41 */
42 read_unlock(&eb->lock); 42 if (eb->lock_nested && current->pid == eb->lock_owner)
43 } 43 return;
44 if (rw == BTRFS_WRITE_LOCK) { 44 if (rw == BTRFS_WRITE_LOCK) {
45 if (atomic_read(&eb->blocking_writers) == 0) { 45 if (atomic_read(&eb->blocking_writers) == 0) {
46 WARN_ON(atomic_read(&eb->spinning_writers) != 1); 46 WARN_ON(atomic_read(&eb->spinning_writers) != 1);
@@ -65,14 +65,15 @@ void btrfs_set_lock_blocking_rw(struct extent_buffer *eb, int rw)
65 */ 65 */
66void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw) 66void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
67{ 67{
68 if (eb->lock_nested) { 68 /*
69 read_lock(&eb->lock); 69 * no lock is required. The lock owner may change if
70 if (eb->lock_nested && current->pid == eb->lock_owner) { 70 * we have a read lock, but it won't change to or away
71 read_unlock(&eb->lock); 71 * from us. If we have the write lock, we are the owner
72 return; 72 * and it'll never change.
73 } 73 */
74 read_unlock(&eb->lock); 74 if (eb->lock_nested && current->pid == eb->lock_owner)
75 } 75 return;
76
76 if (rw == BTRFS_WRITE_LOCK_BLOCKING) { 77 if (rw == BTRFS_WRITE_LOCK_BLOCKING) {
77 BUG_ON(atomic_read(&eb->blocking_writers) != 1); 78 BUG_ON(atomic_read(&eb->blocking_writers) != 1);
78 write_lock(&eb->lock); 79 write_lock(&eb->lock);
@@ -99,6 +100,9 @@ void btrfs_clear_lock_blocking_rw(struct extent_buffer *eb, int rw)
99void btrfs_tree_read_lock(struct extent_buffer *eb) 100void btrfs_tree_read_lock(struct extent_buffer *eb)
100{ 101{
101again: 102again:
103 BUG_ON(!atomic_read(&eb->blocking_writers) &&
104 current->pid == eb->lock_owner);
105
102 read_lock(&eb->lock); 106 read_lock(&eb->lock);
103 if (atomic_read(&eb->blocking_writers) && 107 if (atomic_read(&eb->blocking_writers) &&
104 current->pid == eb->lock_owner) { 108 current->pid == eb->lock_owner) {
@@ -132,7 +136,9 @@ int btrfs_try_tree_read_lock(struct extent_buffer *eb)
132 if (atomic_read(&eb->blocking_writers)) 136 if (atomic_read(&eb->blocking_writers))
133 return 0; 137 return 0;
134 138
135 read_lock(&eb->lock); 139 if (!read_trylock(&eb->lock))
140 return 0;
141
136 if (atomic_read(&eb->blocking_writers)) { 142 if (atomic_read(&eb->blocking_writers)) {
137 read_unlock(&eb->lock); 143 read_unlock(&eb->lock);
138 return 0; 144 return 0;
@@ -151,7 +157,10 @@ int btrfs_try_tree_write_lock(struct extent_buffer *eb)
151 if (atomic_read(&eb->blocking_writers) || 157 if (atomic_read(&eb->blocking_writers) ||
152 atomic_read(&eb->blocking_readers)) 158 atomic_read(&eb->blocking_readers))
153 return 0; 159 return 0;
154 write_lock(&eb->lock); 160
161 if (!write_trylock(&eb->lock))
162 return 0;
163
155 if (atomic_read(&eb->blocking_writers) || 164 if (atomic_read(&eb->blocking_writers) ||
156 atomic_read(&eb->blocking_readers)) { 165 atomic_read(&eb->blocking_readers)) {
157 write_unlock(&eb->lock); 166 write_unlock(&eb->lock);
@@ -168,14 +177,15 @@ int btrfs_try_tree_write_lock(struct extent_buffer *eb)
168 */ 177 */
169void btrfs_tree_read_unlock(struct extent_buffer *eb) 178void btrfs_tree_read_unlock(struct extent_buffer *eb)
170{ 179{
171 if (eb->lock_nested) { 180 /*
172 read_lock(&eb->lock); 181 * if we're nested, we have the write lock. No new locking
173 if (eb->lock_nested && current->pid == eb->lock_owner) { 182 * is needed as long as we are the lock owner.
174 eb->lock_nested = 0; 183 * The write unlock will do a barrier for us, and the lock_nested
175 read_unlock(&eb->lock); 184 * field only matters to the lock owner.
176 return; 185 */
177 } 186 if (eb->lock_nested && current->pid == eb->lock_owner) {
178 read_unlock(&eb->lock); 187 eb->lock_nested = 0;
188 return;
179 } 189 }
180 btrfs_assert_tree_read_locked(eb); 190 btrfs_assert_tree_read_locked(eb);
181 WARN_ON(atomic_read(&eb->spinning_readers) == 0); 191 WARN_ON(atomic_read(&eb->spinning_readers) == 0);
@@ -189,14 +199,15 @@ void btrfs_tree_read_unlock(struct extent_buffer *eb)
189 */ 199 */
190void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb) 200void btrfs_tree_read_unlock_blocking(struct extent_buffer *eb)
191{ 201{
192 if (eb->lock_nested) { 202 /*
193 read_lock(&eb->lock); 203 * if we're nested, we have the write lock. No new locking
194 if (eb->lock_nested && current->pid == eb->lock_owner) { 204 * is needed as long as we are the lock owner.
195 eb->lock_nested = 0; 205 * The write unlock will do a barrier for us, and the lock_nested
196 read_unlock(&eb->lock); 206 * field only matters to the lock owner.
197 return; 207 */
198 } 208 if (eb->lock_nested && current->pid == eb->lock_owner) {
199 read_unlock(&eb->lock); 209 eb->lock_nested = 0;
210 return;
200 } 211 }
201 btrfs_assert_tree_read_locked(eb); 212 btrfs_assert_tree_read_locked(eb);
202 WARN_ON(atomic_read(&eb->blocking_readers) == 0); 213 WARN_ON(atomic_read(&eb->blocking_readers) == 0);
@@ -244,6 +255,7 @@ void btrfs_tree_unlock(struct extent_buffer *eb)
244 BUG_ON(blockers > 1); 255 BUG_ON(blockers > 1);
245 256
246 btrfs_assert_tree_locked(eb); 257 btrfs_assert_tree_locked(eb);
258 eb->lock_owner = 0;
247 atomic_dec(&eb->write_locks); 259 atomic_dec(&eb->write_locks);
248 260
249 if (blockers) { 261 if (blockers) {
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index ac80188eec88..b6d198f5181e 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -2725,11 +2725,8 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2725 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent); 2725 dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
2726 length = btrfs_dev_extent_length(l, dev_extent); 2726 length = btrfs_dev_extent_length(l, dev_extent);
2727 2727
2728 if (found_key.offset + length <= start) { 2728 if (found_key.offset + length <= start)
2729 key.offset = found_key.offset + length; 2729 goto skip;
2730 btrfs_release_path(path);
2731 continue;
2732 }
2733 2730
2734 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent); 2731 chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
2735 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent); 2732 chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
@@ -2740,10 +2737,12 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2740 * the chunk from going away while we scrub it 2737 * the chunk from going away while we scrub it
2741 */ 2738 */
2742 cache = btrfs_lookup_block_group(fs_info, chunk_offset); 2739 cache = btrfs_lookup_block_group(fs_info, chunk_offset);
2743 if (!cache) { 2740
2744 ret = -ENOENT; 2741 /* some chunks are removed but not committed to disk yet,
2745 break; 2742 * continue scrubbing */
2746 } 2743 if (!cache)
2744 goto skip;
2745
2747 dev_replace->cursor_right = found_key.offset + length; 2746 dev_replace->cursor_right = found_key.offset + length;
2748 dev_replace->cursor_left = found_key.offset; 2747 dev_replace->cursor_left = found_key.offset;
2749 dev_replace->item_needs_writeback = 1; 2748 dev_replace->item_needs_writeback = 1;
@@ -2802,7 +2801,7 @@ int scrub_enumerate_chunks(struct scrub_ctx *sctx,
2802 2801
2803 dev_replace->cursor_left = dev_replace->cursor_right; 2802 dev_replace->cursor_left = dev_replace->cursor_right;
2804 dev_replace->item_needs_writeback = 1; 2803 dev_replace->item_needs_writeback = 1;
2805 2804skip:
2806 key.offset = found_key.offset + length; 2805 key.offset = found_key.offset + length;
2807 btrfs_release_path(path); 2806 btrfs_release_path(path);
2808 } 2807 }
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index ffeed6d6326f..c83b24251e53 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -2543,9 +2543,6 @@ static int btrfs_relocate_chunk(struct btrfs_root *root,
2543 remove_extent_mapping(em_tree, em); 2543 remove_extent_mapping(em_tree, em);
2544 write_unlock(&em_tree->lock); 2544 write_unlock(&em_tree->lock);
2545 2545
2546 kfree(map);
2547 em->bdev = NULL;
2548
2549 /* once for the tree */ 2546 /* once for the tree */
2550 free_extent_map(em); 2547 free_extent_map(em);
2551 /* once for us */ 2548 /* once for us */
@@ -4301,9 +4298,11 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
4301 4298
4302 em = alloc_extent_map(); 4299 em = alloc_extent_map();
4303 if (!em) { 4300 if (!em) {
4301 kfree(map);
4304 ret = -ENOMEM; 4302 ret = -ENOMEM;
4305 goto error; 4303 goto error;
4306 } 4304 }
4305 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
4307 em->bdev = (struct block_device *)map; 4306 em->bdev = (struct block_device *)map;
4308 em->start = start; 4307 em->start = start;
4309 em->len = num_bytes; 4308 em->len = num_bytes;
@@ -4346,7 +4345,6 @@ error_del_extent:
4346 /* One for the tree reference */ 4345 /* One for the tree reference */
4347 free_extent_map(em); 4346 free_extent_map(em);
4348error: 4347error:
4349 kfree(map);
4350 kfree(devices_info); 4348 kfree(devices_info);
4351 return ret; 4349 return ret;
4352} 4350}
@@ -4558,7 +4556,6 @@ void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
4558 write_unlock(&tree->map_tree.lock); 4556 write_unlock(&tree->map_tree.lock);
4559 if (!em) 4557 if (!em)
4560 break; 4558 break;
4561 kfree(em->bdev);
4562 /* once for us */ 4559 /* once for us */
4563 free_extent_map(em); 4560 free_extent_map(em);
4564 /* once for the tree */ 4561 /* once for the tree */
@@ -5362,6 +5359,15 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
5362 return 0; 5359 return 0;
5363} 5360}
5364 5361
5362static inline void btrfs_end_bbio(struct btrfs_bio *bbio, struct bio *bio, int err)
5363{
5364 if (likely(bbio->flags & BTRFS_BIO_ORIG_BIO_SUBMITTED))
5365 bio_endio_nodec(bio, err);
5366 else
5367 bio_endio(bio, err);
5368 kfree(bbio);
5369}
5370
5365static void btrfs_end_bio(struct bio *bio, int err) 5371static void btrfs_end_bio(struct bio *bio, int err)
5366{ 5372{
5367 struct btrfs_bio *bbio = bio->bi_private; 5373 struct btrfs_bio *bbio = bio->bi_private;
@@ -5402,12 +5408,6 @@ static void btrfs_end_bio(struct bio *bio, int err)
5402 bio = bbio->orig_bio; 5408 bio = bbio->orig_bio;
5403 } 5409 }
5404 5410
5405 /*
5406 * We have original bio now. So increment bi_remaining to
5407 * account for it in endio
5408 */
5409 atomic_inc(&bio->bi_remaining);
5410
5411 bio->bi_private = bbio->private; 5411 bio->bi_private = bbio->private;
5412 bio->bi_end_io = bbio->end_io; 5412 bio->bi_end_io = bbio->end_io;
5413 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; 5413 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
@@ -5424,9 +5424,8 @@ static void btrfs_end_bio(struct bio *bio, int err)
5424 set_bit(BIO_UPTODATE, &bio->bi_flags); 5424 set_bit(BIO_UPTODATE, &bio->bi_flags);
5425 err = 0; 5425 err = 0;
5426 } 5426 }
5427 kfree(bbio);
5428 5427
5429 bio_endio(bio, err); 5428 btrfs_end_bbio(bbio, bio, err);
5430 } else if (!is_orig_bio) { 5429 } else if (!is_orig_bio) {
5431 bio_put(bio); 5430 bio_put(bio);
5432 } 5431 }
@@ -5589,12 +5588,15 @@ static void bbio_error(struct btrfs_bio *bbio, struct bio *bio, u64 logical)
5589{ 5588{
5590 atomic_inc(&bbio->error); 5589 atomic_inc(&bbio->error);
5591 if (atomic_dec_and_test(&bbio->stripes_pending)) { 5590 if (atomic_dec_and_test(&bbio->stripes_pending)) {
5591 /* Shoud be the original bio. */
5592 WARN_ON(bio != bbio->orig_bio);
5593
5592 bio->bi_private = bbio->private; 5594 bio->bi_private = bbio->private;
5593 bio->bi_end_io = bbio->end_io; 5595 bio->bi_end_io = bbio->end_io;
5594 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num; 5596 btrfs_io_bio(bio)->mirror_num = bbio->mirror_num;
5595 bio->bi_iter.bi_sector = logical >> 9; 5597 bio->bi_iter.bi_sector = logical >> 9;
5596 kfree(bbio); 5598
5597 bio_endio(bio, -EIO); 5599 btrfs_end_bbio(bbio, bio, -EIO);
5598 } 5600 }
5599} 5601}
5600 5602
@@ -5681,6 +5683,7 @@ int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
5681 BUG_ON(!bio); /* -ENOMEM */ 5683 BUG_ON(!bio); /* -ENOMEM */
5682 } else { 5684 } else {
5683 bio = first_bio; 5685 bio = first_bio;
5686 bbio->flags |= BTRFS_BIO_ORIG_BIO_SUBMITTED;
5684 } 5687 }
5685 5688
5686 submit_stripe_bio(root, bbio, bio, 5689 submit_stripe_bio(root, bbio, bio,
@@ -5822,6 +5825,7 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5822 return -ENOMEM; 5825 return -ENOMEM;
5823 } 5826 }
5824 5827
5828 set_bit(EXTENT_FLAG_FS_MAPPING, &em->flags);
5825 em->bdev = (struct block_device *)map; 5829 em->bdev = (struct block_device *)map;
5826 em->start = logical; 5830 em->start = logical;
5827 em->len = length; 5831 em->len = length;
@@ -5846,7 +5850,6 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5846 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid, 5850 map->stripes[i].dev = btrfs_find_device(root->fs_info, devid,
5847 uuid, NULL); 5851 uuid, NULL);
5848 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) { 5852 if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
5849 kfree(map);
5850 free_extent_map(em); 5853 free_extent_map(em);
5851 return -EIO; 5854 return -EIO;
5852 } 5855 }
@@ -5854,7 +5857,6 @@ static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
5854 map->stripes[i].dev = 5857 map->stripes[i].dev =
5855 add_missing_dev(root, devid, uuid); 5858 add_missing_dev(root, devid, uuid);
5856 if (!map->stripes[i].dev) { 5859 if (!map->stripes[i].dev) {
5857 kfree(map);
5858 free_extent_map(em); 5860 free_extent_map(em);
5859 return -EIO; 5861 return -EIO;
5860 } 5862 }
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 1a15bbeb65e2..2aaa00c47816 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -190,11 +190,14 @@ struct btrfs_bio_stripe {
190struct btrfs_bio; 190struct btrfs_bio;
191typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err); 191typedef void (btrfs_bio_end_io_t) (struct btrfs_bio *bio, int err);
192 192
193#define BTRFS_BIO_ORIG_BIO_SUBMITTED 0x1
194
193struct btrfs_bio { 195struct btrfs_bio {
194 atomic_t stripes_pending; 196 atomic_t stripes_pending;
195 struct btrfs_fs_info *fs_info; 197 struct btrfs_fs_info *fs_info;
196 bio_end_io_t *end_io; 198 bio_end_io_t *end_io;
197 struct bio *orig_bio; 199 struct bio *orig_bio;
200 unsigned long flags;
198 void *private; 201 void *private;
199 atomic_t error; 202 atomic_t error;
200 int max_errors; 203 int max_errors;