masklog.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. /*
  150. * smp_processor_id() "helpfully" screams when called outside preemptible
  151. * regions in current kernels. sles doesn't have the variants that don't
  152. * scream. just do this instead of trying to guess which we're building
  153. * against.. *sigh*.
  154. */
  155. #define __mlog_cpu_guess ({ \
  156. unsigned long _cpu = get_cpu(); \
  157. put_cpu(); \
  158. _cpu; \
  159. })
  160. /* In the following two macros, the whitespace after the ',' just
  161. * before ##args is intentional. Otherwise, gcc 2.95 will eat the
  162. * previous token if args expands to nothing.
  163. */
  164. #define __mlog_printk(level, fmt, args...) \
  165. printk(level "(%s,%u,%lu):%s:%d " fmt, current->comm, \
  166. task_pid_nr(current), __mlog_cpu_guess, \
  167. __PRETTY_FUNCTION__, __LINE__ , ##args)
  168. #define mlog(mask, fmt, args...) do { \
  169. u64 __m = MLOG_MASK_PREFIX | (mask); \
  170. if ((__m & ML_ALLOWED_BITS) && \
  171. __mlog_test_u64(__m, mlog_and_bits) && \
  172. !__mlog_test_u64(__m, mlog_not_bits)) { \
  173. if (__m & ML_ERROR) \
  174. __mlog_printk(KERN_ERR, "ERROR: "fmt , ##args); \
  175. else if (__m & ML_NOTICE) \
  176. __mlog_printk(KERN_NOTICE, fmt , ##args); \
  177. else __mlog_printk(KERN_INFO, fmt , ##args); \
  178. } \
  179. } while (0)
  180. #define mlog_errno(st) do { \
  181. int _st = (st); \
  182. if (_st != -ERESTARTSYS && _st != -EINTR && \
  183. _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC) \
  184. mlog(ML_ERROR, "status = %lld\n", (long long)_st); \
  185. } while (0)
  186. #define mlog_bug_on_msg(cond, fmt, args...) do { \
  187. if (cond) { \
  188. mlog(ML_ERROR, "bug expression: " #cond "\n"); \
  189. mlog(ML_ERROR, fmt, ##args); \
  190. BUG(); \
  191. } \
  192. } while (0)
  193. #include <linux/kobject.h>
  194. #include <linux/sysfs.h>
  195. int mlog_sys_init(struct kset *o2cb_subsys);
  196. void mlog_sys_shutdown(void);
  197. #endif /* O2CLUSTER_MASKLOG_H */