]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - net/batman-adv/debugfs.c
Merge branch 'akpm' (Andrew's patch-bomb)
[can-eth-gw-linux.git] / net / batman-adv / debugfs.c
1 /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
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
18  */
19
20 #include "main.h"
21
22 #include <linux/debugfs.h>
23
24 #include "debugfs.h"
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"
33 #include "bridge_loop_avoidance.h"
34 #include "distributed-arp-table.h"
35
36 static struct dentry *batadv_debugfs;
37
38 #ifdef CONFIG_BATMAN_ADV_DEBUG
39 #define BATADV_LOG_BUFF_MASK (batadv_log_buff_len - 1)
40
41 static const int batadv_log_buff_len = BATADV_LOG_BUF_LEN;
42
43 static char *batadv_log_char_addr(struct batadv_debug_log *debug_log,
44                                   size_t idx)
45 {
46         return &debug_log->log_buff[idx & BATADV_LOG_BUFF_MASK];
47 }
48
49 static void batadv_emit_log_char(struct batadv_debug_log *debug_log, char c)
50 {
51         char *char_addr;
52
53         char_addr = batadv_log_char_addr(debug_log, debug_log->log_end);
54         *char_addr = c;
55         debug_log->log_end++;
56
57         if (debug_log->log_end - debug_log->log_start > batadv_log_buff_len)
58                 debug_log->log_start = debug_log->log_end - batadv_log_buff_len;
59 }
60
61 __printf(2, 3)
62 static int batadv_fdebug_log(struct batadv_debug_log *debug_log,
63                              const char *fmt, ...)
64 {
65         va_list args;
66         static char debug_log_buf[256];
67         char *p;
68
69         if (!debug_log)
70                 return 0;
71
72         spin_lock_bh(&debug_log->lock);
73         va_start(args, fmt);
74         vscnprintf(debug_log_buf, sizeof(debug_log_buf), fmt, args);
75         va_end(args);
76
77         for (p = debug_log_buf; *p != 0; p++)
78                 batadv_emit_log_char(debug_log, *p);
79
80         spin_unlock_bh(&debug_log->lock);
81
82         wake_up(&debug_log->queue_wait);
83
84         return 0;
85 }
86
87 int batadv_debug_log(struct batadv_priv *bat_priv, const char *fmt, ...)
88 {
89         va_list args;
90         char tmp_log_buf[256];
91
92         va_start(args, fmt);
93         vscnprintf(tmp_log_buf, sizeof(tmp_log_buf), fmt, args);
94         batadv_fdebug_log(bat_priv->debug_log, "[%10u] %s",
95                           jiffies_to_msecs(jiffies), tmp_log_buf);
96         va_end(args);
97
98         return 0;
99 }
100
101 static int batadv_log_open(struct inode *inode, struct file *file)
102 {
103         if (!try_module_get(THIS_MODULE))
104                 return -EBUSY;
105
106         nonseekable_open(inode, file);
107         file->private_data = inode->i_private;
108         return 0;
109 }
110
111 static int batadv_log_release(struct inode *inode, struct file *file)
112 {
113         module_put(THIS_MODULE);
114         return 0;
115 }
116
117 static int batadv_log_empty(struct batadv_debug_log *debug_log)
118 {
119         return !(debug_log->log_start - debug_log->log_end);
120 }
121
122 static ssize_t batadv_log_read(struct file *file, char __user *buf,
123                                size_t count, loff_t *ppos)
124 {
125         struct batadv_priv *bat_priv = file->private_data;
126         struct batadv_debug_log *debug_log = bat_priv->debug_log;
127         int error, i = 0;
128         char *char_addr;
129         char c;
130
131         if ((file->f_flags & O_NONBLOCK) && batadv_log_empty(debug_log))
132                 return -EAGAIN;
133
134         if (!buf)
135                 return -EINVAL;
136
137         if (count == 0)
138                 return 0;
139
140         if (!access_ok(VERIFY_WRITE, buf, count))
141                 return -EFAULT;
142
143         error = wait_event_interruptible(debug_log->queue_wait,
144                                          (!batadv_log_empty(debug_log)));
145
146         if (error)
147                 return error;
148
149         spin_lock_bh(&debug_log->lock);
150
151         while ((!error) && (i < count) &&
152                (debug_log->log_start != debug_log->log_end)) {
153                 char_addr = batadv_log_char_addr(debug_log,
154                                                  debug_log->log_start);
155                 c = *char_addr;
156
157                 debug_log->log_start++;
158
159                 spin_unlock_bh(&debug_log->lock);
160
161                 error = __put_user(c, buf);
162
163                 spin_lock_bh(&debug_log->lock);
164
165                 buf++;
166                 i++;
167
168         }
169
170         spin_unlock_bh(&debug_log->lock);
171
172         if (!error)
173                 return i;
174
175         return error;
176 }
177
178 static unsigned int batadv_log_poll(struct file *file, poll_table *wait)
179 {
180         struct batadv_priv *bat_priv = file->private_data;
181         struct batadv_debug_log *debug_log = bat_priv->debug_log;
182
183         poll_wait(file, &debug_log->queue_wait, wait);
184
185         if (!batadv_log_empty(debug_log))
186                 return POLLIN | POLLRDNORM;
187
188         return 0;
189 }
190
191 static const struct file_operations batadv_log_fops = {
192         .open           = batadv_log_open,
193         .release        = batadv_log_release,
194         .read           = batadv_log_read,
195         .poll           = batadv_log_poll,
196         .llseek         = no_llseek,
197 };
198
199 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
200 {
201         struct dentry *d;
202
203         if (!bat_priv->debug_dir)
204                 goto err;
205
206         bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
207         if (!bat_priv->debug_log)
208                 goto err;
209
210         spin_lock_init(&bat_priv->debug_log->lock);
211         init_waitqueue_head(&bat_priv->debug_log->queue_wait);
212
213         d = debugfs_create_file("log", S_IFREG | S_IRUSR,
214                                 bat_priv->debug_dir, bat_priv,
215                                 &batadv_log_fops);
216         if (!d)
217                 goto err;
218
219         return 0;
220
221 err:
222         return -ENOMEM;
223 }
224
225 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
226 {
227         kfree(bat_priv->debug_log);
228         bat_priv->debug_log = NULL;
229 }
230 #else /* CONFIG_BATMAN_ADV_DEBUG */
231 static int batadv_debug_log_setup(struct batadv_priv *bat_priv)
232 {
233         bat_priv->debug_log = NULL;
234         return 0;
235 }
236
237 static void batadv_debug_log_cleanup(struct batadv_priv *bat_priv)
238 {
239         return;
240 }
241 #endif
242
243 static int batadv_algorithms_open(struct inode *inode, struct file *file)
244 {
245         return single_open(file, batadv_algo_seq_print_text, NULL);
246 }
247
248 static int batadv_originators_open(struct inode *inode, struct file *file)
249 {
250         struct net_device *net_dev = (struct net_device *)inode->i_private;
251         return single_open(file, batadv_orig_seq_print_text, net_dev);
252 }
253
254 static int batadv_gateways_open(struct inode *inode, struct file *file)
255 {
256         struct net_device *net_dev = (struct net_device *)inode->i_private;
257         return single_open(file, batadv_gw_client_seq_print_text, net_dev);
258 }
259
260 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
261 {
262         struct net_device *net_dev = (struct net_device *)inode->i_private;
263         return single_open(file, batadv_tt_global_seq_print_text, net_dev);
264 }
265
266 #ifdef CONFIG_BATMAN_ADV_BLA
267 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
268 {
269         struct net_device *net_dev = (struct net_device *)inode->i_private;
270         return single_open(file, batadv_bla_claim_table_seq_print_text,
271                            net_dev);
272 }
273
274 static int batadv_bla_backbone_table_open(struct inode *inode,
275                                           struct file *file)
276 {
277         struct net_device *net_dev = (struct net_device *)inode->i_private;
278         return single_open(file, batadv_bla_backbone_table_seq_print_text,
279                            net_dev);
280 }
281
282 #endif
283
284 #ifdef CONFIG_BATMAN_ADV_DAT
285 /**
286  * batadv_dat_cache_open - Prepare file handler for reads from dat_chache
287  * @inode: inode which was opened
288  * @file: file handle to be initialized
289  */
290 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
291 {
292         struct net_device *net_dev = (struct net_device *)inode->i_private;
293         return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
294 }
295 #endif
296
297 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
298 {
299         struct net_device *net_dev = (struct net_device *)inode->i_private;
300         return single_open(file, batadv_tt_local_seq_print_text, net_dev);
301 }
302
303 static int batadv_vis_data_open(struct inode *inode, struct file *file)
304 {
305         struct net_device *net_dev = (struct net_device *)inode->i_private;
306         return single_open(file, batadv_vis_seq_print_text, net_dev);
307 }
308
309 struct batadv_debuginfo {
310         struct attribute attr;
311         const struct file_operations fops;
312 };
313
314 #define BATADV_DEBUGINFO(_name, _mode, _open)           \
315 struct batadv_debuginfo batadv_debuginfo_##_name = {    \
316         .attr = { .name = __stringify(_name),           \
317                   .mode = _mode, },                     \
318         .fops = { .owner = THIS_MODULE,                 \
319                   .open = _open,                        \
320                   .read = seq_read,                     \
321                   .llseek = seq_lseek,                  \
322                   .release = single_release,            \
323                 }                                       \
324 };
325
326 /* the following attributes are general and therefore they will be directly
327  * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
328  */
329 static BATADV_DEBUGINFO(routing_algos, S_IRUGO, batadv_algorithms_open);
330
331 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
332         &batadv_debuginfo_routing_algos,
333         NULL,
334 };
335
336 /* The following attributes are per soft interface */
337 static BATADV_DEBUGINFO(originators, S_IRUGO, batadv_originators_open);
338 static BATADV_DEBUGINFO(gateways, S_IRUGO, batadv_gateways_open);
339 static BATADV_DEBUGINFO(transtable_global, S_IRUGO,
340                         batadv_transtable_global_open);
341 #ifdef CONFIG_BATMAN_ADV_BLA
342 static BATADV_DEBUGINFO(bla_claim_table, S_IRUGO, batadv_bla_claim_table_open);
343 static BATADV_DEBUGINFO(bla_backbone_table, S_IRUGO,
344                         batadv_bla_backbone_table_open);
345 #endif
346 #ifdef CONFIG_BATMAN_ADV_DAT
347 static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
348 #endif
349 static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
350                         batadv_transtable_local_open);
351 static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
352
353 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
354         &batadv_debuginfo_originators,
355         &batadv_debuginfo_gateways,
356         &batadv_debuginfo_transtable_global,
357 #ifdef CONFIG_BATMAN_ADV_BLA
358         &batadv_debuginfo_bla_claim_table,
359         &batadv_debuginfo_bla_backbone_table,
360 #endif
361 #ifdef CONFIG_BATMAN_ADV_DAT
362         &batadv_debuginfo_dat_cache,
363 #endif
364         &batadv_debuginfo_transtable_local,
365         &batadv_debuginfo_vis_data,
366         NULL,
367 };
368
369 void batadv_debugfs_init(void)
370 {
371         struct batadv_debuginfo **bat_debug;
372         struct dentry *file;
373
374         batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
375         if (batadv_debugfs == ERR_PTR(-ENODEV))
376                 batadv_debugfs = NULL;
377
378         if (!batadv_debugfs)
379                 goto err;
380
381         for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
382                 file = debugfs_create_file(((*bat_debug)->attr).name,
383                                            S_IFREG | ((*bat_debug)->attr).mode,
384                                            batadv_debugfs, NULL,
385                                            &(*bat_debug)->fops);
386                 if (!file) {
387                         pr_err("Can't add general debugfs file: %s\n",
388                                ((*bat_debug)->attr).name);
389                         goto err;
390                 }
391         }
392
393         return;
394 err:
395         debugfs_remove_recursive(batadv_debugfs);
396 }
397
398 void batadv_debugfs_destroy(void)
399 {
400         if (batadv_debugfs) {
401                 debugfs_remove_recursive(batadv_debugfs);
402                 batadv_debugfs = NULL;
403         }
404 }
405
406 int batadv_debugfs_add_meshif(struct net_device *dev)
407 {
408         struct batadv_priv *bat_priv = netdev_priv(dev);
409         struct batadv_debuginfo **bat_debug;
410         struct dentry *file;
411
412         if (!batadv_debugfs)
413                 goto out;
414
415         bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
416         if (!bat_priv->debug_dir)
417                 goto out;
418
419         if (batadv_socket_setup(bat_priv) < 0)
420                 goto rem_attr;
421
422         if (batadv_debug_log_setup(bat_priv) < 0)
423                 goto rem_attr;
424
425         for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
426                 file = debugfs_create_file(((*bat_debug)->attr).name,
427                                            S_IFREG | ((*bat_debug)->attr).mode,
428                                            bat_priv->debug_dir,
429                                            dev, &(*bat_debug)->fops);
430                 if (!file) {
431                         batadv_err(dev, "Can't add debugfs file: %s/%s\n",
432                                    dev->name, ((*bat_debug)->attr).name);
433                         goto rem_attr;
434                 }
435         }
436
437         return 0;
438 rem_attr:
439         debugfs_remove_recursive(bat_priv->debug_dir);
440         bat_priv->debug_dir = NULL;
441 out:
442 #ifdef CONFIG_DEBUG_FS
443         return -ENOMEM;
444 #else
445         return 0;
446 #endif /* CONFIG_DEBUG_FS */
447 }
448
449 void batadv_debugfs_del_meshif(struct net_device *dev)
450 {
451         struct batadv_priv *bat_priv = netdev_priv(dev);
452
453         batadv_debug_log_cleanup(bat_priv);
454
455         if (batadv_debugfs) {
456                 debugfs_remove_recursive(bat_priv->debug_dir);
457                 bat_priv->debug_dir = NULL;
458         }
459 }