masklog.h 6.7 KB

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