bcachefs: properly initialize used values
authorDan Robertson <dan@dlrobertson.com>
Sat, 15 May 2021 00:02:44 +0000 (20:02 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:03 +0000 (17:09 -0400)
 - Ensure the second key value in bch_hash_info is initialized to zero
   if the info type is of type BCH_STR_HASH_SIPHASH.

 - Initialize the possibly returned value in bch2_inode_create. Assuming
   bch2_btree_iter_peek returns bkey_s_c_null, the uninitialized value
   of ret could be returned to the user as an error pointer.

 - Fix compiler warning in initialization of bkey_s_c_stripe

fs/bcachefs/buckets.c:1646:35: warning: suggest braces around initialization
of subobject [-Wmissing-braces]
        struct bkey_s_c_stripe new_s = { NULL };
                                         ^~~~

Signed-off-by: Dan Robertson <dan@dlrobertson.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/buckets.c
fs/bcachefs/inode.c
fs/bcachefs/str_hash.h

index 8806e8306e4ef504c46f8bc68dcb64954e1d38dc..dad1c7d27bab5dc37391dabcf7e290e25d7d661d 100644 (file)
@@ -1646,8 +1646,8 @@ static int bch2_trans_mark_stripe(struct btree_trans *trans,
                                  struct bkey_s_c old, struct bkey_s_c new,
                                  unsigned flags)
 {
-       struct bkey_s_c_stripe old_s = { NULL };
-       struct bkey_s_c_stripe new_s = { NULL };
+       struct bkey_s_c_stripe old_s = { .k = NULL };
+       struct bkey_s_c_stripe new_s = { .k = NULL };
        struct bch_replicas_padded r;
        unsigned i;
        int ret = 0;
index 66b0bc01c75e873b8425c1a901fd600ca62e0c38..2ae55467c583132fb22c4f3c2f54209dd3ef3cd9 100644 (file)
@@ -478,7 +478,7 @@ struct btree_iter *bch2_inode_create(struct btree_trans *trans,
        struct btree_iter *iter = NULL;
        struct bkey_s_c k;
        u64 min, max, start, pos, *hint;
-       int ret;
+       int ret = 0;
 
        u64 cpu = raw_smp_processor_id();
        unsigned bits = (c->opts.inodes_32bit
index b85f895de34642b69a2aba132cb693f4d992460a..eab669af7032089a674de63735f5a9630009c1a0 100644 (file)
@@ -33,10 +33,11 @@ bch2_str_hash_opt_to_type(struct bch_fs *c, enum bch_str_hash_opts opt)
 
 struct bch_hash_info {
        u8                      type;
-       union {
-               __le64          crc_key;
-               SIPHASH_KEY     siphash_key;
-       };
+       /*
+        * For crc32 or crc64 string hashes the first key value of
+        * the siphash_key (k0) is used as the key.
+        */
+       SIPHASH_KEY     siphash_key;
 };
 
 static inline struct bch_hash_info
@@ -46,7 +47,7 @@ bch2_hash_info_init(struct bch_fs *c, const struct bch_inode_unpacked *bi)
        struct bch_hash_info info = {
                .type = (bi->bi_flags >> INODE_STR_HASH_OFFSET) &
                        ~(~0U << INODE_STR_HASH_BITS),
-               .crc_key = bi->bi_hash_seed,
+               .siphash_key = { .k0 = bi->bi_hash_seed }
        };
 
        if (unlikely(info.type == BCH_STR_HASH_SIPHASH_OLD)) {
@@ -76,10 +77,12 @@ static inline void bch2_str_hash_init(struct bch_str_hash_ctx *ctx,
 {
        switch (info->type) {
        case BCH_STR_HASH_CRC32C:
-               ctx->crc32c = crc32c(~0, &info->crc_key, sizeof(info->crc_key));
+               ctx->crc32c = crc32c(~0, &info->siphash_key.k0,
+                                    sizeof(info->siphash_key.k0));
                break;
        case BCH_STR_HASH_CRC64:
-               ctx->crc64 = crc64_be(~0, &info->crc_key, sizeof(info->crc_key));
+               ctx->crc64 = crc64_be(~0, &info->siphash_key.k0,
+                                     sizeof(info->siphash_key.k0));
                break;
        case BCH_STR_HASH_SIPHASH_OLD:
        case BCH_STR_HASH_SIPHASH: