Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[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 #endif
32 #include <linux/kp30.h>
33 #include <linux/lustre_compat25.h>
34
35 /* global variables */
36 extern atomic_t obd_memory;
37 extern int obd_memmax;
38 extern unsigned int obd_fail_loc;
39 extern unsigned int obd_timeout;
40 extern char obd_lustre_upcall[128];
41 extern unsigned int obd_sync_filter;
42 extern wait_queue_head_t obd_race_waitq;
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_PIN_NET             0x120
77 #define OBD_FAIL_MDS_UNPIN_NET           0x121
78 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
79 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
80 #define OBD_FAIL_MDS_SYNC_NET            0x124
81 #define OBD_FAIL_MDS_SYNC_PACK           0x125
82 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
83 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
84 #define OBD_FAIL_MDS_ALLOC_OBDO          0x128
85
86 #define OBD_FAIL_OST                     0x200
87 #define OBD_FAIL_OST_CONNECT_NET         0x201
88 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
89 #define OBD_FAIL_OST_GET_INFO_NET        0x203
90 #define OBD_FAIL_OST_CREATE_NET          0x204
91 #define OBD_FAIL_OST_DESTROY_NET         0x205
92 #define OBD_FAIL_OST_GETATTR_NET         0x206
93 #define OBD_FAIL_OST_SETATTR_NET         0x207
94 #define OBD_FAIL_OST_OPEN_NET            0x208
95 #define OBD_FAIL_OST_CLOSE_NET           0x209
96 #define OBD_FAIL_OST_BRW_NET             0x20a
97 #define OBD_FAIL_OST_PUNCH_NET           0x20b
98 #define OBD_FAIL_OST_STATFS_NET          0x20c
99 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
100 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
101 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
102 #define OBD_FAIL_OST_SYNC_NET            0x210
103 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
104 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
105 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
106 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
107
108 #define OBD_FAIL_LDLM                    0x300
109 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
110 #define OBD_FAIL_LDLM_ENQUEUE            0x302
111 #define OBD_FAIL_LDLM_CONVERT            0x303
112 #define OBD_FAIL_LDLM_CANCEL             0x304
113 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
114 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
115 #define OBD_FAIL_LDLM_GL_CALLBACK        0x307
116
117 #define OBD_FAIL_OSC                     0x400
118 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
119 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
120 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
121 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
122 #define OBD_FAIL_OSC_MATCH               0x405
123
124 #define OBD_FAIL_PTLRPC                  0x500
125 #define OBD_FAIL_PTLRPC_ACK              0x501
126 #define OBD_FAIL_PTLRPC_RQBD             0x502
127 #define OBD_FAIL_PTLRPC_BULK_GET_NET     0x503
128 #define OBD_FAIL_PTLRPC_BULK_PUT_NET     0x504
129
130 #define OBD_FAIL_OBD_PING_NET            0x600
131 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
132 #define OBD_FAIL_OBD_LOGD_NET            0x602
133
134 #define OBD_FAIL_TGT_REPLY_NET           0x700
135 #define OBD_FAIL_TGT_CONN_RACE           0x701
136
137 /* preparation for a more advanced failure testbed (not functional yet) */
138 #define OBD_FAIL_MASK_SYS    0x0000FF00
139 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
140 #define OBD_FAIL_ONCE        0x80000000
141 #define OBD_FAILED           0x40000000
142 #define OBD_FAIL_MDS_ALL_NET 0x01000000
143 #define OBD_FAIL_OST_ALL_NET 0x02000000
144
145 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
146                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
147                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
148                                 (OBD_FAILED | OBD_FAIL_ONCE)))
149
150 #define OBD_FAIL_CHECK_ONCE(id)                                              \
151 ({      int _ret_ = 0;                                                       \
152         if (OBD_FAIL_CHECK(id)) {                                            \
153                 CERROR("obd_fail_loc=%x\n", id);                             \
154                 obd_fail_loc |= OBD_FAILED;                                  \
155                 if ((id) & OBD_FAIL_ONCE)                                    \
156                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
157                 _ret_ = 1;                                                   \
158         }                                                                    \
159         _ret_;                                                               \
160 })
161
162 #define OBD_FAIL_RETURN(id, ret)                                             \
163 do {                                                                         \
164         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
165                 RETURN(ret);                                                 \
166         }                                                                    \
167 } while(0)
168
169 #define OBD_FAIL_TIMEOUT(id, secs)                                           \
170 do {                                                                         \
171         if  (OBD_FAIL_CHECK_ONCE(id)) {                                      \
172                 CERROR("obd_fail_timeout id %x sleeping for %d secs\n",     \
173                        (id), (secs));                                        \
174                 set_current_state(TASK_UNINTERRUPTIBLE);                     \
175                 schedule_timeout((secs) * HZ);                               \
176                 set_current_state(TASK_RUNNING);                             \
177                 CERROR("obd_fail_timeout id %x awake\n", (id));              \
178        }                                                                     \
179 } while(0)
180
181 #ifdef __KERNEL__
182 /* The idea here is to synchronise two threads to force a race. The
183  * first thread that calls this with a matching fail_loc is put to
184  * sleep. The next thread that calls with the same fail_loc wakes up
185  * the first and continues. */
186 #define OBD_RACE(id)                                            \
187 do {                                                            \
188         if  (OBD_FAIL_CHECK_ONCE(id)) {                         \
189                 CERROR("obd_race id %x sleeping\n", (id));      \
190                 sleep_on(&obd_race_waitq);                      \
191                 CERROR("obd_fail_race id %x awake\n", (id));    \
192         } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) ==        \
193                     ((id) & OBD_FAIL_MASK_LOC)) {               \
194                 wake_up(&obd_race_waitq);                       \
195         }                                                       \
196 } while(0)
197 #else
198 /* sigh.  an expedient fix until OBD_RACE is fixed up */
199 #define OBD_RACE(foo) LBUG()
200 #endif
201
202 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
203
204 #ifdef __KERNEL__
205 # include <linux/types.h>
206 # include <linux/blkdev.h>
207
208
209 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
210 #  define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
211 #  define ll_bdevname(SB, STORAGE) __bdevname(kdev_t_to_nr(SB->s_dev), STORAGE)
212 #  define ll_lock_kernel lock_kernel()
213 #  define ll_sbdev(SB)    ((SB)->s_bdev)
214 void dev_set_rdonly(struct block_device *, int);
215 # else
216 #  define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
217 #  define ll_sbdev(SB)    (kdev_t_to_nr((SB)->s_dev))
218 #  define ll_bdevname(SB,STORAGE) ((void)__unused_##STORAGE,bdevname(ll_sbdev(SB)))
219 #  define ll_lock_kernel
220 void dev_set_rdonly(kdev_t, int);
221 # endif
222
223 void dev_clear_rdonly(int);
224
225 static inline void OBD_FAIL_WRITE(int id, struct super_block *sb)
226 {
227         if (OBD_FAIL_CHECK(id)) {
228                 BDEVNAME_DECLARE_STORAGE(tmp);
229                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
230                        id, ll_bdevname(sb, tmp));
231                 dev_set_rdonly(ll_sbdev(sb), 2);
232                 /* We set FAIL_ONCE because we never "un-fail" a device */
233                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
234         }
235 }
236 #else /* !__KERNEL__ */
237 # define LTIME_S(time) (time)
238 /* for obd_class.h */
239 # ifndef ERR_PTR
240 #  define ERR_PTR(a) ((void *)(a))
241 # endif
242 #endif  /* __KERNEL__ */
243
244 extern atomic_t portal_kmemory;
245
246 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
247 do {                                                                          \
248         (ptr) = kmalloc(size, (gfp_mask));                                    \
249         if ((ptr) == NULL) {                                                  \
250                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
251                        (int)(size), __FILE__, __LINE__);                      \
252                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
253                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
254         } else {                                                              \
255                 memset(ptr, 0, size);                                         \
256                 atomic_add(size, &obd_memory);                                \
257                 if (atomic_read(&obd_memory) > obd_memmax)                    \
258                         obd_memmax = atomic_read(&obd_memory);                \
259                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
260                        (int)(size), ptr, atomic_read(&obd_memory));           \
261         }                                                                     \
262 } while (0)
263
264 #ifndef OBD_GFP_MASK
265 # define OBD_GFP_MASK GFP_NOFS
266 #endif
267
268 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
269 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, GFP_KERNEL)
270
271 #ifdef __arch_um__
272 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
273 #else
274 # define OBD_VMALLOC(ptr, size)                                               \
275 do {                                                                          \
276         (ptr) = vmalloc(size);                                                \
277         if ((ptr) == NULL) {                                                  \
278                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
279                        (int)(size), __FILE__, __LINE__);                      \
280                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
281                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
282         } else {                                                              \
283                 memset(ptr, 0, size);                                         \
284                 atomic_add(size, &obd_memory);                                \
285                 if (atomic_read(&obd_memory) > obd_memmax)                    \
286                         obd_memmax = atomic_read(&obd_memory);                \
287                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
288                        (int)(size), ptr, atomic_read(&obd_memory));           \
289         }                                                                     \
290 } while (0)
291 #endif
292
293 #ifdef CONFIG_DEBUG_SLAB
294 #define POISON(ptr, c, s) do {} while (0)
295 #else
296 #define POISON(ptr, c, s) memset(ptr, c, s)
297 #endif
298
299 #if POISON_BULK
300 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE);       \
301                                     kunmap(page); } while (0)
302 #else
303 #define POISON_PAGE(page, val) do { } while (0)
304 #endif
305
306 #define OBD_FREE(ptr, size)                                                   \
307 do {                                                                          \
308         LASSERT(ptr);                                                         \
309         atomic_sub(size, &obd_memory);                                        \
310         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",           \
311                (int)(size), ptr, atomic_read(&obd_memory));                   \
312         POISON(ptr, 0x5a, size);                                              \
313         kfree(ptr);                                                           \
314         (ptr) = (void *)0xdeadbeef;                                           \
315 } while (0)
316
317 #ifdef __arch_um__
318 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
319 #else
320 # define OBD_VFREE(ptr, size)                                                 \
321 do {                                                                          \
322         LASSERT(ptr);                                                         \
323         atomic_sub(size, &obd_memory);                                        \
324         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",           \
325                (int)(size), ptr, atomic_read(&obd_memory));                   \
326         POISON(ptr, 0x5a, size);                                              \
327         vfree(ptr);                                                           \
328         (ptr) = (void *)0xdeadbeef;                                           \
329 } while (0)
330 #endif
331
332 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
333  * HAVE A CTOR THAT DOES ANYTHING.  its work will be cleared here.  we'd
334  * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
335 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                 \
336 do {                                                                          \
337         LASSERT(!in_interrupt());                                             \
338         (ptr) = kmem_cache_alloc(slab, (type));                               \
339         if ((ptr) == NULL) {                                                  \
340                 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
341                        (int)(size), __FILE__, __LINE__);                      \
342                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
343                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
344         } else {                                                              \
345                 memset(ptr, 0, size);                                         \
346                 atomic_add(size, &obd_memory);                                \
347                 if (atomic_read(&obd_memory) > obd_memmax)                    \
348                         obd_memmax = atomic_read(&obd_memory);                \
349                 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",\
350                        (int)(size), ptr, atomic_read(&obd_memory));           \
351         }                                                                     \
352 } while (0)
353
354 #define OBD_SLAB_FREE(ptr, slab, size)                                        \
355 do {                                                                          \
356         LASSERT(ptr);                                                         \
357         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n",       \
358                (int)(size), ptr, atomic_read(&obd_memory));                   \
359         atomic_sub(size, &obd_memory);                                        \
360         POISON(ptr, 0x5a, size);                                              \
361         kmem_cache_free(slab, ptr);                                           \
362         (ptr) = (void *)0xdeadbeef;                                           \
363 } while (0)
364
365 #endif