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