Whamcloud - gitweb
LU-10467 ptlrpc: discard SVC_SIGNAL and related functions
[fs/lustre-release.git] / lustre / include / obd_cksum.h
index e56ff82..6e807d7 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * in the LICENSE file that accompanied this code).
  *
  * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see [sun.com URL with a
- * copy of GPLv2].
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * version 2 along with this program; If not, see
+ * http://www.gnu.org/licenses/gpl-2.0.html
  *
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2014, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #ifndef __OBD_CKSUM
 #define __OBD_CKSUM
+#include <libcfs/libcfs.h>
+#include <libcfs/libcfs_crypto.h>
+#include <uapi/linux/lustre/lustre_idl.h>
 
-#if defined(__linux__)
-#include <linux/obd_cksum.h>
-#elif defined(__APPLE__)
-#include <darwin/obd_chksum.h>
-#elif defined(__WINNT__)
-#include <winnt/obd_cksum.h>
-#else
-#error Unsupported operating system.
-#endif
-
-#include <lustre/lustre_idl.h>
-
-/*
- * Checksums
- */
+int obd_t10_cksum_speed(const char *obd_name,
+                       enum cksum_types cksum_type);
 
-#ifndef HAVE_ARCH_CRC32
-/* crc32_le lifted from the Linux kernel, which had the following to say:
- *
- * This code is in the public domain; copyright abandoned.
- * Liability for non-performance of this code is limited to the amount
- * you paid for it.  Since it is distributed for free, your refund will
- * be very very small.  If it breaks, you get to keep both pieces.
- */
-#define CRCPOLY_LE 0xedb88320
-/**
- * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
- * @crc - seed value for computation.  ~0 for Ethernet, sometimes 0 for
- *        other uses, or the previous crc32 value if computing incrementally.
- * @p   - pointer to buffer over which CRC is run
- * @len - length of buffer @p
- */
-static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
+static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
 {
-        int i;
-        while (len--) {
-                crc ^= *p++;
-                for (i = 0; i < 8; i++)
-                        crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
-        }
-        return crc;
+       switch (cksum_type) {
+       case OBD_CKSUM_CRC32:
+               return CFS_HASH_ALG_CRC32;
+       case OBD_CKSUM_ADLER:
+               return CFS_HASH_ALG_ADLER32;
+       case OBD_CKSUM_CRC32C:
+               return CFS_HASH_ALG_CRC32C;
+       default:
+               CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
+               LBUG();
+       }
+       return 0;
 }
-#endif
 
-static inline __u32 init_checksum(cksum_type_t cksum_type)
-{
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return ~0U;
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return 1U;
-#endif
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
-}
+u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
 
-static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
-                                     size_t len, cksum_type_t cksum_type)
+static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
 {
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return crc32_le(cksum, p, len);
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return adler32(cksum, p, len);
-#endif
-        default:
-                CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
-                LBUG();
-        }
-        return 0;
-}
+       switch (o_flags & OBD_FL_CKSUM_ALL) {
+       case OBD_FL_CKSUM_CRC32C:
+               return OBD_CKSUM_CRC32C;
+       case OBD_FL_CKSUM_CRC32:
+               return OBD_CKSUM_CRC32;
+       case OBD_FL_CKSUM_T10IP512:
+               return OBD_CKSUM_T10IP512;
+       case OBD_FL_CKSUM_T10IP4K:
+               return OBD_CKSUM_T10IP4K;
+       case OBD_FL_CKSUM_T10CRC512:
+               return OBD_CKSUM_T10CRC512;
+       case OBD_FL_CKSUM_T10CRC4K:
+               return OBD_CKSUM_T10CRC4K;
+       default:
+               break;
+       }
 
-static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
-{
-        switch(cksum_type) {
-        case OBD_CKSUM_CRC32:
-                return OBD_FL_CKSUM_CRC32;
-#ifdef HAVE_ADLER
-        case OBD_CKSUM_ADLER:
-                return OBD_FL_CKSUM_ADLER;
-#endif
-        default:
-                CWARN("unknown cksum type %x\n", cksum_type);
-        }
-        return OBD_FL_CKSUM_CRC32;
+       return OBD_CKSUM_ADLER;
 }
 
