Whamcloud - gitweb
merge b_devel into HEAD (20030626 merge tag) for 0.7.1
[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
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 #ifndef CONFIG_SMP
17 # define smp_processor_id() 0
18 #endif
19
20 /*
21  *  Debugging
22  */
23 extern unsigned int portal_subsystem_debug;
24 extern unsigned int portal_stack;
25 extern unsigned int portal_debug;
26 extern unsigned int portal_printk;
27 /* Debugging subsystems  (8 bit ID)
28  *
29  * If you add debug subsystem #32, you need to send email to phil, because
30  * you're going to break kernel subsystem debug filtering. */
31 #define S_UNDEFINED    (0 << 24)
32 #define S_MDC          (1 << 24)
33 #define S_MDS          (2 << 24)
34 #define S_OSC          (3 << 24)
35 #define S_OST          (4 << 24)
36 #define S_CLASS        (5 << 24)
37 #define S_OBDFS        (6 << 24) /* obsolete */
38 #define S_LLITE        (7 << 24)
39 #define S_RPC          (8 << 24)
40 #define S_EXT2OBD      (9 << 24) /* obsolete */
41 #define S_PORTALS     (10 << 24)
42 #define S_SOCKNAL     (11 << 24)
43 #define S_QSWNAL      (12 << 24)
44 #define S_PINGER      (13 << 24)
45 #define S_FILTER      (14 << 24)
46 #define S_TRACE       (15 << 24) /* obsolete */
47 #define S_ECHO        (16 << 24)
48 #define S_LDLM        (17 << 24)
49 #define S_LOV         (18 << 24)
50 #define S_GMNAL       (19 << 24)
51 #define S_PTLROUTER   (20 << 24)
52 #define S_COBD        (21 << 24)
53 #define S_PTLBD       (22 << 24)
54 #define S_LOG         (23 << 24)
55
56 /* If you change these values, please keep portals/linux/utils/debug.c
57  * up to date! */
58
59 /* Debugging masks (24 bits, non-overlapping) */
60 #define D_TRACE     (1 << 0) /* ENTRY/EXIT markers */
61 #define D_INODE     (1 << 1)
62 #define D_SUPER     (1 << 2)
63 #define D_EXT2      (1 << 3) /* anything from ext2_debug */
64 #define D_MALLOC    (1 << 4) /* print malloc, free information */
65 #define D_CACHE     (1 << 5) /* cache-related items */
66 #define D_INFO      (1 << 6) /* general information */
67 #define D_IOCTL     (1 << 7) /* ioctl related information */
68 #define D_BLOCKS    (1 << 8) /* ext2 block allocation */
69 #define D_NET       (1 << 9) /* network communications */
70 #define D_WARNING   (1 << 10)
71 #define D_BUFFS     (1 << 11)
72 #define D_OTHER     (1 << 12)
73 #define D_DENTRY    (1 << 13)
74 #define D_PORTALS   (1 << 14) /* ENTRY/EXIT markers */
75 #define D_PAGE      (1 << 15) /* bulk page handling */
76 #define D_DLMTRACE  (1 << 16)
77 #define D_ERROR     (1 << 17) /* CERROR(...) == CDEBUG (D_ERROR, ...) */
78 #define D_EMERG     (1 << 18) /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
79 #define D_HA        (1 << 19) /* recovery and failover */
80 #define D_RPCTRACE  (1 << 20) /* for distributed debugging */
81 #define D_VFSTRACE  (1 << 21)
82
83 #ifndef __KERNEL__
84 #define THREAD_SIZE 8192
85 #endif
86 #ifdef  __ia64__
87 #define CDEBUG_STACK() (THREAD_SIZE -                                      \
88                         ((unsigned long)__builtin_dwarf_cfa() &            \
89                          (THREAD_SIZE - 1)))
90 #else
91 #define CDEBUG_STACK() (THREAD_SIZE -                                      \
92                         ((unsigned long)__builtin_frame_address(0) &       \
93                          (THREAD_SIZE - 1)))
94 #endif
95
96 #ifdef __KERNEL__
97 #define CHECK_STACK(stack)                                                    \
98         do {                                                                  \
99                 if ((stack) > 3*THREAD_SIZE/4 && (stack) > portal_stack) {    \
100                         portals_debug_msg(DEBUG_SUBSYSTEM, D_ERROR,           \
101                                           __FILE__, __FUNCTION__, __LINE__,   \
102                                           (stack),                            \
103                                           "maximum lustre stack %u\n",        \
104                                           portal_stack = (stack));            \
105                       /*panic("LBUG");*/                                      \
106                 }                                                             \
107         } while (0)
108 #else
109 #define CHECK_STACK(stack) do { } while(0)
110 #endif
111
112 #if 1
113 #define CDEBUG(mask, format, a...)                                            \
114 do {                                                                          \
115         CHECK_STACK(CDEBUG_STACK());                                          \
116         if (!(mask) || ((mask) & (D_ERROR | D_EMERG)) ||                      \
117             (portal_debug & (mask) &&                                         \
118              portal_subsystem_debug & (1 << (DEBUG_SUBSYSTEM >> 24))))        \
119                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
120                                   __FILE__, __FUNCTION__, __LINE__,           \
121                                   CDEBUG_STACK(), format , ## a);             \
122 } while (0)
123
124 #define CWARN(format, a...) CDEBUG(D_WARNING, format, ## a)
125 #define CERROR(format, a...) CDEBUG(D_ERROR, format, ## a)
126 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
127
128 #define GOTO(label, rc)                                                 \
129 do {                                                                    \
130         long GOTO__ret = (long)(rc);                                    \
131         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
132                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
133                (signed long)GOTO__ret);                                 \
134         goto label;                                                     \
135 } while (0)
136
137 #define RETURN(rc)                                                      \
138 do {                                                                    \
139         typeof(rc) RETURN__ret = (rc);                                  \
140         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
141                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
142         return RETURN__ret;                                             \
143 } while (0)
144
145 #define ENTRY                                                           \
146 do {                                                                    \
147         CDEBUG(D_TRACE, "Process entered\n");                           \
148 } while (0)
149
150 #define EXIT                                                            \
151 do {                                                                    \
152         CDEBUG(D_TRACE, "Process leaving\n");                           \
153 } while(0)
154 #else
155 #define CDEBUG(mask, format, a...)      do { } while (0)
156 #define CWARN(format, a...)             do { } while (0)
157 #define CERROR(format, a...)            printk("<3>" format, ## a)
158 #define CEMERG(format, a...)            printk("<0>" format, ## a)
159 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
160 #define RETURN(rc)                      return (rc)
161 #define ENTRY                           do { } while (0)
162 #define EXIT                            do { } while (0)
163 #endif
164
165
166 #ifdef __KERNEL__
167 # include <linux/vmalloc.h>
168 # include <linux/time.h>
169 # include <linux/slab.h>
170 # include <linux/interrupt.h>
171 # include <linux/highmem.h>
172 # include <linux/module.h>
173 # include <linux/version.h>
174 # include <portals/lib-nal.h>
175 # include <linux/smp_lock.h>
176 # include <asm/atomic.h>
177
178 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
179 #define schedule_work schedule_task
180 #define prepare_work(wq,cb,cbdata)                                            \
181 do {                                                                          \
182         INIT_TQUEUE((wq), 0, 0);                                              \
183         PREPARE_TQUEUE((wq), (cb), (cbdata));                                 \
184 } while (0)
185
186 #define ll_invalidate_inode_pages invalidate_inode_pages
187 #define PageUptodate Page_Uptodate
188 #define our_recalc_sigpending(current) recalc_sigpending(current)
189 #define num_online_cpus() smp_num_cpus
190 static inline void our_cond_resched(void)
191 {
192         if (current->need_resched)
193                schedule ();
194 }
195
196 #else
197
198 #define prepare_work(wq,cb,cbdata)                                            \
199 do {                                                                          \
200         INIT_WORK((wq), (void *)(cb), (void *)(cbdata));                      \
201 } while (0)
202 #define ll_invalidate_inode_pages(inode) invalidate_inode_pages((inode)->i_mapping)
203 #define wait_on_page wait_on_page_locked
204 #define our_recalc_sigpending(current) recalc_sigpending()
205 #define strtok(a,b) strpbrk(a, b)
206 static inline void our_cond_resched(void)
207 {
208         cond_resched();
209 }
210 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0) */
211
212 #ifdef PORTAL_DEBUG
213 extern void kportal_assertion_failed(char *expr,char *file,char *func,int line);
214 #define LASSERT(e) ((e) ? 0 : kportal_assertion_failed( #e , __FILE__,  \
215                                                         __FUNCTION__, __LINE__))
216 #else
217 #define LASSERT(e)
218 #endif
219
220 #ifdef __arch_um__
221 #define LBUG_WITH_LOC(file, func, line)                                 \
222 do {                                                                    \
223         CEMERG("LBUG - trying to dump log to /tmp/lustre-log\n");       \
224         portals_debug_dumplog();                                        \
225         portals_run_lbug_upcall(file, func, line);                      \
226         panic("LBUG");                                                  \
227 } while (0)
228 #else
229 #define LBUG_WITH_LOC(file, func, line)                                 \
230 do {                                                                    \
231         CEMERG("LBUG\n");                                               \
232         portals_debug_dumplog();                                        \
233         portals_run_lbug_upcall(file, func, line);                      \
234         set_task_state(current, TASK_UNINTERRUPTIBLE);                  \
235         schedule();                                                     \
236 } while (0)
237 #endif /* __arch_um__ */
238
239 #define LBUG() LBUG_WITH_LOC(__FILE__, __FUNCTION__, __LINE__)
240
241 /*
242  * Memory
243  */
244 #ifdef PORTAL_DEBUG
245 extern atomic_t portal_kmemory;
246
247 # define portal_kmem_inc(ptr, size)                                           \
248 do {                                                                          \
249         atomic_add(size, &portal_kmemory);                                    \
250 } while (0)
251
252 # define portal_kmem_dec(ptr, size) do {                                      \
253         atomic_sub(size, &portal_kmemory);                                    \
254 } while (0)
255
256 #else
257 # define portal_kmem_inc(ptr, size) do {} while (0)
258 # define portal_kmem_dec(ptr, size) do {} while (0)
259 #endif /* PORTAL_DEBUG */
260
261 #define PORTAL_VMALLOC_SIZE        16384
262
263 #define PORTAL_ALLOC(ptr, size)                                           \
264 do {                                                                      \
265         LASSERT (!in_interrupt());                                        \
266         if ((size) > PORTAL_VMALLOC_SIZE)                                 \
267                 (ptr) = vmalloc(size);                                    \
268         else                                                              \
269                 (ptr) = kmalloc((size), GFP_NOFS);                        \
270         if ((ptr) == NULL)                                                \
271                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc '"\
272                        #ptr "' = %d)\n", __FILE__, __LINE__, (int)(size));\
273         else {                                                            \
274                 portal_kmem_inc((ptr), (size));                           \
275                 memset((ptr), 0, (size));                                 \
276         }                                                                 \
277         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %d at %p (tot %d).\n",    \
278                (int)(size), (ptr), atomic_read (&portal_kmemory));        \
279 } while (0)
280
281 #define PORTAL_FREE(ptr, size)                                          \
282 do {                                                                    \
283         int s = (size);                                                 \
284         if ((ptr) == NULL) {                                            \
285                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
286                        "%s:%d\n", s, __FILE__, __LINE__);               \
287                 break;                                                  \
288         }                                                               \
289         if (s > PORTAL_VMALLOC_SIZE)                                    \
290                 vfree(ptr);                                             \
291         else                                                            \
292                 kfree(ptr);                                             \
293         portal_kmem_dec((ptr), s);                                      \
294         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
295                s, (ptr), atomic_read(&portal_kmemory));                 \
296 } while (0)
297
298 #define PORTAL_SLAB_ALLOC(ptr, slab, size)                                \
299 do {                                                                      \
300         LASSERT(!in_interrupt());                                         \
301         (ptr) = kmem_cache_alloc((slab), SLAB_KERNEL);                    \
302         if ((ptr) == NULL) {                                              \
303                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc"  \
304                        " '" #ptr "' from slab '" #slab "')\n", __FILE__,  \
305                        __LINE__);                                         \
306         } else {                                                          \
307                 portal_kmem_inc((ptr), (size));                           \
308                 memset((ptr), 0, (size));                                 \
309         }                                                                 \
310         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %ld at %p (tot %d).\n",   \
311                (int)(size), (ptr), atomic_read(&portal_kmemory));         \
312 } while (0)
313
314 #define PORTAL_SLAB_FREE(ptr, slab, size)                               \
315 do {                                                                    \
316         int s = (size);                                                 \
317         if ((ptr) == NULL) {                                            \
318                 CERROR("PORTALS: free NULL '" #ptr "' (%d bytes) at "   \
319                        "%s:%d\n", s, __FILE__, __LINE__);               \
320                 break;                                                  \
321         }                                                               \
322         memset((ptr), 0x5a, s);                                         \
323         kmem_cache_free((slab), ptr);                                   \
324         portal_kmem_dec((ptr), s);                                      \
325         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %d at %p (tot %d).\n",     \
326                s, (ptr), atomic_read (&portal_kmemory));                \
327 } while (0)
328
329 /* ------------------------------------------------------------------- */
330
331 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
332
333 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
334 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
335
336 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
337 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
338
339 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
340 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
341 #else
342
343 #define PORTAL_SYMBOL_REGISTER(x)
344 #define PORTAL_SYMBOL_UNREGISTER(x)
345
346 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
347 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
348
349 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
350 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
351
352 #endif
353
354 /******************************************************************************/
355 /* Kernel Portals Router interface */
356
357 typedef void (*kpr_fwd_callback_t)(void *arg, int error); // completion callback
358
359 /* space for routing targets to stash "stuff" in a forwarded packet */
360 typedef union {
361         long long        _alignment;
362         void            *_space[16];            /* scale with CPU arch */
363 } kprfd_scratch_t;
364
365 /* Kernel Portals Routing Forwarded message Descriptor */
366 typedef struct {
367         struct list_head     kprfd_list;        /* stash in queues (routing target can use) */
368         ptl_nid_t            kprfd_target_nid;  /* final destination NID */
369         ptl_nid_t            kprfd_gateway_nid; /* gateway NID */
370         int                  kprfd_nob;         /* # message bytes (including header) */
371         int                  kprfd_niov;        /* # message frags (including header) */
372         struct iovec        *kprfd_iov;         /* message fragments */
373         void                *kprfd_router_arg;  // originating NAL's router arg
374         kpr_fwd_callback_t   kprfd_callback;    /* completion callback */
375         void                *kprfd_callback_arg; /* completion callback arg */
376         kprfd_scratch_t      kprfd_scratch;    // scratchpad for routing targets
377 } kpr_fwd_desc_t;
378
379 typedef void  (*kpr_fwd_t)(void *arg, kpr_fwd_desc_t *fwd);
380
381 /* NAL's routing interface (Kernel Portals Routing Nal Interface) */
382 typedef const struct {
383         int             kprni_nalid;    /* NAL's id */
384         void           *kprni_arg;      /* Arg to pass when calling into NAL */
385         kpr_fwd_t       kprni_fwd;      /* NAL's forwarding entrypoint */
386 } kpr_nal_interface_t;
387
388 /* Router's routing interface (Kernel Portals Routing Router Interface) */
389 typedef const struct {
390         /* register the calling NAL with the router and get back the handle for
391          * subsequent calls */
392         int     (*kprri_register) (kpr_nal_interface_t *nal_interface,
393                                    void **router_arg);
394
395         /* ask the router to find a gateway that forwards to 'nid' and is a peer
396          * of the calling NAL */
397         int     (*kprri_lookup) (void *router_arg, ptl_nid_t nid,
398                                  ptl_nid_t *gateway_nid);
399
400         /* hand a packet over to the router for forwarding */
401         kpr_fwd_t kprri_fwd_start;
402
403         /* hand a packet back to the router for completion */
404         void    (*kprri_fwd_done) (void *router_arg, kpr_fwd_desc_t *fwd,
405                                    int error);
406
407         /* the calling NAL is shutting down */
408         void    (*kprri_shutdown) (void *router_arg);
409
410         /* deregister the calling NAL with the router */
411         void    (*kprri_deregister) (void *router_arg);
412
413 } kpr_router_interface_t;
414
415 /* Convenient struct for NAL to stash router interface/args */
416 typedef struct {
417         kpr_router_interface_t  *kpr_interface;
418         void                    *kpr_arg;
419 } kpr_router_t;
420
421 /* Router's control interface (Kernel Portals Routing Control Interface) */
422 typedef const struct {
423         int     (*kprci_add_route)(int gateway_nal, ptl_nid_t gateway_nid,
424                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
425         int     (*kprci_del_route)(ptl_nid_t nid);
426         int     (*kprci_get_route)(int index, int *gateway_nal,
427                                    ptl_nid_t *gateway, ptl_nid_t *lo_nid,
428                                    ptl_nid_t *hi_nid);
429 } kpr_control_interface_t;
430
431 extern kpr_control_interface_t  kpr_control_interface;
432 extern kpr_router_interface_t   kpr_router_interface;
433
434 static inline int
435 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
436 {
437         int    rc;
438
439         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
440         if (router->kpr_interface == NULL)
441                 return (-ENOENT);
442
443         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
444         if (rc != 0)
445                 router->kpr_interface = NULL;
446
447         PORTAL_SYMBOL_PUT (kpr_router_interface);
448         return (rc);
449 }
450
451 static inline int
452 kpr_routing (kpr_router_t *router)
453 {
454         return (router->kpr_interface != NULL);
455 }
456
457 static inline int
458 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, ptl_nid_t *gateway_nid)
459 {
460         if (!kpr_routing (router))
461                 return (-EHOSTUNREACH);
462
463         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid,
464                                                     gateway_nid));
465 }
466
467 static inline void
468 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid,
469               int nob, int niov, struct iovec *iov,
470               kpr_fwd_callback_t callback, void *callback_arg)
471 {
472         fwd->kprfd_target_nid   = nid;
473         fwd->kprfd_gateway_nid  = nid;
474         fwd->kprfd_nob          = nob;
475         fwd->kprfd_niov         = niov;
476         fwd->kprfd_iov          = iov;
477         fwd->kprfd_callback     = callback;
478         fwd->kprfd_callback_arg = callback_arg;
479 }
480
481 static inline void
482 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
483 {
484         if (!kpr_routing (router))
485                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -EHOSTUNREACH);
486         else
487                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
488 }
489
490 static inline void
491 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
492 {
493         LASSERT (kpr_routing (router));
494         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
495 }
496
497 static inline void
498 kpr_shutdown (kpr_router_t *router)
499 {
500         if (kpr_routing (router))
501                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
502 }
503
504 static inline void
505 kpr_deregister (kpr_router_t *router)
506 {
507         if (!kpr_routing (router))
508                 return;
509         router->kpr_interface->kprri_deregister (router->kpr_arg);
510         router->kpr_interface = NULL;
511 }
512
513 /******************************************************************************/
514
515 #ifdef PORTALS_PROFILING
516 #define prof_enum(FOO) PROF__##FOO
517 enum {
518         prof_enum(our_recvmsg),
519         prof_enum(our_sendmsg),
520         prof_enum(socknal_recv),
521         prof_enum(lib_parse),
522         prof_enum(conn_list_walk),
523         prof_enum(memcpy),
524         prof_enum(lib_finalize),
525         prof_enum(pingcli_time),
526         prof_enum(gmnal_send),
527         prof_enum(gmnal_recv),
528         MAX_PROFS
529 };
530
531 struct prof_ent {
532         char *str;
533         /* hrmph.  wrap-tastic. */
534         u32       starts;
535         u32       finishes;
536         cycles_t  total_cycles;
537         cycles_t  start;
538         cycles_t  end;
539 };
540
541 extern struct prof_ent prof_ents[MAX_PROFS];
542
543 #define PROF_START(FOO)                                         \
544         do {                                                    \
545                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
546                 pe->starts++;                                   \
547                 pe->start = get_cycles();                       \
548         } while (0)
549
550 #define PROF_FINISH(FOO)                                        \
551         do {                                                    \
552                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
553                 pe->finishes++;                                 \
554                 pe->end = get_cycles();                         \
555                 pe->total_cycles += (pe->end - pe->start);      \
556         } while (0)
557 #else /* !PORTALS_PROFILING */
558 #define PROF_START(FOO) do {} while(0)
559 #define PROF_FINISH(FOO) do {} while(0)
560 #endif /* PORTALS_PROFILING */
561
562 /* debug.c */
563 void portals_run_lbug_upcall(char * file, char *fn, int line);
564 void portals_debug_dumplog(void);
565 int portals_debug_init(unsigned long bufsize);
566 int portals_debug_cleanup(void);
567 int portals_debug_clear_buffer(void);
568 int portals_debug_mark_buffer(char *text);
569 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
570                 char *file, unsigned int size);
571 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
572 #if (__GNUC__)
573 /* Use the special GNU C __attribute__ hack to have the compiler check the
574  * printf style argument string against the actual argument count and
575  * types.
576  */
577 #ifdef printf
578 # warning printf has been defined as a macro...
579 # undef printf
580 #endif
581 void portals_debug_msg (int subsys, int mask, char *file, char *fn, int line,
582                         unsigned long stack, const char *format, ...)
583         __attribute__ ((format (printf, 7, 8)));
584 #else
585 void portals_debug_msg (int subsys, int mask, char *file, char *fn,
586                         int line, unsigned long stack,
587                         const char *format, ...);
588 #endif /* __GNUC__ */
589 void portals_debug_set_level(unsigned int debug_level);
590
591 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
592 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
593 # define time(a) CURRENT_TIME
594
595 extern void kportal_daemonize (char *name);
596 extern void kportal_blockallsigs (void);
597
598 #else  /* !__KERNEL__ */
599 # include <stdio.h>
600 # include <stdlib.h>
601 #ifndef __CYGWIN__
602 # include <stdint.h>
603 #endif
604 # include <unistd.h>
605 # include <time.h>
606 # include <asm/types.h>
607 # ifndef DEBUG_SUBSYSTEM
608 #  define DEBUG_SUBSYSTEM S_UNDEFINED
609 # endif
610 # ifdef PORTAL_DEBUG
611 #  undef NDEBUG
612 #  include <assert.h>
613 #  define LASSERT(e)     assert(e)
614 # else
615 #  define LASSERT(e)
616 # endif
617 # define printk(format, args...) printf (format, ## args)
618 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
619 # define PORTAL_FREE(a, b) do { free(a); } while (0);
620 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
621     printf ("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
622             (subsys) >> 24, (mask), (long)time(0), file, fn, line,            \
623             getpid() , stack, ## a);
624 #endif
625
626 #ifndef CURRENT_TIME
627 # define CURRENT_TIME time(0)
628 #endif
629
630 #include <linux/portals_lib.h>
631
632 /*
633  * USER LEVEL STUFF BELOW
634  */
635
636 #define PORTAL_IOCTL_VERSION 0x00010007
637 #define PING_SYNC       0
638 #define PING_ASYNC      1
639
640 struct portal_ioctl_data {
641         __u32 ioc_len;
642         __u32 ioc_version;
643         __u64 ioc_nid;
644         __u64 ioc_nid2;
645         __u64 ioc_nid3;
646         __u32 ioc_count;
647         __u32 ioc_nal;
648         __u32 ioc_nal_cmd;
649         __u32 ioc_fd;
650         __u32 ioc_id;
651
652         __u32 ioc_flags;
653         __u32 ioc_size;
654
655         __u32 ioc_wait;
656         __u32 ioc_timeout;
657         __u32 ioc_misc;
658
659         __u32 ioc_inllen1;
660         char *ioc_inlbuf1;
661         __u32 ioc_inllen2;
662         char *ioc_inlbuf2;
663
664         __u32 ioc_plen1; /* buffers in userspace */
665         char *ioc_pbuf1;
666         __u32 ioc_plen2; /* buffers in userspace */
667         char *ioc_pbuf2;
668
669         char ioc_bulk[0];
670 };
671
672 struct portal_ioctl_hdr {
673         __u32 ioc_len;
674         __u32 ioc_version;
675 };
676
677 struct portals_debug_ioctl_data
678 {
679         struct portal_ioctl_hdr hdr;
680         unsigned int subs;
681         unsigned int debug;
682 };
683
684 #define PORTAL_IOC_INIT(data)                           \
685 do {                                                    \
686         memset(&data, 0, sizeof(data));                 \
687         data.ioc_version = PORTAL_IOCTL_VERSION;        \
688         data.ioc_len = sizeof(data);                    \
689 } while (0)
690
691 /* FIXME check conflict with lustre_lib.h */
692 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
693
694 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
695 {
696         int len = sizeof(*data);
697         len += size_round(data->ioc_inllen1);
698         len += size_round(data->ioc_inllen2);
699         return len;
700 }
701
702 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
703 {
704         if (data->ioc_len > (1<<30)) {
705                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
706                 return 1;
707         }
708         if (data->ioc_inllen1 > (1<<30)) {
709                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
710                 return 1;
711         }
712         if (data->ioc_inllen2 > (1<<30)) {
713                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
714                 return 1;
715         }
716         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
717                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
718                 return 1;
719         }
720         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
721                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
722                 return 1;
723         }
724         if (data->ioc_pbuf1 && !data->ioc_plen1) {
725                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
726                 return 1;
727         }
728         if (data->ioc_pbuf2 && !data->ioc_plen2) {
729                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
730                 return 1;
731         }
732         if (data->ioc_plen1 && !data->ioc_pbuf1) {
733                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
734                 return 1;
735         }
736         if (data->ioc_plen2 && !data->ioc_pbuf2) {
737                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
738                 return 1;
739         }
740         if (portal_ioctl_packlen(data) != data->ioc_len ) {
741                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
742                 return 1;
743         }
744         if (data->ioc_inllen1 &&
745             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
746                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
747                 return 1;
748         }
749         if (data->ioc_inllen2 &&
750             data->ioc_bulk[size_round(data->ioc_inllen1) +
751                            data->ioc_inllen2 - 1] != '\0') {
752                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
753                 return 1;
754         }
755         return 0;
756 }
757
758 #ifndef __KERNEL__
759 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
760                                     int max)
761 {
762         char *ptr;
763         struct portal_ioctl_data *overlay;
764         data->ioc_len = portal_ioctl_packlen(data);
765         data->ioc_version = PORTAL_IOCTL_VERSION;
766
767         if (*pbuf && portal_ioctl_packlen(data) > max)
768                 return 1;
769         if (*pbuf == NULL) {
770                 *pbuf = malloc(data->ioc_len);
771         }
772         if (!*pbuf)
773                 return 1;
774         overlay = (struct portal_ioctl_data *)*pbuf;
775         memcpy(*pbuf, data, sizeof(*data));
776
777         ptr = overlay->ioc_bulk;
778         if (data->ioc_inlbuf1)
779                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
780         if (data->ioc_inlbuf2)
781                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
782         if (portal_ioctl_is_invalid(overlay))
783                 return 1;
784
785         return 0;
786 }
787 #else
788 #include <asm/uaccess.h>
789
790 /* buffer MUST be at least the size of portal_ioctl_hdr */
791 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
792 {
793         struct portal_ioctl_hdr *hdr;
794         struct portal_ioctl_data *data;
795         int err;
796         ENTRY;
797
798         hdr = (struct portal_ioctl_hdr *)buf;
799         data = (struct portal_ioctl_data *)buf;
800
801         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
802         if ( err ) {
803                 EXIT;
804                 return err;
805         }
806
807         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
808                 CERROR ("PORTALS: version mismatch kernel vs application\n");
809                 return -EINVAL;
810         }
811
812         if (hdr->ioc_len + buf >= end) {
813                 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
814                 return -EINVAL;
815         }
816
817
818         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
819                 CERROR ("PORTALS: user buffer too small for ioctl\n");
820                 return -EINVAL;
821         }
822
823         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
824         if ( err ) {
825                 EXIT;
826                 return err;
827         }
828
829         if (portal_ioctl_is_invalid(data)) {
830                 CERROR ("PORTALS: ioctl not correctly formatted\n");
831                 return -EINVAL;
832         }
833
834         if (data->ioc_inllen1) {
835                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
836         }
837
838         if (data->ioc_inllen2) {
839                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
840                         size_round(data->ioc_inllen1);
841         }
842
843         EXIT;
844         return 0;
845 }
846 #endif
847
848 /* ioctls for manipulating snapshots 30- */
849 #define IOC_PORTAL_TYPE                   'e'
850 #define IOC_PORTAL_MIN_NR                 30
851
852 #define IOC_PORTAL_PING                    _IOWR('e', 30, long)
853 #define IOC_PORTAL_GET_DEBUG               _IOWR('e', 31, long)
854 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, long)
855 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, long)
856 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, long)
857 #define IOC_PORTAL_ADD_ROUTE               _IOWR('e', 35, long)
858 #define IOC_PORTAL_DEL_ROUTE               _IOWR('e', 36, long)
859 #define IOC_PORTAL_GET_ROUTE               _IOWR('e', 37, long)
860 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 38, long)
861 #define IOC_PORTAL_GET_NID                 _IOWR('e', 39, long)
862 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 40, long)
863 #define IOC_PORTAL_SET_DAEMON              _IOWR('e', 41, long)
864
865 #define IOC_PORTAL_MAX_NR               41
866
867 enum {
868         QSWNAL  =  1,
869         SOCKNAL,
870         GMNAL,
871         TOENAL,
872         TCPNAL,
873         SCIMACNAL,
874         NAL_ENUM_END_MARKER
875 };
876
877 #ifdef __KERNEL__
878 extern ptl_handle_ni_t  kqswnal_ni;
879 extern ptl_handle_ni_t  ksocknal_ni;
880 extern ptl_handle_ni_t  ktoenal_ni;
881 extern ptl_handle_ni_t  kgmnal_ni;
882 extern ptl_handle_ni_t  kscimacnal_ni;
883 #endif
884
885 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
886
887 #define NAL_CMD_REGISTER_PEER_FD     100
888 #define NAL_CMD_CLOSE_CONNECTION     101
889 #define NAL_CMD_REGISTER_MYNID       102
890 #define NAL_CMD_PUSH_CONNECTION      103
891
892 enum {
893         DEBUG_DAEMON_START       =  1,
894         DEBUG_DAEMON_STOP        =  2,
895         DEBUG_DAEMON_PAUSE       =  3,
896         DEBUG_DAEMON_CONTINUE    =  4,
897 };
898
899 /* XXX remove to lustre ASAP */
900 struct lustre_peer {
901         ptl_nid_t       peer_nid;
902         ptl_handle_ni_t peer_ni;
903 };
904
905 /* module.c */
906 typedef int (*nal_cmd_handler_t)(struct portal_ioctl_data *, void * private);
907 int kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private);
908 int kportal_nal_unregister(int nal);
909
910 ptl_handle_ni_t *kportal_get_ni (int nal);
911 void kportal_put_ni (int nal);
912
913 #ifdef __CYGWIN__
914 #ifndef BITS_PER_LONG
915 #if (~0UL) == 0xffffffffUL
916 #define BITS_PER_LONG 32
917 #else
918 #define BITS_PER_LONG 64
919 #endif
920 #endif
921 #endif
922
923 #if (BITS_PER_LONG == 32 || __WORDSIZE == 32)
924 # define LPU64 "%Lu"
925 # define LPD64 "%Ld"
926 # define LPX64 "%#Lx"
927 # define LPSZ  "%u"
928 # define LPSSZ "%d"
929 #endif
930 #if (BITS_PER_LONG == 64 || __WORDSIZE == 64)
931 # define LPU64 "%lu"
932 # define LPD64 "%ld"
933 # define LPX64 "%#lx"
934 # define LPSZ  "%lu"
935 # define LPSSZ "%ld"
936 #endif
937 #ifndef LPU64
938 # error "No word size defined"
939 #endif
940
941 #endif