xref: /linux/fs/nfsd/nfsctl.c (revision 64776611a06322b99386f8dfe3b3ba1aa0347a38)
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>
17b084f598SJ. Bruce Fields #include <linux/sunrpc/gss_krb5_enctypes.h>
18813fd320SJeff Layton #include <linux/sunrpc/rpc_pipe_fs.h>
19143cb494SPaul Gortmaker #include <linux/module.h>
20e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h>
211da177e4SLinus Torvalds 
222ca72e17SJ. Bruce Fields #include "idmap.h"
239a74af21SBoaz Harrosh #include "nfsd.h"
249a74af21SBoaz Harrosh #include "cache.h"
25f3c7521fSBryan Schumaker #include "state.h"
267ea34ac1SJeff Layton #include "netns.h"
279cf514ccSChristoph Hellwig #include "pnfs.h"
282e6c6e4cSChuck Lever #include "filecache.h"
299a74af21SBoaz Harrosh 
301da177e4SLinus Torvalds /*
31b0b0c0a2SKevin Coffman  *	We have a single directory with several nodes in it.
321da177e4SLinus Torvalds  */
331da177e4SLinus Torvalds enum {
341da177e4SLinus Torvalds 	NFSD_Root = 1,
351da177e4SLinus Torvalds 	NFSD_List,
3620ad856eSAmir Goldstein 	NFSD_Export_Stats,
37e8e8753fSJ. Bruce Fields 	NFSD_Export_features,
381da177e4SLinus Torvalds 	NFSD_Fh,
394373ea84SWendy Cheng 	NFSD_FO_UnlockIP,
4017efa372SWendy Cheng 	NFSD_FO_UnlockFS,
411da177e4SLinus Torvalds 	NFSD_Threads,
42eed2965aSGreg Banks 	NFSD_Pool_Threads,
4303cf6c9fSGreg Banks 	NFSD_Pool_Stats,
44a2f999a3SJeff Layton 	NFSD_Reply_Cache_Stats,
4570c3b76cSNeilBrown 	NFSD_Versions,
4680212d59SNeilBrown 	NFSD_Ports,
47596bbe53SNeilBrown 	NFSD_MaxBlkSize,
485b8db00bSJeff Layton 	NFSD_MaxConnections,
492e6c6e4cSChuck Lever 	NFSD_Filecache,
50b0b0c0a2SKevin Coffman 	NFSD_SupportedEnctypes,
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);
790964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
807f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
8170c3b76cSNeilBrown #endif
821da177e4SLinus Torvalds 
83c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
841da177e4SLinus Torvalds 	[NFSD_Fh] = write_filehandle,
85b046ccdcSChuck Lever 	[NFSD_FO_UnlockIP] = write_unlock_ip,
86b046ccdcSChuck Lever 	[NFSD_FO_UnlockFS] = write_unlock_fs,
871da177e4SLinus Torvalds 	[NFSD_Threads] = write_threads,
88eed2965aSGreg Banks 	[NFSD_Pool_Threads] = write_pool_threads,
8970c3b76cSNeilBrown 	[NFSD_Versions] = write_versions,
9080212d59SNeilBrown 	[NFSD_Ports] = write_ports,
91596bbe53SNeilBrown 	[NFSD_MaxBlkSize] = write_maxblksize,
925b8db00bSJeff Layton 	[NFSD_MaxConnections] = write_maxconn,
9370c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
941da177e4SLinus Torvalds 	[NFSD_Leasetime] = write_leasetime,
95efc4bb4fSJ. Bruce Fields 	[NFSD_Gracetime] = write_gracetime,
960964a3d3SNeilBrown 	[NFSD_RecoveryDir] = write_recoverydir,
977f5ef2e9SJeff Layton 	[NFSD_V4EndGrace] = write_v4_end_grace,
9870c3b76cSNeilBrown #endif
991da177e4SLinus Torvalds };
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
1021da177e4SLinus Torvalds {
103496ad9aaSAl Viro 	ino_t ino =  file_inode(file)->i_ino;
1041da177e4SLinus Torvalds 	char *data;
1051da177e4SLinus Torvalds 	ssize_t rv;
1061da177e4SLinus Torvalds 
107e8c96f8cSTobias Klauser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1081da177e4SLinus Torvalds 		return -EINVAL;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
1111da177e4SLinus Torvalds 	if (IS_ERR(data))
1121da177e4SLinus Torvalds 		return PTR_ERR(data);
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds 	rv =  write_op[ino](file, data, size);
1158971a101SNeilBrown 	if (rv >= 0) {
1161da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
1171da177e4SLinus Torvalds 		rv = size;
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 	return rv;
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1227390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1237390022dSNeilBrown {
1247390022dSNeilBrown 	if (! file->private_data) {
1257390022dSNeilBrown 		/* An attempt to read a transaction file without writing
1267390022dSNeilBrown 		 * causes a 0-byte write so that the file can return
1277390022dSNeilBrown 		 * state information
1287390022dSNeilBrown 		 */
1297390022dSNeilBrown 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
1307390022dSNeilBrown 		if (rv < 0)
1317390022dSNeilBrown 			return rv;
1327390022dSNeilBrown 	}
1337390022dSNeilBrown 	return simple_transaction_read(file, buf, size, pos);
1347390022dSNeilBrown }
1357390022dSNeilBrown 
1364b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = {
1371da177e4SLinus Torvalds 	.write		= nfsctl_transaction_write,
1387390022dSNeilBrown 	.read		= nfsctl_transaction_read,
1391da177e4SLinus Torvalds 	.release	= simple_transaction_release,
1406038f373SArnd Bergmann 	.llseek		= default_llseek,
1411da177e4SLinus Torvalds };
1421da177e4SLinus Torvalds 
14396d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file)
1441da177e4SLinus Torvalds {
145f2c7ea10SStanislav Kinsbursky 	int err;
146f2c7ea10SStanislav Kinsbursky 	struct seq_file *seq;
14796d851c4SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
148f2c7ea10SStanislav Kinsbursky 
149f2c7ea10SStanislav Kinsbursky 	err = seq_open(file, &nfs_exports_op);
150f2c7ea10SStanislav Kinsbursky 	if (err)
151f2c7ea10SStanislav Kinsbursky 		return err;
152f2c7ea10SStanislav Kinsbursky 
153f2c7ea10SStanislav Kinsbursky 	seq = file->private_data;
154e5f06f72SStanislav Kinsbursky 	seq->private = nn->svc_export_cache;
155f2c7ea10SStanislav Kinsbursky 	return 0;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
15896d851c4SStanislav Kinsbursky static int exports_proc_open(struct inode *inode, struct file *file)
15996d851c4SStanislav Kinsbursky {
16096d851c4SStanislav Kinsbursky 	return exports_net_open(current->nsproxy->net_ns, file);
16196d851c4SStanislav Kinsbursky }
16296d851c4SStanislav Kinsbursky 
16397a32539SAlexey Dobriyan static const struct proc_ops exports_proc_ops = {
16497a32539SAlexey Dobriyan 	.proc_open	= exports_proc_open,
16597a32539SAlexey Dobriyan 	.proc_read	= seq_read,
16697a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
16797a32539SAlexey Dobriyan 	.proc_release	= seq_release,
16896d851c4SStanislav Kinsbursky };
16996d851c4SStanislav Kinsbursky 
17096d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file)
17196d851c4SStanislav Kinsbursky {
17296d851c4SStanislav Kinsbursky 	return exports_net_open(inode->i_sb->s_fs_info, file);
17396d851c4SStanislav Kinsbursky }
17496d851c4SStanislav Kinsbursky 
17596d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = {
17696d851c4SStanislav Kinsbursky 	.open		= exports_nfsd_open,
1771da177e4SLinus Torvalds 	.read		= seq_read,
1781da177e4SLinus Torvalds 	.llseek		= seq_lseek,
1791da177e4SLinus Torvalds 	.release	= seq_release,
1801da177e4SLinus Torvalds };
1811da177e4SLinus Torvalds 
182e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v)
183e8e8753fSJ. Bruce Fields {
184e8e8753fSJ. Bruce Fields 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
185e8e8753fSJ. Bruce Fields 	return 0;
186e8e8753fSJ. Bruce Fields }
187e8e8753fSJ. Bruce Fields 
1889beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(export_features);
189e8e8753fSJ. Bruce Fields 
190b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
191b0b0c0a2SKevin Coffman static int supported_enctypes_show(struct seq_file *m, void *v)
192b0b0c0a2SKevin Coffman {
193b084f598SJ. Bruce Fields 	seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
194b0b0c0a2SKevin Coffman 	return 0;
195b0b0c0a2SKevin Coffman }
196b0b0c0a2SKevin Coffman 
1979beeaab8SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(supported_enctypes);
198b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
199b0b0c0a2SKevin Coffman 
200828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = {
20103cf6c9fSGreg Banks 	.open		= nfsd_pool_stats_open,
20203cf6c9fSGreg Banks 	.read		= seq_read,
20303cf6c9fSGreg Banks 	.llseek		= seq_lseek,
204ed2d8aedSRyusei Yamaguchi 	.release	= nfsd_pool_stats_release,
20503cf6c9fSGreg Banks };
20603cf6c9fSGreg Banks 
207*64776611SChenXiaoSong DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
208a2f999a3SJeff Layton 
2092e6c6e4cSChuck Lever static const struct file_operations filecache_ops = {
2102e6c6e4cSChuck Lever 	.open		= nfsd_file_cache_stats_open,
2112e6c6e4cSChuck Lever 	.read		= seq_read,
2122e6c6e4cSChuck Lever 	.llseek		= seq_lseek,
2132e6c6e4cSChuck Lever 	.release	= single_release,
2142e6c6e4cSChuck Lever };
2152e6c6e4cSChuck Lever 
2161da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * payload - write methods
2191da177e4SLinus Torvalds  */
2201da177e4SLinus Torvalds 
221244c7d44SAl Viro static inline struct net *netns(struct file *file)
222244c7d44SAl Viro {
223244c7d44SAl Viro 	return file_inode(file)->i_sb->s_fs_info;
224244c7d44SAl Viro }
2251da177e4SLinus Torvalds 
226f2453978SChuck Lever /*
227262a0982SChuck Lever  * write_unlock_ip - Release all locks used by a client
228262a0982SChuck Lever  *
229262a0982SChuck Lever  * Experimental.
230262a0982SChuck Lever  *
231262a0982SChuck Lever  * Input:
232262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing a
2334116092bSChuck Lever  *				presentation format IP address
234262a0982SChuck Lever  *			size:	length of C string in @buf
235262a0982SChuck Lever  * Output:
236262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
237262a0982SChuck Lever  *			returns one if one or more locks were not released
238262a0982SChuck Lever  *	On error:	return code is negative errno value
239262a0982SChuck Lever  */
240b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
2414373ea84SWendy Cheng {
2424116092bSChuck Lever 	struct sockaddr_storage address;
2434116092bSChuck Lever 	struct sockaddr *sap = (struct sockaddr *)&address;
2444116092bSChuck Lever 	size_t salen = sizeof(address);
245367c8c7bSChuck Lever 	char *fo_path;
246244c7d44SAl Viro 	struct net *net = netns(file);
2474373ea84SWendy Cheng 
2484373ea84SWendy Cheng 	/* sanity check */
2494373ea84SWendy Cheng 	if (size == 0)
2504373ea84SWendy Cheng 		return -EINVAL;
2514373ea84SWendy Cheng 
2524373ea84SWendy Cheng 	if (buf[size-1] != '\n')
2534373ea84SWendy Cheng 		return -EINVAL;
2544373ea84SWendy Cheng 
2554373ea84SWendy Cheng 	fo_path = buf;
2564373ea84SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
2574373ea84SWendy Cheng 		return -EINVAL;
2584373ea84SWendy Cheng 
25911f77942SStanislav Kinsbursky 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
2604373ea84SWendy Cheng 		return -EINVAL;
2614373ea84SWendy Cheng 
2624116092bSChuck Lever 	return nlmsvc_unlock_all_by_ip(sap);
2634373ea84SWendy Cheng }
2644373ea84SWendy Cheng 
265f2453978SChuck Lever /*
266262a0982SChuck Lever  * write_unlock_fs - Release all locks on a local file system
267262a0982SChuck Lever  *
268262a0982SChuck Lever  * Experimental.
269262a0982SChuck Lever  *
270262a0982SChuck Lever  * Input:
271262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing the
272262a0982SChuck Lever  *				absolute pathname of a local file system
273262a0982SChuck Lever  *			size:	length of C string in @buf
274262a0982SChuck Lever  * Output:
275262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
276262a0982SChuck Lever  *			returns one if one or more locks were not released
277262a0982SChuck Lever  *	On error:	return code is negative errno value
278262a0982SChuck Lever  */
279b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
28017efa372SWendy Cheng {
281a63bb996SAl Viro 	struct path path;
28217efa372SWendy Cheng 	char *fo_path;
28317efa372SWendy Cheng 	int error;
28417efa372SWendy Cheng 
28517efa372SWendy Cheng 	/* sanity check */
28617efa372SWendy Cheng 	if (size == 0)
28717efa372SWendy Cheng 		return -EINVAL;
28817efa372SWendy Cheng 
28917efa372SWendy Cheng 	if (buf[size-1] != '\n')
29017efa372SWendy Cheng 		return -EINVAL;
29117efa372SWendy Cheng 
29217efa372SWendy Cheng 	fo_path = buf;
29317efa372SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
29417efa372SWendy Cheng 		return -EINVAL;
29517efa372SWendy Cheng 
296a63bb996SAl Viro 	error = kern_path(fo_path, 0, &path);
29717efa372SWendy Cheng 	if (error)
29817efa372SWendy Cheng 		return error;
29917efa372SWendy Cheng 
300262a0982SChuck Lever 	/*
301262a0982SChuck Lever 	 * XXX: Needs better sanity checking.  Otherwise we could end up
302262a0982SChuck Lever 	 * releasing locks on the wrong file system.
303262a0982SChuck Lever 	 *
304262a0982SChuck Lever 	 * For example:
305262a0982SChuck Lever 	 * 1.  Does the path refer to a directory?
306262a0982SChuck Lever 	 * 2.  Is that directory a mount point, or
307262a0982SChuck Lever 	 * 3.  Is that directory the root of an exported file system?
308262a0982SChuck Lever 	 */
309d8c9584eSAl Viro 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
31017efa372SWendy Cheng 
311a63bb996SAl Viro 	path_put(&path);
31217efa372SWendy Cheng 	return error;
31317efa372SWendy Cheng }
31417efa372SWendy Cheng 
315f2453978SChuck Lever /*
316262a0982SChuck Lever  * write_filehandle - Get a variable-length NFS file handle by path
317262a0982SChuck Lever  *
318262a0982SChuck Lever  * On input, the buffer contains a '\n'-terminated C string comprised of
319262a0982SChuck Lever  * three alphanumeric words separated by whitespace.  The string may
320262a0982SChuck Lever  * contain escape sequences.
321262a0982SChuck Lever  *
322262a0982SChuck Lever  * Input:
323262a0982SChuck Lever  *			buf:
324262a0982SChuck Lever  *				domain:		client domain name
325262a0982SChuck Lever  *				path:		export pathname
326262a0982SChuck Lever  *				maxsize:	numeric maximum size of
327262a0982SChuck Lever  *						@buf
328262a0982SChuck Lever  *			size:	length of C string in @buf
329262a0982SChuck Lever  * Output:
330262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
331262a0982SChuck Lever  *			string containing a ASCII hex text version
332262a0982SChuck Lever  *			of the NFS file handle;
333262a0982SChuck Lever  *			return code is the size in bytes of the string
334262a0982SChuck Lever  *	On error:	return code is negative errno value
335262a0982SChuck Lever  */
3361da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	char *dname, *path;
3393f649ab7SKees Cook 	int maxsize;
3401da177e4SLinus Torvalds 	char *mesg = buf;
3411da177e4SLinus Torvalds 	int len;
3421da177e4SLinus Torvalds 	struct auth_domain *dom;
3431da177e4SLinus Torvalds 	struct knfsd_fh fh;
3441da177e4SLinus Torvalds 
34587d26ea7SJ. Bruce Fields 	if (size == 0)
34687d26ea7SJ. Bruce Fields 		return -EINVAL;
34787d26ea7SJ. Bruce Fields 
3481da177e4SLinus Torvalds 	if (buf[size-1] != '\n')
3491da177e4SLinus Torvalds 		return -EINVAL;
3501da177e4SLinus Torvalds 	buf[size-1] = 0;
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	dname = mesg;
3531da177e4SLinus Torvalds 	len = qword_get(&mesg, dname, size);
35454224f04SChuck Lever 	if (len <= 0)
35554224f04SChuck Lever 		return -EINVAL;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	path = dname+len+1;
3581da177e4SLinus Torvalds 	len = qword_get(&mesg, path, size);
35954224f04SChuck Lever 	if (len <= 0)
36054224f04SChuck Lever 		return -EINVAL;
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	len = get_int(&mesg, &maxsize);
3631da177e4SLinus Torvalds 	if (len)
3641da177e4SLinus Torvalds 		return len;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	if (maxsize < NFS_FHSIZE)
3671da177e4SLinus Torvalds 		return -EINVAL;
3683c7aa15dSKinglong Mee 	maxsize = min(maxsize, NFS3_FHSIZE);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	if (qword_get(&mesg, mesg, size)>0)
3711da177e4SLinus Torvalds 		return -EINVAL;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	/* we have all the words, they are in buf.. */
3741da177e4SLinus Torvalds 	dom = unix_domain_find(dname);
3751da177e4SLinus Torvalds 	if (!dom)
3761da177e4SLinus Torvalds 		return -ENOMEM;
3771da177e4SLinus Torvalds 
378244c7d44SAl Viro 	len = exp_rootfh(netns(file), dom, path, &fh,  maxsize);
3791da177e4SLinus Torvalds 	auth_domain_put(dom);
3801da177e4SLinus Torvalds 	if (len)
3811da177e4SLinus Torvalds 		return len;
3821da177e4SLinus Torvalds 
38354224f04SChuck Lever 	mesg = buf;
38454224f04SChuck Lever 	len = SIMPLE_TRANSACTION_LIMIT;
385d8b26071SNeilBrown 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
3861da177e4SLinus Torvalds 	mesg[-1] = '\n';
3871da177e4SLinus Torvalds 	return mesg - buf;
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
390f2453978SChuck Lever /*
391262a0982SChuck Lever  * write_threads - Start NFSD, or report the current number of running threads
392262a0982SChuck Lever  *
393262a0982SChuck Lever  * Input:
394262a0982SChuck Lever  *			buf:		ignored
395262a0982SChuck Lever  *			size:		zero
396262a0982SChuck Lever  * Output:
397262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
398262a0982SChuck Lever  *			string numeric value representing the number of
399262a0982SChuck Lever  *			running NFSD threads;
400262a0982SChuck Lever  *			return code is the size in bytes of the string
401262a0982SChuck Lever  *	On error:	return code is zero
402262a0982SChuck Lever  *
403262a0982SChuck Lever  * OR
404262a0982SChuck Lever  *
405262a0982SChuck Lever  * Input:
406262a0982SChuck Lever  *			buf:		C string containing an unsigned
407262a0982SChuck Lever  *					integer value representing the
408262a0982SChuck Lever  *					number of NFSD threads to start
409262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
410262a0982SChuck Lever  * Output:
411262a0982SChuck Lever  *	On success:	NFS service is started;
412262a0982SChuck Lever  *			passed-in buffer filled with '\n'-terminated C
413262a0982SChuck Lever  *			string numeric value representing the number of
414262a0982SChuck Lever  *			running NFSD threads;
415262a0982SChuck Lever  *			return code is the size in bytes of the string
416262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
417262a0982SChuck Lever  */
4181da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size)
4191da177e4SLinus Torvalds {
4201da177e4SLinus Torvalds 	char *mesg = buf;
4211da177e4SLinus Torvalds 	int rv;
422244c7d44SAl Viro 	struct net *net = netns(file);
423d41a9417SStanislav Kinsbursky 
4241da177e4SLinus Torvalds 	if (size > 0) {
4251da177e4SLinus Torvalds 		int newthreads;
4261da177e4SLinus Torvalds 		rv = get_int(&mesg, &newthreads);
4271da177e4SLinus Torvalds 		if (rv)
4281da177e4SLinus Torvalds 			return rv;
4291da177e4SLinus Torvalds 		if (newthreads < 0)
4301da177e4SLinus Torvalds 			return -EINVAL;
4314df493a2STrond Myklebust 		rv = nfsd_svc(newthreads, net, file->f_cred);
43282e12fe9SNeilBrown 		if (rv < 0)
4331da177e4SLinus Torvalds 			return rv;
43482e12fe9SNeilBrown 	} else
4359dd9845fSStanislav Kinsbursky 		rv = nfsd_nrthreads(net);
436e06b6405SChuck Lever 
43782e12fe9SNeilBrown 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
440f2453978SChuck Lever /*
441262a0982SChuck Lever  * write_pool_threads - Set or report the current number of threads per pool
442262a0982SChuck Lever  *
443262a0982SChuck Lever  * Input:
444262a0982SChuck Lever  *			buf:		ignored
445262a0982SChuck Lever  *			size:		zero
446262a0982SChuck Lever  *
447262a0982SChuck Lever  * OR
448262a0982SChuck Lever  *
449262a0982SChuck Lever  * Input:
450262a0982SChuck Lever  * 			buf:		C string containing whitespace-
451262a0982SChuck Lever  * 					separated unsigned integer values
452262a0982SChuck Lever  *					representing the number of NFSD
453262a0982SChuck Lever  *					threads to start in each pool
454262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
455262a0982SChuck Lever  * Output:
456262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
457262a0982SChuck Lever  *			string containing integer values representing the
458262a0982SChuck Lever  *			number of NFSD threads in each pool;
459262a0982SChuck Lever  *			return code is the size in bytes of the string
460262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
461262a0982SChuck Lever  */
462eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
463eed2965aSGreg Banks {
464eed2965aSGreg Banks 	/* if size > 0, look for an array of number of threads per node
465eed2965aSGreg Banks 	 * and apply them  then write out number of threads per node as reply
466eed2965aSGreg Banks 	 */
467eed2965aSGreg Banks 	char *mesg = buf;
468eed2965aSGreg Banks 	int i;
469eed2965aSGreg Banks 	int rv;
470eed2965aSGreg Banks 	int len;
471bedbdd8bSNeil Brown 	int npools;
472eed2965aSGreg Banks 	int *nthreads;
473244c7d44SAl Viro 	struct net *net = netns(file);
474eed2965aSGreg Banks 
475bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
4769dd9845fSStanislav Kinsbursky 	npools = nfsd_nrpools(net);
477eed2965aSGreg Banks 	if (npools == 0) {
478eed2965aSGreg Banks 		/*
479eed2965aSGreg Banks 		 * NFS is shut down.  The admin can start it by
480eed2965aSGreg Banks 		 * writing to the threads file but NOT the pool_threads
481eed2965aSGreg Banks 		 * file, sorry.  Report zero threads.
482eed2965aSGreg Banks 		 */
483bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
484eed2965aSGreg Banks 		strcpy(buf, "0\n");
485eed2965aSGreg Banks 		return strlen(buf);
486eed2965aSGreg Banks 	}
487eed2965aSGreg Banks 
488eed2965aSGreg Banks 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
489bedbdd8bSNeil Brown 	rv = -ENOMEM;
490eed2965aSGreg Banks 	if (nthreads == NULL)
491bedbdd8bSNeil Brown 		goto out_free;
492eed2965aSGreg Banks 
493eed2965aSGreg Banks 	if (size > 0) {
494eed2965aSGreg Banks 		for (i = 0; i < npools; i++) {
495eed2965aSGreg Banks 			rv = get_int(&mesg, &nthreads[i]);
496eed2965aSGreg Banks 			if (rv == -ENOENT)
497eed2965aSGreg Banks 				break;		/* fewer numbers than pools */
498eed2965aSGreg Banks 			if (rv)
499eed2965aSGreg Banks 				goto out_free;	/* syntax error */
500eed2965aSGreg Banks 			rv = -EINVAL;
501eed2965aSGreg Banks 			if (nthreads[i] < 0)
502eed2965aSGreg Banks 				goto out_free;
503eed2965aSGreg Banks 		}
5043938a0d5SStanislav Kinsbursky 		rv = nfsd_set_nrthreads(i, nthreads, net);
505eed2965aSGreg Banks 		if (rv)
506eed2965aSGreg Banks 			goto out_free;
507eed2965aSGreg Banks 	}
508eed2965aSGreg Banks 
5099dd9845fSStanislav Kinsbursky 	rv = nfsd_get_nrthreads(npools, nthreads, net);
510eed2965aSGreg Banks 	if (rv)
511eed2965aSGreg Banks 		goto out_free;
512eed2965aSGreg Banks 
513eed2965aSGreg Banks 	mesg = buf;
514eed2965aSGreg Banks 	size = SIMPLE_TRANSACTION_LIMIT;
515eed2965aSGreg Banks 	for (i = 0; i < npools && size > 0; i++) {
516eed2965aSGreg Banks 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
517eed2965aSGreg Banks 		len = strlen(mesg);
518eed2965aSGreg Banks 		size -= len;
519eed2965aSGreg Banks 		mesg += len;
520eed2965aSGreg Banks 	}
521413d63d7SJ. Bruce Fields 	rv = mesg - buf;
522eed2965aSGreg Banks out_free:
523eed2965aSGreg Banks 	kfree(nthreads);
524bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
525eed2965aSGreg Banks 	return rv;
526eed2965aSGreg Banks }
527eed2965aSGreg Banks 
528ff7d1179STrond Myklebust static ssize_t
529e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
530e333f3bbSTrond Myklebust 		const char *sep, unsigned vers, int minor)
531ff7d1179STrond Myklebust {
532abcb4dacSNeilBrown 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
533e333f3bbSTrond Myklebust 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
534ff7d1179STrond Myklebust 
535abcb4dacSNeilBrown 	if (vers == 4 && minor >= 0 &&
536e333f3bbSTrond Myklebust 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
537ff7d1179STrond Myklebust 		supported = false;
538abcb4dacSNeilBrown 	if (minor == 0 && supported)
539abcb4dacSNeilBrown 		/*
540abcb4dacSNeilBrown 		 * special case for backward compatability.
541abcb4dacSNeilBrown 		 * +4.0 is never reported, it is implied by
542abcb4dacSNeilBrown 		 * +4, unless -4.0 is present.
543abcb4dacSNeilBrown 		 */
544abcb4dacSNeilBrown 		return 0;
545ff7d1179STrond Myklebust 	return snprintf(buf, remaining, format, sep,
546ff7d1179STrond Myklebust 			supported ? '+' : '-', vers, minor);
547ff7d1179STrond Myklebust }
548ff7d1179STrond Myklebust 
5493dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size)
55070c3b76cSNeilBrown {
55170c3b76cSNeilBrown 	char *mesg = buf;
5528daf220aSBenny Halevy 	char *vers, *minorp, sign;
553261758b5SChuck Lever 	int len, num, remaining;
55470c3b76cSNeilBrown 	ssize_t tlen = 0;
55570c3b76cSNeilBrown 	char *sep;
556244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
55770c3b76cSNeilBrown 
55870c3b76cSNeilBrown 	if (size>0) {
5599dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
5606658d3a7SNeilBrown 			/* Cannot change versions without updating
5619dd9845fSStanislav Kinsbursky 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
5626658d3a7SNeilBrown 			 * rq_argp and rq_resp
5636658d3a7SNeilBrown 			 */
56470c3b76cSNeilBrown 			return -EBUSY;
56570c3b76cSNeilBrown 		if (buf[size-1] != '\n')
56670c3b76cSNeilBrown 			return -EINVAL;
56770c3b76cSNeilBrown 		buf[size-1] = 0;
56870c3b76cSNeilBrown 
56970c3b76cSNeilBrown 		vers = mesg;
57070c3b76cSNeilBrown 		len = qword_get(&mesg, vers, size);
57170c3b76cSNeilBrown 		if (len <= 0) return -EINVAL;
57270c3b76cSNeilBrown 		do {
573d3635ff0STrond Myklebust 			enum vers_op cmd;
574abcb4dacSNeilBrown 			unsigned minor;
57570c3b76cSNeilBrown 			sign = *vers;
57670c3b76cSNeilBrown 			if (sign == '+' || sign == '-')
5778daf220aSBenny Halevy 				num = simple_strtol((vers+1), &minorp, 0);
57870c3b76cSNeilBrown 			else
5798daf220aSBenny Halevy 				num = simple_strtol(vers, &minorp, 0);
5808daf220aSBenny Halevy 			if (*minorp == '.') {
581ff89be87SJ. Bruce Fields 				if (num != 4)
5828daf220aSBenny Halevy 					return -EINVAL;
583e35659f1SNeilBrown 				if (kstrtouint(minorp+1, 0, &minor) < 0)
5848daf220aSBenny Halevy 					return -EINVAL;
585abcb4dacSNeilBrown 			}
586abcb4dacSNeilBrown 
587d3635ff0STrond Myklebust 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
58870c3b76cSNeilBrown 			switch(num) {
58970c3b76cSNeilBrown 			case 2:
59070c3b76cSNeilBrown 			case 3:
591e333f3bbSTrond Myklebust 				nfsd_vers(nn, num, cmd);
592d3635ff0STrond Myklebust 				break;
59370c3b76cSNeilBrown 			case 4:
594abcb4dacSNeilBrown 				if (*minorp == '.') {
595e333f3bbSTrond Myklebust 					if (nfsd_minorversion(nn, minor, cmd) < 0)
596abcb4dacSNeilBrown 						return -EINVAL;
597e333f3bbSTrond Myklebust 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
598abcb4dacSNeilBrown 					/*
599abcb4dacSNeilBrown 					 * Either we have +4 and no minors are enabled,
600abcb4dacSNeilBrown 					 * or we have -4 and at least one minor is enabled.
601abcb4dacSNeilBrown 					 * In either case, propagate 'cmd' to all minors.
602abcb4dacSNeilBrown 					 */
603abcb4dacSNeilBrown 					minor = 0;
604e333f3bbSTrond Myklebust 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
605abcb4dacSNeilBrown 						minor++;
606abcb4dacSNeilBrown 				}
60770c3b76cSNeilBrown 				break;
60870c3b76cSNeilBrown 			default:
60970c3b76cSNeilBrown 				return -EINVAL;
61070c3b76cSNeilBrown 			}
61170c3b76cSNeilBrown 			vers += len + 1;
61270c3b76cSNeilBrown 		} while ((len = qword_get(&mesg, vers, size)) > 0);
61370c3b76cSNeilBrown 		/* If all get turned off, turn them back on, as
61470c3b76cSNeilBrown 		 * having no versions is BAD
61570c3b76cSNeilBrown 		 */
616e333f3bbSTrond Myklebust 		nfsd_reset_versions(nn);
61770c3b76cSNeilBrown 	}
618261758b5SChuck Lever 
61970c3b76cSNeilBrown 	/* Now write current state into reply buffer */
62070c3b76cSNeilBrown 	sep = "";
621261758b5SChuck Lever 	remaining = SIMPLE_TRANSACTION_LIMIT;
622ff7d1179STrond Myklebust 	for (num=2 ; num <= 4 ; num++) {
623abcb4dacSNeilBrown 		int minor;
624e333f3bbSTrond Myklebust 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
625e35659f1SNeilBrown 			continue;
626abcb4dacSNeilBrown 
627abcb4dacSNeilBrown 		minor = -1;
628ff7d1179STrond Myklebust 		do {
629e333f3bbSTrond Myklebust 			len = nfsd_print_version_support(nn, buf, remaining,
630ff7d1179STrond Myklebust 					sep, num, minor);
631818f2f57SDan Carpenter 			if (len >= remaining)
632ff7d1179STrond Myklebust 				goto out;
633261758b5SChuck Lever 			remaining -= len;
634261758b5SChuck Lever 			buf += len;
635261758b5SChuck Lever 			tlen += len;
636ff7d1179STrond Myklebust 			minor++;
637abcb4dacSNeilBrown 			if (len)
638ff7d1179STrond Myklebust 				sep = " ";
639ff7d1179STrond Myklebust 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
640261758b5SChuck Lever 	}
641ff7d1179STrond Myklebust out:
642261758b5SChuck Lever 	len = snprintf(buf, remaining, "\n");
643818f2f57SDan Carpenter 	if (len >= remaining)
644261758b5SChuck Lever 		return -EINVAL;
645261758b5SChuck Lever 	return tlen + len;
64670c3b76cSNeilBrown }
64770c3b76cSNeilBrown 
648f2453978SChuck Lever /*
649262a0982SChuck Lever  * write_versions - Set or report the available NFS protocol versions
650262a0982SChuck Lever  *
651262a0982SChuck Lever  * Input:
652262a0982SChuck Lever  *			buf:		ignored
653262a0982SChuck Lever  *			size:		zero
654262a0982SChuck Lever  * Output:
655262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
656262a0982SChuck Lever  *			string containing positive or negative integer
657262a0982SChuck Lever  *			values representing the current status of each
658262a0982SChuck Lever  *			protocol version;
659262a0982SChuck Lever  *			return code is the size in bytes of the string
660262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
661262a0982SChuck Lever  *
662262a0982SChuck Lever  * OR
663262a0982SChuck Lever  *
664262a0982SChuck Lever  * Input:
665262a0982SChuck Lever  * 			buf:		C string containing whitespace-
666262a0982SChuck Lever  * 					separated positive or negative
667262a0982SChuck Lever  * 					integer values representing NFS
668262a0982SChuck Lever  * 					protocol versions to enable ("+n")
669262a0982SChuck Lever  * 					or disable ("-n")
670262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
671262a0982SChuck Lever  * Output:
672262a0982SChuck Lever  *	On success:	status of zero or more protocol versions has
673262a0982SChuck Lever  *			been updated; passed-in buffer filled with
674262a0982SChuck Lever  *			'\n'-terminated C string containing positive
675262a0982SChuck Lever  *			or negative integer values representing the
676262a0982SChuck Lever  *			current status of each protocol version;
677262a0982SChuck Lever  *			return code is the size in bytes of the string
678262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
679262a0982SChuck Lever  */
6803dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size)
6813dd98a3bSJeff Layton {
6823dd98a3bSJeff Layton 	ssize_t rv;
6833dd98a3bSJeff Layton 
6843dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
6853dd98a3bSJeff Layton 	rv = __write_versions(file, buf, size);
6863dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
6873dd98a3bSJeff Layton 	return rv;
6883dd98a3bSJeff Layton }
6893dd98a3bSJeff Layton 
6904cd5dc75SChuck Lever /*
6910a5372d8SChuck Lever  * Zero-length write.  Return a list of NFSD's current listener
6920a5372d8SChuck Lever  * transports.
6930a5372d8SChuck Lever  */
6949dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net)
6950a5372d8SChuck Lever {
6969dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6979dd9845fSStanislav Kinsbursky 
6989dd9845fSStanislav Kinsbursky 	if (nn->nfsd_serv == NULL)
6990a5372d8SChuck Lever 		return 0;
7009dd9845fSStanislav Kinsbursky 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
7010a5372d8SChuck Lever }
7020a5372d8SChuck Lever 
7030a5372d8SChuck Lever /*
7040b7c2f6fSChuck Lever  * A single 'fd' number was written, in which case it must be for
7050b7c2f6fSChuck Lever  * a socket of a supported family/protocol, and we use it as an
7060b7c2f6fSChuck Lever  * nfsd listener.
7070b7c2f6fSChuck Lever  */
7084df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
7090b7c2f6fSChuck Lever {
7100b7c2f6fSChuck Lever 	char *mesg = buf;
7110b7c2f6fSChuck Lever 	int fd, err;
7129dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7130b7c2f6fSChuck Lever 
7140b7c2f6fSChuck Lever 	err = get_int(&mesg, &fd);
7150b7c2f6fSChuck Lever 	if (err != 0 || fd < 0)
7160b7c2f6fSChuck Lever 		return -EINVAL;
7170b7c2f6fSChuck Lever 
71830646394SStanislav Kinsbursky 	if (svc_alien_sock(net, fd)) {
71930646394SStanislav Kinsbursky 		printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
72030646394SStanislav Kinsbursky 		return -EINVAL;
72130646394SStanislav Kinsbursky 	}
72230646394SStanislav Kinsbursky 
7236777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7240b7c2f6fSChuck Lever 	if (err != 0)
7250b7c2f6fSChuck Lever 		return err;
7260b7c2f6fSChuck Lever 
7274df493a2STrond Myklebust 	err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
728ea068badSChuck Lever 
729ec52361dSNeilBrown 	if (err >= 0 &&
730ec52361dSNeilBrown 	    !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
731ec52361dSNeilBrown 		svc_get(nn->nfsd_serv);
732ec52361dSNeilBrown 
733ec52361dSNeilBrown 	nfsd_put(net);
734ea068badSChuck Lever 	return err;
7350b7c2f6fSChuck Lever }
7360b7c2f6fSChuck Lever 
7370b7c2f6fSChuck Lever /*
7384eb68c26SChuck Lever  * A transport listener is added by writing it's transport name and
7394eb68c26SChuck Lever  * a port number.
7404eb68c26SChuck Lever  */
7414df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
7424eb68c26SChuck Lever {
7434eb68c26SChuck Lever 	char transport[16];
74437498292SChuck Lever 	struct svc_xprt *xprt;
7454eb68c26SChuck Lever 	int port, err;
7469dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7474eb68c26SChuck Lever 
748a10fded1SJ. Bruce Fields 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
7494eb68c26SChuck Lever 		return -EINVAL;
7504eb68c26SChuck Lever 
7514be929beSAlexey Dobriyan 	if (port < 1 || port > USHRT_MAX)
7524eb68c26SChuck Lever 		return -EINVAL;
7534eb68c26SChuck Lever 
7546777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7554eb68c26SChuck Lever 	if (err != 0)
7564eb68c26SChuck Lever 		return err;
7574eb68c26SChuck Lever 
758352ad314SChuck Lever 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
7594df493a2STrond Myklebust 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
76068717908SChuck Lever 	if (err < 0)
76137498292SChuck Lever 		goto out_err;
76237498292SChuck Lever 
763352ad314SChuck Lever 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
7644df493a2STrond Myklebust 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
76537498292SChuck Lever 	if (err < 0 && err != -EAFNOSUPPORT)
76637498292SChuck Lever 		goto out_close;
7670cd14a06SJeff Layton 
768ec52361dSNeilBrown 	if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
769ec52361dSNeilBrown 		svc_get(nn->nfsd_serv);
770ec52361dSNeilBrown 
771ec52361dSNeilBrown 	nfsd_put(net);
7724eb68c26SChuck Lever 	return 0;
77337498292SChuck Lever out_close:
7749dd9845fSStanislav Kinsbursky 	xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
77537498292SChuck Lever 	if (xprt != NULL) {
7764355d767SChuck Lever 		svc_xprt_close(xprt);
77737498292SChuck Lever 		svc_xprt_put(xprt);
77837498292SChuck Lever 	}
77937498292SChuck Lever out_err:
7808c62d127SNeilBrown 	nfsd_put(net);
78137498292SChuck Lever 	return err;
7824eb68c26SChuck Lever }
7834eb68c26SChuck Lever 
78408160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size,
78508160352SStanislav Kinsbursky 			     struct net *net)
78680212d59SNeilBrown {
7870a5372d8SChuck Lever 	if (size == 0)
7889dd9845fSStanislav Kinsbursky 		return __write_ports_names(buf, net);
7890b7c2f6fSChuck Lever 
7900b7c2f6fSChuck Lever 	if (isdigit(buf[0]))
7914df493a2STrond Myklebust 		return __write_ports_addfd(buf, net, file->f_cred);
79282d56591SChuck Lever 
7934eb68c26SChuck Lever 	if (isalpha(buf[0]))
7944df493a2STrond Myklebust 		return __write_ports_addxprt(buf, net, file->f_cred);
7954cd5dc75SChuck Lever 
796b41b66d6SNeilBrown 	return -EINVAL;
797b41b66d6SNeilBrown }
79880212d59SNeilBrown 
799f2453978SChuck Lever /*
800262a0982SChuck Lever  * write_ports - Pass a socket file descriptor or transport name to listen on
801262a0982SChuck Lever  *
802262a0982SChuck Lever  * Input:
803262a0982SChuck Lever  *			buf:		ignored
804262a0982SChuck Lever  *			size:		zero
805262a0982SChuck Lever  * Output:
806262a0982SChuck Lever  *	On success:	passed-in buffer filled with a '\n'-terminated C
807262a0982SChuck Lever  *			string containing a whitespace-separated list of
808262a0982SChuck Lever  *			named NFSD listeners;
809262a0982SChuck Lever  *			return code is the size in bytes of the string
810262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
811262a0982SChuck Lever  *
812262a0982SChuck Lever  * OR
813262a0982SChuck Lever  *
814262a0982SChuck Lever  * Input:
815262a0982SChuck Lever  *			buf:		C string containing an unsigned
816262a0982SChuck Lever  *					integer value representing a bound
817262a0982SChuck Lever  *					but unconnected socket that is to be
818c71206a7SChuck Lever  *					used as an NFSD listener; listen(3)
819c71206a7SChuck Lever  *					must be called for a SOCK_STREAM
820c71206a7SChuck Lever  *					socket, otherwise it is ignored
821262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
822262a0982SChuck Lever  * Output:
823262a0982SChuck Lever  *	On success:	NFS service is started;
824262a0982SChuck Lever  *			passed-in buffer filled with a '\n'-terminated C
825262a0982SChuck Lever  *			string containing a unique alphanumeric name of
826262a0982SChuck Lever  *			the listener;
827262a0982SChuck Lever  *			return code is the size in bytes of the string
828262a0982SChuck Lever  *	On error:	return code is a negative errno value
829262a0982SChuck Lever  *
830262a0982SChuck Lever  * OR
831262a0982SChuck Lever  *
832262a0982SChuck Lever  * Input:
833262a0982SChuck Lever  *			buf:		C string containing a transport
834262a0982SChuck Lever  *					name and an unsigned integer value
835262a0982SChuck Lever  *					representing the port to listen on,
836262a0982SChuck Lever  *					separated by whitespace
837262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
838262a0982SChuck Lever  * Output:
839262a0982SChuck Lever  *	On success:	returns zero; NFS service is started
840262a0982SChuck Lever  *	On error:	return code is a negative errno value
841262a0982SChuck Lever  */
842bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size)
843bedbdd8bSNeil Brown {
844bedbdd8bSNeil Brown 	ssize_t rv;
8453dd98a3bSJeff Layton 
846bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
847244c7d44SAl Viro 	rv = __write_ports(file, buf, size, netns(file));
848bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
849bedbdd8bSNeil Brown 	return rv;
850bedbdd8bSNeil Brown }
851bedbdd8bSNeil Brown 
852bedbdd8bSNeil Brown 
853596bbe53SNeilBrown int nfsd_max_blksize;
854596bbe53SNeilBrown 
855f2453978SChuck Lever /*
856262a0982SChuck Lever  * write_maxblksize - Set or report the current NFS blksize
857262a0982SChuck Lever  *
858262a0982SChuck Lever  * Input:
859262a0982SChuck Lever  *			buf:		ignored
860262a0982SChuck Lever  *			size:		zero
861262a0982SChuck Lever  *
862262a0982SChuck Lever  * OR
863262a0982SChuck Lever  *
864262a0982SChuck Lever  * Input:
865262a0982SChuck Lever  * 			buf:		C string containing an unsigned
866262a0982SChuck Lever  * 					integer value representing the new
867262a0982SChuck Lever  * 					NFS blksize
868262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
869262a0982SChuck Lever  * Output:
870262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
871262a0982SChuck Lever  *			containing numeric value of the current NFS blksize
872262a0982SChuck Lever  *			setting;
873262a0982SChuck Lever  *			return code is the size in bytes of the string
874262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
875262a0982SChuck Lever  */
876596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
877596bbe53SNeilBrown {
878596bbe53SNeilBrown 	char *mesg = buf;
879244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
8809dd9845fSStanislav Kinsbursky 
881596bbe53SNeilBrown 	if (size > 0) {
882596bbe53SNeilBrown 		int bsize;
883596bbe53SNeilBrown 		int rv = get_int(&mesg, &bsize);
884596bbe53SNeilBrown 		if (rv)
885596bbe53SNeilBrown 			return rv;
886596bbe53SNeilBrown 		/* force bsize into allowed range and
887596bbe53SNeilBrown 		 * required alignment.
888596bbe53SNeilBrown 		 */
8893c7aa15dSKinglong Mee 		bsize = max_t(int, bsize, 1024);
8903c7aa15dSKinglong Mee 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
891596bbe53SNeilBrown 		bsize &= ~(1024-1);
892bedbdd8bSNeil Brown 		mutex_lock(&nfsd_mutex);
8939dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv) {
894bedbdd8bSNeil Brown 			mutex_unlock(&nfsd_mutex);
895596bbe53SNeilBrown 			return -EBUSY;
896596bbe53SNeilBrown 		}
897596bbe53SNeilBrown 		nfsd_max_blksize = bsize;
898bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
899596bbe53SNeilBrown 	}
900e06b6405SChuck Lever 
901e06b6405SChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
902e06b6405SChuck Lever 							nfsd_max_blksize);
903596bbe53SNeilBrown }
904596bbe53SNeilBrown 
905f2453978SChuck Lever /*
9065b8db00bSJeff Layton  * write_maxconn - Set or report the current max number of connections
9075b8db00bSJeff Layton  *
9085b8db00bSJeff Layton  * Input:
9095b8db00bSJeff Layton  *			buf:		ignored
9105b8db00bSJeff Layton  *			size:		zero
9115b8db00bSJeff Layton  * OR
9125b8db00bSJeff Layton  *
9135b8db00bSJeff Layton  * Input:
9145b8db00bSJeff Layton  * 			buf:		C string containing an unsigned
9155b8db00bSJeff Layton  * 					integer value representing the new
9165b8db00bSJeff Layton  * 					number of max connections
9175b8db00bSJeff Layton  *			size:		non-zero length of C string in @buf
9185b8db00bSJeff Layton  * Output:
9195b8db00bSJeff Layton  *	On success:	passed-in buffer filled with '\n'-terminated C string
9205b8db00bSJeff Layton  *			containing numeric value of max_connections setting
9215b8db00bSJeff Layton  *			for this net namespace;
9225b8db00bSJeff Layton  *			return code is the size in bytes of the string
9235b8db00bSJeff Layton  *	On error:	return code is zero or a negative errno value
9245b8db00bSJeff Layton  */
9255b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
9265b8db00bSJeff Layton {
9275b8db00bSJeff Layton 	char *mesg = buf;
928244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9295b8db00bSJeff Layton 	unsigned int maxconn = nn->max_connections;
9305b8db00bSJeff Layton 
9315b8db00bSJeff Layton 	if (size > 0) {
9325b8db00bSJeff Layton 		int rv = get_uint(&mesg, &maxconn);
9335b8db00bSJeff Layton 
9345b8db00bSJeff Layton 		if (rv)
9355b8db00bSJeff Layton 			return rv;
9365b8db00bSJeff Layton 		nn->max_connections = maxconn;
9375b8db00bSJeff Layton 	}
9385b8db00bSJeff Layton 
9395b8db00bSJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
9405b8db00bSJeff Layton }
9415b8db00bSJeff Layton 
94270c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
9439dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
94420b7d86fSArnd Bergmann 				  time64_t *time, struct nfsd_net *nn)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	char *mesg = buf;
947f0135740SJ. Bruce Fields 	int rv, i;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	if (size > 0) {
9509dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
9513dd98a3bSJeff Layton 			return -EBUSY;
952f0135740SJ. Bruce Fields 		rv = get_int(&mesg, &i);
9531da177e4SLinus Torvalds 		if (rv)
9541da177e4SLinus Torvalds 			return rv;
955e7b184f1SJ. Bruce Fields 		/*
956e7b184f1SJ. Bruce Fields 		 * Some sanity checking.  We don't have a reason for
957e7b184f1SJ. Bruce Fields 		 * these particular numbers, but problems with the
958e7b184f1SJ. Bruce Fields 		 * extremes are:
959e7b184f1SJ. Bruce Fields 		 *	- Too short: the briefest network outage may
960e7b184f1SJ. Bruce Fields 		 *	  cause clients to lose all their locks.  Also,
961e7b184f1SJ. Bruce Fields 		 *	  the frequent polling may be wasteful.
962e7b184f1SJ. Bruce Fields 		 *	- Too long: do you really want reboot recovery
963e7b184f1SJ. Bruce Fields 		 *	  to take more than an hour?  Or to make other
964e7b184f1SJ. Bruce Fields 		 *	  clients wait an hour before being able to
965e7b184f1SJ. Bruce Fields 		 *	  revoke a dead client's locks?
966e7b184f1SJ. Bruce Fields 		 */
967f0135740SJ. Bruce Fields 		if (i < 10 || i > 3600)
9681da177e4SLinus Torvalds 			return -EINVAL;
969f0135740SJ. Bruce Fields 		*time = i;
9701da177e4SLinus Torvalds 	}
971e06b6405SChuck Lever 
97220b7d86fSArnd Bergmann 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
973f0135740SJ. Bruce Fields }
974f0135740SJ. Bruce Fields 
9759dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
97620b7d86fSArnd Bergmann 				time64_t *time, struct nfsd_net *nn)
977f0135740SJ. Bruce Fields {
978f0135740SJ. Bruce Fields 	ssize_t rv;
979f0135740SJ. Bruce Fields 
980f0135740SJ. Bruce Fields 	mutex_lock(&nfsd_mutex);
9819dd9845fSStanislav Kinsbursky 	rv = __nfsd4_write_time(file, buf, size, time, nn);
982f0135740SJ. Bruce Fields 	mutex_unlock(&nfsd_mutex);
983f0135740SJ. Bruce Fields 	return rv;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds 
986f2453978SChuck Lever /*
987262a0982SChuck Lever  * write_leasetime - Set or report the current NFSv4 lease time
988262a0982SChuck Lever  *
989262a0982SChuck Lever  * Input:
990262a0982SChuck Lever  *			buf:		ignored
991262a0982SChuck Lever  *			size:		zero
992262a0982SChuck Lever  *
993262a0982SChuck Lever  * OR
994262a0982SChuck Lever  *
995262a0982SChuck Lever  * Input:
996262a0982SChuck Lever  *			buf:		C string containing an unsigned
997262a0982SChuck Lever  *					integer value representing the new
998262a0982SChuck Lever  *					NFSv4 lease expiry time
999262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1000262a0982SChuck Lever  * Output:
1001262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
1002262a0982SChuck Lever  *			string containing unsigned integer value of the
1003262a0982SChuck Lever  *			current lease expiry time;
1004262a0982SChuck Lever  *			return code is the size in bytes of the string
1005262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1006262a0982SChuck Lever  */
10073dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
10083dd98a3bSJeff Layton {
1009244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10109dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
10113dd98a3bSJeff Layton }
10123dd98a3bSJeff Layton 
1013f2453978SChuck Lever /*
1014efc4bb4fSJ. Bruce Fields  * write_gracetime - Set or report current NFSv4 grace period time
1015efc4bb4fSJ. Bruce Fields  *
1016efc4bb4fSJ. Bruce Fields  * As above, but sets the time of the NFSv4 grace period.
1017efc4bb4fSJ. Bruce Fields  *
1018efc4bb4fSJ. Bruce Fields  * Note this should never be set to less than the *previous*
1019efc4bb4fSJ. Bruce Fields  * lease-period time, but we don't try to enforce this.  (In the common
1020efc4bb4fSJ. Bruce Fields  * case (a new boot), we don't know what the previous lease time was
1021efc4bb4fSJ. Bruce Fields  * anyway.)
1022efc4bb4fSJ. Bruce Fields  */
1023efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1024efc4bb4fSJ. Bruce Fields {
1025244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10269dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1027efc4bb4fSJ. Bruce Fields }
1028efc4bb4fSJ. Bruce Fields 
10299dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
10309dd9845fSStanislav Kinsbursky 				   struct nfsd_net *nn)
10310964a3d3SNeilBrown {
10320964a3d3SNeilBrown 	char *mesg = buf;
10330964a3d3SNeilBrown 	char *recdir;
10340964a3d3SNeilBrown 	int len, status;
10350964a3d3SNeilBrown 
10363dd98a3bSJeff Layton 	if (size > 0) {
10379dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
10383dd98a3bSJeff Layton 			return -EBUSY;
10393dd98a3bSJeff Layton 		if (size > PATH_MAX || buf[size-1] != '\n')
10400964a3d3SNeilBrown 			return -EINVAL;
10410964a3d3SNeilBrown 		buf[size-1] = 0;
10420964a3d3SNeilBrown 
10430964a3d3SNeilBrown 		recdir = mesg;
10440964a3d3SNeilBrown 		len = qword_get(&mesg, recdir, size);
10450964a3d3SNeilBrown 		if (len <= 0)
10460964a3d3SNeilBrown 			return -EINVAL;
10470964a3d3SNeilBrown 
10480964a3d3SNeilBrown 		status = nfs4_reset_recoverydir(recdir);
104969049961SAndi Kleen 		if (status)
105069049961SAndi Kleen 			return status;
10513dd98a3bSJeff Layton 	}
10523d72ab8fSChuck Lever 
10533d72ab8fSChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
10543d72ab8fSChuck Lever 							nfs4_recoverydir());
10550964a3d3SNeilBrown }
10563dd98a3bSJeff Layton 
1057f2453978SChuck Lever /*
1058262a0982SChuck Lever  * write_recoverydir - Set or report the pathname of the recovery directory
1059262a0982SChuck Lever  *
1060262a0982SChuck Lever  * Input:
1061262a0982SChuck Lever  *			buf:		ignored
1062262a0982SChuck Lever  *			size:		zero
1063262a0982SChuck Lever  *
1064262a0982SChuck Lever  * OR
1065262a0982SChuck Lever  *
1066262a0982SChuck Lever  * Input:
1067262a0982SChuck Lever  *			buf:		C string containing the pathname
1068262a0982SChuck Lever  *					of the directory on a local file
1069262a0982SChuck Lever  *					system containing permanent NFSv4
1070262a0982SChuck Lever  *					recovery data
1071262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1072262a0982SChuck Lever  * Output:
1073262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
1074262a0982SChuck Lever  *			containing the current recovery pathname setting;
1075262a0982SChuck Lever  *			return code is the size in bytes of the string
1076262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1077262a0982SChuck Lever  */
10783dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
10793dd98a3bSJeff Layton {
10803dd98a3bSJeff Layton 	ssize_t rv;
1081244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10823dd98a3bSJeff Layton 
10833dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
10849dd9845fSStanislav Kinsbursky 	rv = __write_recoverydir(file, buf, size, nn);
10853dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
10863dd98a3bSJeff Layton 	return rv;
10873dd98a3bSJeff Layton }
10883dd98a3bSJeff Layton 
1089f2453978SChuck Lever /*
10907f5ef2e9SJeff Layton  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
10917f5ef2e9SJeff Layton  *
10927f5ef2e9SJeff Layton  * Input:
10937f5ef2e9SJeff Layton  *			buf:		ignored
10947f5ef2e9SJeff Layton  *			size:		zero
10957f5ef2e9SJeff Layton  * OR
10967f5ef2e9SJeff Layton  *
10977f5ef2e9SJeff Layton  * Input:
10987f5ef2e9SJeff Layton  * 			buf:		any value
10997f5ef2e9SJeff Layton  *			size:		non-zero length of C string in @buf
11007f5ef2e9SJeff Layton  * Output:
11017f5ef2e9SJeff Layton  *			passed-in buffer filled with "Y" or "N" with a newline
11027f5ef2e9SJeff Layton  *			and NULL-terminated C string. This indicates whether
11037f5ef2e9SJeff Layton  *			the grace period has ended in the current net
11047f5ef2e9SJeff Layton  *			namespace. Return code is the size in bytes of the
11057f5ef2e9SJeff Layton  *			string. Writing a string that starts with 'Y', 'y', or
11067f5ef2e9SJeff Layton  *			'1' to the file will end the grace period for nfsd's v4
11077f5ef2e9SJeff Layton  *			lock manager.
11087f5ef2e9SJeff Layton  */
11097f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
11107f5ef2e9SJeff Layton {
1111244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
11127f5ef2e9SJeff Layton 
11137f5ef2e9SJeff Layton 	if (size > 0) {
11147f5ef2e9SJeff Layton 		switch(buf[0]) {
11157f5ef2e9SJeff Layton 		case 'Y':
11167f5ef2e9SJeff Layton 		case 'y':
11177f5ef2e9SJeff Layton 		case '1':
1118dd838821SYihao Wu 			if (!nn->nfsd_serv)
111962a063b8SJ. Bruce Fields 				return -EBUSY;
11207f5ef2e9SJeff Layton 			nfsd4_end_grace(nn);
11217f5ef2e9SJeff Layton 			break;
11227f5ef2e9SJeff Layton 		default:
11237f5ef2e9SJeff Layton 			return -EINVAL;
11247f5ef2e9SJeff Layton 		}
11257f5ef2e9SJeff Layton 	}
11267f5ef2e9SJeff Layton 
11277f5ef2e9SJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
11287f5ef2e9SJeff Layton 			 nn->grace_ended ? 'Y' : 'N');
11297f5ef2e9SJeff Layton }
11307f5ef2e9SJeff Layton 
113170c3b76cSNeilBrown #endif
11320964a3d3SNeilBrown 
11331da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
11341da177e4SLinus Torvalds /*
11351da177e4SLinus Torvalds  *	populating the filesystem.
11361da177e4SLinus Torvalds  */
11371da177e4SLinus Torvalds 
1138e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */
1139e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1140e8a79fb1SJ. Bruce Fields {
1141e8a79fb1SJ. Bruce Fields 	struct inode *inode = new_inode(sb);
1142e8a79fb1SJ. Bruce Fields 	if (!inode)
1143e8a79fb1SJ. Bruce Fields 		return NULL;
1144e8a79fb1SJ. Bruce Fields 	/* Following advice from simple_fill_super documentation: */
1145e8a79fb1SJ. Bruce Fields 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1146e8a79fb1SJ. Bruce Fields 	inode->i_mode = mode;
1147e8a79fb1SJ. Bruce Fields 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1148e8a79fb1SJ. Bruce Fields 	switch (mode & S_IFMT) {
1149e8a79fb1SJ. Bruce Fields 	case S_IFDIR:
1150e8a79fb1SJ. Bruce Fields 		inode->i_fop = &simple_dir_operations;
1151e8a79fb1SJ. Bruce Fields 		inode->i_op = &simple_dir_inode_operations;
1152e8a79fb1SJ. Bruce Fields 		inc_nlink(inode);
115376c50eb7SGustavo A. R. Silva 		break;
1154e8a79fb1SJ. Bruce Fields 	default:
1155e8a79fb1SJ. Bruce Fields 		break;
1156e8a79fb1SJ. Bruce Fields 	}
1157e8a79fb1SJ. Bruce Fields 	return inode;
1158e8a79fb1SJ. Bruce Fields }
1159e8a79fb1SJ. Bruce Fields 
1160bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1161e8a79fb1SJ. Bruce Fields {
1162e8a79fb1SJ. Bruce Fields 	struct inode *inode;
1163e8a79fb1SJ. Bruce Fields 
1164e8a79fb1SJ. Bruce Fields 	inode = nfsd_get_inode(dir->i_sb, mode);
1165e8a79fb1SJ. Bruce Fields 	if (!inode)
1166e8a79fb1SJ. Bruce Fields 		return -ENOMEM;
1167bebd6997SJ. Bruce Fields 	if (ncl) {
1168bebd6997SJ. Bruce Fields 		inode->i_private = ncl;
1169bebd6997SJ. Bruce Fields 		kref_get(&ncl->cl_ref);
1170bebd6997SJ. Bruce Fields 	}
1171e8a79fb1SJ. Bruce Fields 	d_add(dentry, inode);
1172e8a79fb1SJ. Bruce Fields 	inc_nlink(dir);
1173e8a79fb1SJ. Bruce Fields 	fsnotify_mkdir(dir, dentry);
1174e8a79fb1SJ. Bruce Fields 	return 0;
1175e8a79fb1SJ. Bruce Fields }
1176e8a79fb1SJ. Bruce Fields 
1177e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1178e8a79fb1SJ. Bruce Fields {
1179e8a79fb1SJ. Bruce Fields 	struct inode *dir = parent->d_inode;
1180e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1181e8a79fb1SJ. Bruce Fields 	int ret = -ENOMEM;
1182e8a79fb1SJ. Bruce Fields 
1183e8a79fb1SJ. Bruce Fields 	inode_lock(dir);
1184e8a79fb1SJ. Bruce Fields 	dentry = d_alloc_name(parent, name);
1185e8a79fb1SJ. Bruce Fields 	if (!dentry)
1186e8a79fb1SJ. Bruce Fields 		goto out_err;
1187bebd6997SJ. Bruce Fields 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1188e8a79fb1SJ. Bruce Fields 	if (ret)
1189e8a79fb1SJ. Bruce Fields 		goto out_err;
1190e8a79fb1SJ. Bruce Fields out:
1191e8a79fb1SJ. Bruce Fields 	inode_unlock(dir);
1192e8a79fb1SJ. Bruce Fields 	return dentry;
1193e8a79fb1SJ. Bruce Fields out_err:
1194d6846bfbSTetsuo Handa 	dput(dentry);
1195e8a79fb1SJ. Bruce Fields 	dentry = ERR_PTR(ret);
1196e8a79fb1SJ. Bruce Fields 	goto out;
1197e8a79fb1SJ. Bruce Fields }
1198e8a79fb1SJ. Bruce Fields 
119997ad4031SJ. Bruce Fields static void clear_ncl(struct inode *inode)
1200e8a79fb1SJ. Bruce Fields {
120197ad4031SJ. Bruce Fields 	struct nfsdfs_client *ncl = inode->i_private;
120297ad4031SJ. Bruce Fields 
120397ad4031SJ. Bruce Fields 	inode->i_private = NULL;
120497ad4031SJ. Bruce Fields 	kref_put(&ncl->cl_ref, ncl->cl_release);
120597ad4031SJ. Bruce Fields }
120697ad4031SJ. Bruce Fields 
1207b78fa45dSYueHaibing static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
120897ad4031SJ. Bruce Fields {
120997ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc = inode->i_private;
121097ad4031SJ. Bruce Fields 
121197ad4031SJ. Bruce Fields 	if (nc)
121297ad4031SJ. Bruce Fields 		kref_get(&nc->cl_ref);
121397ad4031SJ. Bruce Fields 	return nc;
121497ad4031SJ. Bruce Fields }
121597ad4031SJ. Bruce Fields 
121697ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
121797ad4031SJ. Bruce Fields {
121897ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc;
121997ad4031SJ. Bruce Fields 
1220dc46bba7SJ. Bruce Fields 	inode_lock_shared(inode);
122197ad4031SJ. Bruce Fields 	nc = __get_nfsdfs_client(inode);
1222dc46bba7SJ. Bruce Fields 	inode_unlock_shared(inode);
122397ad4031SJ. Bruce Fields 	return nc;
122497ad4031SJ. Bruce Fields }
122597ad4031SJ. Bruce Fields /* from __rpc_unlink */
122697ad4031SJ. Bruce Fields static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
122797ad4031SJ. Bruce Fields {
122897ad4031SJ. Bruce Fields 	int ret;
122997ad4031SJ. Bruce Fields 
123097ad4031SJ. Bruce Fields 	clear_ncl(d_inode(dentry));
123197ad4031SJ. Bruce Fields 	dget(dentry);
123297ad4031SJ. Bruce Fields 	ret = simple_unlink(dir, dentry);
123329044daeSAmir Goldstein 	d_drop(dentry);
123429044daeSAmir Goldstein 	fsnotify_unlink(dir, dentry);
123597ad4031SJ. Bruce Fields 	dput(dentry);
123697ad4031SJ. Bruce Fields 	WARN_ON_ONCE(ret);
123797ad4031SJ. Bruce Fields }
123897ad4031SJ. Bruce Fields 
123997ad4031SJ. Bruce Fields static void nfsdfs_remove_files(struct dentry *root)
124097ad4031SJ. Bruce Fields {
124197ad4031SJ. Bruce Fields 	struct dentry *dentry, *tmp;
124297ad4031SJ. Bruce Fields 
124397ad4031SJ. Bruce Fields 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
124497ad4031SJ. Bruce Fields 		if (!simple_positive(dentry)) {
124597ad4031SJ. Bruce Fields 			WARN_ON_ONCE(1); /* I think this can't happen? */
124697ad4031SJ. Bruce Fields 			continue;
124797ad4031SJ. Bruce Fields 		}
124897ad4031SJ. Bruce Fields 		nfsdfs_remove_file(d_inode(root), dentry);
124997ad4031SJ. Bruce Fields 	}
125097ad4031SJ. Bruce Fields }
125197ad4031SJ. Bruce Fields 
125297ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
125397ad4031SJ. Bruce Fields  * code instead. */
125497ad4031SJ. Bruce Fields static  int nfsdfs_create_files(struct dentry *root,
1255472d155aSNeilBrown 				const struct tree_descr *files,
1256472d155aSNeilBrown 				struct dentry **fdentries)
125797ad4031SJ. Bruce Fields {
125897ad4031SJ. Bruce Fields 	struct inode *dir = d_inode(root);
125997ad4031SJ. Bruce Fields 	struct inode *inode;
126097ad4031SJ. Bruce Fields 	struct dentry *dentry;
126197ad4031SJ. Bruce Fields 	int i;
126297ad4031SJ. Bruce Fields 
126397ad4031SJ. Bruce Fields 	inode_lock(dir);
126497ad4031SJ. Bruce Fields 	for (i = 0; files->name && files->name[0]; i++, files++) {
126597ad4031SJ. Bruce Fields 		dentry = d_alloc_name(root, files->name);
126697ad4031SJ. Bruce Fields 		if (!dentry)
126797ad4031SJ. Bruce Fields 			goto out;
126897ad4031SJ. Bruce Fields 		inode = nfsd_get_inode(d_inode(root)->i_sb,
126997ad4031SJ. Bruce Fields 					S_IFREG | files->mode);
127097ad4031SJ. Bruce Fields 		if (!inode) {
127197ad4031SJ. Bruce Fields 			dput(dentry);
127297ad4031SJ. Bruce Fields 			goto out;
127397ad4031SJ. Bruce Fields 		}
127497ad4031SJ. Bruce Fields 		inode->i_fop = files->ops;
127597ad4031SJ. Bruce Fields 		inode->i_private = __get_nfsdfs_client(dir);
127697ad4031SJ. Bruce Fields 		d_add(dentry, inode);
127797ad4031SJ. Bruce Fields 		fsnotify_create(dir, dentry);
1278472d155aSNeilBrown 		if (fdentries)
1279472d155aSNeilBrown 			fdentries[i] = dentry;
128097ad4031SJ. Bruce Fields 	}
128197ad4031SJ. Bruce Fields 	inode_unlock(dir);
128297ad4031SJ. Bruce Fields 	return 0;
128397ad4031SJ. Bruce Fields out:
128497ad4031SJ. Bruce Fields 	nfsdfs_remove_files(root);
128597ad4031SJ. Bruce Fields 	inode_unlock(dir);
128697ad4031SJ. Bruce Fields 	return -ENOMEM;
128797ad4031SJ. Bruce Fields }
128897ad4031SJ. Bruce Fields 
128997ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */
129097ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
129197ad4031SJ. Bruce Fields 				 struct nfsdfs_client *ncl, u32 id,
1292472d155aSNeilBrown 				 const struct tree_descr *files,
1293472d155aSNeilBrown 				 struct dentry **fdentries)
129497ad4031SJ. Bruce Fields {
129597ad4031SJ. Bruce Fields 	struct dentry *dentry;
1296e8a79fb1SJ. Bruce Fields 	char name[11];
129797ad4031SJ. Bruce Fields 	int ret;
1298e8a79fb1SJ. Bruce Fields 
1299bf5ed3e3SJ. Bruce Fields 	sprintf(name, "%u", id);
1300e8a79fb1SJ. Bruce Fields 
130197ad4031SJ. Bruce Fields 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
130297ad4031SJ. Bruce Fields 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
130397ad4031SJ. Bruce Fields 		return NULL;
1304472d155aSNeilBrown 	ret = nfsdfs_create_files(dentry, files, fdentries);
130597ad4031SJ. Bruce Fields 	if (ret) {
130697ad4031SJ. Bruce Fields 		nfsd_client_rmdir(dentry);
130797ad4031SJ. Bruce Fields 		return NULL;
130897ad4031SJ. Bruce Fields 	}
130997ad4031SJ. Bruce Fields 	return dentry;
1310e8a79fb1SJ. Bruce Fields }
1311e8a79fb1SJ. Bruce Fields 
1312e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */
1313e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry)
1314e8a79fb1SJ. Bruce Fields {
1315e8a79fb1SJ. Bruce Fields 	struct inode *dir = d_inode(dentry->d_parent);
1316e8a79fb1SJ. Bruce Fields 	struct inode *inode = d_inode(dentry);
1317e8a79fb1SJ. Bruce Fields 	int ret;
1318e8a79fb1SJ. Bruce Fields 
131997ad4031SJ. Bruce Fields 	inode_lock(dir);
132097ad4031SJ. Bruce Fields 	nfsdfs_remove_files(dentry);
132197ad4031SJ. Bruce Fields 	clear_ncl(inode);
1322e8a79fb1SJ. Bruce Fields 	dget(dentry);
1323e8a79fb1SJ. Bruce Fields 	ret = simple_rmdir(dir, dentry);
1324e8a79fb1SJ. Bruce Fields 	WARN_ON_ONCE(ret);
132529044daeSAmir Goldstein 	d_drop(dentry);
132669afd267SJ. Bruce Fields 	fsnotify_rmdir(dir, dentry);
1327bf265401SJ. Bruce Fields 	dput(dentry);
132897ad4031SJ. Bruce Fields 	inode_unlock(dir);
1329e8a79fb1SJ. Bruce Fields }
1330e8a79fb1SJ. Bruce Fields 
133196a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
13321da177e4SLinus Torvalds {
1333e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1334e8a79fb1SJ. Bruce Fields 							nfsd_net_id);
1335e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1336e8a79fb1SJ. Bruce Fields 	int ret;
1337e8a79fb1SJ. Bruce Fields 
1338cda37124SEric Biggers 	static const struct tree_descr nfsd_files[] = {
133996d851c4SStanislav Kinsbursky 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
134020ad856eSAmir Goldstein 		/* Per-export io stats use same ops as exports file */
134120ad856eSAmir Goldstein 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1342e8e8753fSJ. Bruce Fields 		[NFSD_Export_features] = {"export_features",
13439beeaab8SChenXiaoSong 					&export_features_fops, S_IRUGO},
13444373ea84SWendy Cheng 		[NFSD_FO_UnlockIP] = {"unlock_ip",
13454373ea84SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
134617efa372SWendy Cheng 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
134717efa372SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
13481da177e4SLinus Torvalds 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
13491da177e4SLinus Torvalds 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1350eed2965aSGreg Banks 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
135103cf6c9fSGreg Banks 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1352*64776611SChenXiaoSong 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1353*64776611SChenXiaoSong 					&nfsd_reply_cache_stats_fops, S_IRUGO},
135470c3b76cSNeilBrown 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
135580212d59SNeilBrown 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1356596bbe53SNeilBrown 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
13575b8db00bSJeff Layton 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
13582e6c6e4cSChuck Lever 		[NFSD_Filecache] = {"filecache", &filecache_ops, S_IRUGO},
1359b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
13609beeaab8SChenXiaoSong 		[NFSD_SupportedEnctypes] = {"supported_krb5_enctypes",
13619beeaab8SChenXiaoSong 					&supported_enctypes_fops, S_IRUGO},
1362b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
13631da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4
13641da177e4SLinus Torvalds 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1365efc4bb4fSJ. Bruce Fields 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
13660964a3d3SNeilBrown 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
13677f5ef2e9SJeff Layton 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
13681da177e4SLinus Torvalds #endif
13691da177e4SLinus Torvalds 		/* last one */ {""}
13701da177e4SLinus Torvalds 	};
137196a374a3SDavid Howells 
1372e8a79fb1SJ. Bruce Fields 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1373e8a79fb1SJ. Bruce Fields 	if (ret)
1374e8a79fb1SJ. Bruce Fields 		return ret;
1375e8a79fb1SJ. Bruce Fields 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1376e8a79fb1SJ. Bruce Fields 	if (IS_ERR(dentry))
1377e8a79fb1SJ. Bruce Fields 		return PTR_ERR(dentry);
1378e8a79fb1SJ. Bruce Fields 	nn->nfsd_client_dir = dentry;
1379e8a79fb1SJ. Bruce Fields 	return 0;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds 
138296a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc)
13831da177e4SLinus Torvalds {
1384533770ccSAl Viro 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
138596a374a3SDavid Howells }
138696a374a3SDavid Howells 
138796a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc)
138896a374a3SDavid Howells {
138996a374a3SDavid Howells 	if (fc->s_fs_info)
139096a374a3SDavid Howells 		put_net(fc->s_fs_info);
139196a374a3SDavid Howells }
139296a374a3SDavid Howells 
139396a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = {
139496a374a3SDavid Howells 	.free		= nfsd_fs_free_fc,
139596a374a3SDavid Howells 	.get_tree	= nfsd_fs_get_tree,
139696a374a3SDavid Howells };
139796a374a3SDavid Howells 
139896a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc)
139996a374a3SDavid Howells {
140096a374a3SDavid Howells 	put_user_ns(fc->user_ns);
140196a374a3SDavid Howells 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
140296a374a3SDavid Howells 	fc->ops = &nfsd_fs_context_ops;
140396a374a3SDavid Howells 	return 0;
140411f77942SStanislav Kinsbursky }
140511f77942SStanislav Kinsbursky 
140611f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb)
140711f77942SStanislav Kinsbursky {
140811f77942SStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
140911f77942SStanislav Kinsbursky 
1410c6c7f2a8STrond Myklebust 	nfsd_shutdown_threads(net);
1411c6c7f2a8STrond Myklebust 
141211f77942SStanislav Kinsbursky 	kill_litter_super(sb);
141311f77942SStanislav Kinsbursky 	put_net(net);
14141da177e4SLinus Torvalds }
14151da177e4SLinus Torvalds 
14161da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = {
14171da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
14181da177e4SLinus Torvalds 	.name		= "nfsd",
141996a374a3SDavid Howells 	.init_fs_context = nfsd_init_fs_context,
142011f77942SStanislav Kinsbursky 	.kill_sb	= nfsd_umount,
14211da177e4SLinus Torvalds };
14227f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd");
14231da177e4SLinus Torvalds 
1424e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS
1425e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1426e331f606SJ. Bruce Fields {
1427e331f606SJ. Bruce Fields 	struct proc_dir_entry *entry;
1428e331f606SJ. Bruce Fields 
1429e331f606SJ. Bruce Fields 	entry = proc_mkdir("fs/nfs", NULL);
1430e331f606SJ. Bruce Fields 	if (!entry)
1431e331f606SJ. Bruce Fields 		return -ENOMEM;
143297a32539SAlexey Dobriyan 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1433ff7c4b36Sfanchaoting 	if (!entry) {
1434ff7c4b36Sfanchaoting 		remove_proc_entry("fs/nfs", NULL);
1435e331f606SJ. Bruce Fields 		return -ENOMEM;
1436ff7c4b36Sfanchaoting 	}
1437e331f606SJ. Bruce Fields 	return 0;
1438e331f606SJ. Bruce Fields }
1439e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */
1440e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1441e331f606SJ. Bruce Fields {
1442e331f606SJ. Bruce Fields 	return 0;
1443e331f606SJ. Bruce Fields }
1444e331f606SJ. Bruce Fields #endif
1445e331f606SJ. Bruce Fields 
1446c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id;
14475717e012SStanislav Kinsbursky 
14485717e012SStanislav Kinsbursky static __net_init int nfsd_init_net(struct net *net)
14495717e012SStanislav Kinsbursky {
14505717e012SStanislav Kinsbursky 	int retval;
14513d733711SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14525717e012SStanislav Kinsbursky 
14535717e012SStanislav Kinsbursky 	retval = nfsd_export_init(net);
14545717e012SStanislav Kinsbursky 	if (retval)
14555717e012SStanislav Kinsbursky 		goto out_export_error;
1456f69adb2fSStanislav Kinsbursky 	retval = nfsd_idmap_init(net);
1457f69adb2fSStanislav Kinsbursky 	if (retval)
1458f69adb2fSStanislav Kinsbursky 		goto out_idmap_error;
1459e333f3bbSTrond Myklebust 	nn->nfsd_versions = NULL;
1460e333f3bbSTrond Myklebust 	nn->nfsd4_minorversions = NULL;
14617746b32fSDai Ngo 	retval = nfsd4_init_leases_net(nn);
14627746b32fSDai Ngo 	if (retval)
14637746b32fSDai Ngo 		goto out_drc_error;
14643ba75830SJ. Bruce Fields 	retval = nfsd_reply_cache_init(nn);
14653ba75830SJ. Bruce Fields 	if (retval)
14663ba75830SJ. Bruce Fields 		goto out_drc_error;
146791d2e9b5SChuck Lever 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
146891d2e9b5SChuck Lever 	seqlock_init(&nn->writeverf_lock);
14692c830dd7SJ. Bruce Fields 
14705717e012SStanislav Kinsbursky 	return 0;
14715717e012SStanislav Kinsbursky 
14723ba75830SJ. Bruce Fields out_drc_error:
14733ba75830SJ. Bruce Fields 	nfsd_idmap_shutdown(net);
1474f69adb2fSStanislav Kinsbursky out_idmap_error:
1475f69adb2fSStanislav Kinsbursky 	nfsd_export_shutdown(net);
14765717e012SStanislav Kinsbursky out_export_error:
14775717e012SStanislav Kinsbursky 	return retval;
14785717e012SStanislav Kinsbursky }
14795717e012SStanislav Kinsbursky 
14805717e012SStanislav Kinsbursky static __net_exit void nfsd_exit_net(struct net *net)
14815717e012SStanislav Kinsbursky {
14823ba75830SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14833ba75830SJ. Bruce Fields 
14843ba75830SJ. Bruce Fields 	nfsd_reply_cache_shutdown(nn);
1485f69adb2fSStanislav Kinsbursky 	nfsd_idmap_shutdown(net);
14865717e012SStanislav Kinsbursky 	nfsd_export_shutdown(net);
1487e333f3bbSTrond Myklebust 	nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
14887746b32fSDai Ngo 	nfsd4_leases_net_shutdown(nn);
14895717e012SStanislav Kinsbursky }
14905717e012SStanislav Kinsbursky 
14917ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = {
14925717e012SStanislav Kinsbursky 	.init = nfsd_init_net,
14935717e012SStanislav Kinsbursky 	.exit = nfsd_exit_net,
14947ea34ac1SJeff Layton 	.id   = &nfsd_net_id,
14957ea34ac1SJeff Layton 	.size = sizeof(struct nfsd_net),
14967ea34ac1SJeff Layton };
14977ea34ac1SJeff Layton 
14981da177e4SLinus Torvalds static int __init init_nfsd(void)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	int retval;
15011da177e4SLinus Torvalds 
1502bb7ffbf2SGiuseppe Cantavenera 	retval = nfsd4_init_slabs();
1503bb7ffbf2SGiuseppe Cantavenera 	if (retval)
1504b10252c7SAlexander Sverdlin 		return retval;
15059cf514ccSChristoph Hellwig 	retval = nfsd4_init_pnfs();
150665178db4SBryan Schumaker 	if (retval)
150765178db4SBryan Schumaker 		goto out_free_slabs;
1508e567b98cSAmir Goldstein 	retval = nfsd_stat_init();	/* Statistics */
1509e567b98cSAmir Goldstein 	if (retval)
1510e567b98cSAmir Goldstein 		goto out_free_pnfs;
1511027690c7SJ. Bruce Fields 	retval = nfsd_drc_slab_create();
1512027690c7SJ. Bruce Fields 	if (retval)
1513027690c7SJ. Bruce Fields 		goto out_free_stat;
15141da177e4SLinus Torvalds 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1515e331f606SJ. Bruce Fields 	retval = create_proc_exports_entry();
1516e331f606SJ. Bruce Fields 	if (retval)
1517f69adb2fSStanislav Kinsbursky 		goto out_free_lockd;
1518bd5ae928SJ. Bruce Fields 	retval = register_pernet_subsys(&nfsd_net_ops);
1519bd5ae928SJ. Bruce Fields 	if (retval < 0)
15206f6f84aaSZhang Xiaoxu 		goto out_free_exports;
1521b10252c7SAlexander Sverdlin 	retval = register_cld_notifier();
1522b10252c7SAlexander Sverdlin 	if (retval)
152362fdb65eSZhang Xiaoxu 		goto out_free_subsys;
1524d76cc46bSDai Ngo 	retval = nfsd4_create_laundry_wq();
1525d76cc46bSDai Ngo 	if (retval)
15266f6f84aaSZhang Xiaoxu 		goto out_free_cld;
15276f6f84aaSZhang Xiaoxu 	retval = register_filesystem(&nfsd_fs_type);
15286f6f84aaSZhang Xiaoxu 	if (retval)
1529d76cc46bSDai Ngo 		goto out_free_all;
153026808d3fSJ. Bruce Fields 	return 0;
153126808d3fSJ. Bruce Fields out_free_all:
15326f6f84aaSZhang Xiaoxu 	nfsd4_destroy_laundry_wq();
15336f6f84aaSZhang Xiaoxu out_free_cld:
153462fdb65eSZhang Xiaoxu 	unregister_cld_notifier();
153562fdb65eSZhang Xiaoxu out_free_subsys:
1536b10252c7SAlexander Sverdlin 	unregister_pernet_subsys(&nfsd_net_ops);
1537bd5ae928SJ. Bruce Fields out_free_exports:
15381da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15391da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
1540dbf847ecSJ. Bruce Fields out_free_lockd:
15411da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1542027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
1543027690c7SJ. Bruce Fields out_free_stat:
1544d5c3428bSJ. Bruce Fields 	nfsd_stat_shutdown();
1545e567b98cSAmir Goldstein out_free_pnfs:
15469cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
154765178db4SBryan Schumaker out_free_slabs:
154846b25895SJ. Bruce Fields 	nfsd4_free_slabs();
15491da177e4SLinus Torvalds 	return retval;
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds 
15521da177e4SLinus Torvalds static void __exit exit_nfsd(void)
15531da177e4SLinus Torvalds {
15546f6f84aaSZhang Xiaoxu 	unregister_filesystem(&nfsd_fs_type);
1555d76cc46bSDai Ngo 	nfsd4_destroy_laundry_wq();
1556b10252c7SAlexander Sverdlin 	unregister_cld_notifier();
1557bd5ae928SJ. Bruce Fields 	unregister_pernet_subsys(&nfsd_net_ops);
1558027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
15591da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15601da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
15611da177e4SLinus Torvalds 	nfsd_stat_shutdown();
15621da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1563e8ff2a84SJ. Bruce Fields 	nfsd4_free_slabs();
15649cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
15681da177e4SLinus Torvalds MODULE_LICENSE("GPL");
15691da177e4SLinus Torvalds module_init(init_nfsd)
15701da177e4SLinus Torvalds module_exit(exit_nfsd)
1571