Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[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 _OBD_SUPPORT
24 #define _OBD_SUPPORT
25
26 #ifdef __KERNEL__
27 #include <linux/config.h>
28 #include <linux/autoconf.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #else
32
33 #endif
34 #include <linux/kp30.h>
35
36 /* global variables */
37 extern atomic_t obd_memory;
38 extern int obd_memmax;
39 extern unsigned long obd_fail_loc;
40 extern unsigned long obd_timeout;
41 extern char obd_lustre_upcall[128];
42 extern unsigned long obd_sync_filter;
43
44 #define OBD_FAIL_MDS                     0x100
45 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
46 #define OBD_FAIL_MDS_GETATTR_NET         0x102
47 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
48 #define OBD_FAIL_MDS_READPAGE_NET        0x104
49 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
50 #define OBD_FAIL_MDS_SENDPAGE            0x106
51 #define OBD_FAIL_MDS_REINT_NET           0x107
52 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
53 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
54 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
55 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
56 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
57 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
58 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
59 #define OBD_FAIL_MDS_REINT_LINK          0x10f
60 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
61 #define OBD_FAIL_MDS_REINT_RENAME        0x111
62 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
63 #define OBD_FAIL_MDS_OPEN_NET            0x113
64 #define OBD_FAIL_MDS_OPEN_PACK           0x114
65 #define OBD_FAIL_MDS_CLOSE_NET           0x115
66 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
67 #define OBD_FAIL_MDS_CONNECT_NET         0x117
68 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
69 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
70 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
71 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
72 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
73 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
74 #define OBD_FAIL_MDS_STATFS_NET          0x11e
75 #define OBD_FAIL_MDS_GETATTR_NAME_NET    0x11f
76 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x120
77 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x121
78
79 #define OBD_FAIL_OST                     0x200
80 #define OBD_FAIL_OST_CONNECT_NET         0x201
81 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
82 #define OBD_FAIL_OST_GET_INFO_NET        0x203
83 #define OBD_FAIL_OST_CREATE_NET          0x204
84 #define OBD_FAIL_OST_DESTROY_NET         0x205
85 #define OBD_FAIL_OST_GETATTR_NET         0x206
86 #define OBD_FAIL_OST_SETATTR_NET         0x207
87 #define OBD_FAIL_OST_OPEN_NET            0x208
88 #define OBD_FAIL_OST_CLOSE_NET           0x209
89 #define OBD_FAIL_OST_BRW_NET             0x20a
90 #define OBD_FAIL_OST_PUNCH_NET           0x20b
91 #define OBD_FAIL_OST_STATFS_NET          0x20c
92 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
93 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
94 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
95 #define OBD_FAIL_OST_SYNCFS_NET          0x210
96 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
97 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
98 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
99
100 #define OBD_FAIL_LDLM                    0x300
101 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
102 #define OBD_FAIL_LDLM_ENQUEUE            0x302
103 #define OBD_FAIL_LDLM_CONVERT            0x303
104 #define OBD_FAIL_LDLM_CANCEL             0x304
105 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
106 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
107
108 #define OBD_FAIL_OSC                     0x400
109 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
110 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
111 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
112 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
113
114 #define OBD_FAIL_PTLRPC                  0x500
115 #define OBD_FAIL_PTLRPC_ACK              0x501
116
117 /* preparation for a more advanced failure testbed (not functional yet) */
118 #define OBD_FAIL_MASK_SYS    0x0000FF00
119 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
120 #define OBD_FAIL_ONCE        0x80000000
121 #define OBD_FAILED           0x40000000
122 #define OBD_FAIL_MDS_ALL_NET 0x01000000
123 #define OBD_FAIL_OST_ALL_NET 0x02000000
124
125 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
126                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
127                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
128                                 (OBD_FAILED | OBD_FAIL_ONCE)))
129
130 #define OBD_FAIL_CHECK_ONCE(id)                                              \
131 ({      int _ret_ = 0;                                                       \
132         if (OBD_FAIL_CHECK(id)) {                                            \
133                 CERROR("obd_fail_loc=%x\n", id);                             \
134                 obd_fail_loc |= OBD_FAILED;                                  \
135                 if ((id) & OBD_FAIL_ONCE)                                    \
136                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
137                 _ret_ = 1;                                                   \
138         }                                                                    \
139         _ret_;                                                               \
140 })
141
142 #define OBD_FAIL_RETURN(id, ret)                                             \
143 do {                                                                         \
144         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
145                 RETURN(ret);                                                 \
146         }                                                                    \
147 } while(0)
148
149 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
150
151 #ifdef __KERNEL__
152 #include <linux/types.h>
153 #include <linux/blkdev.h>
154
155
156 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
157 #define ll_bdevname(a) __bdevname((a))
158 #define ll_lock_kernel lock_kernel()
159 #define LTIME_S(time) (time.tv_sec)
160 #else
161 #define ll_lock_kernel
162 #define ll_bdevname(a) bdevname((a))
163 #define LTIME_S(time) (time)
164 #endif
165
166
167 static inline void OBD_FAIL_WRITE(int id, kdev_t dev)
168 {
169         if (OBD_FAIL_CHECK(id)) {
170 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
171 #ifdef CONFIG_DEV_RDONLY
172                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
173                        id, ll_bdevname(dev));
174                 dev_set_rdonly(dev, 2);
175 #else
176                 CERROR("obd_fail_loc=%x, can't fail write operation on %s\n",
177                        id, ll_bdevname(dev));
178 #endif
179 #else
180 #ifdef CONFIG_DEV_RDONLY
181                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
182                        id, ll_bdevname(dev.value));
183                 dev_set_rdonly(dev, 2);
184 #else
185                 CERROR("obd_fail_loc=%x, can't fail write operation on %s\n",
186                        id, ll_bdevname(dev.value));
187 #endif
188 #endif
189                 /* We set FAIL_ONCE because we never "un-fail" a device */
190                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
191         }
192 }
193 #else /* !__KERNEL__ */
194 #define LTIME_S(time) (time)
195 #endif  /* __KERNEL__ */
196
197 #define OBD_ALLOC(ptr, size)                                            \
198 do {                                                                    \
199         void *lptr;                                                     \
200         int s = (size);                                                 \
201         (ptr) = lptr = kmalloc(s, GFP_KERNEL);                          \
202         if (lptr == NULL) {                                             \
203                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed "       \
204                        "at %s:%d\n", s, __FILE__, __LINE__);            \
205         } else {                                                        \
206                 int obd_curmem;                                         \
207                 memset(lptr, 0, s);                                     \
208                 atomic_add(s, &obd_memory);                             \
209                 obd_curmem = atomic_read(&obd_memory);                  \
210                 if (obd_curmem > obd_memmax)                            \
211                         obd_memmax = obd_curmem;                        \
212                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p "      \
213                        "(tot %d)\n", s, lptr, obd_curmem);              \
214         }                                                               \
215 } while (0)
216
217 #ifdef __arch_um__
218 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
219 #else
220 # define OBD_VMALLOC(ptr, size)                                         \
221 do {                                                                    \
222         void *lptr;                                                     \
223         int s = (size);                                                 \
224         (ptr) = lptr = vmalloc(s);                                      \
225         if (lptr == NULL) {                                             \
226                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed "       \
227                        "at %s:%d\n", s, __FILE__, __LINE__);            \
228         } else {                                                        \
229                 int obd_curmem;                                         \
230                 memset(lptr, 0, s);                                     \
231                 atomic_add(s, &obd_memory);                             \
232                 obd_curmem = atomic_read(&obd_memory);                  \
233                 if (obd_curmem > obd_memmax)                            \
234                         obd_memmax = obd_curmem;                        \
235                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p "      \
236                        "(tot %d)\n", s, lptr, obd_curmem);              \
237         }                                                               \
238 } while (0)
239 #endif
240
241 #ifdef CONFIG_DEBUG_SLAB
242 #define POISON(lptr, c, s) do {} while (0)
243 #else
244 #define POISON(lptr, c, s) memset(lptr, c, s)
245 #endif
246
247 #define OBD_FREE(ptr, size)                                             \
248 do {                                                                    \
249         void *lptr = (ptr);                                             \
250         int s = (size);                                                 \
251         LASSERT(lptr);                                                  \
252         POISON(lptr, 0x5a, s);                                          \
253         kfree(lptr);                                                    \
254         atomic_sub(s, &obd_memory);                                     \
255         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
256                s, lptr, atomic_read(&obd_memory));                      \
257         (ptr) = (void *)0xdeadbeef;                                     \
258 } while (0)
259
260 #ifdef __arch_um__
261 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
262 #else
263 # define OBD_VFREE(ptr, size)                                           \
264 do {                                                                    \
265         void *lptr = (ptr);                                             \
266         int s = (size);                                                 \
267         LASSERT(lptr);                                                  \
268         POISON(lptr, 0x5a, s);                                          \
269         vfree(lptr);                                                    \
270         atomic_sub(s, &obd_memory);                                     \
271         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",     \
272                s, lptr, atomic_read(&obd_memory));                      \
273         (ptr) = (void *)0xdeadbeef;                                     \
274 } while (0)
275 #endif
276
277 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                             \
278 do {                                                                      \
279         long s = (size);                                                  \
280         void *lptr;                                                       \
281         LASSERT (!in_interrupt());                                        \
282         (ptr) = lptr = kmem_cache_alloc((slab), type);                    \
283         if (lptr == NULL) {                                               \
284                 CERROR("slab-alloc of '" #ptr "' (%ld bytes) failed "     \
285                        "at %s:%d\n", s, __FILE__, __LINE__);              \
286         } else {                                                          \
287                 int obd_curmem;                                           \
288                 memset(lptr, 0, s);                                       \
289                 atomic_add(s, &obd_memory);                               \
290                 obd_curmem = atomic_read(&obd_memory);                    \
291                 if (obd_curmem > obd_memmax)                              \
292                         obd_memmax = obd_curmem;                          \
293                 CDEBUG(D_MALLOC, "slab-alloced '" #ptr "': %ld at %p "    \
294                        "(tot %d)\n", s, lptr, obd_curmem);                \
295         }                                                                 \
296 } while (0)
297
298 #define OBD_SLAB_FREE(ptr, slab, size)                                    \
299 do {                                                                      \
300         long s = (size);                                                  \
301         void *lptr = (ptr);                                               \
302         LASSERT(lptr);                                                    \
303         POISON(lptr, 0x5a, s);                                            \
304         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %ld at %p (tot %d).\n",  \
305                s, lptr, atomic_read(&obd_memory));                        \
306         kmem_cache_free((slab), lptr);                                    \
307         atomic_sub(s, &obd_memory);                                       \
308         (ptr) = (void *)0xdeadbeef;                                       \
309 } while (0)
310
311 #endif