Whamcloud - gitweb
LU-56 libcfs: more common APIs in libcfs
[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)             \
143 do {                                            \
144         cfs_atomic_sub(size, &libcfs_kmemory);  \
145 } while (0)
146
147 # define libcfs_kmem_read()                     \
148         cfs_atomic_read(&libcfs_kmemory)
149
150 #else
151 # define libcfs_kmem_inc(ptr, size) do {} while (0)
152 # define libcfs_kmem_dec(ptr, size) do {} while (0)
153 # define libcfs_kmem_read()     (0)
154 #endif /* LIBCFS_DEBUG */
155
156 #ifndef LIBCFS_VMALLOC_SIZE
157 #define LIBCFS_VMALLOC_SIZE        (2 << CFS_PAGE_SHIFT) /* 2 pages */
158 #endif
159
160 #define LIBCFS_ALLOC_PRE(size, mask)                                        \
161 do {                                                                        \
162         LASSERT(!cfs_in_interrupt() ||                                      \
163                 ((size) <= LIBCFS_VMALLOC_SIZE &&                           \
164                  ((mask) & CFS_ALLOC_ATOMIC)) != 0);                        \
165 } while (0)
166
167 #define LIBCFS_ALLOC_POST(ptr, size)                                        \
168 do {                                                                        \
169         if (unlikely((ptr) == NULL)) {                                      \
170                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"     \
171                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));  \
172                 CERROR("LNET: %d total bytes allocated by lnet\n",          \
173                        libcfs_kmem_read());                                 \
174         } else {                                                            \
175                 memset((ptr), 0, (size));                                   \
176                 libcfs_kmem_inc((ptr), (size));                             \
177                 CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n",  \
178                        (int)(size), (ptr), libcfs_kmem_read());             \
179         }                                                                   \
180 } while (0)
181
182 /**
183  * allocate memory with GFP flags @mask
184  */
185 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                   \
186 do {                                                                        \
187         LIBCFS_ALLOC_PRE((size), (mask));                                   \
188         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
189                 cfs_alloc((size), (mask)) : cfs_alloc_large(size);          \
190         LIBCFS_ALLOC_POST((ptr), (size));                                   \
191 } while (0)
192
193 /**
194  * default allocator
195  */
196 #define LIBCFS_ALLOC(ptr, size) \
197         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
198
199 /**
200  * non-sleeping allocator
201  */
202 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
203         LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
204
205 /**
206  * allocate memory for specified CPU partition
207  *   \a cptab != NULL, \a cpt is CPU partition id of \a cptab
208  *   \a cptab == NULL, \a cpt is HW NUMA node id
209  */
210 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)                   \
211 do {                                                                        \
212         LIBCFS_ALLOC_PRE((size), (mask));                                   \
213         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
214                 cfs_cpt_malloc((cptab), (cpt), (size), (mask)) :            \
215                 cfs_cpt_vmalloc((cptab), (cpt), (size));                    \
216         LIBCFS_ALLOC_POST((ptr), (size));                                   \
217 } while (0)
218
219 /** default numa allocator */
220 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                             \
221         LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, CFS_ALLOC_IO)
222
223 #define LIBCFS_FREE(ptr, size)                                          \
224 do {                                                                    \
225         int s = (size);                                                 \
226         if (unlikely((ptr) == NULL)) {                                  \
227                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
228                        "%s:%d\n", s, __FILE__, __LINE__);               \
229                 break;                                                  \
230         }                                                               \
231         libcfs_kmem_dec((ptr), s);                                      \
232         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
233                s, (ptr), libcfs_kmem_read());                           \
234         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
235                 cfs_free_large(ptr);                                    \
236         else                                                            \
237                 cfs_free(ptr);                                          \
238 } while (0)
239
240 /******************************************************************************/
241
242 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
243 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
244 #define ___htonl(x) __cpu_to_be32(x)
245 #define ___htons(x) __cpu_to_be16(x)
246 #define ___ntohl(x) __be32_to_cpu(x)
247 #define ___ntohs(x) __be16_to_cpu(x)
248 #define htonl(x) ___htonl(x)
249 #define ntohl(x) ___ntohl(x)
250 #define htons(x) ___htons(x)
251 #define ntohs(x) ___ntohs(x)
252 #endif
253
254 void libcfs_debug_dumpstack(cfs_task_t *tsk);
255 void libcfs_run_upcall(char **argv);
256 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
257 void libcfs_debug_dumplog(void);
258 int libcfs_debug_init(unsigned long bufsize);
259 int libcfs_debug_cleanup(void);
260 int libcfs_debug_clear_buffer(void);
261 int libcfs_debug_mark_buffer(const char *text);
262
263 void libcfs_debug_set_level(unsigned int debug_level);
264
265 #else  /* !__KERNEL__ */
266 # ifdef LIBCFS_DEBUG
267 #  undef NDEBUG
268 #  include <assert.h>
269 #  define LASSERT(e)     assert(e)
270 #  define LASSERTF(cond, ...)                                                  \
271 do {                                                                           \
272           if (!(cond))                                                         \
273                 CERROR(__VA_ARGS__);                                           \
274           assert(cond);                                                        \
275 } while (0)
276 #  define LBUG()   assert(0)
277 #  ifdef INVARIANT_CHECK
278 #   define LINVRNT(exp) LASSERT(exp)
279 #  else
280 #   define LINVRNT(exp) ((void)sizeof!!(exp))
281 #  endif
282 # else
283 #  define LASSERT(e) ((void)sizeof!!(e))
284 #  define LASSERTF(cond, ...) ((void)sizeof!!(cond))
285 #  define LBUG()   ((void)(0))
286 #  define LINVRNT(exp) ((void)sizeof!!(exp))
287 # endif /* LIBCFS_DEBUG */
288 # define KLASSERT(e) ((void)0)
289 # define printk printf
290 # ifdef CRAY_XT3                                /* buggy calloc! */
291 #  define LIBCFS_ALLOC_GFP(ptr, size, mask)     \
292    do {                                         \
293         (ptr) = malloc(size);                   \
294         memset(ptr, 0, size);                   \
295    } while (0)
296 # else
297 #  define LIBCFS_ALLOC_GFP(ptr, size, mask)     \
298    do {                                         \
299         (ptr) = calloc(1, size);                \
300    } while (0)
301 # endif
302 # define LIBCFS_FREE(ptr, size) do { free(ptr); } while((size) - (size))
303 # define LIBCFS_ALLOC(ptr, size)                                \
304          LIBCFS_ALLOC_GFP(ptr, size, 0)
305 # define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)      \
306          LIBCFS_ALLOC(ptr, size)
307 # define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                \
308          LIBCFS_ALLOC(ptr, size)
309
310 void libcfs_debug_dumplog(void);
311 int libcfs_debug_init(unsigned long bufsize);
312 int libcfs_debug_cleanup(void);
313
314 #define libcfs_debug_dumpstack(tsk)     ((void)0)
315
316 /*
317  * Generic compiler-dependent macros required for kernel
318  * build go below this comment. Actual compiler/compiler version
319  * specific implementations come from the above header files
320  */
321 #ifdef __GNUC__
322 #define likely(x)      __builtin_expect(!!(x), 1)
323 #define unlikely(x)    __builtin_expect(!!(x), 0)
324 #else
325 #define likely(x)       (!!(x))
326 #define unlikely(x)     (!!(x))
327 #endif
328 /* !__KERNEL__ */
329 #endif
330
331 /*
332  * allocate per-cpu-partition data, returned value is an array of pointers,
333  * variable can be indexed by CPU ID.
334  *      cptable != NULL: size of array is number of CPU partitions
335  *      cptable == NULL: size of array is number of HW cores
336  */
337 void *cfs_percpt_alloc(struct cfs_cpt_table *cptab, unsigned int size);
338 /*
339  * destory per-cpu-partition variable
340  */
341 void  cfs_percpt_free(void *vars);
342 int   cfs_percpt_number(void *vars);
343 void *cfs_percpt_current(void *vars);
344 void *cfs_percpt_index(void *vars, int idx);
345
346 #define cfs_percpt_for_each(var, i, vars)               \
347         for (i = 0; i < cfs_percpt_number(vars) &&      \
348                     ((var) = (vars)[i]) != NULL; i++)
349
350 /*
351  * allocate a variable array, returned value is an array of pointers.
352  * Caller can specify length of array by count.
353  */
354 void *cfs_array_alloc(int count, unsigned int size);
355 void  cfs_array_free(void *vars);
356
357 #define LASSERT_ATOMIC_ENABLED          (1)
358
359 #if LASSERT_ATOMIC_ENABLED
360
361 /** assert value of @a is equal to @v */
362 #define LASSERT_ATOMIC_EQ(a, v)                                 \
363 do {                                                            \
364         LASSERTF(cfs_atomic_read(a) == v,                       \
365                  "value: %d\n", cfs_atomic_read((a)));          \
366 } while (0)
367
368 /** assert value of @a is unequal to @v */
369 #define LASSERT_ATOMIC_NE(a, v)                                 \
370 do {                                                            \
371         LASSERTF(cfs_atomic_read(a) != v,                       \
372                  "value: %d\n", cfs_atomic_read((a)));          \
373 } while (0)
374
375 /** assert value of @a is little than @v */
376 #define LASSERT_ATOMIC_LT(a, v)                                 \
377 do {                                                            \
378         LASSERTF(cfs_atomic_read(a) < v,                        \
379                  "value: %d\n", cfs_atomic_read((a)));          \
380 } while (0)
381
382 /** assert value of @a is little/equal to @v */
383 #define LASSERT_ATOMIC_LE(a, v)                                 \
384 do {                                                            \
385         LASSERTF(cfs_atomic_read(a) <= v,                       \
386                  "value: %d\n", cfs_atomic_read((a)));          \
387 } while (0)
388
389 /** assert value of @a is great than @v */
390 #define LASSERT_ATOMIC_GT(a, v)                                 \
391 do {                                                            \
392         LASSERTF(cfs_atomic_read(a) > v,                        \
393                  "value: %d\n", cfs_atomic_read((a)));          \
394 } while (0)
395
396 /** assert value of @a is great/equal to @v */
397 #define LASSERT_ATOMIC_GE(a, v)                                 \
398 do {                                                            \
399         LASSERTF(cfs_atomic_read(a) >= v,                       \
400                  "value: %d\n", cfs_atomic_read((a)));          \
401 } while (0)
402
403 /** assert value of @a is great than @v1 and little than @v2 */
404 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                         \
405 do {                                                            \
406         int __v = cfs_atomic_read(a);                           \
407         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
408 } while (0)
409
410 /** assert value of @a is great than @v1 and little/equal to @v2 */
411 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                         \
412 do {                                                            \
413         int __v = cfs_atomic_read(a);                           \
414         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
415 } while (0)
416
417 /** assert value of @a is great/equal to @v1 and little than @v2 */
418 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                         \
419 do {                                                            \
420         int __v = cfs_atomic_read(a);                           \
421         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
422 } while (0)
423
424 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
425 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                         \
426 do {                                                            \
427         int __v = cfs_atomic_read(a);                           \
428         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
429 } while (0)
430
431 #else /* !LASSERT_ATOMIC_ENABLED */
432
433 #define LASSERT_ATOMIC_EQ(a, v)                 do {} while (0)
434 #define LASSERT_ATOMIC_NE(a, v)                 do {} while (0)
435 #define LASSERT_ATOMIC_LT(a, v)                 do {} while (0)
436 #define LASSERT_ATOMIC_LE(a, v)                 do {} while (0)
437 #define LASSERT_ATOMIC_GT(a, v)                 do {} while (0)
438 #define LASSERT_ATOMIC_GE(a, v)                 do {} while (0)
439 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)         do {} while (0)
440 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)         do {} while (0)
441 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)         do {} while (0)
442 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)         do {} while (0)
443
444 #endif /* LASSERT_ATOMIC_ENABLED */
445
446 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
447 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
448
449 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
450 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
451
452 /*
453  * percpu partition lock
454  *
455  * There are some use-cases like this in Lustre:
456  * . each CPU partition has it's own private data which is frequently changed,
457  *   and mostly by the local CPU partition.
458  * . all CPU partitions share some global data, these data are rarely changed.
459  *
460  * LNet is typical example.
461  * CPU partition lock is designed for this kind of use-cases:
462  * . each CPU partition has it's own private lock
463  * . change on private data just needs to take the private lock
464  * . read on shared data just needs to take _any_ of private locks
465  * . change on shared data needs to take _all_ private locks,
466  *   which is slow and should be really rare.
467  */
468
469 enum {
470         CFS_PERCPT_LOCK_EX      = -1, /* negative */
471 };
472
473 #ifdef __KERNEL__
474
475 struct cfs_percpt_lock {
476         /* cpu-partition-table for this lock */
477         struct cfs_cpt_table    *pcl_cptab;
478         /* exclusively locked */
479         unsigned int            pcl_locked;
480         /* private lock table */
481         cfs_spinlock_t          **pcl_locks;
482 };
483
484 /* return number of private locks */
485 static inline int
486 cfs_percpt_lock_num(struct cfs_percpt_lock *pcl)
487 {
488         return cfs_cpt_number(pcl->pcl_cptab);
489 }
490
491 #else /* !__KERNEL__ */
492
493 # ifdef HAVE_LIBPTHREAD
494
495 struct cfs_percpt_lock {
496         pthread_mutex_t         pcl_mutex;
497 };
498
499 # else /* !HAVE_LIBPTHREAD */
500 #define CFS_PERCPT_LOCK_MAGIC          0xbabecafe;
501
502 struct cfs_percpt_lock {
503         int                     pcl_lock;
504 };
505 # endif /* HAVE_LIBPTHREAD */
506 # define cfs_percpt_lock_num(pcl)        1
507 #endif /* __KERNEL__ */
508
509 /*
510  * create a cpu-partition lock based on CPU partition table \a cptab,
511  * each private lock has extra \a psize bytes padding data
512  */
513 struct cfs_percpt_lock *cfs_percpt_lock_alloc(struct cfs_cpt_table *cptab);
514 /* destroy a cpu-partition lock */
515 void cfs_percpt_lock_free(struct cfs_percpt_lock *pcl);
516
517 /* lock private lock \a index of \a pcl */
518 void cfs_percpt_lock(struct cfs_percpt_lock *pcl, int index);
519 /* unlock private lock \a index of \a pcl */
520 void cfs_percpt_unlock(struct cfs_percpt_lock *pcl, int index);
521 /* create percpt (atomic) refcount based on @cptab */
522 cfs_atomic_t **cfs_percpt_atomic_alloc(struct cfs_cpt_table *cptab, int val);
523 /* destroy percpt refcount */
524 void cfs_percpt_atomic_free(cfs_atomic_t **refs);
525 /* return sum of all percpu refs */
526 int cfs_percpt_atomic_summary(cfs_atomic_t **refs);
527
528
529 /** Compile-time assertion.
530
531  * Check an invariant described by a constant expression at compile time by
532  * forcing a compiler error if it does not hold.  \a cond must be a constant
533  * expression as defined by the ISO C Standard:
534  *
535  *       6.8.4.2  The switch statement
536  *       ....
537  *       [#3] The expression of each case label shall be  an  integer
538  *       constant   expression  and  no  two  of  the  case  constant
539  *       expressions in the same switch statement shall have the same
540  *       value  after  conversion...
541  *
542  */
543 #define CLASSERT(cond) do {switch(42) {case (cond): case 0: break;}} while (0)
544
545 /* support decl needed both by kernel and liblustre */
546 int         libcfs_isknown_lnd(int type);
547 char       *libcfs_lnd2modname(int type);
548 char       *libcfs_lnd2str(int type);
549 int         libcfs_str2lnd(const char *str);
550 char       *libcfs_net2str(__u32 net);
551 char       *libcfs_nid2str(lnet_nid_t nid);
552 __u32       libcfs_str2net(const char *str);
553 lnet_nid_t  libcfs_str2nid(const char *str);
554 int         libcfs_str2anynid(lnet_nid_t *nid, const char *str);
555 char       *libcfs_id2str(lnet_process_id_t id);
556 void        cfs_free_nidlist(cfs_list_t *list);
557 int         cfs_parse_nidlist(char *str, int len, cfs_list_t *list);
558 int         cfs_match_nid(lnet_nid_t nid, cfs_list_t *list);
559
560 /** \addtogroup lnet_addr
561  * @{ */
562 /* how an LNET NID encodes net:address */
563 /** extract the address part of an lnet_nid_t */
564 #define LNET_NIDADDR(nid)      ((__u32)((nid) & 0xffffffff))
565 /** extract the network part of an lnet_nid_t */
566 #define LNET_NIDNET(nid)       ((__u32)(((nid) >> 32)) & 0xffffffff)
567 /** make an lnet_nid_t from a network part and an address part */
568 #define LNET_MKNID(net,addr)   ((((__u64)(net))<<32)|((__u64)(addr)))
569 /* how net encodes type:number */
570 #define LNET_NETNUM(net)       ((net) & 0xffff)
571 #define LNET_NETTYP(net)       (((net) >> 16) & 0xffff)
572 #define LNET_MKNET(typ,num)    ((((__u32)(typ))<<16)|((__u32)(num)))
573 /** @} lnet_addr */
574
575 /* max value for numeric network address */
576 #define MAX_NUMERIC_VALUE 0xffffffff
577
578 /* implication */
579 #define ergo(a, b) (!(a) || (b))
580 /* logical equivalence */
581 #define equi(a, b) (!!(a) == !!(b))
582
583 #ifndef CFS_CURRENT_TIME
584 # define CFS_CURRENT_TIME time(0)
585 #endif
586
587 /* --------------------------------------------------------------------
588  * Light-weight trace
589  * Support for temporary event tracing with minimal Heisenberg effect.
590  * All stuff about lwt are put in arch/kp30.h
591  * -------------------------------------------------------------------- */
592
593 struct libcfs_device_userstate
594 {
595         int           ldu_memhog_pages;
596         cfs_page_t   *ldu_memhog_root_page;
597 };
598
599 /* what used to be in portals_lib.h */
600 #ifndef MIN
601 # define MIN(a,b) (((a)<(b)) ? (a): (b))
602 #endif
603 #ifndef MAX
604 # define MAX(a,b) (((a)>(b)) ? (a): (b))
605 #endif
606
607 #define MKSTR(ptr) ((ptr))? (ptr) : ""
608
609 static inline int cfs_size_round4 (int val)
610 {
611         return (val + 3) & (~0x3);
612 }
613
614 #ifndef HAVE_CFS_SIZE_ROUND
615 static inline int cfs_size_round (int val)
616 {
617         return (val + 7) & (~0x7);
618 }
619 #define HAVE_CFS_SIZE_ROUND
620 #endif
621
622 static inline int cfs_size_round16(int val)
623 {
624         return (val + 0xf) & (~0xf);
625 }
626
627 static inline int cfs_size_round32(int val)
628 {
629         return (val + 0x1f) & (~0x1f);
630 }
631
632 static inline int cfs_size_round0(int val)
633 {
634         if (!val)
635                 return 0;
636         return (val + 1 + 7) & (~0x7);
637 }
638
639 static inline size_t cfs_round_strlen(char *fset)
640 {
641         return (size_t)cfs_size_round((int)strlen(fset) + 1);
642 }
643
644 /* roundup \a val to power2 */
645 static inline unsigned int cfs_power2_roundup(unsigned int val)
646 {
647         if (val != LOWEST_BIT_SET(val)) { /* not a power of 2 already */
648                 do {
649                         val &= ~LOWEST_BIT_SET(val);
650                 } while (val != LOWEST_BIT_SET(val));
651                 /* ...and round up */
652                 val <<= 1;
653         }
654         return val;
655 }
656
657 #define LOGL(var,len,ptr)                                       \
658 do {                                                            \
659         if (var)                                                \
660                 memcpy((char *)ptr, (const char *)var, len);    \
661         ptr += cfs_size_round(len);                             \
662 } while (0)
663
664 #define LOGU(var,len,ptr)                                       \
665 do {                                                            \
666         if (var)                                                \
667                 memcpy((char *)var, (const char *)ptr, len);    \
668         ptr += cfs_size_round(len);                             \
669 } while (0)
670
671 #define LOGL0(var,len,ptr)                              \
672 do {                                                    \
673         if (!len)                                       \
674                 break;                                  \
675         memcpy((char *)ptr, (const char *)var, len);    \
676         *((char *)(ptr) + len) = 0;                     \
677         ptr += cfs_size_round(len + 1);                 \
678 } while (0)
679
680 /**
681  *  Lustre Network Driver types.
682  */
683 enum {
684         /* Only add to these values (i.e. don't ever change or redefine them):
685          * network addresses depend on them... */
686         QSWLND    = 1,
687         SOCKLND   = 2,
688         GMLND     = 3, /* obsolete, keep it so that libcfs_nid2str works */
689         PTLLND    = 4,
690         O2IBLND   = 5,
691         CIBLND    = 6,
692         OPENIBLND = 7,
693         IIBLND    = 8,
694         LOLND     = 9,
695         RALND     = 10,
696         VIBLND    = 11,
697         MXLND     = 12,
698         GNILND    = 13,
699 };
700
701 #endif