1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
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>
47 #error Unsupported operating system.
50 #include <lustre/lustre_idl.h>
56 #ifndef HAVE_ARCH_CRC32
57 /* crc32_le lifted from the Linux kernel, which had the following to say:
59 * This code is in the public domain; copyright abandoned.
60 * Liability for non-performance of this code is limited to the amount
61 * you paid for it. Since it is distributed for free, your refund will
62 * be very very small. If it breaks, you get to keep both pieces.
64 #define CRCPOLY_LE 0xedb88320
66 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
67 * @crc - seed value for computation. ~0 for Ethernet, sometimes 0 for
68 * other uses, or the previous crc32 value if computing incrementally.
69 * @p - pointer to buffer over which CRC is run
70 * @len - length of buffer @p
72 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
77 for (i = 0; i < 8; i++)
78 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
84 static inline __u32 init_checksum(cksum_type_t cksum_type)
94 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
100 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
101 size_t len, cksum_type_t cksum_type)
104 case OBD_CKSUM_CRC32:
105 return crc32_le(cksum, p, len);
107 case OBD_CKSUM_ADLER:
108 return adler32(cksum, p, len);
111 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
117 static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
120 case OBD_CKSUM_CRC32:
121 return OBD_FL_CKSUM_CRC32;
123 case OBD_CKSUM_ADLER:
124 return OBD_FL_CKSUM_ADLER;
127 CWARN("unknown cksum type %x\n", cksum_type);
129 return OBD_FL_CKSUM_CRC32;
132 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
134 o_flags &= OBD_FL_CKSUM_ALL;
135 if ((o_flags - 1) & o_flags)
136 CWARN("several checksum types are set: %x\n", o_flags);
137 if (o_flags & OBD_FL_CKSUM_ADLER)
139 return OBD_CKSUM_ADLER;
141 CWARN("checksum type is set to adler32, but adler32 is not "
142 "supported (%x)\n", o_flags);
144 return OBD_CKSUM_CRC32;
148 /* Default preferred checksum algorithm to use (if supported by the server) */
149 #define OSC_DEFAULT_CKSUM OBD_CKSUM_ADLER
150 /* Adler-32 is supported */
151 #define CHECKSUM_ADLER OBD_CKSUM_ADLER
153 #define OSC_DEFAULT_CKSUM OBD_CKSUM_CRC32
154 #define CHECKSUM_ADLER 0
157 #define OBD_CKSUM_ALL (OBD_CKSUM_CRC32 | CHECKSUM_ADLER)
159 /* Checksum algorithm names. Must be defined in the same order as the
160 * OBD_CKSUM_* flags. */
161 #define DECLARE_CKSUM_NAME char *cksum_name[] = {"crc32", "adler"}