Whamcloud - gitweb
branch: HEAD
[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/libcfs.h>
27 #include <lvfs.h>
28 #include <lprocfs_status.h>
29
30 #if defined(__linux__)
31 #include <linux/obd_support.h>
32 #elif defined(__APPLE__)
33 #include <darwin/obd_support.h>
34 #elif defined(__WINNT__)
35 #include <winnt/obd_support.h>
36 #else
37 #error Unsupported operating system.
38 #endif
39
40 /* global variables */
41 extern struct lprocfs_stats *obd_memory;
42 enum {
43         OBD_MEMORY_STAT = 0,
44         OBD_MEMORY_PAGES_STAT = 1,
45         OBD_STATS_NUM,
46 };
47
48 enum {
49         OBD_FAIL_LOC_NOSET      = 0,
50         OBD_FAIL_LOC_ORSET      = 1,
51         OBD_FAIL_LOC_RESET      = 2
52 };
53
54 extern unsigned long obd_fail_loc;
55 extern unsigned int obd_fail_val;
56 extern unsigned int obd_debug_peer_on_timeout;
57 extern unsigned int obd_dump_on_timeout;
58 extern unsigned int obd_dump_on_eviction;
59 /* obd_timeout should only be used for recovery, not for
60    networking / disk / timings affected by load (use Adaptive Timeouts) */
61 extern unsigned int obd_timeout;          /* seconds */
62 extern unsigned int ldlm_timeout;         /* seconds */
63 extern unsigned int obd_sync_filter;
64 extern unsigned int obd_max_dirty_pages;
65 extern atomic_t obd_dirty_pages;
66 extern cfs_waitq_t obd_race_waitq;
67 extern int obd_race_state;
68 extern unsigned int obd_alloc_fail_rate;
69
70 int __obd_fail_check_set(__u32 id, __u32 value, int set);
71 int __obd_fail_timeout_set(__u32 id, __u32 value, int ms, int set);
72
73 /* lvfs.c */
74 int obd_alloc_fail(const void *ptr, const char *name, const char *type,
75                    size_t size, const char *file, int line);
76
77 /* Timeout definitions */
78 #define OBD_TIMEOUT_DEFAULT 100
79 #define LDLM_TIMEOUT_DEFAULT 20
80 /* Time to wait for all clients to reconnect during recovery */
81 /* Should be very conservative; must catch the first reconnect after reboot */
82 #define OBD_RECOVERY_FACTOR (3) /* times obd_timeout */
83 /* Change recovery-small 26b time if you change this */
84 #define PING_INTERVAL max(obd_timeout / 4, 1U)
85 /* Client may skip 1 ping; we must wait at least 2.5. But for multiple
86  * failover targets the client only pings one server at a time, and pings
87  * can be lost on a loaded network. Since eviction has serious consequences,
88  * and there's no urgent need to evict a client just because it's idle, we
89  * should be very conservative here. */
90 #define PING_EVICT_TIMEOUT (PING_INTERVAL * 6)
91 #define DISK_TIMEOUT 50          /* Beyond this we warn about disk speed */
92 #define CONNECTION_SWITCH_MIN 5U /* Connection switching rate limiter */
93  /* Max connect interval for nonresponsive servers; ~50s to avoid building up
94     connect requests in the LND queues, but within obd_timeout so we don't
95     miss the recovery window */
96 #define CONNECTION_SWITCH_MAX min(50U, max(CONNECTION_SWITCH_MIN,obd_timeout))
97 #define CONNECTION_SWITCH_INC 5  /* Connection timeout backoff */
98 #ifndef CRAY_XT3
99 /* In general this should be low to have quick detection of a system
100    running on a backup server. (If it's too low, import_select_connection
101    will increase the timeout anyhow.)  */
102 #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/20)
103 #else
104 /* ...but for very large systems (e.g. CRAY) we need to keep the initial
105    connect t.o. high (bz 10803), because they will nearly ALWAYS be doing the
106    connects for the first time (clients "reboot" after every process, so no
107    chance to generate adaptive timeout data. */
108 #define INITIAL_CONNECT_TIMEOUT max(CONNECTION_SWITCH_MIN,obd_timeout/2)
109 #endif
110 #define LONG_UNLINK 300          /* Unlink should happen before now */
111
112
113 #define OBD_FAIL_MDS                     0x100
114 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
115 #define OBD_FAIL_MDS_GETATTR_NET         0x102
116 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
117 #define OBD_FAIL_MDS_READPAGE_NET        0x104
118 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
119 #define OBD_FAIL_MDS_SENDPAGE            0x106
120 #define OBD_FAIL_MDS_REINT_NET           0x107
121 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
122 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
123 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
124 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
125 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
126 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
127 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
128 #define OBD_FAIL_MDS_REINT_LINK          0x10f
129 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
130 #define OBD_FAIL_MDS_REINT_RENAME        0x111
131 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
132 #define OBD_FAIL_MDS_OPEN_NET            0x113
133 #define OBD_FAIL_MDS_OPEN_PACK           0x114
134 #define OBD_FAIL_MDS_CLOSE_NET           0x115
135 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
136 #define OBD_FAIL_MDS_CONNECT_NET         0x117
137 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
138 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
139 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
140 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
141 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
142 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
143 #define OBD_FAIL_MDS_STATFS_NET          0x11e
144 #define OBD_FAIL_MDS_GETATTR_NAME_NET    0x11f
145 #define OBD_FAIL_MDS_PIN_NET             0x120
146 #define OBD_FAIL_MDS_UNPIN_NET           0x121
147 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
148 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
149 #define OBD_FAIL_MDS_SYNC_NET            0x124
150 #define OBD_FAIL_MDS_SYNC_PACK           0x125
151 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
152 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
153 #define OBD_FAIL_MDS_ALLOC_OBDO          0x128
154 #define OBD_FAIL_MDS_PAUSE_OPEN          0x129
155 #define OBD_FAIL_MDS_STATFS_LCW_SLEEP    0x12a
156 #define OBD_FAIL_MDS_OPEN_CREATE         0x12b
157 #define OBD_FAIL_MDS_OST_SETATTR         0x12c
158 #define OBD_FAIL_MDS_QUOTACHECK_NET      0x12d
159 #define OBD_FAIL_MDS_QUOTACTL_NET        0x12e
160 #define OBD_FAIL_MDS_CLIENT_ADD          0x12f
161 #define OBD_FAIL_MDS_GETXATTR_NET        0x130
162 #define OBD_FAIL_MDS_GETXATTR_PACK       0x131
163 #define OBD_FAIL_MDS_SETXATTR_NET        0x132
164 #define OBD_FAIL_MDS_SETXATTR            0x133
165 #define OBD_FAIL_MDS_SETXATTR_WRITE      0x134
166 #define OBD_FAIL_MDS_FS_SETUP            0x135
167 #define OBD_FAIL_MDS_RESEND              0x136
168 #define OBD_FAIL_MDS_IS_SUBDIR_NET       0x137
169 #define OBD_FAIL_MDS_IS_SUBDIR_PACK      0x138
170 #define OBD_FAIL_MDS_SET_INFO_NET        0x139
171 #define OBD_FAIL_MDS_WRITEPAGE_NET       0x13a
172 #define OBD_FAIL_MDS_WRITEPAGE_PACK      0x13b
173 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x13c
174 #define OBD_FAIL_MDS_OSC_PRECREATE       0x13d
175 #define OBD_FAIL_MDS_LOV_SYNC_RACE       0x13e
176 #define OBD_FAIL_MDS_CLOSE_NET_REP       0x13f
177 #define OBD_FAIL_MDS_LLOG_SYNC_TIMEOUT   0x140
178
179 #define OBD_FAIL_OST                     0x200
180 #define OBD_FAIL_OST_CONNECT_NET         0x201
181 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
182 #define OBD_FAIL_OST_GET_INFO_NET        0x203
183 #define OBD_FAIL_OST_CREATE_NET          0x204
184 #define OBD_FAIL_OST_DESTROY_NET         0x205
185 #define OBD_FAIL_OST_GETATTR_NET         0x206
186 #define OBD_FAIL_OST_SETATTR_NET         0x207
187 #define OBD_FAIL_OST_OPEN_NET            0x208
188 #define OBD_FAIL_OST_CLOSE_NET           0x209
189 #define OBD_FAIL_OST_BRW_NET             0x20a
190 #define OBD_FAIL_OST_PUNCH_NET           0x20b
191 #define OBD_FAIL_OST_STATFS_NET          0x20c
192 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
193 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
194 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
195 #define OBD_FAIL_OST_SYNC_NET            0x210
196 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
197 #define OBD_FAIL_OST_ALL_REQUEST_NET     0x212
198 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
199 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
200 #define OBD_FAIL_OST_ENOSPC              0x215
201 #define OBD_FAIL_OST_EROFS               0x216
202 #define OBD_FAIL_OST_ENOENT              0x217
203 #define OBD_FAIL_OST_QUOTACHECK_NET      0x218
204 #define OBD_FAIL_OST_QUOTACTL_NET        0x219
205 #define OBD_FAIL_OST_CHECKSUM_RECEIVE    0x21a
206 #define OBD_FAIL_OST_CHECKSUM_SEND       0x21b
207 #define OBD_FAIL_OST_BRW_SIZE            0x21c
208 #define OBD_FAIL_OST_DROP_REQ            0x21d
209 #define OBD_FAIL_OST_SETATTR_CREDITS     0x21e
210 #define OBD_FAIL_OST_HOLD_WRITE_RPC      0x21f
211 #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
212 #define OBD_FAIL_OST_LLOG_RECOVERY_TIMEOUT 0x221
213 #define OBD_FAIL_OST_CANCEL_COOKIE_TIMEOUT 0x222
214 #define OBD_FAIL_OST_PAUSE_CREATE        0x223
215 #define OBD_FAIL_OST_BRW_PAUSE_PACK      0x224
216 #define OBD_FAIL_OST_CONNECT_NET2        0x225
217
218 #define OBD_FAIL_LDLM                    0x300
219 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
220 #define OBD_FAIL_LDLM_ENQUEUE            0x302
221 #define OBD_FAIL_LDLM_CONVERT            0x303
222 #define OBD_FAIL_LDLM_CANCEL             0x304
223 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
224 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
225 #define OBD_FAIL_LDLM_GL_CALLBACK        0x307
226 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
227 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
228 #define OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
229 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED    0x30b
230 #define OBD_FAIL_LDLM_REPLY              0x30c
231 #define OBD_FAIL_LDLM_RECOV_CLIENTS      0x30d
232 #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
233 #define OBD_FAIL_LDLM_GLIMPSE            0x30f
234 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
235 #define OBD_FAIL_LDLM_CANCEL_EVICT_RACE  0x311
236 #define OBD_FAIL_LDLM_PAUSE_CANCEL       0x312
237 #define OBD_FAIL_LDLM_CLOSE_THREAD       0x313
238 #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE  0x314
239
240 #define OBD_FAIL_OSC                     0x400
241 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
242 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
243 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
244 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
245 #define OBD_FAIL_OSC_MATCH               0x405
246 #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
247 #define OBD_FAIL_OSC_SHUTDOWN            0x407
248 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
249 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
250 #define OBD_FAIL_OSC_BRW_PREP_REQ2       0x40a
251 #define OBD_FAIL_OSC_CONNECT_CKSUM       0x40b
252 #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
253 #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
254
255 #define OBD_FAIL_PTLRPC                  0x500
256 #define OBD_FAIL_PTLRPC_ACK              0x501
257 #define OBD_FAIL_PTLRPC_RQBD             0x502
258 #define OBD_FAIL_PTLRPC_BULK_GET_NET     0x503
259 #define OBD_FAIL_PTLRPC_BULK_PUT_NET     0x504
260 #define OBD_FAIL_PTLRPC_DROP_RPC         0x505
261 #define OBD_FAIL_PTLRPC_DELAY_SEND       0x506
262 #define OBD_FAIL_PTLRPC_DELAY_RECOV      0x507
263 #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
264 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
265 #define OBD_FAIL_PTLRPC_PAUSE_REP        0x50c
266
267 #define OBD_FAIL_OBD_PING_NET            0x600
268 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
269 #define OBD_FAIL_OBD_LOGD_NET            0x602
270 #define OBD_FAIL_OBD_QC_CALLBACK_NET     0x603
271 #define OBD_FAIL_OBD_DQACQ               0x604
272
273 #define OBD_FAIL_TGT_REPLY_NET           0x700
274 #define OBD_FAIL_TGT_CONN_RACE           0x701
275 #define OBD_FAIL_TGT_FORCE_RECONNECT     0x702
276 #define OBD_FAIL_TGT_DELAY_CONNECT       0x703
277 #define OBD_FAIL_TGT_DELAY_RECONNECT     0x704
278 #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
279 #define OBD_FAIL_TGT_TOOMANY_THREADS     0x706
280 #define OBD_FAIL_TGT_REPLAY_DROP         0x707
281
282 #define OBD_FAIL_MDC_REVALIDATE_PAUSE    0x800
283 #define OBD_FAIL_MDC_ENQUEUE_PAUSE       0x801
284 #define OBD_FAIL_MDC_OLD_EXT_FLAGS       0x802
285 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
286
287 #define OBD_FAIL_MGS                     0x900
288 #define OBD_FAIL_MGS_ALL_REQUEST_NET     0x901
289 #define OBD_FAIL_MGS_ALL_REPLY_NET       0x902
290 #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
291 #define OBD_FAIL_MGS_PAUSE_REQ           0x904
292 #define OBD_FAIL_MGS_PAUSE_TARGET_REG    0x905
293
294 #define OBD_FAIL_QUOTA_QD_COUNT_32BIT    0xA00
295
296 #define OBD_FAIL_LPROC_REMOVE            0xB00
297
298 #define OBD_FAIL_GENERAL_ALLOC           0xC00
299
300 #define OBD_FAIL_SEQ                     0x1000
301 #define OBD_FAIL_SEQ_QUERY_NET           0x1001
302
303 #define OBD_FAIL_FLD                     0x1100
304 #define OBD_FAIL_FLD_QUERY_NET           0x1101
305
306 #define OBD_FAIL_SEC_CTX                 0x1200
307 #define OBD_FAIL_SEC_CTX_INIT_NET        0x1201
308 #define OBD_FAIL_SEC_CTX_INIT_CONT_NET   0x1202
309 #define OBD_FAIL_SEC_CTX_FINI_NET        0x1203
310 #define OBD_FAIL_SEC_CTX_HDL_PAUSE       0x1204
311
312 /* Failure injection control */
313 #define OBD_FAIL_MASK_SYS    0x0000FF00
314 #define OBD_FAIL_MASK_LOC   (0x000000FF | OBD_FAIL_MASK_SYS)
315
316 #define OBD_FAILED_BIT       30
317 /* OBD_FAILED is 0x40000000 */
318 #define OBD_FAILED          (1 << OBD_FAILED_BIT)
319
320 #define OBD_FAIL_ONCE_BIT    31
321 /* OBD_FAIL_ONCE is 0x80000000 */
322 #define OBD_FAIL_ONCE       (1 << OBD_FAIL_ONCE_BIT)
323
324 /* The following flags aren't made to be combined */
325 #define OBD_FAIL_SKIP        0x20000000 /* skip N times then fail */
326 #define OBD_FAIL_SOME        0x10000000 /* only fail N times */
327 #define OBD_FAIL_RAND        0x08000000 /* fail 1/N of the times */
328 #define OBD_FAIL_USR1        0x04000000 /* user flag */
329
330 #define OBD_FAIL_PRECHECK(id) (obd_fail_loc &&                                \
331                               (obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
332                               ((id) & OBD_FAIL_MASK_LOC))
333
334 static inline int obd_fail_check_set(__u32 id, __u32 value, int set)
335 {
336         int ret = 0;
337         if (unlikely(OBD_FAIL_PRECHECK(id) &&
338             (ret = __obd_fail_check_set(id, value, set)))) {
339                 CERROR("*** obd_fail_loc=%x ***\n", id);
340         }
341         return ret;
342 }
343
344 /* If id hit obd_fail_loc, return 1, otherwise return 0 */
345 #define OBD_FAIL_CHECK(id) \
346         obd_fail_check_set(id, 0, OBD_FAIL_LOC_NOSET)
347
348 /* If id hit obd_fail_loc, obd_fail_loc |= value and return 1,
349  * otherwise return 0 */
350 #define OBD_FAIL_CHECK_ORSET(id, value) \
351         obd_fail_check_set(id, value, OBD_FAIL_LOC_ORSET)
352
353 /* If id hit obd_fail_loc, obd_fail_loc = value and return 1,
354  * otherwise return 0 */
355 #define OBD_FAIL_CHECK_RESET(id, value) \
356         obd_fail_check_set(id, value, OBD_FAIL_LOC_RESET)
357
358
359 static inline int obd_fail_timeout_set(__u32 id, __u32 value, int ms, int set)
360 {
361         if (unlikely(OBD_FAIL_PRECHECK(id)))
362                 return __obd_fail_timeout_set(id, value, ms, set);
363         else
364                 return 0;
365 }
366
367 /* If id hit obd_fail_loc, sleep for seconds or milliseconds */
368 #define OBD_FAIL_TIMEOUT(id, secs) \
369         obd_fail_timeout_set(id, 0, secs * 1000, OBD_FAIL_LOC_NOSET)
370
371 #define OBD_FAIL_TIMEOUT_MS(id, ms) \
372         obd_fail_timeout_set(id, 0, ms, OBD_FAIL_LOC_NOSET)
373
374 /* If id hit obd_fail_loc, obd_fail_loc |= value and
375  * sleep seconds or milliseconds */
376 #define OBD_FAIL_TIMEOUT_ORSET(id, value, secs) \
377         obd_fail_timeout_set(id, value, secs * 1000, OBD_FAIL_LOC_ORSET)
378
379 #define OBD_FAIL_TIMEOUT_MS_ORSET(id, value, ms) \
380         obd_fail_timeout_set(id, value, ms, OBD_FAIL_LOC_ORSET)
381
382 #ifdef __KERNEL__
383 static inline void obd_fail_write(int id, struct super_block *sb)
384 {
385         /* We set FAIL_ONCE because we never "un-fail" a device */
386         if (OBD_FAIL_CHECK_ORSET(id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
387 #ifdef LIBCFS_DEBUG
388                 BDEVNAME_DECLARE_STORAGE(tmp);
389                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
390                        id, ll_bdevname(sb, tmp));
391 #endif
392                 /* TODO-CMD: fix getting jdev */
393                 __lvfs_set_rdonly(lvfs_sbdev(sb), (lvfs_sbdev_type)0);
394         }
395 }
396 #define OBD_FAIL_WRITE(id, sb) obd_fail_write(id, sb)
397
398 /* The idea here is to synchronise two threads to force a race. The
399  * first thread that calls this with a matching fail_loc is put to
400  * sleep. The next thread that calls with the same fail_loc wakes up
401  * the first and continues. */
402 static inline void obd_race(__u32 id)
403 {
404         if (OBD_FAIL_PRECHECK(id)) {
405                 if (unlikely(__obd_fail_check_set(id, 0, OBD_FAIL_LOC_NOSET))) {
406                         obd_race_state = 0;
407                         CERROR("obd_race id %x sleeping\n", id);
408                         OBD_SLEEP_ON(obd_race_waitq, obd_race_state != 0);
409                         CERROR("obd_fail_race id %x awake\n", id);
410                 } else {
411                         CERROR("obd_fail_race id %x waking\n", id);
412                         obd_race_state = 1;
413                         wake_up(&obd_race_waitq);
414                 }
415         }
416 }
417 #define OBD_RACE(id) obd_race(id)
418 #else
419 /* sigh.  an expedient fix until OBD_RACE is fixed up */
420 #define OBD_RACE(foo) do {} while(0)
421 #endif
422
423 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
424
425 extern atomic_t libcfs_kmemory;
426
427 #ifdef LPROCFS
428 #define obd_memory_add(size)                                                  \
429         lprocfs_counter_add(obd_memory, OBD_MEMORY_STAT, (long)(size))
430 #define obd_memory_sub(size)                                                  \
431         lprocfs_counter_sub(obd_memory, OBD_MEMORY_STAT, (long)(size))
432 #define obd_memory_sum()                                                      \
433         lprocfs_stats_collector(obd_memory, OBD_MEMORY_STAT,                  \
434                                 LPROCFS_FIELDS_FLAGS_SUM)
435 #define obd_pages_add(order)                                                  \
436         lprocfs_counter_add(obd_memory, OBD_MEMORY_PAGES_STAT,                \
437                             (long)(1 << (order)))
438 #define obd_pages_sub(order)                                                  \
439         lprocfs_counter_sub(obd_memory, OBD_MEMORY_PAGES_STAT,                \
440                             (long)(1 << (order)))
441 #define obd_pages_sum()                                                       \
442         lprocfs_stats_collector(obd_memory, OBD_MEMORY_PAGES_STAT,            \
443                                 LPROCFS_FIELDS_FLAGS_SUM)
444
445 extern void obd_update_maxusage(void);
446 extern __u64 obd_memory_max(void);
447 extern __u64 obd_pages_max(void);
448
449 #else
450
451 extern __u64 obd_alloc;
452 extern __u64 obd_pages;
453
454 extern __u64 obd_max_alloc;
455 extern __u64 obd_max_pages;
456
457 static inline void obd_memory_add(long size)
458 {
459         obd_alloc += size;
460         if (obd_alloc > obd_max_alloc)
461                 obd_max_alloc = obd_alloc;
462 }
463
464 static inline void obd_memory_sub(long size)
465 {
466         obd_alloc -= size;
467 }
468
469 static inline void obd_pages_add(int order)
470 {
471         obd_pages += 1<< order;
472         if (obd_pages > obd_max_pages)
473                 obd_max_pages = obd_pages;
474 }
475
476 static inline void obd_pages_sub(int order)
477 {
478         obd_pages -= 1<< order;
479 }
480
481 #define obd_memory_sum() (obd_alloc)
482 #define obd_pages_sum()  (obd_pages)
483
484 #define obd_memory_max() (obd_max_alloc)
485 #define obd_pages_max() (obd_max_pages)
486
487 #endif
488
489 #if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
490
491 #define OBD_MT_WRONG_SIZE    (1 << 0)
492 #define OBD_MT_ALREADY_FREED (1 << 1)
493 #define OBD_MT_LOC_LEN       128
494
495 struct obd_mem_track {
496         struct hlist_node mt_hash;
497         char              mt_loc[OBD_MT_LOC_LEN];
498         int               mt_flags;
499         void             *mt_ptr;
500         int               mt_size;
501 };
502
503 void lvfs_memdbg_show(void);
504 void lvfs_memdbg_insert(struct obd_mem_track *mt);
505 void lvfs_memdbg_remove(struct obd_mem_track *mt);
506 struct obd_mem_track *lvfs_memdbg_find(void *ptr);
507
508 int lvfs_memdbg_check_insert(struct obd_mem_track *mt);
509 struct obd_mem_track *lvfs_memdbg_check_remove(void *ptr);
510
511 static inline struct obd_mem_track *
512 __new_mem_track(void *ptr, int size,
513                 char *file, int line)
514 {
515         struct obd_mem_track *mt;
516
517         mt = kmalloc(sizeof(*mt), GFP_KERNEL);
518         if (unlikely(!mt))
519                 return NULL;
520
521         snprintf(mt->mt_loc, sizeof(mt->mt_loc) - 1,
522                  "%s:%d", file, line);
523
524         mt->mt_size = size;
525         mt->mt_ptr = ptr;
526         mt->mt_flags = 0;
527         return mt;
528 }
529
530 static inline void
531 __free_mem_track(struct obd_mem_track *mt)
532 {
533         kfree(mt);
534 }
535
536 static inline int
537 __get_mem_track(void *ptr, int size,
538                 char *file, int line)
539 {
540         struct obd_mem_track *mt;
541
542         mt = __new_mem_track(ptr, size, file, line);
543         if (unlikely(!mt)) {
544                 CWARN("Can't allocate new memory track\n");
545                 return 0;
546         }
547
548         if (!lvfs_memdbg_check_insert(mt))
549                 __free_mem_track(mt);
550
551         return 1;
552 }
553
554 static inline int
555 __put_mem_track(void *ptr, int size,
556                 char *file, int line)
557 {
558         struct obd_mem_track *mt;
559
560         if (unlikely(!(mt = lvfs_memdbg_check_remove(ptr)))) {
561                 CWARN("Ptr 0x%p is not allocated. Attempt to free "
562                       "not allocated memory at %s:%d\n", ptr,
563                       file, line);
564                 LBUG();
565                 return 0;
566         } else {
567                 if (unlikely(mt->mt_size != size)) {
568                         if (!(mt->mt_flags & OBD_MT_ALREADY_FREED)) {
569                                 mt->mt_flags |= (OBD_MT_WRONG_SIZE |
570                                                  OBD_MT_ALREADY_FREED);
571
572                                 CWARN("Freeing memory chunk (at 0x%p) of "
573                                       "different size than allocated "
574                                       "(%d != %d) at %s:%d, allocated at %s\n",
575                                       ptr, mt->mt_size, size, file, line,
576                                       mt->mt_loc);
577                         }
578                 } else {
579                         __free_mem_track(mt);
580                 }
581                 return 1;
582         }
583 }
584
585 #define get_mem_track(ptr, size, file, line)                                         \
586         __get_mem_track((ptr), (size), (file), (line))
587
588 #define put_mem_track(ptr, size, file, line)                                         \
589         __put_mem_track((ptr), (size), (file), (line))
590
591 #else /* !CONFIG_DEBUG_MEMORY */
592
593 #define get_mem_track(ptr, size, file, line)                                         \
594         do {} while (0)
595
596 #define put_mem_track(ptr, size, file, line)                                         \
597         do {} while (0)
598 #endif /* !CONFIG_DEBUG_MEMORY */
599
600 #define OBD_DEBUG_MEMUSAGE (1)
601
602 #if OBD_DEBUG_MEMUSAGE
603 #define OBD_ALLOC_POST(ptr, size, name)                                 \
604                 obd_memory_add(size);                                   \
605                 get_mem_track((ptr), (size), __FILE__, __LINE__);       \
606                 CDEBUG(D_MALLOC, name " '" #ptr "': %d at %p.\n",       \
607                        (int)(size), ptr)
608
609 #define OBD_FREE_PRE(ptr, size, name)                                   \
610         LASSERT(ptr);                                                   \
611         put_mem_track((ptr), (size), __FILE__, __LINE__);               \
612         obd_memory_sub(size);                                           \
613         CDEBUG(D_MALLOC, name " '" #ptr "': %d at %p.\n",               \
614                (int)(size), ptr);                                       \
615         POISON(ptr, 0x5a, size)
616
617 #else /* !OBD_DEBUG_MEMUSAGE */
618
619 #define OBD_ALLOC_POST(ptr, size, name) ((void)0)
620 #define OBD_FREE_PRE(ptr, size, name)   ((void)0)
621
622 #endif /* !OBD_DEBUG_MEMUSAGE */
623
624 #ifdef RANDOM_FAIL_ALLOC
625 #define HAS_FAIL_ALLOC_FLAG OBD_FAIL_CHECK(OBD_FAIL_GENERAL_ALLOC)
626 #else
627 #define HAS_FAIL_ALLOC_FLAG 0
628 #endif
629
630 #define OBD_ALLOC_FAIL_BITS 24
631 #define OBD_ALLOC_FAIL_MASK ((1 << OBD_ALLOC_FAIL_BITS) - 1)
632 #define OBD_ALLOC_FAIL_MULT (OBD_ALLOC_FAIL_MASK / 100)
633
634 #if defined(LUSTRE_UTILS) /* this version is for utils only */
635 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
636 do {                                                                          \
637         (ptr) = cfs_alloc(size, (gfp_mask));                                  \
638         if (unlikely((ptr) == NULL)) {                                        \
639                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",  \
640                        (int)(size), __FILE__, __LINE__);                      \
641         } else {                                                              \
642                 memset(ptr, 0, size);                                         \
643                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p\n",          \
644                        (int)(size), ptr);                                     \
645         }                                                                     \
646 } while (0)
647 #else /* this version is for the kernel and liblustre */
648 #define OBD_FREE_RTN0(ptr)                                                    \
649 ({                                                                            \
650         cfs_free(ptr);                                                        \
651         (ptr) = NULL;                                                         \
652         0;                                                                    \
653 })
654 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                    \
655 do {                                                                          \
656         (ptr) = cfs_alloc(size, (gfp_mask));                                  \
657         if (likely((ptr) != NULL &&                                           \
658                    (!HAS_FAIL_ALLOC_FLAG || obd_alloc_fail_rate == 0 ||       \
659                     !obd_alloc_fail(ptr, #ptr, "km", size,                    \
660                                     __FILE__, __LINE__) ||                    \
661                     OBD_FREE_RTN0(ptr)))){                                    \
662                 memset(ptr, 0, size);                                         \
663                 OBD_ALLOC_POST(ptr, size, "kmalloced");                       \
664         }                                                                     \
665 } while (0)
666 #endif
667
668 #ifndef OBD_ALLOC_MASK
669 # define OBD_ALLOC_MASK CFS_ALLOC_IO
670 #endif
671
672 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_ALLOC_MASK)
673 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, CFS_ALLOC_STD)
674 #define OBD_ALLOC_PTR(ptr) OBD_ALLOC(ptr, sizeof *(ptr))
675 #define OBD_ALLOC_PTR_WAIT(ptr) OBD_ALLOC_WAIT(ptr, sizeof *(ptr))
676
677 #ifdef __arch_um__
678 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
679 #else
680 # define OBD_VMALLOC(ptr, size)                                               \
681 do {                                                                          \
682         (ptr) = cfs_alloc_large(size);                                        \
683         if (unlikely((ptr) == NULL)) {                                        \
684                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed\n",           \
685                        (int)(size));                                          \
686                 CERROR(LPU64" total bytes allocated by Lustre, %d by LNET\n", \
687                        obd_memory_sum(), atomic_read(&libcfs_kmemory));       \
688         } else {                                                              \
689                 memset(ptr, 0, size);                                         \
690                 OBD_ALLOC_POST(ptr, size, "vmalloced");                       \
691         }                                                                     \
692 } while(0)
693 #endif
694
695 #ifdef CONFIG_DEBUG_SLAB
696 #define POISON(ptr, c, s) do {} while (0)
697 #define POISON_PTR(ptr)  ((void)0)
698 #else
699 #define POISON(ptr, c, s) memset(ptr, c, s)
700 #define POISON_PTR(ptr)  (ptr) = (void *)0xdeadbeef
701 #endif
702
703 #ifdef POISON_BULK
704 #define POISON_PAGE(page, val) do { memset(kmap(page), val, CFS_PAGE_SIZE);   \
705                                     kunmap(page); } while (0)
706 #else
707 #define POISON_PAGE(page, val) do { } while (0)
708 #endif
709
710 #ifdef __KERNEL__
711 #define OBD_FREE(ptr, size)                                                   \
712 do {                                                                          \
713         OBD_FREE_PRE(ptr, size, "kfreed");                                    \
714         cfs_free(ptr);                                                        \
715         POISON_PTR(ptr);                                                      \
716 } while(0)
717
718
719 #ifdef HAVE_RCU
720 # ifdef HAVE_CALL_RCU_PARAM
721 #  define my_call_rcu(rcu, cb)            call_rcu(rcu, cb, rcu)
722 # else
723 #  define my_call_rcu(rcu, cb)            call_rcu(rcu, cb)
724 # endif
725 #else
726 # define my_call_rcu(rcu, cb)             (cb)(rcu)
727 #endif
728
729 #define OBD_FREE_RCU_CB(ptr, size, handle, free_cb)                           \
730 do {                                                                          \
731         struct portals_handle *__h = (handle);                                \
732         LASSERT(handle);                                                      \
733         __h->h_ptr = (ptr);                                                   \
734         __h->h_size = (size);                                                 \
735         __h->h_free_cb = (void (*)(void *, size_t))(free_cb);                 \
736         my_call_rcu(&__h->h_rcu, class_handle_free_cb);                       \
737         POISON_PTR(ptr);                                                      \
738 } while(0)
739 #define OBD_FREE_RCU(ptr, size, handle) OBD_FREE_RCU_CB(ptr, size, handle, NULL)
740
741 #else
742 #define OBD_FREE(ptr, size) ((void)(size), free((ptr)))
743 #define OBD_FREE_RCU(ptr, size, handle) (OBD_FREE(ptr, size))
744 #define OBD_FREE_RCU_CB(ptr, size, handle, cb)     ((*(cb))(ptr, size))
745 #endif /* ifdef __KERNEL__ */
746
747 #ifdef __arch_um__
748 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
749 #else
750 # define OBD_VFREE(ptr, size)                                                 \
751 do {                                                                          \
752         OBD_FREE_PRE(ptr, size, "vfreed");                                    \
753         cfs_free_large(ptr);                                                  \
754         POISON_PTR(ptr);                                                      \
755 } while(0)
756 #endif
757
758 /* we memset() the slab object to 0 when allocation succeeds, so DO NOT
759  * HAVE A CTOR THAT DOES ANYTHING.  its work will be cleared here.  we'd
760  * love to assert on that, but slab.c keeps kmem_cache_s all to itself. */
761 #define OBD_SLAB_FREE_RTN0(ptr, slab)                                         \
762 ({                                                                            \
763         cfs_mem_cache_free((slab), (ptr));                                    \
764         (ptr) = NULL;                                                         \
765         0;                                                                    \
766 })
767 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                 \
768 do {                                                                          \
769         LASSERT(!in_interrupt());                                             \
770         (ptr) = cfs_mem_cache_alloc(slab, (type));                            \
771         if (likely((ptr) != NULL &&                                           \
772                    (!HAS_FAIL_ALLOC_FLAG || obd_alloc_fail_rate == 0 ||       \
773                     !obd_alloc_fail(ptr, #ptr, "slab-", size,                 \
774                                     __FILE__, __LINE__) ||                    \
775                     OBD_SLAB_FREE_RTN0(ptr, slab)))) {                        \
776                 memset(ptr, 0, size);                                         \
777                 OBD_ALLOC_POST(ptr, size, "slab-alloced");                    \
778         }                                                                     \
779 } while(0)
780
781 #define OBD_FREE_PTR(ptr) OBD_FREE(ptr, sizeof *(ptr))
782
783 #define OBD_SLAB_FREE(ptr, slab, size)                                        \
784 do {                                                                          \
785         OBD_FREE_PRE(ptr, size, "slab-freed");                                \
786         cfs_mem_cache_free(slab, ptr);                                        \
787         POISON_PTR(ptr);                                                      \
788 } while(0)
789
790 #define OBD_SLAB_ALLOC_PTR(ptr, slab)                                         \
791         OBD_SLAB_ALLOC((ptr), (slab), CFS_ALLOC_STD, sizeof *(ptr))
792 #define OBD_SLAB_FREE_PTR(ptr, slab)                                          \
793         OBD_SLAB_FREE((ptr), (slab), sizeof *(ptr))
794
795 #define KEY_IS(str) \
796         (keylen >= (sizeof(str)-1) && memcmp(key, str, (sizeof(str)-1)) == 0)
797
798 /* Wrapper for contiguous page frame allocation */
799 #define OBD_PAGES_ALLOC(ptr, order, gfp_mask)                                 \
800 do {                                                                          \
801         (ptr) = cfs_alloc_pages(gfp_mask, order);                             \
802         if (unlikely((ptr) == NULL)) {                                        \
803                 CERROR("alloc_pages of '" #ptr "' %d page(s) / "LPU64" bytes "\
804                        "failed\n", (int)(1 << (order)),                       \
805                        (__u64)((1 << (order)) << CFS_PAGE_SHIFT));            \
806                 CERROR(LPU64" total bytes and "LPU64" total pages "           \
807                        "("LPU64" bytes) allocated by Lustre, "                \
808                        "%d total bytes by LNET\n",                            \
809                        obd_memory_sum(),                                      \
810                        obd_pages_sum() << CFS_PAGE_SHIFT,                     \
811                        obd_pages_sum(),                                       \
812                        atomic_read(&libcfs_kmemory));                         \
813         } else {                                                              \
814                 obd_pages_add(order);                                         \
815                 CDEBUG(D_MALLOC, "alloc_pages '" #ptr "': %d page(s) / "      \
816                        LPU64" bytes at %p.\n",                                \
817                        (int)(1 << (order)),                                   \
818                        (__u64)((1 << (order)) << CFS_PAGE_SHIFT), ptr);       \
819         }                                                                     \
820 } while (0)
821
822 #define OBD_PAGE_ALLOC(ptr, gfp_mask)                                         \
823         OBD_PAGES_ALLOC(ptr, 0, gfp_mask)
824
825 #define OBD_PAGES_FREE(ptr, order)                                            \
826 do {                                                                          \
827         LASSERT(ptr);                                                         \
828         obd_pages_sub(order);                                                 \
829         CDEBUG(D_MALLOC, "free_pages '" #ptr "': %d page(s) / "LPU64" bytes " \
830                "at %p.\n",                                                    \
831                (int)(1 << (order)), (__u64)((1 << (order)) << CFS_PAGE_SHIFT),\
832                ptr);                                                          \
833         __cfs_free_pages(ptr, order);                                         \
834         (ptr) = (void *)0xdeadbeef;                                           \
835 } while (0)
836
837 #define OBD_PAGE_FREE(ptr) OBD_PAGES_FREE(ptr, 0)
838
839 #endif