xref: /linux/fs/nfsd/nfsctl.c (revision c2fc946223f565d99322e942cdc46396d3c7d60b)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Syscall interface to knfsd.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
85a0e3ad6STejun Heo #include <linux/slab.h>
93f8206d4SAl Viro #include <linux/namei.h>
10b41b66d6SNeilBrown #include <linux/ctype.h>
1196a374a3SDavid Howells #include <linux/fs_context.h>
121da177e4SLinus Torvalds 
1380212d59SNeilBrown #include <linux/sunrpc/svcsock.h>
144373ea84SWendy Cheng #include <linux/lockd/lockd.h>
155976687aSJeff Layton #include <linux/sunrpc/addr.h>
16b0b0c0a2SKevin Coffman #include <linux/sunrpc/gss_api.h>
17813fd320SJeff Layton #include <linux/sunrpc/rpc_pipe_fs.h>
18924f4fb0SLorenzo Bianconi #include <linux/sunrpc/svc.h>
19143cb494SPaul Gortmaker #include <linux/module.h>
20e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h>
211da177e4SLinus Torvalds 
222ca72e17SJ. Bruce Fields #include "idmap.h"
239a74af21SBoaz Harrosh #include "nfsd.h"
249a74af21SBoaz Harrosh #include "cache.h"
25f3c7521fSBryan Schumaker #include "state.h"
267ea34ac1SJeff Layton #include "netns.h"
279cf514ccSChristoph Hellwig #include "pnfs.h"
282e6c6e4cSChuck Lever #include "filecache.h"
2939d432fcSChuck Lever #include "trace.h"
3013727f85SLorenzo Bianconi #include "netlink.h"
319a74af21SBoaz Harrosh 
321da177e4SLinus Torvalds /*
33b0b0c0a2SKevin Coffman  *	We have a single directory with several nodes in it.
341da177e4SLinus Torvalds  */
351da177e4SLinus Torvalds enum {
361da177e4SLinus Torvalds 	NFSD_Root = 1,
371da177e4SLinus Torvalds 	NFSD_List,
3820ad856eSAmir Goldstein 	NFSD_Export_Stats,
39e8e8753fSJ. Bruce Fields 	NFSD_Export_features,
401da177e4SLinus Torvalds 	NFSD_Fh,
414373ea84SWendy Cheng 	NFSD_FO_UnlockIP,
4217efa372SWendy Cheng 	NFSD_FO_UnlockFS,
431da177e4SLinus Torvalds 	NFSD_Threads,
44eed2965aSGreg Banks 	NFSD_Pool_Threads,
4503cf6c9fSGreg Banks 	NFSD_Pool_Stats,
46a2f999a3SJeff Layton 	NFSD_Reply_Cache_Stats,
4770c3b76cSNeilBrown 	NFSD_Versions,
4880212d59SNeilBrown 	NFSD_Ports,
49596bbe53SNeilBrown 	NFSD_MaxBlkSize,
505b8db00bSJeff Layton 	NFSD_MaxConnections,
512e6c6e4cSChuck Lever 	NFSD_Filecache,
521da177e4SLinus Torvalds 	NFSD_Leasetime,
53efc4bb4fSJ. Bruce Fields 	NFSD_Gracetime,
540964a3d3SNeilBrown 	NFSD_RecoveryDir,
557f5ef2e9SJeff Layton 	NFSD_V4EndGrace,
56e8a79fb1SJ. Bruce Fields 	NFSD_MaxReserved
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * write() for these nodes.
611da177e4SLinus Torvalds  */
621da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
63b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
64b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
651da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size);
66eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
6770c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size);
6880212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size);
69596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
705b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
7170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
721da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
73efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
7474fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
750964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
7674fd4873SJeff Layton #endif
777f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
7870c3b76cSNeilBrown #endif
791da177e4SLinus Torvalds 
80c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
811da177e4SLinus Torvalds 	[NFSD_Fh] = write_filehandle,
82b046ccdcSChuck Lever 	[NFSD_FO_UnlockIP] = write_unlock_ip,
83b046ccdcSChuck Lever 	[NFSD_FO_UnlockFS] = write_unlock_fs,
841da177e4SLinus Torvalds 	[NFSD_Threads] = write_threads,
85eed2965aSGreg Banks 	[NFSD_Pool_Threads] = write_pool_threads,
8670c3b76cSNeilBrown 	[NFSD_Versions] = write_versions,
8780212d59SNeilBrown 	[NFSD_Ports] = write_ports,
88596bbe53SNeilBrown 	[NFSD_MaxBlkSize] = write_maxblksize,
895b8db00bSJeff Layton 	[NFSD_MaxConnections] = write_maxconn,
9070c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
911da177e4SLinus Torvalds 	[NFSD_Leasetime] = write_leasetime,
92efc4bb4fSJ. Bruce Fields 	[NFSD_Gracetime] = write_gracetime,
9374fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
940964a3d3SNeilBrown 	[NFSD_RecoveryDir] = write_recoverydir,
9574fd4873SJeff Layton #endif
967f5ef2e9SJeff Layton 	[NFSD_V4EndGrace] = write_v4_end_grace,
9770c3b76cSNeilBrown #endif
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
nfsctl_transaction_write(struct file * file,const char __user * buf,size_t size,loff_t * pos)1001da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
1011da177e4SLinus Torvalds {
102496ad9aaSAl Viro 	ino_t ino =  file_inode(file)->i_ino;
1031da177e4SLinus Torvalds 	char *data;
1041da177e4SLinus Torvalds 	ssize_t rv;
1051da177e4SLinus Torvalds 
106e8c96f8cSTobias Klauser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1071da177e4SLinus Torvalds 		return -EINVAL;
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
1101da177e4SLinus Torvalds 	if (IS_ERR(data))
1111da177e4SLinus Torvalds 		return PTR_ERR(data);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
1143434d7aaSChuck Lever 	if (rv < 0)
1151da177e4SLinus Torvalds 		return rv;
1163434d7aaSChuck Lever 
1173434d7aaSChuck Lever 	simple_transaction_set(file, rv);
1183434d7aaSChuck Lever 	return size;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
nfsctl_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)1217390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1227390022dSNeilBrown {
1237390022dSNeilBrown 	if (! file->private_data) {
1247390022dSNeilBrown 		/* An attempt to read a transaction file without writing
1257390022dSNeilBrown 		 * causes a 0-byte write so that the file can return
1267390022dSNeilBrown 		 * state information
1277390022dSNeilBrown 		 */
1287390022dSNeilBrown 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
1297390022dSNeilBrown 		if (rv < 0)
1307390022dSNeilBrown 			return rv;
1317390022dSNeilBrown 	}
1327390022dSNeilBrown 	return simple_transaction_read(file, buf, size, pos);
1337390022dSNeilBrown }
1347390022dSNeilBrown 
1354b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = {
1361da177e4SLinus Torvalds 	.write		= nfsctl_transaction_write,
1377390022dSNeilBrown 	.read		= nfsctl_transaction_read,
1381da177e4SLinus Torvalds 	.release	= simple_transaction_release,
1396038f373SArnd Bergmann 	.llseek		= default_llseek,
1401da177e4SLinus Torvalds };
1411da177e4SLinus Torvalds 
exports_net_open(struct net * net,struct file * file)14296d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file)
1431da177e4SLinus Torvalds {
144f2c7ea10SStanislav Kinsbursky 	int err;
145f2c7ea10SStanislav Kinsbursky 	struct seq_file *seq;
14696d851c4SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
147f2c7ea10SStanislav Kinsbursky 
148f2c7ea10SStanislav Kinsbursky 	err = seq_open(file, &nfs_exports_op);
149f2c7ea10SStanislav Kinsbursky 	if (err)
150f2c7ea10SStanislav Kinsbursky 		return err;
151f2c7ea10SStanislav Kinsbursky 
152f2c7ea10SStanislav Kinsbursky 	seq = file->private_data;
153e5f06f72SStanislav Kinsbursky 	seq->private = nn->svc_export_cache;
154f2c7ea10SStanislav Kinsbursky 	return 0;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
exports_nfsd_open(struct inode * inode,struct file * file)15796d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file)
15896d851c4SStanislav Kinsbursky {
15996d851c4SStanislav Kinsbursky 	return exports_net_open(inode->i_sb->s_fs_info, file);
16096d851c4SStanislav Kinsbursky }
16196d851c4SStanislav Kinsbursky 
16296d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = {
16396d851c4SStanislav Kinsbursky 	.open		= exports_nfsd_open,
1641da177e4SLinus Torvalds 	.read		= seq_read,
1651da177e4SLinus Torvalds 	.llseek		= seq_lseek,
1661da177e4SLinus Torvalds 	.release	= seq_release,
1671da177e4SLinus Torvalds };
1681da177e4SLinus Torvalds 
export_features_show(struct seq_file * m,void * v)169e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v)
170e8e8753fSJ. Bruce Fields {
171e8e8753fSJ. Bruce Fields 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
172e8e8753fSJ. Bruce Fields 	return 0;
173e8e8753fSJ. Bruce Fields }
174e8e8753fSJ. Bruce Fields 
1759beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(export_features);
176e8e8753fSJ. Bruce Fields 
177828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = {
17803cf6c9fSGreg Banks 	.open		= nfsd_pool_stats_open,
17903cf6c9fSGreg Banks 	.read		= seq_read,
18003cf6c9fSGreg Banks 	.llseek		= seq_lseek,
1817b207ccdSNeilBrown 	.release	= seq_release,
18203cf6c9fSGreg Banks };
18303cf6c9fSGreg Banks 
18464776611SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
185a2f999a3SJeff Layton 
1861342f9ddSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
1872e6c6e4cSChuck Lever 
1881da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
1891da177e4SLinus Torvalds /*
1901da177e4SLinus Torvalds  * payload - write methods
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds 
netns(struct file * file)193244c7d44SAl Viro static inline struct net *netns(struct file *file)
194244c7d44SAl Viro {
195244c7d44SAl Viro 	return file_inode(file)->i_sb->s_fs_info;
196244c7d44SAl Viro }
1971da177e4SLinus Torvalds 
198f2453978SChuck Lever /*
199262a0982SChuck Lever  * write_unlock_ip - Release all locks used by a client
200262a0982SChuck Lever  *
201262a0982SChuck Lever  * Experimental.
202262a0982SChuck Lever  *
203262a0982SChuck Lever  * Input:
204262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing a
2054116092bSChuck Lever  *				presentation format IP address
206262a0982SChuck Lever  *			size:	length of C string in @buf
207262a0982SChuck Lever  * Output:
208262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
209262a0982SChuck Lever  *			returns one if one or more locks were not released
210262a0982SChuck Lever  *	On error:	return code is negative errno value
211262a0982SChuck Lever  */
write_unlock_ip(struct file * file,char * buf,size_t size)212b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
2134373ea84SWendy Cheng {
2144116092bSChuck Lever 	struct sockaddr_storage address;
2154116092bSChuck Lever 	struct sockaddr *sap = (struct sockaddr *)&address;
2164116092bSChuck Lever 	size_t salen = sizeof(address);
217367c8c7bSChuck Lever 	char *fo_path;
218244c7d44SAl Viro 	struct net *net = netns(file);
2194373ea84SWendy Cheng 
2204373ea84SWendy Cheng 	/* sanity check */
2214373ea84SWendy Cheng 	if (size == 0)
2224373ea84SWendy Cheng 		return -EINVAL;
2234373ea84SWendy Cheng 
2244373ea84SWendy Cheng 	if (buf[size-1] != '\n')
2254373ea84SWendy Cheng 		return -EINVAL;
2264373ea84SWendy Cheng 
2274373ea84SWendy Cheng 	fo_path = buf;
2284373ea84SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
2294373ea84SWendy Cheng 		return -EINVAL;
2304373ea84SWendy Cheng 
23111f77942SStanislav Kinsbursky 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
2324373ea84SWendy Cheng 		return -EINVAL;
2334373ea84SWendy Cheng 
23439d432fcSChuck Lever 	trace_nfsd_ctl_unlock_ip(net, buf);
2354116092bSChuck Lever 	return nlmsvc_unlock_all_by_ip(sap);
2364373ea84SWendy Cheng }
2374373ea84SWendy Cheng 
238f2453978SChuck Lever /*
239262a0982SChuck Lever  * write_unlock_fs - Release all locks on a local file system
240262a0982SChuck Lever  *
241262a0982SChuck Lever  * Experimental.
242262a0982SChuck Lever  *
243262a0982SChuck Lever  * Input:
244262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing the
245262a0982SChuck Lever  *				absolute pathname of a local file system
246262a0982SChuck Lever  *			size:	length of C string in @buf
247262a0982SChuck Lever  * Output:
248262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
249262a0982SChuck Lever  *			returns one if one or more locks were not released
250262a0982SChuck Lever  *	On error:	return code is negative errno value
251262a0982SChuck Lever  */
write_unlock_fs(struct file * file,char * buf,size_t size)252b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
25317efa372SWendy Cheng {
254a63bb996SAl Viro 	struct path path;
25517efa372SWendy Cheng 	char *fo_path;
25617efa372SWendy Cheng 	int error;
25717efa372SWendy Cheng 
25817efa372SWendy Cheng 	/* sanity check */
25917efa372SWendy Cheng 	if (size == 0)
26017efa372SWendy Cheng 		return -EINVAL;
26117efa372SWendy Cheng 
26217efa372SWendy Cheng 	if (buf[size-1] != '\n')
26317efa372SWendy Cheng 		return -EINVAL;
26417efa372SWendy Cheng 
26517efa372SWendy Cheng 	fo_path = buf;
26617efa372SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
26717efa372SWendy Cheng 		return -EINVAL;
26839d432fcSChuck Lever 	trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
269a63bb996SAl Viro 	error = kern_path(fo_path, 0, &path);
27017efa372SWendy Cheng 	if (error)
27117efa372SWendy Cheng 		return error;
27217efa372SWendy Cheng 
273262a0982SChuck Lever 	/*
274262a0982SChuck Lever 	 * XXX: Needs better sanity checking.  Otherwise we could end up
275262a0982SChuck Lever 	 * releasing locks on the wrong file system.
276262a0982SChuck Lever 	 *
277262a0982SChuck Lever 	 * For example:
278262a0982SChuck Lever 	 * 1.  Does the path refer to a directory?
279262a0982SChuck Lever 	 * 2.  Is that directory a mount point, or
280262a0982SChuck Lever 	 * 3.  Is that directory the root of an exported file system?
281262a0982SChuck Lever 	 */
282d8c9584eSAl Viro 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
2831ac3629bSNeilBrown 	nfsd4_revoke_states(netns(file), path.dentry->d_sb);
28417efa372SWendy Cheng 
285a63bb996SAl Viro 	path_put(&path);
28617efa372SWendy Cheng 	return error;
28717efa372SWendy Cheng }
28817efa372SWendy Cheng 
289f2453978SChuck Lever /*
290262a0982SChuck Lever  * write_filehandle - Get a variable-length NFS file handle by path
291262a0982SChuck Lever  *
292262a0982SChuck Lever  * On input, the buffer contains a '\n'-terminated C string comprised of
293262a0982SChuck Lever  * three alphanumeric words separated by whitespace.  The string may
294262a0982SChuck Lever  * contain escape sequences.
295262a0982SChuck Lever  *
296262a0982SChuck Lever  * Input:
297262a0982SChuck Lever  *			buf:
298262a0982SChuck Lever  *				domain:		client domain name
299262a0982SChuck Lever  *				path:		export pathname
300262a0982SChuck Lever  *				maxsize:	numeric maximum size of
301262a0982SChuck Lever  *						@buf
302262a0982SChuck Lever  *			size:	length of C string in @buf
303262a0982SChuck Lever  * Output:
304262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
305262a0982SChuck Lever  *			string containing a ASCII hex text version
306262a0982SChuck Lever  *			of the NFS file handle;
307262a0982SChuck Lever  *			return code is the size in bytes of the string
308262a0982SChuck Lever  *	On error:	return code is negative errno value
309262a0982SChuck Lever  */
write_filehandle(struct file * file,char * buf,size_t size)3101da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	char *dname, *path;
3133f649ab7SKees Cook 	int maxsize;
3141da177e4SLinus Torvalds 	char *mesg = buf;
3151da177e4SLinus Torvalds 	int len;
3161da177e4SLinus Torvalds 	struct auth_domain *dom;
3171da177e4SLinus Torvalds 	struct knfsd_fh fh;
3181da177e4SLinus Torvalds 
31987d26ea7SJ. Bruce Fields 	if (size == 0)
32087d26ea7SJ. Bruce Fields 		return -EINVAL;
32187d26ea7SJ. Bruce Fields 
3221da177e4SLinus Torvalds 	if (buf[size-1] != '\n')
3231da177e4SLinus Torvalds 		return -EINVAL;
3241da177e4SLinus Torvalds 	buf[size-1] = 0;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	dname = mesg;
3271da177e4SLinus Torvalds 	len = qword_get(&mesg, dname, size);
32854224f04SChuck Lever 	if (len <= 0)
32954224f04SChuck Lever 		return -EINVAL;
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	path = dname+len+1;
3321da177e4SLinus Torvalds 	len = qword_get(&mesg, path, size);
33354224f04SChuck Lever 	if (len <= 0)
33454224f04SChuck Lever 		return -EINVAL;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	len = get_int(&mesg, &maxsize);
3371da177e4SLinus Torvalds 	if (len)
3381da177e4SLinus Torvalds 		return len;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	if (maxsize < NFS_FHSIZE)
3411da177e4SLinus Torvalds 		return -EINVAL;
3423c7aa15dSKinglong Mee 	maxsize = min(maxsize, NFS3_FHSIZE);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	if (qword_get(&mesg, mesg, size) > 0)
3451da177e4SLinus Torvalds 		return -EINVAL;
3461da177e4SLinus Torvalds 
34739d432fcSChuck Lever 	trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
34839d432fcSChuck Lever 
3491da177e4SLinus Torvalds 	/* we have all the words, they are in buf.. */
3501da177e4SLinus Torvalds 	dom = unix_domain_find(dname);
3511da177e4SLinus Torvalds 	if (!dom)
3521da177e4SLinus Torvalds 		return -ENOMEM;
3531da177e4SLinus Torvalds 
354244c7d44SAl Viro 	len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
3551da177e4SLinus Torvalds 	auth_domain_put(dom);
3561da177e4SLinus Torvalds 	if (len)
3571da177e4SLinus Torvalds 		return len;
3581da177e4SLinus Torvalds 
35954224f04SChuck Lever 	mesg = buf;
36054224f04SChuck Lever 	len = SIMPLE_TRANSACTION_LIMIT;
361d8b26071SNeilBrown 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
3621da177e4SLinus Torvalds 	mesg[-1] = '\n';
3631da177e4SLinus Torvalds 	return mesg - buf;
3641da177e4SLinus Torvalds }
3651da177e4SLinus Torvalds 
366f2453978SChuck Lever /*
367262a0982SChuck Lever  * write_threads - Start NFSD, or report the current number of running threads
368262a0982SChuck Lever  *
369262a0982SChuck Lever  * Input:
370262a0982SChuck Lever  *			buf:		ignored
371262a0982SChuck Lever  *			size:		zero
372262a0982SChuck Lever  * Output:
373262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
374262a0982SChuck Lever  *			string numeric value representing the number of
375262a0982SChuck Lever  *			running NFSD threads;
376262a0982SChuck Lever  *			return code is the size in bytes of the string
377262a0982SChuck Lever  *	On error:	return code is zero
378262a0982SChuck Lever  *
379262a0982SChuck Lever  * OR
380262a0982SChuck Lever  *
381262a0982SChuck Lever  * Input:
382262a0982SChuck Lever  *			buf:		C string containing an unsigned
383262a0982SChuck Lever  *					integer value representing the
384262a0982SChuck Lever  *					number of NFSD threads to start
385262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
386262a0982SChuck Lever  * Output:
387262a0982SChuck Lever  *	On success:	NFS service is started;
388262a0982SChuck Lever  *			passed-in buffer filled with '\n'-terminated C
389262a0982SChuck Lever  *			string numeric value representing the number of
390262a0982SChuck Lever  *			running NFSD threads;
391262a0982SChuck Lever  *			return code is the size in bytes of the string
392262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
393262a0982SChuck Lever  */
write_threads(struct file * file,char * buf,size_t size)3941da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size)
3951da177e4SLinus Torvalds {
3961da177e4SLinus Torvalds 	char *mesg = buf;
3971da177e4SLinus Torvalds 	int rv;
398244c7d44SAl Viro 	struct net *net = netns(file);
399d41a9417SStanislav Kinsbursky 
4001da177e4SLinus Torvalds 	if (size > 0) {
4011da177e4SLinus Torvalds 		int newthreads;
4021da177e4SLinus Torvalds 		rv = get_int(&mesg, &newthreads);
4031da177e4SLinus Torvalds 		if (rv)
4041da177e4SLinus Torvalds 			return rv;
4051da177e4SLinus Torvalds 		if (newthreads < 0)
4061da177e4SLinus Torvalds 			return -EINVAL;
40739d432fcSChuck Lever 		trace_nfsd_ctl_threads(net, newthreads);
4080842b4c8SJeff Layton 		mutex_lock(&nfsd_mutex);
4099077d598SJeff Layton 		rv = nfsd_svc(newthreads, net, file->f_cred, NULL);
4100842b4c8SJeff Layton 		mutex_unlock(&nfsd_mutex);
41182e12fe9SNeilBrown 		if (rv < 0)
4121da177e4SLinus Torvalds 			return rv;
41382e12fe9SNeilBrown 	} else
4149dd9845fSStanislav Kinsbursky 		rv = nfsd_nrthreads(net);
415e06b6405SChuck Lever 
41682e12fe9SNeilBrown 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
4171da177e4SLinus Torvalds }
4181da177e4SLinus Torvalds 
419f2453978SChuck Lever /*
420262a0982SChuck Lever  * write_pool_threads - Set or report the current number of threads per pool
421262a0982SChuck Lever  *
422262a0982SChuck Lever  * Input:
423262a0982SChuck Lever  *			buf:		ignored
424262a0982SChuck Lever  *			size:		zero
425262a0982SChuck Lever  *
426262a0982SChuck Lever  * OR
427262a0982SChuck Lever  *
428262a0982SChuck Lever  * Input:
429262a0982SChuck Lever  *			buf:		C string containing whitespace-
430262a0982SChuck Lever  *					separated unsigned integer values
431262a0982SChuck Lever  *					representing the number of NFSD
432262a0982SChuck Lever  *					threads to start in each pool
433262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
434262a0982SChuck Lever  * Output:
435262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
436262a0982SChuck Lever  *			string containing integer values representing the
437262a0982SChuck Lever  *			number of NFSD threads in each pool;
438262a0982SChuck Lever  *			return code is the size in bytes of the string
439262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
440262a0982SChuck Lever  */
write_pool_threads(struct file * file,char * buf,size_t size)441eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
442eed2965aSGreg Banks {
443eed2965aSGreg Banks 	/* if size > 0, look for an array of number of threads per node
444eed2965aSGreg Banks 	 * and apply them  then write out number of threads per node as reply
445eed2965aSGreg Banks 	 */
446eed2965aSGreg Banks 	char *mesg = buf;
447eed2965aSGreg Banks 	int i;
448eed2965aSGreg Banks 	int rv;
449eed2965aSGreg Banks 	int len;
450bedbdd8bSNeil Brown 	int npools;
451eed2965aSGreg Banks 	int *nthreads;
452244c7d44SAl Viro 	struct net *net = netns(file);
453eed2965aSGreg Banks 
454bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
4559dd9845fSStanislav Kinsbursky 	npools = nfsd_nrpools(net);
456eed2965aSGreg Banks 	if (npools == 0) {
457eed2965aSGreg Banks 		/*
458eed2965aSGreg Banks 		 * NFS is shut down.  The admin can start it by
459eed2965aSGreg Banks 		 * writing to the threads file but NOT the pool_threads
460eed2965aSGreg Banks 		 * file, sorry.  Report zero threads.
461eed2965aSGreg Banks 		 */
462bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
463eed2965aSGreg Banks 		strcpy(buf, "0\n");
464eed2965aSGreg Banks 		return strlen(buf);
465eed2965aSGreg Banks 	}
466eed2965aSGreg Banks 
467eed2965aSGreg Banks 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
468bedbdd8bSNeil Brown 	rv = -ENOMEM;
469eed2965aSGreg Banks 	if (nthreads == NULL)
470bedbdd8bSNeil Brown 		goto out_free;
471eed2965aSGreg Banks 
472eed2965aSGreg Banks 	if (size > 0) {
473eed2965aSGreg Banks 		for (i = 0; i < npools; i++) {
474eed2965aSGreg Banks 			rv = get_int(&mesg, &nthreads[i]);
475eed2965aSGreg Banks 			if (rv == -ENOENT)
476eed2965aSGreg Banks 				break;		/* fewer numbers than pools */
477eed2965aSGreg Banks 			if (rv)
478eed2965aSGreg Banks 				goto out_free;	/* syntax error */
479eed2965aSGreg Banks 			rv = -EINVAL;
480eed2965aSGreg Banks 			if (nthreads[i] < 0)
481eed2965aSGreg Banks 				goto out_free;
48239d432fcSChuck Lever 			trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
483eed2965aSGreg Banks 		}
4843938a0d5SStanislav Kinsbursky 		rv = nfsd_set_nrthreads(i, nthreads, net);
485eed2965aSGreg Banks 		if (rv)
486eed2965aSGreg Banks 			goto out_free;
487eed2965aSGreg Banks 	}
488eed2965aSGreg Banks 
4899dd9845fSStanislav Kinsbursky 	rv = nfsd_get_nrthreads(npools, nthreads, net);
490eed2965aSGreg Banks 	if (rv)
491eed2965aSGreg Banks 		goto out_free;
492eed2965aSGreg Banks 
493eed2965aSGreg Banks 	mesg = buf;
494eed2965aSGreg Banks 	size = SIMPLE_TRANSACTION_LIMIT;
495eed2965aSGreg Banks 	for (i = 0; i < npools && size > 0; i++) {
496eed2965aSGreg Banks 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
497eed2965aSGreg Banks 		len = strlen(mesg);
498eed2965aSGreg Banks 		size -= len;
499eed2965aSGreg Banks 		mesg += len;
500eed2965aSGreg Banks 	}
501413d63d7SJ. Bruce Fields 	rv = mesg - buf;
502eed2965aSGreg Banks out_free:
503eed2965aSGreg Banks 	kfree(nthreads);
504bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
505eed2965aSGreg Banks 	return rv;
506eed2965aSGreg Banks }
507eed2965aSGreg Banks 
508ff7d1179STrond Myklebust static ssize_t
nfsd_print_version_support(struct nfsd_net * nn,char * buf,int remaining,const char * sep,unsigned vers,int minor)509e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
510e333f3bbSTrond Myklebust 		const char *sep, unsigned vers, int minor)
511ff7d1179STrond Myklebust {
512abcb4dacSNeilBrown 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
513e333f3bbSTrond Myklebust 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
514ff7d1179STrond Myklebust 
515abcb4dacSNeilBrown 	if (vers == 4 && minor >= 0 &&
516e333f3bbSTrond Myklebust 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
517ff7d1179STrond Myklebust 		supported = false;
518abcb4dacSNeilBrown 	if (minor == 0 && supported)
519abcb4dacSNeilBrown 		/*
520abcb4dacSNeilBrown 		 * special case for backward compatability.
521abcb4dacSNeilBrown 		 * +4.0 is never reported, it is implied by
522abcb4dacSNeilBrown 		 * +4, unless -4.0 is present.
523abcb4dacSNeilBrown 		 */
524abcb4dacSNeilBrown 		return 0;
525ff7d1179STrond Myklebust 	return snprintf(buf, remaining, format, sep,
526ff7d1179STrond Myklebust 			supported ? '+' : '-', vers, minor);
527ff7d1179STrond Myklebust }
528ff7d1179STrond Myklebust 
__write_versions(struct file * file,char * buf,size_t size)5293dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size)
53070c3b76cSNeilBrown {
53170c3b76cSNeilBrown 	char *mesg = buf;
5328daf220aSBenny Halevy 	char *vers, *minorp, sign;
533261758b5SChuck Lever 	int len, num, remaining;
53470c3b76cSNeilBrown 	ssize_t tlen = 0;
53570c3b76cSNeilBrown 	char *sep;
536244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
53770c3b76cSNeilBrown 
53870c3b76cSNeilBrown 	if (size > 0) {
5399dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
5406658d3a7SNeilBrown 			/* Cannot change versions without updating
5419dd9845fSStanislav Kinsbursky 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
5426658d3a7SNeilBrown 			 * rq_argp and rq_resp
5436658d3a7SNeilBrown 			 */
54470c3b76cSNeilBrown 			return -EBUSY;
54570c3b76cSNeilBrown 		if (buf[size-1] != '\n')
54670c3b76cSNeilBrown 			return -EINVAL;
54770c3b76cSNeilBrown 		buf[size-1] = 0;
54839d432fcSChuck Lever 		trace_nfsd_ctl_version(netns(file), buf);
54970c3b76cSNeilBrown 
55070c3b76cSNeilBrown 		vers = mesg;
55170c3b76cSNeilBrown 		len = qword_get(&mesg, vers, size);
55270c3b76cSNeilBrown 		if (len <= 0) return -EINVAL;
55370c3b76cSNeilBrown 		do {
554d3635ff0STrond Myklebust 			enum vers_op cmd;
555abcb4dacSNeilBrown 			unsigned minor;
55670c3b76cSNeilBrown 			sign = *vers;
55770c3b76cSNeilBrown 			if (sign == '+' || sign == '-')
5588daf220aSBenny Halevy 				num = simple_strtol((vers+1), &minorp, 0);
55970c3b76cSNeilBrown 			else
5608daf220aSBenny Halevy 				num = simple_strtol(vers, &minorp, 0);
5618daf220aSBenny Halevy 			if (*minorp == '.') {
562ff89be87SJ. Bruce Fields 				if (num != 4)
5638daf220aSBenny Halevy 					return -EINVAL;
564e35659f1SNeilBrown 				if (kstrtouint(minorp+1, 0, &minor) < 0)
5658daf220aSBenny Halevy 					return -EINVAL;
566abcb4dacSNeilBrown 			}
567abcb4dacSNeilBrown 
568d3635ff0STrond Myklebust 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
56970c3b76cSNeilBrown 			switch(num) {
5702f3a4b2aSJeff Layton #ifdef CONFIG_NFSD_V2
57170c3b76cSNeilBrown 			case 2:
5722f3a4b2aSJeff Layton #endif
57370c3b76cSNeilBrown 			case 3:
574e333f3bbSTrond Myklebust 				nfsd_vers(nn, num, cmd);
575d3635ff0STrond Myklebust 				break;
57670c3b76cSNeilBrown 			case 4:
577abcb4dacSNeilBrown 				if (*minorp == '.') {
578e333f3bbSTrond Myklebust 					if (nfsd_minorversion(nn, minor, cmd) < 0)
579abcb4dacSNeilBrown 						return -EINVAL;
580e333f3bbSTrond Myklebust 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
581abcb4dacSNeilBrown 					/*
582abcb4dacSNeilBrown 					 * Either we have +4 and no minors are enabled,
583abcb4dacSNeilBrown 					 * or we have -4 and at least one minor is enabled.
584abcb4dacSNeilBrown 					 * In either case, propagate 'cmd' to all minors.
585abcb4dacSNeilBrown 					 */
586abcb4dacSNeilBrown 					minor = 0;
587e333f3bbSTrond Myklebust 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
588abcb4dacSNeilBrown 						minor++;
589abcb4dacSNeilBrown 				}
59070c3b76cSNeilBrown 				break;
59170c3b76cSNeilBrown 			default:
5928e823bafSJeff Layton 				/* Ignore requests to disable non-existent versions */
5938e823bafSJeff Layton 				if (cmd == NFSD_SET)
59470c3b76cSNeilBrown 					return -EINVAL;
59570c3b76cSNeilBrown 			}
59670c3b76cSNeilBrown 			vers += len + 1;
59770c3b76cSNeilBrown 		} while ((len = qword_get(&mesg, vers, size)) > 0);
59870c3b76cSNeilBrown 		/* If all get turned off, turn them back on, as
59970c3b76cSNeilBrown 		 * having no versions is BAD
60070c3b76cSNeilBrown 		 */
601e333f3bbSTrond Myklebust 		nfsd_reset_versions(nn);
60270c3b76cSNeilBrown 	}
603261758b5SChuck Lever 
60470c3b76cSNeilBrown 	/* Now write current state into reply buffer */
60570c3b76cSNeilBrown 	sep = "";
606261758b5SChuck Lever 	remaining = SIMPLE_TRANSACTION_LIMIT;
607ff7d1179STrond Myklebust 	for (num=2 ; num <= 4 ; num++) {
608abcb4dacSNeilBrown 		int minor;
609e333f3bbSTrond Myklebust 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
610e35659f1SNeilBrown 			continue;
611abcb4dacSNeilBrown 
612abcb4dacSNeilBrown 		minor = -1;
613ff7d1179STrond Myklebust 		do {
614e333f3bbSTrond Myklebust 			len = nfsd_print_version_support(nn, buf, remaining,
615ff7d1179STrond Myklebust 					sep, num, minor);
616818f2f57SDan Carpenter 			if (len >= remaining)
617ff7d1179STrond Myklebust 				goto out;
618261758b5SChuck Lever 			remaining -= len;
619261758b5SChuck Lever 			buf += len;
620261758b5SChuck Lever 			tlen += len;
621ff7d1179STrond Myklebust 			minor++;
622abcb4dacSNeilBrown 			if (len)
623ff7d1179STrond Myklebust 				sep = " ";
624ff7d1179STrond Myklebust 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
625261758b5SChuck Lever 	}
626ff7d1179STrond Myklebust out:
627261758b5SChuck Lever 	len = snprintf(buf, remaining, "\n");
628818f2f57SDan Carpenter 	if (len >= remaining)
629261758b5SChuck Lever 		return -EINVAL;
630261758b5SChuck Lever 	return tlen + len;
63170c3b76cSNeilBrown }
63270c3b76cSNeilBrown 
633f2453978SChuck Lever /*
634262a0982SChuck Lever  * write_versions - Set or report the available NFS protocol versions
635262a0982SChuck Lever  *
636262a0982SChuck Lever  * Input:
637262a0982SChuck Lever  *			buf:		ignored
638262a0982SChuck Lever  *			size:		zero
639262a0982SChuck Lever  * Output:
640262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
641262a0982SChuck Lever  *			string containing positive or negative integer
642262a0982SChuck Lever  *			values representing the current status of each
643262a0982SChuck Lever  *			protocol version;
644262a0982SChuck Lever  *			return code is the size in bytes of the string
645262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
646262a0982SChuck Lever  *
647262a0982SChuck Lever  * OR
648262a0982SChuck Lever  *
649262a0982SChuck Lever  * Input:
650262a0982SChuck Lever  *			buf:		C string containing whitespace-
651262a0982SChuck Lever  *					separated positive or negative
652262a0982SChuck Lever  *					integer values representing NFS
653262a0982SChuck Lever  *					protocol versions to enable ("+n")
654262a0982SChuck Lever  *					or disable ("-n")
655262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
656262a0982SChuck Lever  * Output:
657262a0982SChuck Lever  *	On success:	status of zero or more protocol versions has
658262a0982SChuck Lever  *			been updated; passed-in buffer filled with
659262a0982SChuck Lever  *			'\n'-terminated C string containing positive
660262a0982SChuck Lever  *			or negative integer values representing the
661262a0982SChuck Lever  *			current status of each protocol version;
662262a0982SChuck Lever  *			return code is the size in bytes of the string
663262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
664262a0982SChuck Lever  */
write_versions(struct file * file,char * buf,size_t size)6653dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size)
6663dd98a3bSJeff Layton {
6673dd98a3bSJeff Layton 	ssize_t rv;
6683dd98a3bSJeff Layton 
6693dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
6703dd98a3bSJeff Layton 	rv = __write_versions(file, buf, size);
6713dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
6723dd98a3bSJeff Layton 	return rv;
6733dd98a3bSJeff Layton }
6743dd98a3bSJeff Layton 
6754cd5dc75SChuck Lever /*
6760a5372d8SChuck Lever  * Zero-length write.  Return a list of NFSD's current listener
6770a5372d8SChuck Lever  * transports.
6780a5372d8SChuck Lever  */
__write_ports_names(char * buf,struct net * net)6799dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net)
6800a5372d8SChuck Lever {
6819dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6829dd9845fSStanislav Kinsbursky 
6839dd9845fSStanislav Kinsbursky 	if (nn->nfsd_serv == NULL)
6840a5372d8SChuck Lever 		return 0;
6859dd9845fSStanislav Kinsbursky 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
6860a5372d8SChuck Lever }
6870a5372d8SChuck Lever 
6880a5372d8SChuck Lever /*
6890b7c2f6fSChuck Lever  * A single 'fd' number was written, in which case it must be for
6900b7c2f6fSChuck Lever  * a socket of a supported family/protocol, and we use it as an
6910b7c2f6fSChuck Lever  * nfsd listener.
6920b7c2f6fSChuck Lever  */
__write_ports_addfd(char * buf,struct net * net,const struct cred * cred)6934df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
6940b7c2f6fSChuck Lever {
6950b7c2f6fSChuck Lever 	char *mesg = buf;
6960b7c2f6fSChuck Lever 	int fd, err;
6979dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
69864e63041SJeff Layton 	struct svc_serv *serv;
6990b7c2f6fSChuck Lever 
7000b7c2f6fSChuck Lever 	err = get_int(&mesg, &fd);
7010b7c2f6fSChuck Lever 	if (err != 0 || fd < 0)
7020b7c2f6fSChuck Lever 		return -EINVAL;
70339d432fcSChuck Lever 	trace_nfsd_ctl_ports_addfd(net, fd);
7040b7c2f6fSChuck Lever 
7056777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7060b7c2f6fSChuck Lever 	if (err != 0)
7070b7c2f6fSChuck Lever 		return err;
7080b7c2f6fSChuck Lever 
70964e63041SJeff Layton 	serv = nn->nfsd_serv;
71064e63041SJeff Layton 	err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
711ea068badSChuck Lever 
7121e3577a4SNeilBrown 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
71317419aefSNeilBrown 		nfsd_destroy_serv(net);
71417419aefSNeilBrown 
715ea068badSChuck Lever 	return err;
7160b7c2f6fSChuck Lever }
7170b7c2f6fSChuck Lever 
7180b7c2f6fSChuck Lever /*
719442a6290SChuck Lever  * A transport listener is added by writing its transport name and
7204eb68c26SChuck Lever  * a port number.
7214eb68c26SChuck Lever  */
__write_ports_addxprt(char * buf,struct net * net,const struct cred * cred)7224df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
7234eb68c26SChuck Lever {
7244eb68c26SChuck Lever 	char transport[16];
72537498292SChuck Lever 	struct svc_xprt *xprt;
7264eb68c26SChuck Lever 	int port, err;
7279dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
72864e63041SJeff Layton 	struct svc_serv *serv;
7294eb68c26SChuck Lever 
730a10fded1SJ. Bruce Fields 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
7314eb68c26SChuck Lever 		return -EINVAL;
7324eb68c26SChuck Lever 
7334be929beSAlexey Dobriyan 	if (port < 1 || port > USHRT_MAX)
7344eb68c26SChuck Lever 		return -EINVAL;
73539d432fcSChuck Lever 	trace_nfsd_ctl_ports_addxprt(net, transport, port);
7364eb68c26SChuck Lever 
7376777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7384eb68c26SChuck Lever 	if (err != 0)
7394eb68c26SChuck Lever 		return err;
7404eb68c26SChuck Lever 
74164e63041SJeff Layton 	serv = nn->nfsd_serv;
74264e63041SJeff Layton 	err = svc_xprt_create(serv, transport, net,
7434df493a2STrond Myklebust 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
74468717908SChuck Lever 	if (err < 0)
74537498292SChuck Lever 		goto out_err;
74637498292SChuck Lever 
74764e63041SJeff Layton 	err = svc_xprt_create(serv, transport, net,
7484df493a2STrond Myklebust 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
74937498292SChuck Lever 	if (err < 0 && err != -EAFNOSUPPORT)
75037498292SChuck Lever 		goto out_close;
7510cd14a06SJeff Layton 
7524eb68c26SChuck Lever 	return 0;
75337498292SChuck Lever out_close:
75464e63041SJeff Layton 	xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
75537498292SChuck Lever 	if (xprt != NULL) {
7564355d767SChuck Lever 		svc_xprt_close(xprt);
75737498292SChuck Lever 		svc_xprt_put(xprt);
75837498292SChuck Lever 	}
75937498292SChuck Lever out_err:
7601e3577a4SNeilBrown 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
76117419aefSNeilBrown 		nfsd_destroy_serv(net);
7622a501f55SNeilBrown 
76337498292SChuck Lever 	return err;
7644eb68c26SChuck Lever }
7654eb68c26SChuck Lever 
__write_ports(struct file * file,char * buf,size_t size,struct net * net)76608160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size,
76708160352SStanislav Kinsbursky 			     struct net *net)
76880212d59SNeilBrown {
7690a5372d8SChuck Lever 	if (size == 0)
7709dd9845fSStanislav Kinsbursky 		return __write_ports_names(buf, net);
7710b7c2f6fSChuck Lever 
7720b7c2f6fSChuck Lever 	if (isdigit(buf[0]))
7734df493a2STrond Myklebust 		return __write_ports_addfd(buf, net, file->f_cred);
77482d56591SChuck Lever 
7754eb68c26SChuck Lever 	if (isalpha(buf[0]))
7764df493a2STrond Myklebust 		return __write_ports_addxprt(buf, net, file->f_cred);
7774cd5dc75SChuck Lever 
778b41b66d6SNeilBrown 	return -EINVAL;
779b41b66d6SNeilBrown }
78080212d59SNeilBrown 
781f2453978SChuck Lever /*
782262a0982SChuck Lever  * write_ports - Pass a socket file descriptor or transport name to listen on
783262a0982SChuck Lever  *
784262a0982SChuck Lever  * Input:
785262a0982SChuck Lever  *			buf:		ignored
786262a0982SChuck Lever  *			size:		zero
787262a0982SChuck Lever  * Output:
788262a0982SChuck Lever  *	On success:	passed-in buffer filled with a '\n'-terminated C
789262a0982SChuck Lever  *			string containing a whitespace-separated list of
790262a0982SChuck Lever  *			named NFSD listeners;
791262a0982SChuck Lever  *			return code is the size in bytes of the string
792262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
793262a0982SChuck Lever  *
794262a0982SChuck Lever  * OR
795262a0982SChuck Lever  *
796262a0982SChuck Lever  * Input:
797262a0982SChuck Lever  *			buf:		C string containing an unsigned
798262a0982SChuck Lever  *					integer value representing a bound
799262a0982SChuck Lever  *					but unconnected socket that is to be
800c71206a7SChuck Lever  *					used as an NFSD listener; listen(3)
801c71206a7SChuck Lever  *					must be called for a SOCK_STREAM
802c71206a7SChuck Lever  *					socket, otherwise it is ignored
803262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
804262a0982SChuck Lever  * Output:
805262a0982SChuck Lever  *	On success:	NFS service is started;
806262a0982SChuck Lever  *			passed-in buffer filled with a '\n'-terminated C
807262a0982SChuck Lever  *			string containing a unique alphanumeric name of
808262a0982SChuck Lever  *			the listener;
809262a0982SChuck Lever  *			return code is the size in bytes of the string
810262a0982SChuck Lever  *	On error:	return code is a negative errno value
811262a0982SChuck Lever  *
812262a0982SChuck Lever  * OR
813262a0982SChuck Lever  *
814262a0982SChuck Lever  * Input:
815262a0982SChuck Lever  *			buf:		C string containing a transport
816262a0982SChuck Lever  *					name and an unsigned integer value
817262a0982SChuck Lever  *					representing the port to listen on,
818262a0982SChuck Lever  *					separated by whitespace
819262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
820262a0982SChuck Lever  * Output:
821262a0982SChuck Lever  *	On success:	returns zero; NFS service is started
822262a0982SChuck Lever  *	On error:	return code is a negative errno value
823262a0982SChuck Lever  */
write_ports(struct file * file,char * buf,size_t size)824bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size)
825bedbdd8bSNeil Brown {
826bedbdd8bSNeil Brown 	ssize_t rv;
8273dd98a3bSJeff Layton 
828bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
829244c7d44SAl Viro 	rv = __write_ports(file, buf, size, netns(file));
830bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
831bedbdd8bSNeil Brown 	return rv;
832bedbdd8bSNeil Brown }
833bedbdd8bSNeil Brown 
834bedbdd8bSNeil Brown 
835596bbe53SNeilBrown int nfsd_max_blksize;
836596bbe53SNeilBrown 
837f2453978SChuck Lever /*
838262a0982SChuck Lever  * write_maxblksize - Set or report the current NFS blksize
839262a0982SChuck Lever  *
840262a0982SChuck Lever  * Input:
841262a0982SChuck Lever  *			buf:		ignored
842262a0982SChuck Lever  *			size:		zero
843262a0982SChuck Lever  *
844262a0982SChuck Lever  * OR
845262a0982SChuck Lever  *
846262a0982SChuck Lever  * Input:
847262a0982SChuck Lever  *			buf:		C string containing an unsigned
848262a0982SChuck Lever  *					integer value representing the new
849262a0982SChuck Lever  *					NFS blksize
850262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
851262a0982SChuck Lever  * Output:
852262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
853262a0982SChuck Lever  *			containing numeric value of the current NFS blksize
854262a0982SChuck Lever  *			setting;
855262a0982SChuck Lever  *			return code is the size in bytes of the string
856262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
857262a0982SChuck Lever  */
write_maxblksize(struct file * file,char * buf,size_t size)858596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
859596bbe53SNeilBrown {
860596bbe53SNeilBrown 	char *mesg = buf;
861244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
8629dd9845fSStanislav Kinsbursky 
863596bbe53SNeilBrown 	if (size > 0) {
864596bbe53SNeilBrown 		int bsize;
865596bbe53SNeilBrown 		int rv = get_int(&mesg, &bsize);
866596bbe53SNeilBrown 		if (rv)
867596bbe53SNeilBrown 			return rv;
86839d432fcSChuck Lever 		trace_nfsd_ctl_maxblksize(netns(file), bsize);
86939d432fcSChuck Lever 
870596bbe53SNeilBrown 		/* force bsize into allowed range and
871596bbe53SNeilBrown 		 * required alignment.
872596bbe53SNeilBrown 		 */
8733c7aa15dSKinglong Mee 		bsize = max_t(int, bsize, 1024);
8743c7aa15dSKinglong Mee 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
875596bbe53SNeilBrown 		bsize &= ~(1024-1);
876bedbdd8bSNeil Brown 		mutex_lock(&nfsd_mutex);
8779dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv) {
878bedbdd8bSNeil Brown 			mutex_unlock(&nfsd_mutex);
879596bbe53SNeilBrown 			return -EBUSY;
880596bbe53SNeilBrown 		}
881596bbe53SNeilBrown 		nfsd_max_blksize = bsize;
882bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
883596bbe53SNeilBrown 	}
884e06b6405SChuck Lever 
885e06b6405SChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
886e06b6405SChuck Lever 							nfsd_max_blksize);
887596bbe53SNeilBrown }
888596bbe53SNeilBrown 
889f2453978SChuck Lever /*
8905b8db00bSJeff Layton  * write_maxconn - Set or report the current max number of connections
8915b8db00bSJeff Layton  *
8925b8db00bSJeff Layton  * Input:
8935b8db00bSJeff Layton  *			buf:		ignored
8945b8db00bSJeff Layton  *			size:		zero
8955b8db00bSJeff Layton  * OR
8965b8db00bSJeff Layton  *
8975b8db00bSJeff Layton  * Input:
8985b8db00bSJeff Layton  *			buf:		C string containing an unsigned
8995b8db00bSJeff Layton  *					integer value representing the new
9005b8db00bSJeff Layton  *					number of max connections
9015b8db00bSJeff Layton  *			size:		non-zero length of C string in @buf
9025b8db00bSJeff Layton  * Output:
9035b8db00bSJeff Layton  *	On success:	passed-in buffer filled with '\n'-terminated C string
9045b8db00bSJeff Layton  *			containing numeric value of max_connections setting
9055b8db00bSJeff Layton  *			for this net namespace;
9065b8db00bSJeff Layton  *			return code is the size in bytes of the string
9075b8db00bSJeff Layton  *	On error:	return code is zero or a negative errno value
9085b8db00bSJeff Layton  */
write_maxconn(struct file * file,char * buf,size_t size)9095b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
9105b8db00bSJeff Layton {
9115b8db00bSJeff Layton 	char *mesg = buf;
912244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9135b8db00bSJeff Layton 	unsigned int maxconn = nn->max_connections;
9145b8db00bSJeff Layton 
9155b8db00bSJeff Layton 	if (size > 0) {
9165b8db00bSJeff Layton 		int rv = get_uint(&mesg, &maxconn);
9175b8db00bSJeff Layton 
9185b8db00bSJeff Layton 		if (rv)
9195b8db00bSJeff Layton 			return rv;
92039d432fcSChuck Lever 		trace_nfsd_ctl_maxconn(netns(file), maxconn);
9215b8db00bSJeff Layton 		nn->max_connections = maxconn;
9225b8db00bSJeff Layton 	}
9235b8db00bSJeff Layton 
9245b8db00bSJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
9255b8db00bSJeff Layton }
9265b8db00bSJeff Layton 
92770c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
__nfsd4_write_time(struct file * file,char * buf,size_t size,time64_t * time,struct nfsd_net * nn)9289dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
92920b7d86fSArnd Bergmann 				  time64_t *time, struct nfsd_net *nn)
9301da177e4SLinus Torvalds {
93139d432fcSChuck Lever 	struct dentry *dentry = file_dentry(file);
9321da177e4SLinus Torvalds 	char *mesg = buf;
933f0135740SJ. Bruce Fields 	int rv, i;
9341da177e4SLinus Torvalds 
9351da177e4SLinus Torvalds 	if (size > 0) {
9369dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
9373dd98a3bSJeff Layton 			return -EBUSY;
938f0135740SJ. Bruce Fields 		rv = get_int(&mesg, &i);
9391da177e4SLinus Torvalds 		if (rv)
9401da177e4SLinus Torvalds 			return rv;
94139d432fcSChuck Lever 		trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
94239d432fcSChuck Lever 				    dentry->d_name.len, i);
94339d432fcSChuck Lever 
944e7b184f1SJ. Bruce Fields 		/*
945e7b184f1SJ. Bruce Fields 		 * Some sanity checking.  We don't have a reason for
946e7b184f1SJ. Bruce Fields 		 * these particular numbers, but problems with the
947e7b184f1SJ. Bruce Fields 		 * extremes are:
948e7b184f1SJ. Bruce Fields 		 *	- Too short: the briefest network outage may
949e7b184f1SJ. Bruce Fields 		 *	  cause clients to lose all their locks.  Also,
950e7b184f1SJ. Bruce Fields 		 *	  the frequent polling may be wasteful.
951e7b184f1SJ. Bruce Fields 		 *	- Too long: do you really want reboot recovery
952e7b184f1SJ. Bruce Fields 		 *	  to take more than an hour?  Or to make other
953e7b184f1SJ. Bruce Fields 		 *	  clients wait an hour before being able to
954e7b184f1SJ. Bruce Fields 		 *	  revoke a dead client's locks?
955e7b184f1SJ. Bruce Fields 		 */
956f0135740SJ. Bruce Fields 		if (i < 10 || i > 3600)
9571da177e4SLinus Torvalds 			return -EINVAL;
958f0135740SJ. Bruce Fields 		*time = i;
9591da177e4SLinus Torvalds 	}
960e06b6405SChuck Lever 
96120b7d86fSArnd Bergmann 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
962f0135740SJ. Bruce Fields }
963f0135740SJ. Bruce Fields 
nfsd4_write_time(struct file * file,char * buf,size_t size,time64_t * time,struct nfsd_net * nn)9649dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
96520b7d86fSArnd Bergmann 				time64_t *time, struct nfsd_net *nn)
966f0135740SJ. Bruce Fields {
967f0135740SJ. Bruce Fields 	ssize_t rv;
968f0135740SJ. Bruce Fields 
969f0135740SJ. Bruce Fields 	mutex_lock(&nfsd_mutex);
9709dd9845fSStanislav Kinsbursky 	rv = __nfsd4_write_time(file, buf, size, time, nn);
971f0135740SJ. Bruce Fields 	mutex_unlock(&nfsd_mutex);
972f0135740SJ. Bruce Fields 	return rv;
9731da177e4SLinus Torvalds }
9741da177e4SLinus Torvalds 
975f2453978SChuck Lever /*
976262a0982SChuck Lever  * write_leasetime - Set or report the current NFSv4 lease time
977262a0982SChuck Lever  *
978262a0982SChuck Lever  * Input:
979262a0982SChuck Lever  *			buf:		ignored
980262a0982SChuck Lever  *			size:		zero
981262a0982SChuck Lever  *
982262a0982SChuck Lever  * OR
983262a0982SChuck Lever  *
984262a0982SChuck Lever  * Input:
985262a0982SChuck Lever  *			buf:		C string containing an unsigned
986262a0982SChuck Lever  *					integer value representing the new
987262a0982SChuck Lever  *					NFSv4 lease expiry time
988262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
989262a0982SChuck Lever  * Output:
990262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
991262a0982SChuck Lever  *			string containing unsigned integer value of the
992262a0982SChuck Lever  *			current lease expiry time;
993262a0982SChuck Lever  *			return code is the size in bytes of the string
994262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
995262a0982SChuck Lever  */
write_leasetime(struct file * file,char * buf,size_t size)9963dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
9973dd98a3bSJeff Layton {
998244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9999dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
10003dd98a3bSJeff Layton }
10013dd98a3bSJeff Layton 
1002f2453978SChuck Lever /*
1003efc4bb4fSJ. Bruce Fields  * write_gracetime - Set or report current NFSv4 grace period time
1004efc4bb4fSJ. Bruce Fields  *
1005efc4bb4fSJ. Bruce Fields  * As above, but sets the time of the NFSv4 grace period.
1006efc4bb4fSJ. Bruce Fields  *
1007efc4bb4fSJ. Bruce Fields  * Note this should never be set to less than the *previous*
1008efc4bb4fSJ. Bruce Fields  * lease-period time, but we don't try to enforce this.  (In the common
1009efc4bb4fSJ. Bruce Fields  * case (a new boot), we don't know what the previous lease time was
1010efc4bb4fSJ. Bruce Fields  * anyway.)
1011efc4bb4fSJ. Bruce Fields  */
write_gracetime(struct file * file,char * buf,size_t size)1012efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1013efc4bb4fSJ. Bruce Fields {
1014244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10159dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1016efc4bb4fSJ. Bruce Fields }
1017efc4bb4fSJ. Bruce Fields 
101874fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
__write_recoverydir(struct file * file,char * buf,size_t size,struct nfsd_net * nn)10199dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
10209dd9845fSStanislav Kinsbursky 				   struct nfsd_net *nn)
10210964a3d3SNeilBrown {
10220964a3d3SNeilBrown 	char *mesg = buf;
10230964a3d3SNeilBrown 	char *recdir;
10240964a3d3SNeilBrown 	int len, status;
10250964a3d3SNeilBrown 
10263dd98a3bSJeff Layton 	if (size > 0) {
10279dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
10283dd98a3bSJeff Layton 			return -EBUSY;
10293dd98a3bSJeff Layton 		if (size > PATH_MAX || buf[size-1] != '\n')
10300964a3d3SNeilBrown 			return -EINVAL;
10310964a3d3SNeilBrown 		buf[size-1] = 0;
10320964a3d3SNeilBrown 
10330964a3d3SNeilBrown 		recdir = mesg;
10340964a3d3SNeilBrown 		len = qword_get(&mesg, recdir, size);
10350964a3d3SNeilBrown 		if (len <= 0)
10360964a3d3SNeilBrown 			return -EINVAL;
103739d432fcSChuck Lever 		trace_nfsd_ctl_recoverydir(netns(file), recdir);
10380964a3d3SNeilBrown 
10390964a3d3SNeilBrown 		status = nfs4_reset_recoverydir(recdir);
104069049961SAndi Kleen 		if (status)
104169049961SAndi Kleen 			return status;
10423dd98a3bSJeff Layton 	}
10433d72ab8fSChuck Lever 
10443d72ab8fSChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
10453d72ab8fSChuck Lever 							nfs4_recoverydir());
10460964a3d3SNeilBrown }
10473dd98a3bSJeff Layton 
1048f2453978SChuck Lever /*
1049262a0982SChuck Lever  * write_recoverydir - Set or report the pathname of the recovery directory
1050262a0982SChuck Lever  *
1051262a0982SChuck Lever  * Input:
1052262a0982SChuck Lever  *			buf:		ignored
1053262a0982SChuck Lever  *			size:		zero
1054262a0982SChuck Lever  *
1055262a0982SChuck Lever  * OR
1056262a0982SChuck Lever  *
1057262a0982SChuck Lever  * Input:
1058262a0982SChuck Lever  *			buf:		C string containing the pathname
1059262a0982SChuck Lever  *					of the directory on a local file
1060262a0982SChuck Lever  *					system containing permanent NFSv4
1061262a0982SChuck Lever  *					recovery data
1062262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1063262a0982SChuck Lever  * Output:
1064262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
1065262a0982SChuck Lever  *			containing the current recovery pathname setting;
1066262a0982SChuck Lever  *			return code is the size in bytes of the string
1067262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1068262a0982SChuck Lever  */
write_recoverydir(struct file * file,char * buf,size_t size)10693dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
10703dd98a3bSJeff Layton {
10713dd98a3bSJeff Layton 	ssize_t rv;
1072244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10733dd98a3bSJeff Layton 
10743dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
10759dd9845fSStanislav Kinsbursky 	rv = __write_recoverydir(file, buf, size, nn);
10763dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
10773dd98a3bSJeff Layton 	return rv;
10783dd98a3bSJeff Layton }
107974fd4873SJeff Layton #endif
10803dd98a3bSJeff Layton 
1081f2453978SChuck Lever /*
10827f5ef2e9SJeff Layton  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
10837f5ef2e9SJeff Layton  *
10847f5ef2e9SJeff Layton  * Input:
10857f5ef2e9SJeff Layton  *			buf:		ignored
10867f5ef2e9SJeff Layton  *			size:		zero
10877f5ef2e9SJeff Layton  * OR
10887f5ef2e9SJeff Layton  *
10897f5ef2e9SJeff Layton  * Input:
10907f5ef2e9SJeff Layton  *			buf:		any value
10917f5ef2e9SJeff Layton  *			size:		non-zero length of C string in @buf
10927f5ef2e9SJeff Layton  * Output:
10937f5ef2e9SJeff Layton  *			passed-in buffer filled with "Y" or "N" with a newline
10947f5ef2e9SJeff Layton  *			and NULL-terminated C string. This indicates whether
10957f5ef2e9SJeff Layton  *			the grace period has ended in the current net
10967f5ef2e9SJeff Layton  *			namespace. Return code is the size in bytes of the
10977f5ef2e9SJeff Layton  *			string. Writing a string that starts with 'Y', 'y', or
10987f5ef2e9SJeff Layton  *			'1' to the file will end the grace period for nfsd's v4
10997f5ef2e9SJeff Layton  *			lock manager.
11007f5ef2e9SJeff Layton  */
write_v4_end_grace(struct file * file,char * buf,size_t size)11017f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
11027f5ef2e9SJeff Layton {
1103244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
11047f5ef2e9SJeff Layton 
11057f5ef2e9SJeff Layton 	if (size > 0) {
11067f5ef2e9SJeff Layton 		switch(buf[0]) {
11077f5ef2e9SJeff Layton 		case 'Y':
11087f5ef2e9SJeff Layton 		case 'y':
11097f5ef2e9SJeff Layton 		case '1':
1110dd838821SYihao Wu 			if (!nn->nfsd_serv)
111162a063b8SJ. Bruce Fields 				return -EBUSY;
111239d432fcSChuck Lever 			trace_nfsd_end_grace(netns(file));
11138073a98eSChuck Lever 			nfsd4_end_grace(nn);
11147f5ef2e9SJeff Layton 			break;
11157f5ef2e9SJeff Layton 		default:
11167f5ef2e9SJeff Layton 			return -EINVAL;
11177f5ef2e9SJeff Layton 		}
11187f5ef2e9SJeff Layton 	}
11197f5ef2e9SJeff Layton 
11207f5ef2e9SJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
11217f5ef2e9SJeff Layton 			 nn->grace_ended ? 'Y' : 'N');
11227f5ef2e9SJeff Layton }
11237f5ef2e9SJeff Layton 
112470c3b76cSNeilBrown #endif
11250964a3d3SNeilBrown 
11261da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
11271da177e4SLinus Torvalds /*
11281da177e4SLinus Torvalds  *	populating the filesystem.
11291da177e4SLinus Torvalds  */
11301da177e4SLinus Torvalds 
1131e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */
nfsd_get_inode(struct super_block * sb,umode_t mode)1132e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1133e8a79fb1SJ. Bruce Fields {
1134e8a79fb1SJ. Bruce Fields 	struct inode *inode = new_inode(sb);
1135e8a79fb1SJ. Bruce Fields 	if (!inode)
1136e8a79fb1SJ. Bruce Fields 		return NULL;
1137e8a79fb1SJ. Bruce Fields 	/* Following advice from simple_fill_super documentation: */
1138e8a79fb1SJ. Bruce Fields 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1139e8a79fb1SJ. Bruce Fields 	inode->i_mode = mode;
114011fec9b9SJeff Layton 	simple_inode_init_ts(inode);
1141e8a79fb1SJ. Bruce Fields 	switch (mode & S_IFMT) {
1142e8a79fb1SJ. Bruce Fields 	case S_IFDIR:
1143e8a79fb1SJ. Bruce Fields 		inode->i_fop = &simple_dir_operations;
1144e8a79fb1SJ. Bruce Fields 		inode->i_op = &simple_dir_inode_operations;
1145e8a79fb1SJ. Bruce Fields 		inc_nlink(inode);
114676c50eb7SGustavo A. R. Silva 		break;
11474df750c9SChuck Lever 	case S_IFLNK:
11484df750c9SChuck Lever 		inode->i_op = &simple_symlink_inode_operations;
11494df750c9SChuck Lever 		break;
1150e8a79fb1SJ. Bruce Fields 	default:
1151e8a79fb1SJ. Bruce Fields 		break;
1152e8a79fb1SJ. Bruce Fields 	}
1153e8a79fb1SJ. Bruce Fields 	return inode;
1154e8a79fb1SJ. Bruce Fields }
1155e8a79fb1SJ. Bruce Fields 
__nfsd_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode,struct nfsdfs_client * ncl)1156bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1157e8a79fb1SJ. Bruce Fields {
1158e8a79fb1SJ. Bruce Fields 	struct inode *inode;
1159e8a79fb1SJ. Bruce Fields 
1160e8a79fb1SJ. Bruce Fields 	inode = nfsd_get_inode(dir->i_sb, mode);
1161e8a79fb1SJ. Bruce Fields 	if (!inode)
1162e8a79fb1SJ. Bruce Fields 		return -ENOMEM;
1163bebd6997SJ. Bruce Fields 	if (ncl) {
1164bebd6997SJ. Bruce Fields 		inode->i_private = ncl;
1165bebd6997SJ. Bruce Fields 		kref_get(&ncl->cl_ref);
1166bebd6997SJ. Bruce Fields 	}
1167e8a79fb1SJ. Bruce Fields 	d_add(dentry, inode);
1168e8a79fb1SJ. Bruce Fields 	inc_nlink(dir);
1169e8a79fb1SJ. Bruce Fields 	fsnotify_mkdir(dir, dentry);
1170e8a79fb1SJ. Bruce Fields 	return 0;
1171e8a79fb1SJ. Bruce Fields }
1172e8a79fb1SJ. Bruce Fields 
nfsd_mkdir(struct dentry * parent,struct nfsdfs_client * ncl,char * name)1173e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1174e8a79fb1SJ. Bruce Fields {
1175e8a79fb1SJ. Bruce Fields 	struct inode *dir = parent->d_inode;
1176e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1177e8a79fb1SJ. Bruce Fields 	int ret = -ENOMEM;
1178e8a79fb1SJ. Bruce Fields 
1179e8a79fb1SJ. Bruce Fields 	inode_lock(dir);
1180e8a79fb1SJ. Bruce Fields 	dentry = d_alloc_name(parent, name);
1181e8a79fb1SJ. Bruce Fields 	if (!dentry)
1182e8a79fb1SJ. Bruce Fields 		goto out_err;
1183bebd6997SJ. Bruce Fields 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1184e8a79fb1SJ. Bruce Fields 	if (ret)
1185e8a79fb1SJ. Bruce Fields 		goto out_err;
1186e8a79fb1SJ. Bruce Fields out:
1187e8a79fb1SJ. Bruce Fields 	inode_unlock(dir);
1188e8a79fb1SJ. Bruce Fields 	return dentry;
1189e8a79fb1SJ. Bruce Fields out_err:
1190d6846bfbSTetsuo Handa 	dput(dentry);
1191e8a79fb1SJ. Bruce Fields 	dentry = ERR_PTR(ret);
1192e8a79fb1SJ. Bruce Fields 	goto out;
1193e8a79fb1SJ. Bruce Fields }
1194e8a79fb1SJ. Bruce Fields 
11954df750c9SChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_GSS)
__nfsd_symlink(struct inode * dir,struct dentry * dentry,umode_t mode,const char * content)11964df750c9SChuck Lever static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
11974df750c9SChuck Lever 			  umode_t mode, const char *content)
11984df750c9SChuck Lever {
11994df750c9SChuck Lever 	struct inode *inode;
12004df750c9SChuck Lever 
12014df750c9SChuck Lever 	inode = nfsd_get_inode(dir->i_sb, mode);
12024df750c9SChuck Lever 	if (!inode)
12034df750c9SChuck Lever 		return -ENOMEM;
12044df750c9SChuck Lever 
12054df750c9SChuck Lever 	inode->i_link = (char *)content;
12064df750c9SChuck Lever 	inode->i_size = strlen(content);
12074df750c9SChuck Lever 
12084df750c9SChuck Lever 	d_add(dentry, inode);
12094df750c9SChuck Lever 	inc_nlink(dir);
12104df750c9SChuck Lever 	fsnotify_create(dir, dentry);
12114df750c9SChuck Lever 	return 0;
12124df750c9SChuck Lever }
12134df750c9SChuck Lever 
12144df750c9SChuck Lever /*
12154df750c9SChuck Lever  * @content is assumed to be a NUL-terminated string that lives
12164df750c9SChuck Lever  * longer than the symlink itself.
12174df750c9SChuck Lever  */
_nfsd_symlink(struct dentry * parent,const char * name,const char * content)121839d432fcSChuck Lever static void _nfsd_symlink(struct dentry *parent, const char *name,
12194df750c9SChuck Lever 			  const char *content)
12204df750c9SChuck Lever {
12214df750c9SChuck Lever 	struct inode *dir = parent->d_inode;
12224df750c9SChuck Lever 	struct dentry *dentry;
12234b471a8bSChuck Lever 	int ret;
12244df750c9SChuck Lever 
12254df750c9SChuck Lever 	inode_lock(dir);
12264df750c9SChuck Lever 	dentry = d_alloc_name(parent, name);
12274df750c9SChuck Lever 	if (!dentry)
12284b471a8bSChuck Lever 		goto out;
12294df750c9SChuck Lever 	ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
12304df750c9SChuck Lever 	if (ret)
12314b471a8bSChuck Lever 		dput(dentry);
12324df750c9SChuck Lever out:
12334df750c9SChuck Lever 	inode_unlock(dir);
12344df750c9SChuck Lever }
12354df750c9SChuck Lever #else
_nfsd_symlink(struct dentry * parent,const char * name,const char * content)123639d432fcSChuck Lever static inline void _nfsd_symlink(struct dentry *parent, const char *name,
12374df750c9SChuck Lever 				 const char *content)
12384df750c9SChuck Lever {
12394df750c9SChuck Lever }
12404df750c9SChuck Lever 
12414df750c9SChuck Lever #endif
12424df750c9SChuck Lever 
clear_ncl(struct dentry * dentry)1243b7a14708SAl Viro static void clear_ncl(struct dentry *dentry)
1244e8a79fb1SJ. Bruce Fields {
1245b7a14708SAl Viro 	struct inode *inode = d_inode(dentry);
124697ad4031SJ. Bruce Fields 	struct nfsdfs_client *ncl = inode->i_private;
124797ad4031SJ. Bruce Fields 
1248b7a14708SAl Viro 	spin_lock(&inode->i_lock);
124997ad4031SJ. Bruce Fields 	inode->i_private = NULL;
1250b7a14708SAl Viro 	spin_unlock(&inode->i_lock);
125197ad4031SJ. Bruce Fields 	kref_put(&ncl->cl_ref, ncl->cl_release);
125297ad4031SJ. Bruce Fields }
125397ad4031SJ. Bruce Fields 
get_nfsdfs_client(struct inode * inode)125497ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
125597ad4031SJ. Bruce Fields {
125697ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc;
125797ad4031SJ. Bruce Fields 
1258b7a14708SAl Viro 	spin_lock(&inode->i_lock);
1259b7a14708SAl Viro 	nc = inode->i_private;
1260b7a14708SAl Viro 	if (nc)
1261b7a14708SAl Viro 		kref_get(&nc->cl_ref);
1262b7a14708SAl Viro 	spin_unlock(&inode->i_lock);
126397ad4031SJ. Bruce Fields 	return nc;
126497ad4031SJ. Bruce Fields }
126597ad4031SJ. Bruce Fields 
126697ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
126797ad4031SJ. Bruce Fields  * code instead. */
nfsdfs_create_files(struct dentry * root,const struct tree_descr * files,struct nfsdfs_client * ncl,struct dentry ** fdentries)126897ad4031SJ. Bruce Fields static  int nfsdfs_create_files(struct dentry *root,
1269472d155aSNeilBrown 				const struct tree_descr *files,
1270b7a14708SAl Viro 				struct nfsdfs_client *ncl,
1271472d155aSNeilBrown 				struct dentry **fdentries)
127297ad4031SJ. Bruce Fields {
127397ad4031SJ. Bruce Fields 	struct inode *dir = d_inode(root);
127497ad4031SJ. Bruce Fields 	struct inode *inode;
127597ad4031SJ. Bruce Fields 	struct dentry *dentry;
127697ad4031SJ. Bruce Fields 	int i;
127797ad4031SJ. Bruce Fields 
127897ad4031SJ. Bruce Fields 	inode_lock(dir);
127997ad4031SJ. Bruce Fields 	for (i = 0; files->name && files->name[0]; i++, files++) {
128097ad4031SJ. Bruce Fields 		dentry = d_alloc_name(root, files->name);
128197ad4031SJ. Bruce Fields 		if (!dentry)
128297ad4031SJ. Bruce Fields 			goto out;
128397ad4031SJ. Bruce Fields 		inode = nfsd_get_inode(d_inode(root)->i_sb,
128497ad4031SJ. Bruce Fields 					S_IFREG | files->mode);
128597ad4031SJ. Bruce Fields 		if (!inode) {
128697ad4031SJ. Bruce Fields 			dput(dentry);
128797ad4031SJ. Bruce Fields 			goto out;
128897ad4031SJ. Bruce Fields 		}
1289b7a14708SAl Viro 		kref_get(&ncl->cl_ref);
129097ad4031SJ. Bruce Fields 		inode->i_fop = files->ops;
1291b7a14708SAl Viro 		inode->i_private = ncl;
129297ad4031SJ. Bruce Fields 		d_add(dentry, inode);
129397ad4031SJ. Bruce Fields 		fsnotify_create(dir, dentry);
1294472d155aSNeilBrown 		if (fdentries)
1295472d155aSNeilBrown 			fdentries[i] = dentry;
129697ad4031SJ. Bruce Fields 	}
129797ad4031SJ. Bruce Fields 	inode_unlock(dir);
129897ad4031SJ. Bruce Fields 	return 0;
129997ad4031SJ. Bruce Fields out:
130097ad4031SJ. Bruce Fields 	inode_unlock(dir);
130197ad4031SJ. Bruce Fields 	return -ENOMEM;
130297ad4031SJ. Bruce Fields }
130397ad4031SJ. Bruce Fields 
130497ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */
nfsd_client_mkdir(struct nfsd_net * nn,struct nfsdfs_client * ncl,u32 id,const struct tree_descr * files,struct dentry ** fdentries)130597ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
130697ad4031SJ. Bruce Fields 				 struct nfsdfs_client *ncl, u32 id,
1307472d155aSNeilBrown 				 const struct tree_descr *files,
1308472d155aSNeilBrown 				 struct dentry **fdentries)
130997ad4031SJ. Bruce Fields {
131097ad4031SJ. Bruce Fields 	struct dentry *dentry;
1311e8a79fb1SJ. Bruce Fields 	char name[11];
131297ad4031SJ. Bruce Fields 	int ret;
1313e8a79fb1SJ. Bruce Fields 
1314bf5ed3e3SJ. Bruce Fields 	sprintf(name, "%u", id);
1315e8a79fb1SJ. Bruce Fields 
131697ad4031SJ. Bruce Fields 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
131797ad4031SJ. Bruce Fields 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
131897ad4031SJ. Bruce Fields 		return NULL;
1319b7a14708SAl Viro 	ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
132097ad4031SJ. Bruce Fields 	if (ret) {
132197ad4031SJ. Bruce Fields 		nfsd_client_rmdir(dentry);
132297ad4031SJ. Bruce Fields 		return NULL;
132397ad4031SJ. Bruce Fields 	}
132497ad4031SJ. Bruce Fields 	return dentry;
1325e8a79fb1SJ. Bruce Fields }
1326e8a79fb1SJ. Bruce Fields 
1327e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */
nfsd_client_rmdir(struct dentry * dentry)1328e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry)
1329e8a79fb1SJ. Bruce Fields {
1330b7a14708SAl Viro 	simple_recursive_removal(dentry, clear_ncl);
1331e8a79fb1SJ. Bruce Fields }
1332e8a79fb1SJ. Bruce Fields 
nfsd_fill_super(struct super_block * sb,struct fs_context * fc)133396a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
13341da177e4SLinus Torvalds {
1335e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1336e8a79fb1SJ. Bruce Fields 							nfsd_net_id);
1337e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1338e8a79fb1SJ. Bruce Fields 	int ret;
1339e8a79fb1SJ. Bruce Fields 
1340cda37124SEric Biggers 	static const struct tree_descr nfsd_files[] = {
134196d851c4SStanislav Kinsbursky 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
134220ad856eSAmir Goldstein 		/* Per-export io stats use same ops as exports file */
134320ad856eSAmir Goldstein 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1344e8e8753fSJ. Bruce Fields 		[NFSD_Export_features] = {"export_features",
13459beeaab8SChenXiaoSong 					&export_features_fops, S_IRUGO},
13464373ea84SWendy Cheng 		[NFSD_FO_UnlockIP] = {"unlock_ip",
13474373ea84SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
134817efa372SWendy Cheng 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
134917efa372SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
13501da177e4SLinus Torvalds 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
13511da177e4SLinus Torvalds 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1352eed2965aSGreg Banks 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
135303cf6c9fSGreg Banks 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
135464776611SChenXiaoSong 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
135564776611SChenXiaoSong 					&nfsd_reply_cache_stats_fops, S_IRUGO},
135670c3b76cSNeilBrown 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
135780212d59SNeilBrown 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1358596bbe53SNeilBrown 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
13595b8db00bSJeff Layton 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
13601342f9ddSChenXiaoSong 		[NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
13611da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4
13621da177e4SLinus Torvalds 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1363efc4bb4fSJ. Bruce Fields 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
13640770249bSNeilBrown #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
13650964a3d3SNeilBrown 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
13660770249bSNeilBrown #endif
13677f5ef2e9SJeff Layton 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
13681da177e4SLinus Torvalds #endif
13691da177e4SLinus Torvalds 		/* last one */ {""}
13701da177e4SLinus Torvalds 	};
137196a374a3SDavid Howells 
1372e8a79fb1SJ. Bruce Fields 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1373e8a79fb1SJ. Bruce Fields 	if (ret)
1374e8a79fb1SJ. Bruce Fields 		return ret;
137539d432fcSChuck Lever 	_nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
13764df750c9SChuck Lever 		      "/proc/net/rpc/gss_krb5_enctypes");
1377e8a79fb1SJ. Bruce Fields 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1378e8a79fb1SJ. Bruce Fields 	if (IS_ERR(dentry))
1379e8a79fb1SJ. Bruce Fields 		return PTR_ERR(dentry);
1380e8a79fb1SJ. Bruce Fields 	nn->nfsd_client_dir = dentry;
1381e8a79fb1SJ. Bruce Fields 	return 0;
13821da177e4SLinus Torvalds }
13831da177e4SLinus Torvalds 
nfsd_fs_get_tree(struct fs_context * fc)138496a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc)
13851da177e4SLinus Torvalds {
1386533770ccSAl Viro 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
138796a374a3SDavid Howells }
138896a374a3SDavid Howells 
nfsd_fs_free_fc(struct fs_context * fc)138996a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc)
139096a374a3SDavid Howells {
139196a374a3SDavid Howells 	if (fc->s_fs_info)
139296a374a3SDavid Howells 		put_net(fc->s_fs_info);
139396a374a3SDavid Howells }
139496a374a3SDavid Howells 
139596a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = {
139696a374a3SDavid Howells 	.free		= nfsd_fs_free_fc,
139796a374a3SDavid Howells 	.get_tree	= nfsd_fs_get_tree,
139896a374a3SDavid Howells };
139996a374a3SDavid Howells 
nfsd_init_fs_context(struct fs_context * fc)140096a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc)
140196a374a3SDavid Howells {
140296a374a3SDavid Howells 	put_user_ns(fc->user_ns);
140396a374a3SDavid Howells 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
140496a374a3SDavid Howells 	fc->ops = &nfsd_fs_context_ops;
140596a374a3SDavid Howells 	return 0;
140611f77942SStanislav Kinsbursky }
140711f77942SStanislav Kinsbursky 
nfsd_umount(struct super_block * sb)140811f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb)
140911f77942SStanislav Kinsbursky {
141011f77942SStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
141111f77942SStanislav Kinsbursky 
1412c6c7f2a8STrond Myklebust 	nfsd_shutdown_threads(net);
1413c6c7f2a8STrond Myklebust 
141411f77942SStanislav Kinsbursky 	kill_litter_super(sb);
141511f77942SStanislav Kinsbursky 	put_net(net);
14161da177e4SLinus Torvalds }
14171da177e4SLinus Torvalds 
14181da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = {
14191da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
14201da177e4SLinus Torvalds 	.name		= "nfsd",
142196a374a3SDavid Howells 	.init_fs_context = nfsd_init_fs_context,
142211f77942SStanislav Kinsbursky 	.kill_sb	= nfsd_umount,
14231da177e4SLinus Torvalds };
14247f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd");
14251da177e4SLinus Torvalds 
1426e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS
1427340086daSTom Rix 
exports_proc_open(struct inode * inode,struct file * file)1428340086daSTom Rix static int exports_proc_open(struct inode *inode, struct file *file)
1429340086daSTom Rix {
1430340086daSTom Rix 	return exports_net_open(current->nsproxy->net_ns, file);
1431340086daSTom Rix }
1432340086daSTom Rix 
1433340086daSTom Rix static const struct proc_ops exports_proc_ops = {
1434340086daSTom Rix 	.proc_open	= exports_proc_open,
1435340086daSTom Rix 	.proc_read	= seq_read,
1436340086daSTom Rix 	.proc_lseek	= seq_lseek,
1437340086daSTom Rix 	.proc_release	= seq_release,
1438340086daSTom Rix };
1439340086daSTom Rix 
create_proc_exports_entry(void)1440e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1441e331f606SJ. Bruce Fields {
1442e331f606SJ. Bruce Fields 	struct proc_dir_entry *entry;
1443e331f606SJ. Bruce Fields 
1444e331f606SJ. Bruce Fields 	entry = proc_mkdir("fs/nfs", NULL);
1445e331f606SJ. Bruce Fields 	if (!entry)
1446e331f606SJ. Bruce Fields 		return -ENOMEM;
144797a32539SAlexey Dobriyan 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1448ff7c4b36Sfanchaoting 	if (!entry) {
1449ff7c4b36Sfanchaoting 		remove_proc_entry("fs/nfs", NULL);
1450e331f606SJ. Bruce Fields 		return -ENOMEM;
1451ff7c4b36Sfanchaoting 	}
1452e331f606SJ. Bruce Fields 	return 0;
1453e331f606SJ. Bruce Fields }
1454e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */
create_proc_exports_entry(void)1455e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1456e331f606SJ. Bruce Fields {
1457e331f606SJ. Bruce Fields 	return 0;
1458e331f606SJ. Bruce Fields }
1459e331f606SJ. Bruce Fields #endif
1460e331f606SJ. Bruce Fields 
1461c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id;
14625717e012SStanislav Kinsbursky 
nfsd_genl_rpc_status_compose_msg(struct sk_buff * skb,struct netlink_callback * cb,struct nfsd_genl_rqstp * rqstp)1463bd9d6a3eSLorenzo Bianconi static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1464bd9d6a3eSLorenzo Bianconi 					    struct netlink_callback *cb,
1465bd9d6a3eSLorenzo Bianconi 					    struct nfsd_genl_rqstp *rqstp)
1466bd9d6a3eSLorenzo Bianconi {
1467bd9d6a3eSLorenzo Bianconi 	void *hdr;
1468bd9d6a3eSLorenzo Bianconi 	u32 i;
1469bd9d6a3eSLorenzo Bianconi 
1470bd9d6a3eSLorenzo Bianconi 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1471bd9d6a3eSLorenzo Bianconi 			  &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1472bd9d6a3eSLorenzo Bianconi 	if (!hdr)
1473bd9d6a3eSLorenzo Bianconi 		return -ENOBUFS;
1474bd9d6a3eSLorenzo Bianconi 
1475bd9d6a3eSLorenzo Bianconi 	if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1476bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1477bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1478bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1479bd9d6a3eSLorenzo Bianconi 	    nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1480bd9d6a3eSLorenzo Bianconi 	    nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1481bd9d6a3eSLorenzo Bianconi 			ktime_to_us(rqstp->rq_stime),
1482bd9d6a3eSLorenzo Bianconi 			NFSD_A_RPC_STATUS_PAD))
1483bd9d6a3eSLorenzo Bianconi 		return -ENOBUFS;
1484bd9d6a3eSLorenzo Bianconi 
1485bd9d6a3eSLorenzo Bianconi 	switch (rqstp->rq_saddr.sa_family) {
1486bd9d6a3eSLorenzo Bianconi 	case AF_INET: {
1487bd9d6a3eSLorenzo Bianconi 		const struct sockaddr_in *s_in, *d_in;
1488bd9d6a3eSLorenzo Bianconi 
1489bd9d6a3eSLorenzo Bianconi 		s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1490bd9d6a3eSLorenzo Bianconi 		d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1491bd9d6a3eSLorenzo Bianconi 		if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1492bd9d6a3eSLorenzo Bianconi 				    s_in->sin_addr.s_addr) ||
1493bd9d6a3eSLorenzo Bianconi 		    nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1494bd9d6a3eSLorenzo Bianconi 				    d_in->sin_addr.s_addr) ||
1495bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1496bd9d6a3eSLorenzo Bianconi 				 s_in->sin_port) ||
1497bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1498bd9d6a3eSLorenzo Bianconi 				 d_in->sin_port))
1499bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1500bd9d6a3eSLorenzo Bianconi 		break;
1501bd9d6a3eSLorenzo Bianconi 	}
1502bd9d6a3eSLorenzo Bianconi 	case AF_INET6: {
1503bd9d6a3eSLorenzo Bianconi 		const struct sockaddr_in6 *s_in, *d_in;
1504bd9d6a3eSLorenzo Bianconi 
1505bd9d6a3eSLorenzo Bianconi 		s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1506bd9d6a3eSLorenzo Bianconi 		d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1507bd9d6a3eSLorenzo Bianconi 		if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1508bd9d6a3eSLorenzo Bianconi 				     &s_in->sin6_addr) ||
1509bd9d6a3eSLorenzo Bianconi 		    nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1510bd9d6a3eSLorenzo Bianconi 				     &d_in->sin6_addr) ||
1511bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1512bd9d6a3eSLorenzo Bianconi 				 s_in->sin6_port) ||
1513bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1514bd9d6a3eSLorenzo Bianconi 				 d_in->sin6_port))
1515bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1516bd9d6a3eSLorenzo Bianconi 		break;
1517bd9d6a3eSLorenzo Bianconi 	}
1518bd9d6a3eSLorenzo Bianconi 	}
1519bd9d6a3eSLorenzo Bianconi 
1520bd9d6a3eSLorenzo Bianconi 	for (i = 0; i < rqstp->rq_opcnt; i++)
1521bd9d6a3eSLorenzo Bianconi 		if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1522bd9d6a3eSLorenzo Bianconi 				rqstp->rq_opnum[i]))
1523bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1524bd9d6a3eSLorenzo Bianconi 
1525bd9d6a3eSLorenzo Bianconi 	genlmsg_end(skb, hdr);
152613727f85SLorenzo Bianconi 	return 0;
152713727f85SLorenzo Bianconi }
152813727f85SLorenzo Bianconi 
1529bd9d6a3eSLorenzo Bianconi /**
1530bd9d6a3eSLorenzo Bianconi  * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1531bd9d6a3eSLorenzo Bianconi  * @skb: reply buffer
1532bd9d6a3eSLorenzo Bianconi  * @cb: netlink metadata and command arguments
1533bd9d6a3eSLorenzo Bianconi  *
1534bd9d6a3eSLorenzo Bianconi  * Returns the size of the reply or a negative errno.
1535bd9d6a3eSLorenzo Bianconi  */
nfsd_nl_rpc_status_get_dumpit(struct sk_buff * skb,struct netlink_callback * cb)153613727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
153713727f85SLorenzo Bianconi 				  struct netlink_callback *cb)
153813727f85SLorenzo Bianconi {
1539bd9d6a3eSLorenzo Bianconi 	int i, ret, rqstp_index = 0;
1540*da2c8fefSLorenzo Bianconi 	struct nfsd_net *nn;
1541*da2c8fefSLorenzo Bianconi 
1542*da2c8fefSLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
1543*da2c8fefSLorenzo Bianconi 
1544*da2c8fefSLorenzo Bianconi 	nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1545*da2c8fefSLorenzo Bianconi 	if (!nn->nfsd_serv) {
1546*da2c8fefSLorenzo Bianconi 		ret = -ENODEV;
1547*da2c8fefSLorenzo Bianconi 		goto out_unlock;
1548*da2c8fefSLorenzo Bianconi 	}
1549bd9d6a3eSLorenzo Bianconi 
1550bd9d6a3eSLorenzo Bianconi 	rcu_read_lock();
1551bd9d6a3eSLorenzo Bianconi 
1552bd9d6a3eSLorenzo Bianconi 	for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1553bd9d6a3eSLorenzo Bianconi 		struct svc_rqst *rqstp;
1554bd9d6a3eSLorenzo Bianconi 
1555bd9d6a3eSLorenzo Bianconi 		if (i < cb->args[0]) /* already consumed */
1556bd9d6a3eSLorenzo Bianconi 			continue;
1557bd9d6a3eSLorenzo Bianconi 
1558bd9d6a3eSLorenzo Bianconi 		rqstp_index = 0;
1559bd9d6a3eSLorenzo Bianconi 		list_for_each_entry_rcu(rqstp,
1560bd9d6a3eSLorenzo Bianconi 				&nn->nfsd_serv->sv_pools[i].sp_all_threads,
1561bd9d6a3eSLorenzo Bianconi 				rq_all) {
1562bd9d6a3eSLorenzo Bianconi 			struct nfsd_genl_rqstp genl_rqstp;
1563bd9d6a3eSLorenzo Bianconi 			unsigned int status_counter;
1564bd9d6a3eSLorenzo Bianconi 
1565bd9d6a3eSLorenzo Bianconi 			if (rqstp_index++ < cb->args[1]) /* already consumed */
1566bd9d6a3eSLorenzo Bianconi 				continue;
1567bd9d6a3eSLorenzo Bianconi 			/*
1568bd9d6a3eSLorenzo Bianconi 			 * Acquire rq_status_counter before parsing the rqst
1569bd9d6a3eSLorenzo Bianconi 			 * fields. rq_status_counter is set to an odd value in
1570bd9d6a3eSLorenzo Bianconi 			 * order to notify the consumers the rqstp fields are
1571bd9d6a3eSLorenzo Bianconi 			 * meaningful.
1572bd9d6a3eSLorenzo Bianconi 			 */
1573bd9d6a3eSLorenzo Bianconi 			status_counter =
1574bd9d6a3eSLorenzo Bianconi 				smp_load_acquire(&rqstp->rq_status_counter);
1575bd9d6a3eSLorenzo Bianconi 			if (!(status_counter & 1))
1576bd9d6a3eSLorenzo Bianconi 				continue;
1577bd9d6a3eSLorenzo Bianconi 
1578bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_xid = rqstp->rq_xid;
1579bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_flags = rqstp->rq_flags;
1580bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_vers = rqstp->rq_vers;
1581bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_prog = rqstp->rq_prog;
1582bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_proc = rqstp->rq_proc;
1583bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_stime = rqstp->rq_stime;
1584bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_opcnt = 0;
1585bd9d6a3eSLorenzo Bianconi 			memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1586bd9d6a3eSLorenzo Bianconi 			       sizeof(struct sockaddr));
1587bd9d6a3eSLorenzo Bianconi 			memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1588bd9d6a3eSLorenzo Bianconi 			       sizeof(struct sockaddr));
1589bd9d6a3eSLorenzo Bianconi 
1590bd9d6a3eSLorenzo Bianconi #ifdef CONFIG_NFSD_V4
1591bd9d6a3eSLorenzo Bianconi 			if (rqstp->rq_vers == NFS4_VERSION &&
1592bd9d6a3eSLorenzo Bianconi 			    rqstp->rq_proc == NFSPROC4_COMPOUND) {
1593bd9d6a3eSLorenzo Bianconi 				/* NFSv4 compound */
1594bd9d6a3eSLorenzo Bianconi 				struct nfsd4_compoundargs *args;
1595bd9d6a3eSLorenzo Bianconi 				int j;
1596bd9d6a3eSLorenzo Bianconi 
1597bd9d6a3eSLorenzo Bianconi 				args = rqstp->rq_argp;
1598bd9d6a3eSLorenzo Bianconi 				genl_rqstp.rq_opcnt = args->opcnt;
1599bd9d6a3eSLorenzo Bianconi 				for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1600bd9d6a3eSLorenzo Bianconi 					genl_rqstp.rq_opnum[j] =
1601bd9d6a3eSLorenzo Bianconi 						args->ops[j].opnum;
1602bd9d6a3eSLorenzo Bianconi 			}
1603bd9d6a3eSLorenzo Bianconi #endif /* CONFIG_NFSD_V4 */
1604bd9d6a3eSLorenzo Bianconi 
1605bd9d6a3eSLorenzo Bianconi 			/*
1606bd9d6a3eSLorenzo Bianconi 			 * Acquire rq_status_counter before reporting the rqst
1607bd9d6a3eSLorenzo Bianconi 			 * fields to the user.
1608bd9d6a3eSLorenzo Bianconi 			 */
1609bd9d6a3eSLorenzo Bianconi 			if (smp_load_acquire(&rqstp->rq_status_counter) !=
1610bd9d6a3eSLorenzo Bianconi 			    status_counter)
1611bd9d6a3eSLorenzo Bianconi 				continue;
1612bd9d6a3eSLorenzo Bianconi 
1613bd9d6a3eSLorenzo Bianconi 			ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1614bd9d6a3eSLorenzo Bianconi 							       &genl_rqstp);
1615bd9d6a3eSLorenzo Bianconi 			if (ret)
1616bd9d6a3eSLorenzo Bianconi 				goto out;
1617bd9d6a3eSLorenzo Bianconi 		}
161813727f85SLorenzo Bianconi 	}
161913727f85SLorenzo Bianconi 
1620bd9d6a3eSLorenzo Bianconi 	cb->args[0] = i;
1621bd9d6a3eSLorenzo Bianconi 	cb->args[1] = rqstp_index;
1622bd9d6a3eSLorenzo Bianconi 	ret = skb->len;
1623bd9d6a3eSLorenzo Bianconi out:
1624bd9d6a3eSLorenzo Bianconi 	rcu_read_unlock();
1625*da2c8fefSLorenzo Bianconi out_unlock:
1626bd9d6a3eSLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
1627bd9d6a3eSLorenzo Bianconi 
1628*da2c8fefSLorenzo Bianconi 	return ret;
162913727f85SLorenzo Bianconi }
163013727f85SLorenzo Bianconi 
16315e092be7SChuck Lever /**
1632924f4fb0SLorenzo Bianconi  * nfsd_nl_threads_set_doit - set the number of running threads
1633924f4fb0SLorenzo Bianconi  * @skb: reply buffer
1634924f4fb0SLorenzo Bianconi  * @info: netlink metadata and command arguments
1635924f4fb0SLorenzo Bianconi  *
1636924f4fb0SLorenzo Bianconi  * Return 0 on success or a negative errno.
1637924f4fb0SLorenzo Bianconi  */
nfsd_nl_threads_set_doit(struct sk_buff * skb,struct genl_info * info)1638924f4fb0SLorenzo Bianconi int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1639924f4fb0SLorenzo Bianconi {
1640924f4fb0SLorenzo Bianconi 	int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem;
1641924f4fb0SLorenzo Bianconi 	struct net *net = genl_info_net(info);
1642924f4fb0SLorenzo Bianconi 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1643924f4fb0SLorenzo Bianconi 	const struct nlattr *attr;
1644924f4fb0SLorenzo Bianconi 	const char *scope = NULL;
1645924f4fb0SLorenzo Bianconi 
1646924f4fb0SLorenzo Bianconi 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1647924f4fb0SLorenzo Bianconi 		return -EINVAL;
1648924f4fb0SLorenzo Bianconi 
1649924f4fb0SLorenzo Bianconi 	/* count number of SERVER_THREADS values */
1650924f4fb0SLorenzo Bianconi 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1651924f4fb0SLorenzo Bianconi 		if (nla_type(attr) == NFSD_A_SERVER_THREADS)
1652924f4fb0SLorenzo Bianconi 			count++;
1653924f4fb0SLorenzo Bianconi 	}
1654924f4fb0SLorenzo Bianconi 
1655924f4fb0SLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
1656924f4fb0SLorenzo Bianconi 
1657924f4fb0SLorenzo Bianconi 	nrpools = nfsd_nrpools(net);
1658924f4fb0SLorenzo Bianconi 	if (nrpools && count > nrpools)
1659924f4fb0SLorenzo Bianconi 		count = nrpools;
1660924f4fb0SLorenzo Bianconi 
1661924f4fb0SLorenzo Bianconi 	/* XXX: make this handle non-global pool-modes */
1662924f4fb0SLorenzo Bianconi 	if (count > 1)
1663924f4fb0SLorenzo Bianconi 		goto out_unlock;
1664924f4fb0SLorenzo Bianconi 
1665924f4fb0SLorenzo Bianconi 	nthreads = nla_get_u32(info->attrs[NFSD_A_SERVER_THREADS]);
1666924f4fb0SLorenzo Bianconi 	if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1667924f4fb0SLorenzo Bianconi 	    info->attrs[NFSD_A_SERVER_LEASETIME] ||
1668924f4fb0SLorenzo Bianconi 	    info->attrs[NFSD_A_SERVER_SCOPE]) {
1669924f4fb0SLorenzo Bianconi 		ret = -EBUSY;
1670924f4fb0SLorenzo Bianconi 		if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1671924f4fb0SLorenzo Bianconi 			goto out_unlock;
1672924f4fb0SLorenzo Bianconi 
1673924f4fb0SLorenzo Bianconi 		ret = -EINVAL;
1674924f4fb0SLorenzo Bianconi 		attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1675924f4fb0SLorenzo Bianconi 		if (attr) {
1676924f4fb0SLorenzo Bianconi 			u32 gracetime = nla_get_u32(attr);
1677924f4fb0SLorenzo Bianconi 
1678924f4fb0SLorenzo Bianconi 			if (gracetime < 10 || gracetime > 3600)
1679924f4fb0SLorenzo Bianconi 				goto out_unlock;
1680924f4fb0SLorenzo Bianconi 
1681924f4fb0SLorenzo Bianconi 			nn->nfsd4_grace = gracetime;
1682924f4fb0SLorenzo Bianconi 		}
1683924f4fb0SLorenzo Bianconi 
1684924f4fb0SLorenzo Bianconi 		attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1685924f4fb0SLorenzo Bianconi 		if (attr) {
1686924f4fb0SLorenzo Bianconi 			u32 leasetime = nla_get_u32(attr);
1687924f4fb0SLorenzo Bianconi 
1688924f4fb0SLorenzo Bianconi 			if (leasetime < 10 || leasetime > 3600)
1689924f4fb0SLorenzo Bianconi 				goto out_unlock;
1690924f4fb0SLorenzo Bianconi 
1691924f4fb0SLorenzo Bianconi 			nn->nfsd4_lease = leasetime;
1692924f4fb0SLorenzo Bianconi 		}
1693924f4fb0SLorenzo Bianconi 
1694924f4fb0SLorenzo Bianconi 		attr = info->attrs[NFSD_A_SERVER_SCOPE];
1695924f4fb0SLorenzo Bianconi 		if (attr)
1696924f4fb0SLorenzo Bianconi 			scope = nla_data(attr);
1697924f4fb0SLorenzo Bianconi 	}
1698924f4fb0SLorenzo Bianconi 
1699924f4fb0SLorenzo Bianconi 	ret = nfsd_svc(nthreads, net, get_current_cred(), scope);
1700924f4fb0SLorenzo Bianconi 
1701924f4fb0SLorenzo Bianconi out_unlock:
1702924f4fb0SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
1703924f4fb0SLorenzo Bianconi 
1704924f4fb0SLorenzo Bianconi 	return ret == nthreads ? 0 : ret;
1705924f4fb0SLorenzo Bianconi }
1706924f4fb0SLorenzo Bianconi 
1707924f4fb0SLorenzo Bianconi /**
1708924f4fb0SLorenzo Bianconi  * nfsd_nl_threads_get_doit - get the number of running threads
1709924f4fb0SLorenzo Bianconi  * @skb: reply buffer
1710924f4fb0SLorenzo Bianconi  * @info: netlink metadata and command arguments
1711924f4fb0SLorenzo Bianconi  *
1712924f4fb0SLorenzo Bianconi  * Return 0 on success or a negative errno.
1713924f4fb0SLorenzo Bianconi  */
nfsd_nl_threads_get_doit(struct sk_buff * skb,struct genl_info * info)1714924f4fb0SLorenzo Bianconi int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1715924f4fb0SLorenzo Bianconi {
1716924f4fb0SLorenzo Bianconi 	struct net *net = genl_info_net(info);
1717924f4fb0SLorenzo Bianconi 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1718924f4fb0SLorenzo Bianconi 	void *hdr;
1719924f4fb0SLorenzo Bianconi 	int err;
1720924f4fb0SLorenzo Bianconi 
1721924f4fb0SLorenzo Bianconi 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1722924f4fb0SLorenzo Bianconi 	if (!skb)
1723924f4fb0SLorenzo Bianconi 		return -ENOMEM;
1724924f4fb0SLorenzo Bianconi 
1725924f4fb0SLorenzo Bianconi 	hdr = genlmsg_iput(skb, info);
1726924f4fb0SLorenzo Bianconi 	if (!hdr) {
1727924f4fb0SLorenzo Bianconi 		err = -EMSGSIZE;
1728924f4fb0SLorenzo Bianconi 		goto err_free_msg;
1729924f4fb0SLorenzo Bianconi 	}
1730924f4fb0SLorenzo Bianconi 
1731924f4fb0SLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
1732924f4fb0SLorenzo Bianconi 
1733924f4fb0SLorenzo Bianconi 	err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1734924f4fb0SLorenzo Bianconi 			  nn->nfsd4_grace) ||
1735924f4fb0SLorenzo Bianconi 	      nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1736924f4fb0SLorenzo Bianconi 			  nn->nfsd4_lease) ||
1737924f4fb0SLorenzo Bianconi 	      nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1738924f4fb0SLorenzo Bianconi 			  nn->nfsd_name);
1739924f4fb0SLorenzo Bianconi 	if (err)
1740924f4fb0SLorenzo Bianconi 		goto err_unlock;
1741924f4fb0SLorenzo Bianconi 
1742924f4fb0SLorenzo Bianconi 	if (nn->nfsd_serv) {
1743924f4fb0SLorenzo Bianconi 		int i;
1744924f4fb0SLorenzo Bianconi 
1745924f4fb0SLorenzo Bianconi 		for (i = 0; i < nfsd_nrpools(net); ++i) {
1746924f4fb0SLorenzo Bianconi 			struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1747924f4fb0SLorenzo Bianconi 
1748924f4fb0SLorenzo Bianconi 			err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1749924f4fb0SLorenzo Bianconi 					  atomic_read(&sp->sp_nrthreads));
1750924f4fb0SLorenzo Bianconi 			if (err)
1751924f4fb0SLorenzo Bianconi 				goto err_unlock;
1752924f4fb0SLorenzo Bianconi 		}
1753924f4fb0SLorenzo Bianconi 	} else {
1754924f4fb0SLorenzo Bianconi 		err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1755924f4fb0SLorenzo Bianconi 		if (err)
1756924f4fb0SLorenzo Bianconi 			goto err_unlock;
1757924f4fb0SLorenzo Bianconi 	}
1758924f4fb0SLorenzo Bianconi 
1759924f4fb0SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
1760924f4fb0SLorenzo Bianconi 
1761924f4fb0SLorenzo Bianconi 	genlmsg_end(skb, hdr);
1762924f4fb0SLorenzo Bianconi 
1763924f4fb0SLorenzo Bianconi 	return genlmsg_reply(skb, info);
1764924f4fb0SLorenzo Bianconi 
1765924f4fb0SLorenzo Bianconi err_unlock:
1766924f4fb0SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
1767924f4fb0SLorenzo Bianconi err_free_msg:
1768924f4fb0SLorenzo Bianconi 	nlmsg_free(skb);
1769924f4fb0SLorenzo Bianconi 
1770924f4fb0SLorenzo Bianconi 	return err;
1771924f4fb0SLorenzo Bianconi }
1772924f4fb0SLorenzo Bianconi 
1773924f4fb0SLorenzo Bianconi /**
17745a939beaSLorenzo Bianconi  * nfsd_nl_version_set_doit - set the nfs enabled versions
17755a939beaSLorenzo Bianconi  * @skb: reply buffer
17765a939beaSLorenzo Bianconi  * @info: netlink metadata and command arguments
17775a939beaSLorenzo Bianconi  *
17785a939beaSLorenzo Bianconi  * Return 0 on success or a negative errno.
17795a939beaSLorenzo Bianconi  */
nfsd_nl_version_set_doit(struct sk_buff * skb,struct genl_info * info)17805a939beaSLorenzo Bianconi int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
17815a939beaSLorenzo Bianconi {
17825a939beaSLorenzo Bianconi 	const struct nlattr *attr;
17835a939beaSLorenzo Bianconi 	struct nfsd_net *nn;
17845a939beaSLorenzo Bianconi 	int i, rem;
17855a939beaSLorenzo Bianconi 
17865a939beaSLorenzo Bianconi 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
17875a939beaSLorenzo Bianconi 		return -EINVAL;
17885a939beaSLorenzo Bianconi 
17895a939beaSLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
17905a939beaSLorenzo Bianconi 
17915a939beaSLorenzo Bianconi 	nn = net_generic(genl_info_net(info), nfsd_net_id);
17925a939beaSLorenzo Bianconi 	if (nn->nfsd_serv) {
17935a939beaSLorenzo Bianconi 		mutex_unlock(&nfsd_mutex);
17945a939beaSLorenzo Bianconi 		return -EBUSY;
17955a939beaSLorenzo Bianconi 	}
17965a939beaSLorenzo Bianconi 
17975a939beaSLorenzo Bianconi 	/* clear current supported versions. */
17985a939beaSLorenzo Bianconi 	nfsd_vers(nn, 2, NFSD_CLEAR);
17995a939beaSLorenzo Bianconi 	nfsd_vers(nn, 3, NFSD_CLEAR);
18005a939beaSLorenzo Bianconi 	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
18015a939beaSLorenzo Bianconi 		nfsd_minorversion(nn, i, NFSD_CLEAR);
18025a939beaSLorenzo Bianconi 
18035a939beaSLorenzo Bianconi 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
18045a939beaSLorenzo Bianconi 		struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
18055a939beaSLorenzo Bianconi 		u32 major, minor = 0;
18065a939beaSLorenzo Bianconi 		bool enabled;
18075a939beaSLorenzo Bianconi 
18085a939beaSLorenzo Bianconi 		if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION)
18095a939beaSLorenzo Bianconi 			continue;
18105a939beaSLorenzo Bianconi 
18115a939beaSLorenzo Bianconi 		if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
18125a939beaSLorenzo Bianconi 				     nfsd_version_nl_policy, info->extack) < 0)
18135a939beaSLorenzo Bianconi 			continue;
18145a939beaSLorenzo Bianconi 
18155a939beaSLorenzo Bianconi 		if (!tb[NFSD_A_VERSION_MAJOR])
18165a939beaSLorenzo Bianconi 			continue;
18175a939beaSLorenzo Bianconi 
18185a939beaSLorenzo Bianconi 		major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
18195a939beaSLorenzo Bianconi 		if (tb[NFSD_A_VERSION_MINOR])
18205a939beaSLorenzo Bianconi 			minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
18215a939beaSLorenzo Bianconi 
18225a939beaSLorenzo Bianconi 		enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
18235a939beaSLorenzo Bianconi 
18245a939beaSLorenzo Bianconi 		switch (major) {
18255a939beaSLorenzo Bianconi 		case 4:
18265a939beaSLorenzo Bianconi 			nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
18275a939beaSLorenzo Bianconi 			break;
18285a939beaSLorenzo Bianconi 		case 3:
18295a939beaSLorenzo Bianconi 		case 2:
18305a939beaSLorenzo Bianconi 			if (!minor)
18315a939beaSLorenzo Bianconi 				nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
18325a939beaSLorenzo Bianconi 			break;
18335a939beaSLorenzo Bianconi 		default:
18345a939beaSLorenzo Bianconi 			break;
18355a939beaSLorenzo Bianconi 		}
18365a939beaSLorenzo Bianconi 	}
18375a939beaSLorenzo Bianconi 
18385a939beaSLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
18395a939beaSLorenzo Bianconi 
18405a939beaSLorenzo Bianconi 	return 0;
18415a939beaSLorenzo Bianconi }
18425a939beaSLorenzo Bianconi 
18435a939beaSLorenzo Bianconi /**
18445a939beaSLorenzo Bianconi  * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
18455a939beaSLorenzo Bianconi  * @skb: reply buffer
18465a939beaSLorenzo Bianconi  * @info: netlink metadata and command arguments
18475a939beaSLorenzo Bianconi  *
18485a939beaSLorenzo Bianconi  * Return 0 on success or a negative errno.
18495a939beaSLorenzo Bianconi  */
nfsd_nl_version_get_doit(struct sk_buff * skb,struct genl_info * info)18505a939beaSLorenzo Bianconi int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
18515a939beaSLorenzo Bianconi {
18525a939beaSLorenzo Bianconi 	struct nfsd_net *nn;
18535a939beaSLorenzo Bianconi 	int i, err;
18545a939beaSLorenzo Bianconi 	void *hdr;
18555a939beaSLorenzo Bianconi 
18565a939beaSLorenzo Bianconi 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
18575a939beaSLorenzo Bianconi 	if (!skb)
18585a939beaSLorenzo Bianconi 		return -ENOMEM;
18595a939beaSLorenzo Bianconi 
18605a939beaSLorenzo Bianconi 	hdr = genlmsg_iput(skb, info);
18615a939beaSLorenzo Bianconi 	if (!hdr) {
18625a939beaSLorenzo Bianconi 		err = -EMSGSIZE;
18635a939beaSLorenzo Bianconi 		goto err_free_msg;
18645a939beaSLorenzo Bianconi 	}
18655a939beaSLorenzo Bianconi 
18665a939beaSLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
18675a939beaSLorenzo Bianconi 	nn = net_generic(genl_info_net(info), nfsd_net_id);
18685a939beaSLorenzo Bianconi 
18695a939beaSLorenzo Bianconi 	for (i = 2; i <= 4; i++) {
18705a939beaSLorenzo Bianconi 		int j;
18715a939beaSLorenzo Bianconi 
18725a939beaSLorenzo Bianconi 		for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
18735a939beaSLorenzo Bianconi 			struct nlattr *attr;
18745a939beaSLorenzo Bianconi 
18755a939beaSLorenzo Bianconi 			/* Don't record any versions the kernel doesn't have
18765a939beaSLorenzo Bianconi 			 * compiled in
18775a939beaSLorenzo Bianconi 			 */
18785a939beaSLorenzo Bianconi 			if (!nfsd_support_version(i))
18795a939beaSLorenzo Bianconi 				continue;
18805a939beaSLorenzo Bianconi 
18815a939beaSLorenzo Bianconi 			/* NFSv{2,3} does not support minor numbers */
18825a939beaSLorenzo Bianconi 			if (i < 4 && j)
18835a939beaSLorenzo Bianconi 				continue;
18845a939beaSLorenzo Bianconi 
18855a939beaSLorenzo Bianconi 			attr = nla_nest_start(skb,
18865a939beaSLorenzo Bianconi 					      NFSD_A_SERVER_PROTO_VERSION);
18875a939beaSLorenzo Bianconi 			if (!attr) {
18885a939beaSLorenzo Bianconi 				err = -EINVAL;
18895a939beaSLorenzo Bianconi 				goto err_nfsd_unlock;
18905a939beaSLorenzo Bianconi 			}
18915a939beaSLorenzo Bianconi 
18925a939beaSLorenzo Bianconi 			if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
18935a939beaSLorenzo Bianconi 			    nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
18945a939beaSLorenzo Bianconi 				err = -EINVAL;
18955a939beaSLorenzo Bianconi 				goto err_nfsd_unlock;
18965a939beaSLorenzo Bianconi 			}
18975a939beaSLorenzo Bianconi 
18985a939beaSLorenzo Bianconi 			/* Set the enabled flag if the version is enabled */
18995a939beaSLorenzo Bianconi 			if (nfsd_vers(nn, i, NFSD_TEST) &&
19005a939beaSLorenzo Bianconi 			    (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
19015a939beaSLorenzo Bianconi 			    nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
19025a939beaSLorenzo Bianconi 				err = -EINVAL;
19035a939beaSLorenzo Bianconi 				goto err_nfsd_unlock;
19045a939beaSLorenzo Bianconi 			}
19055a939beaSLorenzo Bianconi 
19065a939beaSLorenzo Bianconi 			nla_nest_end(skb, attr);
19075a939beaSLorenzo Bianconi 		}
19085a939beaSLorenzo Bianconi 	}
19095a939beaSLorenzo Bianconi 
19105a939beaSLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
19115a939beaSLorenzo Bianconi 	genlmsg_end(skb, hdr);
19125a939beaSLorenzo Bianconi 
19135a939beaSLorenzo Bianconi 	return genlmsg_reply(skb, info);
19145a939beaSLorenzo Bianconi 
19155a939beaSLorenzo Bianconi err_nfsd_unlock:
19165a939beaSLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
19175a939beaSLorenzo Bianconi err_free_msg:
19185a939beaSLorenzo Bianconi 	nlmsg_free(skb);
19195a939beaSLorenzo Bianconi 
19205a939beaSLorenzo Bianconi 	return err;
19215a939beaSLorenzo Bianconi }
19225a939beaSLorenzo Bianconi 
19235a939beaSLorenzo Bianconi /**
192416a47117SLorenzo Bianconi  * nfsd_nl_listener_set_doit - set the nfs running sockets
192516a47117SLorenzo Bianconi  * @skb: reply buffer
192616a47117SLorenzo Bianconi  * @info: netlink metadata and command arguments
192716a47117SLorenzo Bianconi  *
192816a47117SLorenzo Bianconi  * Return 0 on success or a negative errno.
192916a47117SLorenzo Bianconi  */
nfsd_nl_listener_set_doit(struct sk_buff * skb,struct genl_info * info)193016a47117SLorenzo Bianconi int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
193116a47117SLorenzo Bianconi {
193216a47117SLorenzo Bianconi 	struct net *net = genl_info_net(info);
193316a47117SLorenzo Bianconi 	struct svc_xprt *xprt, *tmp;
193416a47117SLorenzo Bianconi 	const struct nlattr *attr;
193516a47117SLorenzo Bianconi 	struct svc_serv *serv;
193616a47117SLorenzo Bianconi 	LIST_HEAD(permsocks);
193716a47117SLorenzo Bianconi 	struct nfsd_net *nn;
193816a47117SLorenzo Bianconi 	int err, rem;
193916a47117SLorenzo Bianconi 
194016a47117SLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
194116a47117SLorenzo Bianconi 
194216a47117SLorenzo Bianconi 	err = nfsd_create_serv(net);
194316a47117SLorenzo Bianconi 	if (err) {
194416a47117SLorenzo Bianconi 		mutex_unlock(&nfsd_mutex);
194516a47117SLorenzo Bianconi 		return err;
194616a47117SLorenzo Bianconi 	}
194716a47117SLorenzo Bianconi 
194816a47117SLorenzo Bianconi 	nn = net_generic(net, nfsd_net_id);
194916a47117SLorenzo Bianconi 	serv = nn->nfsd_serv;
195016a47117SLorenzo Bianconi 
195116a47117SLorenzo Bianconi 	spin_lock_bh(&serv->sv_lock);
195216a47117SLorenzo Bianconi 
195316a47117SLorenzo Bianconi 	/* Move all of the old listener sockets to a temp list */
195416a47117SLorenzo Bianconi 	list_splice_init(&serv->sv_permsocks, &permsocks);
195516a47117SLorenzo Bianconi 
195616a47117SLorenzo Bianconi 	/*
195716a47117SLorenzo Bianconi 	 * Walk the list of server_socks from userland and move any that match
195816a47117SLorenzo Bianconi 	 * back to sv_permsocks
195916a47117SLorenzo Bianconi 	 */
196016a47117SLorenzo Bianconi 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
196116a47117SLorenzo Bianconi 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
196216a47117SLorenzo Bianconi 		const char *xcl_name;
196316a47117SLorenzo Bianconi 		struct sockaddr *sa;
196416a47117SLorenzo Bianconi 
196516a47117SLorenzo Bianconi 		if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
196616a47117SLorenzo Bianconi 			continue;
196716a47117SLorenzo Bianconi 
196816a47117SLorenzo Bianconi 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
196916a47117SLorenzo Bianconi 				     nfsd_sock_nl_policy, info->extack) < 0)
197016a47117SLorenzo Bianconi 			continue;
197116a47117SLorenzo Bianconi 
197216a47117SLorenzo Bianconi 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
197316a47117SLorenzo Bianconi 			continue;
197416a47117SLorenzo Bianconi 
197516a47117SLorenzo Bianconi 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
197616a47117SLorenzo Bianconi 			continue;
197716a47117SLorenzo Bianconi 
197816a47117SLorenzo Bianconi 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
197916a47117SLorenzo Bianconi 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
198016a47117SLorenzo Bianconi 
198116a47117SLorenzo Bianconi 		/* Put back any matching sockets */
198216a47117SLorenzo Bianconi 		list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
198316a47117SLorenzo Bianconi 			/* This shouldn't be possible */
198416a47117SLorenzo Bianconi 			if (WARN_ON_ONCE(xprt->xpt_net != net)) {
198516a47117SLorenzo Bianconi 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
198616a47117SLorenzo Bianconi 				continue;
198716a47117SLorenzo Bianconi 			}
198816a47117SLorenzo Bianconi 
198916a47117SLorenzo Bianconi 			/* If everything matches, put it back */
199016a47117SLorenzo Bianconi 			if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
199116a47117SLorenzo Bianconi 			    rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
199216a47117SLorenzo Bianconi 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
199316a47117SLorenzo Bianconi 				break;
199416a47117SLorenzo Bianconi 			}
199516a47117SLorenzo Bianconi 		}
199616a47117SLorenzo Bianconi 	}
199716a47117SLorenzo Bianconi 
199816a47117SLorenzo Bianconi 	/* For now, no removing old sockets while server is running */
199916a47117SLorenzo Bianconi 	if (serv->sv_nrthreads && !list_empty(&permsocks)) {
200016a47117SLorenzo Bianconi 		list_splice_init(&permsocks, &serv->sv_permsocks);
200116a47117SLorenzo Bianconi 		spin_unlock_bh(&serv->sv_lock);
200216a47117SLorenzo Bianconi 		err = -EBUSY;
200316a47117SLorenzo Bianconi 		goto out_unlock_mtx;
200416a47117SLorenzo Bianconi 	}
200516a47117SLorenzo Bianconi 
200616a47117SLorenzo Bianconi 	/* Close the remaining sockets on the permsocks list */
200716a47117SLorenzo Bianconi 	while (!list_empty(&permsocks)) {
200816a47117SLorenzo Bianconi 		xprt = list_first_entry(&permsocks, struct svc_xprt, xpt_list);
200916a47117SLorenzo Bianconi 		list_move(&xprt->xpt_list, &serv->sv_permsocks);
201016a47117SLorenzo Bianconi 
201116a47117SLorenzo Bianconi 		/*
201216a47117SLorenzo Bianconi 		 * Newly-created sockets are born with the BUSY bit set. Clear
201316a47117SLorenzo Bianconi 		 * it if there are no threads, since nothing can pick it up
201416a47117SLorenzo Bianconi 		 * in that case.
201516a47117SLorenzo Bianconi 		 */
201616a47117SLorenzo Bianconi 		if (!serv->sv_nrthreads)
201716a47117SLorenzo Bianconi 			clear_bit(XPT_BUSY, &xprt->xpt_flags);
201816a47117SLorenzo Bianconi 
201916a47117SLorenzo Bianconi 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
202016a47117SLorenzo Bianconi 		spin_unlock_bh(&serv->sv_lock);
202116a47117SLorenzo Bianconi 		svc_xprt_close(xprt);
202216a47117SLorenzo Bianconi 		spin_lock_bh(&serv->sv_lock);
202316a47117SLorenzo Bianconi 	}
202416a47117SLorenzo Bianconi 
202516a47117SLorenzo Bianconi 	spin_unlock_bh(&serv->sv_lock);
202616a47117SLorenzo Bianconi 
202716a47117SLorenzo Bianconi 	/* walk list of addrs again, open any that still don't exist */
202816a47117SLorenzo Bianconi 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
202916a47117SLorenzo Bianconi 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
203016a47117SLorenzo Bianconi 		const char *xcl_name;
203116a47117SLorenzo Bianconi 		struct sockaddr *sa;
203216a47117SLorenzo Bianconi 		int ret;
203316a47117SLorenzo Bianconi 
203416a47117SLorenzo Bianconi 		if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
203516a47117SLorenzo Bianconi 			continue;
203616a47117SLorenzo Bianconi 
203716a47117SLorenzo Bianconi 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
203816a47117SLorenzo Bianconi 				     nfsd_sock_nl_policy, info->extack) < 0)
203916a47117SLorenzo Bianconi 			continue;
204016a47117SLorenzo Bianconi 
204116a47117SLorenzo Bianconi 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
204216a47117SLorenzo Bianconi 			continue;
204316a47117SLorenzo Bianconi 
204416a47117SLorenzo Bianconi 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
204516a47117SLorenzo Bianconi 			continue;
204616a47117SLorenzo Bianconi 
204716a47117SLorenzo Bianconi 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
204816a47117SLorenzo Bianconi 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
204916a47117SLorenzo Bianconi 
205016a47117SLorenzo Bianconi 		xprt = svc_find_listener(serv, xcl_name, net, sa);
205116a47117SLorenzo Bianconi 		if (xprt) {
205216a47117SLorenzo Bianconi 			svc_xprt_put(xprt);
205316a47117SLorenzo Bianconi 			continue;
205416a47117SLorenzo Bianconi 		}
205516a47117SLorenzo Bianconi 
205616a47117SLorenzo Bianconi 		ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa,
205716a47117SLorenzo Bianconi 					      SVC_SOCK_ANONYMOUS,
205816a47117SLorenzo Bianconi 					      get_current_cred());
205916a47117SLorenzo Bianconi 		/* always save the latest error */
206016a47117SLorenzo Bianconi 		if (ret < 0)
206116a47117SLorenzo Bianconi 			err = ret;
206216a47117SLorenzo Bianconi 	}
206316a47117SLorenzo Bianconi 
206416a47117SLorenzo Bianconi 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
206516a47117SLorenzo Bianconi 		nfsd_destroy_serv(net);
206616a47117SLorenzo Bianconi 
206716a47117SLorenzo Bianconi out_unlock_mtx:
206816a47117SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
206916a47117SLorenzo Bianconi 
207016a47117SLorenzo Bianconi 	return err;
207116a47117SLorenzo Bianconi }
207216a47117SLorenzo Bianconi 
207316a47117SLorenzo Bianconi /**
207416a47117SLorenzo Bianconi  * nfsd_nl_listener_get_doit - get the nfs running listeners
207516a47117SLorenzo Bianconi  * @skb: reply buffer
207616a47117SLorenzo Bianconi  * @info: netlink metadata and command arguments
207716a47117SLorenzo Bianconi  *
207816a47117SLorenzo Bianconi  * Return 0 on success or a negative errno.
207916a47117SLorenzo Bianconi  */
nfsd_nl_listener_get_doit(struct sk_buff * skb,struct genl_info * info)208016a47117SLorenzo Bianconi int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
208116a47117SLorenzo Bianconi {
208216a47117SLorenzo Bianconi 	struct svc_xprt *xprt;
208316a47117SLorenzo Bianconi 	struct svc_serv *serv;
208416a47117SLorenzo Bianconi 	struct nfsd_net *nn;
208516a47117SLorenzo Bianconi 	void *hdr;
208616a47117SLorenzo Bianconi 	int err;
208716a47117SLorenzo Bianconi 
208816a47117SLorenzo Bianconi 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
208916a47117SLorenzo Bianconi 	if (!skb)
209016a47117SLorenzo Bianconi 		return -ENOMEM;
209116a47117SLorenzo Bianconi 
209216a47117SLorenzo Bianconi 	hdr = genlmsg_iput(skb, info);
209316a47117SLorenzo Bianconi 	if (!hdr) {
209416a47117SLorenzo Bianconi 		err = -EMSGSIZE;
209516a47117SLorenzo Bianconi 		goto err_free_msg;
209616a47117SLorenzo Bianconi 	}
209716a47117SLorenzo Bianconi 
209816a47117SLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
209916a47117SLorenzo Bianconi 	nn = net_generic(genl_info_net(info), nfsd_net_id);
210016a47117SLorenzo Bianconi 
210116a47117SLorenzo Bianconi 	/* no nfs server? Just send empty socket list */
210216a47117SLorenzo Bianconi 	if (!nn->nfsd_serv)
210316a47117SLorenzo Bianconi 		goto out_unlock_mtx;
210416a47117SLorenzo Bianconi 
210516a47117SLorenzo Bianconi 	serv = nn->nfsd_serv;
210616a47117SLorenzo Bianconi 	spin_lock_bh(&serv->sv_lock);
210716a47117SLorenzo Bianconi 	list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
210816a47117SLorenzo Bianconi 		struct nlattr *attr;
210916a47117SLorenzo Bianconi 
211016a47117SLorenzo Bianconi 		attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
211116a47117SLorenzo Bianconi 		if (!attr) {
211216a47117SLorenzo Bianconi 			err = -EINVAL;
211316a47117SLorenzo Bianconi 			goto err_serv_unlock;
211416a47117SLorenzo Bianconi 		}
211516a47117SLorenzo Bianconi 
211616a47117SLorenzo Bianconi 		if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
211716a47117SLorenzo Bianconi 				   xprt->xpt_class->xcl_name) ||
211816a47117SLorenzo Bianconi 		    nla_put(skb, NFSD_A_SOCK_ADDR,
211916a47117SLorenzo Bianconi 			    sizeof(struct sockaddr_storage),
212016a47117SLorenzo Bianconi 			    &xprt->xpt_local)) {
212116a47117SLorenzo Bianconi 			err = -EINVAL;
212216a47117SLorenzo Bianconi 			goto err_serv_unlock;
212316a47117SLorenzo Bianconi 		}
212416a47117SLorenzo Bianconi 
212516a47117SLorenzo Bianconi 		nla_nest_end(skb, attr);
212616a47117SLorenzo Bianconi 	}
212716a47117SLorenzo Bianconi 	spin_unlock_bh(&serv->sv_lock);
212816a47117SLorenzo Bianconi out_unlock_mtx:
212916a47117SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
213016a47117SLorenzo Bianconi 	genlmsg_end(skb, hdr);
213116a47117SLorenzo Bianconi 
213216a47117SLorenzo Bianconi 	return genlmsg_reply(skb, info);
213316a47117SLorenzo Bianconi 
213416a47117SLorenzo Bianconi err_serv_unlock:
213516a47117SLorenzo Bianconi 	spin_unlock_bh(&serv->sv_lock);
213616a47117SLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
213716a47117SLorenzo Bianconi err_free_msg:
213816a47117SLorenzo Bianconi 	nlmsg_free(skb);
213916a47117SLorenzo Bianconi 
214016a47117SLorenzo Bianconi 	return err;
214116a47117SLorenzo Bianconi }
214216a47117SLorenzo Bianconi 
214316a47117SLorenzo Bianconi /**
21445e092be7SChuck Lever  * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
21455e092be7SChuck Lever  * @net: a freshly-created network namespace
21465e092be7SChuck Lever  *
21475e092be7SChuck Lever  * This information stays around as long as the network namespace is
21485e092be7SChuck Lever  * alive whether or not there is an NFSD instance running in the
21495e092be7SChuck Lever  * namespace.
21505e092be7SChuck Lever  *
21515e092be7SChuck Lever  * Returns zero on success, or a negative errno otherwise.
21525e092be7SChuck Lever  */
nfsd_net_init(struct net * net)21535e092be7SChuck Lever static __net_init int nfsd_net_init(struct net *net)
21545717e012SStanislav Kinsbursky {
21555717e012SStanislav Kinsbursky 	int retval;
21563d733711SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
21575717e012SStanislav Kinsbursky 
21585717e012SStanislav Kinsbursky 	retval = nfsd_export_init(net);
21595717e012SStanislav Kinsbursky 	if (retval)
21605717e012SStanislav Kinsbursky 		goto out_export_error;
2161f69adb2fSStanislav Kinsbursky 	retval = nfsd_idmap_init(net);
2162f69adb2fSStanislav Kinsbursky 	if (retval)
2163f69adb2fSStanislav Kinsbursky 		goto out_idmap_error;
21647d12cce8SKefeng Wang 	retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
21657d12cce8SKefeng Wang 					  NFSD_STATS_COUNTERS_NUM);
2166ed9ab734SJeff Layton 	if (retval)
2167ed9ab734SJeff Layton 		goto out_repcache_error;
216816fb9808SJosef Bacik 	memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
216916fb9808SJosef Bacik 	nn->nfsd_svcstats.program = &nfsd_program;
2170e333f3bbSTrond Myklebust 	nn->nfsd_versions = NULL;
2171e333f3bbSTrond Myklebust 	nn->nfsd4_minorversions = NULL;
2172f385f7d2SDai Ngo 	nfsd4_init_leases_net(nn);
217391d2e9b5SChuck Lever 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
217491d2e9b5SChuck Lever 	seqlock_init(&nn->writeverf_lock);
217593483ac5SJosef Bacik 	nfsd_proc_stat_init(net);
21762c830dd7SJ. Bruce Fields 
21775717e012SStanislav Kinsbursky 	return 0;
21785717e012SStanislav Kinsbursky 
2179ed9ab734SJeff Layton out_repcache_error:
2180ed9ab734SJeff Layton 	nfsd_idmap_shutdown(net);
2181f69adb2fSStanislav Kinsbursky out_idmap_error:
2182f69adb2fSStanislav Kinsbursky 	nfsd_export_shutdown(net);
21835717e012SStanislav Kinsbursky out_export_error:
21845717e012SStanislav Kinsbursky 	return retval;
21855717e012SStanislav Kinsbursky }
21865717e012SStanislav Kinsbursky 
21875e092be7SChuck Lever /**
21885e092be7SChuck Lever  * nfsd_net_exit - Release the nfsd_net portion of a net namespace
21895e092be7SChuck Lever  * @net: a network namespace that is about to be destroyed
21905e092be7SChuck Lever  *
21915e092be7SChuck Lever  */
nfsd_net_exit(struct net * net)21925e092be7SChuck Lever static __net_exit void nfsd_net_exit(struct net *net)
21935717e012SStanislav Kinsbursky {
2194ed9ab734SJeff Layton 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2195ed9ab734SJeff Layton 
219693483ac5SJosef Bacik 	nfsd_proc_stat_shutdown(net);
21977d12cce8SKefeng Wang 	percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2198f69adb2fSStanislav Kinsbursky 	nfsd_idmap_shutdown(net);
21995717e012SStanislav Kinsbursky 	nfsd_export_shutdown(net);
2200ed9ab734SJeff Layton 	nfsd_netns_free_versions(nn);
22015717e012SStanislav Kinsbursky }
22025717e012SStanislav Kinsbursky 
22037ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = {
22045e092be7SChuck Lever 	.init = nfsd_net_init,
22055e092be7SChuck Lever 	.exit = nfsd_net_exit,
22067ea34ac1SJeff Layton 	.id   = &nfsd_net_id,
22077ea34ac1SJeff Layton 	.size = sizeof(struct nfsd_net),
22087ea34ac1SJeff Layton };
22097ea34ac1SJeff Layton 
init_nfsd(void)22101da177e4SLinus Torvalds static int __init init_nfsd(void)
22111da177e4SLinus Torvalds {
22121da177e4SLinus Torvalds 	int retval;
22131da177e4SLinus Torvalds 
2214bb7ffbf2SGiuseppe Cantavenera 	retval = nfsd4_init_slabs();
2215bb7ffbf2SGiuseppe Cantavenera 	if (retval)
2216b10252c7SAlexander Sverdlin 		return retval;
22179cf514ccSChristoph Hellwig 	retval = nfsd4_init_pnfs();
221865178db4SBryan Schumaker 	if (retval)
221965178db4SBryan Schumaker 		goto out_free_slabs;
2220027690c7SJ. Bruce Fields 	retval = nfsd_drc_slab_create();
2221027690c7SJ. Bruce Fields 	if (retval)
22224b148854SJosef Bacik 		goto out_free_pnfs;
22231da177e4SLinus Torvalds 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
2224e331f606SJ. Bruce Fields 	retval = create_proc_exports_entry();
2225e331f606SJ. Bruce Fields 	if (retval)
2226f69adb2fSStanislav Kinsbursky 		goto out_free_lockd;
2227bd5ae928SJ. Bruce Fields 	retval = register_pernet_subsys(&nfsd_net_ops);
2228bd5ae928SJ. Bruce Fields 	if (retval < 0)
22296f6f84aaSZhang Xiaoxu 		goto out_free_exports;
2230b10252c7SAlexander Sverdlin 	retval = register_cld_notifier();
2231b10252c7SAlexander Sverdlin 	if (retval)
223262fdb65eSZhang Xiaoxu 		goto out_free_subsys;
2233d76cc46bSDai Ngo 	retval = nfsd4_create_laundry_wq();
2234d76cc46bSDai Ngo 	if (retval)
22356f6f84aaSZhang Xiaoxu 		goto out_free_cld;
22366f6f84aaSZhang Xiaoxu 	retval = register_filesystem(&nfsd_fs_type);
22376f6f84aaSZhang Xiaoxu 	if (retval)
2238d76cc46bSDai Ngo 		goto out_free_all;
2239bd9d6a3eSLorenzo Bianconi 	retval = genl_register_family(&nfsd_nl_family);
2240bd9d6a3eSLorenzo Bianconi 	if (retval)
2241bd9d6a3eSLorenzo Bianconi 		goto out_free_all;
2242bd9d6a3eSLorenzo Bianconi 
224326808d3fSJ. Bruce Fields 	return 0;
224426808d3fSJ. Bruce Fields out_free_all:
22456f6f84aaSZhang Xiaoxu 	nfsd4_destroy_laundry_wq();
22466f6f84aaSZhang Xiaoxu out_free_cld:
224762fdb65eSZhang Xiaoxu 	unregister_cld_notifier();
224862fdb65eSZhang Xiaoxu out_free_subsys:
2249b10252c7SAlexander Sverdlin 	unregister_pernet_subsys(&nfsd_net_ops);
2250bd5ae928SJ. Bruce Fields out_free_exports:
22511da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
22521da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
2253dbf847ecSJ. Bruce Fields out_free_lockd:
22541da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
2255027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
2256e567b98cSAmir Goldstein out_free_pnfs:
22579cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
225865178db4SBryan Schumaker out_free_slabs:
225946b25895SJ. Bruce Fields 	nfsd4_free_slabs();
22601da177e4SLinus Torvalds 	return retval;
22611da177e4SLinus Torvalds }
22621da177e4SLinus Torvalds 
exit_nfsd(void)22631da177e4SLinus Torvalds static void __exit exit_nfsd(void)
22641da177e4SLinus Torvalds {
2265bd9d6a3eSLorenzo Bianconi 	genl_unregister_family(&nfsd_nl_family);
22666f6f84aaSZhang Xiaoxu 	unregister_filesystem(&nfsd_fs_type);
2267d76cc46bSDai Ngo 	nfsd4_destroy_laundry_wq();
2268b10252c7SAlexander Sverdlin 	unregister_cld_notifier();
2269bd5ae928SJ. Bruce Fields 	unregister_pernet_subsys(&nfsd_net_ops);
2270027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
22711da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
22721da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
22731da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
2274e8ff2a84SJ. Bruce Fields 	nfsd4_free_slabs();
22759cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
22761da177e4SLinus Torvalds }
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
22795865bafaSJeff Layton MODULE_DESCRIPTION("In-kernel NFS server");
22801da177e4SLinus Torvalds MODULE_LICENSE("GPL");
22811da177e4SLinus Torvalds module_init(init_nfsd)
22821da177e4SLinus Torvalds module_exit(exit_nfsd)
2283