Whamcloud - gitweb
LU-11770 osc: allow build without blk_integrity or crc-t10pi
[fs/lustre-release.git] / lustre / include / obd_cksum.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32
33 #ifndef __OBD_CKSUM
34 #define __OBD_CKSUM
35 #include <libcfs/libcfs.h>
36 #include <libcfs/libcfs_crypto.h>
37 #include <uapi/linux/lustre/lustre_idl.h>
38
39 int obd_t10_cksum_speed(const char *obd_name,
40                         enum cksum_types cksum_type);
41
42 static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
43 {
44         switch (cksum_type) {
45         case OBD_CKSUM_CRC32:
46                 return CFS_HASH_ALG_CRC32;
47         case OBD_CKSUM_ADLER:
48                 return CFS_HASH_ALG_ADLER32;
49         case OBD_CKSUM_CRC32C:
50                 return CFS_HASH_ALG_CRC32C;
51         default:
52                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
53                 LBUG();
54         }
55         return 0;
56 }
57
58 u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
59
60 static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
61 {
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;
75         default:
76                 break;
77         }
78
79         return OBD_CKSUM_ADLER;
80 }
81
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
85  */
86 static inline enum cksum_types obd_cksum_types_supported_client(void)
87 {
88         enum cksum_types ret = OBD_CKSUM_ADLER;
89
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)));
94
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;
99
100         /* Client support all kinds of T10 checksum */
101         ret |= OBD_CKSUM_T10_ALL;
102
103         return ret;
104 }
105
106 enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
107
108 /* Select the best checksum algorithm among those supplied in the cksum_types
109  * input.
110  *
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)
117 {
118         u32 flag = obd_cksum_type_pack(obd_name, cksum_types);
119
120         return obd_cksum_type_unpack(flag);
121 }
122
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"}
127
128 typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
129
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);
137 /*
138  * If checksum type is one T10 checksum types, init the csum_fn and sector
139  * size. Otherwise, init them to NULL/zero.
140  */
141 static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
142                                      obd_dif_csum_fn **fn, int *sector_size)
143 {
144         *fn = NULL;
145         *sector_size = 0;
146
147 #if IS_ENABLED(CONFIG_CRC_T10DIF)
148         switch (cksum_type) {
149         case OBD_CKSUM_T10IP512:
150                 *fn = obd_dif_ip_fn;
151                 *sector_size = 512;
152                 break;
153         case OBD_CKSUM_T10IP4K:
154                 *fn = obd_dif_ip_fn;
155                 *sector_size = 4096;
156                 break;
157         case OBD_CKSUM_T10CRC512:
158                 *fn = obd_dif_crc_fn;
159                 *sector_size = 512;
160                 break;
161         case OBD_CKSUM_T10CRC4K:
162                 *fn = obd_dif_crc_fn;
163                 *sector_size = 4096;
164                 break;
165         default:
166                 break;
167         }
168 #endif /* CONFIG_CRC_T10DIF */
169 }
170
171 enum obd_t10_cksum_type {
172         OBD_T10_CKSUM_UNKNOWN = 0,
173         OBD_T10_CKSUM_IP512,
174         OBD_T10_CKSUM_IP4K,
175         OBD_T10_CKSUM_CRC512,
176         OBD_T10_CKSUM_CRC4K,
177         OBD_T10_CKSUM_MAX
178 };
179
180 #endif /* __OBD_H */