Whamcloud - gitweb
Land b_hd_capa onto HEAD (20050809_1942)
[fs/lustre-release.git] / lustre / include / linux / obd_support.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001, 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef _OBD_SUPPORT
24 #define _OBD_SUPPORT
25
26 #ifdef __KERNEL__
27 #include <linux/config.h>
28 #include <linux/autoconf.h>
29 #include <linux/slab.h>
30 #include <linux/highmem.h>
31 #include <linux/buffer_head.h>
32 #endif
33 #include <libcfs/kp30.h>
34 #include <linux/lustre_compat25.h>
35
36 /* global variables */
37 extern int obd_memmax;
38 extern atomic_t obd_memory;
39
40 extern unsigned int obd_fail_loc;
41 extern unsigned int obd_timeout;
42 extern unsigned int ldlm_timeout;
43 extern char obd_lustre_upcall[128];
44 extern unsigned int obd_sync_filter;
45 extern unsigned int obd_dump_on_timeout;
46 extern wait_queue_head_t obd_race_waitq;
47
48 #define OBD_FAIL_MDS                     0x100
49 #define OBD_FAIL_MDS_HANDLE_UNPACK       0x101
50 #define OBD_FAIL_MDS_GETATTR_NET         0x102
51 #define OBD_FAIL_MDS_GETATTR_PACK        0x103
52 #define OBD_FAIL_MDS_READPAGE_NET        0x104
53 #define OBD_FAIL_MDS_READPAGE_PACK       0x105
54 #define OBD_FAIL_MDS_SENDPAGE            0x106
55 #define OBD_FAIL_MDS_REINT_NET           0x107
56 #define OBD_FAIL_MDS_REINT_UNPACK        0x108
57 #define OBD_FAIL_MDS_REINT_SETATTR       0x109
58 #define OBD_FAIL_MDS_REINT_SETATTR_WRITE 0x10a
59 #define OBD_FAIL_MDS_REINT_CREATE        0x10b
60 #define OBD_FAIL_MDS_REINT_CREATE_WRITE  0x10c
61 #define OBD_FAIL_MDS_REINT_UNLINK        0x10d
62 #define OBD_FAIL_MDS_REINT_UNLINK_WRITE  0x10e
63 #define OBD_FAIL_MDS_REINT_LINK          0x10f
64 #define OBD_FAIL_MDS_REINT_LINK_WRITE    0x110
65 #define OBD_FAIL_MDS_REINT_RENAME        0x111
66 #define OBD_FAIL_MDS_REINT_RENAME_WRITE  0x112
67 #define OBD_FAIL_MDS_OPEN_NET            0x113
68 #define OBD_FAIL_MDS_OPEN_PACK           0x114
69 #define OBD_FAIL_MDS_CLOSE_NET           0x115
70 #define OBD_FAIL_MDS_CLOSE_PACK          0x116
71 #define OBD_FAIL_MDS_CONNECT_NET         0x117
72 #define OBD_FAIL_MDS_CONNECT_PACK        0x118
73 #define OBD_FAIL_MDS_REINT_NET_REP       0x119
74 #define OBD_FAIL_MDS_DISCONNECT_NET      0x11a
75 #define OBD_FAIL_MDS_GETSTATUS_NET       0x11b
76 #define OBD_FAIL_MDS_GETSTATUS_PACK      0x11c
77 #define OBD_FAIL_MDS_STATFS_PACK         0x11d
78 #define OBD_FAIL_MDS_STATFS_NET          0x11e
79 #define OBD_FAIL_MDS_GETATTR_LOCK_NET    0x11f
80 #define OBD_FAIL_MDS_PIN_NET             0x120
81 #define OBD_FAIL_MDS_UNPIN_NET           0x121
82 #define OBD_FAIL_MDS_ALL_REPLY_NET       0x122
83 #define OBD_FAIL_MDS_ALL_REQUEST_NET     0x123
84 #define OBD_FAIL_MDS_SYNC_NET            0x124
85 #define OBD_FAIL_MDS_SYNC_PACK           0x125
86 #define OBD_FAIL_MDS_DONE_WRITING_NET    0x126
87 #define OBD_FAIL_MDS_DONE_WRITING_PACK   0x127
88 #define OBD_FAIL_MDS_ALLOC_OBDO          0x128
89 #define OBD_FAIL_MDS_PAUSE_OPEN          0x129
90 #define OBD_FAIL_MDS_STATFS_LCW_SLEEP    0x12a
91 #define OBD_FAIL_MDS_OPEN_CREATE         0x12b
92 #define OBD_FAIL_MDS_ACCESS_CHECK_NET    0x12c
93 #define OBD_FAIL_MDS_ACCESS_CHECK_PACK   0x12d
94 #define OBD_FAIL_MDS_PACK_CAPA           0x12e
95
96 #define OBD_FAIL_OST                     0x200
97 #define OBD_FAIL_OST_CONNECT_NET         0x201
98 #define OBD_FAIL_OST_DISCONNECT_NET      0x202
99 #define OBD_FAIL_OST_GET_INFO_NET        0x203
100 #define OBD_FAIL_OST_CREATE_NET          0x204
101 #define OBD_FAIL_OST_DESTROY_NET         0x205
102 #define OBD_FAIL_OST_GETATTR_NET         0x206
103 #define OBD_FAIL_OST_SETATTR_NET         0x207
104 #define OBD_FAIL_OST_OPEN_NET            0x208
105 #define OBD_FAIL_OST_CLOSE_NET           0x209
106 #define OBD_FAIL_OST_BRW_NET             0x20a
107 #define OBD_FAIL_OST_PUNCH_NET           0x20b
108 #define OBD_FAIL_OST_STATFS_NET          0x20c
109 #define OBD_FAIL_OST_HANDLE_UNPACK       0x20d
110 #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
111 #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
112 #define OBD_FAIL_OST_SYNC_NET            0x210
113 #define OBD_FAIL_OST_ALL_REPLY_NET       0x211
114 #define OBD_FAIL_OST_ALL_REQUESTS_NET    0x212
115 #define OBD_FAIL_OST_LDLM_REPLY_NET      0x213
116 #define OBD_FAIL_OST_BRW_PAUSE_BULK      0x214
117 #define OBD_FAIL_OST_ENOSPC              0x215
118 #define OBD_FAIL_OST_EROFS               0x216
119
120
121 #define OBD_FAIL_LDLM                    0x300
122 #define OBD_FAIL_LDLM_NAMESPACE_NEW      0x301
123 #define OBD_FAIL_LDLM_ENQUEUE            0x302
124 #define OBD_FAIL_LDLM_CONVERT            0x303
125 #define OBD_FAIL_LDLM_CANCEL             0x304
126 #define OBD_FAIL_LDLM_BL_CALLBACK        0x305
127 #define OBD_FAIL_LDLM_CP_CALLBACK        0x306
128 #define OBD_FAIL_LDLM_GL_CALLBACK        0x307
129 #define OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR 0x308
130 #define OBD_FAIL_LDLM_ENQUEUE_INTENT_ERR 0x309
131 #define OBD_FAIL_LDLM_CREATE_RESOURCE    0x30a
132 #define OBD_FAIL_LDLM_ENQUEUE_BLOCKED    0x30b
133 #define OBD_FAIL_LDLM_REPLY              0x30c
134
135
136 #define OBD_FAIL_OSC                     0x400
137 #define OBD_FAIL_OSC_BRW_READ_BULK       0x401
138 #define OBD_FAIL_OSC_BRW_WRITE_BULK      0x402
139 #define OBD_FAIL_OSC_LOCK_BL_AST         0x403
140 #define OBD_FAIL_OSC_LOCK_CP_AST         0x404
141 #define OBD_FAIL_OSC_MATCH               0x405
142
143 #define OBD_FAIL_PTLRPC                  0x500
144 #define OBD_FAIL_PTLRPC_ACK              0x501
145 #define OBD_FAIL_PTLRPC_RQBD             0x502
146 #define OBD_FAIL_PTLRPC_BULK_GET_NET     0x503
147 #define OBD_FAIL_PTLRPC_BULK_PUT_NET     0x504
148
149 #define OBD_FAIL_OBD_PING_NET            0x600
150 #define OBD_FAIL_OBD_LOG_CANCEL_NET      0x601
151 #define OBD_FAIL_OBD_LOGD_NET            0x602
152
153 #define OBD_FAIL_TGT_REPLY_NET           0x700
154 #define OBD_FAIL_TGT_CONN_RACE           0x701
155
156 #define OBD_FAIL_SVCSEC_ACCEPT_BEG       0x750
157 #define OBD_FAIL_SVCSEC_ACCEPT_END       0x751
158 #define OBD_FAIL_SVCSEC_WRAP_BEG         0x752
159 #define OBD_FAIL_SVCSEC_WRAP_END         0x753
160 #define OBD_FAIL_SVCGSS_ERR_NOTIFY       0x760
161 #define OBD_FAIL_SVCGSS_INIT_REQ         0x780
162 #define OBD_FAIL_SVCGSS_INIT_REP         0x781
163
164 #define OBD_FAIL_MDC_REVALIDATE_PAUSE    0x800
165
166 #define OBD_FAIL_FILTER_VERIFY_CAPA      0x900
167
168 /* preparation for a more advanced failure testbed (not functional yet) */
169 #define OBD_FAIL_MASK_SYS    0x0000FF00
170 #define OBD_FAIL_MASK_LOC    (0x000000FF | OBD_FAIL_MASK_SYS)
171 #define OBD_FAIL_ONCE        0x80000000
172 #define OBD_FAILED           0x40000000
173 #define OBD_FAIL_MDS_ALL_NET 0x01000000
174 #define OBD_FAIL_OST_ALL_NET 0x02000000
175
176 #define OBD_FAIL_CHECK(id)   (((obd_fail_loc & OBD_FAIL_MASK_LOC) ==           \
177                               ((id) & OBD_FAIL_MASK_LOC)) &&                   \
178                               ((obd_fail_loc & (OBD_FAILED | OBD_FAIL_ONCE))!= \
179                                 (OBD_FAILED | OBD_FAIL_ONCE)))
180
181 #define OBD_FAIL_CHECK_ONCE(id)                                              \
182 ({      int _ret_ = 0;                                                       \
183         if (OBD_FAIL_CHECK(id)) {                                            \
184                 CERROR("obd_fail_loc=%x\n", id);                             \
185                 obd_fail_loc |= OBD_FAILED;                                  \
186                 if ((id) & OBD_FAIL_ONCE)                                    \
187                         obd_fail_loc |= OBD_FAIL_ONCE;                       \
188                 _ret_ = 1;                                                   \
189         }                                                                    \
190         _ret_;                                                               \
191 })
192
193 #define OBD_FAIL_RETURN(id, ret)                                             \
194 do {                                                                         \
195         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
196                 RETURN(ret);                                                 \
197         }                                                                    \
198 } while(0)
199
200 #define OBD_FAIL_GOTO(id, label, ret)                                        \
201 do {                                                                         \
202         if (OBD_FAIL_CHECK_ONCE(id)) {                                       \
203                 GOTO(label, (ret));                                          \
204         }                                                                    \
205 } while(0)
206
207 #define OBD_FAIL_TIMEOUT(id, secs)                                           \
208 do {                                                                         \
209         if  (OBD_FAIL_CHECK_ONCE(id)) {                                      \
210                CERROR("obd_fail_timeout id %x sleeping for %d secs\n",       \
211                         (id), (secs));                                       \
212                 set_current_state(TASK_UNINTERRUPTIBLE);                     \
213                 schedule_timeout((secs) * HZ);                               \
214                 set_current_state(TASK_RUNNING);                             \
215                 CERROR("obd_fail_timeout id %x awake\n", (id));              \
216        }                                                                     \
217 } while(0)
218
219 #ifdef __KERNEL__
220
221 /*
222  * sleep_on() is known to be racy, using wait_event() interface instead as
223  * recommended. --umka
224  */
225 #define OBD_SLEEP_ON(wq)                                                     \
226 do {                                                                         \
227         DEFINE_WAIT(__wait);                                                 \
228         prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);                   \
229         schedule();                                                          \
230         finish_wait(&wq, &__wait);                                           \
231 } while (0)
232
233 /* The idea here is to synchronise two threads to force a race. The
234  * first thread that calls this with a matching fail_loc is put to
235  * sleep. The next thread that calls with the same fail_loc wakes up
236  * the first and continues. */
237 #define OBD_RACE(id)                                            \
238 do {                                                            \
239         if  (OBD_FAIL_CHECK_ONCE(id)) {                         \
240                 CERROR("obd_race id %x sleeping\n", (id));      \
241                 OBD_SLEEP_ON(obd_race_waitq);                   \
242                 CERROR("obd_fail_race id %x awake\n", (id));    \
243         } else if ((obd_fail_loc & OBD_FAIL_MASK_LOC) ==        \
244                     ((id) & OBD_FAIL_MASK_LOC)) {               \
245                 wake_up(&obd_race_waitq);                       \
246         }                                                       \
247 } while(0)
248 #else
249 /* sigh.  an expedient fix until OBD_RACE is fixed up */
250 #define OBD_RACE(foo) LBUG()
251 #endif
252
253 #define fixme() CDEBUG(D_OTHER, "FIXME\n");
254
255 #ifdef __KERNEL__
256 # include <linux/types.h>
257 # include <linux/blkdev.h>
258
259
260 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
261 #  define BDEVNAME_DECLARE_STORAGE(foo) char foo[BDEVNAME_SIZE]
262 #  define ll_bdevname(SB, STORAGE) __bdevname(kdev_t_to_nr(SB->s_dev), STORAGE)
263 #  define ll_lock_kernel lock_kernel()
264 #  define ll_sbdev(SB)    ((SB)->s_bdev)
265 #  define ll_sbdev_type      struct block_device *
266 #  define ll_sbdev_sync      fsync_bdev
267 # else
268 #  define BDEVNAME_DECLARE_STORAGE(foo) char __unused_##foo
269 #  define ll_sbdev(SB)    (kdev_t_to_nr((SB)->s_dev))
270 #  define ll_sbdev_type      kdev_t
271 #  define ll_sbdev_sync      fsync_dev
272 #  define ll_bdevname(SB,STORAGE) ((void)__unused_##STORAGE,bdevname(ll_sbdev(SB)))
273 #  define ll_lock_kernel
274 # endif
275
276 #ifdef HAVE_OLD_DEV_SET_RDONLY
277   void dev_set_rdonly(ll_sbdev_type dev, int no_write);
278   void dev_clear_rdonly(int no_write);
279 #else
280   void dev_set_rdonly(ll_sbdev_type dev);
281   void dev_clear_rdonly(ll_sbdev_type dev);
282 #endif
283 int dev_check_rdonly(ll_sbdev_type dev);
284 #define ll_check_rdonly(dev) dev_check_rdonly(dev)
285
286 static inline void ll_set_rdonly(ll_sbdev_type dev)
287 {
288         CDEBUG(D_IOCTL | D_HA, "set dev %ld rdonly\n", (long)dev);
289         ll_sbdev_sync(dev);
290 #ifdef HAVE_OLD_DEV_SET_RDONLY
291         dev_set_rdonly(dev, 2);
292 #else
293         dev_set_rdonly(dev);
294 #endif
295 }
296
297 static inline void ll_clear_rdonly(ll_sbdev_type dev)
298 {
299         CDEBUG(D_IOCTL | D_HA, "unset dev %ld rdonly\n", (long)dev);
300         if (ll_check_rdonly(dev)) {
301                 ll_sbdev_sync(dev);
302 #ifdef HAVE_OLD_DEV_SET_RDONLY
303                 dev_clear_rdonly(2);
304 #else
305                 dev_clear_rdonly(dev);
306 #endif
307         }
308 }
309
310 static inline void OBD_FAIL_WRITE(int id, struct super_block *sb)
311 {
312         if (OBD_FAIL_CHECK(id)) {
313                 BDEVNAME_DECLARE_STORAGE(tmp);
314                 CERROR("obd_fail_loc=%x, fail write operation on %s\n",
315                        id, ll_bdevname(sb, tmp));
316                 ll_set_rdonly(ll_sbdev(sb));
317                 /* We set FAIL_ONCE because we never "un-fail" a device */
318                 obd_fail_loc |= OBD_FAILED | OBD_FAIL_ONCE;
319         }
320 }
321 #else /* !__KERNEL__ */
322 # define LTIME_S(time) (time)
323 /* for obd_class.h */
324 # ifndef ERR_PTR
325 #  define ERR_PTR(a) ((void *)(a))
326 # endif
327 #endif  /* __KERNEL__ */
328
329 extern atomic_t portal_kmemory;
330
331 #if defined (CONFIG_DEBUG_MEMORY) && defined(__KERNEL__)
332
333 #define MT_FLAGS_WRONG_SIZE (1 << 0)
334 #define MEM_LOC_LEN 128
335
336 struct mem_track {
337         struct hlist_node m_hash;
338         char m_loc[MEM_LOC_LEN];
339         int m_flags;
340         void *m_ptr;
341         int m_size;
342 };
343
344 void lvfs_memdbg_show(void);
345 void lvfs_memdbg_insert(struct mem_track *mt);
346 void lvfs_memdbg_remove(struct mem_track *mt);
347 struct mem_track *lvfs_memdbg_find(void *ptr);
348
349 int lvfs_memdbg_check_insert(struct mem_track *mt);
350 struct mem_track *lvfs_memdbg_check_remove(void *ptr);
351
352 static inline struct mem_track *
353 __new_mem_track(void *ptr, int size,
354                 char *file, int line)
355 {
356         struct mem_track *mt;
357
358         mt = kmalloc(sizeof(*mt), GFP_KERNEL);
359         if (!mt)
360                 return NULL;
361
362         snprintf(mt->m_loc, sizeof(mt->m_loc) - 1,
363                  "%s:%d", file, line);
364
365         mt->m_size = size;
366         mt->m_ptr = ptr;
367         mt->m_flags = 0;
368         return mt;
369 }
370
371 static inline void
372 __free_mem_track(struct mem_track *mt)
373 {
374         kfree(mt);
375 }
376
377 static inline int
378 __get_mem_track(void *ptr, int size,
379                 char *file, int line)
380 {
381         struct mem_track *mt;
382
383         mt = __new_mem_track(ptr, size, file, line);
384         if (!mt) {
385                 CWARN("can't allocate new memory track\n");
386                 return 0;
387         }
388         
389         if (!lvfs_memdbg_check_insert(mt))
390                 __free_mem_track(mt);
391         
392         return 1;
393 }
394
395 static inline int
396 __put_mem_track(void *ptr, int size,
397                 char *file, int line)
398 {
399         struct mem_track *mt;
400
401         if (!(mt = lvfs_memdbg_check_remove(ptr))) {
402                 CWARN("ptr 0x%p is not allocated. Attempt to free "
403                       "not allocated memory at %s:%d\n", ptr,
404                       file, line);
405                 return 0;
406         } else {
407                 if (mt->m_size != size) {
408                         mt->m_flags |= MT_FLAGS_WRONG_SIZE;
409                         CWARN("freeing memory chunk of different size "
410                               "than allocated (%d != %d) at %s:%d\n",
411                               mt->m_size, size, file, line);
412                 } else {
413                         __free_mem_track(mt);
414                 }
415                 return 1;
416         }
417 }
418
419 #define get_mem_track(ptr, size, file, line)                                         \
420         __get_mem_track((ptr), (size), (file), (line))
421
422 #define put_mem_track(ptr, size, file, line)                                         \
423         __put_mem_track((ptr), (size), (file), (line))
424
425 #else /* !CONFIG_DEBUG_MEMORY */
426
427 #define get_mem_track(ptr, size, file, line)                                         \
428         do {} while (0)
429
430 #define put_mem_track(ptr, size, file, line)                                         \
431         do {} while (0)
432 #endif /* !CONFIG_DEBUG_MEMORY */
433
434 #define OBD_ALLOC_GFP(ptr, size, gfp_mask)                                           \
435 do {                                                                                 \
436         (ptr) = kmalloc(size, (gfp_mask));                                           \
437         if ((ptr) == NULL) {                                                         \
438                 CERROR("kmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",         \
439                        (int)(size), __FILE__, __LINE__);                             \
440                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n",        \
441                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));      \
442         } else {                                                                     \
443                 memset(ptr, 0, size);                                                \
444                 atomic_add(size, &obd_memory);                                       \
445                 if (atomic_read(&obd_memory) > obd_memmax)                           \
446                         obd_memmax = atomic_read(&obd_memory);                       \
447                 get_mem_track((ptr), (size), __FILE__, __LINE__);                    \
448                 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d)\n",        \
449                        (int)(size), (ptr), atomic_read(&obd_memory));                \
450         }                                                                            \
451 } while (0)
452
453 #ifndef OBD_GFP_MASK
454 # define OBD_GFP_MASK GFP_NOFS
455 #endif
456
457 #ifdef __KERNEL__
458 #define OBD_ALLOC(ptr, size) OBD_ALLOC_GFP(ptr, size, OBD_GFP_MASK)
459 #else
460 #define OBD_ALLOC(ptr, size) (ptr = malloc(size))
461 #endif
462
463 #define OBD_ALLOC_WAIT(ptr, size) OBD_ALLOC_GFP(ptr, size, GFP_KERNEL)
464
465 #ifdef __arch_um__
466 # define OBD_VMALLOC(ptr, size) OBD_ALLOC(ptr, size)
467 #else
468 # define OBD_VMALLOC(ptr, size)                                                      \
469 do {                                                                                 \
470         (ptr) = vmalloc(size);                                                       \
471         if ((ptr) == NULL) {                                                         \
472                 CERROR("vmalloc of '" #ptr "' (%d bytes) failed at %s:%d\n",         \
473                        (int)(size), __FILE__, __LINE__);                             \
474                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n",        \
475                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));      \
476         } else {                                                                     \
477                 memset(ptr, 0, size);                                                \
478                 atomic_add(size, &obd_memory);                                       \
479                 if (atomic_read(&obd_memory) > obd_memmax)                           \
480                         obd_memmax = atomic_read(&obd_memory);                       \
481                 get_mem_track((ptr), (size), __FILE__, __LINE__);                    \
482                 CDEBUG(D_MALLOC, "vmalloced '" #ptr "': %d at %p (tot %d)\n",        \
483                        (int)(size), ptr, atomic_read(&obd_memory));                  \
484         }                                                                            \
485 } while (0)
486 #endif
487
488 #ifdef CONFIG_DEBUG_SLAB
489 #define POISON(ptr, c, s) do {} while (0)
490 #else
491 #define POISON(ptr, c, s) memset(ptr, c, s)
492 #endif
493
494 #if POISON_BULK
495 #define POISON_PAGE(page, val) do { memset(kmap(page), val, PAGE_SIZE);              \
496                                     kunmap(page); } while (0)
497 #else
498 #define POISON_PAGE(page, val) do { } while (0)
499 #endif
500
501 #ifdef __KERNEL__
502 #define OBD_FREE(ptr, size)                                                          \
503 do {                                                                                 \
504         LASSERT(ptr);                                                                \
505         put_mem_track((ptr), (size), __FILE__, __LINE__);                            \
506         atomic_sub(size, &obd_memory);                                               \
507         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",                  \
508                (int)(size), ptr, atomic_read(&obd_memory));                          \
509         POISON(ptr, 0x5a, size);                                                     \
510         kfree(ptr);                                                                  \
511         (ptr) = (void *)0xdeadbeef;                                                  \
512 } while (0)
513 #else
514 #define OBD_FREE(ptr, size) ((void)(size), free((ptr)))
515 #endif
516
517 #ifdef __arch_um__
518 # define OBD_VFREE(ptr, size) OBD_FREE(ptr, size)
519 #else
520 # define OBD_VFREE(ptr, size)                                                        \
521 do {                                                                                 \
522         LASSERT(ptr);                                                                \
523         put_mem_track((ptr), (size), __FILE__, __LINE__);                            \
524         atomic_sub(size, &obd_memory);                                               \
525         CDEBUG(D_MALLOC, "vfreed '" #ptr "': %d at %p (tot %d).\n",                  \
526                (int)(size), ptr, atomic_read(&obd_memory));                          \
527         POISON(ptr, 0x5a, size);                                                     \
528         vfree(ptr);                                                                  \
529         (ptr) = (void *)0xdeadbeef;                                                  \
530 } while (0)
531 #endif
532
533 /*
534  * we memset() the slab object to 0 when allocation succeeds, so DO NOT HAVE A
535  * CTOR THAT DOES ANYTHING. Its work will be cleared here. We'd love to assert
536  * on that, but slab.c keeps kmem_cache_s all to itself.
537  */
538 #define OBD_SLAB_ALLOC(ptr, slab, type, size)                                         \
539 do {                                                                                  \
540         LASSERT(!in_interrupt());                                                     \
541         (ptr) = kmem_cache_alloc(slab, (type));                                       \
542         if ((ptr) == NULL) {                                                          \
543                 CERROR("slab-alloc of '"#ptr"' (%d bytes) failed at %s:%d\n",         \
544                        (int)(size), __FILE__, __LINE__);                              \
545                 CERROR("%d total bytes allocated by Lustre, %d by Portals\n",         \
546                        atomic_read(&obd_memory), atomic_read(&portal_kmemory));       \
547         } else {                                                                      \
548                 memset(ptr, 0, size);                                                 \
549                 atomic_add(size, &obd_memory);                                        \
550                 if (atomic_read(&obd_memory) > obd_memmax)                            \
551                         obd_memmax = atomic_read(&obd_memory);                        \
552                 get_mem_track((ptr), (size), __FILE__, __LINE__);                     \
553                 CDEBUG(D_MALLOC, "slab-alloced '"#ptr"': %d at %p (tot %d)\n",        \
554                        (int)(size), ptr, atomic_read(&obd_memory));                   \
555         }                                                                             \
556 } while (0)
557
558 #define OBD_SLAB_FREE(ptr, slab, size)                                                \
559 do {                                                                                  \
560         LASSERT(ptr);                                                                 \
561         CDEBUG(D_MALLOC, "slab-freed '" #ptr "': %d at %p (tot %d).\n",               \
562                (int)(size), ptr, atomic_read(&obd_memory));                           \
563         put_mem_track((ptr), (size), __FILE__, __LINE__);                             \
564         atomic_sub(size, &obd_memory);                                                \
565         POISON(ptr, 0x5a, size);                                                      \
566         kmem_cache_free(slab, ptr);                                                   \
567         (ptr) = (void *)0xdeadbeef;                                                   \
568 } while (0)
569
570 #endif