Whamcloud - gitweb
LU-8648 all: remove all Sun license and URL references
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/libcfs_private.h
33  *
34  * Various defines for libcfs.
35  *
36  */
37
38 #ifndef __LIBCFS_PRIVATE_H__
39 #define __LIBCFS_PRIVATE_H__
40
41 #ifndef DEBUG_SUBSYSTEM
42 # define DEBUG_SUBSYSTEM S_UNDEFINED
43 #endif
44
45 #ifdef __KERNEL__
46
47 #ifdef LIBCFS_DEBUG
48
49 /*
50  * When this is on, LASSERT macro includes check for assignment used instead
51  * of equality check, but doesn't have unlikely(). Turn this on from time to
52  * time to make test-builds. This shouldn't be on for production release.
53  */
54 #define LASSERT_CHECKED (0)
55
56 #if LASSERT_CHECKED
57 /*
58  * Assertion.
59  *
60  * Strange construction with empty "then" clause is used to trigger compiler
61  * warnings on the assertions of the form LASSERT(a = b);
62  *
63  * "warning: suggest parentheses around assignment used as truth value"
64  *
65  * requires -Wall. Unfortunately this rules out use of likely/unlikely.
66  */
67 #define LASSERTF(cond, fmt, ...)                                        \
68 do {                                                                    \
69         if (cond)                                                       \
70                 ;                                                       \
71         else {                                                          \
72                 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
73                 libcfs_debug_msg(&__msg_data,                           \
74                                  "ASSERTION( %s ) failed: " fmt, #cond, \
75                                  ## __VA_ARGS__);                       \
76                 lbug_with_loc(&__msg_data);                             \
77         }                                                               \
78 } while (0)
79
80 #define LASSERT(cond) LASSERTF(cond, "\n")
81
82 #else /* !LASSERT_CHECKED */
83
84 #define LASSERTF(cond, fmt, ...)                                        \
85 do {                                                                    \
86         if (unlikely(!(cond))) {                                        \
87                 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL);  \
88                 libcfs_debug_msg(&__msg_data,                           \
89                                  "ASSERTION( %s ) failed: " fmt, #cond, \
90                                  ## __VA_ARGS__);                       \
91                 lbug_with_loc(&__msg_data);                             \
92         }                                                               \
93 } while (0)
94
95 #define LASSERT(cond) LASSERTF(cond, "\n")
96 #endif /* !LASSERT_CHECKED */
97 #else /* !LIBCFS_DEBUG */
98 /* sizeof is to use expression without evaluating it. */
99 # define LASSERT(e) ((void)sizeof!!(e))
100 # define LASSERTF(cond, ...) ((void)sizeof!!(cond))
101 #endif /* !LIBCFS_DEBUG */
102
103 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
104 /**
105  * This is for more expensive checks that one doesn't want to be enabled all
106  * the time. LINVRNT() has to be explicitly enabled by --enable-invariants
107  * configure option.
108  */
109 # define LINVRNT(exp) LASSERT(exp)
110 #else
111 # define LINVRNT(exp) ((void)sizeof!!(exp))
112 #endif
113
114 #define KLASSERT(e) LASSERT(e)
115
116 void lbug_with_loc(struct libcfs_debug_msg_data *) __attribute__((noreturn));
117
118 #define LBUG()                                                          \
119 do {                                                                    \
120         LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);             \
121         lbug_with_loc(&msgdata);                                        \
122 } while(0)
123
124 extern atomic_t libcfs_kmemory;
125 /*
126  * Memory
127  */
128 #ifdef LIBCFS_DEBUG
129
130 # define libcfs_kmem_inc(ptr, size)             \
131 do {                                            \
132         atomic_add(size, &libcfs_kmemory);      \
133 } while (0)
134
135 # define libcfs_kmem_dec(ptr, size)             \
136 do {                                            \
137         atomic_sub(size, &libcfs_kmemory);      \
138 } while (0)
139
140 # define libcfs_kmem_read()                     \
141         atomic_read(&libcfs_kmemory)
142
143 #else
144 # define libcfs_kmem_inc(ptr, size) do {} while (0)
145 # define libcfs_kmem_dec(ptr, size) do {} while (0)
146 # define libcfs_kmem_read()     (0)
147 #endif /* LIBCFS_DEBUG */
148
149 #ifndef LIBCFS_VMALLOC_SIZE
150 #define LIBCFS_VMALLOC_SIZE        (2 << PAGE_SHIFT) /* 2 pages */
151 #endif
152
153 #define LIBCFS_ALLOC_PRE(size, mask)                                        \
154 do {                                                                        \
155         LASSERT(!in_interrupt() ||                                          \
156                 ((size) <= LIBCFS_VMALLOC_SIZE &&                           \
157                  ((mask) & GFP_ATOMIC)) != 0);                      \
158 } while (0)
159
160 #define LIBCFS_ALLOC_POST(ptr, size)                                        \
161 do {                                                                        \
162         if (unlikely((ptr) == NULL)) {                                      \
163                 CERROR("LNET: out of memory at %s:%d (tried to alloc '"     \
164                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));  \
165                 CERROR("LNET: %d total bytes allocated by lnet\n",          \
166                        libcfs_kmem_read());                                 \
167         } else {                                                            \
168                 libcfs_kmem_inc((ptr), (size));                             \
169                 CDEBUG(D_MALLOC, "alloc '" #ptr "': %d at %p (tot %d).\n",  \
170                        (int)(size), (ptr), libcfs_kmem_read());             \
171         }                                                                   \
172 } while (0)
173
174 /**
175  * allocate memory with GFP flags @mask
176  * The allocated memory is zeroed-out.
177  */
178 #define LIBCFS_ALLOC_GFP(ptr, size, mask)                                   \
179 do {                                                                        \
180         LIBCFS_ALLOC_PRE((size), (mask));                                   \
181         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
182                 kzalloc((size), (mask)) : vzalloc(size);                    \
183         LIBCFS_ALLOC_POST((ptr), (size));                                   \
184 } while (0)
185
186 /**
187  * default allocator
188  */
189 #define LIBCFS_ALLOC(ptr, size) \
190         LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
191
192 /**
193  * non-sleeping allocator
194  */
195 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
196         LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
197
198 /**
199  * allocate memory for specified CPU partition
200  *   \a cptab != NULL, \a cpt is CPU partition id of \a cptab
201  *   \a cptab == NULL, \a cpt is HW NUMA node id
202  * The allocated memory is zeroed-out.
203  */
204 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)                   \
205 do {                                                                        \
206         LIBCFS_ALLOC_PRE((size), (mask));                                   \
207         (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ?                             \
208                 cfs_cpt_malloc((cptab), (cpt), (size), (mask) | __GFP_ZERO) : \
209                 cfs_cpt_vzalloc((cptab), (cpt), (size));                    \
210         LIBCFS_ALLOC_POST((ptr), (size));                                   \
211 } while (0)
212
213 /** default numa allocator */
214 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                             \
215         LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
216
217 #define LIBCFS_FREE(ptr, size)                                          \
218 do {                                                                    \
219         int s = (size);                                                 \
220         if (unlikely((ptr) == NULL)) {                                  \
221                 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at "    \
222                        "%s:%d\n", s, __FILE__, __LINE__);               \
223                 break;                                                  \
224         }                                                               \
225         libcfs_kmem_dec((ptr), s);                                      \
226         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
227                s, (ptr), libcfs_kmem_read());                           \
228         if (unlikely(s > LIBCFS_VMALLOC_SIZE))                          \
229                 vfree(ptr);                                             \
230         else                                                            \
231                 kfree(ptr);                                             \
232 } while (0)
233
234 /******************************************************************************/
235
236 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
237 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
238 #define ___htonl(x) __cpu_to_be32(x)
239 #define ___htons(x) __cpu_to_be16(x)
240 #define ___ntohl(x) __be32_to_cpu(x)
241 #define ___ntohs(x) __be16_to_cpu(x)
242 #define htonl(x) ___htonl(x)
243 #define ntohl(x) ___ntohl(x)
244 #define htons(x) ___htons(x)
245 #define ntohs(x) ___ntohs(x)
246 #endif
247
248 void libcfs_debug_dumpstack(struct task_struct *tsk);
249 void libcfs_run_upcall(char **argv);
250 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
251 void libcfs_debug_dumplog(void);
252 int libcfs_debug_init(unsigned long bufsize);
253 int libcfs_debug_cleanup(void);
254 int libcfs_debug_clear_buffer(void);
255 int libcfs_debug_mark_buffer(const char *text);
256
257 #else  /* !__KERNEL__ */
258 # ifdef LIBCFS_DEBUG
259 #  undef NDEBUG
260 #  include <assert.h>
261 #  define LASSERT(e)     assert(e)
262 #  define LASSERTF(cond, ...)                                                  \
263 do {                                                                           \
264           if (!(cond))                                                         \
265                 CERROR(__VA_ARGS__);                                           \
266           assert(cond);                                                        \
267 } while (0)
268 #  define LBUG()   assert(0)
269 #  ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
270 #   define LINVRNT(exp) LASSERT(exp)
271 #  else
272 #   define LINVRNT(exp) ((void)sizeof!!(exp))
273 #  endif
274 # else
275 #  define LASSERT(e) ((void)sizeof!!(e))
276 #  define LASSERTF(cond, ...) ((void)sizeof!!(cond))
277 #  define LBUG()   ((void)(0))
278 #  define LINVRNT(exp) ((void)sizeof!!(exp))
279 # endif /* LIBCFS_DEBUG */
280 # define KLASSERT(e) ((void)0)
281 # define printk printf
282 #define LIBCFS_ALLOC_GFP(ptr, size, mask)       \
283 do {                                            \
284         (ptr) = calloc(1, size);                \
285 } while (0)
286 # define LIBCFS_FREE(ptr, size) do { free(ptr); } while((size) - (size))
287 # define LIBCFS_ALLOC(ptr, size)                                \
288          LIBCFS_ALLOC_GFP(ptr, size, 0)
289 # define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask)      \
290          LIBCFS_ALLOC(ptr, size)
291 # define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size)                \
292          LIBCFS_ALLOC(ptr, size)
293
294 void libcfs_debug_dumplog(void);
295 int libcfs_debug_init(unsigned long bufsize);
296 int libcfs_debug_cleanup(void);
297
298 #define libcfs_debug_dumpstack(tsk)     ((void)0)
299
300 /*
301  * Generic compiler-dependent macros required for kernel
302  * build go below this comment. Actual compiler/compiler version
303  * specific implementations come from the above header files
304  */
305 #define likely(x)      __builtin_expect(!!(x), 1)
306 #define unlikely(x)    __builtin_expect(!!(x), 0)
307 /* !__KERNEL__ */
308 #endif
309
310 /*
311  * allocate a variable array, returned value is an array of pointers.
312  * Caller can specify length of array by count.
313  */
314 void *cfs_array_alloc(int count, unsigned int size);
315 void  cfs_array_free(void *vars);
316
317 #define LASSERT_ATOMIC_ENABLED          (1)
318
319 #if LASSERT_ATOMIC_ENABLED
320
321 /** assert value of @a is equal to @v */
322 #define LASSERT_ATOMIC_EQ(a, v)                                 \
323 do {                                                            \
324         LASSERTF(atomic_read(a) == v,                       \
325                  "value: %d\n", atomic_read((a)));          \
326 } while (0)
327
328 /** assert value of @a is unequal to @v */
329 #define LASSERT_ATOMIC_NE(a, v)                                 \
330 do {                                                            \
331         LASSERTF(atomic_read(a) != v,                       \
332                  "value: %d\n", atomic_read((a)));          \
333 } while (0)
334
335 /** assert value of @a is little than @v */
336 #define LASSERT_ATOMIC_LT(a, v)                                 \
337 do {                                                            \
338         LASSERTF(atomic_read(a) < v,                        \
339                  "value: %d\n", atomic_read((a)));          \
340 } while (0)
341
342 /** assert value of @a is little/equal to @v */
343 #define LASSERT_ATOMIC_LE(a, v)                                 \
344 do {                                                            \
345         LASSERTF(atomic_read(a) <= v,                       \
346                  "value: %d\n", atomic_read((a)));          \
347 } while (0)
348
349 /** assert value of @a is great than @v */
350 #define LASSERT_ATOMIC_GT(a, v)                                 \
351 do {                                                            \
352         LASSERTF(atomic_read(a) > v,                        \
353                  "value: %d\n", atomic_read((a)));          \
354 } while (0)
355
356 /** assert value of @a is great/equal to @v */
357 #define LASSERT_ATOMIC_GE(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 great than @v1 and little than @v2 */
364 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)                         \
365 do {                                                            \
366         int __v = atomic_read(a);                           \
367         LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v);     \
368 } while (0)
369
370 /** assert value of @a is great than @v1 and little/equal to @v2 */
371 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)                         \
372 do {                                                            \
373         int __v = atomic_read(a);                           \
374         LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v);    \
375 } while (0)
376
377 /** assert value of @a is great/equal to @v1 and little than @v2 */
378 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)                         \
379 do {                                                            \
380         int __v = atomic_read(a);                           \
381         LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v);    \
382 } while (0)
383
384 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
385 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)                         \
386 do {                                                            \
387         int __v = atomic_read(a);                           \
388         LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v);   \
389 } while (0)
390
391 #else /* !LASSERT_ATOMIC_ENABLED */
392
393 #define LASSERT_ATOMIC_EQ(a, v)                 do {} while (0)
394 #define LASSERT_ATOMIC_NE(a, v)                 do {} while (0)
395 #define LASSERT_ATOMIC_LT(a, v)                 do {} while (0)
396 #define LASSERT_ATOMIC_LE(a, v)                 do {} while (0)
397 #define LASSERT_ATOMIC_GT(a, v)                 do {} while (0)
398 #define LASSERT_ATOMIC_GE(a, v)                 do {} while (0)
399 #define LASSERT_ATOMIC_GT_LT(a, v1, v2)         do {} while (0)
400 #define LASSERT_ATOMIC_GT_LE(a, v1, v2)         do {} while (0)
401 #define LASSERT_ATOMIC_GE_LT(a, v1, v2)         do {} while (0)
402 #define LASSERT_ATOMIC_GE_LE(a, v1, v2)         do {} while (0)
403
404 #endif /* LASSERT_ATOMIC_ENABLED */
405
406 #define LASSERT_ATOMIC_ZERO(a)                  LASSERT_ATOMIC_EQ(a, 0)
407 #define LASSERT_ATOMIC_POS(a)                   LASSERT_ATOMIC_GT(a, 0)
408
409 #define CFS_ALLOC_PTR(ptr)      LIBCFS_ALLOC(ptr, sizeof (*(ptr)));
410 #define CFS_FREE_PTR(ptr)       LIBCFS_FREE(ptr, sizeof (*(ptr)));
411
412 /** Compile-time assertion.
413
414  * Check an invariant described by a constant expression at compile time by
415  * forcing a compiler error if it does not hold.  \a cond must be a constant
416  * expression as defined by the ISO C Standard:
417  *
418  *       6.8.4.2  The switch statement
419  *       ....
420  *       [#3] The expression of each case label shall be  an  integer
421  *       constant   expression  and  no  two  of  the  case  constant
422  *       expressions in the same switch statement shall have the same
423  *       value  after  conversion...
424  *
425  */
426 #define CLASSERT(cond) do {switch (1) {case (cond): case 0: break; } } while (0)
427
428 /* implication */
429 #define ergo(a, b) (!(a) || (b))
430 /* logical equivalence */
431 #define equi(a, b) (!!(a) == !!(b))
432
433 /* what used to be in portals_lib.h */
434 #ifndef MIN
435 # define MIN(a,b) (((a)<(b)) ? (a): (b))
436 #endif
437 #ifndef MAX
438 # define MAX(a,b) (((a)>(b)) ? (a): (b))
439 #endif
440
441 #define MKSTR(ptr) ((ptr))? (ptr) : ""
442
443 static inline size_t cfs_size_round4(size_t val)
444 {
445         return (val + 3) & (~0x3);
446 }
447
448 #ifndef HAVE_CFS_SIZE_ROUND
449 static inline size_t cfs_size_round(size_t val)
450 {
451         return (val + 7) & (~0x7);
452 }
453 #define HAVE_CFS_SIZE_ROUND
454 #endif
455
456 static inline size_t cfs_size_round16(size_t val)
457 {
458         return (val + 0xf) & (~0xf);
459 }
460
461 static inline size_t cfs_size_round32(size_t val)
462 {
463         return (val + 0x1f) & (~0x1f);
464 }
465
466 static inline size_t cfs_size_round0(size_t val)
467 {
468         if (!val)
469                 return 0;
470         return (val + 1 + 7) & (~0x7);
471 }
472
473 static inline size_t cfs_round_strlen(char *fset)
474 {
475         return cfs_size_round(strlen(fset) + 1);
476 }
477
478 #define LOGL(var,len,ptr)                                       \
479 do {                                                            \
480         if (var)                                                \
481                 memcpy((char *)ptr, (const char *)var, len);    \
482         ptr += cfs_size_round(len);                             \
483 } while (0)
484
485 #define LOGU(var,len,ptr)                                       \
486 do {                                                            \
487         if (var)                                                \
488                 memcpy((char *)var, (const char *)ptr, len);    \
489         ptr += cfs_size_round(len);                             \
490 } while (0)
491
492 extern struct cfs_psdev_ops libcfs_psdev_ops;
493 extern struct miscdevice libcfs_dev;
494 extern struct cfs_wi_sched *cfs_sched_rehash;
495
496 #endif