Merge tag 'wberr-v4.14-1' of git://git.kernel.org/pub/scm/linux/kernel/git/jlayton...
[sfrench/cifs-2.6.git] / drivers / staging / lustre / include / linux / libcfs / libcfs_debug.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/include/libcfs/libcfs_debug.h
33  *
34  * Debug messages and assertions
35  *
36  */
37
38 #ifndef __LIBCFS_DEBUG_H__
39 #define __LIBCFS_DEBUG_H__
40
41 #include <uapi/linux/lnet/libcfs_debug.h>
42
43 /*
44  *  Debugging
45  */
46 extern unsigned int libcfs_subsystem_debug;
47 extern unsigned int libcfs_stack;
48 extern unsigned int libcfs_debug;
49 extern unsigned int libcfs_printk;
50 extern unsigned int libcfs_console_ratelimit;
51 extern unsigned int libcfs_console_max_delay;
52 extern unsigned int libcfs_console_min_delay;
53 extern unsigned int libcfs_console_backoff;
54 extern unsigned int libcfs_debug_binary;
55 extern char libcfs_debug_file_path_arr[PATH_MAX];
56
57 int libcfs_debug_mask2str(char *str, int size, int mask, int is_subsys);
58 int libcfs_debug_str2mask(int *mask, const char *str, int is_subsys);
59
60 /* Has there been an LBUG? */
61 extern unsigned int libcfs_catastrophe;
62 extern unsigned int libcfs_panic_on_lbug;
63
64 #ifndef DEBUG_SUBSYSTEM
65 # define DEBUG_SUBSYSTEM S_UNDEFINED
66 #endif
67
68 #define CDEBUG_DEFAULT_MAX_DELAY (cfs_time_seconds(600))         /* jiffies */
69 #define CDEBUG_DEFAULT_MIN_DELAY ((cfs_time_seconds(1) + 1) / 2) /* jiffies */
70 #define CDEBUG_DEFAULT_BACKOFF   2
71 struct cfs_debug_limit_state {
72         unsigned long   cdls_next;
73         unsigned int cdls_delay;
74         int          cdls_count;
75 };
76
77 struct libcfs_debug_msg_data {
78         const char *msg_file;
79         const char *msg_fn;
80         int         msg_subsys;
81         int         msg_line;
82         int         msg_mask;
83         struct cfs_debug_limit_state *msg_cdls;
84 };
85
86 #define LIBCFS_DEBUG_MSG_DATA_INIT(data, mask, cdls)            \
87 do {                                                            \
88         (data)->msg_subsys = DEBUG_SUBSYSTEM;                   \
89         (data)->msg_file   = __FILE__;                          \
90         (data)->msg_fn     = __func__;                          \
91         (data)->msg_line   = __LINE__;                          \
92         (data)->msg_cdls   = (cdls);                            \
93         (data)->msg_mask   = (mask);                            \
94 } while (0)
95
96 #define LIBCFS_DEBUG_MSG_DATA_DECL(dataname, mask, cdls)        \
97         static struct libcfs_debug_msg_data dataname = {        \
98                .msg_subsys = DEBUG_SUBSYSTEM,                   \
99                .msg_file   = __FILE__,                          \
100                .msg_fn     = __func__,                          \
101                .msg_line   = __LINE__,                          \
102                .msg_cdls   = (cdls)      };                     \
103         dataname.msg_mask   = (mask)
104
105 /**
106  * Filters out logging messages based on mask and subsystem.
107  */
108 static inline int cfs_cdebug_show(unsigned int mask, unsigned int subsystem)
109 {
110         return mask & D_CANTMASK ||
111                 ((libcfs_debug & mask) && (libcfs_subsystem_debug & subsystem));
112 }
113
114 #define __CDEBUG(cdls, mask, format, ...)                               \
115 do {                                                                    \
116         static struct libcfs_debug_msg_data msgdata;                    \
117                                                                         \
118         CFS_CHECK_STACK(&msgdata, mask, cdls);                          \
119                                                                         \
120         if (cfs_cdebug_show(mask, DEBUG_SUBSYSTEM)) {                   \
121                 LIBCFS_DEBUG_MSG_DATA_INIT(&msgdata, mask, cdls);       \
122                 libcfs_debug_msg(&msgdata, format, ## __VA_ARGS__);     \
123         }                                                               \
124 } while (0)
125
126 #define CDEBUG(mask, format, ...) __CDEBUG(NULL, mask, format, ## __VA_ARGS__)
127
128 #define CDEBUG_LIMIT(mask, format, ...)                                 \
129 do {                                                                    \
130         static struct cfs_debug_limit_state cdls;                       \
131                                                                         \
132         __CDEBUG(&cdls, mask, format, ## __VA_ARGS__);                  \
133 } while (0)
134
135 #define CWARN(format, ...)      CDEBUG_LIMIT(D_WARNING, format, ## __VA_ARGS__)
136 #define CERROR(format, ...)     CDEBUG_LIMIT(D_ERROR, format, ## __VA_ARGS__)
137 #define CNETERR(format, a...)   CDEBUG_LIMIT(D_NETERROR, format, ## a)
138 #define CEMERG(format, ...)     CDEBUG_LIMIT(D_EMERG, format, ## __VA_ARGS__)
139
140 #define LCONSOLE(mask, format, ...) CDEBUG(D_CONSOLE | (mask), format, ## __VA_ARGS__)
141 #define LCONSOLE_INFO(format, ...)  CDEBUG_LIMIT(D_CONSOLE, format, ## __VA_ARGS__)
142 #define LCONSOLE_WARN(format, ...)  CDEBUG_LIMIT(D_CONSOLE | D_WARNING, format, ## __VA_ARGS__)
143 #define LCONSOLE_ERROR_MSG(errnum, format, ...) CDEBUG_LIMIT(D_CONSOLE | D_ERROR, \
144                            "%x-%x: " format, errnum, LERRCHKSUM(errnum), ## __VA_ARGS__)
145 #define LCONSOLE_ERROR(format, ...) LCONSOLE_ERROR_MSG(0x00, format, ## __VA_ARGS__)
146
147 #define LCONSOLE_EMERG(format, ...) CDEBUG(D_CONSOLE | D_EMERG, format, ## __VA_ARGS__)
148
149 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
150                      const char *format1, ...)
151         __printf(2, 3);
152
153 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
154                        const char *format1,
155                        va_list args, const char *format2, ...)
156         __printf(4, 5);
157
158 /* other external symbols that tracefile provides: */
159 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
160                             const char __user *usr_buffer, int usr_buffer_nob);
161 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
162                              const char *knl_buffer, char *append);
163
164 #define LIBCFS_DEBUG_FILE_PATH_DEFAULT "/tmp/lustre-log"
165
166 #endif  /* __LIBCFS_DEBUG_H__ */