xref: /openbmc/linux/net/sunrpc/sysctl.c (revision 703c6d03)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * linux/net/sunrpc/sysctl.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Sysctl interface to sunrpc module.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * I would prefer to register the sunrpc table below sys/net, but that's
81da177e4SLinus Torvalds  * impossible at the moment.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/types.h>
121da177e4SLinus Torvalds #include <linux/linkage.h>
131da177e4SLinus Torvalds #include <linux/ctype.h>
141da177e4SLinus Torvalds #include <linux/fs.h>
151da177e4SLinus Torvalds #include <linux/sysctl.h>
161da177e4SLinus Torvalds #include <linux/module.h>
171da177e4SLinus Torvalds 
187c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
191da177e4SLinus Torvalds #include <linux/sunrpc/types.h>
201da177e4SLinus Torvalds #include <linux/sunrpc/sched.h>
211da177e4SLinus Torvalds #include <linux/sunrpc/stats.h>
22dc9a16e4STom Tucker #include <linux/sunrpc/svc_xprt.h>
231da177e4SLinus Torvalds 
2470abc49bSStanislav Kinsbursky #include "netns.h"
2570abc49bSStanislav Kinsbursky 
261da177e4SLinus Torvalds /*
271da177e4SLinus Torvalds  * Declare the debug flags here
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds unsigned int	rpc_debug;
30e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(rpc_debug);
31a6eaf8bdSTrond Myklebust 
321da177e4SLinus Torvalds unsigned int	nfs_debug;
33e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(nfs_debug);
34a6eaf8bdSTrond Myklebust 
351da177e4SLinus Torvalds unsigned int	nfsd_debug;
36e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(nfsd_debug);
37a6eaf8bdSTrond Myklebust 
381da177e4SLinus Torvalds unsigned int	nlm_debug;
39e8914c65STrond Myklebust EXPORT_SYMBOL_GPL(nlm_debug);
401da177e4SLinus Torvalds 
41f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
421da177e4SLinus Torvalds 
proc_do_xprt(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)43fe2c6338SJoe Perches static int proc_do_xprt(struct ctl_table *table, int write,
4432927393SChristoph Hellwig 			void *buffer, size_t *lenp, loff_t *ppos)
45dc9a16e4STom Tucker {
46dc9a16e4STom Tucker 	char tmpbuf[256];
47ae297504SDan Carpenter 	ssize_t len;
4827df6f25SCyrill Gorcunov 
49d435c05aSDan Carpenter 	if (write || *ppos) {
50dc9a16e4STom Tucker 		*lenp = 0;
51dc9a16e4STom Tucker 		return 0;
52dc9a16e4STom Tucker 	}
53dc9a16e4STom Tucker 	len = svc_print_xprts(tmpbuf, sizeof(tmpbuf));
54ae297504SDan Carpenter 	len = memory_read_from_buffer(buffer, *lenp, ppos, tmpbuf, len);
55c09f56b8SArtur Molchanov 
56ae297504SDan Carpenter 	if (len < 0) {
57c09f56b8SArtur Molchanov 		*lenp = 0;
58c09f56b8SArtur Molchanov 		return -EINVAL;
59c09f56b8SArtur Molchanov 	}
60ae297504SDan Carpenter 	*lenp = len;
61c09f56b8SArtur Molchanov 	return 0;
62dc9a16e4STom Tucker }
63dc9a16e4STom Tucker 
641da177e4SLinus Torvalds static int
proc_dodebug(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)6532927393SChristoph Hellwig proc_dodebug(struct ctl_table *table, int write, void *buffer, size_t *lenp,
6632927393SChristoph Hellwig 	     loff_t *ppos)
671da177e4SLinus Torvalds {
6832927393SChristoph Hellwig 	char		tmpbuf[20], *s = NULL;
6932927393SChristoph Hellwig 	char *p;
701da177e4SLinus Torvalds 	unsigned int	value;
711da177e4SLinus Torvalds 	size_t		left, len;
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	if ((*ppos && !write) || !*lenp) {
741da177e4SLinus Torvalds 		*lenp = 0;
751da177e4SLinus Torvalds 		return 0;
761da177e4SLinus Torvalds 	}
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	left = *lenp;
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	if (write) {
811da177e4SLinus Torvalds 		p = buffer;
8232927393SChristoph Hellwig 		while (left && isspace(*p)) {
8332927393SChristoph Hellwig 			left--;
8432927393SChristoph Hellwig 			p++;
8532927393SChristoph Hellwig 		}
861da177e4SLinus Torvalds 		if (!left)
871da177e4SLinus Torvalds 			goto done;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds 		if (left > sizeof(tmpbuf) - 1)
901da177e4SLinus Torvalds 			return -EINVAL;
9132927393SChristoph Hellwig 		memcpy(tmpbuf, p, left);
921da177e4SLinus Torvalds 		tmpbuf[left] = '\0';
931da177e4SLinus Torvalds 
94941c3ff3SKinglong Mee 		value = simple_strtol(tmpbuf, &s, 0);
95941c3ff3SKinglong Mee 		if (s) {
96941c3ff3SKinglong Mee 			left -= (s - tmpbuf);
97941c3ff3SKinglong Mee 			if (left && !isspace(*s))
981da177e4SLinus Torvalds 				return -EINVAL;
99ca65a280SJoe Perches 			while (left && isspace(*s)) {
100ca65a280SJoe Perches 				left--;
101ca65a280SJoe Perches 				s++;
102ca65a280SJoe Perches 			}
103941c3ff3SKinglong Mee 		} else
104941c3ff3SKinglong Mee 			left = 0;
1051da177e4SLinus Torvalds 		*(unsigned int *) table->data = value;
1061da177e4SLinus Torvalds 		/* Display the RPC tasks on writing to rpc_debug */
107bc2a3f86SJ. Bruce Fields 		if (strcmp(table->procname, "rpc_debug") == 0)
10870abc49bSStanislav Kinsbursky 			rpc_show_tasks(&init_net);
1091da177e4SLinus Torvalds 	} else {
110941c3ff3SKinglong Mee 		len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
1111da177e4SLinus Torvalds 		if (len > left)
1121da177e4SLinus Torvalds 			len = left;
11332927393SChristoph Hellwig 		memcpy(buffer, tmpbuf, len);
1141da177e4SLinus Torvalds 		if ((left -= len) > 0) {
11532927393SChristoph Hellwig 			*((char *)buffer + len) = '\n';
1161da177e4SLinus Torvalds 			left--;
1171da177e4SLinus Torvalds 		}
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds done:
1211da177e4SLinus Torvalds 	*lenp -= left;
1221da177e4SLinus Torvalds 	*ppos += *lenp;
1231da177e4SLinus Torvalds 	return 0;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
12632e356beSLuis Chamberlain static struct ctl_table_header *sunrpc_table_header;
127a246b010SChuck Lever 
128fe2c6338SJoe Perches static struct ctl_table debug_table[] = {
1291da177e4SLinus Torvalds 	{
1301da177e4SLinus Torvalds 		.procname	= "rpc_debug",
1311da177e4SLinus Torvalds 		.data		= &rpc_debug,
1321da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
1331da177e4SLinus Torvalds 		.mode		= 0644,
1346d456111SEric W. Biederman 		.proc_handler	= proc_dodebug
1351da177e4SLinus Torvalds 	},
1361da177e4SLinus Torvalds 	{
1371da177e4SLinus Torvalds 		.procname	= "nfs_debug",
1381da177e4SLinus Torvalds 		.data		= &nfs_debug,
1391da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
1401da177e4SLinus Torvalds 		.mode		= 0644,
1416d456111SEric W. Biederman 		.proc_handler	= proc_dodebug
1421da177e4SLinus Torvalds 	},
1431da177e4SLinus Torvalds 	{
1441da177e4SLinus Torvalds 		.procname	= "nfsd_debug",
1451da177e4SLinus Torvalds 		.data		= &nfsd_debug,
1461da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
1471da177e4SLinus Torvalds 		.mode		= 0644,
1486d456111SEric W. Biederman 		.proc_handler	= proc_dodebug
1491da177e4SLinus Torvalds 	},
1501da177e4SLinus Torvalds 	{
1511da177e4SLinus Torvalds 		.procname	= "nlm_debug",
1521da177e4SLinus Torvalds 		.data		= &nlm_debug,
1531da177e4SLinus Torvalds 		.maxlen		= sizeof(int),
1541da177e4SLinus Torvalds 		.mode		= 0644,
1556d456111SEric W. Biederman 		.proc_handler	= proc_dodebug
1561da177e4SLinus Torvalds 	},
157dc9a16e4STom Tucker 	{
158dc9a16e4STom Tucker 		.procname	= "transports",
159dc9a16e4STom Tucker 		.maxlen		= 256,
160dc9a16e4STom Tucker 		.mode		= 0444,
1616d456111SEric W. Biederman 		.proc_handler	= proc_do_xprt,
162dc9a16e4STom Tucker 	},
163f8572d8fSEric W. Biederman 	{ }
1641da177e4SLinus Torvalds };
1651da177e4SLinus Torvalds 
16632e356beSLuis Chamberlain void
rpc_register_sysctl(void)16732e356beSLuis Chamberlain rpc_register_sysctl(void)
16832e356beSLuis Chamberlain {
16932e356beSLuis Chamberlain 	if (!sunrpc_table_header)
170*703c6d03SLuis Chamberlain 		sunrpc_table_header = register_sysctl("sunrpc", debug_table);
17132e356beSLuis Chamberlain }
17232e356beSLuis Chamberlain 
17332e356beSLuis Chamberlain void
rpc_unregister_sysctl(void)17432e356beSLuis Chamberlain rpc_unregister_sysctl(void)
17532e356beSLuis Chamberlain {
17632e356beSLuis Chamberlain 	if (sunrpc_table_header) {
17732e356beSLuis Chamberlain 		unregister_sysctl_table(sunrpc_table_header);
17832e356beSLuis Chamberlain 		sunrpc_table_header = NULL;
17932e356beSLuis Chamberlain 	}
18032e356beSLuis Chamberlain }
1811da177e4SLinus Torvalds #endif
182