1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 #ifndef __LIBCFS_KP30_H__
5 #define __LIBCFS_KP30_H__
7 /* Controlled via configure key */
8 /* #define LIBCFS_DEBUG */
10 #include <libcfs/libcfs.h>
11 #include <lnet/types.h>
13 #if defined(__linux__)
14 #include <libcfs/linux/kp30.h>
15 #elif defined(__APPLE__)
16 #include <libcfs/darwin/kp30.h>
17 #elif defined(__WINNT__)
18 #include <libcfs/winnt/kp30.h>
20 #error Unsupported operating system
23 #ifndef DEBUG_SUBSYSTEM
24 # define DEBUG_SUBSYSTEM S_UNDEFINED
32 * When this is on, LASSERT macro includes check for assignment used instead
33 * of equality check, but doesn't have unlikely(). Turn this on from time to
34 * time to make test-builds. This shouldn't be on for production release.
36 #define LASSERT_CHECKED (0)
42 * Strange construction with empty "then" clause is used to trigger compiler
43 * warnings on the assertions of the form LASSERT(a = b);
45 * "warning: suggest parentheses around assignment used as truth value"
47 * requires -Wall. Unfortunately this rules out use of likely/unlikely.
49 #define LASSERT(cond) \
54 libcfs_assertion_failed( #cond , __FILE__, \
55 __FUNCTION__, __LINE__); \
58 #define LASSERTF(cond, fmt, a...) \
63 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
64 __FILE__, __FUNCTION__,__LINE__, \
65 "ASSERTION(" #cond ") failed:" fmt, \
74 #define LASSERT(cond) \
76 if (unlikely(!(cond))) \
77 libcfs_assertion_failed(#cond , __FILE__, \
78 __FUNCTION__, __LINE__); \
81 #define LASSERTF(cond, fmt, a...) \
83 if (unlikely(!(cond))) { \
84 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
85 __FILE__, __FUNCTION__,__LINE__, \
86 "ASSERTION(" #cond ") failed:" fmt, \
97 #define LASSERT(e) ((void)(0))
98 #define LASSERTF(cond, fmt...) ((void)(0))
99 #endif /* LIBCFS_DEBUG */
101 #define KLASSERT(e) LASSERT(e)
103 void lbug_with_loc(char *file, const char *func, const int line)
104 __attribute__((noreturn));
106 #define LBUG() lbug_with_loc(__FILE__, __FUNCTION__, __LINE__)
108 extern atomic_t libcfs_kmemory;
114 # define libcfs_kmem_inc(ptr, size) \
116 atomic_add(size, &libcfs_kmemory); \
119 # define libcfs_kmem_dec(ptr, size) do { \
120 atomic_sub(size, &libcfs_kmemory); \
124 # define libcfs_kmem_inc(ptr, size) do {} while (0)
125 # define libcfs_kmem_dec(ptr, size) do {} while (0)
126 #endif /* LIBCFS_DEBUG */
128 #define LIBCFS_VMALLOC_SIZE 16384
130 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
132 LASSERT(!in_interrupt() || \
133 (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
134 if (unlikely((size) > LIBCFS_VMALLOC_SIZE)) \
135 (ptr) = cfs_alloc_large(size); \
137 (ptr) = cfs_alloc((size), (mask)); \
138 if (unlikely((ptr) == NULL)) { \
139 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
140 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
141 CERROR("LNET: %d total bytes allocated by lnet\n", \
142 atomic_read(&libcfs_kmemory)); \
144 libcfs_kmem_inc((ptr), (size)); \
145 if (!((mask) & CFS_ALLOC_ZERO)) \
146 memset((ptr), 0, (size)); \
148 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n", \
149 (int)(size), (ptr), atomic_read (&libcfs_kmemory)); \
152 #define LIBCFS_ALLOC(ptr, size) \
153 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
155 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
156 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
158 #define LIBCFS_FREE(ptr, size) \
161 if (unlikely((ptr) == NULL)) { \
162 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
163 "%s:%d\n", s, __FILE__, __LINE__); \
166 libcfs_kmem_dec((ptr), s); \
167 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
168 s, (ptr), atomic_read(&libcfs_kmemory)); \
169 if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
170 cfs_free_large(ptr); \
175 /******************************************************************************/
177 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
178 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
179 #define ___htonl(x) __cpu_to_be32(x)
180 #define ___htons(x) __cpu_to_be16(x)
181 #define ___ntohl(x) __be32_to_cpu(x)
182 #define ___ntohs(x) __be16_to_cpu(x)
183 #define htonl(x) ___htonl(x)
184 #define ntohl(x) ___ntohl(x)
185 #define htons(x) ___htons(x)
186 #define ntohs(x) ___ntohs(x)
189 void libcfs_debug_dumpstack(cfs_task_t *tsk);
190 void libcfs_run_upcall(char **argv);
191 void libcfs_run_lbug_upcall(char * file, const char *fn, const int line);
192 void libcfs_debug_dumplog(void);
193 int libcfs_debug_init(unsigned long bufsize);
194 int libcfs_debug_cleanup(void);
195 int libcfs_debug_clear_buffer(void);
196 int libcfs_debug_mark_buffer(char *text);
198 void libcfs_debug_set_level(unsigned int debug_level);
200 #else /* !__KERNEL__ */
204 # define LASSERT(e) assert(e)
205 # define LASSERTF(cond, args...) \
211 # define LBUG() assert(0)
213 # define LASSERT(e) ((void)(0))
214 # define LASSERTF(cond, args...) do { } while (0)
215 # define LBUG() ((void)(0))
216 # endif /* LIBCFS_DEBUG */
217 # define KLASSERT(e) do { } while (0)
218 # define printk(format, args...) printf (format, ## args)
219 # ifdef CRAY_XT3 /* buggy calloc! */
220 # define LIBCFS_ALLOC(ptr, size) \
222 (ptr) = malloc(size); \
223 memset(ptr, 0, size); \
226 # define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
228 # define LIBCFS_FREE(a, b) do { free(a); } while (0)
230 void libcfs_debug_dumplog(void);
231 int libcfs_debug_init(unsigned long bufsize);
232 int libcfs_debug_cleanup(void);
235 * Generic compiler-dependent macros required for kernel
236 * build go below this comment. Actual compiler/compiler version
237 * specific implementations come from the above header files
240 #define likely(x) __builtin_expect(!!(x), 1)
241 #define unlikely(x) __builtin_expect(!!(x), 0)
247 * compile-time assertions. @cond has to be constant expression.
250 * 6.8.4.2 The switch statement
254 * [#3] The expression of each case label shall be an integer
255 * constant expression and no two of the case constant
256 * expressions in the same switch statement shall have the same
257 * value after conversion...
260 #define CLASSERT(cond) ({ switch(42) { case (cond): case 0: break; } })
262 /* support decl needed both by kernel and liblustre */
263 int libcfs_isknown_lnd(int type);
264 char *libcfs_lnd2modname(int type);
265 char *libcfs_lnd2str(int type);
266 int libcfs_str2lnd(const char *str);
267 char *libcfs_net2str(__u32 net);
268 char *libcfs_nid2str(lnet_nid_t nid);
269 __u32 libcfs_str2net(const char *str);
270 lnet_nid_t libcfs_str2nid(const char *str);
271 int libcfs_str2anynid(lnet_nid_t *nid, const char *str);
272 char *libcfs_id2str(lnet_process_id_t id);
273 void libcfs_setnet0alias(int type);
275 /* how an LNET NID encodes net:address */
276 #define LNET_NIDADDR(nid) ((__u32)((nid) & 0xffffffff))
277 #define LNET_NIDNET(nid) ((__u32)(((nid) >> 32)) & 0xffffffff)
278 #define LNET_MKNID(net,addr) ((((__u64)(net))<<32)|((__u64)(addr)))
279 /* how net encodes type:number */
280 #define LNET_NETNUM(net) ((net) & 0xffff)
281 #define LNET_NETTYP(net) (((net) >> 16) & 0xffff)
282 #define LNET_MKNET(typ,num) ((((__u32)(typ))<<16)|((__u32)(num)))
285 #define ergo(a, b) (!(a) || (b))
286 /* logical equivalence */
287 #define equi(a, b) (!!(a) == !!(b))
290 # define CURRENT_TIME time(0)
293 /* --------------------------------------------------------------------
295 * Support for temporary event tracing with minimal Heisenberg effect.
296 * All stuff about lwt are put in arch/kp30.h
297 * -------------------------------------------------------------------- */
299 struct libcfs_device_userstate
301 int ldu_memhog_pages;
302 cfs_page_t *ldu_memhog_root_page;
305 /* what used to be in portals_lib.h */
307 # define MIN(a,b) (((a)<(b)) ? (a): (b))
310 # define MAX(a,b) (((a)>(b)) ? (a): (b))
313 #define MKSTR(ptr) ((ptr))? (ptr) : ""
315 static inline int size_round4 (int val)
317 return (val + 3) & (~0x3);
320 static inline int size_round (int val)
322 return (val + 7) & (~0x7);
325 static inline int size_round16(int val)
327 return (val + 0xf) & (~0xf);
330 static inline int size_round32(int val)
332 return (val + 0x1f) & (~0x1f);
335 static inline int size_round0(int val)
339 return (val + 1 + 7) & (~0x7);
342 static inline size_t round_strlen(char *fset)
344 return (size_t)size_round((int)strlen(fset) + 1);
347 #define LOGL(var,len,ptr) \
350 memcpy((char *)ptr, (const char *)var, len); \
351 ptr += size_round(len); \
354 #define LOGU(var,len,ptr) \
357 memcpy((char *)var, (const char *)ptr, len); \
358 ptr += size_round(len); \
361 #define LOGL0(var,len,ptr) \
365 memcpy((char *)ptr, (const char *)var, len); \
366 *((char *)(ptr) + len) = 0; \
367 ptr += size_round(len + 1); \
371 * USER LEVEL STUFF BELOW
374 #define LIBCFS_IOCTL_VERSION 0x0001000a
376 struct libcfs_ioctl_data {
393 __u32 ioc_plen1; /* buffers in userspace */
395 __u32 ioc_plen2; /* buffers in userspace */
402 struct libcfs_ioctl_hdr {
407 struct libcfs_debug_ioctl_data
409 struct libcfs_ioctl_hdr hdr;
414 #define LIBCFS_IOC_INIT(data) \
416 memset(&data, 0, sizeof(data)); \
417 data.ioc_version = LIBCFS_IOCTL_VERSION; \
418 data.ioc_len = sizeof(data); \
421 /* FIXME check conflict with lustre_lib.h */
422 #define LIBCFS_IOC_DEBUG_MASK _IOWR('f', 250, long)
424 static inline int libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
426 int len = sizeof(*data);
427 len += size_round(data->ioc_inllen1);
428 len += size_round(data->ioc_inllen2);
432 static inline int libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
434 if (data->ioc_len > (1<<30)) {
435 CERROR ("LIBCFS ioctl: ioc_len larger than 1<<30\n");
438 if (data->ioc_inllen1 > (1<<30)) {
439 CERROR ("LIBCFS ioctl: ioc_inllen1 larger than 1<<30\n");
442 if (data->ioc_inllen2 > (1<<30)) {
443 CERROR ("LIBCFS ioctl: ioc_inllen2 larger than 1<<30\n");
446 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
447 CERROR ("LIBCFS ioctl: inlbuf1 pointer but 0 length\n");
450 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
451 CERROR ("LIBCFS ioctl: inlbuf2 pointer but 0 length\n");
454 if (data->ioc_pbuf1 && !data->ioc_plen1) {
455 CERROR ("LIBCFS ioctl: pbuf1 pointer but 0 length\n");
458 if (data->ioc_pbuf2 && !data->ioc_plen2) {
459 CERROR ("LIBCFS ioctl: pbuf2 pointer but 0 length\n");
462 if (data->ioc_plen1 && !data->ioc_pbuf1) {
463 CERROR ("LIBCFS ioctl: plen1 nonzero but no pbuf1 pointer\n");
466 if (data->ioc_plen2 && !data->ioc_pbuf2) {
467 CERROR ("LIBCFS ioctl: plen2 nonzero but no pbuf2 pointer\n");
470 if ((__u32)libcfs_ioctl_packlen(data) != data->ioc_len ) {
471 CERROR ("LIBCFS ioctl: packlen != ioc_len\n");
474 if (data->ioc_inllen1 &&
475 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
476 CERROR ("LIBCFS ioctl: inlbuf1 not 0 terminated\n");
479 if (data->ioc_inllen2 &&
480 data->ioc_bulk[size_round(data->ioc_inllen1) +
481 data->ioc_inllen2 - 1] != '\0') {
482 CERROR ("LIBCFS ioctl: inlbuf2 not 0 terminated\n");
489 static inline int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
493 struct libcfs_ioctl_data *overlay;
494 data->ioc_len = libcfs_ioctl_packlen(data);
495 data->ioc_version = LIBCFS_IOCTL_VERSION;
497 if (*pbuf && libcfs_ioctl_packlen(data) > max)
500 *pbuf = malloc(data->ioc_len);
504 overlay = (struct libcfs_ioctl_data *)*pbuf;
505 memcpy(*pbuf, data, sizeof(*data));
507 ptr = overlay->ioc_bulk;
508 if (data->ioc_inlbuf1)
509 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
510 if (data->ioc_inlbuf2)
511 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
512 if (libcfs_ioctl_is_invalid(overlay))
520 extern int libcfs_ioctl_getdata(char *buf, char *end, void *arg);
521 extern int libcfs_ioctl_popdata(void *arg, void *buf, int size);
525 /* ioctls for manipulating snapshots 30- */
526 #define IOC_LIBCFS_TYPE 'e'
527 #define IOC_LIBCFS_MIN_NR 30
529 #define IOC_LIBCFS_PANIC _IOWR('e', 30, IOCTL_LIBCFS_TYPE)
530 #define IOC_LIBCFS_CLEAR_DEBUG _IOWR('e', 31, IOCTL_LIBCFS_TYPE)
531 #define IOC_LIBCFS_MARK_DEBUG _IOWR('e', 32, IOCTL_LIBCFS_TYPE)
532 #define IOC_LIBCFS_LWT_CONTROL _IOWR('e', 33, IOCTL_LIBCFS_TYPE)
533 #define IOC_LIBCFS_LWT_SNAPSHOT _IOWR('e', 34, IOCTL_LIBCFS_TYPE)
534 #define IOC_LIBCFS_LWT_LOOKUP_STRING _IOWR('e', 35, IOCTL_LIBCFS_TYPE)
535 #define IOC_LIBCFS_MEMHOG _IOWR('e', 36, IOCTL_LIBCFS_TYPE)
536 #define IOC_LIBCFS_PING_TEST _IOWR('e', 37, IOCTL_LIBCFS_TYPE)
538 #define IOC_LIBCFS_GET_NI _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
539 #define IOC_LIBCFS_FAIL_NID _IOWR('e', 51, IOCTL_LIBCFS_TYPE)
540 #define IOC_LIBCFS_ADD_ROUTE _IOWR('e', 52, IOCTL_LIBCFS_TYPE)
541 #define IOC_LIBCFS_DEL_ROUTE _IOWR('e', 53, IOCTL_LIBCFS_TYPE)
542 #define IOC_LIBCFS_GET_ROUTE _IOWR('e', 54, IOCTL_LIBCFS_TYPE)
543 #define IOC_LIBCFS_NOTIFY_ROUTER _IOWR('e', 55, IOCTL_LIBCFS_TYPE)
544 #define IOC_LIBCFS_UNCONFIGURE _IOWR('e', 56, IOCTL_LIBCFS_TYPE)
545 #define IOC_LIBCFS_PORTALS_COMPATIBILITY _IOWR('e', 57, IOCTL_LIBCFS_TYPE)
546 #define IOC_LIBCFS_LNET_DIST _IOWR('e', 58, IOCTL_LIBCFS_TYPE)
547 #define IOC_LIBCFS_CONFIGURE _IOWR('e', 59, IOCTL_LIBCFS_TYPE)
548 #define IOC_LIBCFS_TESTPROTOCOMPAT _IOWR('e', 60, IOCTL_LIBCFS_TYPE)
549 #define IOC_LIBCFS_PING _IOWR('e', 61, IOCTL_LIBCFS_TYPE)
550 #define IOC_LIBCFS_DEBUG_PEER _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
551 #define IOC_LIBCFS_LNETST _IOWR('e', 63, IOCTL_LIBCFS_TYPE)
553 #define IOC_LIBCFS_REGISTER_MYNID _IOWR('e', 70, IOCTL_LIBCFS_TYPE)
554 #define IOC_LIBCFS_CLOSE_CONNECTION _IOWR('e', 71, IOCTL_LIBCFS_TYPE)
555 #define IOC_LIBCFS_PUSH_CONNECTION _IOWR('e', 72, IOCTL_LIBCFS_TYPE)
556 #define IOC_LIBCFS_GET_CONN _IOWR('e', 73, IOCTL_LIBCFS_TYPE)
557 #define IOC_LIBCFS_DEL_PEER _IOWR('e', 74, IOCTL_LIBCFS_TYPE)
558 #define IOC_LIBCFS_ADD_PEER _IOWR('e', 75, IOCTL_LIBCFS_TYPE)
559 #define IOC_LIBCFS_GET_PEER _IOWR('e', 76, IOCTL_LIBCFS_TYPE)
560 #define IOC_LIBCFS_GET_TXDESC _IOWR('e', 77, IOCTL_LIBCFS_TYPE)
561 #define IOC_LIBCFS_ADD_INTERFACE _IOWR('e', 78, IOCTL_LIBCFS_TYPE)
562 #define IOC_LIBCFS_DEL_INTERFACE _IOWR('e', 79, IOCTL_LIBCFS_TYPE)
563 #define IOC_LIBCFS_GET_INTERFACE _IOWR('e', 80, IOCTL_LIBCFS_TYPE)
564 #define IOC_LIBCFS_GET_GMID _IOWR('e', 81, IOCTL_LIBCFS_TYPE)
566 #define IOC_LIBCFS_MAX_NR 81
570 /* Only add to these values (i.e. don't ever change or redefine them):
571 * network addresses depend on them... */
587 DEBUG_DAEMON_START = 1,
588 DEBUG_DAEMON_STOP = 2,
589 DEBUG_DAEMON_PAUSE = 3,
590 DEBUG_DAEMON_CONTINUE = 4,
594 enum cfg_record_type {
595 PORTALS_CFG_TYPE = 1,
596 LUSTRE_CFG_TYPE = 123,
599 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
601 /* lustre_id output helper macros */
602 #define DLID4 "%lu/%lu/%lu/%lu"
605 (unsigned long)(id)->li_fid.lf_id, \
606 (unsigned long)(id)->li_fid.lf_group, \
607 (unsigned long)(id)->li_stc.u.e3s.l3s_ino, \
608 (unsigned long)(id)->li_stc.u.e3s.l3s_gen