xref: /openbmc/linux/fs/ocfs2/cluster/masklog.h (revision fa60ce2c)
1328970deSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2*fa60ce2cSMasahiro Yamada /*
352fd3d6fSZach Brown  * Copyright (C) 2005 Oracle.  All rights reserved.
452fd3d6fSZach Brown  */
552fd3d6fSZach Brown 
652fd3d6fSZach Brown #ifndef O2CLUSTER_MASKLOG_H
752fd3d6fSZach Brown #define O2CLUSTER_MASKLOG_H
852fd3d6fSZach Brown 
952fd3d6fSZach Brown /*
1052fd3d6fSZach Brown  * For now this is a trivial wrapper around printk() that gives the critical
1152fd3d6fSZach Brown  * ability to enable sets of debugging output at run-time.  In the future this
1252fd3d6fSZach Brown  * will almost certainly be redirected to relayfs so that it can pay a
1352fd3d6fSZach Brown  * substantially lower heisenberg tax.
1452fd3d6fSZach Brown  *
1552fd3d6fSZach Brown  * Callers associate the message with a bitmask and a global bitmask is
1652fd3d6fSZach Brown  * maintained with help from /proc.  If any of the bits match the message is
1752fd3d6fSZach Brown  * output.
1852fd3d6fSZach Brown  *
1952fd3d6fSZach Brown  * We must have efficient bit tests on i386 and it seems gcc still emits crazy
2052fd3d6fSZach Brown  * code for the 64bit compare.  It emits very good code for the dual unsigned
2152fd3d6fSZach Brown  * long tests, though, completely avoiding tests that can never pass if the
2252fd3d6fSZach Brown  * caller gives a constant bitmask that fills one of the longs with all 0s.  So
2352fd3d6fSZach Brown  * the desire is to have almost all of the calls decided on by comparing just
2452fd3d6fSZach Brown  * one of the longs.  This leads to having infrequently given bits that are
2552fd3d6fSZach Brown  * frequently matched in the high bits.
2652fd3d6fSZach Brown  *
2752fd3d6fSZach Brown  * _ERROR and _NOTICE are used for messages that always go to the console and
2852fd3d6fSZach Brown  * have appropriate KERN_ prefixes.  We wrap these in our function instead of
2952fd3d6fSZach Brown  * just calling printk() so that this can eventually make its way through
3052fd3d6fSZach Brown  * relayfs along with the debugging messages.  Everything else gets KERN_DEBUG.
3152fd3d6fSZach Brown  * The inline tests and macro dance give GCC the opportunity to quite cleverly
3252fd3d6fSZach Brown  * only emit the appropriage printk() when the caller passes in a constant
3352fd3d6fSZach Brown  * mask, as is almost always the case.
3452fd3d6fSZach Brown  *
352b53bc7bSColy Li  * All this bitmask nonsense is managed from the files under
362b53bc7bSColy Li  * /sys/fs/o2cb/logmask/.  Reading the files gives a straightforward
372b53bc7bSColy Li  * indication of which bits are allowed (allow) or denied (off/deny).
382b53bc7bSColy Li  * 	ENTRY deny
392b53bc7bSColy Li  * 	EXIT deny
4052fd3d6fSZach Brown  * 	TCP off
4152fd3d6fSZach Brown  * 	MSG off
4252fd3d6fSZach Brown  * 	SOCKET off
432b53bc7bSColy Li  * 	ERROR allow
442b53bc7bSColy Li  * 	NOTICE allow
4552fd3d6fSZach Brown  *
4652fd3d6fSZach Brown  * Writing changes the state of a given bit and requires a strictly formatted
4752fd3d6fSZach Brown  * single write() call:
4852fd3d6fSZach Brown  *
492b53bc7bSColy Li  * 	write(fd, "allow", 5);
5052fd3d6fSZach Brown  *
512b53bc7bSColy Li  * Echoing allow/deny/off string into the logmask files can flip the bits
522b53bc7bSColy Li  * on or off as expected; here is the bash script for example:
5352fd3d6fSZach Brown  *
542b53bc7bSColy Li  * log_mask="/sys/fs/o2cb/log_mask"
552b53bc7bSColy Li  * for node in ENTRY EXIT TCP MSG SOCKET ERROR NOTICE; do
562b53bc7bSColy Li  *	echo allow >"$log_mask"/"$node"
5752fd3d6fSZach Brown  * done
582b53bc7bSColy Li  *
592b53bc7bSColy Li  * The debugfs.ocfs2 tool can also flip the bits with the -l option:
602b53bc7bSColy Li  *
612b53bc7bSColy Li  * debugfs.ocfs2 -l TCP allow
6252fd3d6fSZach Brown  */
6352fd3d6fSZach Brown 
6452fd3d6fSZach Brown /* for task_struct */
6552fd3d6fSZach Brown #include <linux/sched.h>
6652fd3d6fSZach Brown 
6752fd3d6fSZach Brown /* bits that are frequently given and infrequently matched in the low word */
6841b41a26SSunil Mushran /* NOTE: If you add a flag, you need to also update masklog.c! */
69b4e1b7e8STao Ma #define ML_TCP		0x0000000000000001ULL /* net cluster/tcp.c */
70b4e1b7e8STao Ma #define ML_MSG		0x0000000000000002ULL /* net network messages */
71b4e1b7e8STao Ma #define ML_SOCKET	0x0000000000000004ULL /* net socket lifetime */
72b4e1b7e8STao Ma #define ML_HEARTBEAT	0x0000000000000008ULL /* hb all heartbeat tracking */
73b4e1b7e8STao Ma #define ML_HB_BIO	0x0000000000000010ULL /* hb io tracing */
74b4e1b7e8STao Ma #define ML_DLMFS	0x0000000000000020ULL /* dlm user dlmfs */
75b4e1b7e8STao Ma #define ML_DLM		0x0000000000000040ULL /* dlm general debugging */
76b4e1b7e8STao Ma #define ML_DLM_DOMAIN	0x0000000000000080ULL /* dlm domain debugging */
77b4e1b7e8STao Ma #define ML_DLM_THREAD	0x0000000000000100ULL /* dlm domain thread */
78b4e1b7e8STao Ma #define ML_DLM_MASTER	0x0000000000000200ULL /* dlm master functions */
79b4e1b7e8STao Ma #define ML_DLM_RECOVERY	0x0000000000000400ULL /* dlm master functions */
80b4e1b7e8STao Ma #define ML_DLM_GLUE	0x0000000000000800ULL /* ocfs2 dlm glue layer */
81b4e1b7e8STao Ma #define ML_VOTE		0x0000000000001000ULL /* ocfs2 node messaging  */
82b4e1b7e8STao Ma #define ML_CONN		0x0000000000002000ULL /* net connection management */
83b4e1b7e8STao Ma #define ML_QUORUM	0x0000000000004000ULL /* net connection quorum */
84b4e1b7e8STao Ma #define ML_BASTS	0x0000000000008000ULL /* dlmglue asts and basts */
85b4e1b7e8STao Ma #define ML_CLUSTER	0x0000000000010000ULL /* cluster stack */
8641b41a26SSunil Mushran 
8752fd3d6fSZach Brown /* bits that are infrequently given and frequently matched in the high word */
8841b41a26SSunil Mushran #define ML_ERROR	0x1000000000000000ULL /* sent to KERN_ERR */
8941b41a26SSunil Mushran #define ML_NOTICE	0x2000000000000000ULL /* setn to KERN_NOTICE */
9041b41a26SSunil Mushran #define ML_KTHREAD	0x4000000000000000ULL /* kernel thread activity */
9152fd3d6fSZach Brown 
9252fd3d6fSZach Brown #define MLOG_INITIAL_AND_MASK (ML_ERROR|ML_NOTICE)
9352fd3d6fSZach Brown #ifndef MLOG_MASK_PREFIX
9452fd3d6fSZach Brown #define MLOG_MASK_PREFIX 0
9552fd3d6fSZach Brown #endif
9652fd3d6fSZach Brown 
972b388c67SJoel Becker /*
982b388c67SJoel Becker  * When logging is disabled, force the bit test to 0 for anything other
992b388c67SJoel Becker  * than errors and notices, allowing gcc to remove the code completely.
1002b388c67SJoel Becker  * When enabled, allow all masks.
1012b388c67SJoel Becker  */
1022b388c67SJoel Becker #if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
1032b388c67SJoel Becker #define ML_ALLOWED_BITS ~0
1042b388c67SJoel Becker #else
1052b388c67SJoel Becker #define ML_ALLOWED_BITS (ML_ERROR|ML_NOTICE)
1062b388c67SJoel Becker #endif
1072b388c67SJoel Becker 
10852fd3d6fSZach Brown #define MLOG_MAX_BITS 64
10952fd3d6fSZach Brown 
11052fd3d6fSZach Brown struct mlog_bits {
11152fd3d6fSZach Brown 	unsigned long words[MLOG_MAX_BITS / BITS_PER_LONG];
11252fd3d6fSZach Brown };
11352fd3d6fSZach Brown 
11452fd3d6fSZach Brown extern struct mlog_bits mlog_and_bits, mlog_not_bits;
11552fd3d6fSZach Brown 
11652fd3d6fSZach Brown #if BITS_PER_LONG == 32
11752fd3d6fSZach Brown 
11852fd3d6fSZach Brown #define __mlog_test_u64(mask, bits)			\
11952fd3d6fSZach Brown 	( (u32)(mask & 0xffffffff) & bits.words[0] || 	\
12052fd3d6fSZach Brown 	  ((u64)(mask) >> 32) & bits.words[1] )
12152fd3d6fSZach Brown #define __mlog_set_u64(mask, bits) do {			\
12252fd3d6fSZach Brown 	bits.words[0] |= (u32)(mask & 0xffffffff);	\
12352fd3d6fSZach Brown        	bits.words[1] |= (u64)(mask) >> 32;		\
12452fd3d6fSZach Brown } while (0)
12552fd3d6fSZach Brown #define __mlog_clear_u64(mask, bits) do {		\
12652fd3d6fSZach Brown 	bits.words[0] &= ~((u32)(mask & 0xffffffff));	\
12752fd3d6fSZach Brown        	bits.words[1] &= ~((u64)(mask) >> 32);		\
12852fd3d6fSZach Brown } while (0)
12952fd3d6fSZach Brown #define MLOG_BITS_RHS(mask) {				\
13052fd3d6fSZach Brown 	{						\
13152fd3d6fSZach Brown 		[0] = (u32)(mask & 0xffffffff),		\
13252fd3d6fSZach Brown 		[1] = (u64)(mask) >> 32,		\
13352fd3d6fSZach Brown 	}						\
13452fd3d6fSZach Brown }
13552fd3d6fSZach Brown 
13652fd3d6fSZach Brown #else /* 32bit long above, 64bit long below */
13752fd3d6fSZach Brown 
13852fd3d6fSZach Brown #define __mlog_test_u64(mask, bits)	((mask) & bits.words[0])
13952fd3d6fSZach Brown #define __mlog_set_u64(mask, bits) do {		\
14052fd3d6fSZach Brown 	bits.words[0] |= (mask);		\
14152fd3d6fSZach Brown } while (0)
14252fd3d6fSZach Brown #define __mlog_clear_u64(mask, bits) do {	\
14352fd3d6fSZach Brown 	bits.words[0] &= ~(mask);		\
14452fd3d6fSZach Brown } while (0)
14552fd3d6fSZach Brown #define MLOG_BITS_RHS(mask) { { (mask) } }
14652fd3d6fSZach Brown 
14752fd3d6fSZach Brown #endif
14852fd3d6fSZach Brown 
1497c2bd2f9SJoe Perches __printf(4, 5)
1507c2bd2f9SJoe Perches void __mlog_printk(const u64 *m, const char *func, int line,
1517c2bd2f9SJoe Perches 		   const char *fmt, ...);
1527c2bd2f9SJoe Perches 
15352fd3d6fSZach Brown /*
1547c2bd2f9SJoe Perches  * Testing before the __mlog_printk call lets the compiler eliminate the
1557c2bd2f9SJoe Perches  * call completely when (m & ML_ALLOWED_BITS) is 0.
15652fd3d6fSZach Brown  */
1577c2bd2f9SJoe Perches #define mlog(mask, fmt, ...)						\
1587c2bd2f9SJoe Perches do {									\
1597c2bd2f9SJoe Perches 	u64 _m = MLOG_MASK_PREFIX | (mask);				\
1607c2bd2f9SJoe Perches 	if (_m & ML_ALLOWED_BITS)					\
1617c2bd2f9SJoe Perches 		__mlog_printk(&_m, __func__, __LINE__, fmt,		\
1627c2bd2f9SJoe Perches 			      ##__VA_ARGS__);				\
16352fd3d6fSZach Brown } while (0)
16452fd3d6fSZach Brown 
1655040f8dfSWengang Wang #define mlog_ratelimited(mask, fmt, ...)				\
1665040f8dfSWengang Wang do {									\
1675040f8dfSWengang Wang 	static DEFINE_RATELIMIT_STATE(_rs,				\
1685040f8dfSWengang Wang 				      DEFAULT_RATELIMIT_INTERVAL,	\
1695040f8dfSWengang Wang 				      DEFAULT_RATELIMIT_BURST);		\
1705040f8dfSWengang Wang 	if (__ratelimit(&_rs))						\
1715040f8dfSWengang Wang 		mlog(mask, fmt, ##__VA_ARGS__);				\
1725040f8dfSWengang Wang } while (0)
1735040f8dfSWengang Wang 
1741d5b8977SAndrew Morton #define mlog_errno(st) ({						\
17552fd3d6fSZach Brown 	int _st = (st);							\
17652fd3d6fSZach Brown 	if (_st != -ERESTARTSYS && _st != -EINTR &&			\
177910bffe0SJoel Becker 	    _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC &&		\
178910bffe0SJoel Becker 	    _st != -EDQUOT)						\
17952fd3d6fSZach Brown 		mlog(ML_ERROR, "status = %lld\n", (long long)_st);	\
1801d5b8977SAndrew Morton 	_st;								\
1811d5b8977SAndrew Morton })
18252fd3d6fSZach Brown 
18352fd3d6fSZach Brown #define mlog_bug_on_msg(cond, fmt, args...) do {			\
18452fd3d6fSZach Brown 	if (cond) {							\
18552fd3d6fSZach Brown 		mlog(ML_ERROR, "bug expression: " #cond "\n");		\
18652fd3d6fSZach Brown 		mlog(ML_ERROR, fmt, ##args);				\
18752fd3d6fSZach Brown 		BUG();							\
18852fd3d6fSZach Brown 	}								\
18952fd3d6fSZach Brown } while (0)
19052fd3d6fSZach Brown 
19152fd3d6fSZach Brown #include <linux/kobject.h>
19252fd3d6fSZach Brown #include <linux/sysfs.h>
193823bccfcSGreg Kroah-Hartman int mlog_sys_init(struct kset *o2cb_subsys);
19452fd3d6fSZach Brown void mlog_sys_shutdown(void);
19552fd3d6fSZach Brown 
19652fd3d6fSZach Brown #endif /* O2CLUSTER_MASKLOG_H */
197