Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / include / obd_cksum.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __OBD_CKSUM
38 #define __OBD_CKSUM
39
40 #if defined(__linux__)
41 #include <linux/obd_cksum.h>
42 #elif defined(__APPLE__)
43 #include <darwin/obd_chksum.h>
44 #elif defined(__WINNT__)
45 #include <winnt/obd_cksum.h>
46 #else
47 #error Unsupported operating system.
48 #endif
49
50 #include <lustre/types.h>
51 #include <lustre/lustre_idl.h>
52
53 /*
54  * Checksums
55  */
56
57 #ifndef HAVE_ARCH_CRC32
58 /* crc32_le lifted from the Linux kernel, which had the following to say:
59  *
60  * This code is in the public domain; copyright abandoned.
61  * Liability for non-performance of this code is limited to the amount
62  * you paid for it.  Since it is distributed for free, your refund will
63  * be very very small.  If it breaks, you get to keep both pieces.
64  */
65 #define CRCPOLY_LE 0xedb88320
66 /**
67  * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
68  * @crc - seed value for computation.  ~0 for Ethernet, sometimes 0 for
69  *        other uses, or the previous crc32 value if computing incrementally.
70  * @p   - pointer to buffer over which CRC is run
71  * @len - length of buffer @p
72  */
73 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
74 {
75         int i;
76         while (len--) {
77                 crc ^= *p++;
78                 for (i = 0; i < 8; i++)
79                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
80         }
81         return crc;
82 }
83 #endif
84
85 static inline __u32 init_checksum(cksum_type_t cksum_type)
86 {
87         switch(cksum_type) {
88         case OBD_CKSUM_CRC32:
89                 return ~0U;
90 #ifdef HAVE_ADLER
91         case OBD_CKSUM_ADLER:
92                 return 1U;
93 #endif
94         default:
95                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
96                 LBUG();
97         }
98         return 0;
99 }
100
101 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
102                                      size_t len, cksum_type_t cksum_type)
103 {
104         switch(cksum_type) {
105         case OBD_CKSUM_CRC32:
106                 return crc32_le(cksum, p, len);
107 #ifdef HAVE_ADLER
108         case OBD_CKSUM_ADLER:
109                 return adler32(cksum, p, len);
110 #endif
111         default:
112                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
113                 LBUG();
114         }
115         return 0;
116 }
117
118 static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
119 {
120         switch(cksum_type) {
121         case OBD_CKSUM_CRC32:
122                 return OBD_FL_CKSUM_CRC32;
123 #ifdef HAVE_ADLER
124         case OBD_CKSUM_ADLER:
125                 return OBD_FL_CKSUM_ADLER;
126 #endif
127         default:
128                 CWARN("unknown cksum type %x\n", cksum_type);
129         }
130         return OBD_FL_CKSUM_CRC32;
131 }
132
133 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
134 {
135         o_flags &= OBD_FL_CKSUM_ALL;
136         if ((o_flags - 1) & o_flags)
137                 CWARN("several checksum types are set: %x\n", o_flags);
138         if (o_flags & OBD_FL_CKSUM_ADLER)
139 #ifdef HAVE_ADLER
140                 return OBD_CKSUM_ADLER;
141 #else
142                 CWARN("checksum type is set to adler32, but adler32 is not "
143                       "supported (%x)\n", o_flags);
144 #endif
145         return OBD_CKSUM_CRC32;
146 }
147
148 #ifdef HAVE_ADLER
149 /* Default preferred checksum algorithm to use (if supported by the server) */
150 #define OSC_DEFAULT_CKSUM OBD_CKSUM_ADLER
151 /* Adler-32 is supported */
152 #define CHECKSUM_ADLER OBD_CKSUM_ADLER
153 #else
154 #define OSC_DEFAULT_CKSUM OBD_CKSUM_CRC32
155 #define CHECKSUM_ADLER 0
156 #endif
157
158 #define OBD_CKSUM_ALL (OBD_CKSUM_CRC32 | CHECKSUM_ADLER)
159
160 /* Checksum algorithm names. Must be defined in the same order as the
161  * OBD_CKSUM_* flags. */
162 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler"}
163
164 #endif /* __OBD_H */