Whamcloud - gitweb
7b45d89a99fb5b69ad964d12281986c139d0070c
[fs/lustre-release.git] / lnet / include / libcfs / libcfs.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #ifndef __LIBCFS_LIBCFS_H__
5 #define __LIBCFS_LIBCFS_H__
6
7 #if !__GNUC__
8 #define __attribute__(x)
9 #endif
10
11 #if defined(__linux__)
12 #include <libcfs/linux/libcfs.h>
13 #elif defined(__APPLE__)
14 #include <libcfs/darwin/libcfs.h>
15 #else
16 #error Unsupported operating system.
17 #endif
18
19 #include "curproc.h"
20
21 #define PORTAL_DEBUG
22
23 #ifndef offsetof
24 # define offsetof(typ,memb)     ((unsigned long)((char *)&(((typ *)0)->memb)))
25 #endif
26
27 #define LOWEST_BIT_SET(x)       ((x) & ~((x) - 1))
28
29 /*
30  *  Debugging
31  */
32 extern unsigned int portal_subsystem_debug;
33 extern unsigned int portal_stack;
34 extern unsigned int portal_debug;
35 extern unsigned int portal_printk;
36
37 /*
38  * struct ptldebug_header is defined in libcfs/<os>/libcfs.h
39  */
40
41 #define PH_FLAG_FIRST_RECORD 1
42
43 /* Debugging subsystems (32 bits, non-overlapping) */
44 #define S_UNDEFINED   0x00000001
45 #define S_MDC         0x00000002
46 #define S_MDS         0x00000004
47 #define S_OSC         0x00000008
48 #define S_OST         0x00000010
49 #define S_CLASS       0x00000020
50 #define S_LOG         0x00000040
51 #define S_LLITE       0x00000080
52 #define S_RPC         0x00000100
53 #define S_MGMT        0x00000200
54 #define S_PORTALS     0x00000400
55 #define S_NAL         0x00000800 /* ALL NALs */
56 #define S_PINGER      0x00001000
57 #define S_FILTER      0x00002000
58 #define S_PTLBD       0x00004000
59 #define S_ECHO        0x00008000
60 #define S_LDLM        0x00010000
61 #define S_LOV         0x00020000
62 #define S_PTLROUTER   0x00040000
63 #define S_COBD        0x00080000
64 #define S_SM          0x00100000
65 #define S_ASOBD       0x00200000
66 #define S_CONFOBD     0x00400000
67 #define S_LMV         0x00800000
68 #define S_CMOBD       0x01000000
69 #define S_SEC         0x02000000
70 /* If you change these values, please keep these files up to date...
71  *    portals/utils/debug.c
72  *    utils/lconf
73  */
74
75 /* Debugging masks (32 bits, non-overlapping) */
76 #define D_TRACE       0x00000001 /* ENTRY/EXIT markers */
77 #define D_INODE       0x00000002
78 #define D_SUPER       0x00000004
79 #define D_EXT2        0x00000008 /* anything from ext2_debug */
80 #define D_MALLOC      0x00000010 /* print malloc, free information */
81 #define D_CACHE       0x00000020 /* cache-related items */
82 #define D_INFO        0x00000040 /* general information */
83 #define D_IOCTL       0x00000080 /* ioctl related information */
84 #define D_BLOCKS      0x00000100 /* ext2 block allocation */
85 #define D_NET         0x00000200 /* network communications */
86 #define D_WARNING     0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
87 #define D_BUFFS       0x00000800
88 #define D_OTHER       0x00001000
89 #define D_DENTRY      0x00002000
90 #define D_PORTALS     0x00004000 /* ENTRY/EXIT markers */
91 #define D_PAGE        0x00008000 /* bulk page handling */
92 #define D_DLMTRACE    0x00010000
93 #define D_ERROR       0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
94 #define D_EMERG       0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
95 #define D_HA          0x00080000 /* recovery and failover */
96 #define D_RPCTRACE    0x00100000 /* for distributed debugging */
97 #define D_VFSTRACE    0x00200000
98 #define D_READA       0x00400000 /* read-ahead */
99 #define D_MMAP        0x00800000
100 #define D_CONFIG      0x01000000
101 #define D_CONSOLE     0x02000000
102 #define D_QUOTA       0x04000000
103 #define D_SEC         0x08000000
104 /* If you change these values, please keep these files up to date...
105  *    portals/utils/debug.c
106  *    utils/lconf
107  */
108
109 #ifndef DEBUG_SUBSYSTEM
110 # define DEBUG_SUBSYSTEM S_UNDEFINED
111 #endif
112
113 #ifdef __KERNEL__
114 #if 1
115 #define CDEBUG(mask, format, a...)                                            \
116 do {                                                                          \
117         CHECK_STACK(CDEBUG_STACK);                                            \
118         if (((mask) & (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)) ||         \
119             (portal_debug & (mask) &&                                         \
120              portal_subsystem_debug & DEBUG_SUBSYSTEM))                       \
121                 portals_debug_msg(DEBUG_SUBSYSTEM, mask,                      \
122                                   __FILE__, __FUNCTION__, __LINE__,           \
123                                   CDEBUG_STACK, format, ## a);                \
124 } while (0)
125
126 #define CDEBUG_MAX_LIMIT 600
127 #define CDEBUG_LIMIT(cdebug_mask, cdebug_format, a...)                        \
128 do {                                                                          \
129         static cfs_time_t cdebug_next = 0;                                    \
130         static int cdebug_count = 0;                                          \
131         static cfs_duration_t cdebug_delay = CFS_MIN_DELAY;                   \
132                                                                               \
133         CHECK_STACK(CDEBUG_STACK);                                            \
134         if (cfs_time_after(cfs_time_current(), cdebug_next)) {                \
135                 portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask, __FILE__,     \
136                                   __FUNCTION__, __LINE__, CDEBUG_STACK,       \
137                                   cdebug_format, ## a);                       \
138                 if (cdebug_count) {                                           \
139                         portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask,       \
140                                           __FILE__, __FUNCTION__, __LINE__,   \
141                                           0, "skipped %d similar messages\n", \
142                                           cdebug_count);                      \
143                         cdebug_count = 0;                                     \
144                 }                                                             \
145                 if (cfs_time_after(cfs_time_current(),                        \
146                                    cdebug_next +                              \
147                                    cfs_time_seconds(CDEBUG_MAX_LIMIT+10)))    \
148                         cdebug_delay = cdebug_delay > (8 * CFS_MIN_DELAY)?    \
149                                        cdebug_delay/8 : CFS_MIN_DELAY;        \
150                 else                                                          \
151                         cdebug_delay = cdebug_delay*2 >= cfs_time_seconds(CDEBUG_MAX_LIMIT)?\
152                                        cfs_time_seconds(CDEBUG_MAX_LIMIT) :   \
153                                        cdebug_delay*2;                        \
154                 cdebug_next = cfs_time_current() + cdebug_delay;              \
155         } else {                                                              \
156                 portals_debug_msg(DEBUG_SUBSYSTEM,                            \
157                                   portal_debug & ~(D_EMERG|D_ERROR|D_WARNING),\
158                                   __FILE__, __FUNCTION__, __LINE__,           \
159                                   CDEBUG_STACK, cdebug_format, ## a);         \
160                 cdebug_count++;                                               \
161         }                                                                     \
162 } while (0)
163
164 #define CWARN(format, a...) CDEBUG_LIMIT(D_WARNING, format, ## a)
165 #define CERROR(format, a...) CDEBUG_LIMIT(D_ERROR, format, ## a)
166 #define CEMERG(format, a...) CDEBUG(D_EMERG, format, ## a)
167
168 #define LCONSOLE(mask, format, a...) CDEBUG(D_CONSOLE | (mask), format, ## a)
169 #define LCONSOLE_INFO(format, a...)  CDEBUG_LIMIT(D_CONSOLE, format, ## a)
170 #define LCONSOLE_WARN(format, a...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## a)
171 #define LCONSOLE_ERROR(format, a...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, format, ## a)
172 #define LCONSOLE_EMERG(format, a...) CDEBUG(D_CONSOLE | D_EMERG, format, ## a)
173
174 #define GOTO(label, rc)                                                 \
175 do {                                                                    \
176         long GOTO__ret = (long)(rc);                                    \
177         CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
178                #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
179                (signed long)GOTO__ret);                                 \
180         goto label;                                                     \
181 } while (0)
182
183 #define CDEBUG_ENTRY_EXIT (0)
184
185 #ifdef CDEBUG_ENTRY_EXIT
186
187 /*
188  * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
189  * there will be a warning in osx.
190  */
191 #define RETURN(rc)                                                      \
192 do {                                                                    \
193         typeof(rc) RETURN__ret = (rc);                                  \
194         CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n",       \
195                (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
196         EXIT_NESTING;                                                   \
197         return RETURN__ret;                                             \
198 } while (0)
199
200 #define ENTRY                                                           \
201 ENTRY_NESTING;                                                          \
202 do {                                                                    \
203         CDEBUG(D_TRACE, "Process entered\n");                           \
204 } while (0)
205
206 #define EXIT                                                            \
207 do {                                                                    \
208         CDEBUG(D_TRACE, "Process leaving\n");                           \
209         EXIT_NESTING;                                                   \
210 } while(0)
211 #else /* !1 */
212
213 #define RETURN(rc) return (rc)
214 #define ENTRY
215 #define EXIT
216
217 #endif
218
219 #else
220 #define CDEBUG(mask, format, a...)      do { } while (0)
221 #define CWARN(format, a...)             printk(KERN_WARNING format, ## a)
222 #define CERROR(format, a...)            printk(KERN_ERR format, ## a)
223 #define CEMERG(format, a...)            printk(KERN_EMERG format, ## a)
224 #define LCONSOLE(mask, format, a...)    do { } while (0)
225 #define LCONSOLE_INFO(format, a...)     printk(KERN_INFO format, ## a)
226 #define LCONSOLE_WARN(format, a...)     printk(KERN_WARNING format, ## a)
227 #define LCONSOLE_ERROR(format, a...)    printk(KERN_ERROR format, ## a)
228 #define LCONSOLE_EMERG(format, a...)    printk(KERN_EMERG format, ## a)
229 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
230 #define RETURN(rc)                      return (rc)
231 #define ENTRY                           do { } while (0)
232 #define EXIT                            do { } while (0)
233 #endif /* !1 */
234 #else /* !__KERNEL__ */
235 #define CDEBUG(mask, format, a...)      do { } while (0)
236 #define LCONSOLE(mask, format, a...)    do { } while (0)
237 #ifdef stderr
238 #define CWARN(format, a...)             fprintf(stderr, "<4>" format, ## a)
239 #define CERROR(format, a...)            fprintf(stderr, "<3>" format, ## a)
240 #define CEMERG(format, a...)            fprintf(stderr, "<0>" format, ## a)
241 #define LCONSOLE_INFO(format, a...)     fprintf(stderr, format, ## a)
242 #define LCONSOLE_WARN(format, a...)     fprintf(stderr, format, ## a)
243 #define LCONSOLE_ERROR(format, a...)    fprintf(stderr, format, ## a)
244 #define LCONSOLE_EMERG(format, a...)    fprintf(stderr, format, ## a)
245 #else
246 #define CWARN(format, a...)             do { } while (0)
247 #define CERROR(format, a...)            do { } while (0)
248 #define CEMERG(format, a...)            do { } while (0)
249 #define LCONSOLE_INFO(format, a...)     do { } while (0)
250 #define LCONSOLE_WARN(format, a...)     do { } while (0)
251 #define LCONSOLE_ERROR(format, a...)    do { } while (0)
252 #define LCONSOLE_EMERG(format, a...)    do { } while (0)
253 #endif /* !stderr */
254 #define GOTO(label, rc)                 do { (void)(rc); goto label; } while (0)
255 #define RETURN(rc)                      return (rc)
256 #define ENTRY                           do { } while (0)
257 #define EXIT                            do { } while (0)
258 #endif /* !__KERNEL__ */
259
260 #define LUSTRE_SRV_PTL_PID      LUSTRE_PTL_PID
261
262 /*
263  * eeb cfg
264  * ecf6
265  * ecfG
266  */
267 #define PORTALS_CFG_VERSION 0xecf60001
268
269 struct portals_cfg {
270         __u32 pcfg_version;
271         __u32 pcfg_command;
272
273         __u32 pcfg_nal;
274         __u32 pcfg_flags;
275
276         __u32 pcfg_gw_nal;
277         __u32 pcfg_padding1;
278
279         __u64 pcfg_nid;
280         __u64 pcfg_nid2;
281         __u64 pcfg_nid3;
282         __u32 pcfg_id;
283         __u32 pcfg_misc;
284         __u32 pcfg_fd;
285         __u32 pcfg_count;
286         __u32 pcfg_size;
287         __u32 pcfg_wait;
288
289         __u32 pcfg_plen1; /* buffers in userspace */
290         __u32 pcfg_plen2; /* buffers in userspace */
291         __u32 pcfg_alloc_size;  /* size of this allocated portals_cfg */
292         char  pcfg_pbuf[0];
293 };
294
295 #define PCFG_INIT(pcfg, cmd)                            \
296 do {                                                    \
297         memset(&(pcfg), 0, sizeof((pcfg)));             \
298         (pcfg).pcfg_version = PORTALS_CFG_VERSION;      \
299         (pcfg).pcfg_command = (cmd);                    \
300                                                         \
301 } while (0)
302
303 #define PCFG_INIT_PBUF(pcfg, cmd, plen1, plen2)                         \
304         do {                                                            \
305                 int bufsize = size_round(sizeof(*(pcfg)));              \
306                 bufsize += size_round(plen1) + size_round(plen2);       \
307                 PORTAL_ALLOC((pcfg), bufsize);                          \
308                 if ((pcfg)) {                                           \
309                         memset((pcfg), 0, bufsize);                     \
310                         (pcfg)->pcfg_version = PORTALS_CFG_VERSION;     \
311                         (pcfg)->pcfg_command = (cmd);                   \
312                         (pcfg)->pcfg_plen1 = (plen1);                   \
313                         (pcfg)->pcfg_plen2 = (plen2);                   \
314                         (pcfg)->pcfg_alloc_size = bufsize;              \
315                 }                                                       \
316         } while (0)
317
318 #define PCFG_FREE_PBUF(pcfg) PORTAL_FREE((pcfg), (pcfg)->pcfg_alloc_size)
319
320 #define PCFG_PBUF(pcfg, idx)                                            \
321         (0 == (idx)                                                     \
322          ? ((char *)(pcfg) + size_round(sizeof(*(pcfg))))               \
323          : (1 == (idx)                                                  \
324             ? ((char *)(pcfg) + size_round(sizeof(*(pcfg))) + size_round(pcfg->pcfg_plen1)) \
325             : (NULL)))
326
327 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
328 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
329 int libcfs_nal_cmd(struct portals_cfg *pcfg);
330 void libcfs_nal_cmd_unregister(int nal);
331
332 struct portal_ioctl_data {
333         __u32 ioc_len;
334         __u32 ioc_version;
335         __u64 ioc_nid;
336         __u64 ioc_nid2;
337         __u64 ioc_nid3;
338         __u32 ioc_count;
339         __u32 ioc_nal;
340         __u32 ioc_nal_cmd;
341         __u32 ioc_fd;
342         __u32 ioc_id;
343
344         __u32 ioc_flags;
345         __u32 ioc_size;
346
347         __u32 ioc_wait;
348         __u32 ioc_timeout;
349         __u32 ioc_misc;
350
351         __u32 ioc_inllen1;
352         char *ioc_inlbuf1;
353         __u32 ioc_inllen2;
354         char *ioc_inlbuf2;
355
356         __u32 ioc_plen1; /* buffers in userspace */
357         char *ioc_pbuf1;
358         __u32 ioc_plen2; /* buffers in userspace */
359         char *ioc_pbuf2;
360
361         char ioc_bulk[0];
362 };
363
364
365 #ifdef __KERNEL__
366
367 #include <libcfs/list.h>
368
369 struct libcfs_ioctl_handler {
370         struct list_head item;
371         int (*handle_ioctl)(struct portal_ioctl_data *data,
372                             unsigned int cmd, unsigned long args);
373 };
374
375 #define DECLARE_IOCTL_HANDLER(ident, func)              \
376         struct libcfs_ioctl_handler ident = {           \
377                 .item = CFS_LIST_HEAD_INIT(ident.item),     \
378                 .handle_ioctl = func                    \
379         }
380
381 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
382 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
383
384 /* libcfs watchdogs */
385 struct lc_watchdog;
386
387 /* Just use the default handler (dumplog)  */
388 #define LC_WATCHDOG_DEFAULT_CB NULL
389
390 /* Add a watchdog which fires after "time" milliseconds of delay.  You have to
391  * touch it once to enable it. */
392 struct lc_watchdog *lc_watchdog_add(int time,
393                                     void (*cb)(struct lc_watchdog *,
394                                                struct task_struct *,
395                                                void *),
396                                     void *data);
397
398 /* Enables a watchdog and resets its timer. */
399 void lc_watchdog_touch(struct lc_watchdog *lcw);
400
401 /* Disable a watchdog; touch it to restart it. */
402 void lc_watchdog_disable(struct lc_watchdog *lcw);
403
404 /* Clean up the watchdog */
405 void lc_watchdog_delete(struct lc_watchdog *lcw);
406
407 /* Dump a debug log */
408 void lc_watchdog_dumplog(struct lc_watchdog *lcw,
409                          struct task_struct *tsk,
410                          void *data);
411
412 /* __KERNEL__ */
413 #endif
414
415 /*
416  * libcfs pseudo device operations
417  *
418  * struct cfs_psdev_t and
419  * cfs_psdev_register() and
420  * cfs_psdev_deregister() are declared in
421  * libcfs/<os>/cfs_prim.h
422  *
423  * It's just draft now.
424  */
425
426 struct cfs_psdev_file {
427         unsigned long   off;
428         void            *private_data;
429         unsigned long   reserved1;
430         unsigned long   reserved2;
431 };
432
433 struct cfs_psdev_ops {
434         int (*p_open)(unsigned long, void *);
435         int (*p_close)(unsigned long, void *);
436         int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
437         int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
438         int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
439 };
440
441 /*
442  * generic time manipulation functions.
443  */
444
445 static inline int cfs_time_after(cfs_time_t t1, cfs_time_t t2)
446 {
447         return cfs_time_before(t2, t1);
448 }
449
450 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
451 {
452         return cfs_time_beforeq(t2, t1);
453 }
454
455 /*
456  * return seconds since UNIX epoch
457  */
458 static inline time_t cfs_unix_seconds(void)
459 {
460         cfs_fs_time_t t;
461
462         cfs_fs_time_current(&t);
463         return cfs_fs_time_sec(&t);
464 }
465
466 #define CFS_RATELIMIT(seconds)                                  \
467 ({                                                              \
468         /*                                                      \
469          * XXX nikita: non-portable initializer                 \
470          */                                                     \
471         static time_t __next_message = 0;                       \
472         int result;                                             \
473                                                                 \
474         if (cfs_time_after(cfs_time_current(), __next_message)) \
475                 result = 1;                                     \
476         else {                                                  \
477                 __next_message = cfs_time_shift(seconds);       \
478                 result = 0;                                     \
479         }                                                       \
480         result;                                                 \
481 })
482
483 extern void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
484                               const int line, unsigned long stack,
485                               char *format, ...)
486             __attribute__ ((format (printf, 7, 8)));
487
488 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
489 {
490         if (cfs_time_after(cfs_time_current(),
491                            cfs_time_add(now, cfs_time_seconds(15))))
492                 CERROR("slow %s %lu sec\n", msg,
493                        cfs_duration_sec(cfs_time_sub(cfs_time_current(), now)));
494 }
495
496 /*
497  * helper function similar to do_gettimeofday() of Linux kernel
498  */
499 static inline void cfs_fs_timeval(struct timeval *tv)
500 {
501         cfs_fs_time_t time;
502
503         cfs_fs_time_current(&time);
504         cfs_fs_time_usec(&time, tv);
505 }
506
507 /*
508  * return valid time-out based on user supplied one. Currently we only check
509  * that time-out is not shorted than allowed.
510  */
511 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
512 {
513         if (timeout < cfs_time_minimal_timeout())
514                 timeout = cfs_time_minimal_timeout();
515         return timeout;
516 }
517
518 /*
519  * Portable memory allocator API (draft)
520  */
521 enum cfs_alloc_flags {
522         /* allocation is not allowed to block */
523         CFS_ALLOC_ATOMIC = (1 << 0),
524         /* allocation is allowed to block */
525         CFS_ALLOC_WAIT = (1 << 1),
526         /* allocation should return zeroed memory */
527         CFS_ALLOC_ZERO   = (1 << 2),
528         /* allocation is allowed to call file-system code to free/clean
529          * memory */
530         CFS_ALLOC_FS     = (1 << 3),
531         /* allocation is allowed to do io to free/clean memory */
532         CFS_ALLOC_IO     = (1 << 4),
533         /* standard allocator flag combination */
534         CFS_ALLOC_STD    = CFS_ALLOC_FS | CFS_ALLOC_IO,
535         CFS_ALLOC_USER   = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
536 };
537
538 #define CFS_SLAB_ATOMIC         CFS_ALLOC_ATOMIC
539 #define CFS_SLAB_WAIT           CFS_ALLOC_WAIT
540 #define CFS_SLAB_ZERO           CFS_ALLOC_ZERO
541 #define CFS_SLAB_FS             CFS_ALLOC_FS
542 #define CFS_SLAB_IO             CFS_ALLOC_IO
543 #define CFS_SLAB_STD            CFS_ALLOC_STD
544 #define CFS_SLAB_USER           CFS_ALLOC_USER
545
546 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
547 enum cfs_page_alloc_flags {
548         /* allow to return page beyond KVM. It has to be mapped into KVM by
549          * cfs_page_map(); */
550         CFS_ALLOC_HIGH   = (1 << 5),
551         CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
552 };
553
554
555 #define _LIBCFS_H
556
557 #endif /* _LIBCFS_H */