write_unlock(&fs_info->mapping_tree_lock);
 }
 
+static int btrfs_chunk_map_num_copies(const struct btrfs_chunk_map *map)
+{
+       enum btrfs_raid_types index = btrfs_bg_flags_to_raid_index(map->type);
+
+       if (map->type & BTRFS_BLOCK_GROUP_RAID5)
+               return 2;
+
+       /*
+        * There could be two corrupted data stripes, we need to loop retry in
+        * order to rebuild the correct data.
+        *
+        * Fail a stripe at a time on every retry except the stripe under
+        * reconstruction.
+        */
+       if (map->type & BTRFS_BLOCK_GROUP_RAID6)
+               return map->num_stripes;
+
+       /* Non-RAID56, use their ncopies from btrfs_raid_array. */
+       return btrfs_raid_array[index].ncopies;
+}
+
 int btrfs_num_copies(struct btrfs_fs_info *fs_info, u64 logical, u64 len)
 {
        struct btrfs_chunk_map *map;
-       enum btrfs_raid_types index;
-       int ret = 1;
+       int ret;
 
        map = btrfs_get_chunk_map(fs_info, logical, len);
        if (IS_ERR(map))
                 */
                return 1;
 
-       index = btrfs_bg_flags_to_raid_index(map->type);
-
-       /* Non-RAID56, use their ncopies from btrfs_raid_array. */
-       if (!(map->type & BTRFS_BLOCK_GROUP_RAID56_MASK))
-               ret = btrfs_raid_array[index].ncopies;
-       else if (map->type & BTRFS_BLOCK_GROUP_RAID5)
-               ret = 2;
-       else if (map->type & BTRFS_BLOCK_GROUP_RAID6)
-               /*
-                * There could be two corrupted data stripes, we need
-                * to loop retry in order to rebuild the correct data.
-                *
-                * Fail a stripe at a time on every retry except the
-                * stripe under reconstruction.
-                */
-               ret = map->num_stripes;
+       ret = btrfs_chunk_map_num_copies(map);
        btrfs_free_chunk_map(map);
        return ret;
 }
        io_geom.stripe_index = 0;
        io_geom.op = op;
 
-       num_copies = btrfs_num_copies(fs_info, logical, fs_info->sectorsize);
-       if (io_geom.mirror_num > num_copies)
-               return -EINVAL;
-
        map = btrfs_get_chunk_map(fs_info, logical, *length);
        if (IS_ERR(map))
                return PTR_ERR(map);
 
+       num_copies = btrfs_chunk_map_num_copies(map);
+       if (io_geom.mirror_num > num_copies)
+               return -EINVAL;
+
        map_offset = logical - map->start;
        io_geom.raid56_full_stripe_start = (u64)-1;
        max_len = btrfs_max_io_len(map, map_offset, &io_geom);