Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (C) 2005 Oracle.  All rights reserved.
0004  */
0005 
0006 #ifndef O2CLUSTER_MASKLOG_H
0007 #define O2CLUSTER_MASKLOG_H
0008 
0009 /*
0010  * For now this is a trivial wrapper around printk() that gives the critical
0011  * ability to enable sets of debugging output at run-time.  In the future this
0012  * will almost certainly be redirected to relayfs so that it can pay a
0013  * substantially lower heisenberg tax.
0014  *
0015  * Callers associate the message with a bitmask and a global bitmask is
0016  * maintained with help from /proc.  If any of the bits match the message is
0017  * output.
0018  *
0019  * We must have efficient bit tests on i386 and it seems gcc still emits crazy
0020  * code for the 64bit compare.  It emits very good code for the dual unsigned
0021  * long tests, though, completely avoiding tests that can never pass if the
0022  * caller gives a constant bitmask that fills one of the longs with all 0s.  So
0023  * the desire is to have almost all of the calls decided on by comparing just
0024  * one of the longs.  This leads to having infrequently given bits that are
0025  * frequently matched in the high bits.
0026  *
0027  * _ERROR and _NOTICE are used for messages that always go to the console and
0028  * have appropriate KERN_ prefixes.  We wrap these in our function instead of
0029  * just calling printk() so that this can eventually make its way through
0030  * relayfs along with the debugging messages.  Everything else gets KERN_DEBUG.
0031  * The inline tests and macro dance give GCC the opportunity to quite cleverly
0032  * only emit the appropriage printk() when the caller passes in a constant
0033  * mask, as is almost always the case.
0034  *
0035  * All this bitmask nonsense is managed from the files under
0036  * /sys/fs/o2cb/logmask/.  Reading the files gives a straightforward
0037  * indication of which bits are allowed (allow) or denied (off/deny).
0038  *  ENTRY deny
0039  *  EXIT deny
0040  *  TCP off
0041  *  MSG off
0042  *  SOCKET off
0043  *  ERROR allow
0044  *  NOTICE allow
0045  *
0046  * Writing changes the state of a given bit and requires a strictly formatted
0047  * single write() call:
0048  *
0049  *  write(fd, "allow", 5);
0050  *
0051  * Echoing allow/deny/off string into the logmask files can flip the bits
0052  * on or off as expected; here is the bash script for example:
0053  *
0054  * log_mask="/sys/fs/o2cb/log_mask"
0055  * for node in ENTRY EXIT TCP MSG SOCKET ERROR NOTICE; do
0056  *  echo allow >"$log_mask"/"$node"
0057  * done
0058  *
0059  * The debugfs.ocfs2 tool can also flip the bits with the -l option:
0060  *
0061  * debugfs.ocfs2 -l TCP allow
0062  */
0063 
0064 /* for task_struct */
0065 #include <linux/sched.h>
0066 
0067 /* bits that are frequently given and infrequently matched in the low word */
0068 /* NOTE: If you add a flag, you need to also update masklog.c! */
0069 #define ML_TCP      0x0000000000000001ULL /* net cluster/tcp.c */
0070 #define ML_MSG      0x0000000000000002ULL /* net network messages */
0071 #define ML_SOCKET   0x0000000000000004ULL /* net socket lifetime */
0072 #define ML_HEARTBEAT    0x0000000000000008ULL /* hb all heartbeat tracking */
0073 #define ML_HB_BIO   0x0000000000000010ULL /* hb io tracing */
0074 #define ML_DLMFS    0x0000000000000020ULL /* dlm user dlmfs */
0075 #define ML_DLM      0x0000000000000040ULL /* dlm general debugging */
0076 #define ML_DLM_DOMAIN   0x0000000000000080ULL /* dlm domain debugging */
0077 #define ML_DLM_THREAD   0x0000000000000100ULL /* dlm domain thread */
0078 #define ML_DLM_MASTER   0x0000000000000200ULL /* dlm master functions */
0079 #define ML_DLM_RECOVERY 0x0000000000000400ULL /* dlm master functions */
0080 #define ML_DLM_GLUE 0x0000000000000800ULL /* ocfs2 dlm glue layer */
0081 #define ML_VOTE     0x0000000000001000ULL /* ocfs2 node messaging  */
0082 #define ML_CONN     0x0000000000002000ULL /* net connection management */
0083 #define ML_QUORUM   0x0000000000004000ULL /* net connection quorum */
0084 #define ML_BASTS    0x0000000000008000ULL /* dlmglue asts and basts */
0085 #define ML_CLUSTER  0x0000000000010000ULL /* cluster stack */
0086 
0087 /* bits that are infrequently given and frequently matched in the high word */
0088 #define ML_ERROR    0x1000000000000000ULL /* sent to KERN_ERR */
0089 #define ML_NOTICE   0x2000000000000000ULL /* setn to KERN_NOTICE */
0090 #define ML_KTHREAD  0x4000000000000000ULL /* kernel thread activity */
0091 
0092 #define MLOG_INITIAL_AND_MASK (ML_ERROR|ML_NOTICE)
0093 #ifndef MLOG_MASK_PREFIX
0094 #define MLOG_MASK_PREFIX 0
0095 #endif
0096 
0097 /*
0098  * When logging is disabled, force the bit test to 0 for anything other
0099  * than errors and notices, allowing gcc to remove the code completely.
0100  * When enabled, allow all masks.
0101  */
0102 #if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
0103 #define ML_ALLOWED_BITS ~0
0104 #else
0105 #define ML_ALLOWED_BITS (ML_ERROR|ML_NOTICE)
0106 #endif
0107 
0108 #define MLOG_MAX_BITS 64
0109 
0110 struct mlog_bits {
0111     unsigned long words[MLOG_MAX_BITS / BITS_PER_LONG];
0112 };
0113 
0114 extern struct mlog_bits mlog_and_bits, mlog_not_bits;
0115 
0116 #if BITS_PER_LONG == 32
0117 
0118 #define __mlog_test_u64(mask, bits)         \
0119     ( (u32)(mask & 0xffffffff) & bits.words[0] ||   \
0120       ((u64)(mask) >> 32) & bits.words[1] )
0121 #define __mlog_set_u64(mask, bits) do {         \
0122     bits.words[0] |= (u32)(mask & 0xffffffff);  \
0123         bits.words[1] |= (u64)(mask) >> 32;     \
0124 } while (0)
0125 #define __mlog_clear_u64(mask, bits) do {       \
0126     bits.words[0] &= ~((u32)(mask & 0xffffffff));   \
0127         bits.words[1] &= ~((u64)(mask) >> 32);      \
0128 } while (0)
0129 #define MLOG_BITS_RHS(mask) {               \
0130     {                       \
0131         [0] = (u32)(mask & 0xffffffff),     \
0132         [1] = (u64)(mask) >> 32,        \
0133     }                       \
0134 }
0135 
0136 #else /* 32bit long above, 64bit long below */
0137 
0138 #define __mlog_test_u64(mask, bits) ((mask) & bits.words[0])
0139 #define __mlog_set_u64(mask, bits) do {     \
0140     bits.words[0] |= (mask);        \
0141 } while (0)
0142 #define __mlog_clear_u64(mask, bits) do {   \
0143     bits.words[0] &= ~(mask);       \
0144 } while (0)
0145 #define MLOG_BITS_RHS(mask) { { (mask) } }
0146 
0147 #endif
0148 
0149 __printf(4, 5)
0150 void __mlog_printk(const u64 *m, const char *func, int line,
0151            const char *fmt, ...);
0152 
0153 /*
0154  * Testing before the __mlog_printk call lets the compiler eliminate the
0155  * call completely when (m & ML_ALLOWED_BITS) is 0.
0156  */
0157 #define mlog(mask, fmt, ...)                        \
0158 do {                                    \
0159     u64 _m = MLOG_MASK_PREFIX | (mask);             \
0160     if (_m & ML_ALLOWED_BITS)                   \
0161         __mlog_printk(&_m, __func__, __LINE__, fmt,     \
0162                   ##__VA_ARGS__);               \
0163 } while (0)
0164 
0165 #define mlog_ratelimited(mask, fmt, ...)                \
0166 do {                                    \
0167     static DEFINE_RATELIMIT_STATE(_rs,              \
0168                       DEFAULT_RATELIMIT_INTERVAL,   \
0169                       DEFAULT_RATELIMIT_BURST);     \
0170     if (__ratelimit(&_rs))                      \
0171         mlog(mask, fmt, ##__VA_ARGS__);             \
0172 } while (0)
0173 
0174 #define mlog_errno(st) ({                       \
0175     int _st = (st);                         \
0176     if (_st != -ERESTARTSYS && _st != -EINTR &&         \
0177         _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC &&      \
0178         _st != -EDQUOT)                     \
0179         mlog(ML_ERROR, "status = %lld\n", (long long)_st);  \
0180     _st;                                \
0181 })
0182 
0183 #define mlog_bug_on_msg(cond, fmt, args...) do {            \
0184     if (cond) {                         \
0185         mlog(ML_ERROR, "bug expression: " #cond "\n");      \
0186         mlog(ML_ERROR, fmt, ##args);                \
0187         BUG();                          \
0188     }                               \
0189 } while (0)
0190 
0191 #include <linux/kobject.h>
0192 #include <linux/sysfs.h>
0193 int mlog_sys_init(struct kset *o2cb_subsys);
0194 void mlog_sys_shutdown(void);
0195 
0196 #endif /* O2CLUSTER_MASKLOG_H */