4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2014, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
35 #include <libcfs/libcfs.h>
36 #include <libcfs/libcfs_crypto.h>
37 #include <uapi/linux/lustre/lustre_idl.h>
39 int obd_t10_cksum_speed(const char *obd_name,
40 enum cksum_types cksum_type);
42 static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
46 return CFS_HASH_ALG_CRC32;
48 return CFS_HASH_ALG_ADLER32;
49 case OBD_CKSUM_CRC32C:
50 return CFS_HASH_ALG_CRC32C;
52 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
58 u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
60 static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
62 switch (o_flags & OBD_FL_CKSUM_ALL) {
63 case OBD_FL_CKSUM_CRC32C:
64 return OBD_CKSUM_CRC32C;
65 case OBD_FL_CKSUM_CRC32:
66 return OBD_CKSUM_CRC32;
67 case OBD_FL_CKSUM_T10IP512:
68 return OBD_CKSUM_T10IP512;
69 case OBD_FL_CKSUM_T10IP4K:
70 return OBD_CKSUM_T10IP4K;
71 case OBD_FL_CKSUM_T10CRC512:
72 return OBD_CKSUM_T10CRC512;
73 case OBD_FL_CKSUM_T10CRC4K:
74 return OBD_CKSUM_T10CRC4K;
79 return OBD_CKSUM_ADLER;
82 /* Return a bitmask of the checksum types supported on this system.
83 * 1.8 supported ADLER it is base and not depend on hw
84 * Client uses all available local algos
86 static inline enum cksum_types obd_cksum_types_supported_client(void)
88 enum cksum_types ret = OBD_CKSUM_ADLER;
90 CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
91 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
92 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
93 cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
95 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
96 ret |= OBD_CKSUM_CRC32C;
97 if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
98 ret |= OBD_CKSUM_CRC32;
100 /* Client support all kinds of T10 checksum */
101 ret |= OBD_CKSUM_T10_ALL;
106 enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
108 /* Select the best checksum algorithm among those supplied in the cksum_types
111 * Currently, calling cksum_type_pack() with a mask will return the fastest
112 * checksum type due to its benchmarking at libcfs module load.
113 * Caution is advised, however, since what is fastest on a single client may
114 * not be the fastest or most efficient algorithm on the server. */
115 static inline enum cksum_types
116 obd_cksum_type_select(const char *obd_name, enum cksum_types cksum_types)
118 u32 flag = obd_cksum_type_pack(obd_name, cksum_types);
120 return obd_cksum_type_unpack(flag);
123 /* Checksum algorithm names. Must be defined in the same order as the
124 * OBD_CKSUM_* flags. */
125 #define DECLARE_CKSUM_NAME const char *cksum_name[] = {"crc32", "adler", \
126 "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
128 typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
130 __u16 obd_dif_crc_fn(void *data, unsigned int len);
131 __u16 obd_dif_ip_fn(void *data, unsigned int len);
132 int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
133 __u32 offset, __u32 length,
134 __u16 *guard_start, int guard_number,
135 int *used_number, int sector_size,
136 obd_dif_csum_fn *fn);
138 * If checksum type is one T10 checksum types, init the csum_fn and sector
139 * size. Otherwise, init them to NULL/zero.
141 static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
142 obd_dif_csum_fn **fn, int *sector_size)
147 switch (cksum_type) {
148 case OBD_CKSUM_T10IP512:
152 case OBD_CKSUM_T10IP4K:
156 case OBD_CKSUM_T10CRC512:
157 *fn = obd_dif_crc_fn;
160 case OBD_CKSUM_T10CRC4K:
161 *fn = obd_dif_crc_fn;
169 enum obd_t10_cksum_type {
170 OBD_T10_CKSUM_UNKNOWN = 0,
173 OBD_T10_CKSUM_CRC512,