Merge tag 'mips_fixes_5.2_2' of git://git.kernel.org/pub/scm/linux/kernel/git/mips...
[linux-2.6/linux-2.6-arm.git] / net / batman-adv / debugfs.c
blobd38d70ccdd5ae8aab3afa87b38c7f498a9d5f826
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2010-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner
5 */
7 #include "debugfs.h"
8 #include "main.h"
10 #include <asm/current.h>
11 #include <linux/dcache.h>
12 #include <linux/debugfs.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/export.h>
16 #include <linux/fs.h>
17 #include <linux/netdevice.h>
18 #include <linux/printk.h>
19 #include <linux/sched.h>
20 #include <linux/seq_file.h>
21 #include <linux/stat.h>
22 #include <linux/stddef.h>
23 #include <linux/stringify.h>
24 #include <linux/sysfs.h>
25 #include <net/net_namespace.h>
27 #include "bat_algo.h"
28 #include "bridge_loop_avoidance.h"
29 #include "distributed-arp-table.h"
30 #include "gateway_client.h"
31 #include "icmp_socket.h"
32 #include "log.h"
33 #include "multicast.h"
34 #include "network-coding.h"
35 #include "originator.h"
36 #include "translation-table.h"
38 static struct dentry *batadv_debugfs;
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
45 void batadv_debugfs_deprecated(struct file *file, const char *alt)
47 struct dentry *dentry = file_dentry(file);
48 const char *name = dentry->d_name.name;
50 pr_warn_ratelimited(DEPRECATED "%s (pid %d) Use of debugfs file \"%s\".\n%s",
51 current->comm, task_pid_nr(current), name, alt);
54 static int batadv_algorithms_open(struct inode *inode, struct file *file)
56 batadv_debugfs_deprecated(file,
57 "Use genl command BATADV_CMD_GET_ROUTING_ALGOS instead\n");
58 return single_open(file, batadv_algo_seq_print_text, NULL);
61 static int neighbors_open(struct inode *inode, struct file *file)
63 struct net_device *net_dev = (struct net_device *)inode->i_private;
65 batadv_debugfs_deprecated(file,
66 "Use genl command BATADV_CMD_GET_NEIGHBORS instead\n");
67 return single_open(file, batadv_hardif_neigh_seq_print_text, net_dev);
70 static int batadv_originators_open(struct inode *inode, struct file *file)
72 struct net_device *net_dev = (struct net_device *)inode->i_private;
74 batadv_debugfs_deprecated(file,
75 "Use genl command BATADV_CMD_GET_ORIGINATORS instead\n");
76 return single_open(file, batadv_orig_seq_print_text, net_dev);
79 /**
80 * batadv_originators_hardif_open() - handles debugfs output for the originator
81 * table of an hard interface
82 * @inode: inode pointer to debugfs file
83 * @file: pointer to the seq_file
85 * Return: 0 on success or negative error number in case of failure
87 static int batadv_originators_hardif_open(struct inode *inode,
88 struct file *file)
90 struct net_device *net_dev = (struct net_device *)inode->i_private;
92 batadv_debugfs_deprecated(file,
93 "Use genl command BATADV_CMD_GET_HARDIFS instead\n");
94 return single_open(file, batadv_orig_hardif_seq_print_text, net_dev);
97 static int batadv_gateways_open(struct inode *inode, struct file *file)
99 struct net_device *net_dev = (struct net_device *)inode->i_private;
101 batadv_debugfs_deprecated(file,
102 "Use genl command BATADV_CMD_GET_GATEWAYS instead\n");
103 return single_open(file, batadv_gw_client_seq_print_text, net_dev);
106 static int batadv_transtable_global_open(struct inode *inode, struct file *file)
108 struct net_device *net_dev = (struct net_device *)inode->i_private;
110 batadv_debugfs_deprecated(file,
111 "Use genl command BATADV_CMD_GET_TRANSTABLE_GLOBAL instead\n");
112 return single_open(file, batadv_tt_global_seq_print_text, net_dev);
115 #ifdef CONFIG_BATMAN_ADV_BLA
116 static int batadv_bla_claim_table_open(struct inode *inode, struct file *file)
118 struct net_device *net_dev = (struct net_device *)inode->i_private;
120 batadv_debugfs_deprecated(file,
121 "Use genl command BATADV_CMD_GET_BLA_CLAIM instead\n");
122 return single_open(file, batadv_bla_claim_table_seq_print_text,
123 net_dev);
126 static int batadv_bla_backbone_table_open(struct inode *inode,
127 struct file *file)
129 struct net_device *net_dev = (struct net_device *)inode->i_private;
131 batadv_debugfs_deprecated(file,
132 "Use genl command BATADV_CMD_GET_BLA_BACKBONE instead\n");
133 return single_open(file, batadv_bla_backbone_table_seq_print_text,
134 net_dev);
137 #endif
139 #ifdef CONFIG_BATMAN_ADV_DAT
141 * batadv_dat_cache_open() - Prepare file handler for reads from dat_cache
142 * @inode: inode which was opened
143 * @file: file handle to be initialized
145 * Return: 0 on success or negative error number in case of failure
147 static int batadv_dat_cache_open(struct inode *inode, struct file *file)
149 struct net_device *net_dev = (struct net_device *)inode->i_private;
151 batadv_debugfs_deprecated(file,
152 "Use genl command BATADV_CMD_GET_DAT_CACHE instead\n");
153 return single_open(file, batadv_dat_cache_seq_print_text, net_dev);
155 #endif
157 static int batadv_transtable_local_open(struct inode *inode, struct file *file)
159 struct net_device *net_dev = (struct net_device *)inode->i_private;
161 batadv_debugfs_deprecated(file,
162 "Use genl command BATADV_CMD_GET_TRANSTABLE_LOCAL instead\n");
163 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
166 struct batadv_debuginfo {
167 struct attribute attr;
168 const struct file_operations fops;
171 #ifdef CONFIG_BATMAN_ADV_NC
172 static int batadv_nc_nodes_open(struct inode *inode, struct file *file)
174 struct net_device *net_dev = (struct net_device *)inode->i_private;
176 batadv_debugfs_deprecated(file, "");
177 return single_open(file, batadv_nc_nodes_seq_print_text, net_dev);
179 #endif
181 #ifdef CONFIG_BATMAN_ADV_MCAST
183 * batadv_mcast_flags_open() - prepare file handler for reads from mcast_flags
184 * @inode: inode which was opened
185 * @file: file handle to be initialized
187 * Return: 0 on success or negative error number in case of failure
189 static int batadv_mcast_flags_open(struct inode *inode, struct file *file)
191 struct net_device *net_dev = (struct net_device *)inode->i_private;
193 batadv_debugfs_deprecated(file,
194 "Use genl command BATADV_CMD_GET_MCAST_FLAGS instead\n");
195 return single_open(file, batadv_mcast_flags_seq_print_text, net_dev);
197 #endif
199 #define BATADV_DEBUGINFO(_name, _mode, _open) \
200 struct batadv_debuginfo batadv_debuginfo_##_name = { \
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 }, \
214 /* the following attributes are general and therefore they will be directly
215 * placed in the BATADV_DEBUGFS_SUBDIR subdirectory of debugfs
217 static BATADV_DEBUGINFO(routing_algos, 0444, batadv_algorithms_open);
219 static struct batadv_debuginfo *batadv_general_debuginfos[] = {
220 &batadv_debuginfo_routing_algos,
221 NULL,
224 /* The following attributes are per soft interface */
225 static BATADV_DEBUGINFO(neighbors, 0444, neighbors_open);
226 static BATADV_DEBUGINFO(originators, 0444, batadv_originators_open);
227 static BATADV_DEBUGINFO(gateways, 0444, batadv_gateways_open);
228 static BATADV_DEBUGINFO(transtable_global, 0444, batadv_transtable_global_open);
229 #ifdef CONFIG_BATMAN_ADV_BLA
230 static BATADV_DEBUGINFO(bla_claim_table, 0444, batadv_bla_claim_table_open);
231 static BATADV_DEBUGINFO(bla_backbone_table, 0444,
232 batadv_bla_backbone_table_open);
233 #endif
234 #ifdef CONFIG_BATMAN_ADV_DAT
235 static BATADV_DEBUGINFO(dat_cache, 0444, batadv_dat_cache_open);
236 #endif
237 static BATADV_DEBUGINFO(transtable_local, 0444, batadv_transtable_local_open);
238 #ifdef CONFIG_BATMAN_ADV_NC
239 static BATADV_DEBUGINFO(nc_nodes, 0444, batadv_nc_nodes_open);
240 #endif
241 #ifdef CONFIG_BATMAN_ADV_MCAST
242 static BATADV_DEBUGINFO(mcast_flags, 0444, batadv_mcast_flags_open);
243 #endif
245 static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
246 &batadv_debuginfo_neighbors,
247 &batadv_debuginfo_originators,
248 &batadv_debuginfo_gateways,
249 &batadv_debuginfo_transtable_global,
250 #ifdef CONFIG_BATMAN_ADV_BLA
251 &batadv_debuginfo_bla_claim_table,
252 &batadv_debuginfo_bla_backbone_table,
253 #endif
254 #ifdef CONFIG_BATMAN_ADV_DAT
255 &batadv_debuginfo_dat_cache,
256 #endif
257 &batadv_debuginfo_transtable_local,
258 #ifdef CONFIG_BATMAN_ADV_NC
259 &batadv_debuginfo_nc_nodes,
260 #endif
261 #ifdef CONFIG_BATMAN_ADV_MCAST
262 &batadv_debuginfo_mcast_flags,
263 #endif
264 NULL,
267 #define BATADV_HARDIF_DEBUGINFO(_name, _mode, _open) \
268 struct 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 }, \
282 static BATADV_HARDIF_DEBUGINFO(originators, 0444,
283 batadv_originators_hardif_open);
285 static struct batadv_debuginfo *batadv_hardif_debuginfos[] = {
286 &batadv_hardif_debuginfo_originators,
287 NULL,
291 * batadv_debugfs_init() - Initialize soft interface independent debugfs entries
293 void batadv_debugfs_init(void)
295 struct batadv_debuginfo **bat_debug;
296 struct dentry *file;
298 batadv_debugfs = debugfs_create_dir(BATADV_DEBUGFS_SUBDIR, NULL);
299 if (batadv_debugfs == ERR_PTR(-ENODEV))
300 batadv_debugfs = NULL;
302 if (!batadv_debugfs)
303 goto err;
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;
317 return;
318 err:
319 debugfs_remove_recursive(batadv_debugfs);
320 batadv_debugfs = NULL;
324 * batadv_debugfs_destroy() - Remove all debugfs entries
326 void batadv_debugfs_destroy(void)
328 debugfs_remove_recursive(batadv_debugfs);
329 batadv_debugfs = NULL;
333 * batadv_debugfs_add_hardif() - creates the base directory for a hard interface
334 * in debugfs.
335 * @hard_iface: hard interface which should be added.
337 * Return: 0 on success or negative error number in case of failure
339 int batadv_debugfs_add_hardif(struct batadv_hard_iface *hard_iface)
341 struct net *net = dev_net(hard_iface->net_dev);
342 struct batadv_debuginfo **bat_debug;
343 struct dentry *file;
345 if (!batadv_debugfs)
346 goto out;
348 if (net != &init_net)
349 return 0;
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;
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;
366 return 0;
367 rem_attr:
368 debugfs_remove_recursive(hard_iface->debug_dir);
369 hard_iface->debug_dir = NULL;
370 out:
371 return -ENOMEM;
375 * batadv_debugfs_rename_hardif() - Fix debugfs path for renamed hardif
376 * @hard_iface: hard interface which was renamed
378 void batadv_debugfs_rename_hardif(struct batadv_hard_iface *hard_iface)
380 const char *name = hard_iface->net_dev->name;
381 struct dentry *dir;
382 struct dentry *d;
384 dir = hard_iface->debug_dir;
385 if (!dir)
386 return;
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);
394 * batadv_debugfs_del_hardif() - delete the base directory for a hard interface
395 * in debugfs.
396 * @hard_iface: hard interface which is deleted.
398 void batadv_debugfs_del_hardif(struct batadv_hard_iface *hard_iface)
400 struct net *net = dev_net(hard_iface->net_dev);
402 if (net != &init_net)
403 return;
405 if (batadv_debugfs) {
406 debugfs_remove_recursive(hard_iface->debug_dir);
407 hard_iface->debug_dir = NULL;
412 * batadv_debugfs_add_meshif() - Initialize interface dependent debugfs entries
413 * @dev: netdev struct of the soft interface
415 * Return: 0 on success or negative error number in case of failure
417 int batadv_debugfs_add_meshif(struct net_device *dev)
419 struct batadv_priv *bat_priv = netdev_priv(dev);
420 struct batadv_debuginfo **bat_debug;
421 struct net *net = dev_net(dev);
422 struct dentry *file;
424 if (!batadv_debugfs)
425 goto out;
427 if (net != &init_net)
428 return 0;
430 bat_priv->debug_dir = debugfs_create_dir(dev->name, batadv_debugfs);
431 if (!bat_priv->debug_dir)
432 goto out;
434 if (batadv_socket_setup(bat_priv) < 0)
435 goto rem_attr;
437 if (batadv_debug_log_setup(bat_priv) < 0)
438 goto rem_attr;
440 for (bat_debug = batadv_mesh_debuginfos; *bat_debug; ++bat_debug) {
441 file = debugfs_create_file(((*bat_debug)->attr).name,
442 S_IFREG | ((*bat_debug)->attr).mode,
443 bat_priv->debug_dir,
444 dev, &(*bat_debug)->fops);
445 if (!file) {
446 batadv_err(dev, "Can't add debugfs file: %s/%s\n",
447 dev->name, ((*bat_debug)->attr).name);
448 goto rem_attr;
452 if (batadv_nc_init_debugfs(bat_priv) < 0)
453 goto rem_attr;
455 return 0;
456 rem_attr:
457 debugfs_remove_recursive(bat_priv->debug_dir);
458 bat_priv->debug_dir = NULL;
459 out:
460 return -ENOMEM;
464 * batadv_debugfs_rename_meshif() - Fix debugfs path for renamed softif
465 * @dev: net_device which was renamed
467 void batadv_debugfs_rename_meshif(struct net_device *dev)
469 struct batadv_priv *bat_priv = netdev_priv(dev);
470 const char *name = dev->name;
471 struct dentry *dir;
472 struct dentry *d;
474 dir = bat_priv->debug_dir;
475 if (!dir)
476 return;
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);
484 * batadv_debugfs_del_meshif() - Remove interface dependent debugfs entries
485 * @dev: netdev struct of the soft interface
487 void batadv_debugfs_del_meshif(struct net_device *dev)
489 struct batadv_priv *bat_priv = netdev_priv(dev);
490 struct net *net = dev_net(dev);
492 if (net != &init_net)
493 return;
495 batadv_debug_log_cleanup(bat_priv);
497 if (batadv_debugfs) {
498 debugfs_remove_recursive(bat_priv->debug_dir);
499 bat_priv->debug_dir = NULL;