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