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.sun.com/software/products/lustre/docs/GPLv2.pdf
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/include/libcfs/libcfs_private.h
38 * Various defines for libcfs.
42 #ifndef __LIBCFS_PRIVATE_H__
43 #define __LIBCFS_PRIVATE_H__
45 #ifndef DEBUG_SUBSYSTEM
46 # define DEBUG_SUBSYSTEM S_UNDEFINED
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.
54 #define LASSERT_CHECKED (0)
56 #define LASSERTF(cond, fmt, ...) \
58 if (unlikely(!(cond))) { \
59 LIBCFS_DEBUG_MSG_DATA_DECL(__msg_data, D_EMERG, NULL); \
60 libcfs_debug_msg(&__msg_data, \
61 "ASSERTION( %s ) failed: " fmt, #cond, \
63 lbug_with_loc(&__msg_data); \
67 #define LASSERT(cond) LASSERTF(cond, "\n")
69 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
71 * This is for more expensive checks that one doesn't want to be enabled all
72 * the time. LINVRNT() has to be explicitly enabled by
73 * CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK option.
75 # define LINVRNT(exp) LASSERT(exp)
77 # define LINVRNT(exp) ((void)sizeof !!(exp))
80 #define KLASSERT(e) LASSERT(e)
82 void __noreturn lbug_with_loc(struct libcfs_debug_msg_data *);
86 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL); \
87 lbug_with_loc(&msgdata); \
90 #ifndef LIBCFS_VMALLOC_SIZE
91 #define LIBCFS_VMALLOC_SIZE (2 << PAGE_SHIFT) /* 2 pages */
94 #define LIBCFS_ALLOC_PRE(size, mask) \
96 LASSERT(!in_interrupt() || \
97 ((size) <= LIBCFS_VMALLOC_SIZE && \
98 !gfpflags_allow_blocking(mask))); \
101 #define LIBCFS_ALLOC_POST(ptr, size) \
103 if (unlikely((ptr) == NULL)) { \
104 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
105 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size)); \
107 memset((ptr), 0, (size)); \
112 * allocate memory with GFP flags @mask
114 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
116 LIBCFS_ALLOC_PRE((size), (mask)); \
117 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
118 kmalloc((size), (mask)) : vmalloc(size); \
119 LIBCFS_ALLOC_POST((ptr), (size)); \
125 #define LIBCFS_ALLOC(ptr, size) \
126 LIBCFS_ALLOC_GFP(ptr, size, GFP_NOFS)
129 * non-sleeping allocator
131 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
132 LIBCFS_ALLOC_GFP(ptr, size, GFP_ATOMIC)
135 * allocate memory for specified CPU partition
136 * \a cptab != NULL, \a cpt is CPU partition id of \a cptab
137 * \a cptab == NULL, \a cpt is HW NUMA node id
139 #define LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, mask) \
141 LIBCFS_ALLOC_PRE((size), (mask)); \
142 (ptr) = (size) <= LIBCFS_VMALLOC_SIZE ? \
143 kmalloc_node((size), (mask), cfs_cpt_spread_node(cptab, cpt)) :\
144 vmalloc_node(size, cfs_cpt_spread_node(cptab, cpt)); \
145 LIBCFS_ALLOC_POST((ptr), (size)); \
148 /** default numa allocator */
149 #define LIBCFS_CPT_ALLOC(ptr, cptab, cpt, size) \
150 LIBCFS_CPT_ALLOC_GFP(ptr, cptab, cpt, size, GFP_NOFS)
152 #define LIBCFS_FREE(ptr, size) \
154 if (unlikely((ptr) == NULL)) { \
155 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
156 "%s:%d\n", (int)(size), __FILE__, __LINE__); \
162 /******************************************************************************/
164 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
165 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
166 #define ___htonl(x) __cpu_to_be32(x)
167 #define ___htons(x) __cpu_to_be16(x)
168 #define ___ntohl(x) __be32_to_cpu(x)
169 #define ___ntohs(x) __be16_to_cpu(x)
170 #define htonl(x) ___htonl(x)
171 #define ntohl(x) ___ntohl(x)
172 #define htons(x) ___htons(x)
173 #define ntohs(x) ___ntohs(x)
176 void libcfs_run_upcall(char **argv);
177 void libcfs_run_lbug_upcall(struct libcfs_debug_msg_data *);
178 void libcfs_debug_dumplog(void);
179 int libcfs_debug_init(unsigned long bufsize);
180 int libcfs_debug_cleanup(void);
181 int libcfs_debug_clear_buffer(void);
182 int libcfs_debug_mark_buffer(const char *text);
185 * allocate a variable array, returned value is an array of pointers.
186 * Caller can specify length of array by count.
188 void *cfs_array_alloc(int count, unsigned int size);
189 void cfs_array_free(void *vars);
191 #define LASSERT_ATOMIC_ENABLED (1)
193 #if LASSERT_ATOMIC_ENABLED
195 /** assert value of @a is equal to @v */
196 #define LASSERT_ATOMIC_EQ(a, v) \
198 LASSERTF(atomic_read(a) == v, \
199 "value: %d\n", atomic_read((a))); \
202 /** assert value of @a is unequal to @v */
203 #define LASSERT_ATOMIC_NE(a, v) \
205 LASSERTF(atomic_read(a) != v, \
206 "value: %d\n", atomic_read((a))); \
209 /** assert value of @a is little than @v */
210 #define LASSERT_ATOMIC_LT(a, v) \
212 LASSERTF(atomic_read(a) < v, \
213 "value: %d\n", atomic_read((a))); \
216 /** assert value of @a is little/equal to @v */
217 #define LASSERT_ATOMIC_LE(a, v) \
219 LASSERTF(atomic_read(a) <= v, \
220 "value: %d\n", atomic_read((a))); \
223 /** assert value of @a is great than @v */
224 #define LASSERT_ATOMIC_GT(a, v) \
226 LASSERTF(atomic_read(a) > v, \
227 "value: %d\n", atomic_read((a))); \
230 /** assert value of @a is great/equal to @v */
231 #define LASSERT_ATOMIC_GE(a, v) \
233 LASSERTF(atomic_read(a) >= v, \
234 "value: %d\n", atomic_read((a))); \
237 /** assert value of @a is great than @v1 and little than @v2 */
238 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) \
240 int __v = atomic_read(a); \
241 LASSERTF(__v > v1 && __v < v2, "value: %d\n", __v); \
244 /** assert value of @a is great than @v1 and little/equal to @v2 */
245 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) \
247 int __v = atomic_read(a); \
248 LASSERTF(__v > v1 && __v <= v2, "value: %d\n", __v); \
251 /** assert value of @a is great/equal to @v1 and little than @v2 */
252 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) \
254 int __v = atomic_read(a); \
255 LASSERTF(__v >= v1 && __v < v2, "value: %d\n", __v); \
258 /** assert value of @a is great/equal to @v1 and little/equal to @v2 */
259 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) \
261 int __v = atomic_read(a); \
262 LASSERTF(__v >= v1 && __v <= v2, "value: %d\n", __v); \
265 #else /* !LASSERT_ATOMIC_ENABLED */
267 #define LASSERT_ATOMIC_EQ(a, v) do {} while (0)
268 #define LASSERT_ATOMIC_NE(a, v) do {} while (0)
269 #define LASSERT_ATOMIC_LT(a, v) do {} while (0)
270 #define LASSERT_ATOMIC_LE(a, v) do {} while (0)
271 #define LASSERT_ATOMIC_GT(a, v) do {} while (0)
272 #define LASSERT_ATOMIC_GE(a, v) do {} while (0)
273 #define LASSERT_ATOMIC_GT_LT(a, v1, v2) do {} while (0)
274 #define LASSERT_ATOMIC_GT_LE(a, v1, v2) do {} while (0)
275 #define LASSERT_ATOMIC_GE_LT(a, v1, v2) do {} while (0)
276 #define LASSERT_ATOMIC_GE_LE(a, v1, v2) do {} while (0)
278 #endif /* LASSERT_ATOMIC_ENABLED */
280 #define LASSERT_ATOMIC_ZERO(a) LASSERT_ATOMIC_EQ(a, 0)
281 #define LASSERT_ATOMIC_POS(a) LASSERT_ATOMIC_GT(a, 0)
283 #define CFS_ALLOC_PTR(ptr) LIBCFS_ALLOC(ptr, sizeof(*(ptr)))
284 #define CFS_FREE_PTR(ptr) LIBCFS_FREE(ptr, sizeof(*(ptr)))
286 /** Compile-time assertion.
288 * Check an invariant described by a constant expression at compile time by
289 * forcing a compiler error if it does not hold. \a cond must be a constant
290 * expression as defined by the ISO C Standard:
292 * 6.8.4.2 The switch statement
294 * [#3] The expression of each case label shall be an integer
295 * constant expression and no two of the case constant
296 * expressions in the same switch statement shall have the same
297 * value after conversion...
300 #define CLASSERT(cond) do {switch (42) {case (cond): case 0: break; } } while (0)
302 /* max value for numeric network address */
303 #define MAX_NUMERIC_VALUE 0xffffffff
306 #define ergo(a, b) (!(a) || (b))
307 /* logical equivalence */
308 #define equi(a, b) (!!(a) == !!(b))
310 /* --------------------------------------------------------------------
312 * Support for temporary event tracing with minimal Heisenberg effect.
313 * -------------------------------------------------------------------- */
315 #define MKSTR(ptr) ((ptr)) ? (ptr) : ""
317 static inline int cfs_size_round4(int val)
319 return (val + 3) & (~0x3);
322 #ifndef HAVE_CFS_SIZE_ROUND
323 static inline int cfs_size_round(int val)
325 return (val + 7) & (~0x7);
328 #define HAVE_CFS_SIZE_ROUND
331 static inline int cfs_size_round16(int val)
333 return (val + 0xf) & (~0xf);
336 static inline int cfs_size_round32(int val)
338 return (val + 0x1f) & (~0x1f);
341 static inline int cfs_size_round0(int val)
345 return (val + 1 + 7) & (~0x7);
348 static inline size_t cfs_round_strlen(char *fset)
350 return (size_t)cfs_size_round((int)strlen(fset) + 1);
353 #define LOGL(var, len, ptr) \
356 memcpy((char *)ptr, (const char *)var, len); \
357 ptr += cfs_size_round(len); \
360 #define LOGU(var, len, ptr) \
363 memcpy((char *)var, (const char *)ptr, len); \
364 ptr += cfs_size_round(len); \
367 #define LOGL0(var, len, ptr) \
371 memcpy((char *)ptr, (const char *)var, len); \
372 *((char *)(ptr) + len) = 0; \
373 ptr += cfs_size_round(len + 1); \