Whamcloud - gitweb
Don't crash in expired_lock_main() with racing client eviction/lock completion.
[fs/lustre-release.git] / lnet / include / linux / kp30.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef _KP30_INCLUDED
5 #define _KP30_INCLUDED
6
7 #include <linux/libcfs.h>
8 #define PORTAL_DEBUG
9
10 #ifndef offsetof
11 # define offsetof(typ,memb)     ((int)((char *)&(((typ *)0)->memb)))
12 #endif
13
14 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
15
16 #ifdef __KERNEL__
17 # include <linux/vmalloc.h>
18 # include <linux/time.h>
19 # include <linux/slab.h>
20 # include <linux/interrupt.h>
21 # include <linux/highmem.h>
22 # include <linux/module.h>
23 # include <linux/version.h>
24 # include <portals/p30.h>
25 # include <linux/smp_lock.h>
26 # include <asm/atomic.h>
27
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
29 #define schedule_work schedule_task
30 #define prepare_work(wq,cb,cbdata)                                            \
31 do {                                                                          \
32         INIT_TQUEUE((wq), 0, 0);                                              \
33         PREPARE_TQUEUE((wq), (cb), (cbdata));                                 \
34 } while (0)
35
36 #define PageUptodate Page_Uptodate
37 #define our_recalc_sigpending(current) recalc_sigpending(current)
38 #define num_online_cpus() smp_num_cpus
39 static inline void our_cond_resched(void)
40 {
41         if (current->need_resched)
42                schedule ();
43 }
44 #define work_struct_t       struct tq_struct
45
46 #else
47
48 #define prepare_work(wq,cb,cbdata)                                            \
49 do {                                                                          \
50         INIT_WORK((wq), (void *)(cb), (void *)(cbdata));                      \
51 } while (0)
52 #define wait_on_page wait_on_page_locked
53 #define our_recalc_sigpending(current) recalc_sigpending()
54 #define strtok(a,b) strpbrk(a, b)
55 static inline void our_cond_resched(void)
56 {
57         cond_resched();
58 }
59 #define work_struct_t      struct work_struct
60
61 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
62
63 #ifdef PORTAL_DEBUG
64 extern void kportal_assertion_failed(char *expr, char *file, const char *func,
65                                      const int line);
66 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__,  \
67                                                         __FUNCTION__, __LINE__))
68 /* it would be great to dump_stack() here, but some kernels
69  * export it as show_stack() and I can't be bothered to
70  * proprely engage in that dance right now */
71 #define LASSERTF(cond, fmt...)                                                \
72         do {                                                                  \
73                 if (unlikely(!(cond))) {                                      \
74                         portals_debug_msg(0, D_EMERG,  __FILE__, __FUNCTION__,\
75                                           __LINE__,  CDEBUG_STACK,            \
76                                           "ASSERTION(" #cond ") failed:" fmt);\
77                         LBUG();                                               \
78                 }                                                             \
79         } while (0)
80
81 #else
82 #define LASSERT(e)
83 #define LASSERTF(cond, fmt...) do { } while (0)
84 #endif
85
86 #ifdef CONFIG_SMP
87 #define LASSERT_SPIN_LOCKED(lock) LASSERT(spin_is_locked(lock))
88 #else
89 #define LASSERT_SPIN_LOCKED(lock) do {} while(0)
90 #endif
91
92 #ifdef __arch_um__
93 #define LBUG_WITH_LOC(file, func, line)                                 \
94 do {                                                                    \
95         CEMERG("LBUG - trying to dump log to /tmp/lustre-log\n");       \
96         portals_debug_dumplog();                                        \
97         portals_run_lbug_upcall(file, func, line);                      \
98         panic("LBUG");                                                  \
99 } while (0)
100 #else
101 #define LBUG_WITH_LOC(file, func, line)                                 \
102 do {                                                                    \
103         CEMERG("LBUG\n");                                               \
104         portals_debug_dumplog();                                        \
105         portals_run_lbug_upcall(file, func, line);                      \
106         set_task_state(current, TASK_UNINTERRUPTIBLE);                  \
107         schedule();                                                     \
108 } while (0)
109 #endif /* __arch_um__ */
110
111 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
112
113 /*
114  * Memory
115  */
116 #ifdef PORTAL_DEBUG
117 extern atomic_t portal_kmemory;
118
119 # define portal_kmem_inc(ptr, size)                                           \
120 do {                                                                          \
121         atomic_add(size, &portal_kmemory);                                    \
122 } while (0)
123
124 # define portal_kmem_dec(ptr, size) do {                                      \
125         atomic_sub(size, &portal_kmemory);                                    \
126 } while (0)
127
128 #else
129 # define portal_kmem_inc(ptr, size) do {} while (0)
130 # define portal_kmem_dec(ptr, size) do {} while (0)
131 #endif /* PORTAL_DEBUG */
132
133 #define PORTAL_VMALLOC_SIZE        16384
134
135 #define PORTAL_ALLOC_GFP(ptr, size, mask)                                 \
136 do {                                                                      \
137         LASSERT(!in_interrupt() ||                                        \
138                (size <= PORTAL_VMALLOC_SIZE && mask == GFP_ATOMIC));      \
139         if ((size) > PORTAL_VMALLOC_SIZE)                                 \
140                 (ptr) = vmalloc(size);                                    \
141         else                                                              \
142                 (ptr) = kmalloc((size), (mask));                          \
143         if ((ptr) == NULL) {                                              \
144                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
145                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
146                 CERROR("PORTALS: %d total bytes allocated by portals\n",  \
147                        atomic_read(&portal_kmemory));                     \
148         } else {                                                          \
149                 portal_kmem_inc((ptr), (size));                           \
150                 memset((ptr), 0, (size));                                 \
151         }                                                                 \
152         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
153                (int)(size), (ptr), atomic_read (&portal_kmemory));        \
154 } while (0)
155
156 #define PORTAL_ALLOC(ptr, size) \
157         PORTAL_ALLOC_GFP(ptr, size, GFP_NOFS)
158
159 #define PORTAL_ALLOC_ATOMIC(ptr, size) \
160         PORTAL_ALLOC_GFP(ptr, size, GFP_ATOMIC)
161
162 #define PORTAL_FREE(ptr, size)                                          \
163 do {                                                                    \
164         int s = (size);                                                 \
165         if ((ptr) == NULL) {                                            \
166                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
167                        "%s:%d\n", s, __FILE__, __LINE__);               \
168                 break;                                                  \
169         }                                                               \
170         if (s > PORTAL_VMALLOC_SIZE)                                    \
171                 vfree(ptr);                                             \
172         else                                                            \
173                 kfree(ptr);                                             \
174         portal_kmem_dec((ptr), s);                                      \
175         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
176                s, (ptr), atomic_read(&portal_kmemory));                 \
177 } while (0)
178
179 /* ------------------------------------------------------------------- */
180
181 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
182
183 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
184 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
185
186 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
187 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
188
189 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
190 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
191 #else
192
193 #define PORTAL_SYMBOL_REGISTER(x)
194 #define PORTAL_SYMBOL_UNREGISTER(x)
195
196 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
197 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
198
199 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
200 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
201
202 #endif
203
204 /******************************************************************************/
205
206 #ifdef PORTALS_PROFILING
207 #define prof_enum(FOO) PROF__##FOO
208 enum {
209         prof_enum(our_recvmsg),
210         prof_enum(our_sendmsg),
211         prof_enum(socknal_recv),
212         prof_enum(lib_parse),
213         prof_enum(conn_list_walk),
214         prof_enum(memcpy),
215         prof_enum(lib_finalize),
216         prof_enum(pingcli_time),
217         prof_enum(gmnal_send),
218         prof_enum(gmnal_recv),
219         MAX_PROFS
220 };
221
222 struct prof_ent {
223         char *str;
224         /* hrmph.  wrap-tastic. */
225         u32       starts;
226         u32       finishes;
227         cycles_t  total_cycles;
228         cycles_t  start;
229         cycles_t  end;
230 };
231
232 extern struct prof_ent prof_ents[MAX_PROFS];
233
234 #define PROF_START(FOO)                                         \
235         do {                                                    \
236                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
237                 pe->starts++;                                   \
238                 pe->start = get_cycles();                       \
239         } while (0)
240
241 #define PROF_FINISH(FOO)                                        \
242         do {                                                    \
243                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
244                 pe->finishes++;                                 \
245                 pe->end = get_cycles();                         \
246                 pe->total_cycles += (pe->end - pe->start);      \
247         } while (0)
248 #else /* !PORTALS_PROFILING */
249 #define PROF_START(FOO) do {} while(0)
250 #define PROF_FINISH(FOO) do {} while(0)
251 #endif /* PORTALS_PROFILING */
252
253 /* debug.c */
254 extern spinlock_t stack_backtrace_lock;
255
256 char *portals_debug_dumpstack(void);
257 void portals_run_upcall(char **argv);
258 void portals_run_lbug_upcall(char * file, const char *fn, const int line);
259 void portals_debug_dumplog(void);
260 int portals_debug_init(unsigned long bufsize);
261 int portals_debug_cleanup(void);
262 int portals_debug_clear_buffer(void);
263 int portals_debug_mark_buffer(char *text);
264 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
265                              char *file, unsigned int size);
266 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
267 #if (__GNUC__)
268 /* Use the special GNU C __attribute__ hack to have the compiler check the
269  * printf style argument string against the actual argument count and
270  * types.
271  */
272 #ifdef printf
273 # warning printf has been defined as a macro...
274 # undef printf
275 #endif
276 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
277                        const int line, unsigned long stack,
278                        char *format, ...)
279         __attribute__ ((format (printf, 7, 8)));
280 #else
281 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
282                        const int line, unsigned long stack,
283                        const char *format, ...);
284 #endif /* __GNUC__ */
285 void portals_debug_set_level(unsigned int debug_level);
286
287 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
288 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
289 # define time(a) CURRENT_TIME
290
291 extern void kportal_daemonize (char *name);
292 extern void kportal_blockallsigs (void);
293
294 #else  /* !__KERNEL__ */
295 # include <stdio.h>
296 # include <stdlib.h>
297 #ifndef __CYGWIN__
298 # include <stdint.h>
299 #else
300 # include <cygwin-ioctl.h>
301 #endif
302 # include <unistd.h>
303 # include <time.h>
304 # include <asm/types.h>
305 # ifndef DEBUG_SUBSYSTEM
306 #  define DEBUG_SUBSYSTEM S_UNDEFINED
307 # endif
308 # ifdef PORTAL_DEBUG
309 #  undef NDEBUG
310 #  include <assert.h>
311 #  define LASSERT(e)     assert(e)
312 #  define LASSERTF(cond, args...)     assert(cond)
313 # else
314 #  define LASSERT(e)
315 #  define LASSERTF(cond, args...) do { } while (0)
316 # endif
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 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
321     printf("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
322            (subsys), (mask), (long)time(0), file, fn, line,                   \
323            getpid() , stack, ## a);
324 #endif
325
326 /* support decl needed both by kernel and liblustre */
327 char *portals_nid2str(int nal, ptl_nid_t nid, char *str);
328
329 #ifndef CURRENT_TIME
330 # define CURRENT_TIME time(0)
331 #endif
332
333 /******************************************************************************/
334 /* Light-weight trace
335  * Support for temporary event tracing with minimal Heisenberg effect. */
336 #define LWT_SUPPORT  0
337
338 #define LWT_MEMORY   (64<<20)
339 #define LWT_MAX_CPUS 4
340
341 typedef struct {
342         cycles_t    lwte_when;
343         char       *lwte_where;
344         void       *lwte_task;
345         long        lwte_p1;
346         long        lwte_p2;
347         long        lwte_p3;
348         long        lwte_p4;
349 #if BITS_PER_LONG > 32
350         long        lwte_pad;
351 #endif
352 } lwt_event_t;
353
354 #if LWT_SUPPORT
355 #ifdef __KERNEL__
356 #define LWT_EVENTS_PER_PAGE (PAGE_SIZE / sizeof (lwt_event_t))
357
358 typedef struct _lwt_page {
359         struct list_head     lwtp_list;
360         struct page         *lwtp_page;
361         lwt_event_t         *lwtp_events;
362 } lwt_page_t;
363
364 typedef struct {
365         int                lwtc_current_index;
366         lwt_page_t        *lwtc_current_page;
367 } lwt_cpu_t;
368
369 extern int       lwt_enabled;
370 extern lwt_cpu_t lwt_cpus[];
371
372 extern int  lwt_init (void);
373 extern void lwt_fini (void);
374 extern int  lwt_lookup_string (int *size, char *knlptr,
375                                char *usrptr, int usrsize);
376 extern int  lwt_control (int enable, int clear);
377 extern int  lwt_snapshot (cycles_t *now, int *ncpu, int *total_size,
378                           void *user_ptr, int user_size);
379
380 /* Note that we _don't_ define LWT_EVENT at all if LWT_SUPPORT isn't set.
381  * This stuff is meant for finding specific problems; it never stays in
382  * production code... */
383
384 #define LWTSTR(n)       #n
385 #define LWTWHERE(f,l)   f ":" LWTSTR(l)
386
387 #define LWT_EVENT(p1, p2, p3, p4)                                       \
388 do {                                                                    \
389         unsigned long    flags;                                         \
390         lwt_cpu_t       *cpu;                                           \
391         lwt_page_t      *p;                                             \
392         lwt_event_t     *e;                                             \
393                                                                         \
394         local_irq_save (flags);                                         \
395                                                                         \
396         if (lwt_enabled) {                                              \
397                 cpu = &lwt_cpus[smp_processor_id()];                    \
398                 p = cpu->lwtc_current_page;                             \
399                 e = &p->lwtp_events[cpu->lwtc_current_index++];         \
400                                                                         \
401                 if (cpu->lwtc_current_index >= LWT_EVENTS_PER_PAGE) {   \
402                         cpu->lwtc_current_page =                        \
403                                 list_entry (p->lwtp_list.next,          \
404                                             lwt_page_t, lwtp_list);     \
405                         cpu->lwtc_current_index = 0;                    \
406                 }                                                       \
407                                                                         \
408                 e->lwte_when  = get_cycles();                           \
409                 e->lwte_where = LWTWHERE(__FILE__,__LINE__);            \
410                 e->lwte_task  = current;                                \
411                 e->lwte_p1    = (long)(p1);                             \
412                 e->lwte_p2    = (long)(p2);                             \
413                 e->lwte_p3    = (long)(p3);                             \
414                 e->lwte_p4    = (long)(p4);                             \
415         }                                                               \
416                                                                         \
417         local_irq_restore (flags);                                      \
418 } while (0)
419 #else  /* __KERNEL__ */
420 #define LWT_EVENT(p1,p2,p3,p4)     /* no userland implementation yet */
421 #endif /* __KERNEL__ */
422 #endif /* LWT_SUPPORT */
423
424 struct portals_device_userstate
425 {
426         int          pdu_memhog_pages;
427         struct page *pdu_memhog_root_page;
428 };
429
430 #include <linux/portals_lib.h>
431
432 /*
433  * USER LEVEL STUFF BELOW
434  */
435
436 #define PORTALS_CFG_VERSION 0x00010001;
437
438 struct portals_cfg {
439         __u32 pcfg_version;
440         __u32 pcfg_command;
441
442         __u32 pcfg_nal;
443         __u32 pcfg_flags;
444
445         __u32 pcfg_gw_nal;
446         __u64 pcfg_nid;
447         __u64 pcfg_nid2;
448         __u64 pcfg_nid3;
449         __u32 pcfg_id;
450         __u32 pcfg_misc;
451         __u32 pcfg_fd;
452         __u32 pcfg_count;
453         __u32 pcfg_size;
454         __u32 pcfg_wait;
455
456         __u32 pcfg_plen1; /* buffers in userspace */
457         char *pcfg_pbuf1;
458         __u32 pcfg_plen2; /* buffers in userspace */
459         char *pcfg_pbuf2;
460 };
461
462 #define PCFG_INIT(pcfg, cmd)                            \
463 do {                                                    \
464         memset(&pcfg, 0, sizeof(pcfg));                 \
465         pcfg.pcfg_version = PORTALS_CFG_VERSION;        \
466         pcfg.pcfg_command = (cmd);                      \
467                                                         \
468 } while (0)
469
470 #define PORTAL_IOCTL_VERSION 0x00010007
471 #define PING_SYNC       0
472 #define PING_ASYNC      1
473
474 struct portal_ioctl_hdr {
475         __u32 ioc_len;
476         __u32 ioc_version;
477 };
478
479 struct portals_debug_ioctl_data
480 {
481         struct portal_ioctl_hdr hdr;
482         unsigned int subs;
483         unsigned int debug;
484 };
485
486 #define PORTAL_IOC_INIT(data)                           \
487 do {                                                    \
488         memset(&data, 0, sizeof(data));                 \
489         data.ioc_version = PORTAL_IOCTL_VERSION;        \
490         data.ioc_len = sizeof(data);                    \
491 } while (0)
492
493 /* FIXME check conflict with lustre_lib.h */
494 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
495
496 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
497 {
498         int len = sizeof(*data);
499         len += size_round(data->ioc_inllen1);
500         len += size_round(data->ioc_inllen2);
501         return len;
502 }
503
504 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
505 {
506         if (data->ioc_len > (1<<30)) {
507                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
508                 return 1;
509         }
510         if (data->ioc_inllen1 > (1<<30)) {
511                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
512                 return 1;
513         }
514         if (data->ioc_inllen2 > (1<<30)) {
515                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
516                 return 1;
517         }
518         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
519                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
520                 return 1;
521         }
522         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
523                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
524                 return 1;
525         }
526         if (data->ioc_pbuf1 && !data->ioc_plen1) {
527                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
528                 return 1;
529         }
530         if (data->ioc_pbuf2 && !data->ioc_plen2) {
531                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
532                 return 1;
533         }
534         if (data->ioc_plen1 && !data->ioc_pbuf1) {
535                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
536                 return 1;
537         }
538         if (data->ioc_plen2 && !data->ioc_pbuf2) {
539                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
540                 return 1;
541         }
542         if (portal_ioctl_packlen(data) != data->ioc_len ) {
543                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
544                 return 1;
545         }
546         if (data->ioc_inllen1 &&
547             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
548                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
549                 return 1;
550         }
551         if (data->ioc_inllen2 &&
552             data->ioc_bulk[size_round(data->ioc_inllen1) +
553                            data->ioc_inllen2 - 1] != '\0') {
554                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
555                 return 1;
556         }
557         return 0;
558 }
559
560 #ifndef __KERNEL__
561 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
562                                     int max)
563 {
564         char *ptr;
565         struct portal_ioctl_data *overlay;
566         data->ioc_len = portal_ioctl_packlen(data);
567         data->ioc_version = PORTAL_IOCTL_VERSION;
568
569         if (*pbuf && portal_ioctl_packlen(data) > max)
570                 return 1;
571         if (*pbuf == NULL) {
572                 *pbuf = malloc(data->ioc_len);
573         }
574         if (!*pbuf)
575                 return 1;
576         overlay = (struct portal_ioctl_data *)*pbuf;
577         memcpy(*pbuf, data, sizeof(*data));
578
579         ptr = overlay->ioc_bulk;
580         if (data->ioc_inlbuf1)
581                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
582         if (data->ioc_inlbuf2)
583                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
584         if (portal_ioctl_is_invalid(overlay))
585                 return 1;
586
587         return 0;
588 }
589 #else
590 #include <asm/uaccess.h>
591
592 /* buffer MUST be at least the size of portal_ioctl_hdr */
593 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
594 {
595         struct portal_ioctl_hdr *hdr;
596         struct portal_ioctl_data *data;
597         int err;
598         ENTRY;
599
600         hdr = (struct portal_ioctl_hdr *)buf;
601         data = (struct portal_ioctl_data *)buf;
602
603         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
604         if ( err ) {
605                 EXIT;
606                 return err;
607         }
608
609         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
610                 CERROR ("PORTALS: version mismatch kernel vs application\n");
611                 return -EINVAL;
612         }
613
614         if (hdr->ioc_len + buf >= end) {
615                 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
616                 return -EINVAL;
617         }
618
619
620         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
621                 CERROR ("PORTALS: user buffer too small for ioctl\n");
622                 return -EINVAL;
623         }
624
625         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
626         if ( err ) {
627                 EXIT;
628                 return err;
629         }
630
631         if (portal_ioctl_is_invalid(data)) {
632                 CERROR ("PORTALS: ioctl not correctly formatted\n");
633                 return -EINVAL;
634         }
635
636         if (data->ioc_inllen1) {
637                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
638         }
639
640         if (data->ioc_inllen2) {
641                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
642                         size_round(data->ioc_inllen1);
643         }
644
645         EXIT;
646         return 0;
647 }
648 #endif
649
650 /* ioctls for manipulating snapshots 30- */
651 #define IOC_PORTAL_TYPE                   'e'
652 #define IOC_PORTAL_MIN_NR                 30
653
654 #define IOC_PORTAL_PING                    _IOWR('e', 30, long)
655 #define IOC_PORTAL_GET_DEBUG               _IOWR('e', 31, long)
656 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, long)
657 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, long)
658 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, long)
659 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 35, long)
660 #define IOC_PORTAL_GET_NID                 _IOWR('e', 36, long)
661 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 37, long)
662 #define IOC_PORTAL_SET_DAEMON              _IOWR('e', 38, long)
663 #define IOC_PORTAL_LWT_CONTROL             _IOWR('e', 39, long)
664 #define IOC_PORTAL_LWT_SNAPSHOT            _IOWR('e', 40, long)
665 #define IOC_PORTAL_LWT_LOOKUP_STRING       _IOWR('e', 41, long)
666 #define IOC_PORTAL_MEMHOG                  _IOWR('e', 42, long)
667 #define IOC_PORTAL_MAX_NR                             42
668
669 enum {
670         QSWNAL    = 1,
671         SOCKNAL   = 2,
672         GMNAL     = 3,
673         /*          4 unused */
674         TCPNAL    = 5,
675         SCIMACNAL = 6,
676         ROUTER    = 7,
677         IBNAL     = 8,
678         NAL_ENUM_END_MARKER
679 };
680
681 #ifdef __KERNEL__
682 extern ptl_handle_ni_t  kqswnal_ni;
683 extern ptl_handle_ni_t  ksocknal_ni;
684 extern ptl_handle_ni_t  kgmnal_ni;
685 extern ptl_handle_ni_t  kibnal_ni;
686 extern ptl_handle_ni_t  kscimacnal_ni;
687 #endif
688
689 #define PTL_NALFMT_SIZE         16
690
691 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
692
693 #define NAL_CMD_REGISTER_PEER_FD     100
694 #define NAL_CMD_CLOSE_CONNECTION     101
695 #define NAL_CMD_REGISTER_MYNID       102
696 #define NAL_CMD_PUSH_CONNECTION      103
697 #define NAL_CMD_GET_CONN             104
698 #define NAL_CMD_DEL_AUTOCONN         105
699 #define NAL_CMD_ADD_AUTOCONN         106
700 #define NAL_CMD_GET_AUTOCONN         107
701 #define NAL_CMD_GET_TXDESC           108
702 #define NAL_CMD_ADD_ROUTE            109
703 #define NAL_CMD_DEL_ROUTE            110
704 #define NAL_CMD_GET_ROUTE            111
705 #define NAL_CMD_NOTIFY_ROUTER        112
706
707 enum {
708         DEBUG_DAEMON_START       =  1,
709         DEBUG_DAEMON_STOP        =  2,
710         DEBUG_DAEMON_PAUSE       =  3,
711         DEBUG_DAEMON_CONTINUE    =  4,
712 };
713
714 /* module.c */
715 typedef int (*nal_cmd_handler_t)(struct portals_cfg *, void * private);
716 int kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private);
717 int kportal_nal_unregister(int nal);
718
719 enum cfg_record_type {
720         PORTALS_CFG_TYPE = 1,
721         LUSTRE_CFG_TYPE = 123,
722 };
723
724 typedef int (*cfg_record_cb_t)(enum cfg_record_type, int len, void *data);
725 int kportal_nal_cmd(struct portals_cfg *);
726
727 ptl_handle_ni_t *kportal_get_ni (int nal);
728 void kportal_put_ni (int nal);
729
730 #ifdef __CYGWIN__
731 # ifndef BITS_PER_LONG
732 #  if (~0UL) == 0xffffffffUL
733 #   define BITS_PER_LONG 32
734 #  else
735 #   define BITS_PER_LONG 64
736 #  endif
737 # endif
738 #endif
739
740 #if defined(__x86_64__)
741 # define LPU64 "%Lu"
742 # define LPD64 "%Ld"
743 # define LPX64 "%#Lx"
744 # define LPSZ  "%lu"
745 # define LPSSZ "%ld"
746 # define LP_POISON ((void *)0x5a5a5a5a5a5a5a5a)
747 #elif (BITS_PER_LONG == 32 || __WORDSIZE == 32)
748 # define LPU64 "%Lu"
749 # define LPD64 "%Ld"
750 # define LPX64 "%#Lx"
751 # define LPSZ  "%u"
752 # define LPSSZ "%d"
753 # define LP_POISON ((void *)0x5a5a5a5a)
754 #elif (BITS_PER_LONG == 64 || __WORDSIZE == 64)
755 # define LPU64 "%lu"
756 # define LPD64 "%ld"
757 # define LPX64 "%#lx"
758 # define LPSZ  "%lu"
759 # define LPSSZ "%ld"
760 # define LP_POISON ((void *)0x5a5a5a5a5a5a5a5a)
761 #endif
762 #ifndef LPU64
763 # error "No word size defined"
764 #endif
765
766 #endif