Merge branch 'v6v7' into devel
[linux/fpc-iii.git] / net / batman-adv / bat_debugfs.c
blob0ae81d07f1027d720e9b69df9e34fa6b2cbba449
1 /*
2 * Copyright (C) 2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
22 #include "main.h"
24 #include <linux/debugfs.h>
26 #include "bat_debugfs.h"
27 #include "translation-table.h"
28 #include "originator.h"
29 #include "hard-interface.h"
30 #include "gateway_common.h"
31 #include "gateway_client.h"
32 #include "soft-interface.h"
33 #include "vis.h"
34 #include "icmp_socket.h"
36 static struct dentry *bat_debugfs;
38 #ifdef CONFIG_BATMAN_ADV_DEBUG
39 #define LOG_BUFF_MASK (log_buff_len-1)
40 #define LOG_BUFF(idx) (debug_log->log_buff[(idx) & LOG_BUFF_MASK])
42 static int log_buff_len = LOG_BUF_LEN;
44 static void emit_log_char(struct debug_log *debug_log, char c)
46 LOG_BUFF(debug_log->log_end) = c;
47 debug_log->log_end++;
49 if (debug_log->log_end - debug_log->log_start > log_buff_len)
50 debug_log->log_start = debug_log->log_end - log_buff_len;
53 static int fdebug_log(struct debug_log *debug_log, char *fmt, ...)
55 int printed_len;
56 va_list args;
57 static char debug_log_buf[256];
58 char *p;
60 if (!debug_log)
61 return 0;
63 spin_lock_bh(&debug_log->lock);
64 va_start(args, fmt);
65 printed_len = vscnprintf(debug_log_buf, sizeof(debug_log_buf),
66 fmt, args);
67 va_end(args);
69 for (p = debug_log_buf; *p != 0; p++)
70 emit_log_char(debug_log, *p);
72 spin_unlock_bh(&debug_log->lock);
74 wake_up(&debug_log->queue_wait);
76 return 0;
79 int debug_log(struct bat_priv *bat_priv, char *fmt, ...)
81 va_list args;
82 char tmp_log_buf[256];
84 va_start(args, fmt);
85 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
86 fdebug_log(bat_priv->debug_log, "[%10u] %s",
87 (jiffies / HZ), tmp_log_buf);
88 va_end(args);
90 return 0;
93 static int log_open(struct inode *inode, struct file *file)
95 nonseekable_open(inode, file);
96 file->private_data = inode->i_private;
97 inc_module_count();
98 return 0;
101 static int log_release(struct inode *inode, struct file *file)
103 dec_module_count();
104 return 0;
107 static ssize_t log_read(struct file *file, char __user *buf,
108 size_t count, loff_t *ppos)
110 struct bat_priv *bat_priv = file->private_data;
111 struct debug_log *debug_log = bat_priv->debug_log;
112 int error, i = 0;
113 char c;
115 if ((file->f_flags & O_NONBLOCK) &&
116 !(debug_log->log_end - debug_log->log_start))
117 return -EAGAIN;
119 if ((!buf) || (count < 0))
120 return -EINVAL;
122 if (count == 0)
123 return 0;
125 if (!access_ok(VERIFY_WRITE, buf, count))
126 return -EFAULT;
128 error = wait_event_interruptible(debug_log->queue_wait,
129 (debug_log->log_start - debug_log->log_end));
131 if (error)
132 return error;
134 spin_lock_bh(&debug_log->lock);
136 while ((!error) && (i < count) &&
137 (debug_log->log_start != debug_log->log_end)) {
138 c = LOG_BUFF(debug_log->log_start);
140 debug_log->log_start++;
142 spin_unlock_bh(&debug_log->lock);
144 error = __put_user(c, buf);
146 spin_lock_bh(&debug_log->lock);
148 buf++;
149 i++;
153 spin_unlock_bh(&debug_log->lock);
155 if (!error)
156 return i;
158 return error;
161 static unsigned int log_poll(struct file *file, poll_table *wait)
163 struct bat_priv *bat_priv = file->private_data;
164 struct debug_log *debug_log = bat_priv->debug_log;
166 poll_wait(file, &debug_log->queue_wait, wait);
168 if (debug_log->log_end - debug_log->log_start)
169 return POLLIN | POLLRDNORM;
171 return 0;
174 static const struct file_operations log_fops = {
175 .open = log_open,
176 .release = log_release,
177 .read = log_read,
178 .poll = log_poll,
179 .llseek = no_llseek,
182 static int debug_log_setup(struct bat_priv *bat_priv)
184 struct dentry *d;
186 if (!bat_priv->debug_dir)
187 goto err;
189 bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
190 if (!bat_priv->debug_log)
191 goto err;
193 spin_lock_init(&bat_priv->debug_log->lock);
194 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
196 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
197 bat_priv->debug_dir, bat_priv, &log_fops);
198 if (d)
199 goto err;
201 return 0;
203 err:
204 return 1;
207 static void debug_log_cleanup(struct bat_priv *bat_priv)
209 kfree(bat_priv->debug_log);
210 bat_priv->debug_log = NULL;
212 #else /* CONFIG_BATMAN_ADV_DEBUG */
213 static int debug_log_setup(struct bat_priv *bat_priv)
215 bat_priv->debug_log = NULL;
216 return 0;
219 static void debug_log_cleanup(struct bat_priv *bat_priv)
221 return;
223 #endif
225 static int originators_open(struct inode *inode, struct file *file)
227 struct net_device *net_dev = (struct net_device *)inode->i_private;
228 return single_open(file, orig_seq_print_text, net_dev);
231 static int gateways_open(struct inode *inode, struct file *file)
233 struct net_device *net_dev = (struct net_device *)inode->i_private;
234 return single_open(file, gw_client_seq_print_text, net_dev);
237 static int softif_neigh_open(struct inode *inode, struct file *file)
239 struct net_device *net_dev = (struct net_device *)inode->i_private;
240 return single_open(file, softif_neigh_seq_print_text, net_dev);
243 static int transtable_global_open(struct inode *inode, struct file *file)
245 struct net_device *net_dev = (struct net_device *)inode->i_private;
246 return single_open(file, hna_global_seq_print_text, net_dev);
249 static int transtable_local_open(struct inode *inode, struct file *file)
251 struct net_device *net_dev = (struct net_device *)inode->i_private;
252 return single_open(file, hna_local_seq_print_text, net_dev);
255 static int vis_data_open(struct inode *inode, struct file *file)
257 struct net_device *net_dev = (struct net_device *)inode->i_private;
258 return single_open(file, vis_seq_print_text, net_dev);
261 struct bat_debuginfo {
262 struct attribute attr;
263 const struct file_operations fops;
266 #define BAT_DEBUGINFO(_name, _mode, _open) \
267 struct bat_debuginfo bat_debuginfo_##_name = { \
268 .attr = { .name = __stringify(_name), \
269 .mode = _mode, }, \
270 .fops = { .owner = THIS_MODULE, \
271 .open = _open, \
272 .read = seq_read, \
273 .llseek = seq_lseek, \
274 .release = single_release, \
278 static BAT_DEBUGINFO(originators, S_IRUGO, originators_open);
279 static BAT_DEBUGINFO(gateways, S_IRUGO, gateways_open);
280 static BAT_DEBUGINFO(softif_neigh, S_IRUGO, softif_neigh_open);
281 static BAT_DEBUGINFO(transtable_global, S_IRUGO, transtable_global_open);
282 static BAT_DEBUGINFO(transtable_local, S_IRUGO, transtable_local_open);
283 static BAT_DEBUGINFO(vis_data, S_IRUGO, vis_data_open);
285 static struct bat_debuginfo *mesh_debuginfos[] = {
286 &bat_debuginfo_originators,
287 &bat_debuginfo_gateways,
288 &bat_debuginfo_softif_neigh,
289 &bat_debuginfo_transtable_global,
290 &bat_debuginfo_transtable_local,
291 &bat_debuginfo_vis_data,
292 NULL,
295 void debugfs_init(void)
297 bat_debugfs = debugfs_create_dir(DEBUGFS_BAT_SUBDIR, NULL);
298 if (bat_debugfs == ERR_PTR(-ENODEV))
299 bat_debugfs = NULL;
302 void debugfs_destroy(void)
304 if (bat_debugfs) {
305 debugfs_remove_recursive(bat_debugfs);
306 bat_debugfs = NULL;
310 int debugfs_add_meshif(struct net_device *dev)
312 struct bat_priv *bat_priv = netdev_priv(dev);
313 struct bat_debuginfo **bat_debug;
314 struct dentry *file;
316 if (!bat_debugfs)
317 goto out;
319 bat_priv->debug_dir = debugfs_create_dir(dev->name, bat_debugfs);
320 if (!bat_priv->debug_dir)
321 goto out;
323 bat_socket_setup(bat_priv);
324 debug_log_setup(bat_priv);
326 for (bat_debug = mesh_debuginfos; *bat_debug; ++bat_debug) {
327 file = debugfs_create_file(((*bat_debug)->attr).name,
328 S_IFREG | ((*bat_debug)->attr).mode,
329 bat_priv->debug_dir,
330 dev, &(*bat_debug)->fops);
331 if (!file) {
332 bat_err(dev, "Can't add debugfs file: %s/%s\n",
333 dev->name, ((*bat_debug)->attr).name);
334 goto rem_attr;
338 return 0;
339 rem_attr:
340 debugfs_remove_recursive(bat_priv->debug_dir);
341 bat_priv->debug_dir = NULL;
342 out:
343 #ifdef CONFIG_DEBUG_FS
344 return -ENOMEM;
345 #else
346 return 0;
347 #endif /* CONFIG_DEBUG_FS */
350 void debugfs_del_meshif(struct net_device *dev)
352 struct bat_priv *bat_priv = netdev_priv(dev);
354 debug_log_cleanup(bat_priv);
356 if (bat_debugfs) {
357 debugfs_remove_recursive(bat_priv->debug_dir);
358 bat_priv->debug_dir = NULL;