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