1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
7 #include <linux/libcfs.h>
11 # include <linux/vmalloc.h>
12 # include <linux/time.h>
13 # include <linux/slab.h>
14 # include <linux/interrupt.h>
15 # include <linux/highmem.h>
16 # include <linux/module.h>
17 # include <linux/version.h>
18 # include <portals/p30.h>
19 # include <linux/smp_lock.h>
20 # include <asm/atomic.h>
22 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
23 #define schedule_work schedule_task
24 #define prepare_work(wq,cb,cbdata) \
26 INIT_TQUEUE((wq), 0, 0); \
27 PREPARE_TQUEUE((wq), (cb), (cbdata)); \
30 #define PageUptodate Page_Uptodate
31 #define our_recalc_sigpending(current) recalc_sigpending(current)
32 #define num_online_cpus() smp_num_cpus
33 static inline void our_cond_resched(void)
35 if (current->need_resched)
38 #define work_struct_t struct tq_struct
42 #define prepare_work(wq,cb,cbdata) \
44 INIT_WORK((wq), (void *)(cb), (void *)(cbdata)); \
46 #define wait_on_page wait_on_page_locked
47 #define our_recalc_sigpending(current) recalc_sigpending()
48 #define strtok(a,b) strpbrk(a, b)
49 static inline void our_cond_resched(void)
53 #define work_struct_t struct work_struct
55 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
58 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
60 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__, \
61 __FUNCTION__, __LINE__))
62 /* it would be great to dump_stack() here, but some kernels
63 * export it as show_stack() and I can't be bothered to
64 * proprely engage in that dance right now */
65 #define LASSERTF(cond, fmt...) \
67 if (unlikely(!(cond))) { \
68 portals_debug_msg(0, D_EMERG, __FILE__, __FUNCTION__,\
69 __LINE__, CDEBUG_STACK, \
70 "ASSERTION(" #cond ") failed:" fmt);\
77 #define LASSERTF(cond, fmt...) do { } while (0)
81 #define LASSERT_SPIN_LOCKED(lock) LASSERT(spin_is_locked(lock))
83 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
87 #define LBUG_WITH_LOC(file, func, line) \
89 CEMERG("LBUG - trying to dump log to /tmp/lustre-log\n"); \
90 portals_debug_dumplog(); \
91 portals_run_lbug_upcall(file, func, line); \
95 #define LBUG_WITH_LOC(file, func, line) \
98 portals_debug_dumplog(); \
99 portals_run_lbug_upcall(file, func, line); \
100 set_task_state(current, TASK_UNINTERRUPTIBLE); \
103 #endif /* __arch_um__ */
105 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
111 extern atomic_t portal_kmemory;
113 # define portal_kmem_inc(ptr, size) \
115 atomic_add(size, &portal_kmemory); \
118 # define portal_kmem_dec(ptr, size) do { \
119 atomic_sub(size, &portal_kmemory); \
123 # define portal_kmem_inc(ptr, size) do {} while (0)
124 # define portal_kmem_dec(ptr, size) do {} while (0)
125 #endif /* PORTAL_DEBUG */
127 #define PORTAL_VMALLOC_SIZE 16384
129 #define PORTAL_ALLOC_GFP(ptr, size, mask) \
131 LASSERT(!in_interrupt() || \
132 (size <= PORTAL_VMALLOC_SIZE && mask == GFP_ATOMIC)); \
133 if ((size) > PORTAL_VMALLOC_SIZE) \
134 (ptr) = vmalloc(size); \
136 (ptr) = kmalloc((size), (mask)); \
137 if ((ptr) == NULL) { \
138 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
139 #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
140 CERROR("PORTALS: %d total bytes allocated by portals\n", \
141 atomic_read(&portal_kmemory)); \
143 portal_kmem_inc((ptr), (size)); \
144 memset((ptr), 0, (size)); \
146 CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n", \
147 (int)(size), (ptr), atomic_read (&portal_kmemory)); \
150 #define PORTAL_ALLOC(ptr, size) \
151 PORTAL_ALLOC_GFP(ptr, size, GFP_NOFS)
153 #define PORTAL_ALLOC_ATOMIC(ptr, size) \
154 PORTAL_ALLOC_GFP(ptr, size, GFP_ATOMIC)
156 #define PORTAL_FREE(ptr, size) \
159 if ((ptr) == NULL) { \
160 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at " \
161 "%s:%d\n", s, __FILE__, __LINE__); \
164 if (s > PORTAL_VMALLOC_SIZE) \
168 portal_kmem_dec((ptr), s); \
169 CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n", \
170 s, (ptr), atomic_read(&portal_kmemory)); \
173 /* ------------------------------------------------------------------- */
175 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
177 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
178 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
180 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
181 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
183 #define PORTAL_MODULE_USE MOD_INC_USE_COUNT
184 #define PORTAL_MODULE_UNUSE MOD_DEC_USE_COUNT
187 #define PORTAL_SYMBOL_REGISTER(x)
188 #define PORTAL_SYMBOL_UNREGISTER(x)
190 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
191 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
193 #define PORTAL_MODULE_USE try_module_get(THIS_MODULE)
194 #define PORTAL_MODULE_UNUSE module_put(THIS_MODULE)
198 /******************************************************************************/
200 #ifdef PORTALS_PROFILING
201 #define prof_enum(FOO) PROF__##FOO
203 prof_enum(our_recvmsg),
204 prof_enum(our_sendmsg),
205 prof_enum(socknal_recv),
206 prof_enum(lib_parse),
207 prof_enum(conn_list_walk),
209 prof_enum(lib_finalize),
210 prof_enum(pingcli_time),
211 prof_enum(gmnal_send),
212 prof_enum(gmnal_recv),
218 /* hrmph. wrap-tastic. */
221 cycles_t total_cycles;
226 extern struct prof_ent prof_ents[MAX_PROFS];
228 #define PROF_START(FOO) \
230 struct prof_ent *pe = &prof_ents[PROF__##FOO]; \
232 pe->start = get_cycles(); \
235 #define PROF_FINISH(FOO) \
237 struct prof_ent *pe = &prof_ents[PROF__##FOO]; \
239 pe->end = get_cycles(); \
240 pe->total_cycles += (pe->end - pe->start); \
242 #else /* !PORTALS_PROFILING */
243 #define PROF_START(FOO) do {} while(0)
244 #define PROF_FINISH(FOO) do {} while(0)
245 #endif /* PORTALS_PROFILING */
248 extern spinlock_t stack_backtrace_lock;
250 char *portals_debug_dumpstack(void);
251 void portals_run_upcall(char **argv);
252 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
253 void portals_debug_dumplog(void);
254 int portals_debug_init(unsigned long bufsize);
255 int portals_debug_cleanup(void);
256 int portals_debug_clear_buffer(void);
257 int portals_debug_mark_buffer(char *text);
258 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
259 char *file, unsigned int size);
260 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
262 /* Use the special GNU C __attribute__ hack to have the compiler check the
263 * printf style argument string against the actual argument count and
267 # warning printf has been defined as a macro...
270 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
271 const int line, unsigned long stack,
273 __attribute__ ((format (printf, 7, 8)));
275 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
276 const int line, unsigned long stack,
277 const char *format, ...);
278 #endif /* __GNUC__ */
279 void portals_debug_set_level(unsigned int debug_level);
281 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
282 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
283 # define time(a) CURRENT_TIME
285 extern void kportal_daemonize (char *name);
286 extern void kportal_blockallsigs (void);
288 #else /* !__KERNEL__ */
294 # include <cygwin-ioctl.h>
299 # include <asm/types.h>
300 # ifndef DEBUG_SUBSYSTEM
301 # define DEBUG_SUBSYSTEM S_UNDEFINED
306 # define LASSERT(e) assert(e)
307 # define LASSERTF(cond, args...) \
315 # define LASSERTF(cond, args...) do { } while (0)
317 # define printk(format, args...) printf (format, ## args)
318 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
319 # define PORTAL_FREE(a, b) do { free(a); } while (0);
320 void portals_debug_dumplog(void);
321 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
322 printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format, \
323 (subsys), (mask), (long)time(0), file, fn, line, \
324 getpid() , stack, ## a);
327 /* support decl needed both by kernel and liblustre */
328 char *portals_nid2str(int nal, ptl_nid_t nid, char *str);
329 char *portals_id2str(int nal, ptl_process_id_t nid, char *str);
332 # define CURRENT_TIME time(0)
335 /******************************************************************************/
336 /* Light-weight trace
337 * Support for temporary event tracing with minimal Heisenberg effect. */
338 #define LWT_SUPPORT 0
340 #define LWT_MEMORY (64<<20)
341 #define LWT_MAX_CPUS 4
351 #if BITS_PER_LONG > 32
358 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
360 typedef struct _lwt_page {
361 struct list_head lwtp_list;
362 struct page *lwtp_page;
363 lwt_event_t *lwtp_events;
367 int lwtc_current_index;
368 lwt_page_t *lwtc_current_page;
371 extern int lwt_enabled;
372 extern lwt_cpu_t lwt_cpus[];
374 extern int lwt_init (void);
375 extern void lwt_fini (void);
376 extern int lwt_lookup_string (int *size, char *knlptr,
377 char *usrptr, int usrsize);
378 extern int lwt_control (int enable, int clear);
379 extern int lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
380 void *user_ptr, int user_size);
382 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
383 * This stuff is meant for finding specific problems; it never stays in
384 * production code... */
387 #define LWTWHERE(f,l) f ":" LWTSTR(l)
389 #define LWT_EVENT(p1, p2, p3, p4) \
391 unsigned long flags; \
396 local_irq_save (flags); \
399 cpu = &lwt_cpus[smp_processor_id()]; \
400 p = cpu->lwtc_current_page; \
401 e = &p->lwtp_events[cpu->lwtc_current_index++]; \
403 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) { \
404 cpu->lwtc_current_page = \
405 list_entry (p->lwtp_list.next, \
406 lwt_page_t, lwtp_list); \
407 cpu->lwtc_current_index = 0; \
410 e->lwte_when = get_cycles(); \
411 e->lwte_where = LWTWHERE(__FILE__,__LINE__); \
412 e->lwte_task = current; \
413 e->lwte_p1 = (long)(p1); \
414 e->lwte_p2 = (long)(p2); \
415 e->lwte_p3 = (long)(p3); \
416 e->lwte_p4 = (long)(p4); \
419 local_irq_restore (flags); \
421 #else /* __KERNEL__ */
422 #define LWT_EVENT(p1,p2,p3,p4) /* no userland implementation yet */
423 #endif /* __KERNEL__ */
424 #endif /* LWT_SUPPORT */
426 struct portals_device_userstate
428 int pdu_memhog_pages;
429 struct page *pdu_memhog_root_page;
432 #include <linux/portals_lib.h>
435 * USER LEVEL STUFF BELOW
438 #define PORTAL_IOCTL_VERSION 0x00010007
442 struct portal_ioctl_hdr {
447 struct portals_debug_ioctl_data
449 struct portal_ioctl_hdr hdr;
454 #define PORTAL_IOC_INIT(data) \
456 memset(&data, 0, sizeof(data)); \
457 data.ioc_version = PORTAL_IOCTL_VERSION; \
458 data.ioc_len = sizeof(data); \
461 /* FIXME check conflict with lustre_lib.h */
462 #define PTL_IOC_DEBUG_MASK _IOWR('f', 250, long)
464 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
466 int len = sizeof(*data);
467 len += size_round(data->ioc_inllen1);
468 len += size_round(data->ioc_inllen2);
472 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
474 if (data->ioc_len > (1<<30)) {
475 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
478 if (data->ioc_inllen1 > (1<<30)) {
479 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
482 if (data->ioc_inllen2 > (1<<30)) {
483 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
486 if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
487 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
490 if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
491 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
494 if (data->ioc_pbuf1 && !data->ioc_plen1) {
495 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
498 if (data->ioc_pbuf2 && !data->ioc_plen2) {
499 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
502 if (data->ioc_plen1 && !data->ioc_pbuf1) {
503 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
506 if (data->ioc_plen2 && !data->ioc_pbuf2) {
507 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
510 if (portal_ioctl_packlen(data) != data->ioc_len ) {
511 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
514 if (data->ioc_inllen1 &&
515 data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
516 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
519 if (data->ioc_inllen2 &&
520 data->ioc_bulk[size_round(data->ioc_inllen1) +
521 data->ioc_inllen2 - 1] != '\0') {
522 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
529 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
533 struct portal_ioctl_data *overlay;
534 data->ioc_len = portal_ioctl_packlen(data);
535 data->ioc_version = PORTAL_IOCTL_VERSION;
537 if (*pbuf && portal_ioctl_packlen(data) > max)
540 *pbuf = malloc(data->ioc_len);
544 overlay = (struct portal_ioctl_data *)*pbuf;
545 memcpy(*pbuf, data, sizeof(*data));
547 ptr = overlay->ioc_bulk;
548 if (data->ioc_inlbuf1)
549 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
550 if (data->ioc_inlbuf2)
551 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
552 if (portal_ioctl_is_invalid(overlay))
558 #include <asm/uaccess.h>
560 /* buffer MUST be at least the size of portal_ioctl_hdr */
561 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
563 struct portal_ioctl_hdr *hdr;
564 struct portal_ioctl_data *data;
568 hdr = (struct portal_ioctl_hdr *)buf;
569 data = (struct portal_ioctl_data *)buf;
571 err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
577 if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
578 CERROR ("PORTALS: version mismatch kernel vs application\n");
582 if (hdr->ioc_len + buf >= end) {
583 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
588 if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
589 CERROR ("PORTALS: user buffer too small for ioctl\n");
593 err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
599 if (portal_ioctl_is_invalid(data)) {
600 CERROR ("PORTALS: ioctl not correctly formatted\n");
604 if (data->ioc_inllen1) {
605 data->ioc_inlbuf1 = &data->ioc_bulk[0];
608 if (data->ioc_inllen2) {
609 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
610 size_round(data->ioc_inllen1);
618 /* ioctls for manipulating snapshots 30- */
619 #define IOC_PORTAL_TYPE 'e'
620 #define IOC_PORTAL_MIN_NR 30
622 #define IOC_PORTAL_PING _IOWR('e', 30, long)
623 #define IOC_PORTAL_GET_DEBUG _IOWR('e', 31, long)
624 #define IOC_PORTAL_CLEAR_DEBUG _IOWR('e', 32, long)
625 #define IOC_PORTAL_MARK_DEBUG _IOWR('e', 33, long)
626 #define IOC_PORTAL_PANIC _IOWR('e', 34, long)
627 #define IOC_PORTAL_NAL_CMD _IOWR('e', 35, long)
628 #define IOC_PORTAL_GET_NID _IOWR('e', 36, long)
629 #define IOC_PORTAL_FAIL_NID _IOWR('e', 37, long)
630 #define IOC_PORTAL_SET_DAEMON _IOWR('e', 38, long)
631 #define IOC_PORTAL_LWT_CONTROL _IOWR('e', 39, long)
632 #define IOC_PORTAL_LWT_SNAPSHOT _IOWR('e', 40, long)
633 #define IOC_PORTAL_LWT_LOOKUP_STRING _IOWR('e', 41, long)
634 #define IOC_PORTAL_MEMHOG _IOWR('e', 42, long)
635 #define IOC_PORTAL_MAX_NR 42
648 #define PTL_NALFMT_SIZE 30 /* %u:%u.%u.%u.%u,%u (10+4+4+4+3+4+1) */
650 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
652 #define NAL_CMD_REGISTER_PEER_FD 100
653 #define NAL_CMD_CLOSE_CONNECTION 101
654 #define NAL_CMD_REGISTER_MYNID 102
655 #define NAL_CMD_PUSH_CONNECTION 103
656 #define NAL_CMD_GET_CONN 104
657 #define NAL_CMD_DEL_AUTOCONN 105
658 #define NAL_CMD_ADD_AUTOCONN 106
659 #define NAL_CMD_GET_AUTOCONN 107
660 #define NAL_CMD_GET_TXDESC 108
661 #define NAL_CMD_ADD_ROUTE 109
662 #define NAL_CMD_DEL_ROUTE 110
663 #define NAL_CMD_GET_ROUTE 111
664 #define NAL_CMD_NOTIFY_ROUTER 112
667 DEBUG_DAEMON_START = 1,
668 DEBUG_DAEMON_STOP = 2,
669 DEBUG_DAEMON_PAUSE = 3,
670 DEBUG_DAEMON_CONTINUE = 4,
674 enum cfg_record_type {
675 PORTALS_CFG_TYPE = 1,
676 LUSTRE_CFG_TYPE = 123,
679 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
682 # ifndef BITS_PER_LONG
683 # if (~0UL) == 0xffffffffUL
684 # define BITS_PER_LONG 32
686 # define BITS_PER_LONG 64
691 #if BITS_PER_LONG > 32
692 # define LI_POISON ((int)0x5a5a5a5a5a5a5a5a)
693 # define LL_POISON ((long)0x5a5a5a5a5a5a5a5a)
694 # define LP_POISON ((void *)(long)0x5a5a5a5a5a5a5a5a)
696 # define LI_POISON ((int)0x5a5a5a5a)
697 # define LL_POISON ((long)0x5a5a5a5a)
698 # define LP_POISON ((void *)(long)0x5a5a5a5a)
701 #if defined(__x86_64__)
704 # define LPX64 "%#Lx"
707 #elif (BITS_PER_LONG == 32 || __WORDSIZE == 32)
710 # define LPX64 "%#Lx"
713 #elif (BITS_PER_LONG == 64 || __WORDSIZE == 64)
716 # define LPX64 "%#lx"
721 # error "No word size defined"