masklog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 hidden from the /proc interface so that Joel
  50. * doesn't have an aneurism. Reading the file gives a straight forward
  51. * indication of which bits are on or off:
  52. * ENTRY off
  53. * EXIT off
  54. * TCP off
  55. * MSG off
  56. * SOCKET off
  57. * ERROR off
  58. * NOTICE on
  59. *
  60. * Writing changes the state of a given bit and requires a strictly formatted
  61. * single write() call:
  62. *
  63. * write(fd, "ENTRY on", 8);
  64. *
  65. * would turn the entry bit on. "1" is also accepted in the place of "on", and
  66. * "off" and "0" behave as expected.
  67. *
  68. * Some trivial shell can flip all the bits on or off:
  69. *
  70. * log_mask="/proc/fs/ocfs2_nodemanager/log_mask"
  71. * cat $log_mask | (
  72. * while read bit status; do
  73. * # $1 is "on" or "off", say
  74. * echo "$bit $1" > $log_mask
  75. * done
  76. * )
  77. */
  78. /* for task_struct */
  79. #include <linux/sched.h>
  80. /* bits that are frequently given and infrequently matched in the low word */
  81. /* NOTE: If you add a flag, you need to also update mlog.c! */
  82. #define ML_ENTRY 0x0000000000000001ULL /* func call entry */
  83. #define ML_EXIT 0x0000000000000002ULL /* func call exit */
  84. #define ML_TCP 0x0000000000000004ULL /* net cluster/tcp.c */
  85. #define ML_MSG 0x0000000000000008ULL /* net network messages */
  86. #define ML_SOCKET 0x0000000000000010ULL /* net socket lifetime */
  87. #define ML_HEARTBEAT 0x0000000000000020ULL /* hb all heartbeat tracking */
  88. #define ML_HB_BIO 0x0000000000000040ULL /* hb io tracing */
  89. #define ML_DLMFS 0x0000000000000080ULL /* dlm user dlmfs */
  90. #define ML_DLM 0x0000000000000100ULL /* dlm general debugging */
  91. #define ML_DLM_DOMAIN 0x0000000000000200ULL /* dlm domain debugging */
  92. #define ML_DLM_THREAD 0x0000000000000400ULL /* dlm domain thread */
  93. #define ML_DLM_MASTER 0x0000000000000800ULL /* dlm master functions */
  94. #define ML_DLM_RECOVERY 0x0000000000001000ULL /* dlm master functions */
  95. #define ML_AIO 0x0000000000002000ULL /* ocfs2 aio read and write */
  96. #define ML_JOURNAL 0x0000000000004000ULL /* ocfs2 journalling functions */
  97. #define ML_DISK_ALLOC 0x0000000000008000ULL /* ocfs2 disk allocation */
  98. #define ML_SUPER 0x0000000000010000ULL /* ocfs2 mount / umount */
  99. #define ML_FILE_IO 0x0000000000020000ULL /* ocfs2 file I/O */
  100. #define ML_EXTENT_MAP 0x0000000000040000ULL /* ocfs2 extent map caching */
  101. #define ML_DLM_GLUE 0x0000000000080000ULL /* ocfs2 dlm glue layer */
  102. #define ML_BH_IO 0x0000000000100000ULL /* ocfs2 buffer I/O */
  103. #define ML_UPTODATE 0x0000000000200000ULL /* ocfs2 caching sequence #'s */
  104. #define ML_NAMEI 0x0000000000400000ULL /* ocfs2 directory / namespace */
  105. #define ML_INODE 0x0000000000800000ULL /* ocfs2 inode manipulation */
  106. #define ML_VOTE 0x0000000001000000ULL /* ocfs2 node messaging */
  107. #define ML_DCACHE 0x0000000002000000ULL /* ocfs2 dcache operations */
  108. #define ML_CONN 0x0000000004000000ULL /* net connection management */
  109. #define ML_QUORUM 0x0000000008000000ULL /* net connection quorum */
  110. #define ML_EXPORT 0x0000000010000000ULL /* ocfs2 export operations */
  111. #define ML_XATTR 0x0000000020000000ULL /* ocfs2 extended attributes */
  112. #define ML_QUOTA 0x0000000040000000ULL /* ocfs2 quota operations */
  113. /* bits that are infrequently given and frequently matched in the high word */
  114. #define ML_ERROR 0x0000000100000000ULL /* sent to KERN_ERR */
  115. #define ML_NOTICE 0x0000000200000000ULL /* setn to KERN_NOTICE */
  116. #define ML_KTHREAD 0x0000000400000000ULL /* kernel thread activity */
  117. #define MLOG_INITIAL_AND_MASK (ML_ERROR|ML_NOTICE)
  118. #define MLOG_INITIAL_NOT_MASK (ML_ENTRY|ML_EXIT)
  119. #ifndef MLOG_MASK_PREFIX
  120. #define MLOG_MASK_PREFIX 0
  121. #endif
  122. /*
  123. * When logging is disabled, force the bit test to 0 for anything other
  124. * than errors and notices, allowing gcc to remove the code completely.
  125. * When enabled, allow all masks.
  126. */
  127. #if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
  128. #define ML_ALLOWED_BITS ~0
  129. #else
  130. #define ML_ALLOWED_BITS (ML_ERROR|ML_NOTICE)
  131. #endif
  132. #define MLOG_MAX_BITS 64
  133. struct mlog_bits {
  134. unsigned long words[MLOG_MAX_BITS / BITS_PER_LONG];
  135. };
  136. extern struct mlog_bits mlog_and_bits, mlog_not_bits;
  137. #if BITS_PER_LONG == 32
  138. #define __mlog_test_u64(mask, bits) \
  139. ( (u32)(mask & 0xffffffff) & bits.words[0] || \
  140. ((u64)(mask) >> 32) & bits.words[1] )
  141. #define __mlog_set_u64(mask, bits) do { \
  142. bits.words[0] |= (u32)(mask & 0xffffffff); \
  143. bits.words[1] |= (u64)(mask) >> 32; \
  144. } while (0)
  145. #define __mlog_clear_u64(mask, bits) do { \
  146. bits.words[0] &= ~((u32)(mask & 0xffffffff)); \
  147. bits.words[1] &= ~((u64)(mask) >> 32); \
  148. } while (0)
  149. #define MLOG_BITS_RHS(mask) { \
  150. { \
  151. [0] = (u32)(mask & 0xffffffff), \
  152. [1] = (u64)(mask) >> 32, \
  153. } \
  154. }
  155. #else /* 32bit long above, 64bit long below */
  156. #define __mlog_test_u64(mask, bits) ((mask) & bits.words[0])
  157. #define __mlog_set_u64(mask, bits) do { \
  158. bits.words[0] |= (mask); \
  159. } while (0)
  160. #define __mlog_clear_u64(mask, bits) do { \
  161. bits.words[0] &= ~(mask); \
  162. } while (0)
  163. #define MLOG_BITS_RHS(mask) { { (mask) } }
  164. #endif
  165. /*
  166. * smp_processor_id() "helpfully" screams when called outside preemptible
  167. * regions in current kernels. sles doesn't have the variants that don't
  168. * scream. just do this instead of trying to guess which we're building
  169. * against.. *sigh*.
  170. */
  171. #define __mlog_cpu_guess ({ \
  172. unsigned long _cpu = get_cpu(); \
  173. put_cpu(); \
  174. _cpu; \
  175. })
  176. /* In the following two macros, the whitespace after the ',' just
  177. * before ##args is intentional. Otherwise, gcc 2.95 will eat the
  178. * previous token if args expands to nothing.
  179. */
  180. #define __mlog_printk(level, fmt, args...) \
  181. printk(level "(%u,%lu):%s:%d " fmt, task_pid_nr(current), \
  182. __mlog_cpu_guess, __PRETTY_FUNCTION__, __LINE__ , \
  183. ##args)
  184. #define mlog(mask, fmt, args...) do { \
  185. u64 __m = MLOG_MASK_PREFIX | (mask); \
  186. if ((__m & ML_ALLOWED_BITS) && \
  187. __mlog_test_u64(__m, mlog_and_bits) && \
  188. !__mlog_test_u64(__m, mlog_not_bits)) { \
  189. if (__m & ML_ERROR) \
  190. __mlog_printk(KERN_ERR, "ERROR: "fmt , ##args); \
  191. else if (__m & ML_NOTICE) \
  192. __mlog_printk(KERN_NOTICE, fmt , ##args); \
  193. else __mlog_printk(KERN_INFO, fmt , ##args); \
  194. } \
  195. } while (0)
  196. #define mlog_errno(st) do { \
  197. int _st = (st); \
  198. if (_st != -ERESTARTSYS && _st != -EINTR && \
  199. _st != AOP_TRUNCATED_PAGE && _st != -ENOSPC) \
  200. mlog(ML_ERROR, "status = %lld\n", (long long)_st); \
  201. } while (0)
  202. #if defined(CONFIG_OCFS2_DEBUG_MASKLOG)
  203. #define mlog_entry(fmt, args...) do { \
  204. mlog(ML_ENTRY, "ENTRY:" fmt , ##args); \
  205. } while (0)
  206. #define mlog_entry_void() do { \
  207. mlog(ML_ENTRY, "ENTRY:\n"); \
  208. } while (0)
  209. /*
  210. * We disable this for sparse.
  211. */
  212. #if !defined(__CHECKER__)
  213. #define mlog_exit(st) do { \
  214. if (__builtin_types_compatible_p(typeof(st), unsigned long)) \
  215. mlog(ML_EXIT, "EXIT: %lu\n", (unsigned long) (st)); \
  216. else if (__builtin_types_compatible_p(typeof(st), signed long)) \
  217. mlog(ML_EXIT, "EXIT: %ld\n", (signed long) (st)); \
  218. else if (__builtin_types_compatible_p(typeof(st), unsigned int) \
  219. || __builtin_types_compatible_p(typeof(st), unsigned short) \
  220. || __builtin_types_compatible_p(typeof(st), unsigned char)) \
  221. mlog(ML_EXIT, "EXIT: %u\n", (unsigned int) (st)); \
  222. else if (__builtin_types_compatible_p(typeof(st), signed int) \
  223. || __builtin_types_compatible_p(typeof(st), signed short) \
  224. || __builtin_types_compatible_p(typeof(st), signed char)) \
  225. mlog(ML_EXIT, "EXIT: %d\n", (signed int) (st)); \
  226. else if (__builtin_types_compatible_p(typeof(st), long long)) \
  227. mlog(ML_EXIT, "EXIT: %lld\n", (long long) (st)); \
  228. else \
  229. mlog(ML_EXIT, "EXIT: %llu\n", (unsigned long long) (st)); \
  230. } while (0)
  231. #else
  232. #define mlog_exit(st) do { \
  233. mlog(ML_EXIT, "EXIT: %lld\n", (long long) (st)); \
  234. } while (0)
  235. #endif
  236. #define mlog_exit_ptr(ptr) do { \
  237. mlog(ML_EXIT, "EXIT: %p\n", ptr); \
  238. } while (0)
  239. #define mlog_exit_void() do { \
  240. mlog(ML_EXIT, "EXIT\n"); \
  241. } while (0)
  242. #else
  243. #define mlog_entry(...) do { } while (0)
  244. #define mlog_entry_void(...) do { } while (0)
  245. #define mlog_exit(...) do { } while (0)
  246. #define mlog_exit_ptr(...) do { } while (0)
  247. #define mlog_exit_void(...) do { } while (0)
  248. #endif /* defined(CONFIG_OCFS2_DEBUG_MASKLOG) */
  249. #define mlog_bug_on_msg(cond, fmt, args...) do { \
  250. if (cond) { \
  251. mlog(ML_ERROR, "bug expression: " #cond "\n"); \
  252. mlog(ML_ERROR, fmt, ##args); \
  253. BUG(); \
  254. } \
  255. } while (0)
  256. #include <linux/kobject.h>
  257. #include <linux/sysfs.h>
  258. int mlog_sys_init(struct kset *o2cb_subsys);
  259. void mlog_sys_shutdown(void);
  260. #endif /* O2CLUSTER_MASKLOG_H */