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
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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.
37 #ifndef __LIBCFS_KP30_H__
38 #define __LIBCFS_KP30_H__
40 /* Controlled via configure key */
41 /* #define LIBCFS_DEBUG */
43 #include <libcfs/libcfs.h>
44 #include <lnet/types.h>
46 #if defined(__linux__)
47 #include <libcfs/linux/kp30.h>
48 #elif defined(__APPLE__)
49 #include <libcfs/darwin/kp30.h>
50 #elif defined(__WINNT__)
51 #include <libcfs/winnt/kp30.h>
53 #error Unsupported operating system
56 #ifndef DEBUG_SUBSYSTEM
57 # define DEBUG_SUBSYSTEM S_UNDEFINED
65 * When this is on, LASSERT macro includes check for assignment used instead
66 * of equality check, but doesn't have unlikely(). Turn this on from time to
67 * time to make test-builds. This shouldn't be on for production release.
69 #define LASSERT_CHECKED (0)
75 * Strange construction with empty "then" clause is used to trigger compiler
76 * warnings on the assertions of the form LASSERT(a = b);
78 * "warning: suggest parentheses around assignment used as truth value"
80 * requires -Wall. Unfortunately this rules out use of likely/unlikely.
82 #define LASSERT(cond) \
87 libcfs_assertion_failed( #cond , __FILE__, \
88 __FUNCTION__, __LINE__); \
91 #define LASSERTF(cond, fmt, a...) \
96 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
97 __FILE__, __FUNCTION__,__LINE__, \
98 "ASSERTION(" #cond ") failed:" fmt, \
104 /* LASSERT_CHECKED */
107 #define LASSERT(cond) \
109 if (unlikely(!(cond))) \
110 libcfs_assertion_failed(#cond , __FILE__, \
111 __FUNCTION__, __LINE__); \
114 #define LASSERTF(cond, fmt, a...) \
116 if (unlikely(!(cond))) { \
117 libcfs_debug_msg(NULL, DEBUG_SUBSYSTEM, D_EMERG, \
118 __FILE__, __FUNCTION__,__LINE__, \
119 "ASSERTION(" #cond ") failed:" fmt, \
125 /* LASSERT_CHECKED */
130 #define LASSERT(e) ((void)(0))
131 #define LASSERTF(cond, fmt...) ((void)(0))
132 #endif /* LIBCFS_DEBUG */
134 #define KLASSERT(e) LASSERT(e)
136 void lbug_with_loc(const char *file, const char *func, const int line)
137 __attribute__((noreturn));
139 #define LBUG() lbug_with_loc(__FILE__, __FUNCTION__, __LINE__)
141 extern atomic_t libcfs_kmemory;
147 # define libcfs_kmem_inc(ptr, size) \
149 atomic_add(size, &libcfs_kmemory); \
152 # define libcfs_kmem_dec(ptr, size) do { \
153 atomic_sub(size, &libcfs_kmemory); \
157 # define libcfs_kmem_inc(ptr, size) do {} while (0)
158 # define libcfs_kmem_dec(ptr, size) do {} while (0)
159 #endif /* LIBCFS_DEBUG */
161 #define LIBCFS_VMALLOC_SIZE 16384
163 #define LIBCFS_ALLOC_GFP(ptr, size, mask) \
165 LASSERT(!in_interrupt() || \
166 (size <= LIBCFS_VMALLOC_SIZE && mask == CFS_ALLOC_ATOMIC));\
167 if (unlikely((size) > LIBCFS_VMALLOC_SIZE)) \
168 (ptr) = cfs_alloc_large(size); \
170 (ptr) = cfs_alloc((size), (mask)); \
171 if (unlikely((ptr) == NULL)) { \
172 CERROR("LNET: out of memory at %s:%d (tried to alloc '" \
173 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
174 CERROR("LNET: %d total bytes allocated by lnet\n", \
175 atomic_read(&libcfs_kmemory)); \
177 libcfs_kmem_inc((ptr), (size)); \
178 if (!((mask) & CFS_ALLOC_ZERO)) \
179 memset((ptr), 0, (size)); \
181 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n", \
182 (int)(size), (ptr), atomic_read (&libcfs_kmemory)); \
185 #define LIBCFS_ALLOC(ptr, size) \
186 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_IO)
188 #define LIBCFS_ALLOC_ATOMIC(ptr, size) \
189 LIBCFS_ALLOC_GFP(ptr, size, CFS_ALLOC_ATOMIC)
191 #define LIBCFS_FREE(ptr, size) \
194 if (unlikely((ptr) == NULL)) { \
195 CERROR("LIBCFS: free NULL '" #ptr "' (%d bytes) at " \
196 "%s:%d\n", s, __FILE__, __LINE__); \
199 libcfs_kmem_dec((ptr), s); \
200 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
201 s, (ptr), atomic_read(&libcfs_kmemory)); \
202 if (unlikely(s > LIBCFS_VMALLOC_SIZE)) \
203 cfs_free_large(ptr); \
208 /******************************************************************************/
210 /* htonl hack - either this, or compile with -O2. Stupid byteorder/generic.h */
211 #if defined(__GNUC__) && (__GNUC__ >= 2) && !defined(__OPTIMIZE__)
212 #define ___htonl(x) __cpu_to_be32(x)
213 #define ___htons(x) __cpu_to_be16(x)
214 #define ___ntohl(x) __be32_to_cpu(x)
215 #define ___ntohs(x) __be16_to_cpu(x)
216 #define htonl(x) ___htonl(x)
217 #define ntohl(x) ___ntohl(x)
218 #define htons(x) ___htons(x)
219 #define ntohs(x) ___ntohs(x)
222 void libcfs_debug_dumpstack(cfs_task_t *tsk);
223 void libcfs_run_upcall(char **argv);
224 void libcfs_run_lbug_upcall(const char * file, const char *fn, const int line);
225 void libcfs_debug_dumplog(void);
226 int libcfs_debug_init(unsigned long bufsize);
227 int libcfs_debug_cleanup(void);
228 int libcfs_debug_clear_buffer(void);
229 int libcfs_debug_mark_buffer(const char *text);
231 void libcfs_debug_set_level(unsigned int debug_level);
233 #else /* !__KERNEL__ */
237 # define LASSERT(e) assert(e)
238 # define LASSERTF(cond, args...) \
244 # define LBUG() assert(0)
246 # define LASSERT(e) ((void)(0))
247 # define LASSERTF(cond, args...) do { } while (0)
248 # define LBUG() ((void)(0))
249 # endif /* LIBCFS_DEBUG */
250 # define KLASSERT(e) do { } while (0)
251 # define printk(format, args...) printf (format, ## args)
252 # ifdef CRAY_XT3 /* buggy calloc! */
253 # define LIBCFS_ALLOC(ptr, size) \
255 (ptr) = malloc(size); \
256 memset(ptr, 0, size); \
259 # define LIBCFS_ALLOC(ptr, size) do { (ptr) = calloc(1,size); } while (0)
261 # define LIBCFS_FREE(a, b) do { free(a); } while (0)
263 void libcfs_debug_dumplog(void);
264 int libcfs_debug_init(unsigned long bufsize);
265 int libcfs_debug_cleanup(void);
268 * Generic compiler-dependent macros required for kernel
269 * build go below this comment. Actual compiler/compiler version
270 * specific implementations come from the above header files
273 #define likely(x) __builtin_expect(!!(x), 1)
274 #define unlikely(x) __builtin_expect(!!(x), 0)
280 * compile-time assertions. @cond has to be constant expression.
283 * 6.8.4.2 The switch statement
287 * [#3] The expression of each case label shall be an integer
288 * constant expression and no two of the case constant
289 * expressions in the same switch statement shall have the same
290 * value after conversion...
293 #define CLASSERT(cond) ({ switch(42) { case (cond): case 0: break; } })
295 /* support decl needed both by kernel and liblustre */
296 int libcfs_isknown_lnd(int type);
297 char *libcfs_lnd2modname(int type);
298 char *libcfs_lnd2str(int type);
299 int libcfs_str2lnd(const char *str);
300 char *libcfs_net2str(__u32 net);
301 char *libcfs_nid2str(lnet_nid_t nid);
302 __u32 libcfs_str2net(const char *str);
303 lnet_nid_t libcfs_str2nid(const char *str);
304 int libcfs_str2anynid(lnet_nid_t *nid, const char *str);
305 char *libcfs_id2str(lnet_process_id_t id);
306 void libcfs_setnet0alias(int type);
308 /* how an LNET NID encodes net:address */
309 #define LNET_NIDADDR(nid) ((__u32)((nid) & 0xffffffff))
310 #define LNET_NIDNET(nid) ((__u32)(((nid) >> 32)) & 0xffffffff)
311 #define LNET_MKNID(net,addr) ((((__u64)(net))<<32)|((__u64)(addr)))
312 /* how net encodes type:number */
313 #define LNET_NETNUM(net) ((net) & 0xffff)
314 #define LNET_NETTYP(net) (((net) >> 16) & 0xffff)
315 #define LNET_MKNET(typ,num) ((((__u32)(typ))<<16)|((__u32)(num)))
318 #define ergo(a, b) (!(a) || (b))
319 /* logical equivalence */
320 #define equi(a, b) (!!(a) == !!(b))
323 # define CURRENT_TIME time(0)
326 /* --------------------------------------------------------------------
328 * Support for temporary event tracing with minimal Heisenberg effect.
329 * All stuff about lwt are put in arch/kp30.h
330 * -------------------------------------------------------------------- */
332 struct libcfs_device_userstate
334 int ldu_memhog_pages;
335 cfs_page_t *ldu_memhog_root_page;
338 /* what used to be in portals_lib.h */
340 # define MIN(a,b) (((a)<(b)) ? (a): (b))
343 # define MAX(a,b) (((a)>(b)) ? (a): (b))
346 #define MKSTR(ptr) ((ptr))? (ptr) : ""
348 static inline int size_round4 (int val)
350 return (val + 3) & (~0x3);
353 static inline int size_round (int val)
355 return (val + 7) & (~0x7);
358 static inline int size_round16(int val)
360 return (val + 0xf) & (~0xf);
363 static inline int size_round32(int val)
365 return (val + 0x1f) & (~0x1f);
368 static inline int size_round0(int val)
372 return (val + 1 + 7) & (~0x7);
375 static inline size_t round_strlen(char *fset)
377 return (size_t)size_round((int)strlen(fset) + 1);
380 #define LOGL(var,len,ptr) \
383 memcpy((char *)ptr, (const char *)var, len); \
384 ptr += size_round(len); \
387 #define LOGU(var,len,ptr) \
390 memcpy((char *)var, (const char *)ptr, len); \
391 ptr += size_round(len); \
394 #define LOGL0(var,len,ptr) \
398 memcpy((char *)ptr, (const char *)var, len); \
399 *((char *)(ptr) + len) = 0; \
400 ptr += size_round(len + 1); \
404 * USER LEVEL STUFF BELOW
407 #define LIBCFS_IOCTL_VERSION 0x0001000a
409 struct libcfs_ioctl_data {
426 __u32 ioc_plen1; /* buffers in userspace */
428 __u32 ioc_plen2; /* buffers in userspace */
435 struct libcfs_ioctl_hdr {
440 struct libcfs_debug_ioctl_data
442 struct libcfs_ioctl_hdr hdr;
447 #define LIBCFS_IOC_INIT(data) \
449 memset(&data, 0, sizeof(data)); \
450 data.ioc_version = LIBCFS_IOCTL_VERSION; \
451 data.ioc_len = sizeof(data); \
454 /* FIXME check conflict with lustre_lib.h */
455 #define LIBCFS_IOC_DEBUG_MASK _IOWR('f', 250, long)
457 static inline int libcfs_ioctl_packlen(struct libcfs_ioctl_data *data)
459 int len = sizeof(*data);
460 len += size_round(data->ioc_inllen1);
461 len += size_round(data->ioc_inllen2);
465 static inline int libcfs_ioctl_is_invalid(struct libcfs_ioctl_data *data)
467 if (data->ioc_len > (1<<30)) {
468 CERROR ("LIBCFS ioctl: ioc_len larger than 1<<30\n");
471 if (data->ioc_inllen1 > (1<<30)) {
472 CERROR ("LIBCFS ioctl: ioc_inllen1 larger than 1<<30\n");
475 if (data->ioc_inllen2 > (1<<30)) {
476 CERROR ("LIBCFS ioctl: ioc_inllen2 larger than 1<<30\n");
479 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
480 CERROR ("LIBCFS ioctl: inlbuf1 pointer but 0 length\n");
483 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
484 CERROR ("LIBCFS ioctl: inlbuf2 pointer but 0 length\n");
487 if (data->ioc_pbuf1 && !data->ioc_plen1) {
488 CERROR ("LIBCFS ioctl: pbuf1 pointer but 0 length\n");
491 if (data->ioc_pbuf2 && !data->ioc_plen2) {
492 CERROR ("LIBCFS ioctl: pbuf2 pointer but 0 length\n");
495 if (data->ioc_plen1 && !data->ioc_pbuf1) {
496 CERROR ("LIBCFS ioctl: plen1 nonzero but no pbuf1 pointer\n");
499 if (data->ioc_plen2 && !data->ioc_pbuf2) {
500 CERROR ("LIBCFS ioctl: plen2 nonzero but no pbuf2 pointer\n");
503 if ((__u32)libcfs_ioctl_packlen(data) != data->ioc_len ) {
504 CERROR ("LIBCFS ioctl: packlen != ioc_len\n");
507 if (data->ioc_inllen1 &&
508 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
509 CERROR ("LIBCFS ioctl: inlbuf1 not 0 terminated\n");
512 if (data->ioc_inllen2 &&
513 data->ioc_bulk[size_round(data->ioc_inllen1) +
514 data->ioc_inllen2 - 1] != '\0') {
515 CERROR ("LIBCFS ioctl: inlbuf2 not 0 terminated\n");
522 static inline int libcfs_ioctl_pack(struct libcfs_ioctl_data *data, char **pbuf,
526 struct libcfs_ioctl_data *overlay;
527 data->ioc_len = libcfs_ioctl_packlen(data);
528 data->ioc_version = LIBCFS_IOCTL_VERSION;
530 if (*pbuf && libcfs_ioctl_packlen(data) > max)
533 *pbuf = malloc(data->ioc_len);
537 overlay = (struct libcfs_ioctl_data *)*pbuf;
538 memcpy(*pbuf, data, sizeof(*data));
540 ptr = overlay->ioc_bulk;
541 if (data->ioc_inlbuf1)
542 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
543 if (data->ioc_inlbuf2)
544 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
545 if (libcfs_ioctl_is_invalid(overlay))
553 extern int libcfs_ioctl_getdata(char *buf, char *end, void *arg);
554 extern int libcfs_ioctl_popdata(void *arg, void *buf, int size);
558 /* ioctls for manipulating snapshots 30- */
559 #define IOC_LIBCFS_TYPE 'e'
560 #define IOC_LIBCFS_MIN_NR 30
562 #define IOC_LIBCFS_PANIC _IOWR('e', 30, IOCTL_LIBCFS_TYPE)
563 #define IOC_LIBCFS_CLEAR_DEBUG _IOWR('e', 31, IOCTL_LIBCFS_TYPE)
564 #define IOC_LIBCFS_MARK_DEBUG _IOWR('e', 32, IOCTL_LIBCFS_TYPE)
565 #define IOC_LIBCFS_LWT_CONTROL _IOWR('e', 33, IOCTL_LIBCFS_TYPE)
566 #define IOC_LIBCFS_LWT_SNAPSHOT _IOWR('e', 34, IOCTL_LIBCFS_TYPE)
567 #define IOC_LIBCFS_LWT_LOOKUP_STRING _IOWR('e', 35, IOCTL_LIBCFS_TYPE)
568 #define IOC_LIBCFS_MEMHOG _IOWR('e', 36, IOCTL_LIBCFS_TYPE)
569 #define IOC_LIBCFS_PING_TEST _IOWR('e', 37, IOCTL_LIBCFS_TYPE)
571 #define IOC_LIBCFS_GET_NI _IOWR('e', 50, IOCTL_LIBCFS_TYPE)
572 #define IOC_LIBCFS_FAIL_NID _IOWR('e', 51, IOCTL_LIBCFS_TYPE)
573 #define IOC_LIBCFS_ADD_ROUTE _IOWR('e', 52, IOCTL_LIBCFS_TYPE)
574 #define IOC_LIBCFS_DEL_ROUTE _IOWR('e', 53, IOCTL_LIBCFS_TYPE)
575 #define IOC_LIBCFS_GET_ROUTE _IOWR('e', 54, IOCTL_LIBCFS_TYPE)
576 #define IOC_LIBCFS_NOTIFY_ROUTER _IOWR('e', 55, IOCTL_LIBCFS_TYPE)
577 #define IOC_LIBCFS_UNCONFIGURE _IOWR('e', 56, IOCTL_LIBCFS_TYPE)
578 #define IOC_LIBCFS_PORTALS_COMPATIBILITY _IOWR('e', 57, IOCTL_LIBCFS_TYPE)
579 #define IOC_LIBCFS_LNET_DIST _IOWR('e', 58, IOCTL_LIBCFS_TYPE)
580 #define IOC_LIBCFS_CONFIGURE _IOWR('e', 59, IOCTL_LIBCFS_TYPE)
581 #define IOC_LIBCFS_TESTPROTOCOMPAT _IOWR('e', 60, IOCTL_LIBCFS_TYPE)
582 #define IOC_LIBCFS_PING _IOWR('e', 61, IOCTL_LIBCFS_TYPE)
583 #define IOC_LIBCFS_DEBUG_PEER _IOWR('e', 62, IOCTL_LIBCFS_TYPE)
584 #define IOC_LIBCFS_LNETST _IOWR('e', 63, IOCTL_LIBCFS_TYPE)
586 #define IOC_LIBCFS_REGISTER_MYNID _IOWR('e', 70, IOCTL_LIBCFS_TYPE)
587 #define IOC_LIBCFS_CLOSE_CONNECTION _IOWR('e', 71, IOCTL_LIBCFS_TYPE)
588 #define IOC_LIBCFS_PUSH_CONNECTION _IOWR('e', 72, IOCTL_LIBCFS_TYPE)
589 #define IOC_LIBCFS_GET_CONN _IOWR('e', 73, IOCTL_LIBCFS_TYPE)
590 #define IOC_LIBCFS_DEL_PEER _IOWR('e', 74, IOCTL_LIBCFS_TYPE)
591 #define IOC_LIBCFS_ADD_PEER _IOWR('e', 75, IOCTL_LIBCFS_TYPE)
592 #define IOC_LIBCFS_GET_PEER _IOWR('e', 76, IOCTL_LIBCFS_TYPE)
593 #define IOC_LIBCFS_GET_TXDESC _IOWR('e', 77, IOCTL_LIBCFS_TYPE)
594 #define IOC_LIBCFS_ADD_INTERFACE _IOWR('e', 78, IOCTL_LIBCFS_TYPE)
595 #define IOC_LIBCFS_DEL_INTERFACE _IOWR('e', 79, IOCTL_LIBCFS_TYPE)
596 #define IOC_LIBCFS_GET_INTERFACE _IOWR('e', 80, IOCTL_LIBCFS_TYPE)
597 #define IOC_LIBCFS_GET_GMID _IOWR('e', 81, IOCTL_LIBCFS_TYPE)
599 #define IOC_LIBCFS_MAX_NR 81
603 /* Only add to these values (i.e. don't ever change or redefine them):
604 * network addresses depend on them... */
620 DEBUG_DAEMON_START = 1,
621 DEBUG_DAEMON_STOP = 2,
622 DEBUG_DAEMON_PAUSE = 3,
623 DEBUG_DAEMON_CONTINUE = 4,
627 enum cfg_record_type {
628 PORTALS_CFG_TYPE = 1,
629 LUSTRE_CFG_TYPE = 123,
632 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
634 /* lustre_id output helper macros */
635 #define DLID4 "%lu/%lu/%lu/%lu"
638 (unsigned long)(id)->li_fid.lf_id, \
639 (unsigned long)(id)->li_fid.lf_group, \
640 (unsigned long)(id)->li_stc.u.e3s.l3s_ino, \
641 (unsigned long)(id)->li_stc.u.e3s.l3s_gen