Whamcloud - gitweb
f1ea70515ca380672ee0d623ffc243578c929179
[fs/lustre-release.git] / libcfs / include / libcfs / libcfs_private.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * libcfs/include/libcfs/libcfs_private.h
39  *
40  * Various defines for libcfs.
41  *
42  */
43
44 #ifndef __LIBCFS_PRIVATE_H__
45 #define __LIBCFS_PRIVATE_H__
46
47 /* XXX this layering violation is for nidstrings */
48 #include <lnet/types.h>
49
50 #ifndef DEBUG_SUBSYSTEM
51 # define DEBUG_SUBSYSTEM S_UNDEFINED
52 #endif
53
54 #ifdef __KERNEL__
55
56 #ifdef LIBCFS_DEBUG
57
58 /*
59  * When this is on, LASSERT macro includes check for assignment used instead
60  * of equality check, but doesn't have unlikely(). Turn this on from time to
61  * time to make test-builds. This shouldn't be on for production release.
62  */
63 #define LASSERT_CHECKED (0)
64
65 #if LASSERT_CHECKED
66 /*
67  * Assertion.
68  *
69  * Strange construction with empty "then" clause is used to trigger compiler
70  * warnings on the assertions of the form LASSERT(a = b);
71  *
72  * "warning: suggest parentheses around assignment used as truth value"
73  *
74  * requires -Wall. Unfortunately this rules out use of likely/unlikely.
75  */
76 #define LASSERT(cond)                                                   \
77 do {                                                                    \
78         if (cond)                                                       \
79                 ;                                                       \
80         else                                                            \
81                 libcfs_assertion_failed( #cond , __FILE__,              \
82                         __FUNCTION__, __LINE__);                        \
83 } while(0)
84
85 #define LASSERTF(cond, fmt, ...)                                        \
86 do {                                                                    \
87          if (cond)                                                      \
88                  ;                                                      \
89          else {                                                         \
90                  libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,       \
91                                   __FILE__, __FUNCTION__,__LINE__,      \
92                                   "ASSERTION(" #cond ") failed: " fmt,  \
93                                   ## __VA_ARGS__);                      \
94                  LBUG();                                                \
95          }                                                              \
96 } while(0)
97
98 #else /* !LASSERT_CHECKED */
99
100 #define LASSERT(cond)                                                   \
101 do {                                                                    \
102         if (unlikely(!(cond)))                                          \
103                 libcfs_assertion_failed(#cond , __FILE__,               \
104                         __FUNCTION__, __LINE__);                        \
105 } while(0)
106
107 #define LASSERTF(cond, fmt, ...)                                        \
108 do {                                                                    \
109         if (unlikely(!(cond))) {                                        \
110                 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,        \
111                                  __FILE__, __FUNCTION__,__LINE__,       \
112                                  "ASSERTION(" #cond ") failed: " fmt,   \
113                                  ## __VA_ARGS__ );                      \
114                 LBUG();                                                 \
115         }                                                               \
116 } while(0)
117
118 #endif /* !LASSERT_CHECKED */
119 #else /* !LIBCFS_DEBUG */
120 /* sizeof is to use expression without evaluating it. */
121 # define LASSERT(e) ((void)sizeof!!(e))
122 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
123 #endif /* !LIBCFS_DEBUG */
124
125 #ifdef INVARIANT_CHECK
126 /**
127  * This is for more expensive checks that one doesn't want to be enabled all
128  * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
129  * configure option.
130  */
131 # define LINVRNT(exp) LASSERT(exp)
132 #else
133 # define LINVRNT(exp) ((void)sizeof!!(exp))
134 #endif
135
136 #define KLASSERT(e) LASSERT(e)
137
138 void lbug_with_loc(const char *file, const char *func, const int line)
139         __attribute__((noreturn));
140
141 #define LBUG() lbug_with_loc(__FILE__, __FUNCTION__, __LINE__)
142
143 extern cfs_atomic_t libcfs_kmemory;
144 /*
145  * Memory
146  */
147 #ifdef LIBCFS_DEBUG
148
149 # define libcfs_kmem_inc(ptr, size)             \
150 do {                                            \
151         cfs_atomic_add(size, &libcfs_kmemory);  \
152 } while (0)
153
154 # define libcfs_kmem_dec(ptr, size) do {        \
155         cfs_atomic_sub(size, &libcfs_kmemory);  \
156 } while (0)
157
158 #else
159 # define libcfs_kmem_inc(ptr, size) do {} while (0)
160 # define libcfs_kmem_dec(ptr, size) do {} while (0)
161 #endif /* LIBCFS_DEBUG */
162
163 #ifndef LIBCFS_VMALLOC_SIZE
164 #define LIBCFS_VMALLOC_SIZE        (2 << CFS_PAGE_SHIFT) /* 2 pages */
165 #endif
166
167 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                 \
168 do {                                                                      \
169         LASSERT(!cfs_in_interrupt() ||                                    \
170                (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
171         if (unlikely((size) > LIBCFS_VMALLOC_SIZE))                       \
172                 (ptr) = cfs_alloc_large(size);                            \
173         else                                                              \
174                 (ptr) = cfs_alloc((size), (mask));                        \
175         if (unlikely((ptr) == NULL)) {                                    \
176                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"   \
177                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
178                 CERROR("LNET: %d total bytes allocated by lnet\n",        \
179                        cfs_atomic_read(&libcfs_kmemory));                 \
180                 break;                                                    \
181         }                                                                 \
182         libcfs_kmem_inc((ptr), (size));                                   \
183         memset((ptr), 0, (size));                                         \
184         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
185                (int)(size), (ptr), cfs_atomic_read (&libcfs_kmemory));    \
186 } while (0)
187
188 #define LIBCFS_ALLOC(ptr, size) \
189         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
190
191 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
192         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
193
194 #define LIBCFS_FREE(ptr, size)                                          \
195 do {                                                                    \
196         int s = (size);                                                 \
197         if (unlikely((ptr) == NULL)) {                                  \
198                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
199                        "%s:%d\n", s, __FILE__, __LINE__);               \
200                 break;                                                  \
201         }                                                               \
202         libcfs_kmem_dec((ptr), s);                                      \
203         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
204                s, (ptr), cfs_atomic_read(&libcfs_kmemory));             \
205         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
206                 cfs_free_large(ptr);                                    \
207         else                                                            \
208                 cfs_free(ptr);                                          \
209 } while (0)
210
211 /******************************************************************************/
212
213 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
214 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
215 #define ___htonl(x) __cpu_to_be32(x)
216 #define ___htons(x) __cpu_to_be16(x)
217 #define ___ntohl(x) __be32_to_cpu(x)
218 #define ___ntohs(x) __be16_to_cpu(x)
219 #define htonl(x) ___htonl(x)
220 #define ntohl(x) ___ntohl(x)
221 #define htons(x) ___htons(x)
222 #define ntohs(x) ___ntohs(x)
223 #endif
224
225 void libcfs_debug_dumpstack(cfs_task_t *tsk);
226 void libcfs_run_upcall(char **argv);
227 void libcfs_run_lbug_upcall(const char * file, const char *fn, const int line);
228 void libcfs_debug_dumplog(void);
229 int libcfs_debug_init(unsigned long bufsize);
230 int libcfs_debug_cleanup(void);
231 int libcfs_debug_clear_buffer(void);
232 int libcfs_debug_mark_buffer(const char *text);
233
234 void libcfs_debug_set_level(unsigned int debug_level);
235
236 #else  /* !__KERNEL__ */
237 # ifdef LIBCFS_DEBUG
238 #  undef NDEBUG
239 #  include <assert.h>
240 #  define LASSERT(e)     assert(e)
241 #  define LASSERTF(cond, ...)                                                  \
242 do {                                                                           \
243           if (!(cond))                                                         \
244                 CERROR(__VA_ARGS__);                                           \
245           assert(cond);                                                        \
246 } while (0)
247 #  define LBUG()   assert(0)
248 #  ifdef INVARIANT_CHECK
249 #   define LINVRNT(exp) LASSERT(exp)
250 #  else
251 #   define LINVRNT(exp) ((void)sizeof!!(exp))
252 #  endif
253 # else
254 #  define LASSERT(e) ((void)sizeof!!(e))
255 #  define LASSERTF(cond, ...) ((void)sizeof!!(cond))
256 #  define LBUG()   ((void)(0))
257 #  define LINVRNT(exp) ((void)sizeof!!(exp))
258 # endif /* LIBCFS_DEBUG */
259 # define KLASSERT(e) ((void)0)
260 # define printk printf
261 # ifdef CRAY_XT3                                /* buggy calloc! */
262 #  define LIBCFS_ALLOC(ptr, size)               \
263    do {                                         \
264         (ptr) = malloc(size);                   \
265         memset(ptr, 0, size);                   \
266    } while (0)
267 # else
268 #  define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
269 # endif
270 # define LIBCFS_FREE(ptr, size) do { free(ptr); } while((size) - (size))
271
272 void libcfs_debug_dumplog(void);
273 int libcfs_debug_init(unsigned long bufsize);
274 int libcfs_debug_cleanup(void);
275
276 #define libcfs_debug_dumpstack(tsk)     ((void)0)
277
278 /*
279  * Generic compiler-dependent macros required for kernel
280  * build go below this comment. Actual compiler/compiler version
281  * specific implementations come from the above header files
282  */
283 #ifdef __GNUC__
284 #define likely(x)      __builtin_expect(!!(x), 1)
285 #define unlikely(x)    __builtin_expect(!!(x), 0)
286 #else
287 #define likely(x)       (!!(x))
288 #define unlikely(x)     (!!(x))
289 #endif
290 /* !__KERNEL__ */
291 #endif
292
293 #define LASSERT_ATOMIC_ENABLED          (1)
294
295 #if LASSERT_ATOMIC_ENABLED
296
297 /** assert value of @a is equal to @v */
298 #define LASSERT_ATOMIC_EQ(a, v)                                 \
299 do {                                                            \
300         LASSERTF(cfs_atomic_read(a) == v,                       \
301                  "value: %d\n", cfs_atomic_read((a)));          \
302 } while (0)
303
304 /** assert value of @a is unequal to @v */
305 #define LASSERT_ATOMIC_NE(a, v)                                 \
306 do {                                                            \
307         LASSERTF(cfs_atomic_read(a) != v,                       \
308                  "value: %d\n", cfs_atomic_read((a)));          \
309 } while (0)
310
311 /** assert value of @a is little than @v */
312 #define LASSERT_ATOMIC_LT(a, v)                                 \
313 do {                                                            \
314         LASSERTF(cfs_atomic_read(a) < v,                        \
315                  "value: %d\n", cfs_atomic_read((a)));          \
316 } while (0)
317
318 /** assert value of @a is little/equal to @v */
319 #define LASSERT_ATOMIC_LE(a, v)                                 \
320 do {                                                            \
321         LASSERTF(cfs_atomic_read(a) <= v,                       \
322                  "value: %d\n", cfs_atomic_read((a)));          \
323 } while (0)
324
325 /** assert value of @a is great than @v */
326 #define LASSERT_ATOMIC_GT(a, v)                                 \
327 do {                                                            \
328         LASSERTF(cfs_atomic_read(a) > v,                        \
329                  "value: %d\n", cfs_atomic_read((a)));          \
330 } while (0)
331
332 /** assert value of @a is great/equal to @v */
333 #define LASSERT_ATOMIC_GE(a, v)                                 \
334 do {                                                            \
335         LASSERTF(cfs_atomic_read(a) >= v,                       \
336                  "value: %d\n", cfs_atomic_read((a)));          \
337 } while (0)
338
339 /** assert value of @a is great than @v1 and little than @v2 */
340 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                         \
341 do {                                                            \
342         int __v = cfs_atomic_read(a);                           \
343         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
344 } while (0)
345
346 /** assert value of @a is great than @v1 and little/equal to @v2 */
347 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                         \
348 do {                                                            \
349         int __v = cfs_atomic_read(a);                           \
350         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
351 } while (0)
352
353 /** assert value of @a is great/equal to @v1 and little than @v2 */
354 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                         \
355 do {                                                            \
356         int __v = cfs_atomic_read(a);                           \
357         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
358 } while (0)
359
360 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
361 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                         \
362 do {                                                            \
363         int __v = cfs_atomic_read(a);                           \
364         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
365 } while (0)
366
367 #else /* !LASSERT_ATOMIC_ENABLED */
368
369 #define LASSERT_ATOMIC_EQ(a, v)                 do {} while (0)
370 #define LASSERT_ATOMIC_NE(a, v)                 do {} while (0)
371 #define LASSERT_ATOMIC_LT(a, v)                 do {} while (0)
372 #define LASSERT_ATOMIC_LE(a, v)                 do {} while (0)
373 #define LASSERT_ATOMIC_GT(a, v)                 do {} while (0)
374 #define LASSERT_ATOMIC_GE(a, v)                 do {} while (0)
375 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)         do {} while (0)
376 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)         do {} while (0)
377 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)         do {} while (0)
378 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)         do {} while (0)
379
380 #endif /* LASSERT_ATOMIC_ENABLED */
381
382 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
383 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
384
385 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
386 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
387
388 /** Compile-time assertion.
389
390  * Check an invariant described by a constant expression at compile time by
391  * forcing a compiler error if it does not hold.  \a cond must be a constant
392  * expression as defined by the ISO C Standard:
393  *
394  *       6.8.4.2  The switch statement
395  *       ....
396  *       [#3] The expression of each case label shall be  an  integer
397  *       constant   expression  and  no  two  of  the  case  constant
398  *       expressions in the same switch statement shall have the same
399  *       value  after  conversion...
400  *
401  */
402 #define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
403
404 /* support decl needed both by kernel and liblustre */
405 int         libcfs_isknown_lnd(int type);
406 char       *libcfs_lnd2modname(int type);
407 char       *libcfs_lnd2str(int type);
408 int         libcfs_str2lnd(const char *str);
409 char       *libcfs_net2str(__u32 net);
410 char       *libcfs_nid2str(lnet_nid_t nid);
411 __u32       libcfs_str2net(const char *str);
412 lnet_nid_t  libcfs_str2nid(const char *str);
413 int         libcfs_str2anynid(lnet_nid_t *nid, const char *str);
414 char       *libcfs_id2str(lnet_process_id_t id);
415 int         cfs_iswhite(char c);
416 void        cfs_free_nidlist(cfs_list_t *list);
417 int         cfs_parse_nidlist(char *str, int len, cfs_list_t *list);
418 int         cfs_match_nid(lnet_nid_t nid, cfs_list_t *list);
419
420 /** \addtogroup lnet_addr
421  * @{ */
422 /* how an LNET NID encodes net:address */
423 /** extract the address part of an lnet_nid_t */
424 #define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
425 /** extract the network part of an lnet_nid_t */
426 #define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
427 /** make an lnet_nid_t from a network part and an address part */
428 #define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
429 /* how net encodes type:number */
430 #define LNET_NETNUM(net)       ((net) & 0xffff)
431 #define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
432 #define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
433 /** @} lnet_addr */
434
435 /* max value for numeric network address */
436 #define MAX_NUMERIC_VALUE 0xffffffff
437
438 /* implication */
439 #define ergo(a, b) (!(a) || (b))
440 /* logical equivalence */
441 #define equi(a, b) (!!(a) == !!(b))
442
443 #ifndef CFS_CURRENT_TIME
444 # define CFS_CURRENT_TIME time(0)
445 #endif
446
447 /* --------------------------------------------------------------------
448  * Light-weight trace
449  * Support for temporary event tracing with minimal Heisenberg effect.
450  * All stuff about lwt are put in arch/kp30.h
451  * -------------------------------------------------------------------- */
452
453 struct libcfs_device_userstate
454 {
455         int           ldu_memhog_pages;
456         cfs_page_t   *ldu_memhog_root_page;
457 };
458
459 /* what used to be in portals_lib.h */
460 #ifndef MIN
461 # define MIN(a,b) (((a)<(b)) ? (a): (b))
462 #endif
463 #ifndef MAX
464 # define MAX(a,b) (((a)>(b)) ? (a): (b))
465 #endif
466
467 #define MKSTR(ptr) ((ptr))? (ptr) : ""
468
469 static inline int cfs_size_round4 (int val)
470 {
471         return (val + 3) & (~0x3);
472 }
473
474 #ifndef HAVE_CFS_SIZE_ROUND
475 static inline int cfs_size_round (int val)
476 {
477         return (val + 7) & (~0x7);
478 }
479 #define HAVE_CFS_SIZE_ROUND
480 #endif
481
482 static inline int cfs_size_round16(int val)
483 {
484         return (val + 0xf) & (~0xf);
485 }
486
487 static inline int cfs_size_round32(int val)
488 {
489         return (val + 0x1f) & (~0x1f);
490 }
491
492 static inline int cfs_size_round0(int val)
493 {
494         if (!val)
495                 return 0;
496         return (val + 1 + 7) & (~0x7);
497 }
498
499 static inline size_t cfs_round_strlen(char *fset)
500 {
501         return (size_t)cfs_size_round((int)strlen(fset) + 1);
502 }
503
504 #define LOGL(var,len,ptr)                                       \
505 do {                                                            \
506         if (var)                                                \
507                 memcpy((char *)ptr, (const char *)var, len);    \
508         ptr += cfs_size_round(len);                             \
509 } while (0)
510
511 #define LOGU(var,len,ptr)                                       \
512 do {                                                            \
513         if (var)                                                \
514                 memcpy((char *)var, (const char *)ptr, len);    \
515         ptr += cfs_size_round(len);                             \
516 } while (0)
517
518 #define LOGL0(var,len,ptr)                              \
519 do {                                                    \
520         if (!len)                                       \
521                 break;                                  \
522         memcpy((char *)ptr, (const char *)var, len);    \
523         *((char *)(ptr) + len) = 0;                     \
524         ptr += cfs_size_round(len + 1);                 \
525 } while (0)
526
527 /**
528  *  Lustre Network Driver types.
529  */
530 enum {
531         /* Only add to these values (i.e. don't ever change or redefine them):
532          * network addresses depend on them... */
533         QSWLND    = 1,
534         SOCKLND   = 2,
535         GMLND     = 3, /* obsolete, keep it so that libcfs_nid2str works */
536         PTLLND    = 4,
537         O2IBLND   = 5,
538         CIBLND    = 6,
539         OPENIBLND = 7,
540         IIBLND    = 8,
541         LOLND     = 9,
542         RALND     = 10,
543         VIBLND    = 11,
544         MXLND     = 12,
545         GNILND    = 13,
546 };
547
548 #endif