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