]> git.hungrycats.org Git - linux/commitdiff
btrfs: cleanup 64bit/32bit divs, provably bounded values
authorDavid Sterba <dsterba@suse.cz>
Fri, 16 Jan 2015 16:26:13 +0000 (17:26 +0100)
committerZygo Blaxell <zblaxell@serenity.furryterror.org>
Wed, 22 Apr 2015 16:24:47 +0000 (12:24 -0400)
The divisor is derived from nodesize or PAGE_SIZE, fits into 32bit type.
Get rid of a few more do_div instances.

Signed-off-by: David Sterba <dsterba@suse.cz>
(cherry picked from commit b8b93addde1e0192b045da8995e296fc1e40c80f)

fs/btrfs/extent-tree.c
fs/btrfs/free-space-cache.c
fs/btrfs/ioctl.c
fs/btrfs/scrub.c
fs/btrfs/volumes.c

index 555f3d7c028b66652e372bb5aa34bead6fb5c552..5089594fa9a7589702a52785b4b7da2046e14ae3 100644 (file)
@@ -5074,7 +5074,7 @@ static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
            BTRFS_I(inode)->csum_bytes == 0)
                return 0;
 
-       old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
+       old_csums = (int)div_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
        if (reserve)
                BTRFS_I(inode)->csum_bytes += num_bytes;
        else
@@ -5083,7 +5083,7 @@ static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
        num_csums_per_leaf = (int)div_u64(csum_size,
                                            sizeof(struct btrfs_csum_item) +
                                            sizeof(struct btrfs_disk_key));
-       num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
+       num_csums = (int)div_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
        num_csums = num_csums + num_csums_per_leaf - 1;
        num_csums = num_csums / num_csums_per_leaf;
 
