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