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