-static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
+/* Return a bitmask of the checksum types supported on this system.
+ * 1.8 supported ADLER it is base and not depend on hw
+ * Client uses all available local algos
+ */
+static inline enum cksum_types obd_cksum_types_supported_client(void)
 {
-        o_flags &= OBD_FL_CKSUM_ALL;
-        if ((o_flags - 1) & o_flags)
-                CWARN("several checksum types are set: %x\n", o_flags);
-        if (o_flags & OBD_FL_CKSUM_ADLER)
-#ifdef HAVE_ADLER
-                return OBD_CKSUM_ADLER;
-#else
-                CWARN("checksum type is set to adler32, but adler32 is not "
-                      "supported (%x)\n", o_flags);
-#endif
-        return OBD_CKSUM_CRC32;
+       enum cksum_types ret = OBD_CKSUM_ADLER;
+
+       CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
+              cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
+
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
+               ret |= OBD_CKSUM_CRC32C;
+       if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
+               ret |= OBD_CKSUM_CRC32;
+
+       /* Client support all kinds of T10 checksum */
+       ret |= OBD_CKSUM_T10_ALL;
+
+       return ret;
 }
 
-#ifdef HAVE_ADLER
-/* Default preferred checksum algorithm to use (if supported by the server) */
-#define OSC_DEFAULT_CKSUM OBD_CKSUM_ADLER
-/* Adler-32 is supported */
-#define CHECKSUM_ADLER OBD_CKSUM_ADLER
-#else
-#define OSC_DEFAULT_CKSUM OBD_CKSUM_CRC32
-#define CHECKSUM_ADLER 0
-#endif
+enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
 
-#define OBD_CKSUM_ALL (OBD_CKSUM_CRC32 | CHECKSUM_ADLER)
+/* Select the best checksum algorithm among those supplied in the cksum_types
+ * input.
+ *
+ * Currently, calling cksum_type_pack() with a mask will return the fastest
+ * checksum type due to its benchmarking at libcfs module load.
+ * Caution is advised, however, since what is fastest on a single client may
+ * not be the fastest or most efficient algorithm on the server.  */
+static inline enum cksum_types
+obd_cksum_type_select(const char *obd_name, enum cksum_types cksum_types)
+{
+       u32 flag = obd_cksum_type_pack(obd_name, cksum_types);
+
+       return obd_cksum_type_unpack(flag);
+}
 
 /* Checksum algorithm names. Must be defined in the same order as the
  * OBD_CKSUM_* flags. */
-#define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler"}
+#define DECLARE_CKSUM_NAME const char *cksum_name[] = {"crc32", "adler", \
+       "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
+
+typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
+
+__u16 obd_dif_crc_fn(void *data, unsigned int len);
+__u16 obd_dif_ip_fn(void *data, unsigned int len);
+int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
+                                __u32 offset, __u32 length,
+                                __u16 *guard_start, int guard_number,
+                                int *used_number, int sector_size,
+                                obd_dif_csum_fn *fn);
+/*
+ * If checksum type is one T10 checksum types, init the csum_fn and sector
+ * size. Otherwise, init them to NULL/zero.
+ */
+static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
+                                    obd_dif_csum_fn **fn, int *sector_size)
+{
+       *fn = NULL;
+       *sector_size = 0;
+
+#if IS_ENABLED(CONFIG_CRC_T10DIF)
+       switch (cksum_type) {
+       case OBD_CKSUM_T10IP512:
+               *fn = obd_dif_ip_fn;
+               *sector_size = 512;
+               break;
+       case OBD_CKSUM_T10IP4K:
+               *fn = obd_dif_ip_fn;
+               *sector_size = 4096;
+               break;
+       case OBD_CKSUM_T10CRC512:
+               *fn = obd_dif_crc_fn;
+               *sector_size = 512;
+               break;
+       case OBD_CKSUM_T10CRC4K:
+               *fn = obd_dif_crc_fn;
+               *sector_size = 4096;
+               break;
+       default:
+               break;
+       }
+#endif /* CONFIG_CRC_T10DIF */
+}
+
+enum obd_t10_cksum_type {
+       OBD_T10_CKSUM_UNKNOWN = 0,
+       OBD_T10_CKSUM_IP512,
+       OBD_T10_CKSUM_IP4K,
+       OBD_T10_CKSUM_CRC512,
+       OBD_T10_CKSUM_CRC4K,
+       OBD_T10_CKSUM_MAX
+};
 
 #endif /* __OBD_H */