masklog.h 7.2 KB

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