1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #ifndef __LIBCFS_LIBCFS_H__
38 #define __LIBCFS_LIBCFS_H__
41 #define __attribute__(x)
44 #if defined(__linux__)
45 #include <libcfs/linux/libcfs.h>
46 #elif defined(__APPLE__)
47 #include <libcfs/darwin/libcfs.h>
48 #elif defined(__WINNT__)
49 #include <libcfs/winnt/libcfs.h>
51 #error Unsupported operating system.
60 /* Controlled via configure key */
61 /* #define LIBCFS_DEBUG */
64 # define offsetof(typ,memb) ((unsigned long)((char *)&(((typ *)0)->memb)))
67 /* cardinality of array */
68 #define sizeof_array(a) ((sizeof (a)) / (sizeof ((a)[0])))
70 #if !defined(container_of)
71 /* given a pointer @ptr to the field @member embedded into type (usually
72 * struct) @type, return pointer to the embedding instance of @type. */
73 #define container_of(ptr, type, member) \
74 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
77 #define container_of0(ptr, type, member) \
79 typeof(ptr) __ptr = (ptr); \
82 if (unlikely(IS_ERR(__ptr) || __ptr == NULL)) \
83 __res = (type *)__ptr; \
85 __res = container_of(__ptr, type, member); \
90 * true iff @i is power-of-2
100 #define LOWEST_BIT_SET(x) ((x) & ~((x) - 1))
105 extern unsigned int libcfs_subsystem_debug;
106 extern unsigned int libcfs_stack;
107 extern unsigned int libcfs_debug;
108 extern unsigned int libcfs_printk;
109 extern unsigned int libcfs_console_ratelimit;
110 extern cfs_duration_t libcfs_console_max_delay;
111 extern cfs_duration_t libcfs_console_min_delay;
112 extern unsigned int libcfs_console_backoff;
113 extern unsigned int libcfs_debug_binary;
114 extern char debug_file_path[1024];
116 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
117 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
119 /* Has there been an LBUG? */
120 extern unsigned int libcfs_catastrophe;
121 extern unsigned int libcfs_panic_on_lbug;
124 * struct ptldebug_header is defined in libcfs/<os>/libcfs.h
127 #define PH_FLAG_FIRST_RECORD 1
129 /* Debugging subsystems (32 bits, non-overlapping) */
130 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
131 #define S_UNDEFINED 0x00000001
132 #define S_MDC 0x00000002
133 #define S_MDS 0x00000004
134 #define S_OSC 0x00000008
135 #define S_OST 0x00000010
136 #define S_CLASS 0x00000020
137 #define S_LOG 0x00000040
138 #define S_LLITE 0x00000080
139 #define S_RPC 0x00000100
140 #define S_MGMT 0x00000200
141 #define S_LNET 0x00000400
142 #define S_LND 0x00000800 /* ALL LNDs */
143 #define S_PINGER 0x00001000
144 #define S_FILTER 0x00002000
146 #define S_ECHO 0x00008000
147 #define S_LDLM 0x00010000
148 #define S_LOV 0x00020000
154 #define S_LMV 0x00800000 /* b_new_cmd */
156 #define S_SEC 0x02000000 /* upcall cache */
157 #define S_GSS 0x04000000 /* b_new_cmd */
159 #define S_MGC 0x10000000
160 #define S_MGS 0x20000000
161 #define S_FID 0x40000000 /* b_new_cmd */
162 #define S_FLD 0x80000000 /* b_new_cmd */
163 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
165 /* Debugging masks (32 bits, non-overlapping) */
166 /* keep these in sync with lnet/utils/debug.c and lnet/libcfs/debug.c */
167 #define D_TRACE 0x00000001 /* ENTRY/EXIT markers */
168 #define D_INODE 0x00000002
169 #define D_SUPER 0x00000004
170 #define D_EXT2 0x00000008 /* anything from ext2_debug */
171 #define D_MALLOC 0x00000010 /* print malloc, free information */
172 #define D_CACHE 0x00000020 /* cache-related items */
173 #define D_INFO 0x00000040 /* general information */
174 #define D_IOCTL 0x00000080 /* ioctl related information */
175 #define D_NETERROR 0x00000100 /* network errors */
176 #define D_NET 0x00000200 /* network communications */
177 #define D_WARNING 0x00000400 /* CWARN(...) == CDEBUG (D_WARNING, ...) */
178 #define D_BUFFS 0x00000800
179 #define D_OTHER 0x00001000
180 #define D_DENTRY 0x00002000
181 #define D_NETTRACE 0x00004000
182 #define D_PAGE 0x00008000 /* bulk page handling */
183 #define D_DLMTRACE 0x00010000
184 #define D_ERROR 0x00020000 /* CERROR(...) == CDEBUG (D_ERROR, ...) */
185 #define D_EMERG 0x00040000 /* CEMERG(...) == CDEBUG (D_EMERG, ...) */
186 #define D_HA 0x00080000 /* recovery and failover */
187 #define D_RPCTRACE 0x00100000 /* for distributed debugging */
188 #define D_VFSTRACE 0x00200000
189 #define D_READA 0x00400000 /* read-ahead */
190 #define D_MMAP 0x00800000
191 #define D_CONFIG 0x01000000
192 #define D_CONSOLE 0x02000000
193 #define D_QUOTA 0x04000000
194 #define D_SEC 0x08000000
195 /* keep these in sync with lnet/{utils,libcfs}/debug.c */
197 #define D_CANTMASK (D_ERROR | D_EMERG | D_WARNING | D_CONSOLE)
199 #ifndef DEBUG_SUBSYSTEM
200 # define DEBUG_SUBSYSTEM S_UNDEFINED
203 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600)) /* jiffies */
204 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
205 #define CDEBUG_DEFAULT_BACKOFF 2
207 cfs_time_t cdls_next;
209 cfs_duration_t cdls_delay;
210 } cfs_debug_limit_state_t;
212 /* Controlled via configure key */
213 /* #define CDEBUG_ENABLED */
215 #if defined(__KERNEL__) || (defined(__arch_lib__) && !defined(LUSTRE_UTILS))
217 #ifdef CDEBUG_ENABLED
218 #define __CDEBUG(cdls, mask, format, a...) \
222 if (((mask) & D_CANTMASK) != 0 || \
223 ((libcfs_debug & (mask)) != 0 && \
224 (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) \
225 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, mask, \
226 __FILE__, __FUNCTION__, __LINE__, \
230 #define CDEBUG(mask, format, a...) __CDEBUG(NULL, mask, format, ## a)
232 #define CDEBUG_LIMIT(mask, format, a...) \
234 static cfs_debug_limit_state_t cdls; \
236 __CDEBUG(&cdls, mask, format, ## a); \
239 #else /* CDEBUG_ENABLED */
240 #define CDEBUG(mask, format, a...) (void)(0)
241 #define CDEBUG_LIMIT(mask, format, a...) (void)(0)
242 #warning "CDEBUG IS DISABLED. THIS SHOULD NEVER BE DONE FOR PRODUCTION!"
247 #define CDEBUG(mask, format, a...) \
249 if (((mask) & D_CANTMASK) != 0) \
250 fprintf(stderr, "(%s:%d:%s()) " format, \
251 __FILE__, __LINE__, __FUNCTION__, ## a); \
254 #define CDEBUG_LIMIT CDEBUG
256 #endif /* !__KERNEL__ */
259 * Lustre Error Checksum: calculates checksum
260 * of Hex number by XORing each bit.
262 #define LERRCHKSUM(hexnum) (((hexnum) & 0xf) ^ ((hexnum) >> 4 & 0xf) ^ \
263 ((hexnum) >> 8 & 0xf))
265 #define CWARN(format, a...) CDEBUG_LIMIT(D_WARNING, format, ## a)
266 #define CERROR(format, a...) CDEBUG_LIMIT(D_ERROR, format, ## a)
267 #define CEMERG(format, a...) CDEBUG_LIMIT(D_EMERG, format, ## a)
269 #define LCONSOLE(mask, format, a...) CDEBUG(D_CONSOLE | (mask), format, ## a)
270 #define LCONSOLE_INFO(format, a...) CDEBUG_LIMIT(D_CONSOLE, format, ## a)
271 #define LCONSOLE_WARN(format, a...) CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## a)
272 #define LCONSOLE_ERROR_MSG(errnum, format, a...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
273 "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## a)
274 #define LCONSOLE_ERROR(format, a...) LCONSOLE_ERROR_MSG(0x00, format, ## a)
276 #define LCONSOLE_EMERG(format, a...) CDEBUG(D_CONSOLE | D_EMERG, format, ## a)
278 #ifdef CDEBUG_ENABLED
280 #define GOTO(label, rc) \
282 long GOTO__ret = (long)(rc); \
283 CDEBUG(D_TRACE,"Process leaving via %s (rc=%lu : %ld : %lx)\n", \
284 #label, (unsigned long)GOTO__ret, (signed long)GOTO__ret,\
285 (signed long)GOTO__ret); \
289 #define GOTO(label, rc) do { ((void)(rc)); goto label; } while (0)
292 /* Controlled via configure key */
293 /* #define CDEBUG_ENTRY_EXIT */
295 #ifdef CDEBUG_ENTRY_EXIT
298 * if rc == NULL, we need to code as RETURN((void *)NULL), otherwise
299 * there will be a warning in osx.
303 typeof(rc) RETURN__ret = (rc); \
304 CDEBUG(D_TRACE, "Process leaving (rc=%lu : %ld : %lx)\n", \
305 (long)RETURN__ret, (long)RETURN__ret, (long)RETURN__ret);\
307 return RETURN__ret; \
313 CDEBUG(D_TRACE, "Process entered\n"); \
318 CDEBUG(D_TRACE, "Process leaving\n"); \
321 #else /* !CDEBUG_ENTRY_EXIT */
323 #define RETURN(rc) return (rc)
324 #define ENTRY do { } while (0)
325 #define EXIT do { } while (0)
327 #endif /* !CDEBUG_ENTRY_EXIT */
330 * Some (nomina odiosa sunt) platforms define NULL as naked 0. This confuses
331 * Lustre RETURN(NULL) macro.
337 #define NULL ((void *)0)
339 #define LUSTRE_SRV_LNET_PID LUSTRE_LNET_PID
343 #include <libcfs/list.h>
345 struct libcfs_ioctl_data; /* forward ref */
347 struct libcfs_ioctl_handler {
348 struct list_head item;
349 int (*handle_ioctl)(unsigned int cmd, struct libcfs_ioctl_data *data);
352 #define DECLARE_IOCTL_HANDLER(ident, func) \
353 struct libcfs_ioctl_handler ident = { \
354 /* .item = */ CFS_LIST_HEAD_INIT(ident.item), \
355 /* .handle_ioctl = */ func \
358 int libcfs_register_ioctl(struct libcfs_ioctl_handler *hand);
359 int libcfs_deregister_ioctl(struct libcfs_ioctl_handler *hand);
362 int libcfs_ipif_query(char *name, int *up, __u32 *ip, __u32 *mask);
363 int libcfs_ipif_enumerate(char ***names);
364 void libcfs_ipif_free_enumeration(char **names, int n);
365 int libcfs_sock_listen(cfs_socket_t **sockp, __u32 ip, int port, int backlog);
366 int libcfs_sock_accept(cfs_socket_t **newsockp, cfs_socket_t *sock);
367 void libcfs_sock_abort_accept(cfs_socket_t *sock);
368 int libcfs_sock_connect(cfs_socket_t **sockp, int *fatal,
369 __u32 local_ip, int local_port,
370 __u32 peer_ip, int peer_port);
371 int libcfs_sock_setbuf(cfs_socket_t *socket, int txbufsize, int rxbufsize);
372 int libcfs_sock_getbuf(cfs_socket_t *socket, int *txbufsize, int *rxbufsize);
373 int libcfs_sock_getaddr(cfs_socket_t *socket, int remote, __u32 *ip, int *port);
374 int libcfs_sock_write(cfs_socket_t *sock, void *buffer, int nob, int timeout);
375 int libcfs_sock_read(cfs_socket_t *sock, void *buffer, int nob, int timeout);
376 void libcfs_sock_release(cfs_socket_t *sock);
378 /* libcfs watchdogs */
381 /* Add a watchdog which fires after "time" milliseconds of delay. You have to
382 * touch it once to enable it. */
383 struct lc_watchdog *lc_watchdog_add(int time,
384 void (*cb)(pid_t pid, void *),
387 /* Enables a watchdog and resets its timer. */
388 void lc_watchdog_touch_ms(struct lc_watchdog *lcw, int timeout_ms);
389 void lc_watchdog_touch(struct lc_watchdog *lcw);
391 /* Disable a watchdog; touch it to restart it. */
392 void lc_watchdog_disable(struct lc_watchdog *lcw);
394 /* Clean up the watchdog */
395 void lc_watchdog_delete(struct lc_watchdog *lcw);
397 /* Dump a debug log */
398 void lc_watchdog_dumplog(pid_t pid, void *data);
403 /* need both kernel and user-land acceptor */
404 #define LNET_ACCEPTOR_MIN_RESERVED_PORT 512
405 #define LNET_ACCEPTOR_MAX_RESERVED_PORT 1023
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 (time_t)cfs_fs_time_sec(&t);
458 static inline cfs_time_t cfs_time_shift(int seconds)
460 return cfs_time_add(cfs_time_current(), cfs_time_seconds(seconds));
463 static inline long cfs_timeval_sub(struct timeval *large, struct timeval *small,
464 struct timeval *result)
467 (large->tv_sec - small->tv_sec) * ONE_MILLION +
468 (large->tv_usec - small->tv_usec));
469 if (result != NULL) {
470 result->tv_usec = r % ONE_MILLION;
471 result->tv_sec = r / ONE_MILLION;
476 #define CFS_RATELIMIT(seconds) \
479 * XXX nikita: non-portable initializer \
481 static time_t __next_message = 0; \
484 if (cfs_time_after(cfs_time_current(), __next_message)) \
487 __next_message = cfs_time_shift(seconds); \
493 struct libcfs_debug_msg_data {
494 cfs_debug_limit_state_t *msg_cdls;
496 const char *msg_file;
501 #define DEBUG_MSG_DATA_INIT(cdls, subsystem, file, func, ln ) { \
502 .msg_cdls = (cdls), \
503 .msg_subsys = (subsystem), \
504 .msg_file = (file), \
510 extern int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls,
511 int subsys, int mask,
512 const char *file, const char *fn, const int line,
513 const char *format1, va_list args,
514 const char *format2, ...)
515 __attribute__ ((format (printf, 9, 10)));
517 #define libcfs_debug_vmsg(cdls, subsys, mask, file, fn, line, format, args) \
518 libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,format,args,NULL,NULL)
520 #define libcfs_debug_msg(cdls, subsys, mask, file, fn, line, format, a...) \
521 libcfs_debug_vmsg2(cdls, subsys, mask, file, fn,line,NULL,NULL,format, ##a)
523 #define cdebug_va(cdls, mask, file, func, line, fmt, args) do { \
526 if (((mask) & D_CANTMASK) != 0 || \
527 ((libcfs_debug & (mask)) != 0 && \
528 (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) \
529 libcfs_debug_vmsg(cdls, DEBUG_SUBSYSTEM, (mask), \
530 (file), (func), (line), fmt, args); \
533 #define cdebug(cdls, mask, file, func, line, fmt, a...) do { \
536 if (((mask) & D_CANTMASK) != 0 || \
537 ((libcfs_debug & (mask)) != 0 && \
538 (libcfs_subsystem_debug & DEBUG_SUBSYSTEM) != 0)) \
539 libcfs_debug_msg(cdls, DEBUG_SUBSYSTEM, (mask), \
540 (file), (func), (line), fmt, ## a); \
543 extern void libcfs_assertion_failed(const char *expr, const char *file,
544 const char *fn, const int line);
546 static inline void cfs_slow_warning(cfs_time_t now, int seconds, char *msg)
548 if (cfs_time_after(cfs_time_current(),
549 cfs_time_add(now, cfs_time_seconds(15))))
550 CERROR("slow %s "CFS_TIME_T" sec\n", msg,
551 cfs_duration_sec(cfs_time_sub(cfs_time_current(),now)));
555 * helper function similar to do_gettimeofday() of Linux kernel
557 static inline void cfs_fs_timeval(struct timeval *tv)
561 cfs_fs_time_current(&time);
562 cfs_fs_time_usec(&time, tv);
566 * return valid time-out based on user supplied one. Currently we only check
567 * that time-out is not shorted than allowed.
569 static inline cfs_duration_t cfs_timeout_cap(cfs_duration_t timeout)
571 if (timeout < CFS_TICK)
577 * Universal memory allocator API
579 enum cfs_alloc_flags {
580 /* allocation is not allowed to block */
581 CFS_ALLOC_ATOMIC = 0x1,
582 /* allocation is allowed to block */
583 CFS_ALLOC_WAIT = 0x2,
584 /* allocation should return zeroed memory */
585 CFS_ALLOC_ZERO = 0x4,
586 /* allocation is allowed to call file-system code to free/clean
589 /* allocation is allowed to do io to free/clean memory */
591 /* don't report allocation failure to the console */
592 CFS_ALLOC_NOWARN = 0x20,
593 /* standard allocator flag combination */
594 CFS_ALLOC_STD = CFS_ALLOC_FS | CFS_ALLOC_IO,
595 CFS_ALLOC_USER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO,
598 /* flags for cfs_page_alloc() in addition to enum cfs_alloc_flags */
599 enum cfs_alloc_page_flags {
600 /* allow to return page beyond KVM. It has to be mapped into KVM by
602 CFS_ALLOC_HIGH = 0x40,
603 CFS_ALLOC_HIGHUSER = CFS_ALLOC_WAIT | CFS_ALLOC_FS | CFS_ALLOC_IO | CFS_ALLOC_HIGH,
607 * Drop into debugger, if possible. Implementation is provided by platform.
610 void cfs_enter_debugger(void);
613 * Defined by platform
615 void cfs_daemonize(char *str);
616 int cfs_daemonize_ctxt(char *str);
617 cfs_sigset_t cfs_get_blocked_sigs(void);
618 cfs_sigset_t cfs_block_allsigs(void);
619 cfs_sigset_t cfs_block_sigs(cfs_sigset_t bits);
620 void cfs_restore_sigs(cfs_sigset_t);
621 int cfs_signal_pending(void);
622 void cfs_clear_sigpending(void);
625 * these macros should be removed in the future,
626 * we keep them just for keeping libcfs compatible
627 * with other branches.
629 #define libcfs_daemonize(s) cfs_daemonize(s)
630 #define cfs_sigmask_lock(f) do { f= 0; } while (0)
631 #define cfs_sigmask_unlock(f) do { f= 0; } while (0)
633 int convert_server_error(__u64 ecode);
634 int convert_client_oflag(int cflag, int *result);
637 * Stack-tracing filling.
641 * Platform-dependent data-type to hold stack frames.
643 struct cfs_stack_trace;
646 * Fill @trace with current back-trace.
648 void cfs_stack_trace_fill(struct cfs_stack_trace *trace);
651 * Return instruction pointer for frame @frame_no. NULL if @frame_no is
654 void *cfs_stack_trace_frame(struct cfs_stack_trace *trace, int frame_no);
657 * Universal open flags.
659 #define CFS_O_ACCMODE 0003
660 #define CFS_O_CREAT 0100
661 #define CFS_O_EXCL 0200
662 #define CFS_O_NOCTTY 0400
663 #define CFS_O_TRUNC 01000
664 #define CFS_O_APPEND 02000
665 #define CFS_O_NONBLOCK 04000
666 #define CFS_O_NDELAY CFS_O_NONBLOCK
667 #define CFS_O_SYNC 010000
668 #define CFS_O_ASYNC 020000
669 #define CFS_O_DIRECT 040000
670 #define CFS_O_LARGEFILE 0100000
671 #define CFS_O_DIRECTORY 0200000
672 #define CFS_O_NOFOLLOW 0400000
673 #define CFS_O_NOATIME 01000000
675 /* convert local open flags to universal open flags */
676 int cfs_oflags2univ(int flags);
677 /* convert universal open flags to local open flags */
678 int cfs_univ2oflags(int flags);
680 #include "libcfs_private.h"
684 #endif /* _LIBCFS_H */