Skip to content

Commit 4824f1f

Browse files
wangxiaoguangmasoncl
authored andcommitted
btrfs: divide btrfs_update_reserved_bytes() into two functions
This patch divides btrfs_update_reserved_bytes() into btrfs_add_reserved_bytes() and btrfs_free_reserved_bytes(), and next patch will extend btrfs_add_reserved_bytes()to fix some false ENOSPC error, please see later patch for detailed info. Signed-off-by: Wang Xiaoguang <[email protected]> Reviewed-by: Josef Bacik <[email protected]> Signed-off-by: David Sterba <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent dcb40c1 commit 4824f1f

File tree

1 file changed

+57
-40
lines changed

1 file changed

+57
-40
lines changed

fs/btrfs/extent-tree.c

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ static int find_next_key(struct btrfs_path *path, int level,
104104
struct btrfs_key *key);
105105
static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106106
int dump_block_groups);
107-
static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
108-
u64 num_bytes, int reserve,
109-
int delalloc);
107+
static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
108+
u64 num_bytes, int reserve, int delalloc);
109+
static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
110+
u64 num_bytes, int delalloc);
110111
static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
111112
u64 num_bytes);
112113
int btrfs_pin_extent(struct btrfs_root *root,
@@ -6497,19 +6498,14 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
64976498
}
64986499

