Whamcloud - gitweb
- add flag no_lov_create in create_spec
[fs/lustre-release.git] / lustre / include / 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 #include <libcfs/kp30.h>
27
28 /* global variables */
29 extern atomic_t obd_memory;
30 extern int obd_memmax;
31 extern unsigned int obd_fail_loc;
32 extern unsigned int obd_dump_on_timeout;
33 extern unsigned int obd_timeout;          /* seconds */
34 #define PING_INTERVAL max(obd_timeout / 4, 1U)
35 #define RECONNECT_INTERVAL max(obd_timeout / 10, 10U)
36 extern unsigned int ldlm_timeout;
37 extern unsigned int obd_health_check_timeout;
38 extern char obd_lustre_upcall[128];
39 extern cfs_waitq_t obd_race_waitq;
40 extern int obd_race_state;
41
42 #define OBD_FAIL_MDS                     0x100
43 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
44 #define OBD_FAIL_MDS_GETATTR_NET         0x102
45 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
46 #define OBD_FAIL_MDS_READPAGE_NET        0x104
47 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
48 #define OBD_FAIL_MDS_SENDPAGE            0x106
49 #define OBD_FAIL_MDS_REINT_NET           0x107
50 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
51 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
52 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
53 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
54 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
55 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
56 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
57 #define OBD_FAIL_MDS_REINT_LINK          0x10f
58 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
59 #define OBD_FAIL_MDS_REINT_RENAME        0x111
60 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
61 #define OBD_FAIL_MDS_OPEN_NET            0x113
62 #define OBD_FAIL_MDS_OPEN_PACK           0x114
63 #define OBD_FAIL_MDS_CLOSE_NET           0x115
64 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
65 #define OBD_FAIL_MDS_CONNECT_NET         0x117
66 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
67 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
68 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
69 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
70 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
71 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
72 #define OBD_FAIL_MDS_STATFS_NET          0x11e
73 #define OBD_FAIL_MDS_GETATTR_NAME_NET    0x11f
74 #define OBD_FAIL_MDS_PIN_NET             0x120
75 #define OBD_FAIL_MDS_UNPIN_NET           0x121
76 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
77 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
78 #define OBD_FAIL_MDS_SYNC_NET            0x124
79 #define OBD_FAIL_MDS_SYNC_PACK           0x125
80 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
81 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
82 #define OBD_FAIL_MDS_ALLOC_OBDO          0x128
83 #define OBD_FAIL_MDS_PAUSE_OPEN          0x129
84 #define OBD_FAIL_MDS_STATFS_LCW_SLEEP    0x12a
85 #define OBD_FAIL_MDS_OPEN_CREATE         0x12b
86 #define OBD_FAIL_MDS_OST_SETATTR         0x12c
87 #define OBD_FAIL_MDS_QUOTACHECK_NET      0x12d
88 #define OBD_FAIL_MDS_QUOTACTL_NET        0x12e
89 #define OBD_FAIL_MDS_CLIENT_ADD          0x12f
90 #define OBD_FAIL_MDS_GETXATTR_NET        0x130
91 #define OBD_FAIL_MDS_GETXATTR_PACK       0x131
92 #define OBD_FAIL_MDS_SETXATTR_NET        0x132
93 #define OBD_FAIL_MDS_SETXATTR            0x133
94 #define OBD_FAIL_MDS_SETXATTR_WRITE      0x134
95 #define OBD_FAIL_MDS_WRITEPAGE_NET       0x135
96 #define OBD_FAIL_MDS_WRITEPAGE_PACK      0x136
97
98 #define OBD_FAIL_OST                     0x200
99 #define OBD_FAIL_OST_CONNECT_NET         0x201
100 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
101 #define OBD_FAIL_OST_GET_INFO_NET        0x203
102 #define OBD_FAIL_OST_CREATE_NET          0x204
103 #define OBD_FAIL_OST_DESTROY_NET         0x205
104 #define OBD_FAIL_OST_GETATTR_NET         0x206
105 #define OBD_FAIL_OST_SETATTR_NET         0x207
106 #define OBD_FAIL_OST_OPEN_NET            0x208
107 #define OBD_FAIL_OST_CLOSE_NET           0x209
108 #define OBD_FAIL_OST_BRW_NET             0x20a
109 #define OBD_FAIL_OST_PUNCH_NET           0x20b
110 #define OBD_FAIL_OST_STATFS_NET          0x20c
111 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
112 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
113 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
114 #define OBD_FAIL_OST_SYNC_NET            0x210
115 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
116 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
117 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
118 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
119 #define OBD_FAIL_OST_ENOSPC              0x215
120 #define OBD_FAIL_OST_EROFS               0x216
121 #define OBD_FAIL_OST_ENOENT              0x217
122 #define OBD_FAIL_OST_QUOTACHECK_NET      0x218
123 #define OBD_FAIL_OST_QUOTACTL_NET        0x219
124 #define OBD_FAIL_OST_BRW_SIZE            0x21a
125
126 #define OBD_FAIL_LDLM                    0x300
127 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
128 #define OBD_FAIL_LDLM_ENQUEUE            0x302
129 #define OBD_FAIL_LDLM_CONVERT            0x303
130 #define OBD_FAIL_LDLM_CANCEL             0x304
131 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
132 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
133 #define OBD_FAIL_LDLM_GL_CALLBACK        0x307
134 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
135 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
136 #define OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
137 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED    0x30b
138 #define OBD_FAIL_LDLM_REPLY              0x30c
139 #define OBD_FAIL_LDLM_RECOV_CLIENTS      0x30d
140 #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
141
142 #define OBD_FAIL_OSC                     0x400
143 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
144 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
145 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
146 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
147 #define OBD_FAIL_OSC_MATCH               0x405
148 #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
149 #define OBD_FAIL_OSC_SHUTDOWN            0x407
150
151 #define OBD_FAIL_PTLRPC                  0x500
152 #define OBD_FAIL_PTLRPC_ACK              0x501
153 #define OBD_FAIL_PTLRPC_RQBD             0x502
154 #define OBD_FAIL_PTLRPC_BULK_GET_NET     0x503
155 #define OBD_FAIL_PTLRPC_BULK_PUT_NET     0x504
156 #define OBD_FAIL_PTLRPC_DROP_RPC         0x505
157 #define OBD_FAIL_PTLRPC_DELAY_SEND       0x506
158
159 #define OBD_FAIL_OBD_PING_NET            0x600
160 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
161 #define OBD_FAIL_OBD_LOGD_NET            0x602
162 #define OBD_FAIL_OBD_QC_CALLBACK_NET     0x603
163 #define OBD_FAIL_OBD_DQACQ               0x604
164
165 #define OBD_FAIL_TGT_REPLY_NET           0x700
166 #define OBD_FAIL_TGT_CONN_RACE           0x701
167 #define OBD_FAIL_TGT_FORCE_RECONNECT     0x702
168 #define OBD_FAIL_TGT_DELAY_CONNECT       0x703
169 #define OBD_FAIL_TGT_DELAY_RECONNECT     0x704
170
171 #define OBD_FAIL_MDC_REVALIDATE_PAUSE    0x800
172
173 #define OBD_FAIL_MGS                     0x900
174 #define OBD_FAIL_MGS_ALL_REQUEST_NET     0x901
175 #define OBD_FAIL_MGS_ALL_REPLY_NET       0x902
176 #define OBD_FAIL_MGC_PROCESS_LOG         0x903
177
178 #define OBD_FAIL_SEQ                     0x1000
179 #define OBD_FAIL_SEQ_QUERY_NET           0x1001
180
181 #define OBD_FAIL_FLD                     0x1100
182 #define OBD_FAIL_FLD_QUERY_NET           0x1101
183
184 /* preparation for a more advanced failure testbed (not functional yet) */
185 #define OBD_FAIL_MASK_SYS    0x0000FF00
186 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
187 #define OBD_FAIL_ONCE        0x80000000
188 #define OBD_FAILED           0x40000000
189
190 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
191                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
192                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
193                                 (OBD_FAILED | OBD_FAIL_ONCE)))
194
195 #define OBD_FAIL_CHECK_ONCE(id)                                              \
196 ({      int _ret_ = 0;                                                       \
197         if (OBD_FAIL_CHECK(id)) {                                            \
198                 CERROR("*** obd_fail_loc=%x ***\n", id);                     \
199                 obd_fail_loc |= OBD_FAILED;                                  \
200                 if ((id) & OBD_FAIL_ONCE)                                    \
201                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
202                 _ret_ = 1;                                                   \
203         }                                                                    \
204         _ret_;                                                               \
205 })
206
207 #define OBD_FAIL_RETURN(id, ret)                                             \
208 do {                                                                         \
209         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
210                 RETURN(ret);                                                 \
211         }                                                                    \
212 } while(0)
213
214 #define OBD_FAIL_TIMEOUT(id, secs)                                           \
215 do {                                                                         \
216         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
217                 CERROR("obd_fail_timeout id %x sleeping for %d secs\n",      \
218                        (id), (secs));                                        \
219                 set_current_state(TASK_UNINTERRUPTIBLE);                     \
220                 cfs_schedule_timeout(CFS_TASK_UNINT,                         \
221                                     cfs_time_seconds(secs));                 \
222                 set_current_state(TASK_RUNNING);                             \
223                 CERROR("obd_fail_timeout id %x awake\n", (id));              \
224        }                                                                     \
225 } while(0)
226
227 #ifdef __KERNEL__
228 /* The idea here is to synchronise two threads to force a race. The
229  * first thread that calls this with a matching fail_loc is put to
230  * sleep. The next thread that calls with the same fail_loc wakes up
231  * the first and continues. */
232 #define OBD_RACE(id)                                                           \
233 do {                                                                           \
234         if  (OBD_FAIL_CHECK_ONCE(id)) {                         \
235                 CERROR("obd_race id %x sleeping\n", (id));      \
236                 OBD_SLEEP_ON(&obd_race_waitq);        \
237                 CERROR("obd_fail_race id %x awake\n", (id));    \
238         } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) ==        \
239                     ((id) & OBD_FAIL_MASK_LOC)) {               \
240                 cfs_waitq_signal(&obd_race_waitq);              \
241         }                                                       \
242 } while(0)
243 #else
244 /* sigh.  an expedient fix until OBD_RACE is fixed up */
245 #define OBD_RACE(foo) do {} while(0)
246 #endif
247
248 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
249
250 extern atomic_t libcfs_kmemory;
251
252 #if defined(LUSTRE_UTILS) /* this version is for utils only */
253 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
254 do {                                                                          \
255         (ptr) = cfs_alloc(size, (gfp_mask));                                  \
256         if ((ptr) == NULL) {                                                  \
257                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
258                        (int)(size), __FILE__, __LINE__);                      \
259         } else {                                                              \
260                 memset(ptr, 0, size);                                         \
261                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p\n",          \
262                        (int)(size), ptr);                                     \
263         }                                                                     \
264 } while (0)
265 #else /* this version is for the kernel and liblustre */
266 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
267 do {                                                                          \
268         (ptr) = cfs_alloc(size, (gfp_mask));                                  \
269         if ((ptr) == NULL) {                                                  \
270                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
271                        (int)(size), __FILE__, __LINE__);                      \
272                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
273                        atomic_read(&obd_memory), atomic_read(&libcfs_kmemory));\
274         } else {                                                              \
275                 memset(ptr, 0, size);                                         \
276                 atomic_add(size, &obd_memory);                                \
277                 if (atomic_read(&obd_memory) > obd_memmax)                    \
278                         obd_memmax = atomic_read(&obd_memory);                \
279                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n", \
280                        (int)(size), ptr, atomic_read(&obd_memory));           \
281         }                                                                     \
282 } while (0)
283 #endif
284
285 #ifndef OBD_ALLOC_MASK
286 # define OBD_ALLOC_MASK CFS_ALLOC_IO
287 #endif
288
289 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_ALLOC_MASK)
290 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, CFS_ALLOC_STD)
291 #define OBD_ALLOC_PTR(ptr) OBD_ALLOC(ptr, sizeof *(ptr))
292 #define OBD_ALLOC_PTR_WAIT(ptr) OBD_ALLOC_WAIT(ptr, sizeof *(ptr))
293
294 #ifdef __arch_um__
295 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
296 #else
297 # define OBD_VMALLOC(ptr, size)                                               \
298 do {                                                                          \
299         (ptr) = cfs_alloc_large(size);                                        \
300         if ((ptr) == NULL) {                                                  \
301                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
302                        (int)(size), __FILE__, __LINE__);                      \
303                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
304                        atomic_read(&obd_memory), atomic_read(&libcfs_kmemory));\
305         } else {                                                              \
306                 memset(ptr, 0, size);                                         \
307                 atomic_add(size, &obd_memory);                                \
308                 if (atomic_read(&obd_memory) > obd_memmax)                    \
309                         obd_memmax = atomic_read(&obd_memory);                \
310                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n", \
311                        (int)(size), ptr, atomic_read(&obd_memory));           \
312         }                                                                     \
313 } while (0)
314 #endif
315
316 #ifdef CONFIG_DEBUG_SLAB
317 #define POISON(ptr, c, s) do {} while (0)
318 #else
319 #define POISON(ptr, c, s) memset(ptr, c, s)
320 #endif
321
322 #if POISON_BULK
323 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE);       \
324                                     kunmap(page); } while (0)
325 #else
326 #define POISON_PAGE(page, val) do { } while (0)
327 #endif
328
329 #ifdef __KERNEL__
330 #define OBD_FREE(ptr, size)                                                   \
331 do {                                                                          \
332         LASSERT(ptr);                                                         \
333         atomic_sub(size, &obd_memory);                                        \
334         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",           \
335                (int)(size), ptr, atomic_read(&obd_memory));                   \
336         POISON(ptr, 0x5a, size);                                              \
337         cfs_free(ptr);                                                        \
338         (ptr) = (void *)0xdeadbeef;                                           \
339 } while (0)
340 #else
341 #define OBD_FREE(ptr, size) ((void)(size), free((ptr)))
342 #endif
343
344 #ifdef __arch_um__
345 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
346 #else
347 # define OBD_VFREE(ptr, size)                                                 \
348 do {                                                                          \
349         LASSERT(ptr);                                                         \
350         atomic_sub(size, &obd_memory);                                        \
351         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",           \
352                (int)(size), ptr, atomic_read(&obd_memory));                   \
353         POISON(ptr, 0x5a, size);                                              \
354         cfs_free_large(ptr);                                                  \
355         (ptr) = (void *)0xdeadbeef;                                           \
356 } while (0)
357 #endif
358
359 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
360  * HAVE A CTOR THAT DOES ANYTHING.  its work will be cleared here.  we'd
361  * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
362 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                 \
363 do {                                                                          \
364         LASSERT(!in_interrupt());                                             \
365         (ptr) = cfs_mem_cache_alloc(slab, (type));                            \
366         if ((ptr) == NULL) {                                                  \
367                 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n", \
368                        (int)(size), __FILE__, __LINE__);                      \
369                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n", \
370                        atomic_read(&obd_memory), atomic_read(&libcfs_kmemory));\
371         } else {                                                              \
372                 memset(ptr, 0, size);                                         \
373                 atomic_add(size, &obd_memory);                                \
374                 if (atomic_read(&obd_memory) > obd_memmax)                    \
375                         obd_memmax = atomic_read(&obd_memory);                \
376                 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",\
377                        (int)(size), ptr, atomic_read(&obd_memory));           \
378         }                                                                     \
379 } while (0)
380
381 #define OBD_FREE_PTR(ptr) OBD_FREE(ptr, sizeof *(ptr))
382
383 #define OBD_SLAB_FREE(ptr, slab, size)                                        \
384 do {                                                                          \
385         LASSERT(ptr);                                                         \
386         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n",       \
387                (int)(size), ptr, atomic_read(&obd_memory));                   \
388         atomic_sub(size, &obd_memory);                                        \
389         POISON(ptr, 0x5a, size);                                              \
390         cfs_mem_cache_free(slab, ptr);                                        \
391         (ptr) = (void *)0xdeadbeef;                                           \
392 } while (0)
393
394 #define KEY_IS(str) \
395         (keylen == strlen(str) && memcmp(key, str, keylen) == 0)
396
397 #if defined(__linux__)
398 #include <linux/obd_support.h>
399 #elif defined(__APPLE__)
400 #include <darwin/obd_support.h>
401 #elif defined(__WINNT__)
402 #include <winnt/obd_support.h>
403 #else
404 #error Unsupported operating system.
405 #endif
406
407 #endif