btrfs: add wrapper for counting BTRFS_MAX_EXTENT_SIZE
authorDavid Sterba <dsterba@suse.com>
Wed, 4 Jan 2017 10:09:51 +0000 (11:09 +0100)
committerDavid Sterba <dsterba@suse.com>
Tue, 14 Feb 2017 14:50:51 +0000 (15:50 +0100)
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>
fs/btrfs/ctree.h
fs/btrfs/extent-tree.c
fs/btrfs/inode.c

index 6a823719b6c580557cccab3d01123d50857ad334..6ebbccf41f464256fdad834e82edf6d73569f8c0 100644 (file)
@@ -97,6 +97,14 @@ static const int btrfs_csum_sizes[] = { 4 };
 
 #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 extent_map_tree map_tree;
 };
index 42a3bfd87c8e219773e1481a53515b124771fd06..2d8d83c6ccbec135ac0b60fac96f0305e94c47d5 100644 (file)
@@ -5844,11 +5844,9 @@ static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes)
 {
        unsigned drop_inode_space = 0;
        unsigned dropped_extents = 0;
-       unsigned num_extents = 0;
+       unsigned num_extents;
 
-       num_extents = (unsigned)div64_u64(num_bytes +
-                                         BTRFS_MAX_EXTENT_SIZE - 1,
-                                         BTRFS_MAX_EXTENT_SIZE);
+       num_extents = count_max_extents(num_bytes);
        ASSERT(num_extents);
        ASSERT(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)
        struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv;
        u64 to_reserve = 0;
        u64 csum_bytes;
-       unsigned nr_extents = 0;
+       unsigned nr_extents;
        enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
        int ret = 0;
        bool delalloc_lock = true;
@@ -5960,9 +5958,7 @@ int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
        num_bytes = ALIGN(num_bytes, fs_info->sectorsize);
 
        spin_lock(&BTRFS_I(inode)->lock);
-       nr_extents = (unsigned)div64_u64(num_bytes +
-                                        BTRFS_MAX_EXTENT_SIZE - 1,
-                                        BTRFS_MAX_EXTENT_SIZE);
+       nr_extents = count_max_extents(num_bytes);
        BTRFS_I(inode)->outstanding_extents += nr_extents;
 
        nr_extents = 0;
index 39bb0bb0f6e99ff47419faf25f03b80910d34feb..cf239f8982817007aa64a6ad5bda94d1ca43d2ec 100644 (file)
@@ -1605,7 +1605,7 @@ static void btrfs_split_extent_hook(struct inode *inode,
 
        size = orig->end - orig->start + 1;
        if (size > BTRFS_MAX_EXTENT_SIZE) {
-               u64 num_extents;
+               u32 num_extents;
                u64 new_size;
 
                /*
@@ -1613,13 +1613,10 @@ static void btrfs_split_extent_hook(struct inode *inode,
                 * applies here, just in reverse.
                 */
                new_size = orig->end - split + 1;
-               num_extents = div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
-                                       BTRFS_MAX_EXTENT_SIZE);
+               num_extents = count_max_extents(new_size);
                new_size = split - orig->start;
-               num_extents += div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
-                                       BTRFS_MAX_EXTENT_SIZE);
-               if (div64_u64(size + BTRFS_MAX_EXTENT_SIZE - 1,
-                             BTRFS_MAX_EXTENT_SIZE) >= num_extents)
+               num_extents += count_max_extents(new_size);
+               if (count_max_extents(size) >= num_extents)
                        return;
        }
 
@@ -1639,7 +1636,7 @@ static void btrfs_merge_extent_hook(struct inode *inode,
                                    struct extent_state *other)
 {
        u64 new_size, old_size;
-       u64 num_extents;
+       u32 num_extents;
 
        /* not delalloc, ignore it */
        if (!(other->state & EXTENT_DELALLOC))
@@ -1677,14 +1674,10 @@ static void btrfs_merge_extent_hook(struct inode *inode,
         * this case.
         */
        old_size = other->end - other->start + 1;
-       num_extents = div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
-                               BTRFS_MAX_EXTENT_SIZE);
+       num_extents = count_max_extents(old_size);
        old_size = new->end - new->start + 1;
-       num_extents += div64_u64(old_size + BTRFS_MAX_EXTENT_SIZE - 1,
-                                BTRFS_MAX_EXTENT_SIZE);
-
-       if (div64_u64(new_size + BTRFS_MAX_EXTENT_SIZE - 1,
-                     BTRFS_MAX_EXTENT_SIZE) >= num_extents)
+       num_extents += count_max_extents(old_size);
+       if (count_max_extents(new_size) >= num_extents)
                return;
 
        spin_lock(&BTRFS_I(inode)->lock);
@@ -1793,8 +1786,7 @@ static void btrfs_clear_bit_hook(struct inode *inode,
 {
        struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
        u64 len = state->end + 1 - state->start;
-       u64 num_extents = div64_u64(len + BTRFS_MAX_EXTENT_SIZE -1,
-                                   BTRFS_MAX_EXTENT_SIZE);
+       u32 num_extents = count_max_extents(len);
 
        spin_lock(&BTRFS_I(inode)->lock);
        if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
@@ -7617,10 +7609,8 @@ static void adjust_dio_outstanding_extents(struct inode *inode,
                                           struct btrfs_dio_data *dio_data,
                                           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
         * 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)
                ret = btrfs_delalloc_reserve_space(inode, offset, count);
                if (ret)
                        goto out;
-               dio_data.outstanding_extents = div64_u64(count +
-                                               BTRFS_MAX_EXTENT_SIZE - 1,
-                                               BTRFS_MAX_EXTENT_SIZE);
+               dio_data.outstanding_extents = count_max_extents(count);
 
                /*
                 * We need to know how many extents we reserved so that we can