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