4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2012, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/include/libcfs/libcfs_private.h
34 * Various defines for libcfs.
38 #ifndef __LIBCFS_PRIVATE_H__
39 #define __LIBCFS_PRIVATE_H__
41 #ifndef DEBUG_SUBSYSTEM
42 # define DEBUG_SUBSYSTEM S_UNDEFINED
46 * When this is on, LASSERT macro includes check for assignment used instead
47 * of equality check, but doesn't have unlikely(). Turn this on from time to
48 * time to make test-builds. This shouldn't be on for production release.
50 #define LASSERT_CHECKED (0)
52 #define LASSERTF(cond, fmt, ...) \
54 if (unlikely(!(cond))) { \
55 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
56 libcfs_debug_msg(&__msg_data, \
57 "ASSERTION( %s ) failed: " fmt, #cond, \
59 lbug_with_loc(&__msg_data); \
63 #define LASSERT(cond) LASSERTF(cond, "\n")
65 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
67 * This is for more expensive checks that one doesn't want to be enabled all
68 * the time. LINVRNT() has to be explicitly enabled by
69 * CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK option.
71 # define LINVRNT(exp) LASSERT(exp)
73 # define LINVRNT(exp) ((void)sizeof !!(exp))
76 #define KLASSERT(e) LASSERT(e)
78 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *);
82 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
83 lbug_with_loc(&msgdata); \
86 #ifndef LIBCFS_VMALLOC_SIZE
87 #define LIBCFS_VMALLOC_SIZE (2 << PAGE_SHIFT) /* 2 pages */
90 #define LIBCFS_ALLOC_PRE(size, mask) \
92 LASSERT(!in_interrupt() || \
93 ((size) <= LIBCFS_VMALLOC_SIZE && \
94 !gfpflags_allow_blocking(mask))); \
97 #define LIBCFS_ALLOC_POST(ptr, size) \
99 if (unlikely((ptr) == NULL)) { \
100 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
101 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size)); \
103 memset((ptr), 0, (size)); \
108 * allocate memory with GFP flags @mask
110 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
112 LIBCFS_ALLOC_PRE((size), (mask)); \
113 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
114 kmalloc((size), (mask)) : vmalloc(size); \
115 LIBCFS_ALLOC_POST((ptr), (size)); \
121 #define LIBCFS_ALLOC(ptr, size) \
122 LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
125 * non-sleeping allocator
127 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
128 LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
131 * allocate memory for specified CPU partition
132 * \a cptab != NULL, \a cpt is CPU partition id of \a cptab
133 * \a cptab == NULL, \a cpt is HW NUMA node id
135 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
137 LIBCFS_ALLOC_PRE((size), (mask)); \
138 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
139 kmalloc_node((size), (mask), cfs_cpt_spread_node(cptab, cpt)) :\
140 vmalloc_node(size, cfs_cpt_spread_node(cptab, cpt)); \
141 LIBCFS_ALLOC_POST((ptr), (size)); \
144 /** default numa allocator */
145 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
146 LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
148 #define LIBCFS_FREE(ptr, size) \
150 if (unlikely((ptr) == NULL)) { \
151 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
152 "%s:%d\n", (int)(size), __FILE__, __LINE__); \
158 /******************************************************************************/
160 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
161 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
162 #define ___htonl(x) __cpu_to_be32(x)
163 #define ___htons(x) __cpu_to_be16(x)
164 #define ___ntohl(x) __be32_to_cpu(x)
165 #define ___ntohs(x) __be16_to_cpu(x)
166 #define htonl(x) ___htonl(x)
167 #define ntohl(x) ___ntohl(x)
168 #define htons(x) ___htons(x)
169 #define ntohs(x) ___ntohs(x)
172 void libcfs_run_upcall(char **argv);
173 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
174 void libcfs_debug_dumplog(void);
175 int libcfs_debug_init(unsigned long bufsize);
176 int libcfs_debug_cleanup(void);
177 int libcfs_debug_clear_buffer(void);
178 int libcfs_debug_mark_buffer(const char *text);
181 * allocate a variable array, returned value is an array of pointers.
182 * Caller can specify length of array by count.
184 void *cfs_array_alloc(int count, unsigned int size);
185 void cfs_array_free(void *vars);
187 #define LASSERT_ATOMIC_ENABLED (1)
189 #if LASSERT_ATOMIC_ENABLED
191 /** assert value of @a is equal to @v */
192 #define LASSERT_ATOMIC_EQ(a, v) \
194 LASSERTF(atomic_read(a) == v, \
195 "value: %d\n", atomic_read((a))); \
198 /** assert value of @a is unequal to @v */
199 #define LASSERT_ATOMIC_NE(a, v) \
201 LASSERTF(atomic_read(a) != v, \
202 "value: %d\n", atomic_read((a))); \
205 /** assert value of @a is little than @v */
206 #define LASSERT_ATOMIC_LT(a, v) \
208 LASSERTF(atomic_read(a) < v, \
209 "value: %d\n", atomic_read((a))); \
212 /** assert value of @a is little/equal to @v */
213 #define LASSERT_ATOMIC_LE(a, v) \
215 LASSERTF(atomic_read(a) <= v, \
216 "value: %d\n", atomic_read((a))); \
219 /** assert value of @a is great than @v */
220 #define LASSERT_ATOMIC_GT(a, v) \
222 LASSERTF(atomic_read(a) > v, \
223 "value: %d\n", atomic_read((a))); \
226 /** assert value of @a is great/equal to @v */
227 #define LASSERT_ATOMIC_GE(a, v) \
229 LASSERTF(atomic_read(a) >= v, \
230 "value: %d\n", atomic_read((a))); \
233 /** assert value of @a is great than @v1 and little than @v2 */
234 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
236 int __v = atomic_read(a); \
237 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
240 /** assert value of @a is great than @v1 and little/equal to @v2 */
241 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
243 int __v = atomic_read(a); \
244 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
247 /** assert value of @a is great/equal to @v1 and little than @v2 */
248 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
250 int __v = atomic_read(a); \
251 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
254 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
255 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
257 int __v = atomic_read(a); \
258 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
261 #else /* !LASSERT_ATOMIC_ENABLED */
263 #define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
264 #define LASSERT_ATOMIC_NE(a, v) do {} while (0)
265 #define LASSERT_ATOMIC_LT(a, v) do {} while (0)
266 #define LASSERT_ATOMIC_LE(a, v) do {} while (0)
267 #define LASSERT_ATOMIC_GT(a, v) do {} while (0)
268 #define LASSERT_ATOMIC_GE(a, v) do {} while (0)
269 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
270 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
271 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
272 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
274 #endif /* LASSERT_ATOMIC_ENABLED */
276 #define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
277 #define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
279 #define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof(*(ptr)))
280 #define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof(*(ptr)))
282 /** Compile-time assertion.
284 * Check an invariant described by a constant expression at compile time by
285 * forcing a compiler error if it does not hold. \a cond must be a constant
286 * expression as defined by the ISO C Standard:
288 * 6.8.4.2 The switch statement
290 * [#3] The expression of each case label shall be an integer
291 * constant expression and no two of the case constant
292 * expressions in the same switch statement shall have the same
293 * value after conversion...
296 #define CLASSERT(cond) do {switch (42) {case (cond): case 0: break; } } while (0)
298 /* max value for numeric network address */
299 #define MAX_NUMERIC_VALUE 0xffffffff
302 #define ergo(a, b) (!(a) || (b))
303 /* logical equivalence */
304 #define equi(a, b) (!!(a) == !!(b))
306 /* --------------------------------------------------------------------
308 * Support for temporary event tracing with minimal Heisenberg effect.
309 * -------------------------------------------------------------------- */
311 #define MKSTR(ptr) ((ptr)) ? (ptr) : ""
313 static inline size_t cfs_size_round4(int val)
315 return (val + 3) & (~0x3);
318 #ifndef HAVE_CFS_SIZE_ROUND
319 static inline size_t cfs_size_round(int val)
321 return (val + 7) & (~0x7);
324 #define HAVE_CFS_SIZE_ROUND
327 static inline size_t cfs_size_round16(int val)
329 return (val + 0xf) & (~0xf);
332 static inline size_t cfs_size_round32(int val)
334 return (val + 0x1f) & (~0x1f);
337 static inline size_t cfs_size_round0(int val)
341 return (val + 1 + 7) & (~0x7);
344 static inline size_t cfs_round_strlen(char *fset)
346 return cfs_size_round((int)strlen(fset) + 1);
349 #define LOGL(var, len, ptr) \
352 memcpy((char *)ptr, (const char *)var, len); \
353 ptr += cfs_size_round(len); \
356 #define LOGU(var, len, ptr) \
359 memcpy((char *)var, (const char *)ptr, len); \
360 ptr += cfs_size_round(len); \