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