* [RFC PATCH 0/3] Btrfs: add xxhash algorithm
@ 2014-05-07 10:56 Liu Bo
2014-05-07 10:56 ` [PATCH 1/3] Crypto: " Liu Bo
` (4 more replies)
0 siblings, 5 replies; 9+ messages in thread
From: Liu Bo @ 2014-05-07 10:56 UTC (permalink / raw)
To: linux-btrfs
"xxHash is an extremely fast non-cryptographic Hash algorithm, working at speeds
close to RAM limits."[1] And xxhash is 32-bits hash, same as crc32.
Here is the hash comparsion extracted from the link[1]:
(single thread, Windows Seven 32 bits, using Open Source's SMHasher on a Core 2
Duo @3GHz)
--------------------------------------------
Name Speed Q.Score Author
xxHash 5.4 GB/s 10
CRC32 0.43 GB/s 9
--------------------------------------------
This patch set adds xxhash into linux kernel and then modifies btrfs's checksum
API a bit and adopts xxhash as an alternative checksum algorithm.
At the very first stage of RFC, I only ran xfstests through to make sure it
can work. A bunch of performance tests will be made in the future.
Note:
We need to update btrfs-progs side as well to set it up, I attach a hacky
patch just for users to play with ;-)
[1]: https://code.google.com/p/xxhash/
Liu Bo (3):
Crypto: add xxhash algorithm
Crypto: xxhash: add tests
Btrfs: add another checksum algorithm xxhash
crypto/Kconfig | 7 +
crypto/Makefile | 1 +
crypto/testmgr.c | 10 ++
crypto/testmgr.h | 33 ++++
crypto/xxhash.c | 383 ++++++++++++++++++++++++++++++++++++++++++++
fs/btrfs/Kconfig | 22 +++
fs/btrfs/compression.c | 6 +-
fs/btrfs/ctree.h | 12 +-
fs/btrfs/dir-item.c | 10 +-
fs/btrfs/disk-io.c | 126 ++++++++-------
fs/btrfs/disk-io.h | 2 -
fs/btrfs/extent-tree.c | 43 +++--
fs/btrfs/file-item.c | 9 +-
fs/btrfs/free-space-cache.c | 15 +-
fs/btrfs/hash.c | 75 +++++++--
fs/btrfs/hash.h | 22 ++-
fs/btrfs/inode-item.c | 6 +-
fs/btrfs/inode.c | 16 +-
fs/btrfs/props.c | 37 ++++-
fs/btrfs/props.h | 3 +-
fs/btrfs/scrub.c | 70 ++++++--
fs/btrfs/send.c | 7 +-
fs/btrfs/super.c | 9 +-
fs/btrfs/tree-log.c | 2 +-
include/crypto/xxhash.h | 209 ++++++++++++++++++++++++
25 files changed, 974 insertions(+), 161 deletions(-)
create mode 100644 crypto/xxhash.c
create mode 100644 include/crypto/xxhash.h
--
1.8.1.4
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH 1/3] Crypto: add xxhash algorithm
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
@ 2014-05-07 10:56 ` Liu Bo
2014-05-07 10:56 ` [PATCH 2/3] Crypto: xxhash: add tests Liu Bo
` (3 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Liu Bo @ 2014-05-07 10:56 UTC (permalink / raw)
To: linux-btrfs
This will be used in btrfs, and maybe in others in the future.
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
crypto/Kconfig | 7 +
crypto/Makefile | 1 +
crypto/xxhash.c | 383 ++++++++++++++++++++++++++++++++++++++++++++++++
include/crypto/xxhash.h | 209 ++++++++++++++++++++++++++
4 files changed, 600 insertions(+)
create mode 100644 crypto/xxhash.c
create mode 100644 include/crypto/xxhash.h
diff --git a/crypto/Kconfig b/crypto/Kconfig
index ce4012a..2e56de0 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -622,6 +622,13 @@ config CRYPTO_GHASH_CLMUL_NI_INTEL
GHASH is message digest algorithm for GCM (Galois/Counter Mode).
The implementation is accelerated by CLMUL-NI of Intel.
+config CRYPTO_XXH32
+ tristate "XXHASH digest algorithm"
+ select CRYPTO_HASH
+ help
+ xxHash - Fast Hash Algorithm
+ source repository : http://code.google.com/p/xxhash/
+
comment "Ciphers"
config CRYPTO_AES
diff --git a/crypto/Makefile b/crypto/Makefile
index 38e64231..7c3f363 100644
--- a/crypto/Makefile
+++ b/crypto/Makefile
@@ -97,6 +97,7 @@ obj-$(CONFIG_CRYPTO_GHASH) += ghash-generic.o
obj-$(CONFIG_CRYPTO_USER_API) += af_alg.o
obj-$(CONFIG_CRYPTO_USER_API_HASH) += algif_hash.o
obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o
+obj-$(CONFIG_CRYPTO_XXH32) += xxhash.o
#
# generic algorithms and the async_tx api
diff --git a/crypto/xxhash.c b/crypto/xxhash.c
new file mode 100644
index 0000000..b84c7cf
--- /dev/null
+++ b/crypto/xxhash.c
@@ -0,0 +1,383 @@
+/*
+ * xxHash - Fast Hash algorithm
+ * Copyright (C) 2012-2014, Yann Collet.
+ * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
+
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the distribution.
+
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ * You can contact the author at :
+ * xxHash source repository : http://code.google.com/p/xxhash/
+ */
+
+#include <crypto/internal/hash.h>
+#include <crypto/xxhash.h>
+#include <linux/string.h>
+#include <linux/mm.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/types.h>
+#include <linux/slab.h>
+
+static inline u32 XXH_readLE32_align(const u32 * ptr, XXH_endianess endian,
+ XXH_alignment align)
+{
+ if (align == XXH_unaligned)
+ return endian ==
+ XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
+ else
+ return endian == XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
+}
+
+static inline u32 XXH_readLE32(const u32 * ptr, XXH_endianess endian)
+{
+ return XXH_readLE32_align(ptr, endian, XXH_unaligned);
+}
+
+/* Simple Hash Functions */
+static inline u32 XXH32_endian_align(const void *input, int len, u32 seed,
+ XXH_endianess endian,
+ XXH_alignment align)
+{
+ const u8 *p = (const u8 *)input;
+ const u8 *const bEnd = p + len;
+ u32 h32;
+
+#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
+ if (p == NULL) {
+ len = 0;
+ p = (const u8 *)(size_t) 16;
+ }
+#endif
+ if (len >= 16) {
+ const u8 *const limit = bEnd - 16;
+ u32 v1 = seed + PRIME32_1 + PRIME32_2;
+ u32 v2 = seed + PRIME32_2;
+ u32 v3 = seed + 0;
+ u32 v4 = seed - PRIME32_1;
+ u32 tmp;
+
+ do {
+ tmp = XXH_readLE32_align((const u32 *)p, endian, align);
+ v1 += tmp * PRIME32_2;
+ v1 = XXH_rotl32(v1, 13);
+ v1 *= PRIME32_1;
+ p += 4;
+
+ tmp = XXH_readLE32_align((const u32 *)p, endian, align);
+ v2 += tmp * PRIME32_2;
+ v2 = XXH_rotl32(v2, 13);
+ v2 *= PRIME32_1;
+ p += 4;
+
+ tmp = XXH_readLE32_align((const u32 *)p, endian, align);
+ v3 += tmp * PRIME32_2;
+ v3 = XXH_rotl32(v3, 13);
+ v3 *= PRIME32_1;
+ p += 4;
+
+ tmp = XXH_readLE32_align((const u32 *)p, endian, align);
+ v4 += tmp * PRIME32_2;
+ v4 = XXH_rotl32(v4, 13);
+ v4 *= PRIME32_1;
+ p += 4;
+ } while (p <= limit);
+
+ h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) +
+ XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
+ } else {
+ h32 = seed + PRIME32_5;
+ }
+
+ h32 += (u32) len;
+
+ while (p <= bEnd - 4) {
+ h32 += XXH_readLE32_align((const u32 *)p, endian, align) *
+ PRIME32_3;
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4;
+ p += 4;
+ }
+
+ while (p < bEnd) {
+ h32 += (*p) * PRIME32_5;
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1;
+ p++;
+ }
+
+ h32 ^= h32 >> 15;
+ h32 *= PRIME32_2;
+ h32 ^= h32 >> 13;
+ h32 *= PRIME32_3;
+ h32 ^= h32 >> 16;
+
+ return h32;
+}
+
+static int XXH32_digest(struct shash_desc *desc, const u8 *data,
+ unsigned int len, u8 *out)
+
+//const void *input, int len, u32 seed)
+
+{
+ u32 seed = 0;
+ u32 dst;
+
+ XXH_endianess endian_detected = (XXH_endianess) XXH_CPU_LITTLE_ENDIAN;
+
+#if !defined(XXH_USE_UNALIGNED_ACCESS)
+ /* Input is aligned, let's leverage the speed advantage */
+ if ((((size_t) data) & 3)) {
+ if ((endian_detected == XXH_littleEndian) ||
+ XXH_FORCE_NATIVE_FORMAT)
+ dst = XXH32_endian_align(data, len, seed,
+ XXH_littleEndian,
+ XXH_aligned);
+ else
+ dst = XXH32_endian_align(data, len, seed,
+ XXH_bigEndian, XXH_aligned);
+ }
+
+#endif
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ dst = XXH32_endian_align(data, len, seed, XXH_littleEndian,
+ XXH_unaligned);
+ else
+ dst = XXH32_endian_align(data, len, seed, XXH_bigEndian,
+ XXH_unaligned);
+
+ *(__le32 *)out = cpu_to_le32(dst);
+
+ return 0;
+}
+
+/* Advanced Hash Functions */
+static int XXH32_resetState(void *state_in, u32 seed)
+{
+ struct xxhash_state *state = (struct xxhash_state *)state_in;
+ state->seed = seed;
+ state->v1 = seed + PRIME32_1 + PRIME32_2;
+ state->v2 = seed + PRIME32_2;
+ state->v3 = seed + 0;
+ state->v4 = seed - PRIME32_1;
+ state->total_len = 0;
+ state->memsize = 0;
+ return 0;
+}
+
+static int XXH32_init(struct shash_desc *desc)
+{
+ struct xxhash_state *sctx = shash_desc_ctx(desc);
+ XXH32_resetState(sctx, 0);
+
+ return 0;
+}
+
+static inline int XXH32_update_endian(void *state_in, const void *input,
+ int len, XXH_endianess endian)
+{
+ struct xxhash_state *state = (struct xxhash_state *)state_in;
+ const u8 *p = (const u8 *)input;
+ const u8 *const bEnd = p + len;
+
+#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
+ if (input == NULL)
+ return -EINVAL;
+#endif
+ state->total_len += len;
+
+ if (state->memsize + len < 16) { // fill in tmp buffer
+ memcpy(state->memory + state->memsize, input, len);
+ state->memsize += len;
+ return 0;
+ }
+
+ if (state->memsize) { // some data left from previous update
+ memcpy(state->memory + state->memsize, input, 16 - state->memsize);
+
+ {
+ const u32 *p32 = (const u32 *)state->memory;
+ state->v1 += XXH_readLE32(p32, endian) * PRIME32_2;
+ state->v1 = XXH_rotl32(state->v1, 13);
+ state->v1 *= PRIME32_1;
+ p32++;
+ state->v2 += XXH_readLE32(p32, endian) * PRIME32_2;
+ state->v2 = XXH_rotl32(state->v2, 13);
+ state->v2 *= PRIME32_1;
+ p32++;
+ state->v3 += XXH_readLE32(p32, endian) * PRIME32_2;
+ state->v3 = XXH_rotl32(state->v3, 13);
+ state->v3 *= PRIME32_1;
+ p32++;
+ state->v4 += XXH_readLE32(p32, endian) * PRIME32_2;
+ state->v4 = XXH_rotl32(state->v4, 13);
+ state->v4 *= PRIME32_1;
+ p32++;
+ }
+
+ p += 16 - state->memsize;
+ state->memsize = 0;
+ }
+ if (p <= bEnd - 16) {
+ const u8 *const limit = bEnd - 16;
+ u32 v1 = state->v1;
+ u32 v2 = state->v2;
+ u32 v3 = state->v3;
+ u32 v4 = state->v4;
+
+ do {
+ v1 += XXH_readLE32((const u32 *)p, endian) * PRIME32_2;
+ v1 = XXH_rotl32(v1, 13);
+ v1 *= PRIME32_1;
+ p += 4;
+
+ v2 += XXH_readLE32((const u32 *)p, endian) * PRIME32_2;
+ v2 = XXH_rotl32(v2, 13);
+ v2 *= PRIME32_1;
+ p += 4;
+
+ v3 += XXH_readLE32((const u32 *)p, endian) * PRIME32_2;
+ v3 = XXH_rotl32(v3, 13);
+ v3 *= PRIME32_1;
+ p += 4;
+
+ v4 += XXH_readLE32((const u32 *)p, endian) * PRIME32_2;
+ v4 = XXH_rotl32(v4, 13);
+ v4 *= PRIME32_1;
+ p += 4;
+ } while (p <= limit);
+
+ state->v1 = v1;
+ state->v2 = v2;
+ state->v3 = v3;
+ state->v4 = v4;
+ }
+
+ if (p < bEnd) {
+ memcpy(state->memory, p, bEnd - p);
+ state->memsize = (int)(bEnd - p);
+ }
+
+ return 0;
+}
+
+static int XXH32_update(struct shash_desc *desc, const u8 *data,
+ unsigned int len)
+{
+ struct xxhash_state *sctx = shash_desc_ctx(desc);
+
+ XXH_endianess endian_detected = (XXH_endianess) XXH_CPU_LITTLE_ENDIAN;
+
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_update_endian(sctx, data, len, XXH_littleEndian);
+ else
+ return XXH32_update_endian(sctx, data, len, XXH_bigEndian);
+}
+
+static inline u32 XXH32_intermediateDigest_endian(void *state_in,
+ XXH_endianess endian)
+{
+ struct xxhash_state *state = (struct xxhash_state *)state_in;
+ const u8 *p = (const u8 *)state->memory;
+ u8 * bEnd = (u8 *) state->memory + state->memsize;
+ u32 h32;
+
+ if (state->total_len >= 16) {
+ h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) +
+ XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
+ } else {
+ h32 = state->seed + PRIME32_5;
+ }
+
+ h32 += (u32)state->total_len;
+
+ while (p <= bEnd - 4) {
+ h32 += XXH_readLE32((const u32 *)p, endian) * PRIME32_3;
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4;
+ p += 4;
+ }
+
+ while (p < bEnd) {
+ h32 += (*p) * PRIME32_5;
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1;
+ p++;
+ }
+
+ h32 ^= h32 >> 15;
+ h32 *= PRIME32_2;
+ h32 ^= h32 >> 13;
+ h32 *= PRIME32_3;
+ h32 ^= h32 >> 16;
+
+ return h32;
+}
+
+static u32 XXH32_intermediateDigest(void *state_in)
+{
+ XXH_endianess endian_detected = (XXH_endianess) XXH_CPU_LITTLE_ENDIAN;
+
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_intermediateDigest_endian(state_in,
+ XXH_littleEndian);
+ else
+ return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian);
+}
+
+static int XXH32_final(struct shash_desc *desc, u8 *out)
+{
+ struct xxhash_state *sctx = shash_desc_ctx(desc);
+ u32 dst = XXH32_intermediateDigest(sctx);
+
+ *(__le32 *)out = cpu_to_le32(dst);
+
+ return 0;
+}
+
+static struct shash_alg alg = {
+ .digestsize = XXH32_DIGEST_SIZE,
+ .init = XXH32_init,
+ .update = XXH32_update,
+ .final = XXH32_final,
+ .digest = XXH32_digest,
+ .descsize = sizeof(struct xxhash_state),
+ .statesize = sizeof(struct xxhash_state),
+ .base = {
+ .cra_name = "xxh32",
+ .cra_blocksize = XXH32_BLOCK_SIZE,
+ .cra_module = THIS_MODULE,
+ }
+};
+
+static int __init xxh32_mod_init(void)
+{
+ return crypto_register_shash(&alg);
+}
+
+static void __exit xxh32_mod_fini(void)
+{
+ crypto_unregister_shash(&alg);
+}
+
+module_init(xxh32_mod_init);
+module_exit(xxh32_mod_fini);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("XXHASH FAST Hash Algorithm");
+MODULE_ALIAS("xxhash");
diff --git a/include/crypto/xxhash.h b/include/crypto/xxhash.h
new file mode 100644
index 0000000..04e1b90
--- /dev/null
+++ b/include/crypto/xxhash.h
@@ -0,0 +1,209 @@
+/*
+ xxHash - Fast Hash algorithm
+ Header File
+ Copyright (C) 2012-2014, Yann Collet.
+ BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the
+ distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ You can contact the author at :
+ - xxHash source repository : http://code.google.com/p/xxhash/
+*/
+
+/* Notice extracted from xxHash homepage :
+
+xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
+It also successfully passes all tests from the SMHasher suite.
+
+Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
+
+Name Speed Q.Score Author
+xxHash 5.4 GB/s 10
+CrapWow 3.2 GB/s 2 Andrew
+MumurHash 3a 2.7 GB/s 10 Austin Appleby
+SpookyHash 2.0 GB/s 10 Bob Jenkins
+SBox 1.4 GB/s 9 Bret Mulvey
+Lookup3 1.2 GB/s 9 Bob Jenkins
+SuperFastHash 1.2 GB/s 1 Paul Hsieh
+CityHash64 1.05 GB/s 10 Pike & Alakuijala
+FNV 0.55 GB/s 5 Fowler, Noll, Vo
+CRC32 0.43 GB/s 9
+MD5-32 0.33 GB/s 10 Ronald L. Rivest
+SHA1-32 0.28 GB/s 10
+
+Q.Score is a measure of quality of the hash function.
+It depends on successfully passing SMHasher test set.
+10 is a perfect score.
+*/
+
+#ifndef _CRPTO_XXHASH_H
+#define _CRPTO_XXHASH_H
+
+#include <linux/types.h>
+
+#define XXH32_DIGEST_SIZE 4
+#define XXH32_BLOCK_SIZE 16
+
+/*
+ * Unaligned memory access is automatically enabled for "common" CPU, such as x86.
+ * For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
+ * If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
+ * You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
+ */
+#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
+ #define XXH_USE_UNALIGNED_ACCESS 1
+#endif
+
+/*
+ * XXH_ACCEPT_NULL_INPUT_POINTER :
+ * If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
+ * When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
+ * This option has a very small performance cost (only measurable on small inputs).
+ * By default, this option is disabled. To enable it, uncomment below define :
+ *
+ * #define XXH_ACCEPT_NULL_INPUT_POINTER 1
+ */
+
+/*
+ * XXH_FORCE_NATIVE_FORMAT :
+ * By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
+ * Results are therefore identical for little-endian and big-endian CPU.
+ * This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
+ * Should endian-independance be of no importance for your application, you may set the #define below to 1.
+ * It will improve speed for Big-endian CPU.
+ * This option has no impact on Little_Endian CPU.
+ */
+#define XXH_FORCE_NATIVE_FORMAT 0
+
+typedef struct _U32_S {
+ u32 v;
+} __attribute__ ((__packed__)) U32_S;
+
+#define A32(x) (((U32_S *)(x))->v)
+
+/* Compiler-specific Functions and Macros */
+#define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
+
+#if GCC_VERSION >= 40300
+ #define XXH_swap32 __builtin_bswap32
+#else
+ static inline u32 XXH_swap32(u32 x)
+ {
+ return ((x << 24) & 0xff000000) |
+ ((x << 8) & 0x00ff0000) |
+ ((x >> 8) & 0x0000ff00) |
+ ((x >> 24) & 0x000000ff);
+ }
+#endif
+
+/* Constants */
+#define PRIME32_1 2654435761U
+#define PRIME32_2 2246822519U
+#define PRIME32_3 3266489917U
+#define PRIME32_4 668265263U
+#define PRIME32_5 374761393U
+
+/* Architecture Macros */
+typedef enum {
+ XXH_bigEndian=0,
+ XXH_littleEndian=1
+} XXH_endianess;
+
+#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
+ static const int one = 1;
+ #define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
+#endif
+
+/* Macros */
+
+/* use only *after* variable declarations */
+#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; }
+
+/* Memory reads */
+typedef enum {
+ XXH_aligned,
+ XXH_unaligned
+} XXH_alignment;
+
+struct xxhash_state {
+ u64 total_len;
+ u32 seed;
+ u32 v1;
+ u32 v2;
+ u32 v3;
+ u32 v4;
+ int memsize;
+ char memory[16];
+};
+
+//****************************
+// Simple Hash Functions
+//****************************
+
+/*
+XXH32() :
+ Calculate the 32-bits hash of sequence of length "len" stored at memory address "input".
+ The memory between input & input+len must be valid (allocated and read-accessible).
+ "seed" can be used to alter the result predictably.
+ This function successfully passes all SMHasher tests.
+ Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
+ Note that "len" is type "int", which means it is limited to 2^31-1.
+ If your data is larger, use the advanced functions below.
+
+unsigned int XXH32 (const void* input, int len, unsigned int seed);
+*/
+
+
+
+//****************************
+// Advanced Hash Functions
+//****************************
+
+/*
+These functions calculate the xxhash of an input provided in several small packets,
+as opposed to an input provided as a single block.
+
+It must be started with :
+void* XXH32_init()
+The function returns a pointer which holds the state of calculation.
+
+This pointer must be provided as "void* state" parameter for XXH32_update().
+XXH32_update() can be called as many times as necessary.
+The user must provide a valid (allocated) input.
+The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
+Note that "len" is type "int", which means it is limited to 2^31-1.
+If your data is larger, it is recommended to chunk your data into blocks
+of size for example 2^30 (1GB) to avoid any "int" overflow issue.
+
+Finally, you can end the calculation anytime, by using XXH32_digest().
+This function returns the final 32-bits hash.
+You must provide the same "void* state" parameter created by XXH32_init().
+Memory will be freed by XXH32_digest().
+
+void* XXH32_init (unsigned int seed);
+XXH_errorcode XXH32_update (void* state, const void* input, int len);
+unsigned int XXH32_final (void* state);
+*/
+
+#endif /* _CRYPTO_XXHASH_H */
--
1.8.1.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 2/3] Crypto: xxhash: add tests
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
2014-05-07 10:56 ` [PATCH 1/3] Crypto: " Liu Bo
@ 2014-05-07 10:56 ` Liu Bo
2014-05-07 10:56 ` [PATCH 3/3] Btrfs: add another checksum algorithm xxhash Liu Bo
` (2 subsequent siblings)
4 siblings, 0 replies; 9+ messages in thread
From: Liu Bo @ 2014-05-07 10:56 UTC (permalink / raw)
To: linux-btrfs
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
crypto/testmgr.c | 10 ++++++++++
crypto/testmgr.h | 33 +++++++++++++++++++++++++++++++++
2 files changed, 43 insertions(+)
diff --git a/crypto/testmgr.c b/crypto/testmgr.c
index dc3cf35..27ba702 100644
--- a/crypto/testmgr.c
+++ b/crypto/testmgr.c
@@ -3153,6 +3153,16 @@ static const struct alg_test_desc alg_test_descs[] = {
}
}
}, {
+ .alg = "xxh32",
+ .test = alg_test_hash,
+ .fips_allowed = 1,
+ .suite = {
+ .hash = {
+ .vecs = xxh32_tv_template,
+ .count = XXH32_TEST_VECTORS
+ }
+ }
+ }, {
.alg = "zlib",
.test = alg_test_pcomp,
.fips_allowed = 1,
diff --git a/crypto/testmgr.h b/crypto/testmgr.h
index 3db83db..8e56884 100644
--- a/crypto/testmgr.h
+++ b/crypto/testmgr.h
@@ -26660,6 +26660,39 @@ static struct hash_testvec michael_mic_tv_template[] = {
}
};
+#define XXH32_TEST_VECTORS 3
+
+static struct hash_testvec xxh32_tv_template[] = {
+ {
+ .plaintext = "\x9e",
+ .psize = 1,
+ .digest = "\xe5\xbe\x5c\xb8",
+ },
+ {
+ .plaintext = "\x9e\xff\x1f\x4b\x5e\x53\x2f\xdd"
+ "\xb5\x54\x4d\x2a\x95\x2b",
+ .psize = 14,
+ .digest = "\xb4\x0a\xaa\xe5",
+ },
+ {
+ .plaintext = "\x9e\xff\x1f\x4b\x5e\x53\x2f\xdd"
+ "\xb5\x54\x4d\x2a\x95\x2b\x57\xae"
+ "\x5d\xba\x74\xe9\xd3\xa6\x4c\x98"
+ "\x30\x60\xc0\x80\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00\x00\x00\x00"
+ "\x00\x00\x00\x00\x00",
+ .psize = 101,
+ .digest = "\x12\xa4\x1a\x1f",
+ }
+};
+
/*
* CRC32C test vectors
*/
--
1.8.1.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH 3/3] Btrfs: add another checksum algorithm xxhash
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
2014-05-07 10:56 ` [PATCH 1/3] Crypto: " Liu Bo
2014-05-07 10:56 ` [PATCH 2/3] Crypto: xxhash: add tests Liu Bo
@ 2014-05-07 10:56 ` Liu Bo
2014-05-07 10:56 ` [PATCH] Btrfs-progs: add xxhash Liu Bo
2014-05-07 11:08 ` [RFC PATCH 0/3] Btrfs: add xxhash algorithm Tomasz Torcz
4 siblings, 0 replies; 9+ messages in thread
From: Liu Bo @ 2014-05-07 10:56 UTC (permalink / raw)
To: linux-btrfs
"xxHash is an extremely fast non-cryptographic Hash algorithm, working at speeds
close to RAM limits."[1] And xxhash is 32-bits hash, same as crc32.
This modifies btrfs's checksum API a bit and adopts xxhash as an alternative
checksum algorithm.
Note: We needs to update btrfs-progs side as well to set it up.
[1]: https://code.google.com/p/xxhash/
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
fs/btrfs/Kconfig | 22 ++++++++
fs/btrfs/compression.c | 6 +--
fs/btrfs/ctree.h | 12 +++--
fs/btrfs/dir-item.c | 10 ++--
fs/btrfs/disk-io.c | 126 ++++++++++++++++++++++++--------------------
fs/btrfs/disk-io.h | 2 -
fs/btrfs/extent-tree.c | 43 ++++++++++-----
fs/btrfs/file-item.c | 9 ++--
fs/btrfs/free-space-cache.c | 15 +++---
fs/btrfs/hash.c | 75 ++++++++++++++++++++------
fs/btrfs/hash.h | 22 ++++----
fs/btrfs/inode-item.c | 6 +--
fs/btrfs/inode.c | 16 +++---
fs/btrfs/props.c | 37 +++++++++++--
fs/btrfs/props.h | 3 +-
fs/btrfs/scrub.c | 70 +++++++++++++++++++-----
fs/btrfs/send.c | 7 ++-
fs/btrfs/super.c | 9 ++--
fs/btrfs/tree-log.c | 2 +-
19 files changed, 331 insertions(+), 161 deletions(-)
diff --git a/fs/btrfs/Kconfig b/fs/btrfs/Kconfig
index a66768e..ef45456 100644
--- a/fs/btrfs/Kconfig
+++ b/fs/btrfs/Kconfig
@@ -2,6 +2,7 @@ config BTRFS_FS
tristate "Btrfs filesystem support"
select CRYPTO
select CRYPTO_CRC32C
+ select CRYPTO_XXH32
select ZLIB_INFLATE
select ZLIB_DEFLATE
select LZO_COMPRESS
@@ -88,3 +89,24 @@ config BTRFS_ASSERT
any of the assertions trip. This is meant for btrfs developers only.
If unsure, say N.
+
+choice
+ prompt "choose checksum algorithm"
+ default BTRFS_CRC32C
+ help
+ This option allows to select a checksum algorithm
+
+config BTRFS_CRC32C
+ depends on CRYPTO_CRC32C
+ bool "BTRFS_CRC32C"
+ help
+ crc32c
+
+config BTRFS_XXH32
+ depends on CRYPTO_XXH32
+ bool "BTRFS_XXH32"
+ help
+ xxhash
+
+endchoice
+
diff --git a/fs/btrfs/compression.c b/fs/btrfs/compression.c
index d43c544..889b0f1 100644
--- a/fs/btrfs/compression.c
+++ b/fs/btrfs/compression.c
@@ -41,6 +41,7 @@
#include "compression.h"
#include "extent_io.h"
#include "extent_map.h"
+#include "hash.h"
struct compressed_bio {
/* number of bios pending for this compressed extent */
@@ -114,17 +115,16 @@ static int check_compressed_csum(struct inode *inode,
char *kaddr;
u32 csum;
u32 *cb_sum = &cb->sums;
+ struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
return 0;
for (i = 0; i < cb->nr_pages; i++) {
page = cb->compressed_pages[i];
- csum = ~(u32)0;
kaddr = kmap_atomic(page);
- csum = btrfs_csum_data(kaddr, csum, PAGE_CACHE_SIZE);
- btrfs_csum_final(csum, (char *)&csum);
+ btrfs_csum_data(fs_info, kaddr, PAGE_CACHE_SIZE, (char *)&csum);
kunmap_atomic(kaddr);
if (csum != *cb_sum) {
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index ba6b885..cbb6533 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -176,12 +176,16 @@ struct btrfs_ordered_sum;
/* 32 bytes in various csum fields */
#define BTRFS_CSUM_SIZE 32
-/* csum types */
+/*
+ * csum types,
+ * - 4 bytes for CRC32(crc32c)
+ * - 4 bytes for XXH32(xxhash)
+ */
#define BTRFS_CSUM_TYPE_CRC32 0
+#define BTRFS_CSUM_TYPE_XXH32 1
-static int btrfs_csum_sizes[] = { 4, 0 };
+static int btrfs_csum_sizes[] = { 4, 4, 0 };
-/* four bytes for CRC32 */
#define BTRFS_EMPTY_DIR_SIZE 0
/* spefic to btrfs_map_block(), therefore not in include/linux/blk_types.h */
@@ -1688,6 +1692,8 @@ struct btrfs_fs_info {
struct semaphore uuid_tree_rescan_sem;
unsigned int update_uuid_tree_gen:1;
+
+ struct crypto_shash *tfm;
};
struct btrfs_subvolume_writers {
diff --git a/fs/btrfs/dir-item.c b/fs/btrfs/dir-item.c
index a0691df..1332858 100644
--- a/fs/btrfs/dir-item.c
+++ b/fs/btrfs/dir-item.c
@@ -87,7 +87,7 @@ int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
key.objectid = objectid;
btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
- key.offset = btrfs_name_hash(name, name_len);
+ key.offset = btrfs_name_hash(root->fs_info, name, name_len);
data_size = sizeof(*dir_item) + name_len + data_len;
dir_item = insert_with_overflow(trans, root, path, &key, data_size,
@@ -138,7 +138,7 @@ int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, struct btrfs_root
key.objectid = btrfs_ino(dir);
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
- key.offset = btrfs_name_hash(name, name_len);
+ key.offset = btrfs_name_hash(root->fs_info, name, name_len);
path = btrfs_alloc_path();
if (!path)
@@ -206,7 +206,7 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
key.objectid = dir;
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
- key.offset = btrfs_name_hash(name, name_len);
+ key.offset = btrfs_name_hash(root->fs_info, name, name_len);
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
if (ret < 0)
@@ -235,7 +235,7 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
key.objectid = dir;
btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
- key.offset = btrfs_name_hash(name, name_len);
+ key.offset = btrfs_name_hash(root->fs_info, name, name_len);
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
@@ -368,7 +368,7 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
key.objectid = dir;
btrfs_set_key_type(&key, BTRFS_XATTR_ITEM_KEY);
- key.offset = btrfs_name_hash(name, name_len);
+ key.offset = btrfs_name_hash(root->fs_info, name, name_len);
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
if (ret < 0)
return ERR_PTR(ret);
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 9833149..e05535c 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -242,34 +242,24 @@ out:
return em;
}
-u32 btrfs_csum_data(char *data, u32 seed, size_t len)
+int btrfs_gen_csum_tree_block(struct btrfs_fs_info *info, struct extent_buffer *buf, char *result)
{
- return btrfs_crc32c(seed, data, len);
-}
-
-void btrfs_csum_final(u32 crc, char *result)
-{
- put_unaligned_le32(~crc, result);
-}
-
-/*
- * compute the csum for a btree block, and either verify it or write it
- * into the csum field of the block.
- */
-static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
- int verify)
-{
- u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
- char *result = NULL;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(info->tfm)];
+ } desc;
unsigned long len;
unsigned long cur_len;
unsigned long offset = BTRFS_CSUM_SIZE;
- char *kaddr;
unsigned long map_start;
unsigned long map_len;
+ char *kaddr;
int err;
- u32 crc = ~(u32)0;
- unsigned long inline_result;
+
+ desc.shash.tfm = info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
len = buf->len - offset;
while (len > 0) {
@@ -278,11 +268,27 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
if (err)
return 1;
cur_len = min(len, map_len - (offset - map_start));
- crc = btrfs_csum_data(kaddr + offset - map_start,
- crc, cur_len);
+ crypto_shash_update(&desc.shash,
+ kaddr + offset - map_start, cur_len);
len -= cur_len;
offset += cur_len;
}
+
+ return crypto_shash_final(&desc.shash, result);
+}
+
+/*
+ * compute the csum for a btree block, and either verify it or write it
+ * into the csum field of the block.
+ */
+static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
+ int verify)
+{
+ u16 csum_size = btrfs_super_csum_size(root->fs_info->super_copy);
+ char *result = NULL;
+ unsigned long inline_result;
+ int err;
+
if (csum_size > sizeof(inline_result)) {
result = kzalloc(csum_size * sizeof(char), GFP_NOFS);
if (!result)
@@ -291,7 +297,12 @@ static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
result = (char *)&inline_result;
}
- btrfs_csum_final(crc, result);
+ err = btrfs_gen_csum_tree_block(root->fs_info, buf, result);
+ if (err) {
+ if (result != (char *)&inline_result)
+ kfree(result);
+ return 1;
+ }
if (verify) {
if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
@@ -376,43 +387,40 @@ out:
* Return 0 if the superblock checksum type matches the checksum value of that
* algorithm. Pass the raw disk superblock data.
*/
-static int btrfs_check_super_csum(char *raw_disk_sb)
+static int btrfs_check_super_csum(struct btrfs_fs_info *info, char *raw_disk_sb)
{
struct btrfs_super_block *disk_sb =
(struct btrfs_super_block *)raw_disk_sb;
u16 csum_type = btrfs_super_csum_type(disk_sb);
int ret = 0;
-
- if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
- u32 crc = ~(u32)0;
- const int csum_size = sizeof(crc);
- char result[csum_size];
-
- /*
- * The super_block structure does not span the whole
- * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
- * is filled with zeros and is included in the checkum.
- */
- crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
- crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
- btrfs_csum_final(crc, result);
-
- if (memcmp(raw_disk_sb, result, csum_size))
- ret = 1;
-
- if (ret && btrfs_super_generation(disk_sb) < 10) {
- printk(KERN_WARNING
- "BTRFS: super block crcs don't match, older mkfs detected\n");
- ret = 0;
- }
- }
+ const int csum_size = btrfs_super_csum_size(disk_sb);
+ char result[csum_size];
if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
printk(KERN_ERR "BTRFS: unsupported checksum algorithm %u\n",
csum_type);
ret = 1;
+ goto out;
}
+ /*
+ * The super_block structure does not span the whole
+ * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
+ * is filled with zeros and is included in the checkum.
+ */
+ btrfs_csum_data(info, raw_disk_sb + BTRFS_CSUM_SIZE,
+ BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
+
+ if (memcmp(raw_disk_sb, result, csum_size))
+ ret = 1;
+
+ if (ret && btrfs_super_generation(disk_sb) < 10) {
+ printk(KERN_WARNING
+ "BTRFS: super block crcs don't match, older mkfs detected\n");
+ ret = 0;
+ }
+
+out:
return ret;
}
@@ -2389,11 +2397,17 @@ int open_ctree(struct super_block *sb,
goto fail_alloc;
}
+ if (btrfs_hash_init(fs_info)) {
+ err = -EINVAL;
+ btrfs_err(fs_info, "BTRFS: hash init error");
+ goto fail_alloc;
+ }
+
/*
* We want to check superblock checksum, the type is stored inside.
* Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
*/
- if (btrfs_check_super_csum(bh->b_data)) {
+ if (btrfs_check_super_csum(fs_info, bh->b_data)) {
printk(KERN_ERR "BTRFS: superblock checksum mismatch\n");
err = -EINVAL;
goto fail_alloc;
@@ -2990,6 +3004,7 @@ fail_tree_roots:
fail_sb_buffer:
btrfs_stop_all_workers(fs_info);
fail_alloc:
+ btrfs_hash_exit(fs_info);
fail_iput:
btrfs_mapping_tree_free(&fs_info->mapping_tree);
@@ -3110,7 +3125,6 @@ static int write_dev_supers(struct btrfs_device *device,
int i;
int ret;
int errors = 0;
- u32 crc;
u64 bytenr;
if (max_mirrors == 0)
@@ -3141,12 +3155,8 @@ static int write_dev_supers(struct btrfs_device *device,
} else {
btrfs_set_super_bytenr(sb, bytenr);
- crc = ~(u32)0;
- crc = btrfs_csum_data((char *)sb +
- BTRFS_CSUM_SIZE, crc,
- BTRFS_SUPER_INFO_SIZE -
- BTRFS_CSUM_SIZE);
- btrfs_csum_final(crc, sb->csum);
+ btrfs_csum_data(device->dev_root->fs_info, (char *)sb + BTRFS_CSUM_SIZE,
+ BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, sb->csum);
/*
* one reference for us, and we leave it for the
@@ -3658,6 +3668,8 @@ int close_ctree(struct btrfs_root *root)
btrfs_free_block_rsv(root, root->orphan_block_rsv);
root->orphan_block_rsv = NULL;
+ btrfs_hash_exit(fs_info);
+
return 0;
}
diff --git a/fs/btrfs/disk-io.h b/fs/btrfs/disk-io.h
index 53059df..d98451e 100644
--- a/fs/btrfs/disk-io.h
+++ b/fs/btrfs/disk-io.h
@@ -115,8 +115,6 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
int atomic);
int btrfs_set_buffer_uptodate(struct extent_buffer *buf);
int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid);
-u32 btrfs_csum_data(char *data, u32 seed, size_t len);
-void btrfs_csum_final(u32 crc, char *result);
int btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
int metadata);
int btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct inode *inode,
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 5590af9..de43238 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -36,6 +36,7 @@
#include "free-space-cache.h"
#include "math.h"
#include "sysfs.h"
+#include "hash.h"
#undef SCRAMBLE_DELAYED_REFS
@@ -1067,26 +1068,44 @@ static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
}
#endif
-static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
+static u64 hash_extent_data_ref(struct btrfs_fs_info *info, u64 root_objectid, u64 owner, u64 offset)
{
- u32 high_crc = ~(u32)0;
- u32 low_crc = ~(u32)0;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(info->tfm)];
+ } desc;
+ int err;
+ u32 high_crc;
+ u32 low_crc;
__le64 lenum;
+ desc.shash.tfm = info->tfm;
+ desc.shash.flags = 0;
+
+ /* high part */
lenum = cpu_to_le64(root_objectid);
- high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
+ crypto_shash_digest(&desc.shash, (u8 *)&lenum, sizeof(lenum), (char *)&high_crc);
+ high_crc = le32_to_cpu(high_crc);
+
+ /* low part */
+ crypto_shash_init(&desc.shash);
+
lenum = cpu_to_le64(owner);
- low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
+ crypto_shash_update(&desc.shash, (u8 *)&lenum, sizeof(lenum));
+
lenum = cpu_to_le64(offset);
- low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
+ crypto_shash_update(&desc.shash, (u8 *)&lenum, sizeof(lenum));
+
+ err = crypto_shash_final(&desc.shash, (char *)&low_crc);
+ low_crc = le32_to_cpu(low_crc);
return ((u64)high_crc << 31) ^ (u64)low_crc;
}
-static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
+static u64 hash_extent_data_ref_item(struct btrfs_fs_info *info, struct extent_buffer *leaf,
struct btrfs_extent_data_ref *ref)
{
- return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
+ return hash_extent_data_ref(info, btrfs_extent_data_ref_root(leaf, ref),
btrfs_extent_data_ref_objectid(leaf, ref),
btrfs_extent_data_ref_offset(leaf, ref));
}
@@ -1123,7 +1142,7 @@ static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
key.offset = parent;
} else {
key.type = BTRFS_EXTENT_DATA_REF_KEY;
- key.offset = hash_extent_data_ref(root_objectid,
+ key.offset = hash_extent_data_ref(root->fs_info, root_objectid,
owner, offset);
}
again:
@@ -1209,7 +1228,7 @@ static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
size = sizeof(struct btrfs_shared_data_ref);
} else {
key.type = BTRFS_EXTENT_DATA_REF_KEY;
- key.offset = hash_extent_data_ref(root_objectid,
+ key.offset = hash_extent_data_ref(root->fs_info, root_objectid,
owner, offset);
size = sizeof(struct btrfs_extent_data_ref);
}
@@ -1612,8 +1631,8 @@ again:
err = 0;
break;
}
- if (hash_extent_data_ref_item(leaf, dref) <
- hash_extent_data_ref(root_objectid, owner, offset))
+ if (hash_extent_data_ref_item(root->fs_info, leaf, dref) <
+ hash_extent_data_ref(root->fs_info, root_objectid, owner, offset))
break;
} else {
u64 ref_offset;
diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c
index 127555b..4e3ec0f 100644
--- a/fs/btrfs/file-item.c
+++ b/fs/btrfs/file-item.c
@@ -25,6 +25,7 @@
#include "transaction.h"
#include "volumes.h"
#include "print-tree.h"
+#include "hash.h"
#define __MAX_CSUM_ITEMS(r, size) ((unsigned long)(((BTRFS_LEAF_DATA_SIZE(r) - \
sizeof(struct btrfs_item) * 2) / \
@@ -491,13 +492,9 @@ int btrfs_csum_one_bio(struct btrfs_root *root, struct inode *inode,
}
data = kmap_atomic(bvec->bv_page);
- sums->sums[index] = ~(u32)0;
- sums->sums[index] = btrfs_csum_data(data + bvec->bv_offset,
- sums->sums[index],
- bvec->bv_len);
+ btrfs_csum_data(root->fs_info, data + bvec->bv_offset,
+ bvec->bv_len, (char *)(sums->sums + index));
kunmap_atomic(data);
- btrfs_csum_final(sums->sums[index],
- (char *)(sums->sums + index));
bio_index++;
index++;
diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c
index 73f3de7..0c16d5b 100644
--- a/fs/btrfs/free-space-cache.c
+++ b/fs/btrfs/free-space-cache.c
@@ -27,6 +27,7 @@
#include "disk-io.h"
#include "extent_io.h"
#include "inode-map.h"
+#include "hash.h"
#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
@@ -418,7 +419,7 @@ static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
{
u32 *tmp;
- u32 crc = ~(u32)0;
+ u32 crc;
unsigned offset = 0;
if (!io_ctl->check_crcs) {
@@ -429,9 +430,8 @@ static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
if (index == 0)
offset = sizeof(u32) * io_ctl->num_pages;
- crc = btrfs_csum_data(io_ctl->orig + offset, crc,
- PAGE_CACHE_SIZE - offset);
- btrfs_csum_final(crc, (char *)&crc);
+ btrfs_csum_data(io_ctl->root->fs_info, io_ctl->orig + offset,
+ PAGE_CACHE_SIZE - offset, (char *)&crc);
io_ctl_unmap_page(io_ctl);
tmp = kmap(io_ctl->pages[0]);
tmp += index;
@@ -442,7 +442,7 @@ static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
{
u32 *tmp, val;
- u32 crc = ~(u32)0;
+ u32 crc;
unsigned offset = 0;
if (!io_ctl->check_crcs) {
@@ -459,9 +459,8 @@ static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
kunmap(io_ctl->pages[0]);
io_ctl_map_page(io_ctl, 0);
- crc = btrfs_csum_data(io_ctl->orig + offset, crc,
- PAGE_CACHE_SIZE - offset);
- btrfs_csum_final(crc, (char *)&crc);
+ btrfs_csum_data(io_ctl->root->fs_info, io_ctl->orig + offset,
+ PAGE_CACHE_SIZE - offset, (char *)&crc);
if (val != crc) {
printk_ratelimited(KERN_ERR "BTRFS: csum mismatch on free "
"space cache\n");
diff --git a/fs/btrfs/hash.c b/fs/btrfs/hash.c
index 85889aa..f93e07d 100644
--- a/fs/btrfs/hash.c
+++ b/fs/btrfs/hash.c
@@ -11,40 +11,85 @@
* General Public License for more details.
*/
-#include <crypto/hash.h>
-#include <linux/err.h>
#include "hash.h"
-static struct crypto_shash *tfm;
-
-int __init btrfs_hash_init(void)
+int btrfs_hash_init(struct btrfs_fs_info *info)
{
+ struct crypto_shash *tfm = NULL;
+ int ret = -EINVAL;
+
+#if defined(CONFIG_BTRFS_CRC32C)
tfm = crypto_alloc_shash("crc32c", 0, 0);
if (IS_ERR(tfm))
return PTR_ERR(tfm);
+ ret = 0;
+#elif defined(CONFIG_BTRFS_XXH32)
+ tfm = crypto_alloc_shash("xxh32", 0, 0);
+ if (IS_ERR(tfm))
+ return PTR_ERR(tfm);
+ ret = 0;
+#endif
- return 0;
+ info->tfm = tfm;
+ return ret;
}
-void btrfs_hash_exit(void)
+void btrfs_hash_exit(struct btrfs_fs_info *info)
{
- crypto_free_shash(tfm);
+ crypto_free_shash(info->tfm);
}
-u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length)
+int btrfs_csum_data(struct btrfs_fs_info *info, const void *address, unsigned int length, u8 *out)
{
struct {
struct shash_desc shash;
- char ctx[crypto_shash_descsize(tfm)];
+ char ctx[crypto_shash_descsize(info->tfm)];
} desc;
int err;
- desc.shash.tfm = tfm;
+ desc.shash.tfm = info->tfm;
desc.shash.flags = 0;
- *(u32 *)desc.ctx = crc;
- err = crypto_shash_update(&desc.shash, address, length);
- BUG_ON(err);
+ err = crypto_shash_digest(&desc.shash, address, length, out);
+ ASSERT(!err);
+
+ return err;
+}
+
+u64 btrfs_name_hash(struct btrfs_fs_info *info, const char *name, int len)
+{
+ u32 hash;
+
+ btrfs_csum_data(info, name, len, (char *)&hash);
- return *(u32 *)desc.ctx;
+ return le32_to_cpu(hash);
}
+
+/*
+ * Figure the key offset of an extended inode ref
+ */
+u64 btrfs_extref_hash(struct btrfs_fs_info *info, u64 parent_objectid, const char *name, int len)
+{
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(info->tfm)];
+ } desc;
+ int err;
+ u32 hash;
+
+ desc.shash.tfm = info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
+
+ crypto_shash_update(&desc.shash, (char *)&parent_objectid, sizeof(u64));
+
+ crypto_shash_update(&desc.shash, name, len);
+
+ err = crypto_shash_final(&desc.shash, (char *)&hash);
+ ASSERT(!err);
+
+ return le32_to_cpu(hash);
+}
+
+
diff --git a/fs/btrfs/hash.h b/fs/btrfs/hash.h
index 118a231..2a16a4f 100644
--- a/fs/btrfs/hash.h
+++ b/fs/btrfs/hash.h
@@ -19,24 +19,22 @@
#ifndef __HASH__
#define __HASH__
-int __init btrfs_hash_init(void);
+#include <crypto/hash.h>
+#include <linux/err.h>
+#include <crypto/xxhash.h>
+#include "ctree.h"
-void btrfs_hash_exit(void);
+int btrfs_hash_init(struct btrfs_fs_info *info);
-u32 btrfs_crc32c(u32 crc, const void *address, unsigned int length);
+void btrfs_hash_exit(struct btrfs_fs_info *info);
-static inline u64 btrfs_name_hash(const char *name, int len)
-{
- return btrfs_crc32c((u32)~1, name, len);
-}
+int btrfs_csum_data(struct btrfs_fs_info *info, const void *address, unsigned int length, u8 *out);
+
+u64 btrfs_name_hash(struct btrfs_fs_info *info, const char *name, int len);
/*
* Figure the key offset of an extended inode ref
*/
-static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
- int len)
-{
- return (u64) btrfs_crc32c(parent_objectid, name, len);
-}
+u64 btrfs_extref_hash(struct btrfs_fs_info *info, u64 parent_objectid, const char *name, int len);
#endif
diff --git a/fs/btrfs/inode-item.c b/fs/btrfs/inode-item.c
index 2be38df..efca894 100644
--- a/fs/btrfs/inode-item.c
+++ b/fs/btrfs/inode-item.c
@@ -106,7 +106,7 @@ btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
key.objectid = inode_objectid;
key.type = BTRFS_INODE_EXTREF_KEY;
- key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
+ key.offset = btrfs_extref_hash(root->fs_info, ref_objectid, name, name_len);
ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
if (ret < 0)
@@ -136,7 +136,7 @@ static int btrfs_del_inode_extref(struct btrfs_trans_handle *trans,
key.objectid = inode_objectid;
btrfs_set_key_type(&key, BTRFS_INODE_EXTREF_KEY);
- key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
+ key.offset = btrfs_extref_hash(root->fs_info, ref_objectid, name, name_len);
path = btrfs_alloc_path();
if (!path)
@@ -283,7 +283,7 @@ static int btrfs_insert_inode_extref(struct btrfs_trans_handle *trans,
key.objectid = inode_objectid;
key.type = BTRFS_INODE_EXTREF_KEY;
- key.offset = btrfs_extref_hash(ref_objectid, name, name_len);
+ key.offset = btrfs_extref_hash(root->fs_info, ref_objectid, name, name_len);
path = btrfs_alloc_path();
if (!path)
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 5f805bc..10dde99 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2825,7 +2825,7 @@ static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
char *kaddr;
struct btrfs_root *root = BTRFS_I(inode)->root;
u32 csum_expected;
- u32 csum = ~(u32)0;
+ u32 csum;
static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL,
DEFAULT_RATELIMIT_BURST);
@@ -2848,8 +2848,7 @@ static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
csum_expected = *(((u32 *)io_bio->csum) + phy_offset);
kaddr = kmap_atomic(page);
- csum = btrfs_csum_data(kaddr + offset, csum, end - start + 1);
- btrfs_csum_final(csum, (char *)&csum);
+ btrfs_csum_data(root->fs_info, kaddr + offset, end - start + 1, (char *)&csum);
if (csum != csum_expected)
goto zeroit;
@@ -3307,9 +3306,9 @@ static noinline int acls_after_inode_item(struct extent_buffer *leaf,
int scanned = 0;
if (!xattr_access) {
- xattr_access = btrfs_name_hash(POSIX_ACL_XATTR_ACCESS,
+ xattr_access = btrfs_name_hash(leaf->fs_info, POSIX_ACL_XATTR_ACCESS,
strlen(POSIX_ACL_XATTR_ACCESS));
- xattr_default = btrfs_name_hash(POSIX_ACL_XATTR_DEFAULT,
+ xattr_default = btrfs_name_hash(leaf->fs_info, POSIX_ACL_XATTR_DEFAULT,
strlen(POSIX_ACL_XATTR_DEFAULT));
}
@@ -7017,14 +7016,13 @@ static void btrfs_endio_direct_read(struct bio *bio, int err)
if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
struct page *page = bvec->bv_page;
char *kaddr;
- u32 csum = ~(u32)0;
+ u32 csum;
unsigned long flags;
local_irq_save(flags);
kaddr = kmap_atomic(page);
- csum = btrfs_csum_data(kaddr + bvec->bv_offset,
- csum, bvec->bv_len);
- btrfs_csum_final(csum, (char *)&csum);
+ btrfs_csum_data(root->fs_info, kaddr + bvec->bv_offset,
+ bvec->bv_len, (char *)&csum);
kunmap_atomic(kaddr);
local_irq_restore(flags);
diff --git a/fs/btrfs/props.c b/fs/btrfs/props.c
index 129b1dd..0f70daa 100644
--- a/fs/btrfs/props.c
+++ b/fs/btrfs/props.c
@@ -23,6 +23,8 @@
#include "transaction.h"
#include "xattr.h"
+static struct crypto_shash *tfm;
+
#define BTRFS_PROP_HANDLERS_HT_BITS 8
static DEFINE_HASHTABLE(prop_handlers_ht, BTRFS_PROP_HANDLERS_HT_BITS);
@@ -54,17 +56,46 @@ static struct prop_handler prop_handlers[] = {
}
};
-void __init btrfs_props_init(void)
+static u64 btrfs_prop_name_hash(const char *name, unsigned long len)
+{
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(tfm)];
+ } desc;
+ int err;
+ u32 h;
+
+ desc.shash.tfm = tfm;
+ desc.shash.flags = 0;
+
+ err = crypto_shash_digest(&desc.shash, name, len, (char *)&h);
+ ASSERT(!err);
+
+ return le32_to_cpu(h);
+}
+
+int __init btrfs_props_init(void)
{
struct prop_handler *p;
+ tfm = crypto_alloc_shash("xxhash", 0, 0);
+ if (IS_ERR(tfm))
+ return PTR_ERR(tfm);
+
hash_init(prop_handlers_ht);
for (p = &prop_handlers[0]; p->xattr_name; p++) {
- u64 h = btrfs_name_hash(p->xattr_name, strlen(p->xattr_name));
+ u64 h = btrfs_prop_name_hash(p->xattr_name, strlen(p->xattr_name));
hash_add(prop_handlers_ht, &p->node, h);
}
+
+ return 0;
+}
+
+void btrfs_props_exit(void)
+{
+ crypto_free_shash(tfm);
}
static const struct hlist_head *find_prop_handlers_by_hash(const u64 hash)
@@ -85,7 +116,7 @@ find_prop_handler(const char *name,
struct prop_handler *h;
if (!handlers) {
- u64 hash = btrfs_name_hash(name, strlen(name));
+ u64 hash = btrfs_prop_name_hash(name, strlen(name));
handlers = find_prop_handlers_by_hash(hash);
if (!handlers)
diff --git a/fs/btrfs/props.h b/fs/btrfs/props.h
index 100f188..7cce909 100644
--- a/fs/btrfs/props.h
+++ b/fs/btrfs/props.h
@@ -21,7 +21,8 @@
#include "ctree.h"
-void __init btrfs_props_init(void);
+int __init btrfs_props_init(void);
+void btrfs_props_exit(void);
int btrfs_set_prop(struct inode *inode,
const char *name,
diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
index 0be7799..927fa1e 100644
--- a/fs/btrfs/scrub.c
+++ b/fs/btrfs/scrub.c
@@ -29,6 +29,7 @@
#include "check-integrity.h"
#include "rcu-string.h"
#include "raid56.h"
+#include "hash.h"
/*
* This is only the first step towards a full-features scrub. It reads all
@@ -1368,8 +1369,11 @@ static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
{
int page_num;
u8 calculated_csum[BTRFS_CSUM_SIZE];
- u32 crc = ~(u32)0;
void *mapped_buffer;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(fs_info->tfm)];
+ } desc;
WARN_ON(!sblock->pagev[0]->page);
if (is_metadata) {
@@ -1395,13 +1399,19 @@ static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
mapped_buffer = kmap_atomic(sblock->pagev[0]->page);
}
+ desc.shash.tfm = fs_info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
+
for (page_num = 0;;) {
if (page_num == 0 && is_metadata)
- crc = btrfs_csum_data(
+ crypto_shash_update(&desc.shash,
((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE,
- crc, PAGE_SIZE - BTRFS_CSUM_SIZE);
+ PAGE_SIZE - BTRFS_CSUM_SIZE);
else
- crc = btrfs_csum_data(mapped_buffer, crc, PAGE_SIZE);
+ crypto_shash_update(&desc.shash, mapped_buffer,
+ PAGE_SIZE);
kunmap_atomic(mapped_buffer);
page_num++;
@@ -1412,7 +1422,8 @@ static void scrub_recheck_block_checksum(struct btrfs_fs_info *fs_info,
mapped_buffer = kmap_atomic(sblock->pagev[page_num]->page);
}
- btrfs_csum_final(crc, calculated_csum);
+ crypto_shash_final(&desc.shash, calculated_csum);
+
if (memcmp(calculated_csum, csum, csum_size))
sblock->checksum_error = 1;
}
@@ -1676,10 +1687,14 @@ static int scrub_checksum_data(struct scrub_block *sblock)
u8 *on_disk_csum;
struct page *page;
void *buffer;
- u32 crc = ~(u32)0;
int fail = 0;
u64 len;
int index;
+ struct btrfs_fs_info *fs_info = sctx->dev_root->fs_info;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(fs_info->tfm)];
+ } desc;
BUG_ON(sblock->page_count < 1);
if (!sblock->pagev[0]->have_csum)
@@ -1691,10 +1706,16 @@ static int scrub_checksum_data(struct scrub_block *sblock)
len = sctx->sectorsize;
index = 0;
+
+ desc.shash.tfm = fs_info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
+
for (;;) {
u64 l = min_t(u64, len, PAGE_SIZE);
- crc = btrfs_csum_data(buffer, crc, l);
+ crypto_shash_update(&desc.shash, buffer, l);
kunmap_atomic(buffer);
len -= l;
if (len == 0)
@@ -1706,7 +1727,8 @@ static int scrub_checksum_data(struct scrub_block *sblock)
buffer = kmap_atomic(page);
}
- btrfs_csum_final(crc, csum);
+ crypto_shash_final(&desc.shash, csum);
+
if (memcmp(csum, on_disk_csum, sctx->csum_size))
fail = 1;
@@ -1725,11 +1747,14 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock)
void *mapped_buffer;
u64 mapped_size;
void *p;
- u32 crc = ~(u32)0;
int fail = 0;
int crc_fail = 0;
u64 len;
int index;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(fs_info->tfm)];
+ } desc;
BUG_ON(sblock->page_count < 1);
page = sblock->pagev[0]->page;
@@ -1761,10 +1786,16 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock)
mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
index = 0;
+
+ desc.shash.tfm = fs_info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
+
for (;;) {
u64 l = min_t(u64, len, mapped_size);
- crc = btrfs_csum_data(p, crc, l);
+ crypto_shash_update(&desc.shash, p, l);
kunmap_atomic(mapped_buffer);
len -= l;
if (len == 0)
@@ -1778,7 +1809,8 @@ static int scrub_checksum_tree_block(struct scrub_block *sblock)
p = mapped_buffer;
}
- btrfs_csum_final(crc, calculated_csum);
+ crypto_shash_final(&desc.shash, calculated_csum);
+
if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
++crc_fail;
@@ -1797,11 +1829,14 @@ static int scrub_checksum_super(struct scrub_block *sblock)
void *mapped_buffer;
u64 mapped_size;
void *p;
- u32 crc = ~(u32)0;
int fail_gen = 0;
int fail_cor = 0;
u64 len;
int index;
+ struct {
+ struct shash_desc shash;
+ char ctx[crypto_shash_descsize(fs_info->tfm)];
+ } desc;
BUG_ON(sblock->page_count < 1);
page = sblock->pagev[0]->page;
@@ -1822,10 +1857,16 @@ static int scrub_checksum_super(struct scrub_block *sblock)
mapped_size = PAGE_SIZE - BTRFS_CSUM_SIZE;
p = ((u8 *)mapped_buffer) + BTRFS_CSUM_SIZE;
index = 0;
+
+ desc.shash.tfm = fs_info->tfm;
+ desc.shash.flags = 0;
+
+ crypto_shash_init(&desc.shash);
+
for (;;) {
u64 l = min_t(u64, len, mapped_size);
- crc = btrfs_csum_data(p, crc, l);
+ crypto_shash_update(&desc.shash, p, l);
kunmap_atomic(mapped_buffer);
len -= l;
if (len == 0)
@@ -1839,7 +1880,8 @@ static int scrub_checksum_super(struct scrub_block *sblock)
p = mapped_buffer;
}
- btrfs_csum_final(crc, calculated_csum);
+ crypto_shash_final(&desc.shash, calculated_csum);
+
if (memcmp(calculated_csum, on_disk_csum, sctx->csum_size))
++fail_cor;
diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index eb6537a..0bc2f71 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -664,13 +664,16 @@ static int send_cmd(struct send_ctx *sctx)
int ret;
struct btrfs_cmd_header *hdr;
u32 crc;
+ char *result;
hdr = (struct btrfs_cmd_header *)sctx->send_buf;
hdr->len = cpu_to_le32(sctx->send_size - sizeof(*hdr));
hdr->crc = 0;
- crc = btrfs_crc32c(0, (unsigned char *)sctx->send_buf, sctx->send_size);
- hdr->crc = cpu_to_le32(crc);
+ result = (char *)&crc;
+ btrfs_csum_data(sctx->send_root->fs_info, (unsigned char *)sctx->send_buf, sctx->send_size, result);
+ /* crc is already convert to __le32. */
+ hdr->crc = crc;
ret = write_buf(sctx->send_filp, sctx->send_buf, sctx->send_size,
&sctx->send_off);
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 9601d25..e89338f 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -880,6 +880,7 @@ static struct dentry *get_default_root(struct super_block *sb,
* it's always been there, but don't freak out, just try and
* mount to root most subvolume.
*/
+ btrfs_info(fs_info, "default NOT FOUND, go for fsroot\n");
btrfs_free_path(path);
dir_id = BTRFS_FIRST_FREE_OBJECTID;
new_root = fs_info->fs_root;
@@ -1903,12 +1904,10 @@ static int __init init_btrfs_fs(void)
{
int err;
- err = btrfs_hash_init();
+ err = btrfs_props_init();
if (err)
return err;
- btrfs_props_init();
-
err = btrfs_init_sysfs();
if (err)
goto free_hash;
@@ -1987,7 +1986,7 @@ free_compress:
btrfs_exit_compress();
btrfs_exit_sysfs();
free_hash:
- btrfs_hash_exit();
+ btrfs_props_exit();
return err;
}
@@ -2006,7 +2005,7 @@ static void __exit exit_btrfs_fs(void)
btrfs_exit_sysfs();
btrfs_cleanup_fs_uuids();
btrfs_exit_compress();
- btrfs_hash_exit();
+ btrfs_props_exit();
}
late_initcall(init_btrfs_fs);
diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c
index e2f45fc..42c68fb 100644
--- a/fs/btrfs/tree-log.c
+++ b/fs/btrfs/tree-log.c
@@ -1014,7 +1014,7 @@ again:
search_key.objectid = inode_objectid;
search_key.type = BTRFS_INODE_EXTREF_KEY;
- search_key.offset = btrfs_extref_hash(parent_objectid,
+ search_key.offset = btrfs_extref_hash(root->fs_info, parent_objectid,
victim_name,
victim_name_len);
ret = 0;
--
1.8.1.4
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH] Btrfs-progs: add xxhash
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
` (2 preceding siblings ...)
2014-05-07 10:56 ` [PATCH 3/3] Btrfs: add another checksum algorithm xxhash Liu Bo
@ 2014-05-07 10:56 ` Liu Bo
2014-05-15 16:57 ` David Sterba
2014-05-07 11:08 ` [RFC PATCH 0/3] Btrfs: add xxhash algorithm Tomasz Torcz
4 siblings, 1 reply; 9+ messages in thread
From: Liu Bo @ 2014-05-07 10:56 UTC (permalink / raw)
To: linux-btrfs
From: root <root@localhost.localdomain>
Signed-off-by: Liu Bo <bo.li.liu@oracle.com>
---
Makefile | 4 +-
crc32c.h | 4 +-
disk-io.c | 2 +-
hash.h | 2 +-
xxhash.c | 448 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xxhash.h | 171 +++++++++++++++++++++++
6 files changed, 626 insertions(+), 5 deletions(-)
create mode 100644 xxhash.c
create mode 100644 xxhash.h
diff --git a/Makefile b/Makefile
index 369df6c..1d70bc9 100644
--- a/Makefile
+++ b/Makefile
@@ -16,10 +16,10 @@ cmds_objects = cmds-subvolume.o cmds-filesystem.o cmds-device.o cmds-scrub.o \
cmds-restore.o cmds-rescue.o chunk-recover.o super-recover.o \
cmds-property.o cmds-dedup.o
libbtrfs_objects = send-stream.o send-utils.o rbtree.o btrfs-list.o crc32c.o \
- uuid-tree.o
+ uuid-tree.o xxhash.o
libbtrfs_headers = send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
- extent_io.h ioctl.h ctree.h btrfsck.h
+ extent_io.h ioctl.h ctree.h btrfsck.h xxhash.h
TESTS = fsck-tests.sh
INSTALL = install
diff --git a/crc32c.h b/crc32c.h
index c552ef6..6dd0ce2 100644
--- a/crc32c.h
+++ b/crc32c.h
@@ -25,9 +25,11 @@
#include <btrfs/kerncompat.h>
#endif /* BTRFS_FLAT_INCLUDES */
+#include "xxhash.h"
+
u32 crc32c_le(u32 seed, unsigned char const *data, size_t length);
void crc32c_optimization_init(void);
-#define crc32c(seed, data, length) crc32c_le(seed, (unsigned char const *)data, length)
+#define crc32c(seed, data, length) XXH32(data, length, 0)
#define btrfs_crc32c crc32c
#endif
diff --git a/disk-io.c b/disk-io.c
index 19b95a7..2c72f7f 100644
--- a/disk-io.c
+++ b/disk-io.c
@@ -67,7 +67,7 @@ u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
void btrfs_csum_final(u32 crc, char *result)
{
- *(__le32 *)result = ~cpu_to_le32(crc);
+ *(__le32 *)result = cpu_to_le32(crc);
}
static int __csum_tree_block_size(struct extent_buffer *buf, u16 csum_size,
diff --git a/hash.h b/hash.h
index c0b88a1..2d1a71d 100644
--- a/hash.h
+++ b/hash.h
@@ -22,6 +22,6 @@
static inline u64 btrfs_name_hash(const char *name, int len)
{
- return ~(crc32c((u32)(~0), name, len));
+ return crc32c((u32)(~0), name, len);
}
#endif
diff --git a/xxhash.c b/xxhash.c
new file mode 100644
index 0000000..f855a58
--- /dev/null
+++ b/xxhash.c
@@ -0,0 +1,448 @@
+/*
+xxHash - Fast Hash algorithm
+Copyright (C) 2012-2014, Yann Collet.
+BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+* Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+* Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+You can contact the author at :
+- xxHash source repository : http://code.google.com/p/xxhash/
+*/
+
+
+//**************************************
+// Tuning parameters
+//**************************************
+// Unaligned memory access is automatically enabled for "common" CPU, such as x86.
+// For others CPU, the compiler will be more cautious, and insert extra code to ensure aligned access is respected.
+// If you know your target CPU supports unaligned memory access, you want to force this option manually to improve performance.
+// You can also enable this parameter if you know your input data will always be aligned (boundaries of 4, for U32).
+#if defined(__ARM_FEATURE_UNALIGNED) || defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
+# define XXH_USE_UNALIGNED_ACCESS 1
+#endif
+
+// XXH_ACCEPT_NULL_INPUT_POINTER :
+// If the input pointer is a null pointer, xxHash default behavior is to trigger a memory access error, since it is a bad pointer.
+// When this option is enabled, xxHash output for null input pointers will be the same as a null-length input.
+// This option has a very small performance cost (only measurable on small inputs).
+// By default, this option is disabled. To enable it, uncomment below define :
+//#define XXH_ACCEPT_NULL_INPUT_POINTER 1
+
+// XXH_FORCE_NATIVE_FORMAT :
+// By default, xxHash library provides endian-independant Hash values, based on little-endian convention.
+// Results are therefore identical for little-endian and big-endian CPU.
+// This comes at a performance cost for big-endian CPU, since some swapping is required to emulate little-endian format.
+// Should endian-independance be of no importance for your application, you may set the #define below to 1.
+// It will improve speed for Big-endian CPU.
+// This option has no impact on Little_Endian CPU.
+#define XXH_FORCE_NATIVE_FORMAT 0
+
+
+//**************************************
+// Compiler Specific Options
+//**************************************
+#define FORCE_INLINE static inline __attribute__((always_inline))
+
+
+//**************************************
+// Includes & Memory related functions
+
+#include "xxhash.h"
+#include "kerncompat.h"
+#include <inttypes.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+
+
+
+//**************************************
+// Modify the local functions below should you wish to use some other memory related routines
+
+FORCE_INLINE void* XXH_malloc(size_t s)
+{
+ return kmalloc(s, GFP_KERNEL);
+}
+
+FORCE_INLINE void XXH_free(void* p)
+{
+ kfree(p);
+}
+
+FORCE_INLINE void* XXH_memcpy(void* dest, const void* src, size_t size)
+{
+ return memcpy(dest, src, size);
+}
+
+
+//**************************************
+// Basic Types
+//**************************************
+typedef unsigned char BYTE;
+typedef unsigned short U16;
+typedef unsigned int U32;
+typedef signed int S32;
+typedef unsigned long long U64;
+
+typedef struct _U32_S {
+ U32 v;
+} __attribute__ ((__packed__)) U32_S;
+
+#define A32(x) (((U32_S *)(x))->v)
+
+
+//***************************************
+// Compiler-specific Functions and Macros
+//***************************************
+
+// Note : although _rotl exists for minGW (GCC under windows), performance seems poor
+#define XXH_rotl32(x,r) ((x << r) | (x >> (32 - r)))
+
+#define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
+
+#if GCC_VERSION >= 403
+ #define XXH_swap32 __builtin_bswap32
+#else
+ static inline U32 XXH_swap32 (U32 x)
+ {
+ return ((x << 24) & 0xff000000 ) |
+ ((x << 8) & 0x00ff0000 ) |
+ ((x >> 8) & 0x0000ff00 ) |
+ ((x >> 24) & 0x000000ff );
+ }
+#endif
+
+
+//**************************************
+// Constants
+//**************************************
+#define PRIME32_1 2654435761U
+#define PRIME32_2 2246822519U
+#define PRIME32_3 3266489917U
+#define PRIME32_4 668265263U
+#define PRIME32_5 374761393U
+
+
+//**************************************
+// Architecture Macros
+//**************************************
+typedef enum {
+ XXH_bigEndian=0,
+ XXH_littleEndian=1
+} XXH_endianess;
+
+#ifndef XXH_CPU_LITTLE_ENDIAN // It is possible to define XXH_CPU_LITTLE_ENDIAN externally, for example using a compiler switch
+ static const int one = 1;
+# define XXH_CPU_LITTLE_ENDIAN (*(char*)(&one))
+#endif
+
+
+//**************************************
+// Macros
+//**************************************
+#define XXH_STATIC_ASSERT(c) { enum { XXH_static_assert = 1/(!!(c)) }; } // use only *after* variable declarations
+
+
+//****************************
+// Memory reads
+//****************************
+typedef enum {
+ XXH_aligned,
+ XXH_unaligned
+} XXH_alignment;
+
+FORCE_INLINE U32 XXH_readLE32_align(const U32* ptr, XXH_endianess endian, XXH_alignment align)
+{
+ if (align == XXH_unaligned)
+ return endian == XXH_littleEndian ? A32(ptr) : XXH_swap32(A32(ptr));
+ else
+ return endian == XXH_littleEndian ? *ptr : XXH_swap32(*ptr);
+}
+
+FORCE_INLINE U32 XXH_readLE32(const U32* ptr, XXH_endianess endian) {
+ return XXH_readLE32_align(ptr, endian, XXH_unaligned);
+}
+
+
+//****************************
+// Simple Hash Functions
+//****************************
+FORCE_INLINE U32 XXH32_endian_align(const void* input, int len, U32 seed, XXH_endianess endian, XXH_alignment align)
+{
+ const BYTE* p = (const BYTE*)input;
+ const BYTE* const bEnd = p + len;
+ U32 h32;
+
+#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
+ if (p==NULL) { len=0; p=(const BYTE*)(size_t)16; }
+#endif
+
+ if (len>=16) {
+ const BYTE* const limit = bEnd - 16;
+ U32 v1 = seed + PRIME32_1 + PRIME32_2;
+ U32 v2 = seed + PRIME32_2;
+ U32 v3 = seed + 0;
+ U32 v4 = seed - PRIME32_1;
+
+ do {
+ v1 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
+ v2 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
+ v3 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
+ v4 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
+ } while (p<=limit);
+
+ h32 = XXH_rotl32(v1, 1) + XXH_rotl32(v2, 7) + XXH_rotl32(v3, 12) + XXH_rotl32(v4, 18);
+ } else {
+ h32 = seed + PRIME32_5;
+ }
+
+ h32 += (U32)len;
+
+ while (p <= bEnd - 4) {
+ h32 += XXH_readLE32_align((const U32*)p, endian, align) * PRIME32_3;
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4 ;
+ p += 4;
+ }
+
+ while (p < bEnd) {
+ h32 += (*p) * PRIME32_5;
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1 ;
+ p++;
+ }
+
+ h32 ^= h32 >> 15;
+ h32 *= PRIME32_2;
+ h32 ^= h32 >> 13;
+ h32 *= PRIME32_3;
+ h32 ^= h32 >> 16;
+
+ return h32;
+}
+
+
+U32 XXH32(const void* input, int len, U32 seed)
+{
+#if 0
+ // Simple version, good for code maintenance, but unfortunately slow for small inputs
+ void* state = XXH32_init(seed);
+ XXH32_update(state, input, len);
+ return XXH32_digest(state);
+#else
+
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
+
+ #if !defined(XXH_USE_UNALIGNED_ACCESS)
+ if ((((size_t)input) & 3)) { // Input is aligned, let's leverage the speed advantage
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_aligned);
+ else
+ return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_aligned);
+ }
+ #endif
+
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_endian_align(input, len, seed, XXH_littleEndian, XXH_unaligned);
+ else
+ return XXH32_endian_align(input, len, seed, XXH_bigEndian, XXH_unaligned);
+#endif
+}
+
+//****************************
+// Advanced Hash Functions
+//****************************
+
+struct XXH_state32_t {
+ U64 total_len;
+ U32 seed;
+ U32 v1;
+ U32 v2;
+ U32 v3;
+ U32 v4;
+ int memsize;
+ char memory[16];
+};
+
+
+int XXH32_sizeofState()
+{
+ XXH_STATIC_ASSERT(XXH32_SIZEOFSTATE >= sizeof(struct XXH_state32_t)); // A compilation error here means XXH32_SIZEOFSTATE is not large enough
+ return sizeof(struct XXH_state32_t);
+}
+
+
+XXH_errorcode XXH32_resetState(void* state_in, U32 seed)
+{
+ struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
+ state->seed = seed;
+ state->v1 = seed + PRIME32_1 + PRIME32_2;
+ state->v2 = seed + PRIME32_2;
+ state->v3 = seed + 0;
+ state->v4 = seed - PRIME32_1;
+ state->total_len = 0;
+ state->memsize = 0;
+ return XXH_OK;
+}
+
+
+void* XXH32_init (U32 seed)
+{
+ void* state = XXH_malloc(sizeof(struct XXH_state32_t));
+ XXH32_resetState(state, seed);
+ return state;
+}
+
+
+FORCE_INLINE XXH_errorcode XXH32_update_endian (void* state_in, const void* input, int len, XXH_endianess endian)
+{
+ struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
+ const BYTE* p = (const BYTE*)input;
+ const BYTE* const bEnd = p + len;
+
+#ifdef XXH_ACCEPT_NULL_INPUT_POINTER
+ if (input == NULL)
+ return XXH_ERROR;
+#endif
+
+ state->total_len += len;
+
+ if (state->memsize + len < 16) { // fill in tmp buffer
+ XXH_memcpy(state->memory + state->memsize, input, len);
+ state->memsize += len;
+ return XXH_OK;
+ }
+
+ if (state->memsize) // some data left from previous update
+ {
+ XXH_memcpy(state->memory + state->memsize, input, 16-state->memsize);
+ {
+ const U32* p32 = (const U32*)state->memory;
+ state->v1 += XXH_readLE32(p32, endian) * PRIME32_2; state->v1 = XXH_rotl32(state->v1, 13); state->v1 *= PRIME32_1; p32++;
+ state->v2 += XXH_readLE32(p32, endian) * PRIME32_2; state->v2 = XXH_rotl32(state->v2, 13); state->v2 *= PRIME32_1; p32++;
+ state->v3 += XXH_readLE32(p32, endian) * PRIME32_2; state->v3 = XXH_rotl32(state->v3, 13); state->v3 *= PRIME32_1; p32++;
+ state->v4 += XXH_readLE32(p32, endian) * PRIME32_2; state->v4 = XXH_rotl32(state->v4, 13); state->v4 *= PRIME32_1; p32++;
+ }
+ p += 16-state->memsize;
+ state->memsize = 0;
+ }
+
+ if (p <= bEnd-16) {
+ const BYTE* const limit = bEnd - 16;
+ U32 v1 = state->v1;
+ U32 v2 = state->v2;
+ U32 v3 = state->v3;
+ U32 v4 = state->v4;
+
+ do {
+ v1 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v1 = XXH_rotl32(v1, 13); v1 *= PRIME32_1; p+=4;
+ v2 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v2 = XXH_rotl32(v2, 13); v2 *= PRIME32_1; p+=4;
+ v3 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v3 = XXH_rotl32(v3, 13); v3 *= PRIME32_1; p+=4;
+ v4 += XXH_readLE32((const U32*)p, endian) * PRIME32_2; v4 = XXH_rotl32(v4, 13); v4 *= PRIME32_1; p+=4;
+ } while (p<=limit);
+
+ state->v1 = v1;
+ state->v2 = v2;
+ state->v3 = v3;
+ state->v4 = v4;
+ }
+
+ if (p < bEnd) {
+ XXH_memcpy(state->memory, p, bEnd-p);
+ state->memsize = (int)(bEnd-p);
+ }
+
+ return XXH_OK;
+}
+
+XXH_errorcode XXH32_update(void* state_in, const void* input, int len)
+{
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
+
+ if ((endian_detected==XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_update_endian(state_in, input, len, XXH_littleEndian);
+ else
+ return XXH32_update_endian(state_in, input, len, XXH_bigEndian);
+}
+
+
+
+FORCE_INLINE U32 XXH32_intermediateDigest_endian (void* state_in, XXH_endianess endian)
+{
+ struct XXH_state32_t * state = (struct XXH_state32_t *) state_in;
+ const BYTE * p = (const BYTE*)state->memory;
+ BYTE* bEnd = (BYTE*)state->memory + state->memsize;
+ U32 h32;
+
+ if (state->total_len >= 16) {
+ h32 = XXH_rotl32(state->v1, 1) + XXH_rotl32(state->v2, 7) + XXH_rotl32(state->v3, 12) + XXH_rotl32(state->v4, 18);
+ } else {
+ h32 = state->seed + PRIME32_5;
+ }
+
+ h32 += (U32)state->total_len;
+
+ while (p <= bEnd - 4) {
+ h32 += XXH_readLE32((const U32*)p, endian) * PRIME32_3;
+ h32 = XXH_rotl32(h32, 17) * PRIME32_4;
+ p+=4;
+ }
+
+ while (p<bEnd) {
+ h32 += (*p) * PRIME32_5;
+ h32 = XXH_rotl32(h32, 11) * PRIME32_1;
+ p++;
+ }
+
+ h32 ^= h32 >> 15;
+ h32 *= PRIME32_2;
+ h32 ^= h32 >> 13;
+ h32 *= PRIME32_3;
+ h32 ^= h32 >> 16;
+
+ return h32;
+}
+
+
+U32 XXH32_intermediateDigest (void* state_in)
+{
+ XXH_endianess endian_detected = (XXH_endianess)XXH_CPU_LITTLE_ENDIAN;
+
+ if ((endian_detected == XXH_littleEndian) || XXH_FORCE_NATIVE_FORMAT)
+ return XXH32_intermediateDigest_endian(state_in, XXH_littleEndian);
+ else
+ return XXH32_intermediateDigest_endian(state_in, XXH_bigEndian);
+}
+
+
+U32 XXH32_digest (void* state_in)
+{
+ U32 h32 = XXH32_intermediateDigest(state_in);
+
+ XXH_free(state_in);
+
+ return h32;
+}
diff --git a/xxhash.h b/xxhash.h
new file mode 100644
index 0000000..97a1bb2
--- /dev/null
+++ b/xxhash.h
@@ -0,0 +1,171 @@
+/*
+ xxHash - Fast Hash algorithm
+ Header File
+ Copyright (C) 2012-2014, Yann Collet.
+ BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are
+ met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following disclaimer
+ in the documentation and/or other materials provided with the
+ distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ You can contact the author at :
+ - xxHash source repository : http://code.google.com/p/xxhash/
+*/
+
+/* Notice extracted from xxHash homepage :
+
+xxHash is an extremely fast Hash algorithm, running at RAM speed limits.
+It also successfully passes all tests from the SMHasher suite.
+
+Comparison (single thread, Windows Seven 32 bits, using SMHasher on a Core 2 Duo @3GHz)
+
+Name Speed Q.Score Author
+xxHash 5.4 GB/s 10
+CrapWow 3.2 GB/s 2 Andrew
+MumurHash 3a 2.7 GB/s 10 Austin Appleby
+SpookyHash 2.0 GB/s 10 Bob Jenkins
+SBox 1.4 GB/s 9 Bret Mulvey
+Lookup3 1.2 GB/s 9 Bob Jenkins
+SuperFastHash 1.2 GB/s 1 Paul Hsieh
+CityHash64 1.05 GB/s 10 Pike & Alakuijala
+FNV 0.55 GB/s 5 Fowler, Noll, Vo
+CRC32 0.43 GB/s 9
+MD5-32 0.33 GB/s 10 Ronald L. Rivest
+SHA1-32 0.28 GB/s 10
+
+Q.Score is a measure of quality of the hash function.
+It depends on successfully passing SMHasher test set.
+10 is a perfect score.
+*/
+
+#pragma once
+
+#ifndef __XXHASH__
+#define __XXHASH__
+
+#if defined (__cplusplus)
+extern "C" {
+#endif
+
+
+//****************************
+// Type
+//****************************
+typedef enum {
+ XXH_OK=0,
+ XXH_ERROR
+} XXH_errorcode;
+
+
+
+//****************************
+// Simple Hash Functions
+//****************************
+
+/*
+XXH32() :
+ Calculate the 32-bits hash of sequence of length "len" stored at memory address "input".
+ The memory between input & input+len must be valid (allocated and read-accessible).
+ "seed" can be used to alter the result predictably.
+ This function successfully passes all SMHasher tests.
+ Speed on Core 2 Duo @ 3 GHz (single thread, SMHasher benchmark) : 5.4 GB/s
+ Note that "len" is type "int", which means it is limited to 2^31-1.
+ If your data is larger, use the advanced functions below.
+*/
+unsigned int XXH32 (const void* input, int len, unsigned int seed);
+
+
+
+//****************************
+// Advanced Hash Functions
+//****************************
+
+void* XXH32_init (unsigned int seed);
+XXH_errorcode XXH32_update (void* state, const void* input, int len);
+unsigned int XXH32_digest (void* state);
+
+/*
+These functions calculate the xxhash of an input provided in several small packets,
+as opposed to an input provided as a single block.
+
+It must be started with :
+void* XXH32_init()
+The function returns a pointer which holds the state of calculation.
+
+This pointer must be provided as "void* state" parameter for XXH32_update().
+XXH32_update() can be called as many times as necessary.
+The user must provide a valid (allocated) input.
+The function returns an error code, with 0 meaning OK, and any other value meaning there is an error.
+Note that "len" is type "int", which means it is limited to 2^31-1.
+If your data is larger, it is recommended to chunk your data into blocks
+of size for example 2^30 (1GB) to avoid any "int" overflow issue.
+
+Finally, you can end the calculation anytime, by using XXH32_digest().
+This function returns the final 32-bits hash.
+You must provide the same "void* state" parameter created by XXH32_init().
+Memory will be freed by XXH32_digest().
+*/
+
+
+int XXH32_sizeofState(void);
+XXH_errorcode XXH32_resetState(void* state, unsigned int seed);
+
+#define XXH32_SIZEOFSTATE 48
+typedef struct { long long ll[(XXH32_SIZEOFSTATE+(sizeof(long long)-1))/sizeof(long long)]; } XXH32_stateSpace_t;
+/*
+These functions allow user application to make its own allocation for state.
+
+XXH32_sizeofState() is used to know how much space must be allocated for the xxHash 32-bits state.
+Note that the state must be aligned to access 'long long' fields. Memory must be allocated and referenced by a pointer.
+This pointer must then be provided as 'state' into XXH32_resetState(), which initializes the state.
+
+For static allocation purposes (such as allocation on stack, or freestanding systems without malloc()),
+use the structure XXH32_stateSpace_t, which will ensure that memory space is large enough and correctly aligned to access 'long long' fields.
+*/
+
+
+unsigned int XXH32_intermediateDigest (void* state);
+/*
+This function does the same as XXH32_digest(), generating a 32-bit hash,
+but preserve memory context.
+This way, it becomes possible to generate intermediate hashes, and then continue feeding data with XXH32_update().
+To free memory context, use XXH32_digest(), or free().
+*/
+
+
+
+//****************************
+// Deprecated function names
+//****************************
+// The following translations are provided to ease code transition
+// You are encouraged to no longer this function names
+#define XXH32_feed XXH32_update
+#define XXH32_result XXH32_digest
+#define XXH32_getIntermediateResult XXH32_intermediateDigest
+
+
+
+#if defined (__cplusplus)
+}
+#endif
+
+#endif
--
1.7.7
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [RFC PATCH 0/3] Btrfs: add xxhash algorithm
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
` (3 preceding siblings ...)
2014-05-07 10:56 ` [PATCH] Btrfs-progs: add xxhash Liu Bo
@ 2014-05-07 11:08 ` Tomasz Torcz
2014-05-07 20:50 ` Darrick J. Wong
4 siblings, 1 reply; 9+ messages in thread
From: Tomasz Torcz @ 2014-05-07 11:08 UTC (permalink / raw)
To: linux-btrfs
On Wed, May 07, 2014 at 06:56:29PM +0800, Liu Bo wrote:
> "xxHash is an extremely fast non-cryptographic Hash algorithm, working at speeds
> close to RAM limits."[1] And xxhash is 32-bits hash, same as crc32.
>
> Here is the hash comparsion extracted from the link[1]:
> (single thread, Windows Seven 32 bits, using Open Source's SMHasher on a Core 2
> Duo @3GHz)
>
> --------------------------------------------
> Name Speed Q.Score Author
> xxHash 5.4 GB/s 10
> CRC32 0.43 GB/s 9
> --------------------------------------------
Core 2 Duo is awfully old CPU. Since 2008, Intel CPUs have crc32 instruction,
hugely speeding up CRC operations.
--
Tomasz Torcz "God, root, what's the difference?"
xmpp: zdzichubg@chrome.pl "God is more forgiving."
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [RFC PATCH 0/3] Btrfs: add xxhash algorithm
2014-05-07 11:08 ` [RFC PATCH 0/3] Btrfs: add xxhash algorithm Tomasz Torcz
@ 2014-05-07 20:50 ` Darrick J. Wong
2014-05-07 20:58 ` Gregory Maxwell
0 siblings, 1 reply; 9+ messages in thread
From: Darrick J. Wong @ 2014-05-07 20:50 UTC (permalink / raw)
To: Tomasz Torcz, linux-btrfs; +Cc: Liu Bo
On Wed, May 07, 2014 at 01:08:06PM +0200, Tomasz Torcz wrote:
> On Wed, May 07, 2014 at 06:56:29PM +0800, Liu Bo wrote:
> > "xxHash is an extremely fast non-cryptographic Hash algorithm, working at speeds
> > close to RAM limits."[1] And xxhash is 32-bits hash, same as crc32.
> >
> > Here is the hash comparsion extracted from the link[1]:
> > (single thread, Windows Seven 32 bits, using Open Source's SMHasher on a Core 2
> > Duo @3GHz)
> >
> > --------------------------------------------
> > Name Speed Q.Score Author
> > xxHash 5.4 GB/s 10
> > CRC32 0.43 GB/s 9
> > --------------------------------------------
>
> Core 2 Duo is awfully old CPU. Since 2008, Intel CPUs have crc32 instruction,
> hugely speeding up CRC operations.
Just for kicks I (sloppily) benchmarked a few of the kernel's hash
implementations on a Core i5-3320M CPU @3.3GHz:
xxhash: 6.0GB/s
crc32c-intel: 11.5GB/s
crc32c (no hw accel): 1.8GB/s
--D
>
>
> --
> Tomasz Torcz "God, root, what's the difference?"
> xmpp: zdzichubg@chrome.pl "God is more forgiving."
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [RFC PATCH 0/3] Btrfs: add xxhash algorithm
2014-05-07 20:50 ` Darrick J. Wong
@ 2014-05-07 20:58 ` Gregory Maxwell
0 siblings, 0 replies; 9+ messages in thread
From: Gregory Maxwell @ 2014-05-07 20:58 UTC (permalink / raw)
To: Darrick J. Wong; +Cc: Tomasz Torcz, linux-btrfs, Liu Bo
On Wed, May 7, 2014 at 1:50 PM, Darrick J. Wong <darrick.wong@oracle.com> wrote:
> Just for kicks I (sloppily) benchmarked a few of the kernel's hash
> implementations on a Core i5-3320M CPU @3.3GHz:
> xxhash: 6.0GB/s
> crc32c-intel: 11.5GB/s
> crc32c (no hw accel): 1.8GB/s
CRC also usually has the very mild data recovery advantage that if
your error is just a bitflip you can correct it using the crc in a
computationally efficient manner, potentially enabling fancy recovery
tools... so it it were merely equal in speed you'd still probably
prefer to use a CRC.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Btrfs-progs: add xxhash
2014-05-07 10:56 ` [PATCH] Btrfs-progs: add xxhash Liu Bo
@ 2014-05-15 16:57 ` David Sterba
0 siblings, 0 replies; 9+ messages in thread
From: David Sterba @ 2014-05-15 16:57 UTC (permalink / raw)
To: Liu Bo; +Cc: linux-btrfs
Obviously I can't put that patch into progs integration as it's changing
the default behaviour. You can add #ifdefs if you want.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2014-05-15 16:57 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-07 10:56 [RFC PATCH 0/3] Btrfs: add xxhash algorithm Liu Bo
2014-05-07 10:56 ` [PATCH 1/3] Crypto: " Liu Bo
2014-05-07 10:56 ` [PATCH 2/3] Crypto: xxhash: add tests Liu Bo
2014-05-07 10:56 ` [PATCH 3/3] Btrfs: add another checksum algorithm xxhash Liu Bo
2014-05-07 10:56 ` [PATCH] Btrfs-progs: add xxhash Liu Bo
2014-05-15 16:57 ` David Sterba
2014-05-07 11:08 ` [RFC PATCH 0/3] Btrfs: add xxhash algorithm Tomasz Torcz
2014-05-07 20:50 ` Darrick J. Wong
2014-05-07 20:58 ` Gregory Maxwell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).