Merge tag 'disintegrate-mtd-20121009' of git://git.infradead.org/users/dhowells/linux...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / batman-adv / debugfs.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21
22#include <linux/debugfs.h>
23
b706b13b 24#include "debugfs.h"
c6c8fea2
SE
25#include "translation-table.h"
26#include "originator.h"
27#include "hard-interface.h"
28#include "gateway_common.h"
29#include "gateway_client.h"
30#include "soft-interface.h"
31#include "vis.h"
32#include "icmp_socket.h"
9bf8e4d4 33#include "bridge_loop_avoidance.h"
c6c8fea2 34
9e466250 35static struct dentry *batadv_debugfs;
c6c8fea2
SE
36
37#ifdef CONFIG_BATMAN_ADV_DEBUG
347c80f0 38#define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
c6c8fea2 39
a8a0a62d
SE
40static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
41
42static char *batadv_log_char_addr(struct batadv_debug_log *debug_log,
43 size_t idx)
44{
45 return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
46}
c6c8fea2 47
56303d34 48static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
c6c8fea2 49{
a8a0a62d
SE
50 char *char_addr;
51
52 char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
53 *char_addr = c;
c6c8fea2
SE
54 debug_log->log_end++;
55
9e466250
SE
56 if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
57 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
c6c8fea2
SE
58}
59
d3a547be 60__printf(2, 3)
56303d34
SE
61static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
62 const char *fmt, ...)
c6c8fea2 63{
c6c8fea2
SE
64 va_list args;
65 static char debug_log_buf[256];
66 char *p;
67
68 if (!debug_log)
69 return 0;
70
71 spin_lock_bh(&debug_log->lock);
72 va_start(args, fmt);
1299bdaa 73 vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
c6c8fea2
SE
74 va_end(args);
75
76 for (p = debug_log_buf; *p != 0; p++)
9e466250 77 batadv_emit_log_char(debug_log, *p);
c6c8fea2
SE
78
79 spin_unlock_bh(&debug_log->lock);
80
81 wake_up(&debug_log->queue_wait);
82
83 return 0;
84}
85
56303d34 86int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
c6c8fea2
SE
87{
88 va_list args;
89 char tmp_log_buf[256];
90
91 va_start(args, fmt);
92 vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
9e466250
SE
93 batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
94 jiffies_to_msecs(jiffies), tmp_log_buf);
c6c8fea2
SE
95 va_end(args);
96
97 return 0;
98}
99
9e466250 100static int batadv_log_open(struct inode *inode, struct file *file)
c6c8fea2
SE
101{
102 nonseekable_open(inode, file);
103 file->private_data = inode->i_private;
3193e8fd 104 batadv_inc_module_count();
c6c8fea2
SE
105 return 0;
106}
107
9e466250 108static int batadv_log_release(struct inode *inode, struct file *file)
c6c8fea2 109{
3193e8fd 110 batadv_dec_module_count();
c6c8fea2
SE
111 return 0;
112}
113
0aca2369
SE
114static int batadv_log_empty(struct batadv_debug_log *debug_log)
115{
116 return !(debug_log->log_start - debug_log->log_end);
117}
118
9e466250
SE
119static ssize_t batadv_log_read(struct file *file, char __user *buf,
120 size_t count, loff_t *ppos)
c6c8fea2 121{
56303d34
SE
122 struct batadv_priv *bat_priv = file->private_data;
123 struct batadv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2 124 int error, i = 0;
a8a0a62d 125 char *char_addr;
c6c8fea2
SE
126 char c;
127
0aca2369 128 if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
c6c8fea2
SE
129 return -EAGAIN;
130
16f14b45 131 if (!buf)
c6c8fea2
SE
132 return -EINVAL;
133
134 if (count == 0)
135 return 0;
136
137 if (!access_ok(VERIFY_WRITE, buf, count))
138 return -EFAULT;
139
140 error = wait_event_interruptible(debug_log->queue_wait,
0aca2369 141 (!batadv_log_empty(debug_log)));
c6c8fea2
SE
142
143 if (error)
144 return error;
145
146 spin_lock_bh(&debug_log->lock);
147
148 while ((!error) && (i < count) &&
149 (debug_log->log_start != debug_log->log_end)) {
a8a0a62d
SE
150 char_addr = batadv_log_char_addr(debug_log,
151 debug_log->log_start);
152 c = *char_addr;
c6c8fea2
SE
153
154 debug_log->log_start++;
155
156 spin_unlock_bh(&debug_log->lock);
157
158 error = __put_user(c, buf);
159
160 spin_lock_bh(&debug_log->lock);
161
162 buf++;
163 i++;
164
165 }
166
167 spin_unlock_bh(&debug_log->lock);
168
169 if (!error)
170 return i;
171
172 return error;
173}
174
9e466250 175static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
c6c8fea2 176{
56303d34
SE
177 struct batadv_priv *bat_priv = file->private_data;
178 struct batadv_debug_log *debug_log = bat_priv->debug_log;
c6c8fea2
SE
179
180 poll_wait(file, &debug_log->queue_wait, wait);
181
0aca2369 182 if (!batadv_log_empty(debug_log))
c6c8fea2
SE
183 return POLLIN | POLLRDNORM;
184
185 return 0;
186}
187
9e466250
SE
188static const struct file_operations batadv_log_fops = {
189 .open = batadv_log_open,
190 .release = batadv_log_release,
191 .read = batadv_log_read,
192 .poll = batadv_log_poll,
c6c8fea2
SE
193 .llseek = no_llseek,
194};
195
56303d34 196static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
197{
198 struct dentry *d;
199
200 if (!bat_priv->debug_dir)
201 goto err;
202
704509b8 203 bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
c6c8fea2
SE
204 if (!bat_priv->debug_log)
205 goto err;
206
207 spin_lock_init(&bat_priv->debug_log->lock);
208 init_waitqueue_head(&bat_priv->debug_log->queue_wait);
209
210 d = debugfs_create_file("log", S_IFREG | S_IRUSR,
9e466250
SE
211 bat_priv->debug_dir, bat_priv,
212 &batadv_log_fops);
5346c35e 213 if (!d)
c6c8fea2
SE
214 goto err;
215
216 return 0;
217
218err:
5346c35e 219 return -ENOMEM;
c6c8fea2
SE
220}
221
56303d34 222static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
223{
224 kfree(bat_priv->debug_log);
225 bat_priv->debug_log = NULL;
226}
227#else /* CONFIG_BATMAN_ADV_DEBUG */
56303d34 228static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
c6c8fea2
SE
229{
230 bat_priv->debug_log = NULL;
231 return 0;
232}
233
56303d34 234static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
c6c8fea2
SE
235{
236 return;
237}
238#endif
239
9e466250 240static int batadv_algorithms_open(struct inode *inode, struct file *file)
1c280471 241{
3193e8fd 242 return single_open(file, batadv_algo_seq_print_text, NULL);
1c280471
ML
243}
244
9e466250 245static int batadv_originators_open(struct inode *inode, struct file *file)
c6c8fea2
SE
246{
247 struct net_device *net_dev = (struct net_device *)inode->i_private;
7d211efc 248 return single_open(file, batadv_orig_seq_print_text, net_dev);
c6c8fea2
SE
249}
250
9e466250 251static int batadv_gateways_open(struct inode *inode, struct file *file)
c6c8fea2
SE
252{
253 struct net_device *net_dev = (struct net_device *)inode->i_private;
7cf06bc6 254 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
c6c8fea2
SE
255}
256
9e466250 257static int batadv_transtable_global_open(struct inode *inode, struct file *file)
c6c8fea2
SE
258{
259 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 260 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
c6c8fea2
SE
261}
262
7a5cc242 263#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 264static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
9bf8e4d4
SW
265{
266 struct net_device *net_dev = (struct net_device *)inode->i_private;
08adf151
SE
267 return single_open(file, batadv_bla_claim_table_seq_print_text,
268 net_dev);
9bf8e4d4 269}
536a23f1
SW
270
271static int batadv_bla_backbone_table_open(struct inode *inode,
272 struct file *file)
273{
274 struct net_device *net_dev = (struct net_device *)inode->i_private;
275 return single_open(file, batadv_bla_backbone_table_seq_print_text,
276 net_dev);
277}
278
7a5cc242 279#endif
9bf8e4d4 280
9e466250 281static int batadv_transtable_local_open(struct inode *inode, struct file *file)
c6c8fea2
SE
282{
283 struct net_device *net_dev = (struct net_device *)inode->i_private;
08c36d3e 284 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
c6c8fea2
SE
285}
286
9e466250 287static int batadv_vis_data_open(struct inode *inode, struct file *file)
c6c8fea2
SE
288{
289 struct net_device *net_dev = (struct net_device *)inode->i_private;
d0f714f4 290 return single_open(file, batadv_vis_seq_print_text, net_dev);
c6c8fea2
SE
291}
292
7f223c0c 293struct batadv_debuginfo {
c6c8fea2
SE
294 struct attribute attr;
295 const struct file_operations fops;
296};
297
347c80f0 298#define BATADV_DEBUGINFO(_name, _mode, _open) \
7f223c0c 299struct batadv_debuginfo batadv_debuginfo_##_name = { \
9e466250
SE
300 .attr = { .name = __stringify(_name), \
301 .mode = _mode, }, \
302 .fops = { .owner = THIS_MODULE, \
303 .open = _open, \
304 .read = seq_read, \
305 .llseek = seq_lseek, \
306 .release = single_release, \
307 } \
c6c8fea2
SE
308};
309
347c80f0
SE
310static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
311static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
312static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
313static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
314 batadv_transtable_global_open);
7a5cc242 315#ifdef CONFIG_BATMAN_ADV_BLA
347c80f0 316static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
536a23f1
SW
317static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
318 batadv_bla_backbone_table_open);
7a5cc242 319#endif
347c80f0
SE
320static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
321 batadv_transtable_local_open);
322static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
c6c8fea2 323
7f223c0c 324static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
9e466250
SE
325 &batadv_debuginfo_originators,
326 &batadv_debuginfo_gateways,
327 &batadv_debuginfo_transtable_global,
7a5cc242 328#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 329 &batadv_debuginfo_bla_claim_table,
536a23f1 330 &batadv_debuginfo_bla_backbone_table,
7a5cc242 331#endif
9e466250
SE
332 &batadv_debuginfo_transtable_local,
333 &batadv_debuginfo_vis_data,
c6c8fea2
SE
334 NULL,
335};
336
40a072d7 337void batadv_debugfs_init(void)
c6c8fea2 338{
7f223c0c 339 struct batadv_debuginfo *bat_debug;
1c280471
ML
340 struct dentry *file;
341
54590e4d 342 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
9e466250
SE
343 if (batadv_debugfs == ERR_PTR(-ENODEV))
344 batadv_debugfs = NULL;
1c280471 345
9e466250 346 if (!batadv_debugfs)
1c280471
ML
347 goto out;
348
9e466250 349 bat_debug = &batadv_debuginfo_routing_algos;
1c280471
ML
350 file = debugfs_create_file(bat_debug->attr.name,
351 S_IFREG | bat_debug->attr.mode,
9e466250 352 batadv_debugfs, NULL, &bat_debug->fops);
1c280471
ML
353 if (!file)
354 pr_err("Can't add debugfs file: %s\n", bat_debug->attr.name);
355
356out:
357 return;
c6c8fea2
SE
358}
359
40a072d7 360void batadv_debugfs_destroy(void)
c6c8fea2 361{
9e466250
SE
362 if (batadv_debugfs) {
363 debugfs_remove_recursive(batadv_debugfs);
364 batadv_debugfs = NULL;
c6c8fea2
SE
365 }
366}
367
40a072d7 368int batadv_debugfs_add_meshif(struct net_device *dev)
c6c8fea2 369{
56303d34 370 struct batadv_priv *bat_priv = netdev_priv(dev);
7f223c0c 371 struct batadv_debuginfo **bat_debug;
c6c8fea2
SE
372 struct dentry *file;
373
9e466250 374 if (!batadv_debugfs)
c6c8fea2
SE
375 goto out;
376
9e466250 377 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
c6c8fea2
SE
378 if (!bat_priv->debug_dir)
379 goto out;
380
9039dc7e 381 if (batadv_socket_setup(bat_priv) < 0)
5346c35e
SE
382 goto rem_attr;
383
9e466250 384 if (batadv_debug_log_setup(bat_priv) < 0)
5346c35e 385 goto rem_attr;
c6c8fea2 386
9e466250 387 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
c6c8fea2 388 file = debugfs_create_file(((*bat_debug)->attr).name,
0aca2369
SE
389 S_IFREG | ((*bat_debug)->attr).mode,
390 bat_priv->debug_dir,
391 dev, &(*bat_debug)->fops);
c6c8fea2 392 if (!file) {
3e34819e
SE
393 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
394 dev->name, ((*bat_debug)->attr).name);
c6c8fea2
SE
395 goto rem_attr;
396 }
397 }
398
399 return 0;
400rem_attr:
401 debugfs_remove_recursive(bat_priv->debug_dir);
402 bat_priv->debug_dir = NULL;
403out:
404#ifdef CONFIG_DEBUG_FS
405 return -ENOMEM;
406#else
407 return 0;
408#endif /* CONFIG_DEBUG_FS */
409}
410
40a072d7 411void batadv_debugfs_del_meshif(struct net_device *dev)
c6c8fea2 412{
56303d34 413 struct batadv_priv *bat_priv = netdev_priv(dev);
c6c8fea2 414
9e466250 415 batadv_debug_log_cleanup(bat_priv);
c6c8fea2 416
9e466250 417 if (batadv_debugfs) {
c6c8fea2
SE
418 debugfs_remove_recursive(bat_priv->debug_dir);
419 bat_priv->debug_dir = NULL;
420 }
421}