index 6e5d0abec6f9e845b084b0c08ceabef89d2de49a..13c9b46d4727a8dffdb7fa40ca297c5746323724 100644 (file)
@@ -1298,11 +1298,11 @@ static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
                                   u64 offset)
 {
        u64 bitmap_start;
-       u64 bytes_per_bitmap;
+       u32 bytes_per_bitmap;
 
        bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
        bitmap_start = offset - ctl->start;
-       bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
+       bitmap_start = div_u64(bitmap_start, bytes_per_bitmap);
        bitmap_start *= bytes_per_bitmap;
        bitmap_start += ctl->start;
 
@@ -1521,10 +1521,10 @@ static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
        u64 bitmap_bytes;
        u64 extent_bytes;
        u64 size = block_group->key.offset;
-       u64 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
-       int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
+       u32 bytes_per_bg = BITS_PER_BITMAP * ctl->unit;
+       u32 max_bitmaps = div_u64(size + bytes_per_bg - 1, bytes_per_bg);
 
-       max_bitmaps = max(max_bitmaps, 1);
+       max_bitmaps = max_t(u32, max_bitmaps, 1);
 
        ASSERT(ctl->total_bitmaps <= max_bitmaps);
 
index 835e097aa36a8ea3fd92b6cabea891feb5f493c7..94cde83eb3da41909bb7d9f9377d70d74e4dd8f7 100644 (file)
@@ -1564,7 +1564,7 @@ static noinline int btrfs_ioctl_resize(struct file *file,
                goto out_free;
        }
 
-       do_div(new_size, root->sectorsize);
+       new_size = div_u64(new_size, root->sectorsize);
        new_size *= root->sectorsize;
 
        printk_in_rcu(KERN_INFO "BTRFS: new size for %s is %llu\n",
index 2c69ae6429c1041b20b77a9113c42fc2b95d88ea..f2cbd81128878223e9d36e4fc725f1940ecc6a68 100644 (file)
@@ -2623,9 +2623,8 @@ static int get_raid56_logic_offset(u64 physical, int num,
        for (i = 0; i < nr_data_stripes(map); i++) {
                *offset = last_offset + i * map->stripe_len;
 
-               stripe_nr = *offset;
-               do_div(stripe_nr, map->stripe_len);
-               do_div(stripe_nr, nr_data_stripes(map));
+               stripe_nr = div_u64(*offset, map->stripe_len);
+               stripe_nr = div_u64(stripe_nr, nr_data_stripes(map));
 
                /* Work out the disk rotation on this stripe-set */
                rot = do_div(stripe_nr, map->num_stripes);
@@ -2994,10 +2993,9 @@ static noinline_for_stack int scrub_stripe(struct scrub_ctx *sctx,
        int extent_mirror_num;
        int stop_loop = 0;
 
-       nstripes = length;
        physical = map->stripes[num].physical;
        offset = 0;
-       do_div(nstripes, map->stripe_len);
+       nstripes = div_u64(length, map->stripe_len);
        if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
                offset = map->stripe_len * num;
                increment = map->stripe_len * map->num_stripes;
index 631ab086c21d797c72b90d0c200281e68e159597..a2aba1f6ebcc79055dae22eb79c1f9e582e1adf5 100644 (file)
@@ -3016,7 +3016,7 @@ static int chunk_drange_filter(struct extent_buffer *leaf,
 
                stripe_offset = btrfs_stripe_offset(leaf, stripe);
                stripe_length = btrfs_chunk_length(leaf, chunk);
-               do_div(stripe_length, factor);
+               stripe_length = div_u64(stripe_length, factor);
 
                if (stripe_offset < bargs->pend &&
                    stripe_offset + stripe_length > bargs->pstart)
@@ -4390,8 +4390,8 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
         */
        if (stripe_size * data_stripes > max_chunk_size) {
                u64 mask = (1ULL << 24) - 1;
-               stripe_size = max_chunk_size;
-               do_div(stripe_size, data_stripes);
+
+               stripe_size = div_u64(max_chunk_size, data_stripes);
 
                /* bump the answer up to a 16MB boundary */
                stripe_size = (stripe_size + mask) & ~mask;
@@ -4403,10 +4403,10 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
                        stripe_size = devices_info[ndevs-1].max_avail;
        }
 
-       do_div(stripe_size, dev_stripes);
+       stripe_size = div_u64(stripe_size, dev_stripes);
 
        /* align to BTRFS_STRIPE_LEN */
-       do_div(stripe_size, raid_stripe_len);
+       stripe_size = div_u64(stripe_size, raid_stripe_len);
        stripe_size *= raid_stripe_len;
 
        map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
@@ -5126,7 +5126,7 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
        stripe_index = 0;
        stripe_nr_orig = stripe_nr;
        stripe_nr_end = ALIGN(offset + *length, map->stripe_len);
-       do_div(stripe_nr_end, map->stripe_len);
+       stripe_nr_end = div_u64(stripe_nr_end, map->stripe_len);
        stripe_end_offset = stripe_nr_end * map->stripe_len -
                            (offset + *length);
 
@@ -5188,8 +5188,8 @@ static int __btrfs_map_block(struct btrfs_fs_info *fs_info, int rw,
                    ((rw & (REQ_WRITE | REQ_GET_READ_MIRRORS)) ||
                     mirror_num > 1)) {
                        /* push stripe_nr back to the start of the full stripe */
-                       stripe_nr = raid56_full_stripe_start;
-                       do_div(stripe_nr, stripe_len * nr_data_stripes(map));
+                       stripe_nr = div_u64(raid56_full_stripe_start,
+                                       stripe_len * nr_data_stripes(map));
 
                        /* RAID[56] write or recovery. Return all stripes */
                        num_stripes = map->num_stripes;
@@ -5525,11 +5525,11 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
        rmap_len = map->stripe_len;
 
        if (map->type & BTRFS_BLOCK_GROUP_RAID10)
-               do_div(length, map->num_stripes / map->sub_stripes);
+               length = div_u64(length, map->num_stripes / map->sub_stripes);
        else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
-               do_div(length, map->num_stripes);
+               length = div_u64(length, map->num_stripes);
        else if (map->type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
-               do_div(length, nr_data_stripes(map));
+               length = div_u64(length, nr_data_stripes(map));
                rmap_len = map->stripe_len * nr_data_stripes(map);
        }
 
@@ -5544,11 +5544,11 @@ int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
                        continue;
 
                stripe_nr = physical - map->stripes[i].physical;
-               do_div(stripe_nr, map->stripe_len);
+               stripe_nr = div_u64(stripe_nr, map->stripe_len);
 
                if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
                        stripe_nr = stripe_nr * map->num_stripes + i;
-                       do_div(stripe_nr, map->sub_stripes);
+                       stripe_nr = div_u64(stripe_nr, map->sub_stripes);
                } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
                        stripe_nr = stripe_nr * map->num_stripes + i;
                } /* else if RAID[56], multiply by nr_data_stripes().