summaryrefslogtreecommitdiff
path: root/libbcache/str_hash.h
diff options
context:
space:
mode:
Diffstat (limited to 'libbcache/str_hash.h')
-rw-r--r--libbcache/str_hash.h84
1 files changed, 54 insertions, 30 deletions
diff --git a/libbcache/str_hash.h b/libbcache/str_hash.h
index a489304..b14d05c 100644
--- a/libbcache/str_hash.h
+++ b/libbcache/str_hash.h
@@ -3,37 +3,74 @@
#include "btree_iter.h"
#include "checksum.h"
+#include "inode.h"
#include "siphash.h"
#include "super.h"
-#include <crypto/sha1_base.h>
#include <linux/crc32c.h>
+#include <crypto/hash.h>
-static const SIPHASH_KEY bch_siphash_key = {
- .k0 = cpu_to_le64(0x5a9585fd80087730ULL),
- .k1 = cpu_to_le64(0xc8de666d50b45664ULL ),
+struct bch_hash_info {
+ u8 type;
+ union {
+ __le64 crc_key;
+ SIPHASH_KEY siphash_key;
+ };
};
+static inline struct bch_hash_info
+bch_hash_info_init(const struct bch_inode_unpacked *bi)
+{
+ /* XXX ick */
+ struct bch_hash_info info = {
+ .type = (bi->i_flags >> INODE_STR_HASH_OFFSET) &
+ ~(~0 << INODE_STR_HASH_BITS)
+ };
+
+ switch (info.type) {
+ case BCH_STR_HASH_CRC32C:
+ case BCH_STR_HASH_CRC64:
+ info.crc_key = bi->i_hash_seed;
+ break;
+ case BCH_STR_HASH_SIPHASH: {
+ SHASH_DESC_ON_STACK(desc, bch_sha256);
+ u8 digest[crypto_shash_digestsize(bch_sha256)];
+
+ desc->tfm = bch_sha256;
+ desc->flags = 0;
+
+ crypto_shash_digest(desc, (void *) &bi->i_hash_seed,
+ sizeof(bi->i_hash_seed), digest);
+ memcpy(&info.siphash_key, digest, sizeof(info.siphash_key));
+ break;
+ }
+ default:
+ BUG();
+ }
+
+ return info;
+}
+
struct bch_str_hash_ctx {
union {
- u32 crc32c;
- u64 crc64;
- SIPHASH_CTX siphash;
+ u32 crc32c;
+ u64 crc64;
+ SIPHASH_CTX siphash;
};
};
static inline void bch_str_hash_init(struct bch_str_hash_ctx *ctx,
- enum bch_str_hash_type type)
+ const struct bch_hash_info *info)
{
- switch (type) {
+ switch (info->type) {
case BCH_STR_HASH_CRC32C:
- ctx->crc32c = ~0;
+ ctx->crc32c = crc32c(~0, &info->crc_key, sizeof(info->crc_key));
break;
case BCH_STR_HASH_CRC64:
- ctx->crc64 = ~0;
+ ctx->crc64 = bch_crc64_update(~0, &info->crc_key, sizeof(info->crc_key));
break;
case BCH_STR_HASH_SIPHASH:
- SipHash24_Init(&ctx->siphash, &bch_siphash_key);
+ SipHash24_Init(&ctx->siphash, &info->siphash_key);
break;
default:
BUG();
@@ -41,10 +78,10 @@ static inline void bch_str_hash_init(struct bch_str_hash_ctx *ctx,
}
static inline void bch_str_hash_update(struct bch_str_hash_ctx *ctx,
- enum bch_str_hash_type type,
- const void *data, size_t len)
+ const struct bch_hash_info *info,
+ const void *data, size_t len)
{
- switch (type) {
+ switch (info->type) {
case BCH_STR_HASH_CRC32C:
ctx->crc32c = crc32c(ctx->crc32c, data, len);
break;
@@ -60,9 +97,9 @@ static inline void bch_str_hash_update(struct bch_str_hash_ctx *ctx,
}
static inline u64 bch_str_hash_end(struct bch_str_hash_ctx *ctx,
- enum bch_str_hash_type type)
+ const struct bch_hash_info *info)
{
- switch (type) {
+ switch (info->type) {
case BCH_STR_HASH_CRC32C:
return ctx->crc32c;
case BCH_STR_HASH_CRC64:
@@ -74,19 +111,6 @@ static inline u64 bch_str_hash_end(struct bch_str_hash_ctx *ctx,
}
}
-struct bch_hash_info {
- u64 seed;
- u8 type;
-};
-
-static inline struct bch_hash_info bch_hash_info_init(const struct bch_inode *bi)
-{
- return (struct bch_hash_info) {
- .seed = le64_to_cpu(bi->i_hash_seed),
- .type = INODE_STR_HASH_TYPE(bi),
- };
-}
-
struct bch_hash_desc {
enum btree_id btree_id;
u8 key_type;