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