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