History

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.

David Sterba committed 9 years ago

Commit 823bb201 parent 95995db3 files · +23 −31Browse files

Unified · Split

3 files
  1. fs/btrfs/ctree.h +8 −0
  2. fs/btrfs/extent-tree.c +4 −8
  3. fs/btrfs/inode.c +11 −23
fs/btrfs/ctree.h+8−0
@@ -97,6 +97,14 @@ static const int btrfs_csum_sizes[] = { 4 }; · 96 unmodified lines
97 97
98#define BTRFS_MAX_EXTENT_SIZE SZ_128M98#define BTRFS_MAX_EXTENT_SIZE SZ_128M
99 99
100/*
101 * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
102 */
103static inline u32 count_max_extents(u64 size)
104{
105 return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
106}
107
100struct btrfs_mapping_tree {108struct btrfs_mapping_tree {
101 struct extent_map_tree map_tree;109 struct extent_map_tree map_tree;
102};110};
fs/btrfs/extent-tree.c+4−8
@@ -5844,11 +5844,9 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) · 5843 unmodified lines
5844{5844{
5845 unsigned drop_inode_space = 0;5845 unsigned drop_inode_space = 0;
5846 unsigned dropped_extents = 0;5846 unsigned dropped_extents = 0;
5847 unsigned num_extents = 0;5847 unsigned num_extents;
5848 5848
5849 num_extents = (unsigned)div64_u64(num_bytes +5849 num_extents = count_max_extents(num_bytes);
5850 BTRFS_MAX_EXTENT_SIZE - 1,
5851 BTRFS_MAX_EXTENT_SIZE);
5852 ASSERT(num_extents);5850 ASSERT(num_extents);
5853 ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);5851 ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents);
5854 BTRFS_I(inode)->outstanding_extents -= num_extents;5852 BTRFS_I(inode)->outstanding_extents -= num_extents;
@@ -5927,7 +5925,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) · 72 unmodified lines
5927 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;5925 struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
5928 u64 to_reserve = 0;5926 u64 to_reserve = 0;
5929 u64 csum_bytes;5927 u64 csum_bytes;
5930 unsigned nr_extents = 0;5928 unsigned nr_extents;
5931 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;5929 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
5932 int ret = 0;5930 int ret = 0;
5933 bool delalloc_lock = true;5931 bool delalloc_lock = true;
@@ -5960,9 +5958,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) · 26 unmodified lines
5960 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);5958 num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
5961 5959
5962 spin_lock(&BTRFS_I(inode)->lock);5960 spin_lock(&BTRFS_I(inode)->lock);
5963 nr_extents = (unsigned)div64_u64(num_bytes +5961 nr_extents = count_max_extents(num_bytes);
5964 BTRFS_MAX_EXTENT_SIZE - 1,
5965 BTRFS_MAX_EXTENT_SIZE);
5966 BTRFS_I(inode)->outstanding_extents += nr_extents;5962 BTRFS_I(inode)->outstanding_extents += nr_extents;
5967 5963
5968 nr_extents = 0;5964 nr_extents = 0;
fs/btrfs/inode.c+11−23
@@ -1605,7 +1605,7 @@ static void btrfs_split_extent_hook(struct inode *inode, · 1604 unmodified lines
1605 1605
1606 size = orig->end - orig->start + 1;1606 size = orig->end - orig->start + 1;
1607 if (size > BTRFS_MAX_EXTENT_SIZE) {1607 if (size > BTRFS_MAX_EXTENT_SIZE) {
1608 u64 num_extents;1608 u32 num_extents;
1609 u64 new_size;1609 u64 new_size;
1610 1610
1611 /*1611 /*
@@ -1613,13 +1613,10 @@ static void btrfs_split_extent_hook(struct inode *inode, · 1 unmodified line
1613 * applies here, just in reverse.1613 * applies here, just in reverse.
1614 */1614 */
1615 new_size = orig->end - split + 1;1615 new_size = orig->end - split + 1;
1616 num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,1616 num_extents = count_max_extents(new_size);
1617 BTRFS_MAX_EXTENT_SIZE);
1618 new_size = split - orig->start;1617 new_size = split - orig->start;
1619 num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,1618 num_extents += count_max_extents(new_size);
1620 BTRFS_MAX_EXTENT_SIZE);1619 if (count_max_extents(size) >= num_extents)
1621 if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
1622 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1623 return;1620 return;
1624 }1621 }
1625 1622
@@ -1639,7 +1636,7 @@ static void btrfs_merge_extent_hook(struct inode *inode, · 13 unmodified lines
1639 struct extent_state *other)1636 struct extent_state *other)
1640{1637{
1641 u64 new_size, old_size;1638 u64 new_size, old_size;
1642 u64 num_extents;1639 u32 num_extents;
1643 1640
1644 /* not delalloc, ignore it */1641 /* not delalloc, ignore it */
1645 if (!(other->state & EXTENT_DELALLOC))1642 if (!(other->state & EXTENT_DELALLOC))
@@ -1677,14 +1674,10 @@ static void btrfs_merge_extent_hook(struct inode *inode, · 31 unmodified lines
1677 * this case.1674 * this case.
1678 */1675 */
1679 old_size = other->end - other->start + 1;1676 old_size = other->end - other->start + 1;
1680 num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,1677 num_extents = count_max_extents(old_size);
1681 BTRFS_MAX_EXTENT_SIZE);
1682 old_size = new->end - new->start + 1;1678 old_size = new->end - new->start + 1;
1683 num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,1679 num_extents += count_max_extents(old_size);
1684 BTRFS_MAX_EXTENT_SIZE);1680 if (count_max_extents(new_size) >= num_extents)
1685
1686 if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
1687 BTRFS_MAX_EXTENT_SIZE) >= num_extents)
1688 return;1681 return;
1689 1682
1690 spin_lock(&BTRFS_I(inode)->lock);1683 spin_lock(&BTRFS_I(inode)->lock);
@@ -1793,8 +1786,7 @@ static void btrfs_clear_bit_hook(struct inode *inode, · 102 unmodified lines
1793{1786{
1794 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);1787 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1795 u64 len = state->end + 1 - state->start;1788 u64 len = state->end + 1 - state->start;
1796 u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,1789 u32 num_extents = count_max_extents(len);
1797 BTRFS_MAX_EXTENT_SIZE);
1798 1790
1799 spin_lock(&BTRFS_I(inode)->lock);1791 spin_lock(&BTRFS_I(inode)->lock);
1800 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))1792 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
@@ -7617,10 +7609,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode, · 5816 unmodified lines
7617 struct btrfs_dio_data *dio_data,7609 struct btrfs_dio_data *dio_data,
7618 const u64 len)7610 const u64 len)
7619{7611{
7620 unsigned num_extents;7612 unsigned num_extents = count_max_extents(len);
7621 7613
7622 num_extents = (unsigned) div64_u64(len + BTRFS_MAX_EXTENT_SIZE - 1,
7623 BTRFS_MAX_EXTENT_SIZE);
7624 /*7614 /*
7625 * If we have an outstanding_extents count still set then we're7615 * If we have an outstanding_extents count still set then we're
7626 * within our reservation, otherwise we need to adjust our inode7616 * 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) · 1054 unmodified lines
8681 ret = btrfs_delalloc_reserve_space(inode, offset, count);8671 ret = btrfs_delalloc_reserve_space(inode, offset, count);
8682 if (ret)8672 if (ret)
8683 goto out;8673 goto out;
8684 dio_data.outstanding_extents = div64_u64(count +8674 dio_data.outstanding_extents = count_max_extents(count);
8685 BTRFS_MAX_EXTENT_SIZE - 1,
8686 BTRFS_MAX_EXTENT_SIZE);
8687 8675
8688 /*8676 /*
8689 * We need to know how many extents we reserved so that we can8677 * We need to know how many extents we reserved so that we can

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t