1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 /* XXX this layering violation is for nidstrings */
46 #include <lnet/types.h>
48 #ifndef DEBUG_SUBSYSTEM
49 # define DEBUG_SUBSYSTEM S_UNDEFINED
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.
61 #define LASSERT_CHECKED (0)
67 * Strange construction with empty "then" clause is used to trigger compiler
68 * warnings on the assertions of the form LASSERT(a = b);
70 * "warning: suggest parentheses around assignment used as truth value"
72 * requires -Wall. Unfortunately this rules out use of likely/unlikely.
74 #define LASSERT(cond) \
79 libcfs_assertion_failed( #cond , __FILE__, \
80 __FUNCTION__, __LINE__); \
83 #define LASSERTF(cond, fmt, a...) \
88 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
89 __FILE__, __FUNCTION__,__LINE__, \
90 "ASSERTION(" #cond ") failed:" fmt, \
97 #else /* !LASSERT_CHECKED */
99 #define LASSERT(cond) \
101 if (unlikely(!(cond))) \
102 libcfs_assertion_failed(#cond , __FILE__, \
103 __FUNCTION__, __LINE__); \
106 #define LASSERTF(cond, fmt, a...) \
108 if (unlikely(!(cond))) { \
109 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
110 __FILE__, __FUNCTION__,__LINE__, \
111 "ASSERTION(" #cond ") failed:" fmt, \
118 #endif /* !LASSERT_CHECKED */
121 #else /* !LIBCFS_DEBUG */
122 #define LASSERT(e) ((void)(0))
123 #define LASSERTF(cond, fmt...) ((void)(0))
124 #endif /* !LIBCFS_DEBUG */
126 #define KLASSERT(e) LASSERT(e)
128 void lbug_with_loc(char *file, const char *func, const int line)
129 __attribute__((noreturn));
131 #define LBUG() lbug_with_loc(__FILE__, __FUNCTION__, __LINE__)
133 extern atomic_t libcfs_kmemory;
139 # define libcfs_kmem_inc(ptr, size) \
141 atomic_add(size, &libcfs_kmemory); \
144 # define libcfs_kmem_dec(ptr, size) do { \
145 atomic_sub(size, &libcfs_kmemory); \
149 # define libcfs_kmem_inc(ptr, size) do {} while (0)
150 # define libcfs_kmem_dec(ptr, size) do {} while (0)
151 #endif /* LIBCFS_DEBUG */
153 #define LIBCFS_VMALLOC_SIZE 16384
155 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
157 LASSERT(!in_interrupt() || \
158 (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
159 if (unlikely((size) > LIBCFS_VMALLOC_SIZE)) \
160 (ptr) = cfs_alloc_large(size); \
162 (ptr) = cfs_alloc((size), (mask)); \
163 if (unlikely((ptr) == NULL)) { \
164 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
165 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
166 CERROR("LNET: %d total bytes allocated by lnet\n", \
167 atomic_read(&libcfs_kmemory)); \
169 libcfs_kmem_inc((ptr), (size)); \
170 if (!((mask) & CFS_ALLOC_ZERO)) \
171 memset((ptr), 0, (size)); \
173 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n", \
174 (int)(size), (ptr), atomic_read (&libcfs_kmemory)); \
177 #define LIBCFS_ALLOC(ptr, size) \
178 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
180 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
181 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
183 #define LIBCFS_FREE(ptr, size) \
186 if (unlikely((ptr) == NULL)) { \
187 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
188 "%s:%d\n", s, __FILE__, __LINE__); \
191 libcfs_kmem_dec((ptr), s); \
192 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
193 s, (ptr), atomic_read(&libcfs_kmemory)); \
194 if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
195 cfs_free_large(ptr); \
200 /******************************************************************************/
202 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
203 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
204 #define ___htonl(x) __cpu_to_be32(x)
205 #define ___htons(x) __cpu_to_be16(x)
206 #define ___ntohl(x) __be32_to_cpu(x)
207 #define ___ntohs(x) __be16_to_cpu(x)
208 #define htonl(x) ___htonl(x)
209 #define ntohl(x) ___ntohl(x)
210 #define htons(x) ___htons(x)
211 #define ntohs(x) ___ntohs(x)
214 void libcfs_debug_dumpstack(cfs_task_t *tsk);
215 void libcfs_run_upcall(char **argv);
216 void libcfs_run_lbug_upcall(char * file, const char *fn, const int line);
217 void libcfs_debug_dumplog(void);
218 int libcfs_debug_init(unsigned long bufsize);
219 int libcfs_debug_cleanup(void);
220 int libcfs_debug_clear_buffer(void);
221 int libcfs_debug_mark_buffer(char *text);
223 void libcfs_debug_set_level(unsigned int debug_level);
225 #else /* !__KERNEL__ */
229 # define LASSERT(e) assert(e)
230 # define LASSERTF(cond, args...) \
236 # define LBUG() assert(0)
238 # define LASSERT(e) ((void)(0))
239 # define LASSERTF(cond, args...) do { } while (0)
240 # define LBUG() ((void)(0))
241 # endif /* LIBCFS_DEBUG */
242 # define KLASSERT(e) do { } while (0)
243 # define printk(format, args...) printf (format, ## args)
244 # ifdef CRAY_XT3 /* buggy calloc! */
245 # define LIBCFS_ALLOC(ptr, size) \
247 (ptr) = malloc(size); \
248 memset(ptr, 0, size); \
251 # define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
253 # define LIBCFS_FREE(a, b) do { free(a); } while (0)
255 void libcfs_debug_dumplog(void);
256 int libcfs_debug_init(unsigned long bufsize);
257 int libcfs_debug_cleanup(void);
260 * Generic compiler-dependent macros required for kernel
261 * build go below this comment. Actual compiler/compiler version
262 * specific implementations come from the above header files
265 #define likely(x) __builtin_expect(!!(x), 1)
266 #define unlikely(x) __builtin_expect(!!(x), 0)
272 * compile-time assertions. @cond has to be constant expression.
275 * 6.8.4.2 The switch statement
279 * [#3] The expression of each case label shall be an integer
280 * constant expression and no two of the case constant
281 * expressions in the same switch statement shall have the same
282 * value after conversion...
285 #define CLASSERT(cond) ({ switch(42) { case (cond): case 0: break; } })
287 /* support decl needed both by kernel and liblustre */
288 int libcfs_isknown_lnd(int type);
289 char *libcfs_lnd2modname(int type);
290 char *libcfs_lnd2str(int type);
291 int libcfs_str2lnd(const char *str);
292 char *libcfs_net2str(__u32 net);
293 char *libcfs_nid2str(lnet_nid_t nid);
294 __u32 libcfs_str2net(const char *str);
295 lnet_nid_t libcfs_str2nid(const char *str);
296 int libcfs_str2anynid(lnet_nid_t *nid, const char *str);
297 char *libcfs_id2str(lnet_process_id_t id);
299 /* how an LNET NID encodes net:address */
300 #define LNET_NIDADDR(nid) ((__u32)((nid) & 0xffffffff))
301 #define LNET_NIDNET(nid) ((__u32)(((nid) >> 32)) & 0xffffffff)
302 #define LNET_MKNID(net,addr) ((((__u64)(net))<<32)|((__u64)(addr)))
303 /* how net encodes type:number */
304 #define LNET_NETNUM(net) ((net) & 0xffff)
305 #define LNET_NETTYP(net) (((net) >> 16) & 0xffff)
306 #define LNET_MKNET(typ,num) ((((__u32)(typ))<<16)|((__u32)(num)))
309 #define ergo(a, b) (!(a) || (b))
310 /* logical equivalence */
311 #define equi(a, b) (!!(a) == !!(b))
314 # define CURRENT_TIME time(0)
317 /* --------------------------------------------------------------------
319 * Support for temporary event tracing with minimal Heisenberg effect.
320 * All stuff about lwt are put in arch/kp30.h
321 * -------------------------------------------------------------------- */
323 struct libcfs_device_userstate
325 int ldu_memhog_pages;
326 cfs_page_t *ldu_memhog_root_page;
329 /* what used to be in portals_lib.h */
331 # define MIN(a,b) (((a)<(b)) ? (a): (b))
334 # define MAX(a,b) (((a)>(b)) ? (a): (b))
337 #define MKSTR(ptr) ((ptr))? (ptr) : ""
339 static inline int size_round4 (int val)
341 return (val + 3) & (~0x3);
344 static inline int size_round (int val)
346 return (val + 7) & (~0x7);
349 static inline int size_round16(int val)
351 return (val + 0xf) & (~0xf);
354 static inline int size_round32(int val)
356 return (val + 0x1f) & (~0x1f);
359 static inline int size_round0(int val)
363 return (val + 1 + 7) & (~0x7);
366 static inline size_t round_strlen(char *fset)
368 return (size_t)size_round((int)strlen(fset) + 1);
371 #define LOGL(var,len,ptr) \
374 memcpy((char *)ptr, (const char *)var, len); \
375 ptr += size_round(len); \
378 #define LOGU(var,len,ptr) \
381 memcpy((char *)var, (const char *)ptr, len); \
382 ptr += size_round(len); \
385 #define LOGL0(var,len,ptr) \
389 memcpy((char *)ptr, (const char *)var, len); \
390 *((char *)(ptr) + len) = 0; \
391 ptr += size_round(len + 1); \
395 * Lustre Network Driver types.
398 /* Only add to these values (i.e. don't ever change or redefine them):
399 * network addresses depend on them... */