1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
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>
33 #error Unsupported operating system.
36 #include <lustre/types.h>
37 #include <lustre/lustre_idl.h>
43 #ifndef HAVE_ARCH_CRC32
44 /* crc32_le lifted from the Linux kernel, which had the following to say:
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.
51 #define CRCPOLY_LE 0xedb88320
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
59 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
64 for (i = 0; i < 8; i++)
65 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
71 static inline __u32 init_checksum(cksum_type_t cksum_type)
81 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
87 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
88 size_t len, cksum_type_t cksum_type)
92 return crc32_le(cksum, p, len);
95 return adler32(cksum, p, len);
98 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
104 static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
107 case OBD_CKSUM_CRC32:
108 return OBD_FL_CKSUM_CRC32;
110 case OBD_CKSUM_ADLER:
111 return OBD_FL_CKSUM_ADLER;
114 CWARN("unknown cksum type %x\n", cksum_type);
116 return OBD_FL_CKSUM_CRC32;
119 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
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)
126 return OBD_CKSUM_ADLER;
128 CWARN("checksum type is set to adler32, but adler32 is not "
129 "supported (%x)\n", o_flags);
131 return OBD_CKSUM_CRC32;
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
140 #define OSC_DEFAULT_CKSUM OBD_CKSUM_CRC32
141 #define CHECKSUM_ADLER 0
144 #define OBD_CKSUM_ALL (OBD_CKSUM_CRC32 | CHECKSUM_ADLER)
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"}