Whamcloud - gitweb
- merge 0.7rc1 from b_devel to HEAD (20030612 merge point)
[fs/lustre-release.git] / lustre / portals / 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         long s = size;                                                    \
266         LASSERT (!in_interrupt());                                        \
267         if (s > PORTAL_VMALLOC_SIZE)                                      \
268                 (ptr) = vmalloc(s);                                       \
269         else                                                              \
270                 (ptr) = kmalloc(s, GFP_NOFS);                             \
271         if ((ptr) == NULL)                                                \
272                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc"  \
273                        " '" #ptr "' = %ld)\n", __FILE__, __LINE__, s);    \
274         else {                                                            \
275                 portal_kmem_inc((ptr), s);                                \
276                 memset((ptr), 0, s);                                      \
277         }                                                                 \
278         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %ld at %p (tot %d).\n",   \
279                s, (ptr), atomic_read (&portal_kmemory));                  \
280 } while (0)
281
282 #define PORTAL_FREE(ptr, size)                                          \
283 do {                                                                    \
284         long s = (size);                                                \
285         if ((ptr) == NULL) {                                            \
286                 CERROR("PORTALS: free NULL '" #ptr "' (%ld bytes) at "  \
287                        "%s:%d\n", s, __FILE__, __LINE__);               \
288                 break;                                                  \
289         }                                                               \
290         if (s > PORTAL_VMALLOC_SIZE)                                    \
291                 vfree(ptr);                                             \
292         else                                                            \
293                 kfree(ptr);                                             \
294         portal_kmem_dec((ptr), s);                                      \
295         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %ld at %p (tot %d).\n",    \
296                s, (ptr), atomic_read (&portal_kmemory));                \
297 } while (0)
298
299 #define PORTAL_SLAB_ALLOC(ptr, slab, size)                                \
300 do {                                                                      \
301         long s = (size);                                                  \
302         LASSERT (!in_interrupt());                                        \
303         (ptr) = kmem_cache_alloc((slab), SLAB_KERNEL);                    \
304         if ((ptr) == NULL) {                                              \
305                 CERROR("PORTALS: out of memory at %s:%d (tried to alloc"  \
306                        " '" #ptr "' from slab '" #slab "')\n", __FILE__,  \
307                        __LINE__);                                         \
308         } else {                                                          \
309                 portal_kmem_inc((ptr), s);                                \
310                 memset((ptr), 0, s);                                      \
311         }                                                                 \
312         CDEBUG(D_MALLOC, "kmalloced '" #ptr "': %ld at %p (tot %d).\n",   \
313                s, (ptr), atomic_read (&portal_kmemory));                  \
314 } while (0)
315
316 #define PORTAL_SLAB_FREE(ptr, slab, size)                               \
317 do {                                                                    \
318         long s = (size);                                                \
319         if ((ptr) == NULL) {                                            \
320                 CERROR("PORTALS: free NULL '" #ptr "' (%ld bytes) at "  \
321                        "%s:%d\n", s, __FILE__, __LINE__);               \
322                 break;                                                  \
323         }                                                               \
324         memset((ptr), 0x5a, s);                                         \
325         kmem_cache_free((slab), ptr);                                   \
326         portal_kmem_dec((ptr), s);                                      \
327         CDEBUG(D_MALLOC, "kfreed '" #ptr "': %ld at %p (tot %d).\n",    \
328                s, (ptr), atomic_read (&portal_kmemory));                \
329 } while (0)
330
331 /* ------------------------------------------------------------------- */
332
333 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
334
335 #define PORTAL_SYMBOL_REGISTER(x) inter_module_register(#x, THIS_MODULE, &x)
336 #define PORTAL_SYMBOL_UNREGISTER(x) inter_module_unregister(#x)
337
338 #define PORTAL_SYMBOL_GET(x) ((typeof(&x))inter_module_get(#x))
339 #define PORTAL_SYMBOL_PUT(x) inter_module_put(#x)
340
341 #define PORTAL_MODULE_USE       MOD_INC_USE_COUNT
342 #define PORTAL_MODULE_UNUSE     MOD_DEC_USE_COUNT
343 #else
344
345 #define PORTAL_SYMBOL_REGISTER(x)
346 #define PORTAL_SYMBOL_UNREGISTER(x)
347
348 #define PORTAL_SYMBOL_GET(x) symbol_get(x)
349 #define PORTAL_SYMBOL_PUT(x) symbol_put(x)
350
351 #define PORTAL_MODULE_USE       try_module_get(THIS_MODULE)
352 #define PORTAL_MODULE_UNUSE     module_put(THIS_MODULE)
353
354 #endif
355
356 /******************************************************************************/
357 /* Kernel Portals Router interface */
358
359 typedef void (*kpr_fwd_callback_t)(void *arg, int error); // completion callback
360
361 /* space for routing targets to stash "stuff" in a forwarded packet */
362 typedef union {
363         long long        _alignment;
364         void            *_space[16];            /* scale with CPU arch */
365 } kprfd_scratch_t;
366
367 /* Kernel Portals Routing Forwarded message Descriptor */
368 typedef struct {
369         struct list_head     kprfd_list;        /* stash in queues (routing target can use) */
370         ptl_nid_t            kprfd_target_nid;  /* final destination NID */
371         ptl_nid_t            kprfd_gateway_nid; /* gateway NID */
372         int                  kprfd_nob;         /* # message bytes (including header) */
373         int                  kprfd_niov;        /* # message frags (including header) */
374         struct iovec        *kprfd_iov;         /* message fragments */
375         void                *kprfd_router_arg;  // originating NAL's router arg
376         kpr_fwd_callback_t   kprfd_callback;    /* completion callback */
377         void                *kprfd_callback_arg; /* completion callback arg */
378         kprfd_scratch_t      kprfd_scratch;    // scratchpad for routing targets
379 } kpr_fwd_desc_t;
380
381 typedef void  (*kpr_fwd_t)(void *arg, kpr_fwd_desc_t *fwd);
382
383 /* NAL's routing interface (Kernel Portals Routing Nal Interface) */
384 typedef const struct {
385         int             kprni_nalid;    /* NAL's id */
386         void           *kprni_arg;      /* Arg to pass when calling into NAL */
387         kpr_fwd_t       kprni_fwd;      /* NAL's forwarding entrypoint */
388 } kpr_nal_interface_t;
389
390 /* Router's routing interface (Kernel Portals Routing Router Interface) */
391 typedef const struct {
392         /* register the calling NAL with the router and get back the handle for
393          * subsequent calls */
394         int     (*kprri_register) (kpr_nal_interface_t *nal_interface,
395                                    void **router_arg);
396
397         /* ask the router to find a gateway that forwards to 'nid' and is a peer
398          * of the calling NAL */
399         int     (*kprri_lookup) (void *router_arg, ptl_nid_t nid,
400                                  ptl_nid_t *gateway_nid);
401
402         /* hand a packet over to the router for forwarding */
403         kpr_fwd_t kprri_fwd_start;
404
405         /* hand a packet back to the router for completion */
406         void    (*kprri_fwd_done) (void *router_arg, kpr_fwd_desc_t *fwd,
407                                    int error);
408
409         /* the calling NAL is shutting down */
410         void    (*kprri_shutdown) (void *router_arg);
411
412         /* deregister the calling NAL with the router */
413         void    (*kprri_deregister) (void *router_arg);
414
415 } kpr_router_interface_t;
416
417 /* Convenient struct for NAL to stash router interface/args */
418 typedef struct {
419         kpr_router_interface_t  *kpr_interface;
420         void                    *kpr_arg;
421 } kpr_router_t;
422
423 /* Router's control interface (Kernel Portals Routing Control Interface) */
424 typedef const struct {
425         int     (*kprci_add_route)(int gateway_nal, ptl_nid_t gateway_nid,
426                                    ptl_nid_t lo_nid, ptl_nid_t hi_nid);
427         int     (*kprci_del_route)(ptl_nid_t nid);
428         int     (*kprci_get_route)(int index, int *gateway_nal,
429                                    ptl_nid_t *gateway, ptl_nid_t *lo_nid,
430                                    ptl_nid_t *hi_nid);
431 } kpr_control_interface_t;
432
433 extern kpr_control_interface_t  kpr_control_interface;
434 extern kpr_router_interface_t   kpr_router_interface;
435
436 static inline int
437 kpr_register (kpr_router_t *router, kpr_nal_interface_t *nalif)
438 {
439         int    rc;
440
441         router->kpr_interface = PORTAL_SYMBOL_GET (kpr_router_interface);
442         if (router->kpr_interface == NULL)
443                 return (-ENOENT);
444
445         rc = (router->kpr_interface)->kprri_register (nalif, &router->kpr_arg);
446         if (rc != 0)
447                 router->kpr_interface = NULL;
448
449         PORTAL_SYMBOL_PUT (kpr_router_interface);
450         return (rc);
451 }
452
453 static inline int
454 kpr_routing (kpr_router_t *router)
455 {
456         return (router->kpr_interface != NULL);
457 }
458
459 static inline int
460 kpr_lookup (kpr_router_t *router, ptl_nid_t nid, ptl_nid_t *gateway_nid)
461 {
462         if (!kpr_routing (router))
463                 return (-EHOSTUNREACH);
464
465         return (router->kpr_interface->kprri_lookup(router->kpr_arg, nid,
466                                                     gateway_nid));
467 }
468
469 static inline void
470 kpr_fwd_init (kpr_fwd_desc_t *fwd, ptl_nid_t nid,
471               int nob, int niov, struct iovec *iov,
472               kpr_fwd_callback_t callback, void *callback_arg)
473 {
474         fwd->kprfd_target_nid   = nid;
475         fwd->kprfd_gateway_nid  = nid;
476         fwd->kprfd_nob          = nob;
477         fwd->kprfd_niov         = niov;
478         fwd->kprfd_iov          = iov;
479         fwd->kprfd_callback     = callback;
480         fwd->kprfd_callback_arg = callback_arg;
481 }
482
483 static inline void
484 kpr_fwd_start (kpr_router_t *router, kpr_fwd_desc_t *fwd)
485 {
486         if (!kpr_routing (router))
487                 fwd->kprfd_callback (fwd->kprfd_callback_arg, -EHOSTUNREACH);
488         else
489                 router->kpr_interface->kprri_fwd_start (router->kpr_arg, fwd);
490 }
491
492 static inline void
493 kpr_fwd_done (kpr_router_t *router, kpr_fwd_desc_t *fwd, int error)
494 {
495         LASSERT (kpr_routing (router));
496         router->kpr_interface->kprri_fwd_done (router->kpr_arg, fwd, error);
497 }
498
499 static inline void
500 kpr_shutdown (kpr_router_t *router)
501 {
502         if (kpr_routing (router))
503                 router->kpr_interface->kprri_shutdown (router->kpr_arg);
504 }
505
506 static inline void
507 kpr_deregister (kpr_router_t *router)
508 {
509         if (!kpr_routing (router))
510                 return;
511         router->kpr_interface->kprri_deregister (router->kpr_arg);
512         router->kpr_interface = NULL;
513 }
514
515 /******************************************************************************/
516
517 #ifdef PORTALS_PROFILING
518 #define prof_enum(FOO) PROF__##FOO
519 enum {
520         prof_enum(our_recvmsg),
521         prof_enum(our_sendmsg),
522         prof_enum(socknal_recv),
523         prof_enum(lib_parse),
524         prof_enum(conn_list_walk),
525         prof_enum(memcpy),
526         prof_enum(lib_finalize),
527         prof_enum(pingcli_time),
528         prof_enum(gmnal_send),
529         prof_enum(gmnal_recv),
530         MAX_PROFS
531 };
532
533 struct prof_ent {
534         char *str;
535         /* hrmph.  wrap-tastic. */
536         u32       starts;
537         u32       finishes;
538         cycles_t  total_cycles;
539         cycles_t  start;
540         cycles_t  end;
541 };
542
543 extern struct prof_ent prof_ents[MAX_PROFS];
544
545 #define PROF_START(FOO)                                         \
546         do {                                                    \
547                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
548                 pe->starts++;                                   \
549                 pe->start = get_cycles();                       \
550         } while (0)
551
552 #define PROF_FINISH(FOO)                                        \
553         do {                                                    \
554                 struct prof_ent *pe = &prof_ents[PROF__##FOO];  \
555                 pe->finishes++;                                 \
556                 pe->end = get_cycles();                         \
557                 pe->total_cycles += (pe->end - pe->start);      \
558         } while (0)
559 #else /* !PORTALS_PROFILING */
560 #define PROF_START(FOO) do {} while(0)
561 #define PROF_FINISH(FOO) do {} while(0)
562 #endif /* PORTALS_PROFILING */
563
564 /* debug.c */
565 void portals_run_lbug_upcall(char * file, char *fn, int line);
566 void portals_debug_dumplog(void);
567 int portals_debug_init(unsigned long bufsize);
568 int portals_debug_cleanup(void);
569 int portals_debug_clear_buffer(void);
570 int portals_debug_mark_buffer(char *text);
571 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
572                 char *file, unsigned int size);
573 __s32 portals_debug_copy_to_user(char *buf, unsigned long len);
574 #if (__GNUC__)
575 /* Use the special GNU C __attribute__ hack to have the compiler check the
576  * printf style argument string against the actual argument count and
577  * types.
578  */
579 #ifdef printf
580 # warning printf has been defined as a macro...
581 # undef printf
582 #endif
583 void portals_debug_msg (int subsys, int mask, char *file, char *fn, int line,
584                         unsigned long stack, const char *format, ...)
585         __attribute__ ((format (printf, 7, 8)));
586 #else
587 void portals_debug_msg (int subsys, int mask, char *file, char *fn,
588                         int line, unsigned long stack,
589                         const char *format, ...);
590 #endif /* __GNUC__ */
591 void portals_debug_set_level(unsigned int debug_level);
592
593 # define fprintf(a, format, b...) CDEBUG(D_OTHER, format , ## b)
594 # define printf(format, b...) CDEBUG(D_OTHER, format , ## b)
595 # define time(a) CURRENT_TIME
596
597 extern void kportal_daemonize (char *name);
598 extern void kportal_blockallsigs (void);
599
600 #else  /* !__KERNEL__ */
601 # include <stdio.h>
602 # include <stdlib.h>
603 #ifndef __CYGWIN__
604 # include <stdint.h>
605 #endif
606 # include <unistd.h>
607 # include <time.h>
608 # include <asm/types.h>
609 # ifndef DEBUG_SUBSYSTEM
610 #  define DEBUG_SUBSYSTEM S_UNDEFINED
611 # endif
612 # ifdef PORTAL_DEBUG
613 #  undef NDEBUG
614 #  include <assert.h>
615 #  define LASSERT(e)     assert(e)
616 # else
617 #  define LASSERT(e)
618 # endif
619 # define printk(format, args...) printf (format, ## args)
620 # define PORTAL_ALLOC(ptr, size) do { (ptr) = malloc(size); } while (0);
621 # define PORTAL_FREE(a, b) do { free(a); } while (0);
622 # define portals_debug_msg(subsys, mask, file, fn, line, stack, format, a...) \
623     printf ("%02x:%06x (@%lu %s:%s,l. %d %d %lu): " format,                    \
624             (subsys) >> 24, (mask), (long)time(0), file, fn, line,            \
625             getpid() , stack, ## a);
626 #endif
627
628 #ifndef CURRENT_TIME
629 # define CURRENT_TIME time(0)
630 #endif
631
632 #include <linux/portals_lib.h>
633
634 /*
635  * USER LEVEL STUFF BELOW
636  */
637
638 #define PORTAL_IOCTL_VERSION 0x00010007
639 #define PING_SYNC       0
640 #define PING_ASYNC      1
641
642 struct portal_ioctl_data {
643         __u32 ioc_len;
644         __u32 ioc_version;
645         __u64 ioc_nid;
646         __u64 ioc_nid2;
647         __u64 ioc_nid3;
648         __u32 ioc_count;
649         __u32 ioc_nal;
650         __u32 ioc_nal_cmd;
651         __u32 ioc_fd;
652         __u32 ioc_id;
653
654         __u32 ioc_flags;
655         __u32 ioc_size;
656
657         __u32 ioc_wait;
658         __u32 ioc_timeout;
659         __u32 ioc_misc;
660
661         __u32 ioc_inllen1;
662         char *ioc_inlbuf1;
663         __u32 ioc_inllen2;
664         char *ioc_inlbuf2;
665
666         __u32 ioc_plen1; /* buffers in userspace */
667         char *ioc_pbuf1;
668         __u32 ioc_plen2; /* buffers in userspace */
669         char *ioc_pbuf2;
670
671         char ioc_bulk[0];
672 };
673
674 struct portal_ioctl_hdr {
675         __u32 ioc_len;
676         __u32 ioc_version;
677 };
678
679 struct portals_debug_ioctl_data
680 {
681         struct portal_ioctl_hdr hdr;
682         unsigned int subs;
683         unsigned int debug;
684 };
685
686 #define PORTAL_IOC_INIT(data)                           \
687 do {                                                    \
688         memset(&data, 0, sizeof(data));                 \
689         data.ioc_version = PORTAL_IOCTL_VERSION;        \
690         data.ioc_len = sizeof(data);                    \
691 } while (0)
692
693 /* FIXME check conflict with lustre_lib.h */
694 #define PTL_IOC_DEBUG_MASK             _IOWR('f', 250, long)
695
696 static inline int portal_ioctl_packlen(struct portal_ioctl_data *data)
697 {
698         int len = sizeof(*data);
699         len += size_round(data->ioc_inllen1);
700         len += size_round(data->ioc_inllen2);
701         return len;
702 }
703
704 static inline int portal_ioctl_is_invalid(struct portal_ioctl_data *data)
705 {
706         if (data->ioc_len > (1<<30)) {
707                 CERROR ("PORTALS ioctl: ioc_len larger than 1<<30\n");
708                 return 1;
709         }
710         if (data->ioc_inllen1 > (1<<30)) {
711                 CERROR ("PORTALS ioctl: ioc_inllen1 larger than 1<<30\n");
712                 return 1;
713         }
714         if (data->ioc_inllen2 > (1<<30)) {
715                 CERROR ("PORTALS ioctl: ioc_inllen2 larger than 1<<30\n");
716                 return 1;
717         }
718         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
719                 CERROR ("PORTALS ioctl: inlbuf1 pointer but 0 length\n");
720                 return 1;
721         }
722         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
723                 CERROR ("PORTALS ioctl: inlbuf2 pointer but 0 length\n");
724                 return 1;
725         }
726         if (data->ioc_pbuf1 && !data->ioc_plen1) {
727                 CERROR ("PORTALS ioctl: pbuf1 pointer but 0 length\n");
728                 return 1;
729         }
730         if (data->ioc_pbuf2 && !data->ioc_plen2) {
731                 CERROR ("PORTALS ioctl: pbuf2 pointer but 0 length\n");
732                 return 1;
733         }
734         if (data->ioc_plen1 && !data->ioc_pbuf1) {
735                 CERROR ("PORTALS ioctl: plen1 nonzero but no pbuf1 pointer\n");
736                 return 1;
737         }
738         if (data->ioc_plen2 && !data->ioc_pbuf2) {
739                 CERROR ("PORTALS ioctl: plen2 nonzero but no pbuf2 pointer\n");
740                 return 1;
741         }
742         if (portal_ioctl_packlen(data) != data->ioc_len ) {
743                 CERROR ("PORTALS ioctl: packlen != ioc_len\n");
744                 return 1;
745         }
746         if (data->ioc_inllen1 &&
747             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') {
748                 CERROR ("PORTALS ioctl: inlbuf1 not 0 terminated\n");
749                 return 1;
750         }
751         if (data->ioc_inllen2 &&
752             data->ioc_bulk[size_round(data->ioc_inllen1) +
753                            data->ioc_inllen2 - 1] != '\0') {
754                 CERROR ("PORTALS ioctl: inlbuf2 not 0 terminated\n");
755                 return 1;
756         }
757         return 0;
758 }
759
760 #ifndef __KERNEL__
761 static inline int portal_ioctl_pack(struct portal_ioctl_data *data, char **pbuf,
762                                     int max)
763 {
764         char *ptr;
765         struct portal_ioctl_data *overlay;
766         data->ioc_len = portal_ioctl_packlen(data);
767         data->ioc_version = PORTAL_IOCTL_VERSION;
768
769         if (*pbuf && portal_ioctl_packlen(data) > max)
770                 return 1;
771         if (*pbuf == NULL) {
772                 *pbuf = malloc(data->ioc_len);
773         }
774         if (!*pbuf)
775                 return 1;
776         overlay = (struct portal_ioctl_data *)*pbuf;
777         memcpy(*pbuf, data, sizeof(*data));
778
779         ptr = overlay->ioc_bulk;
780         if (data->ioc_inlbuf1)
781                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
782         if (data->ioc_inlbuf2)
783                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
784         if (portal_ioctl_is_invalid(overlay))
785                 return 1;
786
787         return 0;
788 }
789 #else
790 #include <asm/uaccess.h>
791
792 /* buffer MUST be at least the size of portal_ioctl_hdr */
793 static inline int portal_ioctl_getdata(char *buf, char *end, void *arg)
794 {
795         struct portal_ioctl_hdr *hdr;
796         struct portal_ioctl_data *data;
797         int err;
798         ENTRY;
799
800         hdr = (struct portal_ioctl_hdr *)buf;
801         data = (struct portal_ioctl_data *)buf;
802
803         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
804         if ( err ) {
805                 EXIT;
806                 return err;
807         }
808
809         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
810                 CERROR ("PORTALS: version mismatch kernel vs application\n");
811                 return -EINVAL;
812         }
813
814         if (hdr->ioc_len + buf >= end) {
815                 CERROR ("PORTALS: user buffer exceeds kernel buffer\n");
816                 return -EINVAL;
817         }
818
819
820         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
821                 CERROR ("PORTALS: user buffer too small for ioctl\n");
822                 return -EINVAL;
823         }
824
825         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
826         if ( err ) {
827                 EXIT;
828                 return err;
829         }
830
831         if (portal_ioctl_is_invalid(data)) {
832                 CERROR ("PORTALS: ioctl not correctly formatted\n");
833                 return -EINVAL;
834         }
835
836         if (data->ioc_inllen1) {
837                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
838         }
839
840         if (data->ioc_inllen2) {
841                 data->ioc_inlbuf2 = &data->ioc_bulk[0] +
842                         size_round(data->ioc_inllen1);
843         }
844
845         EXIT;
846         return 0;
847 }
848 #endif
849
850 /* ioctls for manipulating snapshots 30- */
851 #define IOC_PORTAL_TYPE                   'e'
852 #define IOC_PORTAL_MIN_NR                 30
853
854 #define IOC_PORTAL_PING                    _IOWR('e', 30, long)
855 #define IOC_PORTAL_GET_DEBUG               _IOWR('e', 31, long)
856 #define IOC_PORTAL_CLEAR_DEBUG             _IOWR('e', 32, long)
857 #define IOC_PORTAL_MARK_DEBUG              _IOWR('e', 33, long)
858 #define IOC_PORTAL_PANIC                   _IOWR('e', 34, long)
859 #define IOC_PORTAL_ADD_ROUTE               _IOWR('e', 35, long)
860 #define IOC_PORTAL_DEL_ROUTE               _IOWR('e', 36, long)
861 #define IOC_PORTAL_GET_ROUTE               _IOWR('e', 37, long)
862 #define IOC_PORTAL_NAL_CMD                 _IOWR('e', 38, long)
863 #define IOC_PORTAL_GET_NID                 _IOWR('e', 39, long)
864 #define IOC_PORTAL_FAIL_NID                _IOWR('e', 40, long)
865 #define IOC_PORTAL_SET_DAEMON              _IOWR('e', 41, long)
866
867 #define IOC_PORTAL_MAX_NR               41
868
869 enum {
870         QSWNAL  =  1,
871         SOCKNAL,
872         GMNAL,
873         TOENAL,
874         TCPNAL,
875         SCIMACNAL,
876         NAL_ENUM_END_MARKER
877 };
878
879 #ifdef __KERNEL__
880 extern ptl_handle_ni_t  kqswnal_ni;
881 extern ptl_handle_ni_t  ksocknal_ni;
882 extern ptl_handle_ni_t  ktoenal_ni;
883 extern ptl_handle_ni_t  kgmnal_ni;
884 extern ptl_handle_ni_t  kscimacnal_ni;
885 #endif
886
887 #define NAL_MAX_NR (NAL_ENUM_END_MARKER - 1)
888
889 #define NAL_CMD_REGISTER_PEER_FD     100
890 #define NAL_CMD_CLOSE_CONNECTION     101
891 #define NAL_CMD_REGISTER_MYNID       102
892 #define NAL_CMD_PUSH_CONNECTION      103
893
894 enum {
895         DEBUG_DAEMON_START       =  1,
896         DEBUG_DAEMON_STOP        =  2,
897         DEBUG_DAEMON_PAUSE       =  3,
898         DEBUG_DAEMON_CONTINUE    =  4,
899 };
900
901 /* XXX remove to lustre ASAP */
902 struct lustre_peer {
903         ptl_nid_t       peer_nid;
904         ptl_handle_ni_t peer_ni;
905 };
906
907 /* module.c */
908 typedef int (*nal_cmd_handler_t)(struct portal_ioctl_data *, void * private);
909 int kportal_nal_register(int nal, nal_cmd_handler_t handler, void * private);
910 int kportal_nal_unregister(int nal);
911
912 ptl_handle_ni_t *kportal_get_ni (int nal);
913 void kportal_put_ni (int nal);
914
915 #ifdef __CYGWIN__
916 #ifndef BITS_PER_LONG
917 #if (~0UL) == 0xffffffffUL
918 #define BITS_PER_LONG 32
919 #else
920 #define BITS_PER_LONG 64
921 #endif
922 #endif
923 #endif
924
925 #if (BITS_PER_LONG == 32 || __WORDSIZE == 32)
926 # define LPU64 "%Lu"
927 # define LPD64 "%Ld"
928 # define LPX64 "%#Lx"
929 # define LPSZ  "%u"
930 # define LPSSZ "%d"
931 #endif
932 #if (BITS_PER_LONG == 64 || __WORDSIZE == 64)
933 # define LPU64 "%lu"
934 # define LPD64 "%ld"
935 # define LPX64 "%#lx"
936 # define LPSZ  "%lu"
937 # define LPSSZ "%ld"
938 #endif
939 #ifndef LPU64
940 # error "No word size defined"
941 #endif
942
943 #endif