blob: ea2ed9f56c94ad0654a5f79259e50a1f046bed97 [file] [log] [blame]
Zach Brown52fd3d62005-12-15 14:31:23 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * Copyright (C) 2005 Oracle. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 021110-1307, USA.
20 */
21
22#ifndef O2CLUSTER_MASKLOG_H
23#define O2CLUSTER_MASKLOG_H
24
25/*
26 * For now this is a trivial wrapper around printk() that gives the critical
27 * ability to enable sets of debugging output at run-time. In the future this
28 * will almost certainly be redirected to relayfs so that it can pay a
29 * substantially lower heisenberg tax.
30 *
31 * Callers associate the message with a bitmask and a global bitmask is
32 * maintained with help from /proc. If any of the bits match the message is
33 * output.
34 *
35 * We must have efficient bit tests on i386 and it seems gcc still emits crazy
36 * code for the 64bit compare. It emits very good code for the dual unsigned
37 * long tests, though, completely avoiding tests that can never pass if the
38 * caller gives a constant bitmask that fills one of the longs with all 0s. So
39 * the desire is to have almost all of the calls decided on by comparing just
40 * one of the longs. This leads to having infrequently given bits that are
41 * frequently matched in the high bits.
42 *
43 * _ERROR and _NOTICE are used for messages that always go to the console and
44 * have appropriate KERN_ prefixes. We wrap these in our function instead of
45 * just calling printk() so that this can eventually make its way through
46 * relayfs along with the debugging messages. Everything else gets KERN_DEBUG.
47 * The inline tests and macro dance give GCC the opportunity to quite cleverly
48 * only emit the appropriage printk() when the caller passes in a constant
49 * mask, as is almost always the case.
50 *
Coly Li2b53bc72009-05-05 20:03:28 +080051 * All this bitmask nonsense is managed from the files under
52 * /sys/fs/o2cb/logmask/. Reading the files gives a straightforward
53 * indication of which bits are allowed (allow) or denied (off/deny).
54 * ENTRY deny
55 * EXIT deny
Zach Brown52fd3d62005-12-15 14:31:23 -080056 * TCP off
57 * MSG off
58 * SOCKET off
Coly Li2b53bc72009-05-05 20:03:28 +080059 * ERROR allow
60 * NOTICE allow
Zach Brown52fd3d62005-12-15 14:31:23 -080061 *
62 * Writing changes the state of a given bit and requires a strictly formatted
63 * single write() call:
64 *
Coly Li2b53bc72009-05-05 20:03:28 +080065 * write(fd, "allow", 5);
Zach Brown52fd3d62005-12-15 14:31:23 -080066 *
Coly Li2b53bc72009-05-05 20:03:28 +080067 * Echoing allow/deny/off string into the logmask files can flip the bits
68 * on or off as expected; here is the bash script for example:
Zach Brown52fd3d62005-12-15 14:31:23 -080069 *
Coly Li2b53bc72009-05-05 20:03:28 +080070 * log_mask="/sys/fs/o2cb/log_mask"
71 * for node in ENTRY EXIT TCP MSG SOCKET ERROR NOTICE; do
72 * echo allow >"$log_mask"/"$node"
73 * done
Zach Brown52fd3d62005-12-15 14:31:23 -080074 *
Coly Li2b53bc72009-05-05 20:03:28 +080075 * The debugfs.ocfs2 tool can also flip the bits with the -l option:
76 *
77 * debugfs.ocfs2 -l TCP allow
Zach Brown52fd3d62005-12-15 14:31:23 -080078 */
79
80/* for task_struct */
81#include <linux/sched.h>
82
83/* bits that are frequently given and infrequently matched in the low word */
84/* NOTE: If you add a flag, you need to also update mlog.c! */
85#define ML_ENTRY 0x0000000000000001ULL /* func call entry */
86#define ML_EXIT 0x0000000000000002ULL /* func call exit */
87#define ML_TCP 0x0000000000000004ULL /* net cluster/tcp.c */
88#define ML_MSG 0x0000000000000008ULL /* net network messages */
89#define ML_SOCKET 0x0000000000000010ULL /* net socket lifetime */
90#define ML_HEARTBEAT 0x0000000000000020ULL /* hb all heartbeat tracking */
91#define ML_HB_BIO 0x0000000000000040ULL /* hb io tracing */
92#define ML_DLMFS 0x0000000000000080ULL /* dlm user dlmfs */
93#define ML_DLM 0x0000000000000100ULL /* dlm general debugging */
94#define ML_DLM_DOMAIN 0x0000000000000200ULL /* dlm domain debugging */
95#define ML_DLM_THREAD 0x0000000000000400ULL /* dlm domain thread */
96#define ML_DLM_MASTER 0x0000000000000800ULL /* dlm master functions */
97#define ML_DLM_RECOVERY 0x0000000000001000ULL /* dlm master functions */
98#define ML_AIO 0x0000000000002000ULL /* ocfs2 aio read and write */
99#define ML_JOURNAL 0x0000000000004000ULL /* ocfs2 journalling functions */
100#define ML_DISK_ALLOC 0x0000000000008000ULL /* ocfs2 disk allocation */
101#define ML_SUPER 0x0000000000010000ULL /* ocfs2 mount / umount */
102#define ML_FILE_IO 0x0000000000020000ULL /* ocfs2 file I/O */
103#define ML_EXTENT_MAP 0x0000000000040000ULL /* ocfs2 extent map caching */
104#define ML_DLM_GLUE 0x0000000000080000ULL /* ocfs2 dlm glue layer */
105#define ML_BH_IO 0x0000000000100000ULL /* ocfs2 buffer I/O */
106#define ML_UPTODATE 0x0000000000200000ULL /* ocfs2 caching sequence #'s */
107#define ML_NAMEI 0x0000000000400000ULL /* ocfs2 directory / namespace */
108#define ML_INODE 0x0000000000800000ULL /* ocfs2 inode manipulation */
109#define ML_VOTE 0x0000000001000000ULL /* ocfs2 node messaging */
110#define ML_DCACHE 0x0000000002000000ULL /* ocfs2 dcache operations */
111#define ML_CONN 0x0000000004000000ULL /* net connection management */
112#define ML_QUORUM 0x0000000008000000ULL /* net connection quorum */
113#define ML_EXPORT 0x0000000010000000ULL /* ocfs2 export operations */
Tao Maf56654c2008-08-18 17:38:48 +0800114#define ML_XATTR 0x0000000020000000ULL /* ocfs2 extended attributes */
Jan Kara9e33d692008-08-25 19:56:50 +0200115#define ML_QUOTA 0x0000000040000000ULL /* ocfs2 quota operations */
Tao Maf2c870e2009-08-18 11:19:26 +0800116#define ML_REFCOUNT 0x0000000080000000ULL /* refcount tree operations */
Sunil Mushran9b915182010-02-26 19:42:44 -0800117#define ML_BASTS 0x0000001000000000ULL /* dlmglue asts and basts */
Zach Brown52fd3d62005-12-15 14:31:23 -0800118/* bits that are infrequently given and frequently matched in the high word */
119#define ML_ERROR 0x0000000100000000ULL /* sent to KERN_ERR */
120#define ML_NOTICE 0x0000000200000000ULL /* setn to KERN_NOTICE */
121#define ML_KTHREAD 0x0000000400000000ULL /* kernel thread activity */
Sunil Mushran39a29852010-10-07 17:30:17 -0700122#define ML_RESERVATIONS 0x0000000800000000ULL /* ocfs2 alloc reservations */
123#define ML_CLUSTER 0x0000001000000000ULL /* cluster stack */
Zach Brown52fd3d62005-12-15 14:31:23 -0800124
125#define MLOG_INITIAL_AND_MASK (ML_ERROR|ML_NOTICE)
126#define MLOG_INITIAL_NOT_MASK (ML_ENTRY|ML_EXIT)
127#ifndef MLOG_MASK_PREFIX
128#define MLOG_MASK_PREFIX 0
129#endif
130
Joel Becker2b388c62006-05-10 18:28:59 -0700131/*
132 * When logging is disabled, force the bit test to 0 for anything other
133 * than errors and notices, allowing gcc to remove the code completely.
134 * When enabled, allow all masks.
135 */
136#if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
137#define ML_ALLOWED_BITS ~0
138#else
139#define ML_ALLOWED_BITS (ML_ERROR|ML_NOTICE)
140#endif
141
Zach Brown52fd3d62005-12-15 14:31:23 -0800142#define MLOG_MAX_BITS 64
143
144struct mlog_bits {
145 unsigned long words[MLOG_MAX_BITS / BITS_PER_LONG];
146};
147
148extern struct mlog_bits mlog_and_bits, mlog_not_bits;
149
150#if BITS_PER_LONG == 32
151
152#define __mlog_test_u64(mask, bits) \
153 ( (u32)(mask & 0xffffffff) & bits.words[0] || \
154 ((u64)(mask) >> 32) & bits.words[1] )
155#define __mlog_set_u64(mask, bits) do { \
156 bits.words[0] |= (u32)(mask & 0xffffffff); \
157 bits.words[1] |= (u64)(mask) >> 32; \
158} while (0)
159#define __mlog_clear_u64(mask, bits) do { \
160 bits.words[0] &= ~((u32)(mask & 0xffffffff)); \
161 bits.words[1] &= ~((u64)(mask) >> 32); \
162} while (0)
163#define MLOG_BITS_RHS(mask) { \
164 { \
165 [0] = (u32)(mask & 0xffffffff), \
166 [1] = (u64)(mask) >> 32, \
167 } \
168}
169
170#else /* 32bit long above, 64bit long below */
171
172#define __mlog_test_u64(mask, bits) ((mask) & bits.words[0])
173#define __mlog_set_u64(mask, bits) do { \
174 bits.words[0] |= (mask); \
175} while (0)
176#define __mlog_clear_u64(mask, bits) do { \
177 bits.words[0] &= ~(mask); \
178} while (0)
179#define MLOG_BITS_RHS(mask) { { (mask) } }
180
181#endif
182
183/*
184 * smp_processor_id() "helpfully" screams when called outside preemptible
185 * regions in current kernels. sles doesn't have the variants that don't
186 * scream. just do this instead of trying to guess which we're building
187 * against.. *sigh*.
188 */
189#define __mlog_cpu_guess ({ \
190 unsigned long _cpu = get_cpu(); \
191 put_cpu(); \
192 _cpu; \
193})
194
195/* In the following two macros, the whitespace after the ',' just
196 * before ##args is intentional. Otherwise, gcc 2.95 will eat the
197 * previous token if args expands to nothing.
198 */
199#define __mlog_printk(level, fmt, args...) \
Sunil Mushran8545e032010-02-12 14:09:06 -0800200 printk(level "(%s,%u,%lu):%s:%d " fmt, current->comm, \
201 task_pid_nr(current), __mlog_cpu_guess, \
202 __PRETTY_FUNCTION__, __LINE__ , ##args)
Zach Brown52fd3d62005-12-15 14:31:23 -0800203
204#define mlog(mask, fmt, args...) do { \
205 u64 __m = MLOG_MASK_PREFIX | (mask); \
Joel Becker2b388c62006-05-10 18:28:59 -0700206 if ((__m & ML_ALLOWED_BITS) && \
207 __mlog_test_u64(__m, mlog_and_bits) && \
Zach Brown52fd3d62005-12-15 14:31:23 -0800208 !__mlog_test_u64(__m, mlog_not_bits)) { \
209 if (__m & ML_ERROR) \
210 __mlog_printk(KERN_ERR, "ERROR: "fmt , ##args); \
211 else if (__m & ML_NOTICE) \
212 __mlog_printk(KERN_NOTICE, fmt , ##args); \
213 else __mlog_printk(KERN_INFO, fmt , ##args); \
214 } \
215} while (0)
216
217#define mlog_errno(st) do { \
218 int _st = (st); \
219 if (_st != -ERESTARTSYS && _st != -EINTR && \
Mark Fashehef9f86c2007-11-19 18:31:17 -0800220 _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC) \
Zach Brown52fd3d62005-12-15 14:31:23 -0800221 mlog(ML_ERROR, "status = %lld\n", (long long)_st); \
222} while (0)
223
Joel Becker2b388c62006-05-10 18:28:59 -0700224#if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
Zach Brown52fd3d62005-12-15 14:31:23 -0800225#define mlog_entry(fmt, args...) do { \
226 mlog(ML_ENTRY, "ENTRY:" fmt , ##args); \
227} while (0)
228
229#define mlog_entry_void() do { \
230 mlog(ML_ENTRY, "ENTRY:\n"); \
231} while (0)
232
Andrew Mortona1365642006-01-08 01:04:09 -0800233/*
234 * We disable this for sparse.
Zach Brown52fd3d62005-12-15 14:31:23 -0800235 */
Andrew Mortona1365642006-01-08 01:04:09 -0800236#if !defined(__CHECKER__)
Zach Brown52fd3d62005-12-15 14:31:23 -0800237#define mlog_exit(st) do { \
238 if (__builtin_types_compatible_p(typeof(st), unsigned long)) \
239 mlog(ML_EXIT, "EXIT: %lu\n", (unsigned long) (st)); \
240 else if (__builtin_types_compatible_p(typeof(st), signed long)) \
241 mlog(ML_EXIT, "EXIT: %ld\n", (signed long) (st)); \
242 else if (__builtin_types_compatible_p(typeof(st), unsigned int) \
243 || __builtin_types_compatible_p(typeof(st), unsigned short) \
244 || __builtin_types_compatible_p(typeof(st), unsigned char)) \
245 mlog(ML_EXIT, "EXIT: %u\n", (unsigned int) (st)); \
246 else if (__builtin_types_compatible_p(typeof(st), signed int) \
247 || __builtin_types_compatible_p(typeof(st), signed short) \
248 || __builtin_types_compatible_p(typeof(st), signed char)) \
249 mlog(ML_EXIT, "EXIT: %d\n", (signed int) (st)); \
250 else if (__builtin_types_compatible_p(typeof(st), long long)) \
251 mlog(ML_EXIT, "EXIT: %lld\n", (long long) (st)); \
252 else \
253 mlog(ML_EXIT, "EXIT: %llu\n", (unsigned long long) (st)); \
254} while (0)
255#else
256#define mlog_exit(st) do { \
257 mlog(ML_EXIT, "EXIT: %lld\n", (long long) (st)); \
258} while (0)
259#endif
260
261#define mlog_exit_ptr(ptr) do { \
262 mlog(ML_EXIT, "EXIT: %p\n", ptr); \
263} while (0)
264
265#define mlog_exit_void() do { \
266 mlog(ML_EXIT, "EXIT\n"); \
267} while (0)
Joel Becker2b388c62006-05-10 18:28:59 -0700268#else
269#define mlog_entry(...) do { } while (0)
270#define mlog_entry_void(...) do { } while (0)
271#define mlog_exit(...) do { } while (0)
272#define mlog_exit_ptr(...) do { } while (0)
273#define mlog_exit_void(...) do { } while (0)
274#endif /* defined(CONFIG_OCFS2_DEBUG_MASKLOG) */
Zach Brown52fd3d62005-12-15 14:31:23 -0800275
276#define mlog_bug_on_msg(cond, fmt, args...) do { \
277 if (cond) { \
278 mlog(ML_ERROR, "bug expression: " #cond "\n"); \
279 mlog(ML_ERROR, fmt, ##args); \
280 BUG(); \
281 } \
282} while (0)
283
Zach Brown52fd3d62005-12-15 14:31:23 -0800284#include <linux/kobject.h>
285#include <linux/sysfs.h>
Greg Kroah-Hartman823bccf2007-04-13 13:15:19 -0700286int mlog_sys_init(struct kset *o2cb_subsys);
Zach Brown52fd3d62005-12-15 14:31:23 -0800287void mlog_sys_shutdown(void);
288
289#endif /* O2CLUSTER_MASKLOG_H */