masklog.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * Copyright (C) 2004, 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. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/string.h>
  26. #include <asm/uaccess.h>
  27. #include "masklog.h"
  28. struct mlog_bits mlog_and_bits = MLOG_BITS_RHS(MLOG_INITIAL_AND_MASK);
  29. EXPORT_SYMBOL_GPL(mlog_and_bits);
  30. struct mlog_bits mlog_not_bits = MLOG_BITS_RHS(0);
  31. EXPORT_SYMBOL_GPL(mlog_not_bits);
  32. static ssize_t mlog_mask_show(u64 mask, char *buf)
  33. {
  34. char *state;
  35. if (__mlog_test_u64(mask, mlog_and_bits))
  36. state = "allow";
  37. else if (__mlog_test_u64(mask, mlog_not_bits))
  38. state = "deny";
  39. else
  40. state = "off";
  41. return snprintf(buf, PAGE_SIZE, "%s\n", state);
  42. }
  43. static ssize_t mlog_mask_store(u64 mask, const char *buf, size_t count)
  44. {
  45. if (!strncasecmp(buf, "allow", 5)) {
  46. __mlog_set_u64(mask, mlog_and_bits);
  47. __mlog_clear_u64(mask, mlog_not_bits);
  48. } else if (!strncasecmp(buf, "deny", 4)) {
  49. __mlog_set_u64(mask, mlog_not_bits);
  50. __mlog_clear_u64(mask, mlog_and_bits);
  51. } else if (!strncasecmp(buf, "off", 3)) {
  52. __mlog_clear_u64(mask, mlog_not_bits);
  53. __mlog_clear_u64(mask, mlog_and_bits);
  54. } else
  55. return -EINVAL;
  56. return count;
  57. }
  58. void __mlog_printk(const u64 *mask, const char *func, int line,
  59. const char *fmt, ...)
  60. {
  61. struct va_format vaf;
  62. va_list args;
  63. const char *level;
  64. const char *prefix = "";
  65. if (!__mlog_test_u64(*mask, mlog_and_bits) ||
  66. __mlog_test_u64(*mask, mlog_not_bits))
  67. return;
  68. if (*mask & ML_ERROR) {
  69. level = KERN_ERR;
  70. prefix = "ERROR: ";
  71. } else if (*mask & ML_NOTICE) {
  72. level = KERN_NOTICE;
  73. } else {
  74. level = KERN_INFO;
  75. }
  76. va_start(args, fmt);
  77. vaf.fmt = fmt;
  78. vaf.va = &args;
  79. printk("%s(%s,%u,%u):%s:%d %s%pV",
  80. level, current->comm, task_pid_nr(current),
  81. raw_smp_processor_id(), func, line, prefix, &vaf);
  82. va_end(args);
  83. }
  84. EXPORT_SYMBOL_GPL(__mlog_printk);
  85. struct mlog_attribute {
  86. struct attribute attr;
  87. u64 mask;
  88. };
  89. #define to_mlog_attr(_attr) container_of(_attr, struct mlog_attribute, attr)
  90. #define define_mask(_name) { \
  91. .attr = { \
  92. .name = #_name, \
  93. .mode = S_IRUGO | S_IWUSR, \
  94. }, \
  95. .mask = ML_##_name, \
  96. }
  97. static struct mlog_attribute mlog_attrs[MLOG_MAX_BITS] = {
  98. define_mask(TCP),
  99. define_mask(MSG),
  100. define_mask(SOCKET),
  101. define_mask(HEARTBEAT),
  102. define_mask(HB_BIO),
  103. define_mask(DLMFS),
  104. define_mask(DLM),
  105. define_mask(DLM_DOMAIN),
  106. define_mask(DLM_THREAD),
  107. define_mask(DLM_MASTER),
  108. define_mask(DLM_RECOVERY),
  109. define_mask(DLM_GLUE),
  110. define_mask(VOTE),
  111. define_mask(CONN),
  112. define_mask(QUORUM),
  113. define_mask(BASTS),
  114. define_mask(CLUSTER),
  115. define_mask(ERROR),
  116. define_mask(NOTICE),
  117. define_mask(KTHREAD),
  118. };
  119. static struct attribute *mlog_attr_ptrs[MLOG_MAX_BITS] = {NULL, };
  120. static ssize_t mlog_show(struct kobject *obj, struct attribute *attr,
  121. char *buf)
  122. {
  123. struct mlog_attribute *mlog_attr = to_mlog_attr(attr);
  124. return mlog_mask_show(mlog_attr->mask, buf);
  125. }
  126. static ssize_t mlog_store(struct kobject *obj, struct attribute *attr,
  127. const char *buf, size_t count)
  128. {
  129. struct mlog_attribute *mlog_attr = to_mlog_attr(attr);
  130. return mlog_mask_store(mlog_attr->mask, buf, count);
  131. }
  132. static const struct sysfs_ops mlog_attr_ops = {
  133. .show = mlog_show,
  134. .store = mlog_store,
  135. };
  136. static struct kobj_type mlog_ktype = {
  137. .default_attrs = mlog_attr_ptrs,
  138. .sysfs_ops = &mlog_attr_ops,
  139. };
  140. static struct kset mlog_kset = {
  141. .kobj = {.ktype = &mlog_ktype},
  142. };
  143. int mlog_sys_init(struct kset *o2cb_kset)
  144. {
  145. int i = 0;
  146. while (mlog_attrs[i].attr.mode) {
  147. mlog_attr_ptrs[i] = &mlog_attrs[i].attr;
  148. i++;
  149. }
  150. mlog_attr_ptrs[i] = NULL;
  151. kobject_set_name(&mlog_kset.kobj, "logmask");
  152. mlog_kset.kobj.kset = o2cb_kset;
  153. return kset_register(&mlog_kset);
  154. }
  155. void mlog_sys_shutdown(void)
  156. {
  157. kset_unregister(&mlog_kset);
  158. }