Commit 823bb20a authored by David Sterba's avatar David Sterba

btrfs: add wrapper for counting BTRFS_MAX_EXTENT_SIZE

The expression is open-coded in several places, this asks for a wrapper.
As we know the MAX_EXTENT fits to u32, we can use the appropirate
division helper. This cascades to the result type updates.

Compiler is clever enough to use shift instead of integer division, so
there's no change in the generated assembly.
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 95995dbb
...@@ -97,6 +97,14 @@ static const int btrfs_csum_sizes[] = { 4 }; ...@@ -97,6 +97,14 @@ static const int btrfs_csum_sizes[] = { 4 };
#define BTRFS_MAX_EXTENT_SIZE SZ_128M #define BTRFS_MAX_EXTENT_SIZE SZ_128M
/*
* Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
*/
static inline u32 count_max_extents(u64 size)
{
return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
}
struct btrfs_mapping_tree { struct btrfs_mapping_tree {
struct extent_map_tree map_tree; struct extent_map_tree map_tree;
}; };
......
...@@ -5844,11 +5844,9 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) ...@@ -5844,11 +5844,9 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
{ {
unsigned drop_inode_space = 0; unsigned drop_inode_space = 0;
unsigned dropped_extents = 0; unsigned dropped_extents = 0;
unsigned num_extents = 0; unsigned num_extents;
num_extents = (unsigned)div64_u64(num_bytes + num_extents = count_max_extents(num_bytes);
BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE);
ASSERT(num_extents); ASSERT(num_extents);
ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents); ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
BTRFS_I(inode)->outstanding_extents -= num_extents; BTRFS_I(inode)->outstanding_extents -= num_extents;
...@@ -5927,7 +5925,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) ...@@ -5927,7 +5925,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv; struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
u64 to_reserve = 0; u64 to_reserve = 0;
u64 csum_bytes; u64 csum_bytes;
unsigned nr_extents = 0; unsigned nr_extents;
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
int ret = 0; int ret = 0;
bool delalloc_lock = true; bool delalloc_lock = true;
...@@ -5960,9 +5958,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) ...@@ -5960,9 +5958,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
num_bytes = ALIGN(num_bytes, fs_info->sectorsize); num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
spin_lock(&BTRFS_I(inode)->lock); spin_lock(&BTRFS_I(inode)->lock);
nr_extents = (unsigned)div64_u64(num_bytes + nr_extents = count_max_extents(num_bytes);
BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE);
BTRFS_I(inode)->outstanding_extents += nr_extents; BTRFS_I(inode)->outstanding_extents += nr_extents;
nr_extents = 0; nr_extents = 0;
......
...@@ -1605,7 +1605,7 @@ static void btrfs_split_extent_hook(struct inode *inode, ...@@ -1605,7 +1605,7 @@ static void btrfs_split_extent_hook(struct inode *inode,
size = orig->end - orig->start + 1; size = orig->end - orig->start + 1;
if (size > BTRFS_MAX_EXTENT_SIZE) { if (size > BTRFS_MAX_EXTENT_SIZE) {
u64 num_extents; u32 num_extents;
u64 new_size; u64 new_size;
/* /*
...@@ -1613,13 +1613,10 @@ static void btrfs_split_extent_hook(struct inode *inode, ...@@ -1613,13 +1613,10 @@ static void btrfs_split_extent_hook(struct inode *inode,
* applies here, just in reverse. * applies here, just in reverse.
*/ */
new_size = orig->end - split + 1; new_size = orig->end - split + 1;
num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, num_extents = count_max_extents(new_size);
BTRFS_MAX_EXTENT_SIZE);
new_size = split - orig->start; new_size = split - orig->start;
num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1, num_extents += count_max_extents(new_size);
BTRFS_MAX_EXTENT_SIZE); if (count_max_extents(size) >= num_extents)
if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE) >= num_extents)
return; return;
} }
...@@ -1639,7 +1636,7 @@ static void btrfs_merge_extent_hook(struct inode *inode, ...@@ -1639,7 +1636,7 @@ static void btrfs_merge_extent_hook(struct inode *inode,
struct extent_state *other) struct extent_state *other)
{ {
u64 new_size, old_size; u64 new_size, old_size;
u64 num_extents; u32 num_extents;
/* not delalloc, ignore it */ /* not delalloc, ignore it */
if (!(other->state & EXTENT_DELALLOC)) if (!(other->state & EXTENT_DELALLOC))
...@@ -1677,14 +1674,10 @@ static void btrfs_merge_extent_hook(struct inode *inode, ...@@ -1677,14 +1674,10 @@ static void btrfs_merge_extent_hook(struct inode *inode,
* this case. * this case.
*/ */
old_size = other->end - other->start + 1; old_size = other->end - other->start + 1;
num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, num_extents = count_max_extents(old_size);
BTRFS_MAX_EXTENT_SIZE);
old_size = new->end - new->start + 1; old_size = new->end - new->start + 1;
num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1, num_extents += count_max_extents(old_size);
BTRFS_MAX_EXTENT_SIZE); if (count_max_extents(new_size) >= num_extents)
if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE) >= num_extents)
return; return;
spin_lock(&BTRFS_I(inode)->lock); spin_lock(&BTRFS_I(inode)->lock);
...@@ -1793,8 +1786,7 @@ static void btrfs_clear_bit_hook(struct inode *inode, ...@@ -1793,8 +1786,7 @@ static void btrfs_clear_bit_hook(struct inode *inode,
{ {
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
u64 len = state->end + 1 - state->start; u64 len = state->end + 1 - state->start;
u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1, u32 num_extents = count_max_extents(len);
BTRFS_MAX_EXTENT_SIZE);
spin_lock(&BTRFS_I(inode)->lock); spin_lock(&BTRFS_I(inode)->lock);
if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
...@@ -7617,10 +7609,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode, ...@@ -7617,10 +7609,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode,
struct btrfs_dio_data *dio_data, struct btrfs_dio_data *dio_data,
const u64 len) const u64 len)
{ {
unsigned num_extents; unsigned num_extents = count_max_extents(len);
num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE);
/* /*
* If we have an outstanding_extents count still set then we're * If we have an outstanding_extents count still set then we're
* within our reservation, otherwise we need to adjust our inode * within our reservation, otherwise we need to adjust our inode
...@@ -8681,9 +8671,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) ...@@ -8681,9 +8671,7 @@ static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
ret = btrfs_delalloc_reserve_space(inode, offset, count); ret = btrfs_delalloc_reserve_space(inode, offset, count);
if (ret) if (ret)
goto out; goto out;
dio_data.outstanding_extents = div64_u64(count + dio_data.outstanding_extents = count_max_extents(count);
BTRFS_MAX_EXTENT_SIZE - 1,
BTRFS_MAX_EXTENT_SIZE);
/* /*
* We need to know how many extents we reserved so that we can * We need to know how many extents we reserved so that we can
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment