Whamcloud - gitweb
dd046021cd9a1ea31a436d28eadebd173d9c8abf
[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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 LASSERT(cond)                                           \
75 ({                                                              \
76         if (cond)                                               \
77                 ;                                               \
78         else                                                    \
79                 libcfs_assertion_failed( #cond , __FILE__,      \
80                         __FUNCTION__, __LINE__);                \
81 })
82
83 #define LASSERTF(cond, fmt, a...)                                       \
84 ({                                                                      \
85          if (cond)                                                      \
86                  ;                                                      \
87          else {                                                         \
88                  libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,       \
89                                   __FILE__, __FUNCTION__,__LINE__,      \
90                                   "ASSERTION(" #cond ") failed: " fmt,  \
91                                   ## a);                                \
92                  LBUG();                                                \
93          }                                                              \
94 })
95
96
97 #else /* !LASSERT_CHECKED */
98
99 #define LASSERT(cond)                                           \
100 ({                                                              \
101         if (unlikely(!(cond)))                                  \
102                 libcfs_assertion_failed(#cond , __FILE__,       \
103                         __FUNCTION__, __LINE__);                \
104 })
105
106 #define LASSERTF(cond, fmt, a...)                                       \
107 ({                                                                      \
108         if (unlikely(!(cond))) {                                        \
109                 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG,        \
110                                  __FILE__, __FUNCTION__,__LINE__,       \
111                                  "ASSERTION(" #cond ") failed: " fmt,   \
112                                  ## a);                                 \
113                 LBUG();                                                 \
114         }                                                               \
115 })
116
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, fmt...) ((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 atomic_t libcfs_kmemory;
144 /*
145  * Memory
146  */
147 #ifdef LIBCFS_DEBUG
148
149 # define libcfs_kmem_inc(ptr, size)             \
150 do {                                            \
151         atomic_add(size, &libcfs_kmemory);      \
152 } while (0)
153
154 # define libcfs_kmem_dec(ptr, size) do {        \
155         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 #define LIBCFS_VMALLOC_SIZE        16384
164
165 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                 \
166 do {                                                                      \
167         LASSERT(!in_interrupt() ||                                        \
168                (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
169         if (unlikely((size) > LIBCFS_VMALLOC_SIZE))                     \
170                 (ptr) = cfs_alloc_large(size);                            \
171         else                                                              \
172                 (ptr) = cfs_alloc((size), (mask));                        \
173         if (unlikely((ptr) == NULL)) {                                  \
174                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"   \
175                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
176                 CERROR("LNET: %d total bytes allocated by lnet\n",        \
177                        atomic_read(&libcfs_kmemory));                     \
178         } else {                                                          \
179                 libcfs_kmem_inc((ptr), (size));                           \
180                 if (!((mask) & CFS_ALLOC_ZERO))                           \
181                        memset((ptr), 0, (size));                          \
182         }                                                                 \
183         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
184                (int)(size), (ptr), atomic_read (&libcfs_kmemory));        \
185 } while (0)
186
187 #define LIBCFS_ALLOC(ptr, size) \
188         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
189
190 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
191         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
192
193 #define LIBCFS_FREE(ptr, size)                                          \
194 do {                                                                    \
195         int s = (size);                                                 \
196         if (unlikely((ptr) == NULL)) {                                  \
197                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
198                        "%s:%d\n", s, __FILE__, __LINE__);               \
199                 break;                                                  \
200         }                                                               \
201         libcfs_kmem_dec((ptr), s);                                      \
202         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
203                s, (ptr), atomic_read(&libcfs_kmemory));                 \
204         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
205                 cfs_free_large(ptr);                                    \
206         else                                                            \
207                 cfs_free(ptr);                                          \
208 } while (0)
209
210 /******************************************************************************/
211
212 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
213 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
214 #define ___htonl(x) __cpu_to_be32(x)
215 #define ___htons(x) __cpu_to_be16(x)
216 #define ___ntohl(x) __be32_to_cpu(x)
217 #define ___ntohs(x) __be16_to_cpu(x)
218 #define htonl(x) ___htonl(x)
219 #define ntohl(x) ___ntohl(x)
220 #define htons(x) ___htons(x)
221 #define ntohs(x) ___ntohs(x)
222 #endif
223
224 void libcfs_debug_dumpstack(cfs_task_t *tsk);
225 void libcfs_run_upcall(char **argv);
226 void libcfs_run_lbug_upcall(const char * file, const char *fn, const int line);
227 void libcfs_debug_dumplog(void);
228 int libcfs_debug_init(unsigned long bufsize);
229 int libcfs_debug_cleanup(void);
230 int libcfs_debug_clear_buffer(void);
231 int libcfs_debug_mark_buffer(const char *text);
232
233 void libcfs_debug_set_level(unsigned int debug_level);
234
235 #else  /* !__KERNEL__ */
236 # ifdef LIBCFS_DEBUG
237 #  undef NDEBUG
238 #  include <assert.h>
239 #  define LASSERT(e)     assert(e)
240 #  define LASSERTF(cond, args...)                                              \
241 do {                                                                           \
242           if (!(cond))                                                         \
243                 CERROR(args);                                                  \
244           assert(cond);                                                        \
245 } while (0)
246 #  define LBUG()   assert(0)
247 #  ifdef INVARIANT_CHECK
248 #   define LINVRNT(exp) LASSERT(exp)
249 #  else
250 #   define LINVRNT(exp) ((void)sizeof!!(exp))
251 #  endif
252 # else
253 #  define LASSERT(e) ((void)sizeof!!(e))
254 #  define LASSERTF(cond, args...) ((void)sizeof!!(cond))
255 #  define LBUG()   ((void)(0))
256 #  define LINVRNT(exp) ((void)sizeof!!(exp))
257 # endif /* LIBCFS_DEBUG */
258 # define KLASSERT(e) ((void)0)
259 # define printk(format, args...) printf (format, ## args)
260 # ifdef CRAY_XT3                                /* buggy calloc! */
261 #  define LIBCFS_ALLOC(ptr, size)               \
262    do {                                         \
263         (ptr) = malloc(size);                   \
264         memset(ptr, 0, size);                   \
265    } while (0)
266 # else
267 #  define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
268 # endif
269 # define LIBCFS_FREE(a, b) do { free(a); } while (0)
270
271 void libcfs_debug_dumplog(void);
272 int libcfs_debug_init(unsigned long bufsize);
273 int libcfs_debug_cleanup(void);
274
275 /*
276  * Generic compiler-dependent macros required for kernel
277  * build go below this comment. Actual compiler/compiler version
278  * specific implementations come from the above header files
279  */
280
281 #define likely(x)      __builtin_expect(!!(x), 1)
282 #define unlikely(x)    __builtin_expect(!!(x), 0)
283
284 /* !__KERNEL__ */
285 #endif
286
287 /*
288  * compile-time assertions. @cond has to be constant expression.
289  * ISO C Standard:
290  *
291  *        6.8.4.2  The switch statement
292  *
293  *       ....
294  *
295  *       [#3] The expression of each case label shall be  an  integer
296  *       constant   expression  and  no  two  of  the  case  constant
297  *       expressions in the same switch statement shall have the same
298  *       value  after  conversion...
299  *
300  */
301 #define CLASSERT(cond) ({ switch(42) { case (cond): case 0: break; } })
302
303 /* support decl needed both by kernel and liblustre */
304 int         libcfs_isknown_lnd(int type);
305 char       *libcfs_lnd2modname(int type);
306 char       *libcfs_lnd2str(int type);
307 int         libcfs_str2lnd(const char *str);
308 char       *libcfs_net2str(__u32 net);
309 char       *libcfs_nid2str(lnet_nid_t nid);
310 __u32       libcfs_str2net(const char *str);
311 lnet_nid_t  libcfs_str2nid(const char *str);
312 int         libcfs_str2anynid(lnet_nid_t *nid, const char *str);
313 char       *libcfs_id2str(lnet_process_id_t id);
314
315 /* how an LNET NID encodes net:address */
316 #define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
317 #define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
318 #define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
319 /* how net encodes type:number */
320 #define LNET_NETNUM(net)       ((net) & 0xffff)
321 #define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
322 #define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
323
324 /* implication */
325 #define ergo(a, b) (!(a) || (b))
326 /* logical equivalence */
327 #define equi(a, b) (!!(a) == !!(b))
328
329 #ifndef CURRENT_TIME
330 # define CURRENT_TIME time(0)
331 #endif
332
333 /* --------------------------------------------------------------------
334  * Light-weight trace
335  * Support for temporary event tracing with minimal Heisenberg effect.
336  * All stuff about lwt are put in arch/kp30.h
337  * -------------------------------------------------------------------- */
338
339 struct libcfs_device_userstate
340 {
341         int           ldu_memhog_pages;
342         cfs_page_t   *ldu_memhog_root_page;
343 };
344
345 /* what used to be in portals_lib.h */
346 #ifndef MIN
347 # define MIN(a,b) (((a)<(b)) ? (a): (b))
348 #endif
349 #ifndef MAX
350 # define MAX(a,b) (((a)>(b)) ? (a): (b))
351 #endif
352
353 #define MKSTR(ptr) ((ptr))? (ptr) : ""
354
355 static inline int size_round4 (int val)
356 {
357         return (val + 3) & (~0x3);
358 }
359
360 static inline int size_round (int val)
361 {
362         return (val + 7) & (~0x7);
363 }
364
365 static inline int size_round16(int val)
366 {
367         return (val + 0xf) & (~0xf);
368 }
369
370 static inline int size_round32(int val)
371 {
372         return (val + 0x1f) & (~0x1f);
373 }
374
375 static inline int size_round0(int val)
376 {
377         if (!val)
378                 return 0;
379         return (val + 1 + 7) & (~0x7);
380 }
381
382 static inline size_t round_strlen(char *fset)
383 {
384         return (size_t)size_round((int)strlen(fset) + 1);
385 }
386
387 #define LOGL(var,len,ptr)                                       \
388 do {                                                            \
389         if (var)                                                \
390                 memcpy((char *)ptr, (const char *)var, len);    \
391         ptr += size_round(len);                                 \
392 } while (0)
393
394 #define LOGU(var,len,ptr)                                       \
395 do {                                                            \
396         if (var)                                                \
397                 memcpy((char *)var, (const char *)ptr, len);    \
398         ptr += size_round(len);                                 \
399 } while (0)
400
401 #define LOGL0(var,len,ptr)                              \
402 do {                                                    \
403         if (!len)                                       \
404                 break;                                  \
405         memcpy((char *)ptr, (const char *)var, len);    \
406         *((char *)(ptr) + len) = 0;                     \
407         ptr += size_round(len + 1);                     \
408 } while (0)
409
410 /**
411  *  Lustre Network Driver types.
412  */
413 enum {
414         /* Only add to these values (i.e. don't ever change or redefine them):
415          * network addresses depend on them... */
416         QSWLND    = 1,
417         SOCKLND   = 2,
418         GMLND     = 3,
419         PTLLND    = 4,
420         O2IBLND   = 5,
421         CIBLND    = 6,
422         OPENIBLND = 7,
423         IIBLND    = 8,
424         LOLND     = 9,
425         RALND     = 10,
426         VIBLND    = 11,
427         MXLND     = 12,
428 };
429
430 #endif