Whamcloud - gitweb
land lustre part of b_hd_sec on 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 _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 <libcfs/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_dump_on_timeout;
40 extern unsigned int obd_timeout;
41 extern unsigned int ldlm_timeout;
42 extern char obd_lustre_upcall[128];
43 extern unsigned int obd_sync_filter;
44 extern wait_queue_head_t obd_race_waitq;
45
46 #define OBD_FAIL_MDS                     0x100
47 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
48 #define OBD_FAIL_MDS_GETATTR_NET         0x102
49 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
50 #define OBD_FAIL_MDS_READPAGE_NET        0x104
51 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
52 #define OBD_FAIL_MDS_SENDPAGE            0x106
53 #define OBD_FAIL_MDS_REINT_NET           0x107
54 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
55 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
56 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
57 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
58 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
59 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
60 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
61 #define OBD_FAIL_MDS_REINT_LINK          0x10f
62 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
63 #define OBD_FAIL_MDS_REINT_RENAME        0x111
64 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
65 #define OBD_FAIL_MDS_OPEN_NET            0x113
66 #define OBD_FAIL_MDS_OPEN_PACK           0x114
67 #define OBD_FAIL_MDS_CLOSE_NET           0x115
68 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
69 #define OBD_FAIL_MDS_CONNECT_NET         0x117
70 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
71 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
72 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
73 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
74 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
75 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
76 #define OBD_FAIL_MDS_STATFS_NET          0x11e
77 #define OBD_FAIL_MDS_GETATTR_LOCK_NET    0x11f
78 #define OBD_FAIL_MDS_PIN_NET             0x120
79 #define OBD_FAIL_MDS_UNPIN_NET           0x121
80 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
81 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
82 #define OBD_FAIL_MDS_SYNC_NET            0x124
83 #define OBD_FAIL_MDS_SYNC_PACK           0x125
84 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
85 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
86 #define OBD_FAIL_MDS_ALLOC_OBDO          0x128
87
88 #define OBD_FAIL_OST                     0x200
89 #define OBD_FAIL_OST_CONNECT_NET         0x201
90 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
91 #define OBD_FAIL_OST_GET_INFO_NET        0x203
92 #define OBD_FAIL_OST_CREATE_NET          0x204
93 #define OBD_FAIL_OST_DESTROY_NET         0x205
94 #define OBD_FAIL_OST_GETATTR_NET         0x206
95 #define OBD_FAIL_OST_SETATTR_NET         0x207
96 #define OBD_FAIL_OST_OPEN_NET            0x208
97 #define OBD_FAIL_OST_CLOSE_NET           0x209
98 #define OBD_FAIL_OST_BRW_NET             0x20a
99 #define OBD_FAIL_OST_PUNCH_NET           0x20b
100 #define OBD_FAIL_OST_STATFS_NET          0x20c
101 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
102 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
103 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
104 #define OBD_FAIL_OST_SYNC_NET            0x210
105 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
106 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
107 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
108 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
109 #define OBD_FAIL_OST_ENOSPC              0x215
110 #define OBD_FAIL_OST_EROFS               0x216
111
112
113 #define OBD_FAIL_LDLM                    0x300
114 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
115 #define OBD_FAIL_LDLM_ENQUEUE            0x302
116 #define OBD_FAIL_LDLM_CONVERT            0x303
117 #define OBD_FAIL_LDLM_CANCEL             0x304
118 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
119 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
120 #define OBD_FAIL_LDLM_GL_CALLBACK        0x307
121 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
122 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
123 #define OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
124 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED    0x30b
125 #define OBD_FAIL_LDLM_REPLY              0x30c
126
127
128 #define OBD_FAIL_OSC                     0x400
129 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
130 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
131 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
132 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
133 #define OBD_FAIL_OSC_MATCH               0x405
134
135 #define OBD_FAIL_PTLRPC                  0x500
136 #define OBD_FAIL_PTLRPC_ACK              0x501
137 #define OBD_FAIL_PTLRPC_RQBD             0x502
138 #define OBD_FAIL_PTLRPC_BULK_GET_NET     0x503
139 #define OBD_FAIL_PTLRPC_BULK_PUT_NET     0x504
140
141 #define OBD_FAIL_OBD_PING_NET            0x600
142 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
143 #define OBD_FAIL_OBD_LOGD_NET            0x602
144
145 #define OBD_FAIL_TGT_REPLY_NET           0x700
146 #define OBD_FAIL_TGT_CONN_RACE           0x701
147
148 #define OBD_FAIL_SVCSEC_ACCEPT_BEG       0x750
149 #define OBD_FAIL_SVCSEC_ACCEPT_END       0x751
150 #define OBD_FAIL_SVCSEC_WRAP_BEG         0x752
151 #define OBD_FAIL_SVCSEC_WRAP_END         0x753
152 #define OBD_FAIL_SVCGSS_ERR_NOTIFY       0x760
153 #define OBD_FAIL_SVCGSS_INIT_REQ         0x780
154 #define OBD_FAIL_SVCGSS_INIT_REP         0x781
155
156 /* preparation for a more advanced failure testbed (not functional yet) */
157 #define OBD_FAIL_MASK_SYS    0x0000FF00
158 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
159 #define OBD_FAIL_ONCE        0x80000000
160 #define OBD_FAILED           0x40000000
161 #define OBD_FAIL_MDS_ALL_NET 0x01000000
162 #define OBD_FAIL_OST_ALL_NET 0x02000000
163
164 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
165                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
166                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
167                                 (OBD_FAILED | OBD_FAIL_ONCE)))
168
169 #define OBD_FAIL_CHECK_ONCE(id)                                              \
170 ({      int _ret_ = 0;                                                       \
171         if (OBD_FAIL_CHECK(id)) {                                            \
172                 CERROR("obd_fail_loc=%x\n", id);                             \
173                 obd_fail_loc |= OBD_FAILED;                                  \
174                 if ((id) & OBD_FAIL_ONCE)                                    \
175                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
176                 _ret_ = 1;                                                   \
177         }                                                                    \
178         _ret_;                                                               \
179 })
180
181 #define OBD_FAIL_RETURN(id, ret)                                             \
182 do {                                                                         \
183         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
184                 RETURN(ret);                                                 \
185         }                                                                    \
186 } while(0)
187
188 #define OBD_FAIL_TIMEOUT(id, secs)                                           \
189 do {                                                                         \
190         if  (OBD_FAIL_CHECK_ONCE(id)) {                                      \
191                CERROR("obd_fail_timeout id %x sleeping for %d secs\n",       \
192                         (id), (secs));                                       \
193                 set_current_state(TASK_UNINTERRUPTIBLE);                     \
194                 schedule_timeout((secs) * HZ);                               \
195                 set_current_state(TASK_RUNNING);                             \
196                 CERROR("obd_fail_timeout id %x awake\n", (id));              \
197        }                                                                     \
198 } while(0)
199
200 #ifdef __KERNEL__
201 /* The idea here is to synchronise two threads to force a race. The
202  * first thread that calls this with a matching fail_loc is put to
203  * sleep. The next thread that calls with the same fail_loc wakes up
204  * the first and continues. */
205 #define OBD_RACE(id)                                            \
206 do {                                                            \
207         if  (OBD_FAIL_CHECK_ONCE(id)) {                         \
208                 CERROR("obd_race id %x sleeping\n", (id));      \
209                 sleep_on(&obd_race_waitq);                      \
210                 CERROR("obd_fail_race id %x awake\n", (id));    \
211         } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) ==        \
212                     ((id) & OBD_FAIL_MASK_LOC)) {               \
213                 wake_up(&obd_race_waitq);                       \
214         }                                                       \
215 } while(0)
216 #else
217 /* sigh.  an expedient fix until OBD_RACE is fixed up */
218 #define OBD_RACE(foo) LBUG()
219 #endif
220
221 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
222
223 #ifdef __KERNEL__
224 # include <linux/types.h>
225 # include <linux/blkdev.h>
226
227
228 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
229 #  define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
230 #  define ll_bdevname(SB, STORAGE) __bdevname(kdev_t_to_nr(SB->s_dev), STORAGE)
231 #  define ll_lock_kernel lock_kernel()
232 #  define ll_sbdev(SB)    ((SB)->s_bdev)
233 void dev_set_rdonly(struct block_device *, int);
234 # else
235 #  define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
236 #  define ll_sbdev(SB)    (kdev_t_to_nr((SB)->s_dev))
237 #  define ll_bdevname(SB,STORAGE) ((void)__unused_##STORAGE,bdevname(ll_sbdev(SB)))
238 #  define ll_lock_kernel
239 void dev_set_rdonly(kdev_t, int);
240 # endif
241
242 void dev_clear_rdonly(int);
243
244 static inline void OBD_FAIL_WRITE(int id, struct super_block *sb)
245 {
246         if (OBD_FAIL_CHECK(id)) {
247                 BDEVNAME_DECLARE_STORAGE(tmp);
248                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
249                        id, ll_bdevname(sb, tmp));
250                 dev_set_rdonly(ll_sbdev(sb), 2);
251                 /* We set FAIL_ONCE because we never "un-fail" a device */
252                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
253         }
254 }
255 #else /* !__KERNEL__ */
256 # define LTIME_S(time) (time)
257 /* for obd_class.h */
258 # ifndef ERR_PTR
259 #  define ERR_PTR(a) ((void *)(a))
260 # endif
261 #endif  /* __KERNEL__ */
262
263 extern atomic_t portal_kmemory;
264
265 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
266 do {                                                                          \
267         (ptr) = kmalloc(size, (gfp_mask));                                    \
268         if ((ptr) == NULL) {                                                  \
269                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
270                        (int)(size), __FILE__, __LINE__);                      \
271                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
272                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
273         } else {                                                              \
274                 memset(ptr, 0, size);                                         \
275                 atomic_add(size, &obd_memory);                                \
276                 if (atomic_read(&obd_memory) > obd_memmax)                    \
277                         obd_memmax = atomic_read(&obd_memory);                \
278                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
279                        (int)(size), ptr, atomic_read(&obd_memory));           \
280         }                                                                     \
281 } while (0)
282
283 #ifndef OBD_GFP_MASK
284 # define OBD_GFP_MASK GFP_NOFS
285 #endif
286
287 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
288 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, GFP_KERNEL)
289
290 #ifdef __arch_um__
291 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
292 #else
293 # define OBD_VMALLOC(ptr, size)                                               \
294 do {                                                                          \
295         (ptr) = vmalloc(size);                                                \
296         if ((ptr) == NULL) {                                                  \
297                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
298                        (int)(size), __FILE__, __LINE__);                      \
299                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
300                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
301         } else {                                                              \
302                 memset(ptr, 0, size);                                         \
303                 atomic_add(size, &obd_memory);                                \
304                 if (atomic_read(&obd_memory) > obd_memmax)                    \
305                         obd_memmax = atomic_read(&obd_memory);                \
306                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
307                        (int)(size), ptr, atomic_read(&obd_memory));           \
308         }                                                                     \
309 } while (0)
310 #endif
311
312 #ifdef CONFIG_DEBUG_SLAB
313 #define POISON(ptr, c, s) do {} while (0)
314 #else
315 #define POISON(ptr, c, s) memset(ptr, c, s)
316 #endif
317
318 #if POISON_BULK
319 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE);       \
320                                     kunmap(page); } while (0)
321 #else
322 #define POISON_PAGE(page, val) do { } while (0)
323 #endif
324
325 #define OBD_FREE(ptr, size)                                                   \
326 do {                                                                          \
327         LASSERT(ptr);                                                         \
328         atomic_sub(size, &obd_memory);                                        \
329         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",           \
330                (int)(size), ptr, atomic_read(&obd_memory));                   \
331         POISON(ptr, 0x5a, size);                                              \
332         kfree(ptr);                                                           \
333         (ptr) = (void *)0xdeadbeef;                                           \
334 } while (0)
335
336 #ifdef __arch_um__
337 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
338 #else
339 # define OBD_VFREE(ptr, size)                                                 \
340 do {                                                                          \
341         LASSERT(ptr);                                                         \
342         atomic_sub(size, &obd_memory);                                        \
343         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",           \
344                (int)(size), ptr, atomic_read(&obd_memory));                   \
345         POISON(ptr, 0x5a, size);                                              \
346         vfree(ptr);                                                           \
347         (ptr) = (void *)0xdeadbeef;                                           \
348 } while (0)
349 #endif
350
351 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
352  * HAVE A CTOR THAT DOES ANYTHING.  its work will be cleared here.  we'd
353  * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
354 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                 \
355 do {                                                                          \
356         LASSERT(!in_interrupt());                                             \
357         (ptr) = kmem_cache_alloc(slab, (type));                               \
358         if ((ptr) == NULL) {                                                  \
359                 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
360                        (int)(size), __FILE__, __LINE__);                      \
361                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
362                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));\
363         } else {                                                              \
364                 memset(ptr, 0, size);                                         \
365                 atomic_add(size, &obd_memory);                                \
366                 if (atomic_read(&obd_memory) > obd_memmax)                    \
367                         obd_memmax = atomic_read(&obd_memory);                \
368                 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",\
369                        (int)(size), ptr, atomic_read(&obd_memory));           \
370         }                                                                     \
371 } while (0)
372
373 #define OBD_SLAB_FREE(ptr, slab, size)                                        \
374 do {                                                                          \
375         LASSERT(ptr);                                                         \
376         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n",       \
377                (int)(size), ptr, atomic_read(&obd_memory));                   \
378         atomic_sub(size, &obd_memory);                                        \
379         POISON(ptr, 0x5a, size);                                              \
380         kmem_cache_free(slab, ptr);                                           \
381         (ptr) = (void *)0xdeadbeef;                                           \
382 } while (0)
383
384 #endif