2 * KLOG Generic Logging facility built upon the relayfs infrastructure
4 * Authors: Hubertus Franke (frankeh@us.ibm.com)
5 * Tom Zanussi (zanussi@us.ibm.com)
7 * Please direct all questions/comments to zanussi@us.ibm.com
9 * Copyright (C) 2003, IBM Corp
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 #include <linux/kernel.h>
18 #include <linux/smp_lock.h>
19 #include <linux/console.h>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/config.h>
23 #include <linux/delay.h>
24 #include <linux/smp.h>
25 #include <linux/sysctl.h>
26 #include <linux/relayfs_fs.h>
27 #include <linux/klog.h>
30 static int klog_channel = -1;
32 /* maximum size of klog formatting buffer beyond which truncation will occur */
33 #define KLOG_BUF_SIZE (512)
34 /* per-cpu klog formatting buffer */
35 static char buf[NR_CPUS][KLOG_BUF_SIZE];
38 * klog_enabled determines whether klog()/klog_raw() actually do write
39 * to the klog channel at any given time. If klog_enabled == 1 they do,
40 * otherwise they don't. Settable using sysctl fs.relayfs.klog_enabled.
42 #ifdef CONFIG_KLOG_CHANNEL_AUTOENABLE
43 static int klog_enabled = 1;
45 static int klog_enabled = 0;
49 * klog - write a formatted string into the klog channel
52 * Returns number of bytes written, negative number on failure.
54 int klog(const char *fmt, ...)
61 if (!klog_enabled || klog_channel < 0)
64 local_irq_save(flags);
65 cbuf = buf[smp_processor_id()];
68 len = vsnprintf(cbuf, KLOG_BUF_SIZE, fmt, args);
71 err = relay_write(klog_channel, cbuf, len, -1, NULL);
72 local_irq_restore(flags);
78 * klog_raw - directly write into the klog channel
79 * @buf: buffer containing data to write
80 * @len: # bytes to write
82 * Returns number of bytes written, negative number on failure.
84 int klog_raw(const char *buf,int len)
88 if (klog_enabled && klog_channel >= 0)
89 err = relay_write(klog_channel, buf, len, -1, NULL);
97 * Only sys/fs/relayfs/klog_enabled for now.
99 #define CTL_ENABLE_KLOG 100
100 #define CTL_RELAYFS 100
102 static struct ctl_table_header *relayfs_ctl_table_header;
104 static struct ctl_table relayfs_table[] =
107 .ctl_name = CTL_ENABLE_KLOG,
108 .procname = "klog_enabled",
109 .data = &klog_enabled,
110 .maxlen = sizeof(int),
112 .proc_handler = &proc_dointvec,
119 static struct ctl_table relayfs_dir_table[] =
122 .ctl_name = CTL_RELAYFS,
123 .procname = "relayfs",
127 .child = relayfs_table,
134 static struct ctl_table relayfs_root_table[] =
142 .child = relayfs_dir_table,
150 * create_klog_channel - creates channel /mnt/relay/klog
152 * Returns channel id on success, negative otherwise.
155 create_klog_channel(void)
160 channel_flags = RELAY_DELIVERY_PACKET | RELAY_USAGE_GLOBAL;
161 channel_flags |= RELAY_SCHEME_ANY | RELAY_TIMESTAMP_ANY;
163 bufsize = 1 << (CONFIG_KLOG_CHANNEL_SHIFT - 2);
166 klog_channel = relay_open("klog",
180 if (klog_channel < 0)
181 printk("klog channel creation failed, errcode: %d\n", klog_channel);
183 printk("klog channel created (%u bytes)\n", 1 << CONFIG_KLOG_CHANNEL_SHIFT);
184 relayfs_ctl_table_header = register_sysctl_table(relayfs_root_table, 1);
191 * remove_klog_channel - destroys channel /mnt/relay/klog
193 * Returns 0, negative otherwise.
196 remove_klog_channel(void)
198 if (relayfs_ctl_table_header)
199 unregister_sysctl_table(relayfs_ctl_table_header);
201 return relay_close(klog_channel);
205 EXPORT_SYMBOL(klog_raw);