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