Whamcloud - gitweb
8234d0b650aa96d085f6d9bdc47a071c4b5216bc
[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         flag = obd_cksum_type_pack(obd_name, cksum_types);
125
126         return obd_cksum_type_unpack(flag);
127 }
128
129 /* Checksum algorithm names. Must be defined in the same order as the
130  * OBD_CKSUM_* flags. */
131 #define DECLARE_CKSUM_NAME const char *const cksum_name[] = {"crc32", "adler", \
132         "crc32c", "reserved", "t10ip512", "t10ip4K", "t10crc512", "t10crc4K"}
133
134 typedef __u16 (obd_dif_csum_fn) (void *, unsigned int);
135
136 __u16 obd_dif_crc_fn(void *data, unsigned int len);
137 __u16 obd_dif_ip_fn(void *data, unsigned int len);
138 int obd_page_dif_generate_buffer(const char *obd_name, struct page *page,
139                                  __u32 offset, __u32 length,
140                                  __u16 *guard_start, int guard_number,
141                                  int *used_number, int sector_size,
142                                  obd_dif_csum_fn *fn);
143 /*
144  * If checksum type is one T10 checksum types, init the csum_fn and sector
145  * size. Otherwise, init them to NULL/zero.
146  */
147 static inline void obd_t10_cksum2dif(enum cksum_types cksum_type,
148                                      obd_dif_csum_fn **fn, int *sector_size)
149 {
150         *fn = NULL;
151         *sector_size = 0;
152
153 #if IS_ENABLED(CONFIG_CRC_T10DIF)
154         switch (cksum_type) {
155         case OBD_CKSUM_T10IP512:
156                 *fn = obd_dif_ip_fn;
157                 *sector_size = 512;
158                 break;
159         case OBD_CKSUM_T10IP4K:
160                 *fn = obd_dif_ip_fn;
161                 *sector_size = 4096;
162                 break;
163         case OBD_CKSUM_T10CRC512:
164                 *fn = obd_dif_crc_fn;
165                 *sector_size = 512;
166                 break;
167         case OBD_CKSUM_T10CRC4K:
168                 *fn = obd_dif_crc_fn;
169                 *sector_size = 4096;
170                 break;
171         default:
172                 break;
173         }
174 #endif /* CONFIG_CRC_T10DIF */
175 }
176
177 enum obd_t10_cksum_type {
178         OBD_T10_CKSUM_UNKNOWN = 0,
179         OBD_T10_CKSUM_IP512,
180         OBD_T10_CKSUM_IP4K,
181         OBD_T10_CKSUM_CRC512,
182         OBD_T10_CKSUM_CRC4K,
183         OBD_T10_CKSUM_MAX
184 };
185
186 #endif /* __OBD_H */