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