]> git.baikalelectronics.ru Git - kernel.git/commitdiff
btrfs: replace BUILD_BUG_ON by static_assert
authorDavid Sterba <dsterba@suse.com>
Tue, 1 Feb 2022 14:42:07 +0000 (15:42 +0100)
committerDavid Sterba <dsterba@suse.com>
Mon, 14 Mar 2022 12:13:49 +0000 (13:13 +0100)
The static_assert introduced in 6bab69c65013 ("build_bug.h: add wrapper
for _Static_assert") has been supported by compilers for a long time
(gcc 4.6, clang 3.0) and can be used in header files. We don't need to
put BUILD_BUG_ON to random functions but rather keep it next to the
definition.

The exception here is the UAPI header btrfs_tree.h that could be
potentially included by userspace code and the static assert is not
defined (nor used in any other header).

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/compression.h
fs/btrfs/ctree.h
fs/btrfs/inode.c
fs/btrfs/ioctl.c
fs/btrfs/sysfs.c

index 56eef0821e3e31d6680515ed7393d2cdf65b9002..7dbd14caab01123287194fe99f9db4e5f474aa41 100644 (file)
@@ -22,6 +22,8 @@ struct btrfs_inode;
 
 /* Maximum length of compressed data stored on disk */
 #define BTRFS_MAX_COMPRESSED           (SZ_128K)
+static_assert((BTRFS_MAX_COMPRESSED % PAGE_SIZE) == 0);
+
 /* Maximum size of data before compression */
 #define BTRFS_MAX_UNCOMPRESSED         (SZ_128K)
 
index 54513c4e891a8226ae9d90c4ff23970e0f585685..89d2ed6c2eb1304aefe2e915c8e3cd09adfc3fac 100644 (file)
@@ -1639,25 +1639,25 @@ DECLARE_BTRFS_SETGET_BITS(64)
 static inline u##bits btrfs_##name(const struct extent_buffer *eb,     \
                                   const type *s)                       \
 {                                                                      \
-       BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);   \
+       static_assert(sizeof(u##bits) == sizeof(((type *)0))->member);  \
        return btrfs_get_##bits(eb, s, offsetof(type, member));         \
 }                                                                      \
 static inline void btrfs_set_##name(const struct extent_buffer *eb, type *s, \
                                    u##bits val)                        \
 {                                                                      \
-       BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);   \
+       static_assert(sizeof(u##bits) == sizeof(((type *)0))->member);  \
        btrfs_set_##bits(eb, s, offsetof(type, member), val);           \
 }                                                                      \
 static inline u##bits btrfs_token_##name(struct btrfs_map_token *token,        \
                                         const type *s)                 \
 {                                                                      \
-       BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);   \
+       static_assert(sizeof(u##bits) == sizeof(((type *)0))->member);  \
        return btrfs_get_token_##bits(token, s, offsetof(type, member));\
 }                                                                      \
 static inline void btrfs_set_token_##name(struct btrfs_map_token *token,\
                                          type *s, u##bits val)         \
 {                                                                      \
-       BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member);   \
+       static_assert(sizeof(u##bits) == sizeof(((type *)0))->member);  \
        btrfs_set_token_##bits(token, s, offsetof(type, member), val);  \
 }
 
@@ -1688,8 +1688,8 @@ static inline void btrfs_set_##name(type *s, u##bits val)         \
 static inline u64 btrfs_device_total_bytes(const struct extent_buffer *eb,
                                           struct btrfs_dev_item *s)
 {
-       BUILD_BUG_ON(sizeof(u64) !=
-                    sizeof(((struct btrfs_dev_item *)0))->total_bytes);
+       static_assert(sizeof(u64) ==
+                     sizeof(((struct btrfs_dev_item *)0))->total_bytes);
        return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
                                            total_bytes));
 }
@@ -1697,8 +1697,8 @@ static inline void btrfs_set_device_total_bytes(const struct extent_buffer *eb,
                                                struct btrfs_dev_item *s,
                                                u64 val)
 {
-       BUILD_BUG_ON(sizeof(u64) !=
-                    sizeof(((struct btrfs_dev_item *)0))->total_bytes);
+       static_assert(sizeof(u64) ==
+                     sizeof(((struct btrfs_dev_item *)0))->total_bytes);
        WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
        btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
 }
index fc3e2f06cb026c1085ac9735f648a58fd24b8a69..1ce1ccf6d22c29e06d5fde5844d57b4ba043e15a 100644 (file)
@@ -629,7 +629,6 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)
 again:
        will_compress = 0;
        nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
