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