net :sunrpc :clnt :Fix xps refcount imbalance on the error path
[linux-2.6-block.git] / net / batman-adv / debugfs.c
CommitLineData
7db7d9f3 1// SPDX-License-Identifier: GPL-2.0
7a79d717 2/* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Marek Lindner
c6c8fea2
SE
5 */
6
1e2c2a4f 7#include "debugfs.h"
c6c8fea2
SE
8#include "main.h"
9
a5dac4da 10#include <asm/current.h>
36dc621c 11#include <linux/dcache.h>
c6c8fea2 12#include <linux/debugfs.h>
3e7514af 13#include <linux/err.h>
1e2c2a4f
SE
14#include <linux/errno.h>
15#include <linux/export.h>
1e2c2a4f 16#include <linux/fs.h>
1e2c2a4f 17#include <linux/netdevice.h>
1e2c2a4f 18#include <linux/printk.h>
a5dac4da 19#include <linux/sched.h>
1e2c2a4f 20#include <linux/seq_file.h>
1e2c2a4f
SE
21#include <linux/stat.h>
22#include <linux/stddef.h>
23#include <linux/stringify.h>
24#include <linux/sysfs.h>
94969208 25#include <net/net_namespace.h>
c6c8fea2 26
01d350d1 27#include "bat_algo.h"
9bf8e4d4 28#include "bridge_loop_avoidance.h"
2f1dfbe1 29#include "distributed-arp-table.h"
1e2c2a4f
SE
30#include "gateway_client.h"
31#include "icmp_socket.h"
ba412080 32#include "log.h"
4e3e823b 33#include "multicast.h"
d56b1705 34#include "network-coding.h"
1e2c2a4f
SE
35#include "originator.h"
36#include "translation-table.h"
c6c8fea2 37
9e466250 38static struct dentry *batadv_debugfs;
c6c8fea2 39
00caf6a2
SE
40/**
41 * batadv_debugfs_deprecated() - Log use of deprecated batadv debugfs access
42 * @file: file which was accessed
43 * @alt: explanation what can be used as alternative
44 */
45void batadv_debugfs_deprecated(struct file *file, const char *alt)
46{
47 struct dentry *dentry = file_dentry(file);
48 const char *name = dentry->d_name.name;
49
50 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
51 current->comm, task_pid_nr(current), name, alt);
52}
53
9e466250 54static int batadv_algorithms_open(struct inode *inode, struct file *file)
1c280471 55{
00caf6a2
SE
56 batadv_debugfs_deprecated(file,
57 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
3193e8fd 58 return single_open(file, batadv_algo_seq_print_text, NULL);
1c280471
ML
59}
60
7587405a
ML
61static int neighbors_open(struct inode *inode, struct file *file)
62{
63 struct net_device *net_dev = (struct net_device *)inode->i_private;
64
00caf6a2
SE
65 batadv_debugfs_deprecated(file,
66 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
7587405a
ML
67 return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
68}
69
9e466250 70static int batadv_originators_open(struct inode *inode, struct file *file)
c6c8fea2
SE
71{
72 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 73
00caf6a2
SE
74 batadv_debugfs_deprecated(file,
75 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
7d211efc 76 return single_open(file, batadv_orig_seq_print_text, net_dev);
c6c8fea2
SE
77}
78
cb1c92ec 79/**
7e9a8c2c
SE
80 * batadv_originators_hardif_open() - handles debugfs output for the originator
81 * table of an hard interface
cb1c92ec
SW
82 * @inode: inode pointer to debugfs file
83 * @file: pointer to the seq_file
7afcbbef
SE
84 *
85 * Return: 0 on success or negative error number in case of failure
cb1c92ec
SW
86 */
87static int batadv_originators_hardif_open(struct inode *inode,
88 struct file *file)
89{
90 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 91
00caf6a2
SE
92 batadv_debugfs_deprecated(file,
93 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
cb1c92ec
SW
94 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
95}
96
9e466250 97static int batadv_gateways_open(struct inode *inode, struct file *file)
c6c8fea2
SE
98{
99 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 100
00caf6a2
SE
101 batadv_debugfs_deprecated(file,
102 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
7cf06bc6 103 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
c6c8fea2
SE
104}
105
9e466250 106static int batadv_transtable_global_open(struct inode *inode, struct file *file)
c6c8fea2
SE
107{
108 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 109
00caf6a2
SE
110 batadv_debugfs_deprecated(file,
111 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
08c36d3e 112 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
c6c8fea2
SE
113}
114
7a5cc242 115#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 116static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
9bf8e4d4
SW
117{
118 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 119
00caf6a2
SE
120 batadv_debugfs_deprecated(file,
121 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
08adf151
SE
122 return single_open(file, batadv_bla_claim_table_seq_print_text,
123 net_dev);
9bf8e4d4 124}
536a23f1
SW
125
126static int batadv_bla_backbone_table_open(struct inode *inode,
127 struct file *file)
128{
129 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 130
00caf6a2
SE
131 batadv_debugfs_deprecated(file,
132 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
536a23f1
SW
133 return single_open(file, batadv_bla_backbone_table_seq_print_text,
134 net_dev);
135}
136
7a5cc242 137#endif
9bf8e4d4 138
17224474 139#ifdef CONFIG_BATMAN_ADV_DAT
2f1dfbe1 140/**
be01dc33 141 * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
2f1dfbe1
AQ
142 * @inode: inode which was opened
143 * @file: file handle to be initialized
7afcbbef
SE
144 *
145 * Return: 0 on success or negative error number in case of failure
2f1dfbe1
AQ
146 */
147static int batadv_dat_cache_open(struct inode *inode, struct file *file)
148{
149 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 150
00caf6a2
SE
151 batadv_debugfs_deprecated(file,
152 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
2f1dfbe1
AQ
153 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
154}
17224474 155#endif
2f1dfbe1 156
9e466250 157static int batadv_transtable_local_open(struct inode *inode, struct file *file)
c6c8fea2
SE
158{
159 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 160
00caf6a2
SE
161 batadv_debugfs_deprecated(file,
162 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
08c36d3e 163 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
c6c8fea2
SE
164}
165
7f223c0c 166struct batadv_debuginfo {
c6c8fea2
SE
167 struct attribute attr;
168 const struct file_operations fops;
169};
170
d56b1705
MH
171#ifdef CONFIG_BATMAN_ADV_NC
172static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
173{
174 struct net_device *net_dev = (struct net_device *)inode->i_private;
f138694b 175
00caf6a2 176 batadv_debugfs_deprecated(file, "");
d56b1705
MH
177 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
178}
179#endif
180
4e3e823b
LL
181#ifdef CONFIG_BATMAN_ADV_MCAST
182/**
7e9a8c2c 183 * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
4e3e823b
LL
184 * @inode: inode which was opened
185 * @file: file handle to be initialized
186 *
187 * Return: 0 on success or negative error number in case of failure
188 */
189static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
190{
191 struct net_device *net_dev = (struct net_device *)inode->i_private;
192
00caf6a2
SE
193 batadv_debugfs_deprecated(file,
194 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
4e3e823b
LL
195 return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
196}
197#endif
198
347c80f0 199#define BATADV_DEBUGINFO(_name, _mode, _open) \
7f223c0c 200struct batadv_debuginfo batadv_debuginfo_##_name = { \
121bdca0
SW
201 .attr = { \
202 .name = __stringify(_name), \
203 .mode = _mode, \
204 }, \
205 .fops = { \
206 .owner = THIS_MODULE, \
207 .open = _open, \
208 .read = seq_read, \
209 .llseek = seq_lseek, \
210 .release = single_release, \
211 }, \
2b64df20 212}
c6c8fea2 213
637fbd12
AQ
214/* the following attributes are general and therefore they will be directly
215 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
216 */
507b37cf 217static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
637fbd12
AQ
218
219static struct batadv_debuginfo *batadv_general_debuginfos[] = {
220 &batadv_debuginfo_routing_algos,
221 NULL,
222};
223
224/* The following attributes are per soft interface */
507b37cf
SE
225static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
226static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
227static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
228static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
7a5cc242 229#ifdef CONFIG_BATMAN_ADV_BLA
507b37cf
SE
230static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
231static BATADV_DEBUGINFO(bla_backbone_table, 0444,
536a23f1 232 batadv_bla_backbone_table_open);
7a5cc242 233#endif
17224474 234#ifdef CONFIG_BATMAN_ADV_DAT
507b37cf 235static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
17224474 236#endif
507b37cf 237static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
d56b1705 238#ifdef CONFIG_BATMAN_ADV_NC
507b37cf 239static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
d56b1705 240#endif
4e3e823b 241#ifdef CONFIG_BATMAN_ADV_MCAST
507b37cf 242static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
4e3e823b 243#endif
c6c8fea2 244
7f223c0c 245static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
7587405a 246 &batadv_debuginfo_neighbors,
9e466250
SE
247 &batadv_debuginfo_originators,
248 &batadv_debuginfo_gateways,
249 &batadv_debuginfo_transtable_global,
7a5cc242 250#ifdef CONFIG_BATMAN_ADV_BLA
9e466250 251 &batadv_debuginfo_bla_claim_table,
536a23f1 252 &batadv_debuginfo_bla_backbone_table,
7a5cc242 253#endif
17224474 254#ifdef CONFIG_BATMAN_ADV_DAT
2f1dfbe1 255 &batadv_debuginfo_dat_cache,
17224474 256#endif
9e466250 257 &batadv_debuginfo_transtable_local,
d56b1705
MH
258#ifdef CONFIG_BATMAN_ADV_NC
259 &batadv_debuginfo_nc_nodes,
4e3e823b
LL
260#endif
261#ifdef CONFIG_BATMAN_ADV_MCAST
262 &batadv_debuginfo_mcast_flags,
d56b1705 263#endif
c6c8fea2
SE
264 NULL,
265};
266
5bc7c1eb
SW
267#define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
268struct batadv_debuginfo batadv_hardif_debuginfo_##_name = { \
269 .attr = { \
270 .name = __stringify(_name), \
271 .mode = _mode, \
272 }, \
273 .fops = { \
274 .owner = THIS_MODULE, \
275 .open = _open, \
276 .read = seq_read, \
277 .llseek = seq_lseek, \
278 .release = single_release, \
279 }, \
2b64df20 280}
aa143d28 281
507b37cf 282static BATADV_HARDIF_DEBUGINFO(originators, 0444,
cb1c92ec 283 batadv_originators_hardif_open);
5bc7c1eb
SW
284
285static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
cb1c92ec 286 &batadv_hardif_debuginfo_originators,
5bc7c1eb
SW
287 NULL,
288};
289
ff15c27c
SE
290/**
291 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
292 */
40a072d7 293void batadv_debugfs_init(void)
c6c8fea2 294{
637fbd12 295 struct batadv_debuginfo **bat_debug;
1c280471
ML
296 struct dentry *file;
297
54590e4d 298 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
9e466250
SE
299 if (batadv_debugfs == ERR_PTR(-ENODEV))
300 batadv_debugfs = NULL;
1c280471 301
9e466250 302 if (!batadv_debugfs)
637fbd12 303 goto err;
1c280471 304
637fbd12
AQ
305 for (bat_debug = batadv_general_debuginfos; *bat_debug; ++bat_debug) {
306 file = debugfs_create_file(((*bat_debug)->attr).name,
307 S_IFREG | ((*bat_debug)->attr).mode,
308 batadv_debugfs, NULL,
309 &(*bat_debug)->fops);
310 if (!file) {
311 pr_err("Can't add general debugfs file: %s\n",
312 ((*bat_debug)->attr).name);
313 goto err;
314 }
315 }
1c280471 316
1c280471 317 return;
637fbd12
AQ
318err:
319 debugfs_remove_recursive(batadv_debugfs);
5bc7c1eb 320 batadv_debugfs = NULL;
c6c8fea2
SE
321}
322
ff15c27c
SE
323/**
324 * batadv_debugfs_destroy() - Remove all debugfs entries
325 */
40a072d7 326void batadv_debugfs_destroy(void)
c6c8fea2 327{
3f87c423
AQ
328 debugfs_remove_recursive(batadv_debugfs);
329 batadv_debugfs = NULL;
c6c8fea2
SE
330}
331
5bc7c1eb 332/**
7e9a8c2c 333 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
5bc7c1eb
SW
334 * in debugfs.
335 * @hard_iface: hard interface which should be added.
7afcbbef
SE
336 *
337 * Return: 0 on success or negative error number in case of failure
5bc7c1eb
SW
338 */
339int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
340{
94969208 341 struct net *net = dev_net(hard_iface->net_dev);
5bc7c1eb
SW
342 struct batadv_debuginfo **bat_debug;
343 struct dentry *file;
344
345 if (!batadv_debugfs)
346 goto out;
347
94969208
AL
348 if (net != &init_net)
349 return 0;
350
5bc7c1eb
SW
351 hard_iface->debug_dir = debugfs_create_dir(hard_iface->net_dev->name,
352 batadv_debugfs);
353 if (!hard_iface->debug_dir)
354 goto out;
355
356 for (bat_debug = batadv_hardif_debuginfos; *bat_debug; ++bat_debug) {
357 file = debugfs_create_file(((*bat_debug)->attr).name,
358 S_IFREG | ((*bat_debug)->attr).mode,
359 hard_iface->debug_dir,
360 hard_iface->net_dev,
361 &(*bat_debug)->fops);
362 if (!file)
363 goto rem_attr;
364 }
365
366 return 0;
367rem_attr:
368 debugfs_remove_recursive(hard_iface->debug_dir);
369 hard_iface->debug_dir = NULL;
370out:
5bc7c1eb 371 return -ENOMEM;
5bc7c1eb
SW
372}
373
36dc621c
SE
374/**
375 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
376 * @hard_iface: hard interface which was renamed
377 */
378void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
379{
380 const char *name = hard_iface->net_dev->name;
381 struct dentry *dir;
382 struct dentry *d;
383
384 dir = hard_iface->debug_dir;
385 if (!dir)
386 return;
387
388 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
389 if (!d)
390 pr_err("Can't rename debugfs dir to %s\n", name);
391}
392
5bc7c1eb 393/**
7e9a8c2c 394 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
5bc7c1eb
SW
395 * in debugfs.
396 * @hard_iface: hard interface which is deleted.
397 */
398void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
399{
94969208
AL
400 struct net *net = dev_net(hard_iface->net_dev);
401
402 if (net != &init_net)
403 return;
404
5bc7c1eb
SW
405 if (batadv_debugfs) {
406 debugfs_remove_recursive(hard_iface->debug_dir);
407 hard_iface->debug_dir = NULL;
408 }
409}
410
ff15c27c
SE
411/**
412 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
413 * @dev: netdev struct of the soft interface
414 *
415 * Return: 0 on success or negative error number in case of failure
416 */
40a072d7 417int batadv_debugfs_add_meshif(struct net_device *dev)
c6c8fea2 418{
56303d34 419 struct batadv_priv *bat_priv = netdev_priv(dev);
7f223c0c 420 struct batadv_debuginfo **bat_debug;
94969208 421 struct net *net = dev_net(dev);
c6c8fea2
SE
422 struct dentry *file;
423
9e466250 424 if (!batadv_debugfs)
c6c8fea2
SE
425 goto out;
426
94969208
AL
427 if (net != &init_net)
428 return 0;
429
9e466250 430 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
c6c8fea2
SE
431 if (!bat_priv->debug_dir)
432 goto out;
433
9039dc7e 434 if (batadv_socket_setup(bat_priv) < 0)
5346c35e
SE
435 goto rem_attr;
436
9e466250 437 if (batadv_debug_log_setup(bat_priv) < 0)
5346c35e 438 goto rem_attr;
c6c8fea2 439
9e466250 440 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
c6c8fea2 441 file = debugfs_create_file(((*bat_debug)->attr).name,
0aca2369
SE
442 S_IFREG | ((*bat_debug)->attr).mode,
443 bat_priv->debug_dir,
444 dev, &(*bat_debug)->fops);
c6c8fea2 445 if (!file) {
3e34819e
SE
446 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
447 dev->name, ((*bat_debug)->attr).name);
c6c8fea2
SE
448 goto rem_attr;
449 }
450 }
451
d56b1705
MH
452 if (batadv_nc_init_debugfs(bat_priv) < 0)
453 goto rem_attr;
454
c6c8fea2
SE
455 return 0;
456rem_attr:
457 debugfs_remove_recursive(bat_priv->debug_dir);
458 bat_priv->debug_dir = NULL;
459out:
c6c8fea2 460 return -ENOMEM;
c6c8fea2
SE
461}
462
6da7be7d
SE
463/**
464 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
465 * @dev: net_device which was renamed
466 */
467void batadv_debugfs_rename_meshif(struct net_device *dev)
468{
469 struct batadv_priv *bat_priv = netdev_priv(dev);
470 const char *name = dev->name;
471 struct dentry *dir;
472 struct dentry *d;
473
474 dir = bat_priv->debug_dir;
475 if (!dir)
476 return;
477
478 d = debugfs_rename(dir->d_parent, dir, dir->d_parent, name);
479 if (!d)
480 pr_err("Can't rename debugfs dir to %s\n", name);
481}
482
ff15c27c
SE
483/**
484 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
485 * @dev: netdev struct of the soft interface
486 */
40a072d7 487void batadv_debugfs_del_meshif(struct net_device *dev)
c6c8fea2 488{
56303d34 489 struct batadv_priv *bat_priv = netdev_priv(dev);
94969208
AL
490 struct net *net = dev_net(dev);
491
492 if (net != &init_net)
493 return;
c6c8fea2 494
9e466250 495 batadv_debug_log_cleanup(bat_priv);
c6c8fea2 496
9e466250 497 if (batadv_debugfs) {
c6c8fea2
SE
498 debugfs_remove_recursive(bat_priv->debug_dir);
499 bat_priv->debug_dir = NULL;
500 }
501}