xref: /linux/fs/nfsd/nfsctl.c (revision 17419aefcbfd9891863e8b8132f0bca9a6b2984e)
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>
18143cb494SPaul Gortmaker #include <linux/module.h>
19e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h>
201da177e4SLinus Torvalds 
212ca72e17SJ. Bruce Fields #include "idmap.h"
229a74af21SBoaz Harrosh #include "nfsd.h"
239a74af21SBoaz Harrosh #include "cache.h"
24f3c7521fSBryan Schumaker #include "state.h"
257ea34ac1SJeff Layton #include "netns.h"
269cf514ccSChristoph Hellwig #include "pnfs.h"
272e6c6e4cSChuck Lever #include "filecache.h"
2839d432fcSChuck Lever #include "trace.h"
2913727f85SLorenzo Bianconi #include "netlink.h"
309a74af21SBoaz Harrosh 
311da177e4SLinus Torvalds /*
32b0b0c0a2SKevin Coffman  *	We have a single directory with several nodes in it.
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds enum {
351da177e4SLinus Torvalds 	NFSD_Root = 1,
361da177e4SLinus Torvalds 	NFSD_List,
3720ad856eSAmir Goldstein 	NFSD_Export_Stats,
38e8e8753fSJ. Bruce Fields 	NFSD_Export_features,
391da177e4SLinus Torvalds 	NFSD_Fh,
404373ea84SWendy Cheng 	NFSD_FO_UnlockIP,
4117efa372SWendy Cheng 	NFSD_FO_UnlockFS,
421da177e4SLinus Torvalds 	NFSD_Threads,
43eed2965aSGreg Banks 	NFSD_Pool_Threads,
4403cf6c9fSGreg Banks 	NFSD_Pool_Stats,
45a2f999a3SJeff Layton 	NFSD_Reply_Cache_Stats,
4670c3b76cSNeilBrown 	NFSD_Versions,
4780212d59SNeilBrown 	NFSD_Ports,
48596bbe53SNeilBrown 	NFSD_MaxBlkSize,
495b8db00bSJeff Layton 	NFSD_MaxConnections,
502e6c6e4cSChuck Lever 	NFSD_Filecache,
5170c3b76cSNeilBrown 	/*
5270c3b76cSNeilBrown 	 * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
5370c3b76cSNeilBrown 	 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
5470c3b76cSNeilBrown 	 */
5570c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
561da177e4SLinus Torvalds 	NFSD_Leasetime,
57efc4bb4fSJ. Bruce Fields 	NFSD_Gracetime,
580964a3d3SNeilBrown 	NFSD_RecoveryDir,
597f5ef2e9SJeff Layton 	NFSD_V4EndGrace,
6070c3b76cSNeilBrown #endif
61e8a79fb1SJ. Bruce Fields 	NFSD_MaxReserved
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * write() for these nodes.
661da177e4SLinus Torvalds  */
671da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
68b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
69b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
701da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size);
71eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
7270c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size);
7380212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size);
74596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
755b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
7670c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
771da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
78efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
7974fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
800964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
8174fd4873SJeff Layton #endif
827f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
8370c3b76cSNeilBrown #endif
841da177e4SLinus Torvalds 
85c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
861da177e4SLinus Torvalds 	[NFSD_Fh] = write_filehandle,
87b046ccdcSChuck Lever 	[NFSD_FO_UnlockIP] = write_unlock_ip,
88b046ccdcSChuck Lever 	[NFSD_FO_UnlockFS] = write_unlock_fs,
891da177e4SLinus Torvalds 	[NFSD_Threads] = write_threads,
90eed2965aSGreg Banks 	[NFSD_Pool_Threads] = write_pool_threads,
9170c3b76cSNeilBrown 	[NFSD_Versions] = write_versions,
9280212d59SNeilBrown 	[NFSD_Ports] = write_ports,
93596bbe53SNeilBrown 	[NFSD_MaxBlkSize] = write_maxblksize,
945b8db00bSJeff Layton 	[NFSD_MaxConnections] = write_maxconn,
9570c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
961da177e4SLinus Torvalds 	[NFSD_Leasetime] = write_leasetime,
97efc4bb4fSJ. Bruce Fields 	[NFSD_Gracetime] = write_gracetime,
9874fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
990964a3d3SNeilBrown 	[NFSD_RecoveryDir] = write_recoverydir,
10074fd4873SJeff Layton #endif
1017f5ef2e9SJeff Layton 	[NFSD_V4EndGrace] = write_v4_end_grace,
10270c3b76cSNeilBrown #endif
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
1061da177e4SLinus Torvalds {
107496ad9aaSAl Viro 	ino_t ino =  file_inode(file)->i_ino;
1081da177e4SLinus Torvalds 	char *data;
1091da177e4SLinus Torvalds 	ssize_t rv;
1101da177e4SLinus Torvalds 
111e8c96f8cSTobias Klauser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1121da177e4SLinus Torvalds 		return -EINVAL;
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
1151da177e4SLinus Torvalds 	if (IS_ERR(data))
1161da177e4SLinus Torvalds 		return PTR_ERR(data);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	rv = write_op[ino](file, data, size);
1193434d7aaSChuck Lever 	if (rv < 0)
1201da177e4SLinus Torvalds 		return rv;
1213434d7aaSChuck Lever 
1223434d7aaSChuck Lever 	simple_transaction_set(file, rv);
1233434d7aaSChuck Lever 	return size;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1267390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1277390022dSNeilBrown {
1287390022dSNeilBrown 	if (! file->private_data) {
1297390022dSNeilBrown 		/* An attempt to read a transaction file without writing
1307390022dSNeilBrown 		 * causes a 0-byte write so that the file can return
1317390022dSNeilBrown 		 * state information
1327390022dSNeilBrown 		 */
1337390022dSNeilBrown 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
1347390022dSNeilBrown 		if (rv < 0)
1357390022dSNeilBrown 			return rv;
1367390022dSNeilBrown 	}
1377390022dSNeilBrown 	return simple_transaction_read(file, buf, size, pos);
1387390022dSNeilBrown }
1397390022dSNeilBrown 
1404b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = {
1411da177e4SLinus Torvalds 	.write		= nfsctl_transaction_write,
1427390022dSNeilBrown 	.read		= nfsctl_transaction_read,
1431da177e4SLinus Torvalds 	.release	= simple_transaction_release,
1446038f373SArnd Bergmann 	.llseek		= default_llseek,
1451da177e4SLinus Torvalds };
1461da177e4SLinus Torvalds 
14796d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file)
1481da177e4SLinus Torvalds {
149f2c7ea10SStanislav Kinsbursky 	int err;
150f2c7ea10SStanislav Kinsbursky 	struct seq_file *seq;
15196d851c4SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
152f2c7ea10SStanislav Kinsbursky 
153f2c7ea10SStanislav Kinsbursky 	err = seq_open(file, &nfs_exports_op);
154f2c7ea10SStanislav Kinsbursky 	if (err)
155f2c7ea10SStanislav Kinsbursky 		return err;
156f2c7ea10SStanislav Kinsbursky 
157f2c7ea10SStanislav Kinsbursky 	seq = file->private_data;
158e5f06f72SStanislav Kinsbursky 	seq->private = nn->svc_export_cache;
159f2c7ea10SStanislav Kinsbursky 	return 0;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
16296d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file)
16396d851c4SStanislav Kinsbursky {
16496d851c4SStanislav Kinsbursky 	return exports_net_open(inode->i_sb->s_fs_info, file);
16596d851c4SStanislav Kinsbursky }
16696d851c4SStanislav Kinsbursky 
16796d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = {
16896d851c4SStanislav Kinsbursky 	.open		= exports_nfsd_open,
1691da177e4SLinus Torvalds 	.read		= seq_read,
1701da177e4SLinus Torvalds 	.llseek		= seq_lseek,
1711da177e4SLinus Torvalds 	.release	= seq_release,
1721da177e4SLinus Torvalds };
1731da177e4SLinus Torvalds 
174e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v)
175e8e8753fSJ. Bruce Fields {
176e8e8753fSJ. Bruce Fields 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
177e8e8753fSJ. Bruce Fields 	return 0;
178e8e8753fSJ. Bruce Fields }
179e8e8753fSJ. Bruce Fields 
1809beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(export_features);
181e8e8753fSJ. Bruce Fields 
182828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = {
18303cf6c9fSGreg Banks 	.open		= nfsd_pool_stats_open,
18403cf6c9fSGreg Banks 	.read		= seq_read,
18503cf6c9fSGreg Banks 	.llseek		= seq_lseek,
1867b207ccdSNeilBrown 	.release	= seq_release,
18703cf6c9fSGreg Banks };
18803cf6c9fSGreg Banks 
18964776611SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
190a2f999a3SJeff Layton 
1911342f9ddSChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
1922e6c6e4cSChuck Lever 
1931da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
1941da177e4SLinus Torvalds /*
1951da177e4SLinus Torvalds  * payload - write methods
1961da177e4SLinus Torvalds  */
1971da177e4SLinus Torvalds 
198244c7d44SAl Viro static inline struct net *netns(struct file *file)
199244c7d44SAl Viro {
200244c7d44SAl Viro 	return file_inode(file)->i_sb->s_fs_info;
201244c7d44SAl Viro }
2021da177e4SLinus Torvalds 
203f2453978SChuck Lever /*
204262a0982SChuck Lever  * write_unlock_ip - Release all locks used by a client
205262a0982SChuck Lever  *
206262a0982SChuck Lever  * Experimental.
207262a0982SChuck Lever  *
208262a0982SChuck Lever  * Input:
209262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing a
2104116092bSChuck Lever  *				presentation format IP address
211262a0982SChuck Lever  *			size:	length of C string in @buf
212262a0982SChuck Lever  * Output:
213262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
214262a0982SChuck Lever  *			returns one if one or more locks were not released
215262a0982SChuck Lever  *	On error:	return code is negative errno value
216262a0982SChuck Lever  */
217b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
2184373ea84SWendy Cheng {
2194116092bSChuck Lever 	struct sockaddr_storage address;
2204116092bSChuck Lever 	struct sockaddr *sap = (struct sockaddr *)&address;
2214116092bSChuck Lever 	size_t salen = sizeof(address);
222367c8c7bSChuck Lever 	char *fo_path;
223244c7d44SAl Viro 	struct net *net = netns(file);
2244373ea84SWendy Cheng 
2254373ea84SWendy Cheng 	/* sanity check */
2264373ea84SWendy Cheng 	if (size == 0)
2274373ea84SWendy Cheng 		return -EINVAL;
2284373ea84SWendy Cheng 
2294373ea84SWendy Cheng 	if (buf[size-1] != '\n')
2304373ea84SWendy Cheng 		return -EINVAL;
2314373ea84SWendy Cheng 
2324373ea84SWendy Cheng 	fo_path = buf;
2334373ea84SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
2344373ea84SWendy Cheng 		return -EINVAL;
2354373ea84SWendy Cheng 
23611f77942SStanislav Kinsbursky 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
2374373ea84SWendy Cheng 		return -EINVAL;
2384373ea84SWendy Cheng 
23939d432fcSChuck Lever 	trace_nfsd_ctl_unlock_ip(net, buf);
2404116092bSChuck Lever 	return nlmsvc_unlock_all_by_ip(sap);
2414373ea84SWendy Cheng }
2424373ea84SWendy Cheng 
243f2453978SChuck Lever /*
244262a0982SChuck Lever  * write_unlock_fs - Release all locks on a local file system
245262a0982SChuck Lever  *
246262a0982SChuck Lever  * Experimental.
247262a0982SChuck Lever  *
248262a0982SChuck Lever  * Input:
249262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing the
250262a0982SChuck Lever  *				absolute pathname of a local file system
251262a0982SChuck Lever  *			size:	length of C string in @buf
252262a0982SChuck Lever  * Output:
253262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
254262a0982SChuck Lever  *			returns one if one or more locks were not released
255262a0982SChuck Lever  *	On error:	return code is negative errno value
256262a0982SChuck Lever  */
257b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
25817efa372SWendy Cheng {
259a63bb996SAl Viro 	struct path path;
26017efa372SWendy Cheng 	char *fo_path;
26117efa372SWendy Cheng 	int error;
26217efa372SWendy Cheng 
26317efa372SWendy Cheng 	/* sanity check */
26417efa372SWendy Cheng 	if (size == 0)
26517efa372SWendy Cheng 		return -EINVAL;
26617efa372SWendy Cheng 
26717efa372SWendy Cheng 	if (buf[size-1] != '\n')
26817efa372SWendy Cheng 		return -EINVAL;
26917efa372SWendy Cheng 
27017efa372SWendy Cheng 	fo_path = buf;
27117efa372SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
27217efa372SWendy Cheng 		return -EINVAL;
27339d432fcSChuck Lever 	trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
274a63bb996SAl Viro 	error = kern_path(fo_path, 0, &path);
27517efa372SWendy Cheng 	if (error)
27617efa372SWendy Cheng 		return error;
27717efa372SWendy Cheng 
278262a0982SChuck Lever 	/*
279262a0982SChuck Lever 	 * XXX: Needs better sanity checking.  Otherwise we could end up
280262a0982SChuck Lever 	 * releasing locks on the wrong file system.
281262a0982SChuck Lever 	 *
282262a0982SChuck Lever 	 * For example:
283262a0982SChuck Lever 	 * 1.  Does the path refer to a directory?
284262a0982SChuck Lever 	 * 2.  Is that directory a mount point, or
285262a0982SChuck Lever 	 * 3.  Is that directory the root of an exported file system?
286262a0982SChuck Lever 	 */
287d8c9584eSAl Viro 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
28817efa372SWendy Cheng 
289a63bb996SAl Viro 	path_put(&path);
29017efa372SWendy Cheng 	return error;
29117efa372SWendy Cheng }
29217efa372SWendy Cheng 
293f2453978SChuck Lever /*
294262a0982SChuck Lever  * write_filehandle - Get a variable-length NFS file handle by path
295262a0982SChuck Lever  *
296262a0982SChuck Lever  * On input, the buffer contains a '\n'-terminated C string comprised of
297262a0982SChuck Lever  * three alphanumeric words separated by whitespace.  The string may
298262a0982SChuck Lever  * contain escape sequences.
299262a0982SChuck Lever  *
300262a0982SChuck Lever  * Input:
301262a0982SChuck Lever  *			buf:
302262a0982SChuck Lever  *				domain:		client domain name
303262a0982SChuck Lever  *				path:		export pathname
304262a0982SChuck Lever  *				maxsize:	numeric maximum size of
305262a0982SChuck Lever  *						@buf
306262a0982SChuck Lever  *			size:	length of C string in @buf
307262a0982SChuck Lever  * Output:
308262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
309262a0982SChuck Lever  *			string containing a ASCII hex text version
310262a0982SChuck Lever  *			of the NFS file handle;
311262a0982SChuck Lever  *			return code is the size in bytes of the string
312262a0982SChuck Lever  *	On error:	return code is negative errno value
313262a0982SChuck Lever  */
3141da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	char *dname, *path;
3173f649ab7SKees Cook 	int maxsize;
3181da177e4SLinus Torvalds 	char *mesg = buf;
3191da177e4SLinus Torvalds 	int len;
3201da177e4SLinus Torvalds 	struct auth_domain *dom;
3211da177e4SLinus Torvalds 	struct knfsd_fh fh;
3221da177e4SLinus Torvalds 
32387d26ea7SJ. Bruce Fields 	if (size == 0)
32487d26ea7SJ. Bruce Fields 		return -EINVAL;
32587d26ea7SJ. Bruce Fields 
3261da177e4SLinus Torvalds 	if (buf[size-1] != '\n')
3271da177e4SLinus Torvalds 		return -EINVAL;
3281da177e4SLinus Torvalds 	buf[size-1] = 0;
3291da177e4SLinus Torvalds 
3301da177e4SLinus Torvalds 	dname = mesg;
3311da177e4SLinus Torvalds 	len = qword_get(&mesg, dname, size);
33254224f04SChuck Lever 	if (len <= 0)
33354224f04SChuck Lever 		return -EINVAL;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	path = dname+len+1;
3361da177e4SLinus Torvalds 	len = qword_get(&mesg, path, size);
33754224f04SChuck Lever 	if (len <= 0)
33854224f04SChuck Lever 		return -EINVAL;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	len = get_int(&mesg, &maxsize);
3411da177e4SLinus Torvalds 	if (len)
3421da177e4SLinus Torvalds 		return len;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	if (maxsize < NFS_FHSIZE)
3451da177e4SLinus Torvalds 		return -EINVAL;
3463c7aa15dSKinglong Mee 	maxsize = min(maxsize, NFS3_FHSIZE);
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	if (qword_get(&mesg, mesg, size) > 0)
3491da177e4SLinus Torvalds 		return -EINVAL;
3501da177e4SLinus Torvalds 
35139d432fcSChuck Lever 	trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
35239d432fcSChuck Lever 
3531da177e4SLinus Torvalds 	/* we have all the words, they are in buf.. */
3541da177e4SLinus Torvalds 	dom = unix_domain_find(dname);
3551da177e4SLinus Torvalds 	if (!dom)
3561da177e4SLinus Torvalds 		return -ENOMEM;
3571da177e4SLinus Torvalds 
358244c7d44SAl Viro 	len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
3591da177e4SLinus Torvalds 	auth_domain_put(dom);
3601da177e4SLinus Torvalds 	if (len)
3611da177e4SLinus Torvalds 		return len;
3621da177e4SLinus Torvalds 
36354224f04SChuck Lever 	mesg = buf;
36454224f04SChuck Lever 	len = SIMPLE_TRANSACTION_LIMIT;
365d8b26071SNeilBrown 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
3661da177e4SLinus Torvalds 	mesg[-1] = '\n';
3671da177e4SLinus Torvalds 	return mesg - buf;
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
370f2453978SChuck Lever /*
371262a0982SChuck Lever  * write_threads - Start NFSD, or report the current number of running threads
372262a0982SChuck Lever  *
373262a0982SChuck Lever  * Input:
374262a0982SChuck Lever  *			buf:		ignored
375262a0982SChuck Lever  *			size:		zero
376262a0982SChuck Lever  * Output:
377262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
378262a0982SChuck Lever  *			string numeric value representing the number of
379262a0982SChuck Lever  *			running NFSD threads;
380262a0982SChuck Lever  *			return code is the size in bytes of the string
381262a0982SChuck Lever  *	On error:	return code is zero
382262a0982SChuck Lever  *
383262a0982SChuck Lever  * OR
384262a0982SChuck Lever  *
385262a0982SChuck Lever  * Input:
386262a0982SChuck Lever  *			buf:		C string containing an unsigned
387262a0982SChuck Lever  *					integer value representing the
388262a0982SChuck Lever  *					number of NFSD threads to start
389262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
390262a0982SChuck Lever  * Output:
391262a0982SChuck Lever  *	On success:	NFS service is started;
392262a0982SChuck Lever  *			passed-in buffer filled with '\n'-terminated C
393262a0982SChuck Lever  *			string numeric value representing the number of
394262a0982SChuck Lever  *			running NFSD threads;
395262a0982SChuck Lever  *			return code is the size in bytes of the string
396262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
397262a0982SChuck Lever  */
3981da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	char *mesg = buf;
4011da177e4SLinus Torvalds 	int rv;
402244c7d44SAl Viro 	struct net *net = netns(file);
403d41a9417SStanislav Kinsbursky 
4041da177e4SLinus Torvalds 	if (size > 0) {
4051da177e4SLinus Torvalds 		int newthreads;
4061da177e4SLinus Torvalds 		rv = get_int(&mesg, &newthreads);
4071da177e4SLinus Torvalds 		if (rv)
4081da177e4SLinus Torvalds 			return rv;
4091da177e4SLinus Torvalds 		if (newthreads < 0)
4101da177e4SLinus Torvalds 			return -EINVAL;
41139d432fcSChuck Lever 		trace_nfsd_ctl_threads(net, newthreads);
4124df493a2STrond Myklebust 		rv = nfsd_svc(newthreads, net, file->f_cred);
41382e12fe9SNeilBrown 		if (rv < 0)
4141da177e4SLinus Torvalds 			return rv;
41582e12fe9SNeilBrown 	} else
4169dd9845fSStanislav Kinsbursky 		rv = nfsd_nrthreads(net);
417e06b6405SChuck Lever 
41882e12fe9SNeilBrown 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
421f2453978SChuck Lever /*
422262a0982SChuck Lever  * write_pool_threads - Set or report the current number of threads per pool
423262a0982SChuck Lever  *
424262a0982SChuck Lever  * Input:
425262a0982SChuck Lever  *			buf:		ignored
426262a0982SChuck Lever  *			size:		zero
427262a0982SChuck Lever  *
428262a0982SChuck Lever  * OR
429262a0982SChuck Lever  *
430262a0982SChuck Lever  * Input:
431262a0982SChuck Lever  *			buf:		C string containing whitespace-
432262a0982SChuck Lever  *					separated unsigned integer values
433262a0982SChuck Lever  *					representing the number of NFSD
434262a0982SChuck Lever  *					threads to start in each pool
435262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
436262a0982SChuck Lever  * Output:
437262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
438262a0982SChuck Lever  *			string containing integer values representing the
439262a0982SChuck Lever  *			number of NFSD threads in each pool;
440262a0982SChuck Lever  *			return code is the size in bytes of the string
441262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
442262a0982SChuck Lever  */
443eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
444eed2965aSGreg Banks {
445eed2965aSGreg Banks 	/* if size > 0, look for an array of number of threads per node
446eed2965aSGreg Banks 	 * and apply them  then write out number of threads per node as reply
447eed2965aSGreg Banks 	 */
448eed2965aSGreg Banks 	char *mesg = buf;
449eed2965aSGreg Banks 	int i;
450eed2965aSGreg Banks 	int rv;
451eed2965aSGreg Banks 	int len;
452bedbdd8bSNeil Brown 	int npools;
453eed2965aSGreg Banks 	int *nthreads;
454244c7d44SAl Viro 	struct net *net = netns(file);
455eed2965aSGreg Banks 
456bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
4579dd9845fSStanislav Kinsbursky 	npools = nfsd_nrpools(net);
458eed2965aSGreg Banks 	if (npools == 0) {
459eed2965aSGreg Banks 		/*
460eed2965aSGreg Banks 		 * NFS is shut down.  The admin can start it by
461eed2965aSGreg Banks 		 * writing to the threads file but NOT the pool_threads
462eed2965aSGreg Banks 		 * file, sorry.  Report zero threads.
463eed2965aSGreg Banks 		 */
464bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
465eed2965aSGreg Banks 		strcpy(buf, "0\n");
466eed2965aSGreg Banks 		return strlen(buf);
467eed2965aSGreg Banks 	}
468eed2965aSGreg Banks 
469eed2965aSGreg Banks 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
470bedbdd8bSNeil Brown 	rv = -ENOMEM;
471eed2965aSGreg Banks 	if (nthreads == NULL)
472bedbdd8bSNeil Brown 		goto out_free;
473eed2965aSGreg Banks 
474eed2965aSGreg Banks 	if (size > 0) {
475eed2965aSGreg Banks 		for (i = 0; i < npools; i++) {
476eed2965aSGreg Banks 			rv = get_int(&mesg, &nthreads[i]);
477eed2965aSGreg Banks 			if (rv == -ENOENT)
478eed2965aSGreg Banks 				break;		/* fewer numbers than pools */
479eed2965aSGreg Banks 			if (rv)
480eed2965aSGreg Banks 				goto out_free;	/* syntax error */
481eed2965aSGreg Banks 			rv = -EINVAL;
482eed2965aSGreg Banks 			if (nthreads[i] < 0)
483eed2965aSGreg Banks 				goto out_free;
48439d432fcSChuck Lever 			trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
485eed2965aSGreg Banks 		}
4863938a0d5SStanislav Kinsbursky 		rv = nfsd_set_nrthreads(i, nthreads, net);
487eed2965aSGreg Banks 		if (rv)
488eed2965aSGreg Banks 			goto out_free;
489eed2965aSGreg Banks 	}
490eed2965aSGreg Banks 
4919dd9845fSStanislav Kinsbursky 	rv = nfsd_get_nrthreads(npools, nthreads, net);
492eed2965aSGreg Banks 	if (rv)
493eed2965aSGreg Banks 		goto out_free;
494eed2965aSGreg Banks 
495eed2965aSGreg Banks 	mesg = buf;
496eed2965aSGreg Banks 	size = SIMPLE_TRANSACTION_LIMIT;
497eed2965aSGreg Banks 	for (i = 0; i < npools && size > 0; i++) {
498eed2965aSGreg Banks 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
499eed2965aSGreg Banks 		len = strlen(mesg);
500eed2965aSGreg Banks 		size -= len;
501eed2965aSGreg Banks 		mesg += len;
502eed2965aSGreg Banks 	}
503413d63d7SJ. Bruce Fields 	rv = mesg - buf;
504eed2965aSGreg Banks out_free:
505eed2965aSGreg Banks 	kfree(nthreads);
506bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
507eed2965aSGreg Banks 	return rv;
508eed2965aSGreg Banks }
509eed2965aSGreg Banks 
510ff7d1179STrond Myklebust static ssize_t
511e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
512e333f3bbSTrond Myklebust 		const char *sep, unsigned vers, int minor)
513ff7d1179STrond Myklebust {
514abcb4dacSNeilBrown 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
515e333f3bbSTrond Myklebust 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
516ff7d1179STrond Myklebust 
517abcb4dacSNeilBrown 	if (vers == 4 && minor >= 0 &&
518e333f3bbSTrond Myklebust 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
519ff7d1179STrond Myklebust 		supported = false;
520abcb4dacSNeilBrown 	if (minor == 0 && supported)
521abcb4dacSNeilBrown 		/*
522abcb4dacSNeilBrown 		 * special case for backward compatability.
523abcb4dacSNeilBrown 		 * +4.0 is never reported, it is implied by
524abcb4dacSNeilBrown 		 * +4, unless -4.0 is present.
525abcb4dacSNeilBrown 		 */
526abcb4dacSNeilBrown 		return 0;
527ff7d1179STrond Myklebust 	return snprintf(buf, remaining, format, sep,
528ff7d1179STrond Myklebust 			supported ? '+' : '-', vers, minor);
529ff7d1179STrond Myklebust }
530ff7d1179STrond Myklebust 
5313dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size)
53270c3b76cSNeilBrown {
53370c3b76cSNeilBrown 	char *mesg = buf;
5348daf220aSBenny Halevy 	char *vers, *minorp, sign;
535261758b5SChuck Lever 	int len, num, remaining;
53670c3b76cSNeilBrown 	ssize_t tlen = 0;
53770c3b76cSNeilBrown 	char *sep;
538244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
53970c3b76cSNeilBrown 
54070c3b76cSNeilBrown 	if (size > 0) {
5419dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
5426658d3a7SNeilBrown 			/* Cannot change versions without updating
5439dd9845fSStanislav Kinsbursky 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
5446658d3a7SNeilBrown 			 * rq_argp and rq_resp
5456658d3a7SNeilBrown 			 */
54670c3b76cSNeilBrown 			return -EBUSY;
54770c3b76cSNeilBrown 		if (buf[size-1] != '\n')
54870c3b76cSNeilBrown 			return -EINVAL;
54970c3b76cSNeilBrown 		buf[size-1] = 0;
55039d432fcSChuck Lever 		trace_nfsd_ctl_version(netns(file), buf);
55170c3b76cSNeilBrown 
55270c3b76cSNeilBrown 		vers = mesg;
55370c3b76cSNeilBrown 		len = qword_get(&mesg, vers, size);
55470c3b76cSNeilBrown 		if (len <= 0) return -EINVAL;
55570c3b76cSNeilBrown 		do {
556d3635ff0STrond Myklebust 			enum vers_op cmd;
557abcb4dacSNeilBrown 			unsigned minor;
55870c3b76cSNeilBrown 			sign = *vers;
55970c3b76cSNeilBrown 			if (sign == '+' || sign == '-')
5608daf220aSBenny Halevy 				num = simple_strtol((vers+1), &minorp, 0);
56170c3b76cSNeilBrown 			else
5628daf220aSBenny Halevy 				num = simple_strtol(vers, &minorp, 0);
5638daf220aSBenny Halevy 			if (*minorp == '.') {
564ff89be87SJ. Bruce Fields 				if (num != 4)
5658daf220aSBenny Halevy 					return -EINVAL;
566e35659f1SNeilBrown 				if (kstrtouint(minorp+1, 0, &minor) < 0)
5678daf220aSBenny Halevy 					return -EINVAL;
568abcb4dacSNeilBrown 			}
569abcb4dacSNeilBrown 
570d3635ff0STrond Myklebust 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
57170c3b76cSNeilBrown 			switch(num) {
5722f3a4b2aSJeff Layton #ifdef CONFIG_NFSD_V2
57370c3b76cSNeilBrown 			case 2:
5742f3a4b2aSJeff Layton #endif
57570c3b76cSNeilBrown 			case 3:
576e333f3bbSTrond Myklebust 				nfsd_vers(nn, num, cmd);
577d3635ff0STrond Myklebust 				break;
57870c3b76cSNeilBrown 			case 4:
579abcb4dacSNeilBrown 				if (*minorp == '.') {
580e333f3bbSTrond Myklebust 					if (nfsd_minorversion(nn, minor, cmd) < 0)
581abcb4dacSNeilBrown 						return -EINVAL;
582e333f3bbSTrond Myklebust 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
583abcb4dacSNeilBrown 					/*
584abcb4dacSNeilBrown 					 * Either we have +4 and no minors are enabled,
585abcb4dacSNeilBrown 					 * or we have -4 and at least one minor is enabled.
586abcb4dacSNeilBrown 					 * In either case, propagate 'cmd' to all minors.
587abcb4dacSNeilBrown 					 */
588abcb4dacSNeilBrown 					minor = 0;
589e333f3bbSTrond Myklebust 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
590abcb4dacSNeilBrown 						minor++;
591abcb4dacSNeilBrown 				}
59270c3b76cSNeilBrown 				break;
59370c3b76cSNeilBrown 			default:
5948e823bafSJeff Layton 				/* Ignore requests to disable non-existent versions */
5958e823bafSJeff Layton 				if (cmd == NFSD_SET)
59670c3b76cSNeilBrown 					return -EINVAL;
59770c3b76cSNeilBrown 			}
59870c3b76cSNeilBrown 			vers += len + 1;
59970c3b76cSNeilBrown 		} while ((len = qword_get(&mesg, vers, size)) > 0);
60070c3b76cSNeilBrown 		/* If all get turned off, turn them back on, as
60170c3b76cSNeilBrown 		 * having no versions is BAD
60270c3b76cSNeilBrown 		 */
603e333f3bbSTrond Myklebust 		nfsd_reset_versions(nn);
60470c3b76cSNeilBrown 	}
605261758b5SChuck Lever 
60670c3b76cSNeilBrown 	/* Now write current state into reply buffer */
60770c3b76cSNeilBrown 	sep = "";
608261758b5SChuck Lever 	remaining = SIMPLE_TRANSACTION_LIMIT;
609ff7d1179STrond Myklebust 	for (num=2 ; num <= 4 ; num++) {
610abcb4dacSNeilBrown 		int minor;
611e333f3bbSTrond Myklebust 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
612e35659f1SNeilBrown 			continue;
613abcb4dacSNeilBrown 
614abcb4dacSNeilBrown 		minor = -1;
615ff7d1179STrond Myklebust 		do {
616e333f3bbSTrond Myklebust 			len = nfsd_print_version_support(nn, buf, remaining,
617ff7d1179STrond Myklebust 					sep, num, minor);
618818f2f57SDan Carpenter 			if (len >= remaining)
619ff7d1179STrond Myklebust 				goto out;
620261758b5SChuck Lever 			remaining -= len;
621261758b5SChuck Lever 			buf += len;
622261758b5SChuck Lever 			tlen += len;
623ff7d1179STrond Myklebust 			minor++;
624abcb4dacSNeilBrown 			if (len)
625ff7d1179STrond Myklebust 				sep = " ";
626ff7d1179STrond Myklebust 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
627261758b5SChuck Lever 	}
628ff7d1179STrond Myklebust out:
629261758b5SChuck Lever 	len = snprintf(buf, remaining, "\n");
630818f2f57SDan Carpenter 	if (len >= remaining)
631261758b5SChuck Lever 		return -EINVAL;
632261758b5SChuck Lever 	return tlen + len;
63370c3b76cSNeilBrown }
63470c3b76cSNeilBrown 
635f2453978SChuck Lever /*
636262a0982SChuck Lever  * write_versions - Set or report the available NFS protocol versions
637262a0982SChuck Lever  *
638262a0982SChuck Lever  * Input:
639262a0982SChuck Lever  *			buf:		ignored
640262a0982SChuck Lever  *			size:		zero
641262a0982SChuck Lever  * Output:
642262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
643262a0982SChuck Lever  *			string containing positive or negative integer
644262a0982SChuck Lever  *			values representing the current status of each
645262a0982SChuck Lever  *			protocol version;
646262a0982SChuck Lever  *			return code is the size in bytes of the string
647262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
648262a0982SChuck Lever  *
649262a0982SChuck Lever  * OR
650262a0982SChuck Lever  *
651262a0982SChuck Lever  * Input:
652262a0982SChuck Lever  *			buf:		C string containing whitespace-
653262a0982SChuck Lever  *					separated positive or negative
654262a0982SChuck Lever  *					integer values representing NFS
655262a0982SChuck Lever  *					protocol versions to enable ("+n")
656262a0982SChuck Lever  *					or disable ("-n")
657262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
658262a0982SChuck Lever  * Output:
659262a0982SChuck Lever  *	On success:	status of zero or more protocol versions has
660262a0982SChuck Lever  *			been updated; passed-in buffer filled with
661262a0982SChuck Lever  *			'\n'-terminated C string containing positive
662262a0982SChuck Lever  *			or negative integer values representing the
663262a0982SChuck Lever  *			current status of each protocol version;
664262a0982SChuck Lever  *			return code is the size in bytes of the string
665262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
666262a0982SChuck Lever  */
6673dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size)
6683dd98a3bSJeff Layton {
6693dd98a3bSJeff Layton 	ssize_t rv;
6703dd98a3bSJeff Layton 
6713dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
6723dd98a3bSJeff Layton 	rv = __write_versions(file, buf, size);
6733dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
6743dd98a3bSJeff Layton 	return rv;
6753dd98a3bSJeff Layton }
6763dd98a3bSJeff Layton 
6774cd5dc75SChuck Lever /*
6780a5372d8SChuck Lever  * Zero-length write.  Return a list of NFSD's current listener
6790a5372d8SChuck Lever  * transports.
6800a5372d8SChuck Lever  */
6819dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net)
6820a5372d8SChuck Lever {
6839dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6849dd9845fSStanislav Kinsbursky 
6859dd9845fSStanislav Kinsbursky 	if (nn->nfsd_serv == NULL)
6860a5372d8SChuck Lever 		return 0;
6879dd9845fSStanislav Kinsbursky 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
6880a5372d8SChuck Lever }
6890a5372d8SChuck Lever 
6900a5372d8SChuck Lever /*
6910b7c2f6fSChuck Lever  * A single 'fd' number was written, in which case it must be for
6920b7c2f6fSChuck Lever  * a socket of a supported family/protocol, and we use it as an
6930b7c2f6fSChuck Lever  * nfsd listener.
6940b7c2f6fSChuck Lever  */
6954df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
6960b7c2f6fSChuck Lever {
6970b7c2f6fSChuck Lever 	char *mesg = buf;
6980b7c2f6fSChuck Lever 	int fd, err;
6999dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
70064e63041SJeff Layton 	struct svc_serv *serv;
7010b7c2f6fSChuck Lever 
7020b7c2f6fSChuck Lever 	err = get_int(&mesg, &fd);
7030b7c2f6fSChuck Lever 	if (err != 0 || fd < 0)
7040b7c2f6fSChuck Lever 		return -EINVAL;
70539d432fcSChuck Lever 	trace_nfsd_ctl_ports_addfd(net, fd);
7060b7c2f6fSChuck Lever 
7076777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7080b7c2f6fSChuck Lever 	if (err != 0)
7090b7c2f6fSChuck Lever 		return err;
7100b7c2f6fSChuck Lever 
71164e63041SJeff Layton 	serv = nn->nfsd_serv;
71264e63041SJeff Layton 	err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
713ea068badSChuck Lever 
7141e3577a4SNeilBrown 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
715*17419aefSNeilBrown 		nfsd_destroy_serv(net);
716*17419aefSNeilBrown 
717ea068badSChuck Lever 	return err;
7180b7c2f6fSChuck Lever }
7190b7c2f6fSChuck Lever 
7200b7c2f6fSChuck Lever /*
721442a6290SChuck Lever  * A transport listener is added by writing its transport name and
7224eb68c26SChuck Lever  * a port number.
7234eb68c26SChuck Lever  */
7244df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
7254eb68c26SChuck Lever {
7264eb68c26SChuck Lever 	char transport[16];
72737498292SChuck Lever 	struct svc_xprt *xprt;
7284eb68c26SChuck Lever 	int port, err;
7299dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
73064e63041SJeff Layton 	struct svc_serv *serv;
7314eb68c26SChuck Lever 
732a10fded1SJ. Bruce Fields 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
7334eb68c26SChuck Lever 		return -EINVAL;
7344eb68c26SChuck Lever 
7354be929beSAlexey Dobriyan 	if (port < 1 || port > USHRT_MAX)
7364eb68c26SChuck Lever 		return -EINVAL;
73739d432fcSChuck Lever 	trace_nfsd_ctl_ports_addxprt(net, transport, port);
7384eb68c26SChuck Lever 
7396777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7404eb68c26SChuck Lever 	if (err != 0)
7414eb68c26SChuck Lever 		return err;
7424eb68c26SChuck Lever 
74364e63041SJeff Layton 	serv = nn->nfsd_serv;
74464e63041SJeff Layton 	err = svc_xprt_create(serv, transport, net,
7454df493a2STrond Myklebust 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
74668717908SChuck Lever 	if (err < 0)
74737498292SChuck Lever 		goto out_err;
74837498292SChuck Lever 
74964e63041SJeff Layton 	err = svc_xprt_create(serv, transport, net,
7504df493a2STrond Myklebust 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
75137498292SChuck Lever 	if (err < 0 && err != -EAFNOSUPPORT)
75237498292SChuck Lever 		goto out_close;
7530cd14a06SJeff Layton 
7544eb68c26SChuck Lever 	return 0;
75537498292SChuck Lever out_close:
75664e63041SJeff Layton 	xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
75737498292SChuck Lever 	if (xprt != NULL) {
7584355d767SChuck Lever 		svc_xprt_close(xprt);
75937498292SChuck Lever 		svc_xprt_put(xprt);
76037498292SChuck Lever 	}
76137498292SChuck Lever out_err:
7621e3577a4SNeilBrown 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
763*17419aefSNeilBrown 		nfsd_destroy_serv(net);
7642a501f55SNeilBrown 
76537498292SChuck Lever 	return err;
7664eb68c26SChuck Lever }
7674eb68c26SChuck Lever 
76808160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size,
76908160352SStanislav Kinsbursky 			     struct net *net)
77080212d59SNeilBrown {
7710a5372d8SChuck Lever 	if (size == 0)
7729dd9845fSStanislav Kinsbursky 		return __write_ports_names(buf, net);
7730b7c2f6fSChuck Lever 
7740b7c2f6fSChuck Lever 	if (isdigit(buf[0]))
7754df493a2STrond Myklebust 		return __write_ports_addfd(buf, net, file->f_cred);
77682d56591SChuck Lever 
7774eb68c26SChuck Lever 	if (isalpha(buf[0]))
7784df493a2STrond Myklebust 		return __write_ports_addxprt(buf, net, file->f_cred);
7794cd5dc75SChuck Lever 
780b41b66d6SNeilBrown 	return -EINVAL;
781b41b66d6SNeilBrown }
78280212d59SNeilBrown 
783f2453978SChuck Lever /*
784262a0982SChuck Lever  * write_ports - Pass a socket file descriptor or transport name to listen on
785262a0982SChuck Lever  *
786262a0982SChuck Lever  * Input:
787262a0982SChuck Lever  *			buf:		ignored
788262a0982SChuck Lever  *			size:		zero
789262a0982SChuck Lever  * Output:
790262a0982SChuck Lever  *	On success:	passed-in buffer filled with a '\n'-terminated C
791262a0982SChuck Lever  *			string containing a whitespace-separated list of
792262a0982SChuck Lever  *			named NFSD listeners;
793262a0982SChuck Lever  *			return code is the size in bytes of the string
794262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
795262a0982SChuck Lever  *
796262a0982SChuck Lever  * OR
797262a0982SChuck Lever  *
798262a0982SChuck Lever  * Input:
799262a0982SChuck Lever  *			buf:		C string containing an unsigned
800262a0982SChuck Lever  *					integer value representing a bound
801262a0982SChuck Lever  *					but unconnected socket that is to be
802c71206a7SChuck Lever  *					used as an NFSD listener; listen(3)
803c71206a7SChuck Lever  *					must be called for a SOCK_STREAM
804c71206a7SChuck Lever  *					socket, otherwise it is ignored
805262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
806262a0982SChuck Lever  * Output:
807262a0982SChuck Lever  *	On success:	NFS service is started;
808262a0982SChuck Lever  *			passed-in buffer filled with a '\n'-terminated C
809262a0982SChuck Lever  *			string containing a unique alphanumeric name of
810262a0982SChuck Lever  *			the listener;
811262a0982SChuck Lever  *			return code is the size in bytes of the string
812262a0982SChuck Lever  *	On error:	return code is a negative errno value
813262a0982SChuck Lever  *
814262a0982SChuck Lever  * OR
815262a0982SChuck Lever  *
816262a0982SChuck Lever  * Input:
817262a0982SChuck Lever  *			buf:		C string containing a transport
818262a0982SChuck Lever  *					name and an unsigned integer value
819262a0982SChuck Lever  *					representing the port to listen on,
820262a0982SChuck Lever  *					separated by whitespace
821262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
822262a0982SChuck Lever  * Output:
823262a0982SChuck Lever  *	On success:	returns zero; NFS service is started
824262a0982SChuck Lever  *	On error:	return code is a negative errno value
825262a0982SChuck Lever  */
826bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size)
827bedbdd8bSNeil Brown {
828bedbdd8bSNeil Brown 	ssize_t rv;
8293dd98a3bSJeff Layton 
830bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
831244c7d44SAl Viro 	rv = __write_ports(file, buf, size, netns(file));
832bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
833bedbdd8bSNeil Brown 	return rv;
834bedbdd8bSNeil Brown }
835bedbdd8bSNeil Brown 
836bedbdd8bSNeil Brown 
837596bbe53SNeilBrown int nfsd_max_blksize;
838596bbe53SNeilBrown 
839f2453978SChuck Lever /*
840262a0982SChuck Lever  * write_maxblksize - Set or report the current NFS blksize
841262a0982SChuck Lever  *
842262a0982SChuck Lever  * Input:
843262a0982SChuck Lever  *			buf:		ignored
844262a0982SChuck Lever  *			size:		zero
845262a0982SChuck Lever  *
846262a0982SChuck Lever  * OR
847262a0982SChuck Lever  *
848262a0982SChuck Lever  * Input:
849262a0982SChuck Lever  *			buf:		C string containing an unsigned
850262a0982SChuck Lever  *					integer value representing the new
851262a0982SChuck Lever  *					NFS blksize
852262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
853262a0982SChuck Lever  * Output:
854262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
855262a0982SChuck Lever  *			containing numeric value of the current NFS blksize
856262a0982SChuck Lever  *			setting;
857262a0982SChuck Lever  *			return code is the size in bytes of the string
858262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
859262a0982SChuck Lever  */
860596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
861596bbe53SNeilBrown {
862596bbe53SNeilBrown 	char *mesg = buf;
863244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
8649dd9845fSStanislav Kinsbursky 
865596bbe53SNeilBrown 	if (size > 0) {
866596bbe53SNeilBrown 		int bsize;
867596bbe53SNeilBrown 		int rv = get_int(&mesg, &bsize);
868596bbe53SNeilBrown 		if (rv)
869596bbe53SNeilBrown 			return rv;
87039d432fcSChuck Lever 		trace_nfsd_ctl_maxblksize(netns(file), bsize);
87139d432fcSChuck Lever 
872596bbe53SNeilBrown 		/* force bsize into allowed range and
873596bbe53SNeilBrown 		 * required alignment.
874596bbe53SNeilBrown 		 */
8753c7aa15dSKinglong Mee 		bsize = max_t(int, bsize, 1024);
8763c7aa15dSKinglong Mee 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
877596bbe53SNeilBrown 		bsize &= ~(1024-1);
878bedbdd8bSNeil Brown 		mutex_lock(&nfsd_mutex);
8799dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv) {
880bedbdd8bSNeil Brown 			mutex_unlock(&nfsd_mutex);
881596bbe53SNeilBrown 			return -EBUSY;
882596bbe53SNeilBrown 		}
883596bbe53SNeilBrown 		nfsd_max_blksize = bsize;
884bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
885596bbe53SNeilBrown 	}
886e06b6405SChuck Lever 
887e06b6405SChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
888e06b6405SChuck Lever 							nfsd_max_blksize);
889596bbe53SNeilBrown }
890596bbe53SNeilBrown 
891f2453978SChuck Lever /*
8925b8db00bSJeff Layton  * write_maxconn - Set or report the current max number of connections
8935b8db00bSJeff Layton  *
8945b8db00bSJeff Layton  * Input:
8955b8db00bSJeff Layton  *			buf:		ignored
8965b8db00bSJeff Layton  *			size:		zero
8975b8db00bSJeff Layton  * OR
8985b8db00bSJeff Layton  *
8995b8db00bSJeff Layton  * Input:
9005b8db00bSJeff Layton  *			buf:		C string containing an unsigned
9015b8db00bSJeff Layton  *					integer value representing the new
9025b8db00bSJeff Layton  *					number of max connections
9035b8db00bSJeff Layton  *			size:		non-zero length of C string in @buf
9045b8db00bSJeff Layton  * Output:
9055b8db00bSJeff Layton  *	On success:	passed-in buffer filled with '\n'-terminated C string
9065b8db00bSJeff Layton  *			containing numeric value of max_connections setting
9075b8db00bSJeff Layton  *			for this net namespace;
9085b8db00bSJeff Layton  *			return code is the size in bytes of the string
9095b8db00bSJeff Layton  *	On error:	return code is zero or a negative errno value
9105b8db00bSJeff Layton  */
9115b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
9125b8db00bSJeff Layton {
9135b8db00bSJeff Layton 	char *mesg = buf;
914244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9155b8db00bSJeff Layton 	unsigned int maxconn = nn->max_connections;
9165b8db00bSJeff Layton 
9175b8db00bSJeff Layton 	if (size > 0) {
9185b8db00bSJeff Layton 		int rv = get_uint(&mesg, &maxconn);
9195b8db00bSJeff Layton 
9205b8db00bSJeff Layton 		if (rv)
9215b8db00bSJeff Layton 			return rv;
92239d432fcSChuck Lever 		trace_nfsd_ctl_maxconn(netns(file), maxconn);
9235b8db00bSJeff Layton 		nn->max_connections = maxconn;
9245b8db00bSJeff Layton 	}
9255b8db00bSJeff Layton 
9265b8db00bSJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
9275b8db00bSJeff Layton }
9285b8db00bSJeff Layton 
92970c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
9309dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
93120b7d86fSArnd Bergmann 				  time64_t *time, struct nfsd_net *nn)
9321da177e4SLinus Torvalds {
93339d432fcSChuck Lever 	struct dentry *dentry = file_dentry(file);
9341da177e4SLinus Torvalds 	char *mesg = buf;
935f0135740SJ. Bruce Fields 	int rv, i;
9361da177e4SLinus Torvalds 
9371da177e4SLinus Torvalds 	if (size > 0) {
9389dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
9393dd98a3bSJeff Layton 			return -EBUSY;
940f0135740SJ. Bruce Fields 		rv = get_int(&mesg, &i);
9411da177e4SLinus Torvalds 		if (rv)
9421da177e4SLinus Torvalds 			return rv;
94339d432fcSChuck Lever 		trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
94439d432fcSChuck Lever 				    dentry->d_name.len, i);
94539d432fcSChuck Lever 
946e7b184f1SJ. Bruce Fields 		/*
947e7b184f1SJ. Bruce Fields 		 * Some sanity checking.  We don't have a reason for
948e7b184f1SJ. Bruce Fields 		 * these particular numbers, but problems with the
949e7b184f1SJ. Bruce Fields 		 * extremes are:
950e7b184f1SJ. Bruce Fields 		 *	- Too short: the briefest network outage may
951e7b184f1SJ. Bruce Fields 		 *	  cause clients to lose all their locks.  Also,
952e7b184f1SJ. Bruce Fields 		 *	  the frequent polling may be wasteful.
953e7b184f1SJ. Bruce Fields 		 *	- Too long: do you really want reboot recovery
954e7b184f1SJ. Bruce Fields 		 *	  to take more than an hour?  Or to make other
955e7b184f1SJ. Bruce Fields 		 *	  clients wait an hour before being able to
956e7b184f1SJ. Bruce Fields 		 *	  revoke a dead client's locks?
957e7b184f1SJ. Bruce Fields 		 */
958f0135740SJ. Bruce Fields 		if (i < 10 || i > 3600)
9591da177e4SLinus Torvalds 			return -EINVAL;
960f0135740SJ. Bruce Fields 		*time = i;
9611da177e4SLinus Torvalds 	}
962e06b6405SChuck Lever 
96320b7d86fSArnd Bergmann 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
964f0135740SJ. Bruce Fields }
965f0135740SJ. Bruce Fields 
9669dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
96720b7d86fSArnd Bergmann 				time64_t *time, struct nfsd_net *nn)
968f0135740SJ. Bruce Fields {
969f0135740SJ. Bruce Fields 	ssize_t rv;
970f0135740SJ. Bruce Fields 
971f0135740SJ. Bruce Fields 	mutex_lock(&nfsd_mutex);
9729dd9845fSStanislav Kinsbursky 	rv = __nfsd4_write_time(file, buf, size, time, nn);
973f0135740SJ. Bruce Fields 	mutex_unlock(&nfsd_mutex);
974f0135740SJ. Bruce Fields 	return rv;
9751da177e4SLinus Torvalds }
9761da177e4SLinus Torvalds 
977f2453978SChuck Lever /*
978262a0982SChuck Lever  * write_leasetime - Set or report the current NFSv4 lease time
979262a0982SChuck Lever  *
980262a0982SChuck Lever  * Input:
981262a0982SChuck Lever  *			buf:		ignored
982262a0982SChuck Lever  *			size:		zero
983262a0982SChuck Lever  *
984262a0982SChuck Lever  * OR
985262a0982SChuck Lever  *
986262a0982SChuck Lever  * Input:
987262a0982SChuck Lever  *			buf:		C string containing an unsigned
988262a0982SChuck Lever  *					integer value representing the new
989262a0982SChuck Lever  *					NFSv4 lease expiry time
990262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
991262a0982SChuck Lever  * Output:
992262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
993262a0982SChuck Lever  *			string containing unsigned integer value of the
994262a0982SChuck Lever  *			current lease expiry time;
995262a0982SChuck Lever  *			return code is the size in bytes of the string
996262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
997262a0982SChuck Lever  */
9983dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
9993dd98a3bSJeff Layton {
1000244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10019dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
10023dd98a3bSJeff Layton }
10033dd98a3bSJeff Layton 
1004f2453978SChuck Lever /*
1005efc4bb4fSJ. Bruce Fields  * write_gracetime - Set or report current NFSv4 grace period time
1006efc4bb4fSJ. Bruce Fields  *
1007efc4bb4fSJ. Bruce Fields  * As above, but sets the time of the NFSv4 grace period.
1008efc4bb4fSJ. Bruce Fields  *
1009efc4bb4fSJ. Bruce Fields  * Note this should never be set to less than the *previous*
1010efc4bb4fSJ. Bruce Fields  * lease-period time, but we don't try to enforce this.  (In the common
1011efc4bb4fSJ. Bruce Fields  * case (a new boot), we don't know what the previous lease time was
1012efc4bb4fSJ. Bruce Fields  * anyway.)
1013efc4bb4fSJ. Bruce Fields  */
1014efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1015efc4bb4fSJ. Bruce Fields {
1016244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10179dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1018efc4bb4fSJ. Bruce Fields }
1019efc4bb4fSJ. Bruce Fields 
102074fd4873SJeff Layton #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
10219dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
10229dd9845fSStanislav Kinsbursky 				   struct nfsd_net *nn)
10230964a3d3SNeilBrown {
10240964a3d3SNeilBrown 	char *mesg = buf;
10250964a3d3SNeilBrown 	char *recdir;
10260964a3d3SNeilBrown 	int len, status;
10270964a3d3SNeilBrown 
10283dd98a3bSJeff Layton 	if (size > 0) {
10299dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
10303dd98a3bSJeff Layton 			return -EBUSY;
10313dd98a3bSJeff Layton 		if (size > PATH_MAX || buf[size-1] != '\n')
10320964a3d3SNeilBrown 			return -EINVAL;
10330964a3d3SNeilBrown 		buf[size-1] = 0;
10340964a3d3SNeilBrown 
10350964a3d3SNeilBrown 		recdir = mesg;
10360964a3d3SNeilBrown 		len = qword_get(&mesg, recdir, size);
10370964a3d3SNeilBrown 		if (len <= 0)
10380964a3d3SNeilBrown 			return -EINVAL;
103939d432fcSChuck Lever 		trace_nfsd_ctl_recoverydir(netns(file), recdir);
10400964a3d3SNeilBrown 
10410964a3d3SNeilBrown 		status = nfs4_reset_recoverydir(recdir);
104269049961SAndi Kleen 		if (status)
104369049961SAndi Kleen 			return status;
10443dd98a3bSJeff Layton 	}
10453d72ab8fSChuck Lever 
10463d72ab8fSChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
10473d72ab8fSChuck Lever 							nfs4_recoverydir());
10480964a3d3SNeilBrown }
10493dd98a3bSJeff Layton 
1050f2453978SChuck Lever /*
1051262a0982SChuck Lever  * write_recoverydir - Set or report the pathname of the recovery directory
1052262a0982SChuck Lever  *
1053262a0982SChuck Lever  * Input:
1054262a0982SChuck Lever  *			buf:		ignored
1055262a0982SChuck Lever  *			size:		zero
1056262a0982SChuck Lever  *
1057262a0982SChuck Lever  * OR
1058262a0982SChuck Lever  *
1059262a0982SChuck Lever  * Input:
1060262a0982SChuck Lever  *			buf:		C string containing the pathname
1061262a0982SChuck Lever  *					of the directory on a local file
1062262a0982SChuck Lever  *					system containing permanent NFSv4
1063262a0982SChuck Lever  *					recovery data
1064262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1065262a0982SChuck Lever  * Output:
1066262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
1067262a0982SChuck Lever  *			containing the current recovery pathname setting;
1068262a0982SChuck Lever  *			return code is the size in bytes of the string
1069262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1070262a0982SChuck Lever  */
10713dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
10723dd98a3bSJeff Layton {
10733dd98a3bSJeff Layton 	ssize_t rv;
1074244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10753dd98a3bSJeff Layton 
10763dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
10779dd9845fSStanislav Kinsbursky 	rv = __write_recoverydir(file, buf, size, nn);
10783dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
10793dd98a3bSJeff Layton 	return rv;
10803dd98a3bSJeff Layton }
108174fd4873SJeff Layton #endif
10823dd98a3bSJeff Layton 
1083f2453978SChuck Lever /*
10847f5ef2e9SJeff Layton  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
10857f5ef2e9SJeff Layton  *
10867f5ef2e9SJeff Layton  * Input:
10877f5ef2e9SJeff Layton  *			buf:		ignored
10887f5ef2e9SJeff Layton  *			size:		zero
10897f5ef2e9SJeff Layton  * OR
10907f5ef2e9SJeff Layton  *
10917f5ef2e9SJeff Layton  * Input:
10927f5ef2e9SJeff Layton  *			buf:		any value
10937f5ef2e9SJeff Layton  *			size:		non-zero length of C string in @buf
10947f5ef2e9SJeff Layton  * Output:
10957f5ef2e9SJeff Layton  *			passed-in buffer filled with "Y" or "N" with a newline
10967f5ef2e9SJeff Layton  *			and NULL-terminated C string. This indicates whether
10977f5ef2e9SJeff Layton  *			the grace period has ended in the current net
10987f5ef2e9SJeff Layton  *			namespace. Return code is the size in bytes of the
10997f5ef2e9SJeff Layton  *			string. Writing a string that starts with 'Y', 'y', or
11007f5ef2e9SJeff Layton  *			'1' to the file will end the grace period for nfsd's v4
11017f5ef2e9SJeff Layton  *			lock manager.
11027f5ef2e9SJeff Layton  */
11037f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
11047f5ef2e9SJeff Layton {
1105244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
11067f5ef2e9SJeff Layton 
11077f5ef2e9SJeff Layton 	if (size > 0) {
11087f5ef2e9SJeff Layton 		switch(buf[0]) {
11097f5ef2e9SJeff Layton 		case 'Y':
11107f5ef2e9SJeff Layton 		case 'y':
11117f5ef2e9SJeff Layton 		case '1':
1112dd838821SYihao Wu 			if (!nn->nfsd_serv)
111362a063b8SJ. Bruce Fields 				return -EBUSY;
111439d432fcSChuck Lever 			trace_nfsd_end_grace(netns(file));
11158073a98eSChuck Lever 			nfsd4_end_grace(nn);
11167f5ef2e9SJeff Layton 			break;
11177f5ef2e9SJeff Layton 		default:
11187f5ef2e9SJeff Layton 			return -EINVAL;
11197f5ef2e9SJeff Layton 		}
11207f5ef2e9SJeff Layton 	}
11217f5ef2e9SJeff Layton 
11227f5ef2e9SJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
11237f5ef2e9SJeff Layton 			 nn->grace_ended ? 'Y' : 'N');
11247f5ef2e9SJeff Layton }
11257f5ef2e9SJeff Layton 
112670c3b76cSNeilBrown #endif
11270964a3d3SNeilBrown 
11281da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
11291da177e4SLinus Torvalds /*
11301da177e4SLinus Torvalds  *	populating the filesystem.
11311da177e4SLinus Torvalds  */
11321da177e4SLinus Torvalds 
1133e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */
1134e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1135e8a79fb1SJ. Bruce Fields {
1136e8a79fb1SJ. Bruce Fields 	struct inode *inode = new_inode(sb);
1137e8a79fb1SJ. Bruce Fields 	if (!inode)
1138e8a79fb1SJ. Bruce Fields 		return NULL;
1139e8a79fb1SJ. Bruce Fields 	/* Following advice from simple_fill_super documentation: */
1140e8a79fb1SJ. Bruce Fields 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1141e8a79fb1SJ. Bruce Fields 	inode->i_mode = mode;
114211fec9b9SJeff Layton 	simple_inode_init_ts(inode);
1143e8a79fb1SJ. Bruce Fields 	switch (mode & S_IFMT) {
1144e8a79fb1SJ. Bruce Fields 	case S_IFDIR:
1145e8a79fb1SJ. Bruce Fields 		inode->i_fop = &simple_dir_operations;
1146e8a79fb1SJ. Bruce Fields 		inode->i_op = &simple_dir_inode_operations;
1147e8a79fb1SJ. Bruce Fields 		inc_nlink(inode);
114876c50eb7SGustavo A. R. Silva 		break;
11494df750c9SChuck Lever 	case S_IFLNK:
11504df750c9SChuck Lever 		inode->i_op = &simple_symlink_inode_operations;
11514df750c9SChuck Lever 		break;
1152e8a79fb1SJ. Bruce Fields 	default:
1153e8a79fb1SJ. Bruce Fields 		break;
1154e8a79fb1SJ. Bruce Fields 	}
1155e8a79fb1SJ. Bruce Fields 	return inode;
1156e8a79fb1SJ. Bruce Fields }
1157e8a79fb1SJ. Bruce Fields 
1158bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1159e8a79fb1SJ. Bruce Fields {
1160e8a79fb1SJ. Bruce Fields 	struct inode *inode;
1161e8a79fb1SJ. Bruce Fields 
1162e8a79fb1SJ. Bruce Fields 	inode = nfsd_get_inode(dir->i_sb, mode);
1163e8a79fb1SJ. Bruce Fields 	if (!inode)
1164e8a79fb1SJ. Bruce Fields 		return -ENOMEM;
1165bebd6997SJ. Bruce Fields 	if (ncl) {
1166bebd6997SJ. Bruce Fields 		inode->i_private = ncl;
1167bebd6997SJ. Bruce Fields 		kref_get(&ncl->cl_ref);
1168bebd6997SJ. Bruce Fields 	}
1169e8a79fb1SJ. Bruce Fields 	d_add(dentry, inode);
1170e8a79fb1SJ. Bruce Fields 	inc_nlink(dir);
1171e8a79fb1SJ. Bruce Fields 	fsnotify_mkdir(dir, dentry);
1172e8a79fb1SJ. Bruce Fields 	return 0;
1173e8a79fb1SJ. Bruce Fields }
1174e8a79fb1SJ. Bruce Fields 
1175e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1176e8a79fb1SJ. Bruce Fields {
1177e8a79fb1SJ. Bruce Fields 	struct inode *dir = parent->d_inode;
1178e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1179e8a79fb1SJ. Bruce Fields 	int ret = -ENOMEM;
1180e8a79fb1SJ. Bruce Fields 
1181e8a79fb1SJ. Bruce Fields 	inode_lock(dir);
1182e8a79fb1SJ. Bruce Fields 	dentry = d_alloc_name(parent, name);
1183e8a79fb1SJ. Bruce Fields 	if (!dentry)
1184e8a79fb1SJ. Bruce Fields 		goto out_err;
1185bebd6997SJ. Bruce Fields 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1186e8a79fb1SJ. Bruce Fields 	if (ret)
1187e8a79fb1SJ. Bruce Fields 		goto out_err;
1188e8a79fb1SJ. Bruce Fields out:
1189e8a79fb1SJ. Bruce Fields 	inode_unlock(dir);
1190e8a79fb1SJ. Bruce Fields 	return dentry;
1191e8a79fb1SJ. Bruce Fields out_err:
1192d6846bfbSTetsuo Handa 	dput(dentry);
1193e8a79fb1SJ. Bruce Fields 	dentry = ERR_PTR(ret);
1194e8a79fb1SJ. Bruce Fields 	goto out;
1195e8a79fb1SJ. Bruce Fields }
1196e8a79fb1SJ. Bruce Fields 
11974df750c9SChuck Lever #if IS_ENABLED(CONFIG_SUNRPC_GSS)
11984df750c9SChuck Lever static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
11994df750c9SChuck Lever 			  umode_t mode, const char *content)
12004df750c9SChuck Lever {
12014df750c9SChuck Lever 	struct inode *inode;
12024df750c9SChuck Lever 
12034df750c9SChuck Lever 	inode = nfsd_get_inode(dir->i_sb, mode);
12044df750c9SChuck Lever 	if (!inode)
12054df750c9SChuck Lever 		return -ENOMEM;
12064df750c9SChuck Lever 
12074df750c9SChuck Lever 	inode->i_link = (char *)content;
12084df750c9SChuck Lever 	inode->i_size = strlen(content);
12094df750c9SChuck Lever 
12104df750c9SChuck Lever 	d_add(dentry, inode);
12114df750c9SChuck Lever 	inc_nlink(dir);
12124df750c9SChuck Lever 	fsnotify_create(dir, dentry);
12134df750c9SChuck Lever 	return 0;
12144df750c9SChuck Lever }
12154df750c9SChuck Lever 
12164df750c9SChuck Lever /*
12174df750c9SChuck Lever  * @content is assumed to be a NUL-terminated string that lives
12184df750c9SChuck Lever  * longer than the symlink itself.
12194df750c9SChuck Lever  */
122039d432fcSChuck Lever static void _nfsd_symlink(struct dentry *parent, const char *name,
12214df750c9SChuck Lever 			  const char *content)
12224df750c9SChuck Lever {
12234df750c9SChuck Lever 	struct inode *dir = parent->d_inode;
12244df750c9SChuck Lever 	struct dentry *dentry;
12254b471a8bSChuck Lever 	int ret;
12264df750c9SChuck Lever 
12274df750c9SChuck Lever 	inode_lock(dir);
12284df750c9SChuck Lever 	dentry = d_alloc_name(parent, name);
12294df750c9SChuck Lever 	if (!dentry)
12304b471a8bSChuck Lever 		goto out;
12314df750c9SChuck Lever 	ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
12324df750c9SChuck Lever 	if (ret)
12334b471a8bSChuck Lever 		dput(dentry);
12344df750c9SChuck Lever out:
12354df750c9SChuck Lever 	inode_unlock(dir);
12364df750c9SChuck Lever }
12374df750c9SChuck Lever #else
123839d432fcSChuck Lever static inline void _nfsd_symlink(struct dentry *parent, const char *name,
12394df750c9SChuck Lever 				 const char *content)
12404df750c9SChuck Lever {
12414df750c9SChuck Lever }
12424df750c9SChuck Lever 
12434df750c9SChuck Lever #endif
12444df750c9SChuck Lever 
124597ad4031SJ. Bruce Fields static void clear_ncl(struct inode *inode)
1246e8a79fb1SJ. Bruce Fields {
124797ad4031SJ. Bruce Fields 	struct nfsdfs_client *ncl = inode->i_private;
124897ad4031SJ. Bruce Fields 
124997ad4031SJ. Bruce Fields 	inode->i_private = NULL;
125097ad4031SJ. Bruce Fields 	kref_put(&ncl->cl_ref, ncl->cl_release);
125197ad4031SJ. Bruce Fields }
125297ad4031SJ. Bruce Fields 
1253b78fa45dSYueHaibing static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
125497ad4031SJ. Bruce Fields {
125597ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc = inode->i_private;
125697ad4031SJ. Bruce Fields 
125797ad4031SJ. Bruce Fields 	if (nc)
125897ad4031SJ. Bruce Fields 		kref_get(&nc->cl_ref);
125997ad4031SJ. Bruce Fields 	return nc;
126097ad4031SJ. Bruce Fields }
126197ad4031SJ. Bruce Fields 
126297ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
126397ad4031SJ. Bruce Fields {
126497ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc;
126597ad4031SJ. Bruce Fields 
1266dc46bba7SJ. Bruce Fields 	inode_lock_shared(inode);
126797ad4031SJ. Bruce Fields 	nc = __get_nfsdfs_client(inode);
1268dc46bba7SJ. Bruce Fields 	inode_unlock_shared(inode);
126997ad4031SJ. Bruce Fields 	return nc;
127097ad4031SJ. Bruce Fields }
127197ad4031SJ. Bruce Fields /* from __rpc_unlink */
127297ad4031SJ. Bruce Fields static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
127397ad4031SJ. Bruce Fields {
127497ad4031SJ. Bruce Fields 	int ret;
127597ad4031SJ. Bruce Fields 
127697ad4031SJ. Bruce Fields 	clear_ncl(d_inode(dentry));
127797ad4031SJ. Bruce Fields 	dget(dentry);
127897ad4031SJ. Bruce Fields 	ret = simple_unlink(dir, dentry);
127929044daeSAmir Goldstein 	d_drop(dentry);
128029044daeSAmir Goldstein 	fsnotify_unlink(dir, dentry);
128197ad4031SJ. Bruce Fields 	dput(dentry);
128297ad4031SJ. Bruce Fields 	WARN_ON_ONCE(ret);
128397ad4031SJ. Bruce Fields }
128497ad4031SJ. Bruce Fields 
128597ad4031SJ. Bruce Fields static void nfsdfs_remove_files(struct dentry *root)
128697ad4031SJ. Bruce Fields {
128797ad4031SJ. Bruce Fields 	struct dentry *dentry, *tmp;
128897ad4031SJ. Bruce Fields 
128997ad4031SJ. Bruce Fields 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
129097ad4031SJ. Bruce Fields 		if (!simple_positive(dentry)) {
129197ad4031SJ. Bruce Fields 			WARN_ON_ONCE(1); /* I think this can't happen? */
129297ad4031SJ. Bruce Fields 			continue;
129397ad4031SJ. Bruce Fields 		}
129497ad4031SJ. Bruce Fields 		nfsdfs_remove_file(d_inode(root), dentry);
129597ad4031SJ. Bruce Fields 	}
129697ad4031SJ. Bruce Fields }
129797ad4031SJ. Bruce Fields 
129897ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
129997ad4031SJ. Bruce Fields  * code instead. */
130097ad4031SJ. Bruce Fields static  int nfsdfs_create_files(struct dentry *root,
1301472d155aSNeilBrown 				const struct tree_descr *files,
1302472d155aSNeilBrown 				struct dentry **fdentries)
130397ad4031SJ. Bruce Fields {
130497ad4031SJ. Bruce Fields 	struct inode *dir = d_inode(root);
130597ad4031SJ. Bruce Fields 	struct inode *inode;
130697ad4031SJ. Bruce Fields 	struct dentry *dentry;
130797ad4031SJ. Bruce Fields 	int i;
130897ad4031SJ. Bruce Fields 
130997ad4031SJ. Bruce Fields 	inode_lock(dir);
131097ad4031SJ. Bruce Fields 	for (i = 0; files->name && files->name[0]; i++, files++) {
131197ad4031SJ. Bruce Fields 		dentry = d_alloc_name(root, files->name);
131297ad4031SJ. Bruce Fields 		if (!dentry)
131397ad4031SJ. Bruce Fields 			goto out;
131497ad4031SJ. Bruce Fields 		inode = nfsd_get_inode(d_inode(root)->i_sb,
131597ad4031SJ. Bruce Fields 					S_IFREG | files->mode);
131697ad4031SJ. Bruce Fields 		if (!inode) {
131797ad4031SJ. Bruce Fields 			dput(dentry);
131897ad4031SJ. Bruce Fields 			goto out;
131997ad4031SJ. Bruce Fields 		}
132097ad4031SJ. Bruce Fields 		inode->i_fop = files->ops;
132197ad4031SJ. Bruce Fields 		inode->i_private = __get_nfsdfs_client(dir);
132297ad4031SJ. Bruce Fields 		d_add(dentry, inode);
132397ad4031SJ. Bruce Fields 		fsnotify_create(dir, dentry);
1324472d155aSNeilBrown 		if (fdentries)
1325472d155aSNeilBrown 			fdentries[i] = dentry;
132697ad4031SJ. Bruce Fields 	}
132797ad4031SJ. Bruce Fields 	inode_unlock(dir);
132897ad4031SJ. Bruce Fields 	return 0;
132997ad4031SJ. Bruce Fields out:
133097ad4031SJ. Bruce Fields 	nfsdfs_remove_files(root);
133197ad4031SJ. Bruce Fields 	inode_unlock(dir);
133297ad4031SJ. Bruce Fields 	return -ENOMEM;
133397ad4031SJ. Bruce Fields }
133497ad4031SJ. Bruce Fields 
133597ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */
133697ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
133797ad4031SJ. Bruce Fields 				 struct nfsdfs_client *ncl, u32 id,
1338472d155aSNeilBrown 				 const struct tree_descr *files,
1339472d155aSNeilBrown 				 struct dentry **fdentries)
134097ad4031SJ. Bruce Fields {
134197ad4031SJ. Bruce Fields 	struct dentry *dentry;
1342e8a79fb1SJ. Bruce Fields 	char name[11];
134397ad4031SJ. Bruce Fields 	int ret;
1344e8a79fb1SJ. Bruce Fields 
1345bf5ed3e3SJ. Bruce Fields 	sprintf(name, "%u", id);
1346e8a79fb1SJ. Bruce Fields 
134797ad4031SJ. Bruce Fields 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
134897ad4031SJ. Bruce Fields 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
134997ad4031SJ. Bruce Fields 		return NULL;
1350472d155aSNeilBrown 	ret = nfsdfs_create_files(dentry, files, fdentries);
135197ad4031SJ. Bruce Fields 	if (ret) {
135297ad4031SJ. Bruce Fields 		nfsd_client_rmdir(dentry);
135397ad4031SJ. Bruce Fields 		return NULL;
135497ad4031SJ. Bruce Fields 	}
135597ad4031SJ. Bruce Fields 	return dentry;
1356e8a79fb1SJ. Bruce Fields }
1357e8a79fb1SJ. Bruce Fields 
1358e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */
1359e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry)
1360e8a79fb1SJ. Bruce Fields {
1361e8a79fb1SJ. Bruce Fields 	struct inode *dir = d_inode(dentry->d_parent);
1362e8a79fb1SJ. Bruce Fields 	struct inode *inode = d_inode(dentry);
1363e8a79fb1SJ. Bruce Fields 	int ret;
1364e8a79fb1SJ. Bruce Fields 
136597ad4031SJ. Bruce Fields 	inode_lock(dir);
136697ad4031SJ. Bruce Fields 	nfsdfs_remove_files(dentry);
136797ad4031SJ. Bruce Fields 	clear_ncl(inode);
1368e8a79fb1SJ. Bruce Fields 	dget(dentry);
1369e8a79fb1SJ. Bruce Fields 	ret = simple_rmdir(dir, dentry);
1370e8a79fb1SJ. Bruce Fields 	WARN_ON_ONCE(ret);
137129044daeSAmir Goldstein 	d_drop(dentry);
137269afd267SJ. Bruce Fields 	fsnotify_rmdir(dir, dentry);
1373bf265401SJ. Bruce Fields 	dput(dentry);
137497ad4031SJ. Bruce Fields 	inode_unlock(dir);
1375e8a79fb1SJ. Bruce Fields }
1376e8a79fb1SJ. Bruce Fields 
137796a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
13781da177e4SLinus Torvalds {
1379e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1380e8a79fb1SJ. Bruce Fields 							nfsd_net_id);
1381e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1382e8a79fb1SJ. Bruce Fields 	int ret;
1383e8a79fb1SJ. Bruce Fields 
1384cda37124SEric Biggers 	static const struct tree_descr nfsd_files[] = {
138596d851c4SStanislav Kinsbursky 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
138620ad856eSAmir Goldstein 		/* Per-export io stats use same ops as exports file */
138720ad856eSAmir Goldstein 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1388e8e8753fSJ. Bruce Fields 		[NFSD_Export_features] = {"export_features",
13899beeaab8SChenXiaoSong 					&export_features_fops, S_IRUGO},
13904373ea84SWendy Cheng 		[NFSD_FO_UnlockIP] = {"unlock_ip",
13914373ea84SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
139217efa372SWendy Cheng 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
139317efa372SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
13941da177e4SLinus Torvalds 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
13951da177e4SLinus Torvalds 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1396eed2965aSGreg Banks 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
139703cf6c9fSGreg Banks 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
139864776611SChenXiaoSong 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
139964776611SChenXiaoSong 					&nfsd_reply_cache_stats_fops, S_IRUGO},
140070c3b76cSNeilBrown 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
140180212d59SNeilBrown 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1402596bbe53SNeilBrown 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
14035b8db00bSJeff Layton 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
14041342f9ddSChenXiaoSong 		[NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
14051da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4
14061da177e4SLinus Torvalds 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1407efc4bb4fSJ. Bruce Fields 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
14080964a3d3SNeilBrown 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
14097f5ef2e9SJeff Layton 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
14101da177e4SLinus Torvalds #endif
14111da177e4SLinus Torvalds 		/* last one */ {""}
14121da177e4SLinus Torvalds 	};
141396a374a3SDavid Howells 
1414e8a79fb1SJ. Bruce Fields 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1415e8a79fb1SJ. Bruce Fields 	if (ret)
1416e8a79fb1SJ. Bruce Fields 		return ret;
141739d432fcSChuck Lever 	_nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
14184df750c9SChuck Lever 		      "/proc/net/rpc/gss_krb5_enctypes");
1419e8a79fb1SJ. Bruce Fields 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1420e8a79fb1SJ. Bruce Fields 	if (IS_ERR(dentry))
1421e8a79fb1SJ. Bruce Fields 		return PTR_ERR(dentry);
1422e8a79fb1SJ. Bruce Fields 	nn->nfsd_client_dir = dentry;
1423e8a79fb1SJ. Bruce Fields 	return 0;
14241da177e4SLinus Torvalds }
14251da177e4SLinus Torvalds 
142696a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc)
14271da177e4SLinus Torvalds {
1428533770ccSAl Viro 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
142996a374a3SDavid Howells }
143096a374a3SDavid Howells 
143196a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc)
143296a374a3SDavid Howells {
143396a374a3SDavid Howells 	if (fc->s_fs_info)
143496a374a3SDavid Howells 		put_net(fc->s_fs_info);
143596a374a3SDavid Howells }
143696a374a3SDavid Howells 
143796a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = {
143896a374a3SDavid Howells 	.free		= nfsd_fs_free_fc,
143996a374a3SDavid Howells 	.get_tree	= nfsd_fs_get_tree,
144096a374a3SDavid Howells };
144196a374a3SDavid Howells 
144296a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc)
144396a374a3SDavid Howells {
144496a374a3SDavid Howells 	put_user_ns(fc->user_ns);
144596a374a3SDavid Howells 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
144696a374a3SDavid Howells 	fc->ops = &nfsd_fs_context_ops;
144796a374a3SDavid Howells 	return 0;
144811f77942SStanislav Kinsbursky }
144911f77942SStanislav Kinsbursky 
145011f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb)
145111f77942SStanislav Kinsbursky {
145211f77942SStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
145311f77942SStanislav Kinsbursky 
1454c6c7f2a8STrond Myklebust 	nfsd_shutdown_threads(net);
1455c6c7f2a8STrond Myklebust 
145611f77942SStanislav Kinsbursky 	kill_litter_super(sb);
145711f77942SStanislav Kinsbursky 	put_net(net);
14581da177e4SLinus Torvalds }
14591da177e4SLinus Torvalds 
14601da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = {
14611da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
14621da177e4SLinus Torvalds 	.name		= "nfsd",
146396a374a3SDavid Howells 	.init_fs_context = nfsd_init_fs_context,
146411f77942SStanislav Kinsbursky 	.kill_sb	= nfsd_umount,
14651da177e4SLinus Torvalds };
14667f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd");
14671da177e4SLinus Torvalds 
1468e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS
1469340086daSTom Rix 
1470340086daSTom Rix static int exports_proc_open(struct inode *inode, struct file *file)
1471340086daSTom Rix {
1472340086daSTom Rix 	return exports_net_open(current->nsproxy->net_ns, file);
1473340086daSTom Rix }
1474340086daSTom Rix 
1475340086daSTom Rix static const struct proc_ops exports_proc_ops = {
1476340086daSTom Rix 	.proc_open	= exports_proc_open,
1477340086daSTom Rix 	.proc_read	= seq_read,
1478340086daSTom Rix 	.proc_lseek	= seq_lseek,
1479340086daSTom Rix 	.proc_release	= seq_release,
1480340086daSTom Rix };
1481340086daSTom Rix 
1482e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1483e331f606SJ. Bruce Fields {
1484e331f606SJ. Bruce Fields 	struct proc_dir_entry *entry;
1485e331f606SJ. Bruce Fields 
1486e331f606SJ. Bruce Fields 	entry = proc_mkdir("fs/nfs", NULL);
1487e331f606SJ. Bruce Fields 	if (!entry)
1488e331f606SJ. Bruce Fields 		return -ENOMEM;
148997a32539SAlexey Dobriyan 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1490ff7c4b36Sfanchaoting 	if (!entry) {
1491ff7c4b36Sfanchaoting 		remove_proc_entry("fs/nfs", NULL);
1492e331f606SJ. Bruce Fields 		return -ENOMEM;
1493ff7c4b36Sfanchaoting 	}
1494e331f606SJ. Bruce Fields 	return 0;
1495e331f606SJ. Bruce Fields }
1496e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */
1497e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1498e331f606SJ. Bruce Fields {
1499e331f606SJ. Bruce Fields 	return 0;
1500e331f606SJ. Bruce Fields }
1501e331f606SJ. Bruce Fields #endif
1502e331f606SJ. Bruce Fields 
1503c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id;
15045717e012SStanislav Kinsbursky 
15055e092be7SChuck Lever /**
1506bd9d6a3eSLorenzo Bianconi  * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit
1507bd9d6a3eSLorenzo Bianconi  * @cb: netlink metadata and command arguments
1508bd9d6a3eSLorenzo Bianconi  *
1509bd9d6a3eSLorenzo Bianconi  * Return values:
1510bd9d6a3eSLorenzo Bianconi  *   %0: The rpc_status_get command may proceed
1511bd9d6a3eSLorenzo Bianconi  *   %-ENODEV: There is no NFSD running in this namespace
1512bd9d6a3eSLorenzo Bianconi  */
151313727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb)
151413727f85SLorenzo Bianconi {
1515bd9d6a3eSLorenzo Bianconi 	struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id);
1516bd9d6a3eSLorenzo Bianconi 	int ret = -ENODEV;
1517bd9d6a3eSLorenzo Bianconi 
1518bd9d6a3eSLorenzo Bianconi 	mutex_lock(&nfsd_mutex);
15191bd773b4SNeilBrown 	if (nn->nfsd_serv)
1520bd9d6a3eSLorenzo Bianconi 		ret = 0;
15211bd773b4SNeilBrown 	else
1522bd9d6a3eSLorenzo Bianconi 		mutex_unlock(&nfsd_mutex);
1523bd9d6a3eSLorenzo Bianconi 
1524bd9d6a3eSLorenzo Bianconi 	return ret;
1525bd9d6a3eSLorenzo Bianconi }
1526bd9d6a3eSLorenzo Bianconi 
1527bd9d6a3eSLorenzo Bianconi static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1528bd9d6a3eSLorenzo Bianconi 					    struct netlink_callback *cb,
1529bd9d6a3eSLorenzo Bianconi 					    struct nfsd_genl_rqstp *rqstp)
1530bd9d6a3eSLorenzo Bianconi {
1531bd9d6a3eSLorenzo Bianconi 	void *hdr;
1532bd9d6a3eSLorenzo Bianconi 	u32 i;
1533bd9d6a3eSLorenzo Bianconi 
1534bd9d6a3eSLorenzo Bianconi 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1535bd9d6a3eSLorenzo Bianconi 			  &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1536bd9d6a3eSLorenzo Bianconi 	if (!hdr)
1537bd9d6a3eSLorenzo Bianconi 		return -ENOBUFS;
1538bd9d6a3eSLorenzo Bianconi 
1539bd9d6a3eSLorenzo Bianconi 	if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1540bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1541bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1542bd9d6a3eSLorenzo Bianconi 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1543bd9d6a3eSLorenzo Bianconi 	    nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1544bd9d6a3eSLorenzo Bianconi 	    nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1545bd9d6a3eSLorenzo Bianconi 			ktime_to_us(rqstp->rq_stime),
1546bd9d6a3eSLorenzo Bianconi 			NFSD_A_RPC_STATUS_PAD))
1547bd9d6a3eSLorenzo Bianconi 		return -ENOBUFS;
1548bd9d6a3eSLorenzo Bianconi 
1549bd9d6a3eSLorenzo Bianconi 	switch (rqstp->rq_saddr.sa_family) {
1550bd9d6a3eSLorenzo Bianconi 	case AF_INET: {
1551bd9d6a3eSLorenzo Bianconi 		const struct sockaddr_in *s_in, *d_in;
1552bd9d6a3eSLorenzo Bianconi 
1553bd9d6a3eSLorenzo Bianconi 		s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1554bd9d6a3eSLorenzo Bianconi 		d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1555bd9d6a3eSLorenzo Bianconi 		if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1556bd9d6a3eSLorenzo Bianconi 				    s_in->sin_addr.s_addr) ||
1557bd9d6a3eSLorenzo Bianconi 		    nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1558bd9d6a3eSLorenzo Bianconi 				    d_in->sin_addr.s_addr) ||
1559bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1560bd9d6a3eSLorenzo Bianconi 				 s_in->sin_port) ||
1561bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1562bd9d6a3eSLorenzo Bianconi 				 d_in->sin_port))
1563bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1564bd9d6a3eSLorenzo Bianconi 		break;
1565bd9d6a3eSLorenzo Bianconi 	}
1566bd9d6a3eSLorenzo Bianconi 	case AF_INET6: {
1567bd9d6a3eSLorenzo Bianconi 		const struct sockaddr_in6 *s_in, *d_in;
1568bd9d6a3eSLorenzo Bianconi 
1569bd9d6a3eSLorenzo Bianconi 		s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1570bd9d6a3eSLorenzo Bianconi 		d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1571bd9d6a3eSLorenzo Bianconi 		if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1572bd9d6a3eSLorenzo Bianconi 				     &s_in->sin6_addr) ||
1573bd9d6a3eSLorenzo Bianconi 		    nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1574bd9d6a3eSLorenzo Bianconi 				     &d_in->sin6_addr) ||
1575bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1576bd9d6a3eSLorenzo Bianconi 				 s_in->sin6_port) ||
1577bd9d6a3eSLorenzo Bianconi 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1578bd9d6a3eSLorenzo Bianconi 				 d_in->sin6_port))
1579bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1580bd9d6a3eSLorenzo Bianconi 		break;
1581bd9d6a3eSLorenzo Bianconi 	}
1582bd9d6a3eSLorenzo Bianconi 	}
1583bd9d6a3eSLorenzo Bianconi 
1584bd9d6a3eSLorenzo Bianconi 	for (i = 0; i < rqstp->rq_opcnt; i++)
1585bd9d6a3eSLorenzo Bianconi 		if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1586bd9d6a3eSLorenzo Bianconi 				rqstp->rq_opnum[i]))
1587bd9d6a3eSLorenzo Bianconi 			return -ENOBUFS;
1588bd9d6a3eSLorenzo Bianconi 
1589bd9d6a3eSLorenzo Bianconi 	genlmsg_end(skb, hdr);
159013727f85SLorenzo Bianconi 	return 0;
159113727f85SLorenzo Bianconi }
159213727f85SLorenzo Bianconi 
1593bd9d6a3eSLorenzo Bianconi /**
1594bd9d6a3eSLorenzo Bianconi  * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1595bd9d6a3eSLorenzo Bianconi  * @skb: reply buffer
1596bd9d6a3eSLorenzo Bianconi  * @cb: netlink metadata and command arguments
1597bd9d6a3eSLorenzo Bianconi  *
1598bd9d6a3eSLorenzo Bianconi  * Returns the size of the reply or a negative errno.
1599bd9d6a3eSLorenzo Bianconi  */
160013727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
160113727f85SLorenzo Bianconi 				  struct netlink_callback *cb)
160213727f85SLorenzo Bianconi {
1603bd9d6a3eSLorenzo Bianconi 	struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1604bd9d6a3eSLorenzo Bianconi 	int i, ret, rqstp_index = 0;
1605bd9d6a3eSLorenzo Bianconi 
1606bd9d6a3eSLorenzo Bianconi 	rcu_read_lock();
1607bd9d6a3eSLorenzo Bianconi 
1608bd9d6a3eSLorenzo Bianconi 	for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1609bd9d6a3eSLorenzo Bianconi 		struct svc_rqst *rqstp;
1610bd9d6a3eSLorenzo Bianconi 
1611bd9d6a3eSLorenzo Bianconi 		if (i < cb->args[0]) /* already consumed */
1612bd9d6a3eSLorenzo Bianconi 			continue;
1613bd9d6a3eSLorenzo Bianconi 
1614bd9d6a3eSLorenzo Bianconi 		rqstp_index = 0;
1615bd9d6a3eSLorenzo Bianconi 		list_for_each_entry_rcu(rqstp,
1616bd9d6a3eSLorenzo Bianconi 				&nn->nfsd_serv->sv_pools[i].sp_all_threads,
1617bd9d6a3eSLorenzo Bianconi 				rq_all) {
1618bd9d6a3eSLorenzo Bianconi 			struct nfsd_genl_rqstp genl_rqstp;
1619bd9d6a3eSLorenzo Bianconi 			unsigned int status_counter;
1620bd9d6a3eSLorenzo Bianconi 
1621bd9d6a3eSLorenzo Bianconi 			if (rqstp_index++ < cb->args[1]) /* already consumed */
1622bd9d6a3eSLorenzo Bianconi 				continue;
1623bd9d6a3eSLorenzo Bianconi 			/*
1624bd9d6a3eSLorenzo Bianconi 			 * Acquire rq_status_counter before parsing the rqst
1625bd9d6a3eSLorenzo Bianconi 			 * fields. rq_status_counter is set to an odd value in
1626bd9d6a3eSLorenzo Bianconi 			 * order to notify the consumers the rqstp fields are
1627bd9d6a3eSLorenzo Bianconi 			 * meaningful.
1628bd9d6a3eSLorenzo Bianconi 			 */
1629bd9d6a3eSLorenzo Bianconi 			status_counter =
1630bd9d6a3eSLorenzo Bianconi 				smp_load_acquire(&rqstp->rq_status_counter);
1631bd9d6a3eSLorenzo Bianconi 			if (!(status_counter & 1))
1632bd9d6a3eSLorenzo Bianconi 				continue;
1633bd9d6a3eSLorenzo Bianconi 
1634bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_xid = rqstp->rq_xid;
1635bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_flags = rqstp->rq_flags;
1636bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_vers = rqstp->rq_vers;
1637bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_prog = rqstp->rq_prog;
1638bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_proc = rqstp->rq_proc;
1639bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_stime = rqstp->rq_stime;
1640bd9d6a3eSLorenzo Bianconi 			genl_rqstp.rq_opcnt = 0;
1641bd9d6a3eSLorenzo Bianconi 			memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1642bd9d6a3eSLorenzo Bianconi 			       sizeof(struct sockaddr));
1643bd9d6a3eSLorenzo Bianconi 			memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1644bd9d6a3eSLorenzo Bianconi 			       sizeof(struct sockaddr));
1645bd9d6a3eSLorenzo Bianconi 
1646bd9d6a3eSLorenzo Bianconi #ifdef CONFIG_NFSD_V4
1647bd9d6a3eSLorenzo Bianconi 			if (rqstp->rq_vers == NFS4_VERSION &&
1648bd9d6a3eSLorenzo Bianconi 			    rqstp->rq_proc == NFSPROC4_COMPOUND) {
1649bd9d6a3eSLorenzo Bianconi 				/* NFSv4 compound */
1650bd9d6a3eSLorenzo Bianconi 				struct nfsd4_compoundargs *args;
1651bd9d6a3eSLorenzo Bianconi 				int j;
1652bd9d6a3eSLorenzo Bianconi 
1653bd9d6a3eSLorenzo Bianconi 				args = rqstp->rq_argp;
1654bd9d6a3eSLorenzo Bianconi 				genl_rqstp.rq_opcnt = args->opcnt;
1655bd9d6a3eSLorenzo Bianconi 				for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1656bd9d6a3eSLorenzo Bianconi 					genl_rqstp.rq_opnum[j] =
1657bd9d6a3eSLorenzo Bianconi 						args->ops[j].opnum;
1658bd9d6a3eSLorenzo Bianconi 			}
1659bd9d6a3eSLorenzo Bianconi #endif /* CONFIG_NFSD_V4 */
1660bd9d6a3eSLorenzo Bianconi 
1661bd9d6a3eSLorenzo Bianconi 			/*
1662bd9d6a3eSLorenzo Bianconi 			 * Acquire rq_status_counter before reporting the rqst
1663bd9d6a3eSLorenzo Bianconi 			 * fields to the user.
1664bd9d6a3eSLorenzo Bianconi 			 */
1665bd9d6a3eSLorenzo Bianconi 			if (smp_load_acquire(&rqstp->rq_status_counter) !=
1666bd9d6a3eSLorenzo Bianconi 			    status_counter)
1667bd9d6a3eSLorenzo Bianconi 				continue;
1668bd9d6a3eSLorenzo Bianconi 
1669bd9d6a3eSLorenzo Bianconi 			ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1670bd9d6a3eSLorenzo Bianconi 							       &genl_rqstp);
1671bd9d6a3eSLorenzo Bianconi 			if (ret)
1672bd9d6a3eSLorenzo Bianconi 				goto out;
1673bd9d6a3eSLorenzo Bianconi 		}
167413727f85SLorenzo Bianconi 	}
167513727f85SLorenzo Bianconi 
1676bd9d6a3eSLorenzo Bianconi 	cb->args[0] = i;
1677bd9d6a3eSLorenzo Bianconi 	cb->args[1] = rqstp_index;
1678bd9d6a3eSLorenzo Bianconi 	ret = skb->len;
1679bd9d6a3eSLorenzo Bianconi out:
1680bd9d6a3eSLorenzo Bianconi 	rcu_read_unlock();
1681bd9d6a3eSLorenzo Bianconi 
1682bd9d6a3eSLorenzo Bianconi 	return ret;
1683bd9d6a3eSLorenzo Bianconi }
1684bd9d6a3eSLorenzo Bianconi 
1685bd9d6a3eSLorenzo Bianconi /**
1686bd9d6a3eSLorenzo Bianconi  * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing
1687bd9d6a3eSLorenzo Bianconi  * @cb: netlink metadata and command arguments
1688bd9d6a3eSLorenzo Bianconi  *
1689bd9d6a3eSLorenzo Bianconi  * Return values:
1690bd9d6a3eSLorenzo Bianconi  *   %0: Success
1691bd9d6a3eSLorenzo Bianconi  */
169213727f85SLorenzo Bianconi int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb)
169313727f85SLorenzo Bianconi {
1694bd9d6a3eSLorenzo Bianconi 	mutex_unlock(&nfsd_mutex);
1695bd9d6a3eSLorenzo Bianconi 
169613727f85SLorenzo Bianconi 	return 0;
169713727f85SLorenzo Bianconi }
169813727f85SLorenzo Bianconi 
16995e092be7SChuck Lever /**
17005e092be7SChuck Lever  * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
17015e092be7SChuck Lever  * @net: a freshly-created network namespace
17025e092be7SChuck Lever  *
17035e092be7SChuck Lever  * This information stays around as long as the network namespace is
17045e092be7SChuck Lever  * alive whether or not there is an NFSD instance running in the
17055e092be7SChuck Lever  * namespace.
17065e092be7SChuck Lever  *
17075e092be7SChuck Lever  * Returns zero on success, or a negative errno otherwise.
17085e092be7SChuck Lever  */
17095e092be7SChuck Lever static __net_init int nfsd_net_init(struct net *net)
17105717e012SStanislav Kinsbursky {
17115717e012SStanislav Kinsbursky 	int retval;
17123d733711SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
17135717e012SStanislav Kinsbursky 
17145717e012SStanislav Kinsbursky 	retval = nfsd_export_init(net);
17155717e012SStanislav Kinsbursky 	if (retval)
17165717e012SStanislav Kinsbursky 		goto out_export_error;
1717f69adb2fSStanislav Kinsbursky 	retval = nfsd_idmap_init(net);
1718f69adb2fSStanislav Kinsbursky 	if (retval)
1719f69adb2fSStanislav Kinsbursky 		goto out_idmap_error;
1720ed9ab734SJeff Layton 	retval = nfsd_net_reply_cache_init(nn);
1721ed9ab734SJeff Layton 	if (retval)
1722ed9ab734SJeff Layton 		goto out_repcache_error;
1723e333f3bbSTrond Myklebust 	nn->nfsd_versions = NULL;
1724e333f3bbSTrond Myklebust 	nn->nfsd4_minorversions = NULL;
1725f385f7d2SDai Ngo 	nfsd4_init_leases_net(nn);
172691d2e9b5SChuck Lever 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
172791d2e9b5SChuck Lever 	seqlock_init(&nn->writeverf_lock);
17282c830dd7SJ. Bruce Fields 
17295717e012SStanislav Kinsbursky 	return 0;
17305717e012SStanislav Kinsbursky 
1731ed9ab734SJeff Layton out_repcache_error:
1732ed9ab734SJeff Layton 	nfsd_idmap_shutdown(net);
1733f69adb2fSStanislav Kinsbursky out_idmap_error:
1734f69adb2fSStanislav Kinsbursky 	nfsd_export_shutdown(net);
17355717e012SStanislav Kinsbursky out_export_error:
17365717e012SStanislav Kinsbursky 	return retval;
17375717e012SStanislav Kinsbursky }
17385717e012SStanislav Kinsbursky 
17395e092be7SChuck Lever /**
17405e092be7SChuck Lever  * nfsd_net_exit - Release the nfsd_net portion of a net namespace
17415e092be7SChuck Lever  * @net: a network namespace that is about to be destroyed
17425e092be7SChuck Lever  *
17435e092be7SChuck Lever  */
17445e092be7SChuck Lever static __net_exit void nfsd_net_exit(struct net *net)
17455717e012SStanislav Kinsbursky {
1746ed9ab734SJeff Layton 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1747ed9ab734SJeff Layton 
1748ed9ab734SJeff Layton 	nfsd_net_reply_cache_destroy(nn);
1749f69adb2fSStanislav Kinsbursky 	nfsd_idmap_shutdown(net);
17505717e012SStanislav Kinsbursky 	nfsd_export_shutdown(net);
1751ed9ab734SJeff Layton 	nfsd_netns_free_versions(nn);
17525717e012SStanislav Kinsbursky }
17535717e012SStanislav Kinsbursky 
17547ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = {
17555e092be7SChuck Lever 	.init = nfsd_net_init,
17565e092be7SChuck Lever 	.exit = nfsd_net_exit,
17577ea34ac1SJeff Layton 	.id   = &nfsd_net_id,
17587ea34ac1SJeff Layton 	.size = sizeof(struct nfsd_net),
17597ea34ac1SJeff Layton };
17607ea34ac1SJeff Layton 
17611da177e4SLinus Torvalds static int __init init_nfsd(void)
17621da177e4SLinus Torvalds {
17631da177e4SLinus Torvalds 	int retval;
17641da177e4SLinus Torvalds 
1765bb7ffbf2SGiuseppe Cantavenera 	retval = nfsd4_init_slabs();
1766bb7ffbf2SGiuseppe Cantavenera 	if (retval)
1767b10252c7SAlexander Sverdlin 		return retval;
17689cf514ccSChristoph Hellwig 	retval = nfsd4_init_pnfs();
176965178db4SBryan Schumaker 	if (retval)
177065178db4SBryan Schumaker 		goto out_free_slabs;
1771e567b98cSAmir Goldstein 	retval = nfsd_stat_init();	/* Statistics */
1772e567b98cSAmir Goldstein 	if (retval)
1773e567b98cSAmir Goldstein 		goto out_free_pnfs;
1774027690c7SJ. Bruce Fields 	retval = nfsd_drc_slab_create();
1775027690c7SJ. Bruce Fields 	if (retval)
1776027690c7SJ. Bruce Fields 		goto out_free_stat;
17771da177e4SLinus Torvalds 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1778e331f606SJ. Bruce Fields 	retval = create_proc_exports_entry();
1779e331f606SJ. Bruce Fields 	if (retval)
1780f69adb2fSStanislav Kinsbursky 		goto out_free_lockd;
1781bd5ae928SJ. Bruce Fields 	retval = register_pernet_subsys(&nfsd_net_ops);
1782bd5ae928SJ. Bruce Fields 	if (retval < 0)
17836f6f84aaSZhang Xiaoxu 		goto out_free_exports;
1784b10252c7SAlexander Sverdlin 	retval = register_cld_notifier();
1785b10252c7SAlexander Sverdlin 	if (retval)
178662fdb65eSZhang Xiaoxu 		goto out_free_subsys;
1787d76cc46bSDai Ngo 	retval = nfsd4_create_laundry_wq();
1788d76cc46bSDai Ngo 	if (retval)
17896f6f84aaSZhang Xiaoxu 		goto out_free_cld;
17906f6f84aaSZhang Xiaoxu 	retval = register_filesystem(&nfsd_fs_type);
17916f6f84aaSZhang Xiaoxu 	if (retval)
1792d76cc46bSDai Ngo 		goto out_free_all;
1793bd9d6a3eSLorenzo Bianconi 	retval = genl_register_family(&nfsd_nl_family);
1794bd9d6a3eSLorenzo Bianconi 	if (retval)
1795bd9d6a3eSLorenzo Bianconi 		goto out_free_all;
1796bd9d6a3eSLorenzo Bianconi 
179726808d3fSJ. Bruce Fields 	return 0;
179826808d3fSJ. Bruce Fields out_free_all:
17996f6f84aaSZhang Xiaoxu 	nfsd4_destroy_laundry_wq();
18006f6f84aaSZhang Xiaoxu out_free_cld:
180162fdb65eSZhang Xiaoxu 	unregister_cld_notifier();
180262fdb65eSZhang Xiaoxu out_free_subsys:
1803b10252c7SAlexander Sverdlin 	unregister_pernet_subsys(&nfsd_net_ops);
1804bd5ae928SJ. Bruce Fields out_free_exports:
18051da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
18061da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
1807dbf847ecSJ. Bruce Fields out_free_lockd:
18081da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1809027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
1810027690c7SJ. Bruce Fields out_free_stat:
1811d5c3428bSJ. Bruce Fields 	nfsd_stat_shutdown();
1812e567b98cSAmir Goldstein out_free_pnfs:
18139cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
181465178db4SBryan Schumaker out_free_slabs:
181546b25895SJ. Bruce Fields 	nfsd4_free_slabs();
18161da177e4SLinus Torvalds 	return retval;
18171da177e4SLinus Torvalds }
18181da177e4SLinus Torvalds 
18191da177e4SLinus Torvalds static void __exit exit_nfsd(void)
18201da177e4SLinus Torvalds {
1821bd9d6a3eSLorenzo Bianconi 	genl_unregister_family(&nfsd_nl_family);
18226f6f84aaSZhang Xiaoxu 	unregister_filesystem(&nfsd_fs_type);
1823d76cc46bSDai Ngo 	nfsd4_destroy_laundry_wq();
1824b10252c7SAlexander Sverdlin 	unregister_cld_notifier();
1825bd5ae928SJ. Bruce Fields 	unregister_pernet_subsys(&nfsd_net_ops);
1826027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
18271da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
18281da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
18291da177e4SLinus Torvalds 	nfsd_stat_shutdown();
18301da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1831e8ff2a84SJ. Bruce Fields 	nfsd4_free_slabs();
18329cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
18365865bafaSJeff Layton MODULE_DESCRIPTION("In-kernel NFS server");
18371da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18381da177e4SLinus Torvalds module_init(init_nfsd)
18391da177e4SLinus Torvalds module_exit(exit_nfsd)
1840