^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0-or-later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* -*- mode: c; c-basic-offset: 8; -*-
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vim: noexpandtab sw=8 ts=8 sts=0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005 Oracle. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #ifndef O2CLUSTER_MASKLOG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define O2CLUSTER_MASKLOG_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * For now this is a trivial wrapper around printk() that gives the critical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * ability to enable sets of debugging output at run-time. In the future this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * will almost certainly be redirected to relayfs so that it can pay a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * substantially lower heisenberg tax.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Callers associate the message with a bitmask and a global bitmask is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * maintained with help from /proc. If any of the bits match the message is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * We must have efficient bit tests on i386 and it seems gcc still emits crazy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * code for the 64bit compare. It emits very good code for the dual unsigned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * long tests, though, completely avoiding tests that can never pass if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * caller gives a constant bitmask that fills one of the longs with all 0s. So
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * the desire is to have almost all of the calls decided on by comparing just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * one of the longs. This leads to having infrequently given bits that are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * frequently matched in the high bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * _ERROR and _NOTICE are used for messages that always go to the console and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * have appropriate KERN_ prefixes. We wrap these in our function instead of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * just calling printk() so that this can eventually make its way through
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * relayfs along with the debugging messages. Everything else gets KERN_DEBUG.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * The inline tests and macro dance give GCC the opportunity to quite cleverly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * only emit the appropriage printk() when the caller passes in a constant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * mask, as is almost always the case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * All this bitmask nonsense is managed from the files under
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * /sys/fs/o2cb/logmask/. Reading the files gives a straightforward
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * indication of which bits are allowed (allow) or denied (off/deny).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * ENTRY deny
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) * EXIT deny
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * TCP off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * MSG off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * SOCKET off
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * ERROR allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * NOTICE allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Writing changes the state of a given bit and requires a strictly formatted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * single write() call:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * write(fd, "allow", 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Echoing allow/deny/off string into the logmask files can flip the bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * on or off as expected; here is the bash script for example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * log_mask="/sys/fs/o2cb/log_mask"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * for node in ENTRY EXIT TCP MSG SOCKET ERROR NOTICE; do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * echo allow >"$log_mask"/"$node"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * The debugfs.ocfs2 tool can also flip the bits with the -l option:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * debugfs.ocfs2 -l TCP allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* for task_struct */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /* bits that are frequently given and infrequently matched in the low word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* NOTE: If you add a flag, you need to also update masklog.c! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define ML_TCP 0x0000000000000001ULL /* net cluster/tcp.c */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define ML_MSG 0x0000000000000002ULL /* net network messages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define ML_SOCKET 0x0000000000000004ULL /* net socket lifetime */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define ML_HEARTBEAT 0x0000000000000008ULL /* hb all heartbeat tracking */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define ML_HB_BIO 0x0000000000000010ULL /* hb io tracing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define ML_DLMFS 0x0000000000000020ULL /* dlm user dlmfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define ML_DLM 0x0000000000000040ULL /* dlm general debugging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define ML_DLM_DOMAIN 0x0000000000000080ULL /* dlm domain debugging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define ML_DLM_THREAD 0x0000000000000100ULL /* dlm domain thread */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define ML_DLM_MASTER 0x0000000000000200ULL /* dlm master functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define ML_DLM_RECOVERY 0x0000000000000400ULL /* dlm master functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define ML_DLM_GLUE 0x0000000000000800ULL /* ocfs2 dlm glue layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define ML_VOTE 0x0000000000001000ULL /* ocfs2 node messaging */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define ML_CONN 0x0000000000002000ULL /* net connection management */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define ML_QUORUM 0x0000000000004000ULL /* net connection quorum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define ML_BASTS 0x0000000000008000ULL /* dlmglue asts and basts */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define ML_CLUSTER 0x0000000000010000ULL /* cluster stack */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* bits that are infrequently given and frequently matched in the high word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #define ML_ERROR 0x1000000000000000ULL /* sent to KERN_ERR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #define ML_NOTICE 0x2000000000000000ULL /* setn to KERN_NOTICE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #define ML_KTHREAD 0x4000000000000000ULL /* kernel thread activity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define MLOG_INITIAL_AND_MASK (ML_ERROR|ML_NOTICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #ifndef MLOG_MASK_PREFIX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define MLOG_MASK_PREFIX 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * When logging is disabled, force the bit test to 0 for anything other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * than errors and notices, allowing gcc to remove the code completely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * When enabled, allow all masks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define ML_ALLOWED_BITS ~0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define ML_ALLOWED_BITS (ML_ERROR|ML_NOTICE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define MLOG_MAX_BITS 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct mlog_bits {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) unsigned long words[MLOG_MAX_BITS / BITS_PER_LONG];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) extern struct mlog_bits mlog_and_bits, mlog_not_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #if BITS_PER_LONG == 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #define __mlog_test_u64(mask, bits) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) ( (u32)(mask & 0xffffffff) & bits.words[0] || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ((u64)(mask) >> 32) & bits.words[1] )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #define __mlog_set_u64(mask, bits) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) bits.words[0] |= (u32)(mask & 0xffffffff); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) bits.words[1] |= (u64)(mask) >> 32; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) #define __mlog_clear_u64(mask, bits) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bits.words[0] &= ~((u32)(mask & 0xffffffff)); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) bits.words[1] &= ~((u64)(mask) >> 32); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #define MLOG_BITS_RHS(mask) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) [0] = (u32)(mask & 0xffffffff), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) [1] = (u64)(mask) >> 32, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #else /* 32bit long above, 64bit long below */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) #define __mlog_test_u64(mask, bits) ((mask) & bits.words[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) #define __mlog_set_u64(mask, bits) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) bits.words[0] |= (mask); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define __mlog_clear_u64(mask, bits) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) bits.words[0] &= ~(mask); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define MLOG_BITS_RHS(mask) { { (mask) } }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) __printf(4, 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) void __mlog_printk(const u64 *m, const char *func, int line,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const char *fmt, ...);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * Testing before the __mlog_printk call lets the compiler eliminate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * call completely when (m & ML_ALLOWED_BITS) is 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define mlog(mask, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) u64 _m = MLOG_MASK_PREFIX | (mask); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (_m & ML_ALLOWED_BITS) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) __mlog_printk(&_m, __func__, __LINE__, fmt, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #define mlog_ratelimited(mask, fmt, ...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static DEFINE_RATELIMIT_STATE(_rs, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) DEFAULT_RATELIMIT_INTERVAL, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) DEFAULT_RATELIMIT_BURST); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (__ratelimit(&_rs)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) mlog(mask, fmt, ##__VA_ARGS__); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) #define mlog_errno(st) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int _st = (st); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if (_st != -ERESTARTSYS && _st != -EINTR && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC && \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) _st != -EDQUOT) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) mlog(ML_ERROR, "status = %lld\n", (long long)_st); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) _st; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) #define mlog_bug_on_msg(cond, fmt, args...) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (cond) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) mlog(ML_ERROR, "bug expression: " #cond "\n"); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) mlog(ML_ERROR, fmt, ##args); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) BUG(); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int mlog_sys_init(struct kset *o2cb_subsys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) void mlog_sys_shutdown(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #endif /* O2CLUSTER_MASKLOG_H */