Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / include / linux / obd_support.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 _LINUX_OBD_SUPPORT
24 #define _LINUX_OBD_SUPPORT
25
26 #ifndef _OBD_SUPPORT
27 #error Do not #include this file directly. #include <obd_support.h> instead
28 #endif
29
30 #ifdef __KERNEL__
31 #ifndef AUTOCONF_INCLUDED
32 #include <linux/config.h>
33 #endif
34 #include <linux/autoconf.h>
35 #include <linux/slab.h>
36 #include <linux/highmem.h>
37 #endif
38 #include <libcfs/kp30.h>
39 #include <linux/lustre_compat25.h>
40 #include <lustre/lustre_idl.h>
41
42 /* Prefer the kernel's version, if it exports it, because it might be
43  * optimized for this CPU. */
44 #if defined(__KERNEL__) && (defined(CONFIG_CRC32) || defined(CONFIG_CRC32_MODULE))
45 # include <linux/crc32.h>
46 #else
47 /* crc32_le lifted from the Linux kernel, which had the following to say:
48  *
49  * This code is in the public domain; copyright abandoned.
50  * Liability for non-performance of this code is limited to the amount
51  * you paid for it.  Since it is distributed for free, your refund will
52  * be very very small.  If it breaks, you get to keep both pieces.
53  */
54 #define CRCPOLY_LE 0xedb88320
55 /**
56  * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32
57  * @crc - seed value for computation.  ~0 for Ethernet, sometimes 0 for
58  *        other uses, or the previous crc32 value if computing incrementally.
59  * @p   - pointer to buffer over which CRC is run
60  * @len - length of buffer @p
61  */
62 static inline __u32 crc32_le(__u32 crc, unsigned char const *p, size_t len)
63 {
64         int i;
65         while (len--) {
66                 crc ^= *p++;
67                 for (i = 0; i < 8; i++)
68                         crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
69         }
70         return crc;
71 }
72 #endif
73
74 #ifdef __KERNEL__
75 # include <linux/zutil.h>
76 # ifndef HAVE_ADLER
77 #  define HAVE_ADLER
78 # endif
79 #else /* ! __KERNEL__ */
80 # ifdef HAVE_ADLER
81 #  include <zlib.h>
82
83 static inline __u32 zlib_adler32(__u32 adler, unsigned char const *p,
84                                  size_t len)
85 {
86         return adler32(adler, p, len);
87 }
88 # endif
89 #endif /* __KERNEL__ */
90
91 static inline __u32 init_checksum(cksum_type_t cksum_type)
92 {
93         switch(cksum_type) {
94         case OBD_CKSUM_CRC32:
95                 return ~0U;
96 #ifdef HAVE_ADLER
97         case OBD_CKSUM_ADLER:
98                 return 1U;
99 #endif
100         default:
101                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
102                 LBUG();
103         }
104         return 0;
105 }
106
107 static inline __u32 compute_checksum(__u32 cksum, unsigned char const *p,
108                                      size_t len, cksum_type_t cksum_type)
109 {
110         switch(cksum_type) {
111         case OBD_CKSUM_CRC32:
112                 return crc32_le(cksum, p, len);
113 #ifdef HAVE_ADLER
114         case OBD_CKSUM_ADLER:
115                 return zlib_adler32(cksum, p, len);
116 #endif
117         default:
118                 CERROR("Unknown checksum type (%x)!!!\n", cksum_type);
119                 LBUG();
120         }
121         return 0;
122 }
123
124 static inline obd_flag cksum_type_pack(cksum_type_t cksum_type)
125 {
126         switch(cksum_type) {
127         case OBD_CKSUM_CRC32:
128                 return OBD_FL_CKSUM_CRC32;
129 #ifdef HAVE_ADLER
130         case OBD_CKSUM_ADLER:
131                 return OBD_FL_CKSUM_ADLER;
132 #endif
133         default:
134                 CWARN("unknown cksum type %x\n", cksum_type);
135         }
136         return OBD_FL_CKSUM_CRC32;
137 }
138
139 static inline cksum_type_t cksum_type_unpack(obd_flag o_flags)
140 {
141         o_flags &= OBD_FL_CKSUM_ALL;
142         if ((o_flags - 1) & o_flags)
143                 CWARN("several checksum types are set: %x\n", o_flags);
144         if (o_flags & OBD_FL_CKSUM_ADLER)
145 #ifdef HAVE_ADLER
146                 return OBD_CKSUM_ADLER;
147 #else
148                 CWARN("checksum type is set to adler32, but adler32 is not "
149                       "supported (%x)\n", o_flags);
150 #endif
151         return OBD_CKSUM_CRC32;
152 }
153
154 #ifdef __KERNEL__
155 # include <linux/types.h>
156 # include <linux/blkdev.h>
157 # include <lvfs.h>
158 # define OBD_SLEEP_ON(wq, state)  wait_event_interruptible(wq, state)
159 #else /* !__KERNEL__ */
160 # define LTIME_S(time) (time)
161 /* for obd_class.h */
162 # ifndef ERR_PTR
163 #  define ERR_PTR(a) ((void *)(a))
164 # endif
165 #endif  /* __KERNEL__ */
166
167 #endif