-       BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
        nr_pages = min_t(unsigned long, nr_pages,
                        BTRFS_MAX_COMPRESSED / PAGE_SIZE);
 
@@ -5609,21 +5608,17 @@ static struct inode *new_simple_dir(struct super_block *s,
        return inode;
 }
 
+static_assert(BTRFS_FT_UNKNOWN == FT_UNKNOWN);
+static_assert(BTRFS_FT_REG_FILE == FT_REG_FILE);
+static_assert(BTRFS_FT_DIR == FT_DIR);
+static_assert(BTRFS_FT_CHRDEV == FT_CHRDEV);
+static_assert(BTRFS_FT_BLKDEV == FT_BLKDEV);
+static_assert(BTRFS_FT_FIFO == FT_FIFO);
+static_assert(BTRFS_FT_SOCK == FT_SOCK);
+static_assert(BTRFS_FT_SYMLINK == FT_SYMLINK);
+
 static inline u8 btrfs_inode_type(struct inode *inode)
 {
-       /*
-        * Compile-time asserts that generic FT_* types still match
-        * BTRFS_FT_* types
-        */
-       BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
-       BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
-       BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
-       BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
-       BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
-       BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
-       BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
-       BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
-
        return fs_umode_to_ftype(inode->i_mode);
 }
 
index 8b2538b7027de38ae70c6fab4df3c6155b6e4dca..413e2e72b4e24fd1918b8b9b5d981ccbdac76ea0 100644 (file)
@@ -1527,6 +1527,7 @@ next:
 }
 
 #define CLUSTER_SIZE   (SZ_256K)
+static_assert(IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
 
 /*
  * Defrag one contiguous target range.
@@ -1672,7 +1673,6 @@ static int defrag_one_cluster(struct btrfs_inode *inode,
        LIST_HEAD(target_list);
        int ret;
 
-       BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
        ret = defrag_collect_targets(inode, start, len, extent_thresh,
                                     newer_than, do_compress, false,
                                     &target_list, NULL);
@@ -1815,9 +1815,6 @@ int btrfs_defrag_file(struct inode *inode, struct file_ra_state *ra,
                u64 last_scanned = cur;
                u64 cluster_end;
 
-               /* The cluster size 256K should always be page aligned */
-               BUILD_BUG_ON(!IS_ALIGNED(CLUSTER_SIZE, PAGE_SIZE));
-
                if (btrfs_defrag_cancelled(fs_info)) {
                        ret = -EAGAIN;
                        break;
index 5f4812fd8b509b7f403d3f1eabbe324d3c7ffb80..17389a42a3ab7214971f6f02c3883c8caef31cb0 100644 (file)
@@ -1107,6 +1107,11 @@ static inline struct btrfs_fs_info *to_fs_info(struct kobject *kobj)
 static char btrfs_unknown_feature_names[FEAT_MAX][NUM_FEATURE_BITS][BTRFS_FEATURE_NAME_MAX];
 static struct btrfs_feature_attr btrfs_feature_attrs[FEAT_MAX][NUM_FEATURE_BITS];
 
+static_assert(ARRAY_SIZE(btrfs_unknown_feature_names) ==
+             ARRAY_SIZE(btrfs_feature_attrs));
+static_assert(ARRAY_SIZE(btrfs_unknown_feature_names[0]) ==
+             ARRAY_SIZE(btrfs_feature_attrs[0]));
+
 static const u64 supported_feature_masks[FEAT_MAX] = {
        [FEAT_COMPAT]    = BTRFS_FEATURE_COMPAT_SUPP,
        [FEAT_COMPAT_RO] = BTRFS_FEATURE_COMPAT_RO_SUPP,
@@ -1275,11 +1280,6 @@ static void init_feature_attrs(void)
        struct btrfs_feature_attr *fa;
        int set, i;
 
-       BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names) !=
-                    ARRAY_SIZE(btrfs_feature_attrs));
-       BUILD_BUG_ON(ARRAY_SIZE(btrfs_unknown_feature_names[0]) !=
-                    ARRAY_SIZE(btrfs_feature_attrs[0]));
-
        memset(btrfs_feature_attrs, 0, sizeof(btrfs_feature_attrs));
        memset(btrfs_unknown_feature_names, 0,
               sizeof(btrfs_unknown_feature_names));