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