64996500
/**
6500-
* btrfs_update_reserved_bytes - update the block_group and space info counters
6501+
* btrfs_add_reserved_bytes - update the block_group and space info counters
65016502
* @cache: The cache we are manipulating
65026503
* @num_bytes: The number of bytes in question
65036504
* @reserve: One of the reservation enums
65046505
* @delalloc: The blocks are allocated for the delalloc write
65056506
*
6506-
* This is called by the allocator when it reserves space, or by somebody who is
6507-
* freeing space that was never actually used on disk. For example if you
6508-
* reserve some space for a new leaf in transaction A and before transaction A
6509-
* commits you free that leaf, you call this with reserve set to 0 in order to
6510-
* clear the reservation.
6511-
*
6512-
* Metadata reservations should be called with RESERVE_ALLOC so we do the proper
6507+
* This is called by the allocator when it reserves space. Metadata
6508+
* reservations should be called with RESERVE_ALLOC so we do the proper
65136509
* ENOSPC accounting. For data we handle the reservation through clearing the
65146510
* delalloc bits in the io_tree. We have to do this since we could end up
65156511
* allocating less disk space for the amount of data we have reserved in the
@@ -6519,44 +6515,65 @@ void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg)
65196515
* make the reservation and return -EAGAIN, otherwise this function always
65206516
* succeeds.
65216517
*/
6522-
static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
6523-
u64 num_bytes, int reserve, int delalloc)
6518+
static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache,
6519+
u64 num_bytes, int reserve, int delalloc)
65246520
{
65256521
struct btrfs_space_info *space_info = cache->space_info;
65266522
int ret = 0;
65276523

65286524
spin_lock(&space_info->lock);
65296525
spin_lock(&cache->lock);
6530-
if (reserve != RESERVE_FREE) {
6531-
if (cache->ro) {
6532-
ret = -EAGAIN;
6533-
} else {
6534-
cache->reserved += num_bytes;
6535-
space_info->bytes_reserved += num_bytes;
6536-
if (reserve == RESERVE_ALLOC) {
6537-
trace_btrfs_space_reservation(cache->fs_info,
6538-
"space_info", space_info->flags,
6539-
num_bytes, 0);
6540-
space_info->bytes_may_use -= num_bytes;
6541-
}
6542-
6543-
if (delalloc)
6544-
cache->delalloc_bytes += num_bytes;
6545-
}
6526+
if (cache->ro) {
6527+
ret = -EAGAIN;
65466528
} else {
6547-
if (cache->ro)
6548-
space_info->bytes_readonly += num_bytes;
6549-
cache->reserved -= num_bytes;
6550-
space_info->bytes_reserved -= num_bytes;
6529+
cache->reserved += num_bytes;
6530+
space_info->bytes_reserved += num_bytes;
6531+
if (reserve == RESERVE_ALLOC) {
6532+
trace_btrfs_space_reservation(cache->fs_info,
6533+
"space_info", space_info->flags,
6534+
num_bytes, 0);
6535+
space_info->bytes_may_use -= num_bytes;
6536+
}
65516537

65526538
if (delalloc)
6553-
cache->delalloc_bytes -= num_bytes;
6539+
cache->delalloc_bytes += num_bytes;
65546540
}
65556541
spin_unlock(&cache->lock);
65566542
spin_unlock(&space_info->lock);
65576543
return ret;
65586544
}
65596545

6546+
/**
6547+
* btrfs_free_reserved_bytes - update the block_group and space info counters
6548+
* @cache: The cache we are manipulating
6549+
* @num_bytes: The number of bytes in question
6550+
* @delalloc: The blocks are allocated for the delalloc write
6551+
*
6552+
* This is called by somebody who is freeing space that was never actually used
6553+
* on disk. For example if you reserve some space for a new leaf in transaction
6554+
* A and before transaction A commits you free that leaf, you call this with
6555+
* reserve set to 0 in order to clear the reservation.
6556+
*/
6557+
6558+
static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache,
6559+
u64 num_bytes, int delalloc)
6560+
{
6561+
struct btrfs_space_info *space_info = cache->space_info;
6562+
int ret = 0;
6563+
6564+
spin_lock(&space_info->lock);
6565+
spin_lock(&cache->lock);
6566+
if (cache->ro)
6567+
space_info->bytes_readonly += num_bytes;
6568+
cache->reserved -= num_bytes;
6569+
space_info->bytes_reserved -= num_bytes;
6570+
6571+
if (delalloc)
6572+
cache->delalloc_bytes -= num_bytes;
6573+
spin_unlock(&cache->lock);
6574+
spin_unlock(&space_info->lock);
6575+
return ret;
6576+
}
65606577
void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
65616578
struct btrfs_root *root)
65626579
{
@@ -7191,7 +7208,7 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
71917208
WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
71927209

71937210
btrfs_add_free_space(cache, buf->start, buf->len);
7194-
btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
7211+
btrfs_free_reserved_bytes(cache, buf->len, 0);
71957212
btrfs_put_block_group(cache);
71967213
trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
71977214
pin = 0;
@@ -7763,8 +7780,8 @@ static noinline int find_free_extent(struct btrfs_root *orig_root,
77637780
search_start - offset);
77647781
BUG_ON(offset > search_start);
77657782

7766-
ret = btrfs_update_reserved_bytes(block_group, num_bytes,
7767-
alloc_type, delalloc);
7783+
ret = btrfs_add_reserved_bytes(block_group, num_bytes,
7784+
alloc_type, delalloc);
77687785
if (ret == -EAGAIN) {
77697786
btrfs_add_free_space(block_group, offset, num_bytes);
77707787
goto loop;
@@ -7995,7 +8012,7 @@ static int __btrfs_free_reserved_extent(struct btrfs_root *root,
79958012
if (btrfs_test_opt(root->fs_info, DISCARD))
79968013
ret = btrfs_discard_extent(root, start, len, NULL);
79978014
btrfs_add_free_space(cache, start, len);
7998-
btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
8015+
btrfs_free_reserved_bytes(cache, len, delalloc);
79998016
trace_btrfs_reserved_extent_free(root, start, len);
80008017
}
80018018

@@ -8223,8 +8240,8 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
82238240
if (!block_group)
82248241
return -EINVAL;
82258242

8226-
ret = btrfs_update_reserved_bytes(block_group, ins->offset,
8227-
RESERVE_ALLOC_NO_ACCOUNT, 0);
8243+
ret = btrfs_add_reserved_bytes(block_group, ins->offset,
8244+
RESERVE_ALLOC_NO_ACCOUNT, 0);
82288245
BUG_ON(ret); /* logic error */
82298246
ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
82308247
0, owner, offset, ins, 1);

0 commit comments

Comments
 (0)