Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2004, 2005 Oracle.  All rights reserved.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/kernel.h>
0008 #include <linux/proc_fs.h>
0009 #include <linux/seq_file.h>
0010 #include <linux/string.h>
0011 #include <linux/uaccess.h>
0012 
0013 #include "masklog.h"
0014 
0015 struct mlog_bits mlog_and_bits = MLOG_BITS_RHS(MLOG_INITIAL_AND_MASK);
0016 EXPORT_SYMBOL_GPL(mlog_and_bits);
0017 struct mlog_bits mlog_not_bits = MLOG_BITS_RHS(0);
0018 EXPORT_SYMBOL_GPL(mlog_not_bits);
0019 
0020 static ssize_t mlog_mask_show(u64 mask, char *buf)
0021 {
0022     char *state;
0023 
0024     if (__mlog_test_u64(mask, mlog_and_bits))
0025         state = "allow";
0026     else if (__mlog_test_u64(mask, mlog_not_bits))
0027         state = "deny";
0028     else
0029         state = "off";
0030 
0031     return snprintf(buf, PAGE_SIZE, "%s\n", state);
0032 }
0033 
0034 static ssize_t mlog_mask_store(u64 mask, const char *buf, size_t count)
0035 {
0036     if (!strncasecmp(buf, "allow", 5)) {
0037         __mlog_set_u64(mask, mlog_and_bits);
0038         __mlog_clear_u64(mask, mlog_not_bits);
0039     } else if (!strncasecmp(buf, "deny", 4)) {
0040         __mlog_set_u64(mask, mlog_not_bits);
0041         __mlog_clear_u64(mask, mlog_and_bits);
0042     } else if (!strncasecmp(buf, "off", 3)) {
0043         __mlog_clear_u64(mask, mlog_not_bits);
0044         __mlog_clear_u64(mask, mlog_and_bits);
0045     } else
0046         return -EINVAL;
0047 
0048     return count;
0049 }
0050 
0051 void __mlog_printk(const u64 *mask, const char *func, int line,
0052            const char *fmt, ...)
0053 {
0054     struct va_format vaf;
0055     va_list args;
0056     const char *level;
0057     const char *prefix = "";
0058 
0059     if (!__mlog_test_u64(*mask, mlog_and_bits) ||
0060         __mlog_test_u64(*mask, mlog_not_bits))
0061         return;
0062 
0063     if (*mask & ML_ERROR) {
0064         level = KERN_ERR;
0065         prefix = "ERROR: ";
0066     } else if (*mask & ML_NOTICE) {
0067         level = KERN_NOTICE;
0068     } else {
0069         level = KERN_INFO;
0070     }
0071 
0072     va_start(args, fmt);
0073 
0074     vaf.fmt = fmt;
0075     vaf.va = &args;
0076 
0077     printk("%s(%s,%u,%u):%s:%d %s%pV",
0078            level, current->comm, task_pid_nr(current),
0079            raw_smp_processor_id(), func, line, prefix, &vaf);
0080 
0081     va_end(args);
0082 }
0083 EXPORT_SYMBOL_GPL(__mlog_printk);
0084 
0085 struct mlog_attribute {
0086     struct attribute attr;
0087     u64 mask;
0088 };
0089 
0090 #define to_mlog_attr(_attr) container_of(_attr, struct mlog_attribute, attr)
0091 
0092 #define define_mask(_name) {            \
0093     .attr = {               \
0094         .name = #_name,         \
0095         .mode = S_IRUGO | S_IWUSR,  \
0096     },                  \
0097     .mask = ML_##_name,         \
0098 }
0099 
0100 static struct mlog_attribute mlog_attrs[MLOG_MAX_BITS] = {
0101     define_mask(TCP),
0102     define_mask(MSG),
0103     define_mask(SOCKET),
0104     define_mask(HEARTBEAT),
0105     define_mask(HB_BIO),
0106     define_mask(DLMFS),
0107     define_mask(DLM),
0108     define_mask(DLM_DOMAIN),
0109     define_mask(DLM_THREAD),
0110     define_mask(DLM_MASTER),
0111     define_mask(DLM_RECOVERY),
0112     define_mask(DLM_GLUE),
0113     define_mask(VOTE),
0114     define_mask(CONN),
0115     define_mask(QUORUM),
0116     define_mask(BASTS),
0117     define_mask(CLUSTER),
0118     define_mask(ERROR),
0119     define_mask(NOTICE),
0120     define_mask(KTHREAD),
0121 };
0122 
0123 static struct attribute *mlog_default_attrs[MLOG_MAX_BITS] = {NULL, };
0124 ATTRIBUTE_GROUPS(mlog_default);
0125 
0126 static ssize_t mlog_show(struct kobject *obj, struct attribute *attr,
0127              char *buf)
0128 {
0129     struct mlog_attribute *mlog_attr = to_mlog_attr(attr);
0130 
0131     return mlog_mask_show(mlog_attr->mask, buf);
0132 }
0133 
0134 static ssize_t mlog_store(struct kobject *obj, struct attribute *attr,
0135               const char *buf, size_t count)
0136 {
0137     struct mlog_attribute *mlog_attr = to_mlog_attr(attr);
0138 
0139     return mlog_mask_store(mlog_attr->mask, buf, count);
0140 }
0141 
0142 static const struct sysfs_ops mlog_attr_ops = {
0143     .show  = mlog_show,
0144     .store = mlog_store,
0145 };
0146 
0147 static struct kobj_type mlog_ktype = {
0148     .default_groups = mlog_default_groups,
0149     .sysfs_ops      = &mlog_attr_ops,
0150 };
0151 
0152 static struct kset mlog_kset = {
0153     .kobj   = {.ktype = &mlog_ktype},
0154 };
0155 
0156 int mlog_sys_init(struct kset *o2cb_kset)
0157 {
0158     int i = 0;
0159 
0160     while (mlog_attrs[i].attr.mode) {
0161         mlog_default_attrs[i] = &mlog_attrs[i].attr;
0162         i++;
0163     }
0164     mlog_default_attrs[i] = NULL;
0165 
0166     kobject_set_name(&mlog_kset.kobj, "logmask");
0167     mlog_kset.kobj.kset = o2cb_kset;
0168     return kset_register(&mlog_kset);
0169 }
0170 
0171 void mlog_sys_shutdown(void)
0172 {
0173     kset_unregister(&mlog_kset);
0174 }