Skip to content

Commit 823bb20

Browse files
committed
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: David Sterba <dsterba@suse.com>
1 parent 95995db commit 823bb20

3 files changed

Lines changed: 23 additions & 31 deletions

File tree

‎fs/btrfs/ctree.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ static const int btrfs_csum_sizes[] = { 4 };
9797

9898
#define BTRFS_MAX_EXTENT_SIZE SZ_128M
9999

100+
/*
101+
* Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
102+
*/
103+
static inline u32 count_max_extents(u64 size)
104+
{
105+
return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
106+
}
107+
100108
struct btrfs_mapping_tree {
101109
struct extent_map_tree map_tree;
102110
};

‎fs/btrfs/extent-tree.c‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5844,11 +5844,9 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
58445844
{
58455845
unsigned drop_inode_space = 0;
58465846
unsigned dropped_extents = 0;
5847-
unsigned num_extents = 0;
5847+
unsigned num_extents;
58485848

5849-
num_extents = (unsigned)div64_u64(num_bytes +
5850-
BTRFS_MAX_EXTENT_SIZE - 1,
5851-
BTRFS_MAX_EXTENT_SIZE);
5849+
num_extents = count_max_extents(num_bytes);
58525850
ASSERT(num_extents);
58535851
ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
58545852
BTRFS_I(inode)->outstanding_extents -= num_extents;
@@ -5927,7 +5925,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
59275925
struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
59285926
u64 to_reserve = 0;
59295927
u64 csum_bytes;
5930-
unsigned nr_extents = 0;
5928+
unsigned nr_extents;
59315929
enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
59325930
int ret = 0;
59335931
bool delalloc_lock = true;
@@ -5960,9 +5958,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
59605958
num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
59615959

59625960
spin_lock(&BTRFS_I(inode)->lock);
5963-
nr_extents = (unsigned)div64_u64(num_bytes +
5964-
BTRFS_MAX_EXTENT_SIZE - 1,
5965-
BTRFS_MAX_EXTENT_SIZE);
5961+
nr_extents = count_max_extents(num_bytes);
59665962
BTRFS_I(inode)->outstanding_extents += nr_extents;
59675963

59685964
nr_extents = 0;

‎fs/btrfs/inode.c‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,21 +1605,18 @@ static void btrfs_split_extent_hook(struct inode *inode,
16051605

16061606
size = orig->end - orig->start + 1;
16071607
if (size > BTRFS_MAX_EXTENT_SIZE) {
1608-
u64 num_extents;
1608+
u32 num_extents;
16091609
u64 new_size;
16101610

16111611
/*
16121612
* See the explanation in btrfs_merge_extent_hook, the same
16131613
* applies here, just in reverse.
16141614
*/
16151615
new_size = orig->end - split + 1;
1616-
num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1617-
BTRFS_MAX_EXTENT_SIZE);
1616+
num_extents = count_max_extents(new_size);
16181617
new_size = split - orig->start;
1619-
num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1620-
BTRFS_MAX_EXTENT_SIZE);
1621-
if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1622-
BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1618+
num_extents += count_max_extents(new_size);
1619+
if (count_max_extents(size) >= num_extents)
16231620
return;
16241621
}
16251622

@@ -1639,7 +1636,7 @@ static void btrfs_merge_extent_hook(struct inode *inode,
16391636
struct extent_state *other)
16401637
{
16411638
u64 new_size, old_size;
1642-
u64 num_extents;
1639+
u32 num_extents;
16431640

16441641
/* not delalloc, ignore it */
16451642
if (!(other->state & EXTENT_DELALLOC))
@@ -1677,14 +1674,10 @@ static void btrfs_merge_extent_hook(struct inode *inode,
16771674
* this case.
16781675
*/
16791676
old_size = other->end - other->start + 1;
1680-
num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1681-
BTRFS_MAX_EXTENT_SIZE);
1677+
num_extents = count_max_extents(old_size);
16821678
old_size = new->end - new->start + 1;
1683-
num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
1684-
BTRFS_MAX_EXTENT_SIZE);
1685-
1686-
if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1687-
BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1679+
num_extents += count_max_extents(old_size);
1680+
if (count_max_extents(new_size) >= num_extents)
16881681
return;
16891682

16901683
spin_lock(&BTRFS_I(inode)->lock);
@@ -1793,8 +1786,7 @@ static void btrfs_clear_bit_hook(struct inode *inode,
17931786
{
17941787
struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
17951788
u64 len = state->end + 1 - state->start;
1796-
u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
1797-
BTRFS_MAX_EXTENT_SIZE);
1789+
u32 num_extents = count_max_extents(len);
17981790

17991791
spin_lock(&BTRFS_I(inode)->lock);
18001792
if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
@@ -7617,10 +7609,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode,
76177609
struct btrfs_dio_data *dio_data,
76187610
const u64 len)
76197611
{
7620-
unsigned num_extents;
7612+
unsigned num_extents = count_max_extents(len);
76217613

7622-
num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7623-
BTRFS_MAX_EXTENT_SIZE);
76247614
/*
76257615
* If we have an outstanding_extents count still set then we're
76267616
* 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)
86818671
ret = btrfs_delalloc_reserve_space(inode, offset, count);
86828672
if (ret)
86838673
goto out;
8684-
dio_data.outstanding_extents = div64_u64(count +
8685-
BTRFS_MAX_EXTENT_SIZE - 1,
8686-
BTRFS_MAX_EXTENT_SIZE);
8674+
dio_data.outstanding_extents = count_max_extents(count);
86878675

86888676
/*
86898677
* We need to know how many extents we reserved so that we can

0 commit comments

Comments
 (0)