Whamcloud - gitweb
029d1a7275cff22b45308417167f55f574bb4840
[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  */
31
32 #ifndef __OBD_CKSUM
33 #define __OBD_CKSUM
34 #include <libcfs/libcfs.h>
35 #include <libcfs/libcfs_crypto.h>
36 #include <uapi/linux/lustre/lustre_idl.h>
37
38 int obd_t10_cksum_speed(const char *obd_name,
39                         enum cksum_types cksum_type);
40
41 static inline unsigned char cksum_obd2cfs(enum cksum_types cksum_type)
42 {
43         switch (cksum_type) {
44         case OBD_CKSUM_CRC32:
45                 return CFS_HASH_ALG_CRC32;
46         case OBD_CKSUM_ADLER:
47                 return CFS_HASH_ALG_ADLER32;
48         case OBD_CKSUM_CRC32C:
49                 return CFS_HASH_ALG_CRC32C;
50         default:
51                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
52                 LBUG();
53         }
54         return 0;
55 }
56
57 u32 obd_cksum_type_pack(const char *obd_name, enum cksum_types cksum_type);
58
59 static inline enum cksum_types obd_cksum_type_unpack(u32 o_flags)
60 {
61         switch (o_flags & OBD_FL_CKSUM_ALL) {
62         case OBD_FL_CKSUM_CRC32C:
63                 return OBD_CKSUM_CRC32C;
64         case OBD_FL_CKSUM_CRC32:
65                 return OBD_CKSUM_CRC32;
66         case OBD_FL_CKSUM_T10IP512:
67                 return OBD_CKSUM_T10IP512;
68         case OBD_FL_CKSUM_T10IP4K:
69                 return OBD_CKSUM_T10IP4K;
70         case OBD_FL_CKSUM_T10CRC512:
71                 return OBD_CKSUM_T10CRC512;
72         case OBD_FL_CKSUM_T10CRC4K:
73                 return OBD_CKSUM_T10CRC4K;
74         default:
75                 break;
76         }
77
78         return OBD_CKSUM_ADLER;
79 }
80
81 /* Return a bitmask of the checksum types supported on this system.
82  * 1.8 supported ADLER it is base and not depend on hw
83  * Client uses all available local algos
84  */
85 static inline enum cksum_types obd_cksum_types_supported_client(void)
86 {
87         enum cksum_types ret = OBD_CKSUM_ADLER;
88
89         CDEBUG(D_INFO, "Crypto hash speed: crc %d, crc32c %d, adler %d\n",
90                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)),
91                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)),
92                cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_ADLER)));
93
94         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32C)) > 0)
95                 ret |= OBD_CKSUM_CRC32C;
96         if (cfs_crypto_hash_speed(cksum_obd2cfs(OBD_CKSUM_CRC32)) > 0)
97                 ret |= OBD_CKSUM_CRC32;
98
99         /* Client support all kinds of T10 checksum */
100         ret |= OBD_CKSUM_T10_ALL;
101
102         return ret;
103 }
104
105 enum cksum_types obd_cksum_types_supported_server(const char *obd_name);
106
107 /* Select the best checksum algorithm among those supplied in the cksum_types
108  * input.
109  *
110  * Currently, calling cksum_type_pack() with a mask will return the fastest
111  * checksum type due to its benchmarking at libcfs module load.
112  * Caution is advised, however, since what is fastest on a single client may
113  * not be the fastest or most efficient algorithm on the server.  */
114 static inline
115 enum cksum_types obd_cksum_type_select(const char *obd_name,
116                                        enum cksum_types cksum_types,
117                                        enum cksum_types preferred)
118 {
119         u32 flag;
120
121         if (preferred & cksum_types)
122                 return preferred;
123
124         /*
125          * Server reporting a single T10 checksum type
126          * means the target actually supports T10-PI.
127          */
128         if (hweight32(cksum_types & OBD_CKSUM_T10_ALL) == 1)
129                 return cksum_types & OBD_CKSUM_T10_ALL;
130
131         flag = obd_cksum_type_pack(obd_name, cksum_types);
132
133         return obd_cksum_type_unpack(flag);
134 }
135
136 /* Checksum algorithm names. Must be defined in the same order as the
137  * OBD_CKSUM_* flags. */
138 #define DECLARE_CKSUM_NAME const char *const cksum_name[] = {"crc32", "adler", \
139         "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
140
141 typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
142
143 __u16 obd_dif_crc_fn(void *data, unsigned int len);
144 __u16 obd_dif_ip_fn(void *data, unsigned int len);
145 int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
146                                  __u32 offset, __u32 length,
147                                  __u16 *guard_start, int guard_number,
148                                  int *used_number, int sector_size,
149                                  obd_dif_csum_fn *fn);
150 /*
151  * If checksum type is one T10 checksum types, init the csum_fn and sector
152  * size. Otherwise, init them to NULL/zero.
153  */
154 static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
155                                      obd_dif_csum_fn **fn, int *sector_size)
156 {
157         *fn = NULL;
158         *sector_size = 0;
159
160 #if IS_ENABLED(CONFIG_CRC_T10DIF)
161         switch (cksum_type) {
162         case OBD_CKSUM_T10IP512:
163                 *fn = obd_dif_ip_fn;
164                 *sector_size = 512;
165                 break;
166         case OBD_CKSUM_T10IP4K:
167                 *fn = obd_dif_ip_fn;
168                 *sector_size = 4096;
169                 break;
170         case OBD_CKSUM_T10CRC512:
171                 *fn = obd_dif_crc_fn;
172                 *sector_size = 512;
173                 break;
174         case OBD_CKSUM_T10CRC4K:
175                 *fn = obd_dif_crc_fn;
176                 *sector_size = 4096;
177                 break;
178         default:
179                 break;
180         }
181 #endif /* CONFIG_CRC_T10DIF */
182 }
183
184 enum obd_t10_cksum_type {
185         OBD_T10_CKSUM_UNKNOWN = 0,
186         OBD_T10_CKSUM_IP512,
187         OBD_T10_CKSUM_IP4K,
188         OBD_T10_CKSUM_CRC512,
189         OBD_T10_CKSUM_CRC4K,
190         OBD_T10_CKSUM_MAX
191 };
192
193 #endif /* __OBD_H */