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