1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 #ifndef __LIBCFS_LIBCFS_H__
5 #define __LIBCFS_LIBCFS_H__
8 #define __attribute__(x)
11 #if defined(__linux__)
12 #include <libcfs/linux/libcfs.h>
13 #elif defined(__APPLE__)
14 #include <libcfs/darwin/libcfs.h>
16 #error Unsupported operating system.
28 # define offsetof(typ,memb) ((unsigned long)((char *)&(((typ *)0)->memb)))
31 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
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;
41 /* Has there been an LBUG? */
42 extern unsigned int portals_catastrophe;
45 * struct ptldebug_header is defined in libcfs/<os>/libcfs.h
48 #define PH_FLAG_FIRST_RECORD 1
50 /* Debugging subsystems (32 bits, non-overlapping) */
51 #define S_UNDEFINED 0x00000001
52 #define S_MDC 0x00000002
53 #define S_MDS 0x00000004
54 #define S_OSC 0x00000008
55 #define S_OST 0x00000010
56 #define S_CLASS 0x00000020
57 #define S_LOG 0x00000040
58 #define S_LLITE 0x00000080
59 #define S_RPC 0x00000100
60 #define S_MGMT 0x00000200
61 #define S_PORTALS 0x00000400
62 #define S_NAL 0x00000800 /* ALL NALs */
63 #define S_PINGER 0x00001000
64 #define S_FILTER 0x00002000
65 #define S_PTLBD 0x00004000
66 #define S_ECHO 0x00008000
67 #define S_LDLM 0x00010000
68 #define S_LOV 0x00020000
69 #define S_PTLROUTER 0x00040000
70 #define S_COBD 0x00080000
71 #define S_SM 0x00100000
72 #define S_ASOBD 0x00200000
73 #define S_CONFOBD 0x00400000
74 #define S_LMV 0x00800000
75 #define S_CMOBD 0x01000000
76 #define S_SEC 0x02000000
77 #define S_GSS 0x04000000
78 #define S_GKS 0x08000000
79 /* If you change these values, please keep these files up to date...
80 * portals/utils/debug.c
84 /* Debugging masks (32 bits, non-overlapping) */
85 #define D_TRACE 0x00000001 /* ENTRY/EXIT markers */
86 #define D_INODE 0x00000002
87 #define D_SUPER 0x00000004
88 #define D_EXT2 0x00000008 /* anything from ext2_debug */
89 #define D_MALLOC 0x00000010 /* print malloc, free information */
90 #define D_CACHE 0x00000020 /* cache-related items */
91 #define D_INFO 0x00000040 /* general information */
92 #define D_IOCTL 0x00000080 /* ioctl related information */
93 #define D_BLOCKS 0x00000100 /* ext2 block allocation */
94 #define D_NET 0x00000200 /* network communications */
95 #define D_WARNING 0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
96 #define D_BUFFS 0x00000800
97 #define D_OTHER 0x00001000
98 #define D_DENTRY 0x00002000
99 #define D_PORTALS 0x00004000 /* ENTRY/EXIT markers */
100 #define D_PAGE 0x00008000 /* bulk page handling */
101 #define D_DLMTRACE 0x00010000
102 #define D_ERROR 0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
103 #define D_EMERG 0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
104 #define D_HA 0x00080000 /* recovery and failover */
105 #define D_RPCTRACE 0x00100000 /* for distributed debugging */
106 #define D_VFSTRACE 0x00200000
107 #define D_READA 0x00400000 /* read-ahead */
108 #define D_MMAP 0x00800000
109 #define D_CONFIG 0x01000000
110 #define D_CONSOLE 0x02000000
111 #define D_QUOTA 0x04000000
112 #define D_SEC 0x08000000
113 /* If you change these values, please keep these files up to date...
114 * portals/utils/debug.c
118 #ifndef DEBUG_SUBSYSTEM
119 # define DEBUG_SUBSYSTEM S_UNDEFINED
123 #define CDEBUG(mask, format, a...) \
125 CHECK_STACK(CDEBUG_STACK); \
126 if (((mask) & (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)) || \
127 (portal_debug & (mask) && \
128 portal_subsystem_debug & DEBUG_SUBSYSTEM)) \
129 portals_debug_msg(DEBUG_SUBSYSTEM, mask, \
130 __FILE__, __FUNCTION__, __LINE__, \
131 CDEBUG_STACK, format, ## a); \
134 #define CDEBUG_MAX_LIMIT 600
135 #define CDEBUG_LIMIT(cdebug_mask, cdebug_format, a...) \
137 static cfs_time_t cdebug_next = 0; \
138 static int cdebug_count = 0; \
139 static cfs_duration_t cdebug_delay = CFS_MIN_DELAY; \
141 CHECK_STACK(CDEBUG_STACK); \
142 if (cfs_time_after(cfs_time_current(), cdebug_next)) { \
143 portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask, __FILE__, \
144 __FUNCTION__, __LINE__, CDEBUG_STACK, \
145 cdebug_format, ## a); \
146 if (cdebug_count) { \
147 portals_debug_msg(DEBUG_SUBSYSTEM, cdebug_mask, \
148 __FILE__, __FUNCTION__, __LINE__,0, \
149 "previously skipped %d similar " \
150 "messages\n", cdebug_count); \
153 if (cfs_time_after(cfs_time_current(), \
155 cfs_time_seconds(CDEBUG_MAX_LIMIT+10))) \
156 cdebug_delay = cdebug_delay > (8 * CFS_MIN_DELAY)? \
157 cdebug_delay/8 : CFS_MIN_DELAY; \
159 cdebug_delay = cdebug_delay*2 >= cfs_time_seconds(CDEBUG_MAX_LIMIT)?\
160 cfs_time_seconds(CDEBUG_MAX_LIMIT) : \
162 cdebug_next = cfs_time_current() + cdebug_delay; \
164 portals_debug_msg(DEBUG_SUBSYSTEM, \
166 ~(D_EMERG|D_ERROR|D_WARNING|D_CONSOLE), \
167 __FILE__, __FUNCTION__, __LINE__, \
168 CDEBUG_STACK, cdebug_format, ## a); \
173 #elif defined(LUSTRE_UTILS)
175 #define CDEBUG(mask, format, a...) \
177 if ((mask) & (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)) \
178 fprintf(stderr, "(%s:%d:%s()) " format, \
179 __FILE__, __LINE__, __FUNCTION__, ## a); \
181 #define CDEBUG_LIMIT CDEBUG
183 #else /* !__KERNEL__ && !LUSTRE_UTILS*/
185 #define CDEBUG(mask, format, a...) \
187 if (((mask) & (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)) || \
188 (portal_debug & (mask) && \
189 portal_subsystem_debug & DEBUG_SUBSYSTEM)) \
190 fprintf(stderr, "(%s:%d:%s()) " format, \
191 __FILE__, __LINE__, __FUNCTION__, ## a); \
193 #define CDEBUG_LIMIT CDEBUG
195 #endif /* !__KERNEL__ */
197 #define CWARN(format, a...) CDEBUG_LIMIT(D_WARNING, format, ## a)
198 #define CERROR(format, a...) CDEBUG_LIMIT(D_ERROR, format, ## a)
199 #define CEMERG(format, a...) CDEBUG_LIMIT(D_EMERG, format, ## a)
201 #define LCONSOLE(mask, format, a...) CDEBUG(D_CONSOLE | (mask), format, ## a)
202 #define LCONSOLE_INFO(format, a...) CDEBUG_LIMIT(D_CONSOLE, format, ## a)
203 #define LCONSOLE_WARN(format, a...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## a)
204 #define LCONSOLE_ERROR(format, a...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, format, ## a)
205 #define LCONSOLE_EMERG(format, a...) CDEBUG(D_CONSOLE | D_EMERG, format, ## a)
207 #define GOTO(label, rc) \
209 long GOTO__ret = (long)(rc); \
210 CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
211 #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
212 (signed long)GOTO__ret); \
216 #define CDEBUG_ENTRY_EXIT 1
217 #if CDEBUG_ENTRY_EXIT
220 * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
221 * there will be a warning in osx.
225 typeof(rc) RETURN__ret = (rc); \
226 CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n", \
227 (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
229 return RETURN__ret; \
235 CDEBUG(D_TRACE, "Process entered\n"); \
240 CDEBUG(D_TRACE, "Process leaving\n"); \
243 #else /* !CDEBUG_ENTRY_EXIT */
245 #define RETURN(rc) return (rc)
246 #define ENTRY do { } while (0)
247 #define EXIT do { } while (0)
249 #endif /* !CDEBUG_ENTRY_EXIT */
252 #define LUSTRE_SRV_PTL_PID LUSTRE_PTL_PID
259 #define PORTALS_CFG_VERSION 0xecf60001
281 __u32 pcfg_plen1; /* buffers in userspace */
282 __u32 pcfg_plen2; /* buffers in userspace */
283 __u32 pcfg_alloc_size; /* size of this allocated portals_cfg */
287 #define PCFG_INIT(pcfg, cmd) \
289 memset(&(pcfg), 0, sizeof((pcfg))); \
290 (pcfg).pcfg_version = PORTALS_CFG_VERSION; \
291 (pcfg).pcfg_command = (cmd); \
295 #define PCFG_INIT_PBUF(pcfg, cmd, plen1, plen2) \
297 int bufsize = size_round(sizeof(*(pcfg))); \
298 bufsize += size_round(plen1) + size_round(plen2); \
299 PORTAL_ALLOC((pcfg), bufsize); \
301 memset((pcfg), 0, bufsize); \
302 (pcfg)->pcfg_version = PORTALS_CFG_VERSION; \
303 (pcfg)->pcfg_command = (cmd); \
304 (pcfg)->pcfg_plen1 = (plen1); \
305 (pcfg)->pcfg_plen2 = (plen2); \
306 (pcfg)->pcfg_alloc_size = bufsize; \
310 #define PCFG_FREE_PBUF(pcfg) PORTAL_FREE((pcfg), (pcfg)->pcfg_alloc_size)
312 #define PCFG_PBUF(pcfg, idx) \
314 ? ((char *)(pcfg) + size_round(sizeof(*(pcfg)))) \
316 ? ((char *)(pcfg) + size_round(sizeof(*(pcfg))) + size_round(pcfg->pcfg_plen1)) \
319 typedef int (nal_cmd_handler_fn)(struct portals_cfg *, void *);
320 int libcfs_nal_cmd_register(int nal, nal_cmd_handler_fn *handler, void *arg);
321 int libcfs_nal_cmd(struct portals_cfg *pcfg);
322 void libcfs_nal_cmd_unregister(int nal);
324 struct portal_ioctl_data {
348 __u32 ioc_plen1; /* buffers in userspace */
350 __u32 ioc_plen2; /* buffers in userspace */
359 #include <libcfs/list.h>
361 struct libcfs_ioctl_handler {
362 struct list_head item;
363 int (*handle_ioctl)(struct portal_ioctl_data *data,
364 unsigned int cmd, unsigned long args);
367 #define DECLARE_IOCTL_HANDLER(ident, func) \
368 struct libcfs_ioctl_handler ident = { \
369 .item = CFS_LIST_HEAD_INIT(ident.item), \
370 .handle_ioctl = func \
373 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
374 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
376 /* libcfs watchdogs */
379 /* Just use the default handler (dumplog) */
380 #define LC_WATCHDOG_DEFAULT_CB NULL
382 /* Add a watchdog which fires after "time" milliseconds of delay. You have to
383 * touch it once to enable it. */
384 struct lc_watchdog *lc_watchdog_add(int time,
385 void (*cb)(struct lc_watchdog *,
386 struct task_struct *,
390 /* Enables a watchdog and resets its timer. */
391 void lc_watchdog_touch(struct lc_watchdog *lcw);
393 /* Disable a watchdog; touch it to restart it. */
394 void lc_watchdog_disable(struct lc_watchdog *lcw);
396 /* Clean up the watchdog */
397 void lc_watchdog_delete(struct lc_watchdog *lcw);
399 /* Dump a debug log */
400 void lc_watchdog_dumplog(struct lc_watchdog *lcw,
401 struct task_struct *tsk,
408 * libcfs pseudo device operations
410 * struct cfs_psdev_t and
411 * cfs_psdev_register() and
412 * cfs_psdev_deregister() are declared in
413 * libcfs/<os>/cfs_prim.h
415 * It's just draft now.
418 struct cfs_psdev_file {
421 unsigned long reserved1;
422 unsigned long reserved2;
425 struct cfs_psdev_ops {
426 int (*p_open)(unsigned long, void *);
427 int (*p_close)(unsigned long, void *);
428 int (*p_read)(struct cfs_psdev_file *, char *, unsigned long);
429 int (*p_write)(struct cfs_psdev_file *, char *, unsigned long);
430 int (*p_ioctl)(struct cfs_psdev_file *, unsigned long, void *);
434 * generic time manipulation functions.
437 static inline int cfs_time_after(cfs_time_t t1, cfs_time_t t2)
439 return cfs_time_before(t2, t1);
442 static inline int cfs_time_aftereq(cfs_time_t t1, cfs_time_t t2)
444 return cfs_time_beforeq(t2, t1);
448 * return seconds since UNIX epoch
450 static inline time_t cfs_unix_seconds(void)
454 cfs_fs_time_current(&t);
455 return cfs_fs_time_sec(&t);
458 #define CFS_RATELIMIT(seconds) \
461 * XXX nikita: non-portable initializer \
463 static time_t __next_message = 0; \
466 if (cfs_time_after(cfs_time_current(), __next_message)) \
469 __next_message = cfs_time_shift(seconds); \
475 extern void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
476 const int line, unsigned long stack,
478 __attribute__ ((format (printf, 7, 8)));
480 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
482 if (cfs_time_after(cfs_time_current(),
483 cfs_time_add(now, cfs_time_seconds(15))))
484 CERROR("slow %s %lu sec\n", msg,
485 cfs_duration_sec(cfs_time_sub(cfs_time_current(), now)));
489 * helper function similar to do_gettimeofday() of Linux kernel
491 static inline void cfs_fs_timeval(struct timeval *tv)
495 cfs_fs_time_current(&time);
496 cfs_fs_time_usec(&time, tv);
500 * return valid time-out based on user supplied one. Currently we only check
501 * that time-out is not shorted than allowed.
503 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
505 if (timeout < cfs_time_minimal_timeout())
506 timeout = cfs_time_minimal_timeout();
511 * Portable memory allocator API (draft)
513 enum cfs_alloc_flags {
514 /* allocation is not allowed to block */
515 CFS_ALLOC_ATOMIC = (1 << 0),
516 /* allocation is allowed to block */
517 CFS_ALLOC_WAIT = (1 << 1),
518 /* allocation should return zeroed memory */
519 CFS_ALLOC_ZERO = (1 << 2),
520 /* allocation is allowed to call file-system code to free/clean
522 CFS_ALLOC_FS = (1 << 3),
523 /* allocation is allowed to do io to free/clean memory */
524 CFS_ALLOC_IO = (1 << 4),
525 /* standard allocator flag combination */
526 CFS_ALLOC_STD = CFS_ALLOC_FS | CFS_ALLOC_IO,
527 CFS_ALLOC_USER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
530 #define CFS_SLAB_ATOMIC CFS_ALLOC_ATOMIC
531 #define CFS_SLAB_WAIT CFS_ALLOC_WAIT
532 #define CFS_SLAB_ZERO CFS_ALLOC_ZERO
533 #define CFS_SLAB_FS CFS_ALLOC_FS
534 #define CFS_SLAB_IO CFS_ALLOC_IO
535 #define CFS_SLAB_STD CFS_ALLOC_STD
536 #define CFS_SLAB_USER CFS_ALLOC_USER
538 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
539 enum cfs_page_alloc_flags {
540 /* allow to return page beyond KVM. It has to be mapped into KVM by
542 CFS_ALLOC_HIGH = (1 << 5),
543 CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
549 #endif /* _LIBCFS_H */