summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKent Overstreet <kent.overstreet@gmail.com>2016-09-17 15:58:24 -0800
committerKent Overstreet <kent.overstreet@gmail.com>2016-09-17 17:09:19 -0800
commit81973bd4cbcd44e0ee3ea456bfd3d0e8412d7349 (patch)
treeda06c2538259f86ede72697baab4acd66c2c51e3
parent088cb2b3cb26baab7e12a3550efa8ac3b236fa2e (diff)
bcache: fix sha1 usage
Ugly, but sha1 is going away in the next on disk format change
-rw-r--r--drivers/md/bcache/dirent.c31
-rw-r--r--drivers/md/bcache/str_hash.h16
-rw-r--r--drivers/md/bcache/super.c9
-rw-r--r--drivers/md/bcache/super.h1
-rw-r--r--drivers/md/bcache/xattr.c35
5 files changed, 64 insertions, 28 deletions
diff --git a/drivers/md/bcache/dirent.c b/drivers/md/bcache/dirent.c
index b81c64683d07..47b4a365a7d8 100644
--- a/drivers/md/bcache/dirent.c
+++ b/drivers/md/bcache/dirent.c
@@ -21,15 +21,34 @@ static unsigned dirent_name_bytes(struct bkey_s_c_dirent d)
static u64 bch_dirent_hash(const struct bch_hash_info *info,
const struct qstr *name)
{
- struct bch_str_hash_ctx ctx;
+ switch (info->type) {
+ case BCH_STR_HASH_SHA1: {
+ SHASH_DESC_ON_STACK(desc, bch_sha1);
+ u8 digest[SHA1_DIGEST_SIZE];
+ u64 ret;
+ desc->tfm = bch_sha1;
+ desc->flags = 0;
+ crypto_shash_init(desc);
+
+ crypto_shash_update(desc, (void *) &info->seed, sizeof(info->seed));
+
+ crypto_shash_update(desc, (void *) name->name, name->len);
+ crypto_shash_final(desc, digest);
+ memcpy(&ret, &digest, sizeof(ret));
+ return max_t(u64, ret >> 1, 2);
+ }
+ default: {
+ struct bch_str_hash_ctx ctx;
- bch_str_hash_init(&ctx, info->type);
- bch_str_hash_update(&ctx, info->type, &info->seed, sizeof(info->seed));
+ bch_str_hash_init(&ctx, info->type);
+ bch_str_hash_update(&ctx, info->type, &info->seed, sizeof(info->seed));
- bch_str_hash_update(&ctx, info->type, name->name, name->len);
+ bch_str_hash_update(&ctx, info->type, name->name, name->len);
- /* [0,2) reserved for dots */
- return max_t(u64, bch_str_hash_end(&ctx, info->type), 2);
+ /* [0,2) reserved for dots */
+ return max_t(u64, bch_str_hash_end(&ctx, info->type), 2);
+ }
+ }
}
static u64 dirent_hash_key(const struct bch_hash_info *info, const void *key)
diff --git a/drivers/md/bcache/str_hash.h b/drivers/md/bcache/str_hash.h
index e41cb715a20b..f6a441b7be00 100644
--- a/drivers/md/bcache/str_hash.h
+++ b/drivers/md/bcache/str_hash.h
@@ -4,6 +4,7 @@
#include "btree_iter.h"
#include "checksum.h"
#include "siphash.h"
+#include "super.h"
#include <crypto/sha1_base.h>
#include <linux/crc32c.h>
@@ -18,7 +19,6 @@ struct bch_str_hash_ctx {
u32 crc32c;
u64 crc64;
SIPHASH_CTX siphash;
- struct shash_desc sha1;
};
};
@@ -35,9 +35,6 @@ static inline void bch_str_hash_init(struct bch_str_hash_ctx *ctx,
case BCH_STR_HASH_SIPHASH:
SipHash24_Init(&ctx->siphash, &bch_siphash_key);
break;
- case BCH_STR_HASH_SHA1:
- sha1_base_init(&ctx->sha1);
- break;
default:
BUG();
}
@@ -57,9 +54,6 @@ static inline void bch_str_hash_update(struct bch_str_hash_ctx *ctx,
case BCH_STR_HASH_SIPHASH:
SipHash24_Update(&ctx->siphash, data, len);
break;
- case BCH_STR_HASH_SHA1:
- crypto_sha1_update(&ctx->sha1, data, len);
- break;
default:
BUG();
}
@@ -75,14 +69,6 @@ static inline u64 bch_str_hash_end(struct bch_str_hash_ctx *ctx,
return ctx->crc64 >> 1;
case BCH_STR_HASH_SIPHASH:
return SipHash24_End(&ctx->siphash) >> 1;
- case BCH_STR_HASH_SHA1: {
- u8 out[SHA1_DIGEST_SIZE];
- u64 ret;
-
- crypto_sha1_finup(&ctx->sha1, NULL, 0, out);
- memcpy(&ret, &out, sizeof(ret));
- return ret >> 1;
- }
default:
BUG();
}
diff --git a/drivers/md/bcache/super.c b/drivers/md/bcache/super.c
index 09086dc01fdb..f3558e573c07 100644
--- a/drivers/md/bcache/super.c
+++ b/drivers/md/bcache/super.c
@@ -43,6 +43,7 @@
#include <linux/random.h>
#include <linux/reboot.h>
#include <linux/sysfs.h>
+#include <crypto/hash.h>
#include <trace/events/bcache.h>
@@ -65,8 +66,8 @@ static struct class *bch_chardev_class;
static struct device *bch_chardev;
static DEFINE_IDR(bch_chardev_minor);
static DECLARE_WAIT_QUEUE_HEAD(bch_read_only_wait);
-
struct workqueue_struct *bcache_io_wq;
+struct crypto_shash *bch_sha1;
static void bch_cache_stop(struct cache *);
static int bch_cache_online(struct cache *);
@@ -2397,6 +2398,8 @@ static void bcache_exit(void)
class_destroy(bch_chardev_class);
if (bch_chardev_major > 0)
unregister_chrdev(bch_chardev_major, "bcache");
+ if (!IS_ERR_OR_NULL(bch_sha1))
+ crypto_free_shash(bch_sha1);
unregister_reboot_notifier(&reboot);
}
@@ -2420,6 +2423,10 @@ static int __init bcache_init(void)
closure_debug_init();
bkey_pack_test();
+ bch_sha1 = crypto_alloc_shash("sha1", 0, 0);
+ if (IS_ERR(bch_sha1))
+ goto err;
+
bch_chardev_major = register_chrdev(0, "bcache-ctl", &bch_chardev_fops);
if (bch_chardev_major < 0)
goto err;
diff --git a/drivers/md/bcache/super.h b/drivers/md/bcache/super.h
index 24f9b0ead9c8..517047938aa2 100644
--- a/drivers/md/bcache/super.h
+++ b/drivers/md/bcache/super.h
@@ -184,6 +184,7 @@ extern struct mutex bch_register_lock;
extern struct list_head bch_cache_sets;
extern struct idr bch_cache_set_minor;
extern struct workqueue_struct *bcache_io_wq;
+extern struct crypto_shash *bch_sha1;
extern struct kobj_type bch_cache_set_ktype;
extern struct kobj_type bch_cache_set_internal_ktype;
diff --git a/drivers/md/bcache/xattr.c b/drivers/md/bcache/xattr.c
index bfaf2875e229..b5686e472ab9 100644
--- a/drivers/md/bcache/xattr.c
+++ b/drivers/md/bcache/xattr.c
@@ -9,6 +9,7 @@
#include <linux/posix_acl_xattr.h>
#include <linux/xattr.h>
+#include <crypto/hash.h>
struct xattr_search_key {
u8 type;
@@ -21,15 +22,37 @@ struct xattr_search_key {
static u64 bch_xattr_hash(const struct bch_hash_info *info,
const struct xattr_search_key *key)
{
- struct bch_str_hash_ctx ctx;
+ switch (info->type) {
+ case BCH_STR_HASH_SHA1: {
+ SHASH_DESC_ON_STACK(desc, bch_sha1);
+ u8 digest[SHA1_DIGEST_SIZE];
+ u64 ret;
- bch_str_hash_init(&ctx, info->type);
- bch_str_hash_update(&ctx, info->type, &info->seed, sizeof(info->seed));
+ desc->tfm = bch_sha1;
+ desc->flags = 0;
+ crypto_shash_init(desc);
- bch_str_hash_update(&ctx, info->type, &key->type, sizeof(key->type));
- bch_str_hash_update(&ctx, info->type, key->name.name, key->name.len);
+ crypto_shash_update(desc, (void *) &info->seed, sizeof(info->seed));
- return bch_str_hash_end(&ctx, info->type);
+ crypto_shash_update(desc, (void *) &key->type, sizeof(key->type));
+ crypto_shash_update(desc, (void *) key->name.name, key->name.len);
+
+ crypto_shash_final(desc, digest);
+ memcpy(&ret, &digest, sizeof(ret));
+ return ret >> 1;
+ }
+ default: {
+ struct bch_str_hash_ctx ctx;
+
+ bch_str_hash_init(&ctx, info->type);
+ bch_str_hash_update(&ctx, info->type, &info->seed, sizeof(info->seed));
+
+ bch_str_hash_update(&ctx, info->type, &key->type, sizeof(key->type));
+ bch_str_hash_update(&ctx, info->type, key->name.name, key->name.len);
+
+ return bch_str_hash_end(&ctx, info->type);
+ }
+ }
}
#define xattr_val(_xattr) ((_xattr)->x_name + (_xattr)->x_name_len)