diff options
Diffstat (limited to 'fs/btrfs/extent-tree.c')
-rw-r--r-- | fs/btrfs/extent-tree.c | 250 |
1 files changed, 105 insertions, 145 deletions
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c index a684086c3c81..571f402d3fc4 100644 --- a/fs/btrfs/extent-tree.c +++ b/fs/btrfs/extent-tree.c | |||
@@ -74,8 +74,9 @@ enum { | |||
74 | RESERVE_ALLOC_NO_ACCOUNT = 2, | 74 | RESERVE_ALLOC_NO_ACCOUNT = 2, |
75 | }; | 75 | }; |
76 | 76 | ||
77 | static int update_block_group(struct btrfs_root *root, | 77 | static int update_block_group(struct btrfs_trans_handle *trans, |
78 | u64 bytenr, u64 num_bytes, int alloc); | 78 | struct btrfs_root *root, u64 bytenr, |
79 | u64 num_bytes, int alloc); | ||
79 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | 80 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
80 | struct btrfs_root *root, | 81 | struct btrfs_root *root, |
81 | u64 bytenr, u64 num_bytes, u64 parent, | 82 | u64 bytenr, u64 num_bytes, u64 parent, |
@@ -1925,7 +1926,7 @@ int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, | |||
1925 | */ | 1926 | */ |
1926 | ret = 0; | 1927 | ret = 0; |
1927 | } | 1928 | } |
1928 | kfree(bbio); | 1929 | btrfs_put_bbio(bbio); |
1929 | } | 1930 | } |
1930 | 1931 | ||
1931 | if (actual_bytes) | 1932 | if (actual_bytes) |
@@ -2768,7 +2769,6 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | |||
2768 | struct btrfs_delayed_ref_head *head; | 2769 | struct btrfs_delayed_ref_head *head; |
2769 | int ret; | 2770 | int ret; |
2770 | int run_all = count == (unsigned long)-1; | 2771 | int run_all = count == (unsigned long)-1; |
2771 | int run_most = 0; | ||
2772 | 2772 | ||
2773 | /* We'll clean this up in btrfs_cleanup_transaction */ | 2773 | /* We'll clean this up in btrfs_cleanup_transaction */ |
2774 | if (trans->aborted) | 2774 | if (trans->aborted) |
@@ -2778,10 +2778,8 @@ int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | |||
2778 | root = root->fs_info->tree_root; | 2778 | root = root->fs_info->tree_root; |
2779 | 2779 | ||
2780 | delayed_refs = &trans->transaction->delayed_refs; | 2780 | delayed_refs = &trans->transaction->delayed_refs; |
2781 | if (count == 0) { | 2781 | if (count == 0) |
2782 | count = atomic_read(&delayed_refs->num_entries) * 2; | 2782 | count = atomic_read(&delayed_refs->num_entries) * 2; |
2783 | run_most = 1; | ||
2784 | } | ||
2785 | 2783 | ||
2786 | again: | 2784 | again: |
2787 | #ifdef SCRAMBLE_DELAYED_REFS | 2785 | #ifdef SCRAMBLE_DELAYED_REFS |
@@ -3315,120 +3313,42 @@ int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, | |||
3315 | struct btrfs_root *root) | 3313 | struct btrfs_root *root) |
3316 | { | 3314 | { |
3317 | struct btrfs_block_group_cache *cache; | 3315 | struct btrfs_block_group_cache *cache; |
3318 | int err = 0; | 3316 | struct btrfs_transaction *cur_trans = trans->transaction; |
3317 | int ret = 0; | ||
3319 | struct btrfs_path *path; | 3318 | struct btrfs_path *path; |
3320 | u64 last = 0; | 3319 | |
3320 | if (list_empty(&cur_trans->dirty_bgs)) | ||
3321 | return 0; | ||
3321 | 3322 | ||
3322 | path = btrfs_alloc_path(); | 3323 | path = btrfs_alloc_path(); |
3323 | if (!path) | 3324 | if (!path) |
3324 | return -ENOMEM; | 3325 | return -ENOMEM; |
3325 | 3326 | ||
3326 | again: | 3327 | /* |
3327 | while (1) { | 3328 | * We don't need the lock here since we are protected by the transaction |
3328 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | 3329 | * commit. We want to do the cache_save_setup first and then run the |
3329 | while (cache) { | 3330 | * delayed refs to make sure we have the best chance at doing this all |
3330 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) | 3331 | * in one shot. |
3331 | break; | 3332 | */ |
3332 | cache = next_block_group(root, cache); | 3333 | while (!list_empty(&cur_trans->dirty_bgs)) { |
3333 | } | 3334 | cache = list_first_entry(&cur_trans->dirty_bgs, |
3334 | if (!cache) { | 3335 | struct btrfs_block_group_cache, |
3335 | if (last == 0) | 3336 | dirty_list); |
3336 | break; | 3337 | list_del_init(&cache->dirty_list); |
3337 | last = 0; | 3338 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) |
3338 | continue; | 3339 | cache_save_setup(cache, trans, path); |
3339 | } | 3340 | if (!ret) |
3340 | err = cache_save_setup(cache, trans, path); | 3341 | ret = btrfs_run_delayed_refs(trans, root, |
3341 | last = cache->key.objectid + cache->key.offset; | 3342 | (unsigned long) -1); |
3342 | btrfs_put_block_group(cache); | 3343 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) |
3343 | } | 3344 | btrfs_write_out_cache(root, trans, cache, path); |
3344 | 3345 | if (!ret) | |
3345 | while (1) { | 3346 | ret = write_one_cache_group(trans, root, path, cache); |
3346 | if (last == 0) { | ||
3347 | err = btrfs_run_delayed_refs(trans, root, | ||
3348 | (unsigned long)-1); | ||
3349 | if (err) /* File system offline */ | ||
3350 | goto out; | ||
3351 | } | ||
3352 | |||
3353 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | ||
3354 | while (cache) { | ||
3355 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { | ||
3356 | btrfs_put_block_group(cache); | ||
3357 | goto again; | ||
3358 | } | ||
3359 | |||
3360 | if (cache->dirty) | ||
3361 | break; | ||
3362 | cache = next_block_group(root, cache); | ||
3363 | } | ||
3364 | if (!cache) { | ||
3365 | if (last == 0) | ||
3366 | break; | ||
3367 | last = 0; | ||
3368 | continue; | ||
3369 | } | ||
3370 | |||
3371 | if (cache->disk_cache_state == BTRFS_DC_SETUP) | ||
3372 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; | ||
3373 | cache->dirty = 0; | ||
3374 | last = cache->key.objectid + cache->key.offset; | ||
3375 | |||
3376 | err = write_one_cache_group(trans, root, path, cache); | ||
3377 | btrfs_put_block_group(cache); | ||
3378 | if (err) /* File system offline */ | ||
3379 | goto out; | ||
3380 | } | ||
3381 | |||
3382 | while (1) { | ||
3383 | /* | ||
3384 | * I don't think this is needed since we're just marking our | ||
3385 | * preallocated extent as written, but just in case it can't | ||
3386 | * hurt. | ||
3387 | */ | ||
3388 | if (last == 0) { | ||
3389 | err = btrfs_run_delayed_refs(trans, root, | ||
3390 | (unsigned long)-1); | ||
3391 | if (err) /* File system offline */ | ||
3392 | goto out; | ||
3393 | } | ||
3394 | |||
3395 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | ||
3396 | while (cache) { | ||
3397 | /* | ||
3398 | * Really this shouldn't happen, but it could if we | ||
3399 | * couldn't write the entire preallocated extent and | ||
3400 | * splitting the extent resulted in a new block. | ||
3401 | */ | ||
3402 | if (cache->dirty) { | ||
3403 | btrfs_put_block_group(cache); | ||
3404 | goto again; | ||
3405 | } | ||
3406 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) | ||
3407 | break; | ||
3408 | cache = next_block_group(root, cache); | ||
3409 | } | ||
3410 | if (!cache) { | ||
3411 | if (last == 0) | ||
3412 | break; | ||
3413 | last = 0; | ||
3414 | continue; | ||
3415 | } | ||
3416 | |||
3417 | err = btrfs_write_out_cache(root, trans, cache, path); | ||
3418 | |||
3419 | /* | ||
3420 | * If we didn't have an error then the cache state is still | ||
3421 | * NEED_WRITE, so we can set it to WRITTEN. | ||
3422 | */ | ||
3423 | if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE) | ||
3424 | cache->disk_cache_state = BTRFS_DC_WRITTEN; | ||
3425 | last = cache->key.objectid + cache->key.offset; | ||
3426 | btrfs_put_block_group(cache); | 3347 | btrfs_put_block_group(cache); |
3427 | } | 3348 | } |
3428 | out: | ||
3429 | 3349 | ||
3430 | btrfs_free_path(path); | 3350 | btrfs_free_path(path); |
3431 | return err; | 3351 | return ret; |
3432 | } | 3352 | } |
3433 | 3353 | ||
3434 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) | 3354 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
@@ -5043,19 +4963,25 @@ void btrfs_subvolume_release_metadata(struct btrfs_root *root, | |||
5043 | /** | 4963 | /** |
5044 | * drop_outstanding_extent - drop an outstanding extent | 4964 | * drop_outstanding_extent - drop an outstanding extent |
5045 | * @inode: the inode we're dropping the extent for | 4965 | * @inode: the inode we're dropping the extent for |
4966 | * @num_bytes: the number of bytes we're relaseing. | ||
5046 | * | 4967 | * |
5047 | * This is called when we are freeing up an outstanding extent, either called | 4968 | * This is called when we are freeing up an outstanding extent, either called |
5048 | * after an error or after an extent is written. This will return the number of | 4969 | * after an error or after an extent is written. This will return the number of |
5049 | * reserved extents that need to be freed. This must be called with | 4970 | * reserved extents that need to be freed. This must be called with |
5050 | * BTRFS_I(inode)->lock held. | 4971 | * BTRFS_I(inode)->lock held. |
5051 | */ | 4972 | */ |
5052 | static unsigned drop_outstanding_extent(struct inode *inode) | 4973 | static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) |
5053 | { | 4974 | { |
5054 | unsigned drop_inode_space = 0; | 4975 | unsigned drop_inode_space = 0; |
5055 | unsigned dropped_extents = 0; | 4976 | unsigned dropped_extents = 0; |
4977 | unsigned num_extents = 0; | ||
5056 | 4978 | ||
5057 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); | 4979 | num_extents = (unsigned)div64_u64(num_bytes + |
5058 | BTRFS_I(inode)->outstanding_extents--; | 4980 | BTRFS_MAX_EXTENT_SIZE - 1, |
4981 | BTRFS_MAX_EXTENT_SIZE); | ||
4982 | ASSERT(num_extents); | ||
4983 | ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents); | ||
4984 | BTRFS_I(inode)->outstanding_extents -= num_extents; | ||
5059 | 4985 | ||
5060 | if (BTRFS_I(inode)->outstanding_extents == 0 && | 4986 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
5061 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, | 4987 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
@@ -5226,7 +5152,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) | |||
5226 | 5152 | ||
5227 | out_fail: | 5153 | out_fail: |
5228 | spin_lock(&BTRFS_I(inode)->lock); | 5154 | spin_lock(&BTRFS_I(inode)->lock); |
5229 | dropped = drop_outstanding_extent(inode); | 5155 | dropped = drop_outstanding_extent(inode, num_bytes); |
5230 | /* | 5156 | /* |
5231 | * If the inodes csum_bytes is the same as the original | 5157 | * If the inodes csum_bytes is the same as the original |
5232 | * csum_bytes then we know we haven't raced with any free()ers | 5158 | * csum_bytes then we know we haven't raced with any free()ers |
@@ -5305,7 +5231,7 @@ void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) | |||
5305 | 5231 | ||
5306 | num_bytes = ALIGN(num_bytes, root->sectorsize); | 5232 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
5307 | spin_lock(&BTRFS_I(inode)->lock); | 5233 | spin_lock(&BTRFS_I(inode)->lock); |
5308 | dropped = drop_outstanding_extent(inode); | 5234 | dropped = drop_outstanding_extent(inode, num_bytes); |
5309 | 5235 | ||
5310 | if (num_bytes) | 5236 | if (num_bytes) |
5311 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); | 5237 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
@@ -5375,8 +5301,9 @@ void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) | |||
5375 | btrfs_free_reserved_data_space(inode, num_bytes); | 5301 | btrfs_free_reserved_data_space(inode, num_bytes); |
5376 | } | 5302 | } |
5377 | 5303 | ||
5378 | static int update_block_group(struct btrfs_root *root, | 5304 | static int update_block_group(struct btrfs_trans_handle *trans, |
5379 | u64 bytenr, u64 num_bytes, int alloc) | 5305 | struct btrfs_root *root, u64 bytenr, |
5306 | u64 num_bytes, int alloc) | ||
5380 | { | 5307 | { |
5381 | struct btrfs_block_group_cache *cache = NULL; | 5308 | struct btrfs_block_group_cache *cache = NULL; |
5382 | struct btrfs_fs_info *info = root->fs_info; | 5309 | struct btrfs_fs_info *info = root->fs_info; |
@@ -5414,6 +5341,14 @@ static int update_block_group(struct btrfs_root *root, | |||
5414 | if (!alloc && cache->cached == BTRFS_CACHE_NO) | 5341 | if (!alloc && cache->cached == BTRFS_CACHE_NO) |
5415 | cache_block_group(cache, 1); | 5342 | cache_block_group(cache, 1); |
5416 | 5343 | ||
5344 | spin_lock(&trans->transaction->dirty_bgs_lock); | ||
5345 | if (list_empty(&cache->dirty_list)) { | ||
5346 | list_add_tail(&cache->dirty_list, | ||
5347 | &trans->transaction->dirty_bgs); | ||
5348 | btrfs_get_block_group(cache); | ||
5349 | } | ||
5350 | spin_unlock(&trans->transaction->dirty_bgs_lock); | ||
5351 | |||
5417 | byte_in_group = bytenr - cache->key.objectid; | 5352 | byte_in_group = bytenr - cache->key.objectid; |
5418 | WARN_ON(byte_in_group > cache->key.offset); | 5353 | WARN_ON(byte_in_group > cache->key.offset); |
5419 | 5354 | ||
@@ -5424,7 +5359,6 @@ static int update_block_group(struct btrfs_root *root, | |||
5424 | cache->disk_cache_state < BTRFS_DC_CLEAR) | 5359 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
5425 | cache->disk_cache_state = BTRFS_DC_CLEAR; | 5360 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
5426 | 5361 | ||
5427 | cache->dirty = 1; | ||
5428 | old_val = btrfs_block_group_used(&cache->item); | 5362 | old_val = btrfs_block_group_used(&cache->item); |
5429 | num_bytes = min(total, cache->key.offset - byte_in_group); | 5363 | num_bytes = min(total, cache->key.offset - byte_in_group); |
5430 | if (alloc) { | 5364 | if (alloc) { |
@@ -5807,10 +5741,13 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |||
5807 | unpin = &fs_info->freed_extents[0]; | 5741 | unpin = &fs_info->freed_extents[0]; |
5808 | 5742 | ||
5809 | while (1) { | 5743 | while (1) { |
5744 | mutex_lock(&fs_info->unused_bg_unpin_mutex); | ||
5810 | ret = find_first_extent_bit(unpin, 0, &start, &end, | 5745 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
5811 | EXTENT_DIRTY, NULL); | 5746 | EXTENT_DIRTY, NULL); |
5812 | if (ret) | 5747 | if (ret) { |
5748 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | ||
5813 | break; | 5749 | break; |
5750 | } | ||
5814 | 5751 | ||
5815 | if (btrfs_test_opt(root, DISCARD)) | 5752 | if (btrfs_test_opt(root, DISCARD)) |
5816 | ret = btrfs_discard_extent(root, start, | 5753 | ret = btrfs_discard_extent(root, start, |
@@ -5818,6 +5755,7 @@ int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |||
5818 | 5755 | ||
5819 | clear_extent_dirty(unpin, start, end, GFP_NOFS); | 5756 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
5820 | unpin_extent_range(root, start, end, true); | 5757 | unpin_extent_range(root, start, end, true); |
5758 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | ||
5821 | cond_resched(); | 5759 | cond_resched(); |
5822 | } | 5760 | } |
5823 | 5761 | ||
@@ -6103,7 +6041,7 @@ static int __btrfs_free_extent(struct btrfs_trans_handle *trans, | |||
6103 | } | 6041 | } |
6104 | } | 6042 | } |
6105 | 6043 | ||
6106 | ret = update_block_group(root, bytenr, num_bytes, 0); | 6044 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
6107 | if (ret) { | 6045 | if (ret) { |
6108 | btrfs_abort_transaction(trans, extent_root, ret); | 6046 | btrfs_abort_transaction(trans, extent_root, ret); |
6109 | goto out; | 6047 | goto out; |
@@ -6205,7 +6143,6 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, | |||
6205 | struct extent_buffer *buf, | 6143 | struct extent_buffer *buf, |
6206 | u64 parent, int last_ref) | 6144 | u64 parent, int last_ref) |
6207 | { | 6145 | { |
6208 | struct btrfs_block_group_cache *cache = NULL; | ||
6209 | int pin = 1; | 6146 | int pin = 1; |
6210 | int ret; | 6147 | int ret; |
6211 | 6148 | ||
@@ -6221,17 +6158,20 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, | |||
6221 | if (!last_ref) | 6158 | if (!last_ref) |
6222 | return; | 6159 | return; |
6223 | 6160 | ||
6224 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); | ||
6225 | |||
6226 | if (btrfs_header_generation(buf) == trans->transid) { | 6161 | if (btrfs_header_generation(buf) == trans->transid) { |
6162 | struct btrfs_block_group_cache *cache; | ||
6163 | |||
6227 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | 6164 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
6228 | ret = check_ref_cleanup(trans, root, buf->start); | 6165 | ret = check_ref_cleanup(trans, root, buf->start); |
6229 | if (!ret) | 6166 | if (!ret) |
6230 | goto out; | 6167 | goto out; |
6231 | } | 6168 | } |
6232 | 6169 | ||
6170 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); | ||
6171 | |||
6233 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | 6172 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
6234 | pin_down_extent(root, cache, buf->start, buf->len, 1); | 6173 | pin_down_extent(root, cache, buf->start, buf->len, 1); |
6174 | btrfs_put_block_group(cache); | ||
6235 | goto out; | 6175 | goto out; |
6236 | } | 6176 | } |
6237 | 6177 | ||
@@ -6239,6 +6179,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans, | |||
6239 | 6179 | ||
6240 | btrfs_add_free_space(cache, buf->start, buf->len); | 6180 | btrfs_add_free_space(cache, buf->start, buf->len); |
6241 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0); | 6181 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0); |
6182 | btrfs_put_block_group(cache); | ||
6242 | trace_btrfs_reserved_extent_free(root, buf->start, buf->len); | 6183 | trace_btrfs_reserved_extent_free(root, buf->start, buf->len); |
6243 | pin = 0; | 6184 | pin = 0; |
6244 | } | 6185 | } |
@@ -6253,7 +6194,6 @@ out: | |||
6253 | * anymore. | 6194 | * anymore. |
6254 | */ | 6195 | */ |
6255 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); | 6196 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); |
6256 | btrfs_put_block_group(cache); | ||
6257 | } | 6197 | } |
6258 | 6198 | ||
6259 | /* Can return -ENOMEM */ | 6199 | /* Can return -ENOMEM */ |
@@ -7063,7 +7003,7 @@ static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |||
7063 | if (ret) | 7003 | if (ret) |
7064 | return ret; | 7004 | return ret; |
7065 | 7005 | ||
7066 | ret = update_block_group(root, ins->objectid, ins->offset, 1); | 7006 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
7067 | if (ret) { /* -ENOENT, logic error */ | 7007 | if (ret) { /* -ENOENT, logic error */ |
7068 | btrfs_err(fs_info, "update block group failed for %llu %llu", | 7008 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
7069 | ins->objectid, ins->offset); | 7009 | ins->objectid, ins->offset); |
@@ -7152,7 +7092,8 @@ static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, | |||
7152 | return ret; | 7092 | return ret; |
7153 | } | 7093 | } |
7154 | 7094 | ||
7155 | ret = update_block_group(root, ins->objectid, root->nodesize, 1); | 7095 | ret = update_block_group(trans, root, ins->objectid, root->nodesize, |
7096 | 1); | ||
7156 | if (ret) { /* -ENOENT, logic error */ | 7097 | if (ret) { /* -ENOENT, logic error */ |
7157 | btrfs_err(fs_info, "update block group failed for %llu %llu", | 7098 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
7158 | ins->objectid, ins->offset); | 7099 | ins->objectid, ins->offset); |
@@ -7217,11 +7158,11 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, | |||
7217 | 7158 | ||
7218 | static struct extent_buffer * | 7159 | static struct extent_buffer * |
7219 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, | 7160 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
7220 | u64 bytenr, u32 blocksize, int level) | 7161 | u64 bytenr, int level) |
7221 | { | 7162 | { |
7222 | struct extent_buffer *buf; | 7163 | struct extent_buffer *buf; |
7223 | 7164 | ||
7224 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | 7165 | buf = btrfs_find_create_tree_block(root, bytenr); |
7225 | if (!buf) | 7166 | if (!buf) |
7226 | return ERR_PTR(-ENOMEM); | 7167 | return ERR_PTR(-ENOMEM); |
7227 | btrfs_set_header_generation(buf, trans->transid); | 7168 | btrfs_set_header_generation(buf, trans->transid); |
@@ -7340,7 +7281,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, | |||
7340 | 7281 | ||
7341 | if (btrfs_test_is_dummy_root(root)) { | 7282 | if (btrfs_test_is_dummy_root(root)) { |
7342 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, | 7283 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
7343 | blocksize, level); | 7284 | level); |
7344 | if (!IS_ERR(buf)) | 7285 | if (!IS_ERR(buf)) |
7345 | root->alloc_bytenr += blocksize; | 7286 | root->alloc_bytenr += blocksize; |
7346 | return buf; | 7287 | return buf; |
@@ -7357,8 +7298,7 @@ struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, | |||
7357 | return ERR_PTR(ret); | 7298 | return ERR_PTR(ret); |
7358 | } | 7299 | } |
7359 | 7300 | ||
7360 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, | 7301 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
7361 | blocksize, level); | ||
7362 | BUG_ON(IS_ERR(buf)); /* -ENOMEM */ | 7302 | BUG_ON(IS_ERR(buf)); /* -ENOMEM */ |
7363 | 7303 | ||
7364 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { | 7304 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { |
@@ -7487,7 +7427,7 @@ static noinline void reada_walk_down(struct btrfs_trans_handle *trans, | |||
7487 | continue; | 7427 | continue; |
7488 | } | 7428 | } |
7489 | reada: | 7429 | reada: |
7490 | readahead_tree_block(root, bytenr, blocksize); | 7430 | readahead_tree_block(root, bytenr); |
7491 | nread++; | 7431 | nread++; |
7492 | } | 7432 | } |
7493 | wc->reada_slot = slot; | 7433 | wc->reada_slot = slot; |
@@ -7828,7 +7768,7 @@ static noinline int do_walk_down(struct btrfs_trans_handle *trans, | |||
7828 | 7768 | ||
7829 | next = btrfs_find_tree_block(root, bytenr); | 7769 | next = btrfs_find_tree_block(root, bytenr); |
7830 | if (!next) { | 7770 | if (!next) { |
7831 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); | 7771 | next = btrfs_find_create_tree_block(root, bytenr); |
7832 | if (!next) | 7772 | if (!next) |
7833 | return -ENOMEM; | 7773 | return -ENOMEM; |
7834 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, | 7774 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
@@ -8548,14 +8488,6 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, | |||
8548 | if (IS_ERR(trans)) | 8488 | if (IS_ERR(trans)) |
8549 | return PTR_ERR(trans); | 8489 | return PTR_ERR(trans); |
8550 | 8490 | ||
8551 | alloc_flags = update_block_group_flags(root, cache->flags); | ||
8552 | if (alloc_flags != cache->flags) { | ||
8553 | ret = do_chunk_alloc(trans, root, alloc_flags, | ||
8554 | CHUNK_ALLOC_FORCE); | ||
8555 | if (ret < 0) | ||
8556 | goto out; | ||
8557 | } | ||
8558 | |||
8559 | ret = set_block_group_ro(cache, 0); | 8491 | ret = set_block_group_ro(cache, 0); |
8560 | if (!ret) | 8492 | if (!ret) |
8561 | goto out; | 8493 | goto out; |
@@ -8566,6 +8498,11 @@ int btrfs_set_block_group_ro(struct btrfs_root *root, | |||
8566 | goto out; | 8498 | goto out; |
8567 | ret = set_block_group_ro(cache, 0); | 8499 | ret = set_block_group_ro(cache, 0); |
8568 | out: | 8500 | out: |
8501 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { | ||
8502 | alloc_flags = update_block_group_flags(root, cache->flags); | ||
8503 | check_system_chunk(trans, root, alloc_flags); | ||
8504 | } | ||
8505 | |||
8569 | btrfs_end_transaction(trans, root); | 8506 | btrfs_end_transaction(trans, root); |
8570 | return ret; | 8507 | return ret; |
8571 | } | 8508 | } |
@@ -9005,6 +8942,7 @@ btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) | |||
9005 | INIT_LIST_HEAD(&cache->cluster_list); | 8942 | INIT_LIST_HEAD(&cache->cluster_list); |
9006 | INIT_LIST_HEAD(&cache->bg_list); | 8943 | INIT_LIST_HEAD(&cache->bg_list); |
9007 | INIT_LIST_HEAD(&cache->ro_list); | 8944 | INIT_LIST_HEAD(&cache->ro_list); |
8945 | INIT_LIST_HEAD(&cache->dirty_list); | ||
9008 | btrfs_init_free_space_ctl(cache); | 8946 | btrfs_init_free_space_ctl(cache); |
9009 | atomic_set(&cache->trimming, 0); | 8947 | atomic_set(&cache->trimming, 0); |
9010 | 8948 | ||
@@ -9068,9 +9006,8 @@ int btrfs_read_block_groups(struct btrfs_root *root) | |||
9068 | * b) Setting 'dirty flag' makes sure that we flush | 9006 | * b) Setting 'dirty flag' makes sure that we flush |
9069 | * the new space cache info onto disk. | 9007 | * the new space cache info onto disk. |
9070 | */ | 9008 | */ |
9071 | cache->disk_cache_state = BTRFS_DC_CLEAR; | ||
9072 | if (btrfs_test_opt(root, SPACE_CACHE)) | 9009 | if (btrfs_test_opt(root, SPACE_CACHE)) |
9073 | cache->dirty = 1; | 9010 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
9074 | } | 9011 | } |
9075 | 9012 | ||
9076 | read_extent_buffer(leaf, &cache->item, | 9013 | read_extent_buffer(leaf, &cache->item, |
@@ -9460,6 +9397,13 @@ int btrfs_remove_block_group(struct btrfs_trans_handle *trans, | |||
9460 | } | 9397 | } |
9461 | } | 9398 | } |
9462 | 9399 | ||
9400 | spin_lock(&trans->transaction->dirty_bgs_lock); | ||
9401 | if (!list_empty(&block_group->dirty_list)) { | ||
9402 | list_del_init(&block_group->dirty_list); | ||
9403 | btrfs_put_block_group(block_group); | ||
9404 | } | ||
9405 | spin_unlock(&trans->transaction->dirty_bgs_lock); | ||
9406 | |||
9463 | btrfs_remove_free_space_cache(block_group); | 9407 | btrfs_remove_free_space_cache(block_group); |
9464 | 9408 | ||
9465 | spin_lock(&block_group->space_info->lock); | 9409 | spin_lock(&block_group->space_info->lock); |
@@ -9611,7 +9555,8 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) | |||
9611 | * Want to do this before we do anything else so we can recover | 9555 | * Want to do this before we do anything else so we can recover |
9612 | * properly if we fail to join the transaction. | 9556 | * properly if we fail to join the transaction. |
9613 | */ | 9557 | */ |
9614 | trans = btrfs_join_transaction(root); | 9558 | /* 1 for btrfs_orphan_reserve_metadata() */ |
9559 | trans = btrfs_start_transaction(root, 1); | ||
9615 | if (IS_ERR(trans)) { | 9560 | if (IS_ERR(trans)) { |
9616 | btrfs_set_block_group_rw(root, block_group); | 9561 | btrfs_set_block_group_rw(root, block_group); |
9617 | ret = PTR_ERR(trans); | 9562 | ret = PTR_ERR(trans); |
@@ -9624,18 +9569,33 @@ void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) | |||
9624 | */ | 9569 | */ |
9625 | start = block_group->key.objectid; | 9570 | start = block_group->key.objectid; |
9626 | end = start + block_group->key.offset - 1; | 9571 | end = start + block_group->key.offset - 1; |
9572 | /* | ||
9573 | * Hold the unused_bg_unpin_mutex lock to avoid racing with | ||
9574 | * btrfs_finish_extent_commit(). If we are at transaction N, | ||
9575 | * another task might be running finish_extent_commit() for the | ||
9576 | * previous transaction N - 1, and have seen a range belonging | ||
9577 | * to the block group in freed_extents[] before we were able to | ||
9578 | * clear the whole block group range from freed_extents[]. This | ||
9579 | * means that task can lookup for the block group after we | ||
9580 | * unpinned it from freed_extents[] and removed it, leading to | ||
9581 | * a BUG_ON() at btrfs_unpin_extent_range(). | ||
9582 | */ | ||
9583 | mutex_lock(&fs_info->unused_bg_unpin_mutex); | ||
9627 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, | 9584 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
9628 | EXTENT_DIRTY, GFP_NOFS); | 9585 | EXTENT_DIRTY, GFP_NOFS); |
9629 | if (ret) { | 9586 | if (ret) { |
9587 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | ||
9630 | btrfs_set_block_group_rw(root, block_group); | 9588 | btrfs_set_block_group_rw(root, block_group); |
9631 | goto end_trans; | 9589 | goto end_trans; |
9632 | } | 9590 | } |
9633 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, | 9591 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, |
9634 | EXTENT_DIRTY, GFP_NOFS); | 9592 | EXTENT_DIRTY, GFP_NOFS); |
9635 | if (ret) { | 9593 | if (ret) { |
9594 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | ||
9636 | btrfs_set_block_group_rw(root, block_group); | 9595 | btrfs_set_block_group_rw(root, block_group); |
9637 | goto end_trans; | 9596 | goto end_trans; |
9638 | } | 9597 | } |
9598 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | ||
9639 | 9599 | ||
9640 | /* Reset pinned so btrfs_put_block_group doesn't complain */ | 9600 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
9641 | block_group->pinned = 0; | 9601 | block_group->pinned = 0; |