xref: /openbmc/linux/fs/nfsd/nfsctl.c (revision 29044dae)
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"
289a74af21SBoaz Harrosh 
291da177e4SLinus Torvalds /*
30b0b0c0a2SKevin Coffman  *	We have a single directory with several nodes in it.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds enum {
331da177e4SLinus Torvalds 	NFSD_Root = 1,
341da177e4SLinus Torvalds 	NFSD_List,
3520ad856eSAmir Goldstein 	NFSD_Export_Stats,
36e8e8753fSJ. Bruce Fields 	NFSD_Export_features,
371da177e4SLinus Torvalds 	NFSD_Fh,
384373ea84SWendy Cheng 	NFSD_FO_UnlockIP,
3917efa372SWendy Cheng 	NFSD_FO_UnlockFS,
401da177e4SLinus Torvalds 	NFSD_Threads,
41eed2965aSGreg Banks 	NFSD_Pool_Threads,
4203cf6c9fSGreg Banks 	NFSD_Pool_Stats,
43a2f999a3SJeff Layton 	NFSD_Reply_Cache_Stats,
4470c3b76cSNeilBrown 	NFSD_Versions,
4580212d59SNeilBrown 	NFSD_Ports,
46596bbe53SNeilBrown 	NFSD_MaxBlkSize,
475b8db00bSJeff Layton 	NFSD_MaxConnections,
48b0b0c0a2SKevin Coffman 	NFSD_SupportedEnctypes,
4970c3b76cSNeilBrown 	/*
5070c3b76cSNeilBrown 	 * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
5170c3b76cSNeilBrown 	 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
5270c3b76cSNeilBrown 	 */
5370c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
541da177e4SLinus Torvalds 	NFSD_Leasetime,
55efc4bb4fSJ. Bruce Fields 	NFSD_Gracetime,
560964a3d3SNeilBrown 	NFSD_RecoveryDir,
577f5ef2e9SJeff Layton 	NFSD_V4EndGrace,
5870c3b76cSNeilBrown #endif
59e8a79fb1SJ. Bruce Fields 	NFSD_MaxReserved
601da177e4SLinus Torvalds };
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds /*
631da177e4SLinus Torvalds  * write() for these nodes.
641da177e4SLinus Torvalds  */
651da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
66b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
67b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
681da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size);
69eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
7070c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size);
7180212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size);
72596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
735b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
7470c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
751da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
76efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
770964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
787f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
7970c3b76cSNeilBrown #endif
801da177e4SLinus Torvalds 
81c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
821da177e4SLinus Torvalds 	[NFSD_Fh] = write_filehandle,
83b046ccdcSChuck Lever 	[NFSD_FO_UnlockIP] = write_unlock_ip,
84b046ccdcSChuck Lever 	[NFSD_FO_UnlockFS] = write_unlock_fs,
851da177e4SLinus Torvalds 	[NFSD_Threads] = write_threads,
86eed2965aSGreg Banks 	[NFSD_Pool_Threads] = write_pool_threads,
8770c3b76cSNeilBrown 	[NFSD_Versions] = write_versions,
8880212d59SNeilBrown 	[NFSD_Ports] = write_ports,
89596bbe53SNeilBrown 	[NFSD_MaxBlkSize] = write_maxblksize,
905b8db00bSJeff Layton 	[NFSD_MaxConnections] = write_maxconn,
9170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
921da177e4SLinus Torvalds 	[NFSD_Leasetime] = write_leasetime,
93efc4bb4fSJ. Bruce Fields 	[NFSD_Gracetime] = write_gracetime,
940964a3d3SNeilBrown 	[NFSD_RecoveryDir] = write_recoverydir,
957f5ef2e9SJeff Layton 	[NFSD_V4EndGrace] = write_v4_end_grace,
9670c3b76cSNeilBrown #endif
971da177e4SLinus Torvalds };
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
1001da177e4SLinus Torvalds {
101496ad9aaSAl Viro 	ino_t ino =  file_inode(file)->i_ino;
1021da177e4SLinus Torvalds 	char *data;
1031da177e4SLinus Torvalds 	ssize_t rv;
1041da177e4SLinus Torvalds 
105e8c96f8cSTobias Klauser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1061da177e4SLinus Torvalds 		return -EINVAL;
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
1091da177e4SLinus Torvalds 	if (IS_ERR(data))
1101da177e4SLinus Torvalds 		return PTR_ERR(data);
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds 	rv =  write_op[ino](file, data, size);
1138971a101SNeilBrown 	if (rv >= 0) {
1141da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
1151da177e4SLinus Torvalds 		rv = size;
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds 	return rv;
1181da177e4SLinus Torvalds }
1191da177e4SLinus Torvalds 
1207390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1217390022dSNeilBrown {
1227390022dSNeilBrown 	if (! file->private_data) {
1237390022dSNeilBrown 		/* An attempt to read a transaction file without writing
1247390022dSNeilBrown 		 * causes a 0-byte write so that the file can return
1257390022dSNeilBrown 		 * state information
1267390022dSNeilBrown 		 */
1277390022dSNeilBrown 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
1287390022dSNeilBrown 		if (rv < 0)
1297390022dSNeilBrown 			return rv;
1307390022dSNeilBrown 	}
1317390022dSNeilBrown 	return simple_transaction_read(file, buf, size, pos);
1327390022dSNeilBrown }
1337390022dSNeilBrown 
1344b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = {
1351da177e4SLinus Torvalds 	.write		= nfsctl_transaction_write,
1367390022dSNeilBrown 	.read		= nfsctl_transaction_read,
1371da177e4SLinus Torvalds 	.release	= simple_transaction_release,
1386038f373SArnd Bergmann 	.llseek		= default_llseek,
1391da177e4SLinus Torvalds };
1401da177e4SLinus Torvalds 
14196d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file)
1421da177e4SLinus Torvalds {
143f2c7ea10SStanislav Kinsbursky 	int err;
144f2c7ea10SStanislav Kinsbursky 	struct seq_file *seq;
14596d851c4SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
146f2c7ea10SStanislav Kinsbursky 
147f2c7ea10SStanislav Kinsbursky 	err = seq_open(file, &nfs_exports_op);
148f2c7ea10SStanislav Kinsbursky 	if (err)
149f2c7ea10SStanislav Kinsbursky 		return err;
150f2c7ea10SStanislav Kinsbursky 
151f2c7ea10SStanislav Kinsbursky 	seq = file->private_data;
152e5f06f72SStanislav Kinsbursky 	seq->private = nn->svc_export_cache;
153f2c7ea10SStanislav Kinsbursky 	return 0;
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds 
15696d851c4SStanislav Kinsbursky static int exports_proc_open(struct inode *inode, struct file *file)
15796d851c4SStanislav Kinsbursky {
15896d851c4SStanislav Kinsbursky 	return exports_net_open(current->nsproxy->net_ns, file);
15996d851c4SStanislav Kinsbursky }
16096d851c4SStanislav Kinsbursky 
16197a32539SAlexey Dobriyan static const struct proc_ops exports_proc_ops = {
16297a32539SAlexey Dobriyan 	.proc_open	= exports_proc_open,
16397a32539SAlexey Dobriyan 	.proc_read	= seq_read,
16497a32539SAlexey Dobriyan 	.proc_lseek	= seq_lseek,
16597a32539SAlexey Dobriyan 	.proc_release	= seq_release,
16696d851c4SStanislav Kinsbursky };
16796d851c4SStanislav Kinsbursky 
16896d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file)
16996d851c4SStanislav Kinsbursky {
17096d851c4SStanislav Kinsbursky 	return exports_net_open(inode->i_sb->s_fs_info, file);
17196d851c4SStanislav Kinsbursky }
17296d851c4SStanislav Kinsbursky 
17396d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = {
17496d851c4SStanislav Kinsbursky 	.open		= exports_nfsd_open,
1751da177e4SLinus Torvalds 	.read		= seq_read,
1761da177e4SLinus Torvalds 	.llseek		= seq_lseek,
1771da177e4SLinus Torvalds 	.release	= seq_release,
1781da177e4SLinus Torvalds };
1791da177e4SLinus Torvalds 
180e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v)
181e8e8753fSJ. Bruce Fields {
182e8e8753fSJ. Bruce Fields 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
183e8e8753fSJ. Bruce Fields 	return 0;
184e8e8753fSJ. Bruce Fields }
185e8e8753fSJ. Bruce Fields 
186e8e8753fSJ. Bruce Fields static int export_features_open(struct inode *inode, struct file *file)
187e8e8753fSJ. Bruce Fields {
188e8e8753fSJ. Bruce Fields 	return single_open(file, export_features_show, NULL);
189e8e8753fSJ. Bruce Fields }
190e8e8753fSJ. Bruce Fields 
19175ef9de1SAl Viro static const struct file_operations export_features_operations = {
192e8e8753fSJ. Bruce Fields 	.open		= export_features_open,
193e8e8753fSJ. Bruce Fields 	.read		= seq_read,
194e8e8753fSJ. Bruce Fields 	.llseek		= seq_lseek,
195e8e8753fSJ. Bruce Fields 	.release	= single_release,
196e8e8753fSJ. Bruce Fields };
197e8e8753fSJ. Bruce Fields 
198b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
199b0b0c0a2SKevin Coffman static int supported_enctypes_show(struct seq_file *m, void *v)
200b0b0c0a2SKevin Coffman {
201b084f598SJ. Bruce Fields 	seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
202b0b0c0a2SKevin Coffman 	return 0;
203b0b0c0a2SKevin Coffman }
204b0b0c0a2SKevin Coffman 
205b0b0c0a2SKevin Coffman static int supported_enctypes_open(struct inode *inode, struct file *file)
206b0b0c0a2SKevin Coffman {
207b0b0c0a2SKevin Coffman 	return single_open(file, supported_enctypes_show, NULL);
208b0b0c0a2SKevin Coffman }
209b0b0c0a2SKevin Coffman 
21075ef9de1SAl Viro static const struct file_operations supported_enctypes_ops = {
211b0b0c0a2SKevin Coffman 	.open		= supported_enctypes_open,
212b0b0c0a2SKevin Coffman 	.read		= seq_read,
213b0b0c0a2SKevin Coffman 	.llseek		= seq_lseek,
214b0b0c0a2SKevin Coffman 	.release	= single_release,
215b0b0c0a2SKevin Coffman };
216b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
217b0b0c0a2SKevin Coffman 
218828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = {
21903cf6c9fSGreg Banks 	.open		= nfsd_pool_stats_open,
22003cf6c9fSGreg Banks 	.read		= seq_read,
22103cf6c9fSGreg Banks 	.llseek		= seq_lseek,
222ed2d8aedSRyusei Yamaguchi 	.release	= nfsd_pool_stats_release,
22303cf6c9fSGreg Banks };
22403cf6c9fSGreg Banks 
2257ba630f5SJulia Lawall static const struct file_operations reply_cache_stats_operations = {
226a2f999a3SJeff Layton 	.open		= nfsd_reply_cache_stats_open,
227a2f999a3SJeff Layton 	.read		= seq_read,
228a2f999a3SJeff Layton 	.llseek		= seq_lseek,
229a2f999a3SJeff Layton 	.release	= single_release,
230a2f999a3SJeff Layton };
231a2f999a3SJeff Layton 
2321da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
2331da177e4SLinus Torvalds /*
2341da177e4SLinus Torvalds  * payload - write methods
2351da177e4SLinus Torvalds  */
2361da177e4SLinus Torvalds 
237244c7d44SAl Viro static inline struct net *netns(struct file *file)
238244c7d44SAl Viro {
239244c7d44SAl Viro 	return file_inode(file)->i_sb->s_fs_info;
240244c7d44SAl Viro }
2411da177e4SLinus Torvalds 
242f2453978SChuck Lever /*
243262a0982SChuck Lever  * write_unlock_ip - Release all locks used by a client
244262a0982SChuck Lever  *
245262a0982SChuck Lever  * Experimental.
246262a0982SChuck Lever  *
247262a0982SChuck Lever  * Input:
248262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing a
2494116092bSChuck Lever  *				presentation format IP address
250262a0982SChuck Lever  *			size:	length of C string in @buf
251262a0982SChuck Lever  * Output:
252262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
253262a0982SChuck Lever  *			returns one if one or more locks were not released
254262a0982SChuck Lever  *	On error:	return code is negative errno value
255262a0982SChuck Lever  */
256b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
2574373ea84SWendy Cheng {
2584116092bSChuck Lever 	struct sockaddr_storage address;
2594116092bSChuck Lever 	struct sockaddr *sap = (struct sockaddr *)&address;
2604116092bSChuck Lever 	size_t salen = sizeof(address);
261367c8c7bSChuck Lever 	char *fo_path;
262244c7d44SAl Viro 	struct net *net = netns(file);
2634373ea84SWendy Cheng 
2644373ea84SWendy Cheng 	/* sanity check */
2654373ea84SWendy Cheng 	if (size == 0)
2664373ea84SWendy Cheng 		return -EINVAL;
2674373ea84SWendy Cheng 
2684373ea84SWendy Cheng 	if (buf[size-1] != '\n')
2694373ea84SWendy Cheng 		return -EINVAL;
2704373ea84SWendy Cheng 
2714373ea84SWendy Cheng 	fo_path = buf;
2724373ea84SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
2734373ea84SWendy Cheng 		return -EINVAL;
2744373ea84SWendy Cheng 
27511f77942SStanislav Kinsbursky 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
2764373ea84SWendy Cheng 		return -EINVAL;
2774373ea84SWendy Cheng 
2784116092bSChuck Lever 	return nlmsvc_unlock_all_by_ip(sap);
2794373ea84SWendy Cheng }
2804373ea84SWendy Cheng 
281f2453978SChuck Lever /*
282262a0982SChuck Lever  * write_unlock_fs - Release all locks on a local file system
283262a0982SChuck Lever  *
284262a0982SChuck Lever  * Experimental.
285262a0982SChuck Lever  *
286262a0982SChuck Lever  * Input:
287262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing the
288262a0982SChuck Lever  *				absolute pathname of a local file system
289262a0982SChuck Lever  *			size:	length of C string in @buf
290262a0982SChuck Lever  * Output:
291262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
292262a0982SChuck Lever  *			returns one if one or more locks were not released
293262a0982SChuck Lever  *	On error:	return code is negative errno value
294262a0982SChuck Lever  */
295b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
29617efa372SWendy Cheng {
297a63bb996SAl Viro 	struct path path;
29817efa372SWendy Cheng 	char *fo_path;
29917efa372SWendy Cheng 	int error;
30017efa372SWendy Cheng 
30117efa372SWendy Cheng 	/* sanity check */
30217efa372SWendy Cheng 	if (size == 0)
30317efa372SWendy Cheng 		return -EINVAL;
30417efa372SWendy Cheng 
30517efa372SWendy Cheng 	if (buf[size-1] != '\n')
30617efa372SWendy Cheng 		return -EINVAL;
30717efa372SWendy Cheng 
30817efa372SWendy Cheng 	fo_path = buf;
30917efa372SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
31017efa372SWendy Cheng 		return -EINVAL;
31117efa372SWendy Cheng 
312a63bb996SAl Viro 	error = kern_path(fo_path, 0, &path);
31317efa372SWendy Cheng 	if (error)
31417efa372SWendy Cheng 		return error;
31517efa372SWendy Cheng 
316262a0982SChuck Lever 	/*
317262a0982SChuck Lever 	 * XXX: Needs better sanity checking.  Otherwise we could end up
318262a0982SChuck Lever 	 * releasing locks on the wrong file system.
319262a0982SChuck Lever 	 *
320262a0982SChuck Lever 	 * For example:
321262a0982SChuck Lever 	 * 1.  Does the path refer to a directory?
322262a0982SChuck Lever 	 * 2.  Is that directory a mount point, or
323262a0982SChuck Lever 	 * 3.  Is that directory the root of an exported file system?
324262a0982SChuck Lever 	 */
325d8c9584eSAl Viro 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
32617efa372SWendy Cheng 
327a63bb996SAl Viro 	path_put(&path);
32817efa372SWendy Cheng 	return error;
32917efa372SWendy Cheng }
33017efa372SWendy Cheng 
331f2453978SChuck Lever /*
332262a0982SChuck Lever  * write_filehandle - Get a variable-length NFS file handle by path
333262a0982SChuck Lever  *
334262a0982SChuck Lever  * On input, the buffer contains a '\n'-terminated C string comprised of
335262a0982SChuck Lever  * three alphanumeric words separated by whitespace.  The string may
336262a0982SChuck Lever  * contain escape sequences.
337262a0982SChuck Lever  *
338262a0982SChuck Lever  * Input:
339262a0982SChuck Lever  *			buf:
340262a0982SChuck Lever  *				domain:		client domain name
341262a0982SChuck Lever  *				path:		export pathname
342262a0982SChuck Lever  *				maxsize:	numeric maximum size of
343262a0982SChuck Lever  *						@buf
344262a0982SChuck Lever  *			size:	length of C string in @buf
345262a0982SChuck Lever  * Output:
346262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
347262a0982SChuck Lever  *			string containing a ASCII hex text version
348262a0982SChuck Lever  *			of the NFS file handle;
349262a0982SChuck Lever  *			return code is the size in bytes of the string
350262a0982SChuck Lever  *	On error:	return code is negative errno value
351262a0982SChuck Lever  */
3521da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
3531da177e4SLinus Torvalds {
3541da177e4SLinus Torvalds 	char *dname, *path;
3553f649ab7SKees Cook 	int maxsize;
3561da177e4SLinus Torvalds 	char *mesg = buf;
3571da177e4SLinus Torvalds 	int len;
3581da177e4SLinus Torvalds 	struct auth_domain *dom;
3591da177e4SLinus Torvalds 	struct knfsd_fh fh;
3601da177e4SLinus Torvalds 
36187d26ea7SJ. Bruce Fields 	if (size == 0)
36287d26ea7SJ. Bruce Fields 		return -EINVAL;
36387d26ea7SJ. Bruce Fields 
3641da177e4SLinus Torvalds 	if (buf[size-1] != '\n')
3651da177e4SLinus Torvalds 		return -EINVAL;
3661da177e4SLinus Torvalds 	buf[size-1] = 0;
3671da177e4SLinus Torvalds 
3681da177e4SLinus Torvalds 	dname = mesg;
3691da177e4SLinus Torvalds 	len = qword_get(&mesg, dname, size);
37054224f04SChuck Lever 	if (len <= 0)
37154224f04SChuck Lever 		return -EINVAL;
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	path = dname+len+1;
3741da177e4SLinus Torvalds 	len = qword_get(&mesg, path, size);
37554224f04SChuck Lever 	if (len <= 0)
37654224f04SChuck Lever 		return -EINVAL;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	len = get_int(&mesg, &maxsize);
3791da177e4SLinus Torvalds 	if (len)
3801da177e4SLinus Torvalds 		return len;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	if (maxsize < NFS_FHSIZE)
3831da177e4SLinus Torvalds 		return -EINVAL;
3843c7aa15dSKinglong Mee 	maxsize = min(maxsize, NFS3_FHSIZE);
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	if (qword_get(&mesg, mesg, size)>0)
3871da177e4SLinus Torvalds 		return -EINVAL;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	/* we have all the words, they are in buf.. */
3901da177e4SLinus Torvalds 	dom = unix_domain_find(dname);
3911da177e4SLinus Torvalds 	if (!dom)
3921da177e4SLinus Torvalds 		return -ENOMEM;
3931da177e4SLinus Torvalds 
394244c7d44SAl Viro 	len = exp_rootfh(netns(file), dom, path, &fh,  maxsize);
3951da177e4SLinus Torvalds 	auth_domain_put(dom);
3961da177e4SLinus Torvalds 	if (len)
3971da177e4SLinus Torvalds 		return len;
3981da177e4SLinus Torvalds 
39954224f04SChuck Lever 	mesg = buf;
40054224f04SChuck Lever 	len = SIMPLE_TRANSACTION_LIMIT;
401d8b26071SNeilBrown 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
4021da177e4SLinus Torvalds 	mesg[-1] = '\n';
4031da177e4SLinus Torvalds 	return mesg - buf;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
406f2453978SChuck Lever /*
407262a0982SChuck Lever  * write_threads - Start NFSD, or report the current number of running threads
408262a0982SChuck Lever  *
409262a0982SChuck Lever  * Input:
410262a0982SChuck Lever  *			buf:		ignored
411262a0982SChuck Lever  *			size:		zero
412262a0982SChuck Lever  * Output:
413262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
414262a0982SChuck Lever  *			string numeric value representing the number of
415262a0982SChuck Lever  *			running NFSD threads;
416262a0982SChuck Lever  *			return code is the size in bytes of the string
417262a0982SChuck Lever  *	On error:	return code is zero
418262a0982SChuck Lever  *
419262a0982SChuck Lever  * OR
420262a0982SChuck Lever  *
421262a0982SChuck Lever  * Input:
422262a0982SChuck Lever  *			buf:		C string containing an unsigned
423262a0982SChuck Lever  *					integer value representing the
424262a0982SChuck Lever  *					number of NFSD threads to start
425262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
426262a0982SChuck Lever  * Output:
427262a0982SChuck Lever  *	On success:	NFS service is started;
428262a0982SChuck Lever  *			passed-in buffer filled with '\n'-terminated C
429262a0982SChuck Lever  *			string numeric value representing the number of
430262a0982SChuck Lever  *			running NFSD threads;
431262a0982SChuck Lever  *			return code is the size in bytes of the string
432262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
433262a0982SChuck Lever  */
4341da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	char *mesg = buf;
4371da177e4SLinus Torvalds 	int rv;
438244c7d44SAl Viro 	struct net *net = netns(file);
439d41a9417SStanislav Kinsbursky 
4401da177e4SLinus Torvalds 	if (size > 0) {
4411da177e4SLinus Torvalds 		int newthreads;
4421da177e4SLinus Torvalds 		rv = get_int(&mesg, &newthreads);
4431da177e4SLinus Torvalds 		if (rv)
4441da177e4SLinus Torvalds 			return rv;
4451da177e4SLinus Torvalds 		if (newthreads < 0)
4461da177e4SLinus Torvalds 			return -EINVAL;
4474df493a2STrond Myklebust 		rv = nfsd_svc(newthreads, net, file->f_cred);
44882e12fe9SNeilBrown 		if (rv < 0)
4491da177e4SLinus Torvalds 			return rv;
45082e12fe9SNeilBrown 	} else
4519dd9845fSStanislav Kinsbursky 		rv = nfsd_nrthreads(net);
452e06b6405SChuck Lever 
45382e12fe9SNeilBrown 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
4541da177e4SLinus Torvalds }
4551da177e4SLinus Torvalds 
456f2453978SChuck Lever /*
457262a0982SChuck Lever  * write_pool_threads - Set or report the current number of threads per pool
458262a0982SChuck Lever  *
459262a0982SChuck Lever  * Input:
460262a0982SChuck Lever  *			buf:		ignored
461262a0982SChuck Lever  *			size:		zero
462262a0982SChuck Lever  *
463262a0982SChuck Lever  * OR
464262a0982SChuck Lever  *
465262a0982SChuck Lever  * Input:
466262a0982SChuck Lever  * 			buf:		C string containing whitespace-
467262a0982SChuck Lever  * 					separated unsigned integer values
468262a0982SChuck Lever  *					representing the number of NFSD
469262a0982SChuck Lever  *					threads to start in each pool
470262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
471262a0982SChuck Lever  * Output:
472262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
473262a0982SChuck Lever  *			string containing integer values representing the
474262a0982SChuck Lever  *			number of NFSD threads in each pool;
475262a0982SChuck Lever  *			return code is the size in bytes of the string
476262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
477262a0982SChuck Lever  */
478eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
479eed2965aSGreg Banks {
480eed2965aSGreg Banks 	/* if size > 0, look for an array of number of threads per node
481eed2965aSGreg Banks 	 * and apply them  then write out number of threads per node as reply
482eed2965aSGreg Banks 	 */
483eed2965aSGreg Banks 	char *mesg = buf;
484eed2965aSGreg Banks 	int i;
485eed2965aSGreg Banks 	int rv;
486eed2965aSGreg Banks 	int len;
487bedbdd8bSNeil Brown 	int npools;
488eed2965aSGreg Banks 	int *nthreads;
489244c7d44SAl Viro 	struct net *net = netns(file);
490eed2965aSGreg Banks 
491bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
4929dd9845fSStanislav Kinsbursky 	npools = nfsd_nrpools(net);
493eed2965aSGreg Banks 	if (npools == 0) {
494eed2965aSGreg Banks 		/*
495eed2965aSGreg Banks 		 * NFS is shut down.  The admin can start it by
496eed2965aSGreg Banks 		 * writing to the threads file but NOT the pool_threads
497eed2965aSGreg Banks 		 * file, sorry.  Report zero threads.
498eed2965aSGreg Banks 		 */
499bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
500eed2965aSGreg Banks 		strcpy(buf, "0\n");
501eed2965aSGreg Banks 		return strlen(buf);
502eed2965aSGreg Banks 	}
503eed2965aSGreg Banks 
504eed2965aSGreg Banks 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
505bedbdd8bSNeil Brown 	rv = -ENOMEM;
506eed2965aSGreg Banks 	if (nthreads == NULL)
507bedbdd8bSNeil Brown 		goto out_free;
508eed2965aSGreg Banks 
509eed2965aSGreg Banks 	if (size > 0) {
510eed2965aSGreg Banks 		for (i = 0; i < npools; i++) {
511eed2965aSGreg Banks 			rv = get_int(&mesg, &nthreads[i]);
512eed2965aSGreg Banks 			if (rv == -ENOENT)
513eed2965aSGreg Banks 				break;		/* fewer numbers than pools */
514eed2965aSGreg Banks 			if (rv)
515eed2965aSGreg Banks 				goto out_free;	/* syntax error */
516eed2965aSGreg Banks 			rv = -EINVAL;
517eed2965aSGreg Banks 			if (nthreads[i] < 0)
518eed2965aSGreg Banks 				goto out_free;
519eed2965aSGreg Banks 		}
5203938a0d5SStanislav Kinsbursky 		rv = nfsd_set_nrthreads(i, nthreads, net);
521eed2965aSGreg Banks 		if (rv)
522eed2965aSGreg Banks 			goto out_free;
523eed2965aSGreg Banks 	}
524eed2965aSGreg Banks 
5259dd9845fSStanislav Kinsbursky 	rv = nfsd_get_nrthreads(npools, nthreads, net);
526eed2965aSGreg Banks 	if (rv)
527eed2965aSGreg Banks 		goto out_free;
528eed2965aSGreg Banks 
529eed2965aSGreg Banks 	mesg = buf;
530eed2965aSGreg Banks 	size = SIMPLE_TRANSACTION_LIMIT;
531eed2965aSGreg Banks 	for (i = 0; i < npools && size > 0; i++) {
532eed2965aSGreg Banks 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
533eed2965aSGreg Banks 		len = strlen(mesg);
534eed2965aSGreg Banks 		size -= len;
535eed2965aSGreg Banks 		mesg += len;
536eed2965aSGreg Banks 	}
537413d63d7SJ. Bruce Fields 	rv = mesg - buf;
538eed2965aSGreg Banks out_free:
539eed2965aSGreg Banks 	kfree(nthreads);
540bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
541eed2965aSGreg Banks 	return rv;
542eed2965aSGreg Banks }
543eed2965aSGreg Banks 
544ff7d1179STrond Myklebust static ssize_t
545e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
546e333f3bbSTrond Myklebust 		const char *sep, unsigned vers, int minor)
547ff7d1179STrond Myklebust {
548abcb4dacSNeilBrown 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
549e333f3bbSTrond Myklebust 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
550ff7d1179STrond Myklebust 
551abcb4dacSNeilBrown 	if (vers == 4 && minor >= 0 &&
552e333f3bbSTrond Myklebust 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
553ff7d1179STrond Myklebust 		supported = false;
554abcb4dacSNeilBrown 	if (minor == 0 && supported)
555abcb4dacSNeilBrown 		/*
556abcb4dacSNeilBrown 		 * special case for backward compatability.
557abcb4dacSNeilBrown 		 * +4.0 is never reported, it is implied by
558abcb4dacSNeilBrown 		 * +4, unless -4.0 is present.
559abcb4dacSNeilBrown 		 */
560abcb4dacSNeilBrown 		return 0;
561ff7d1179STrond Myklebust 	return snprintf(buf, remaining, format, sep,
562ff7d1179STrond Myklebust 			supported ? '+' : '-', vers, minor);
563ff7d1179STrond Myklebust }
564ff7d1179STrond Myklebust 
5653dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size)
56670c3b76cSNeilBrown {
56770c3b76cSNeilBrown 	char *mesg = buf;
5688daf220aSBenny Halevy 	char *vers, *minorp, sign;
569261758b5SChuck Lever 	int len, num, remaining;
57070c3b76cSNeilBrown 	ssize_t tlen = 0;
57170c3b76cSNeilBrown 	char *sep;
572244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
57370c3b76cSNeilBrown 
57470c3b76cSNeilBrown 	if (size>0) {
5759dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
5766658d3a7SNeilBrown 			/* Cannot change versions without updating
5779dd9845fSStanislav Kinsbursky 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
5786658d3a7SNeilBrown 			 * rq_argp and rq_resp
5796658d3a7SNeilBrown 			 */
58070c3b76cSNeilBrown 			return -EBUSY;
58170c3b76cSNeilBrown 		if (buf[size-1] != '\n')
58270c3b76cSNeilBrown 			return -EINVAL;
58370c3b76cSNeilBrown 		buf[size-1] = 0;
58470c3b76cSNeilBrown 
58570c3b76cSNeilBrown 		vers = mesg;
58670c3b76cSNeilBrown 		len = qword_get(&mesg, vers, size);
58770c3b76cSNeilBrown 		if (len <= 0) return -EINVAL;
58870c3b76cSNeilBrown 		do {
589d3635ff0STrond Myklebust 			enum vers_op cmd;
590abcb4dacSNeilBrown 			unsigned minor;
59170c3b76cSNeilBrown 			sign = *vers;
59270c3b76cSNeilBrown 			if (sign == '+' || sign == '-')
5938daf220aSBenny Halevy 				num = simple_strtol((vers+1), &minorp, 0);
59470c3b76cSNeilBrown 			else
5958daf220aSBenny Halevy 				num = simple_strtol(vers, &minorp, 0);
5968daf220aSBenny Halevy 			if (*minorp == '.') {
597ff89be87SJ. Bruce Fields 				if (num != 4)
5988daf220aSBenny Halevy 					return -EINVAL;
599e35659f1SNeilBrown 				if (kstrtouint(minorp+1, 0, &minor) < 0)
6008daf220aSBenny Halevy 					return -EINVAL;
601abcb4dacSNeilBrown 			}
602abcb4dacSNeilBrown 
603d3635ff0STrond Myklebust 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
60470c3b76cSNeilBrown 			switch(num) {
60570c3b76cSNeilBrown 			case 2:
60670c3b76cSNeilBrown 			case 3:
607e333f3bbSTrond Myklebust 				nfsd_vers(nn, num, cmd);
608d3635ff0STrond Myklebust 				break;
60970c3b76cSNeilBrown 			case 4:
610abcb4dacSNeilBrown 				if (*minorp == '.') {
611e333f3bbSTrond Myklebust 					if (nfsd_minorversion(nn, minor, cmd) < 0)
612abcb4dacSNeilBrown 						return -EINVAL;
613e333f3bbSTrond Myklebust 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
614abcb4dacSNeilBrown 					/*
615abcb4dacSNeilBrown 					 * Either we have +4 and no minors are enabled,
616abcb4dacSNeilBrown 					 * or we have -4 and at least one minor is enabled.
617abcb4dacSNeilBrown 					 * In either case, propagate 'cmd' to all minors.
618abcb4dacSNeilBrown 					 */
619abcb4dacSNeilBrown 					minor = 0;
620e333f3bbSTrond Myklebust 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
621abcb4dacSNeilBrown 						minor++;
622abcb4dacSNeilBrown 				}
62370c3b76cSNeilBrown 				break;
62470c3b76cSNeilBrown 			default:
62570c3b76cSNeilBrown 				return -EINVAL;
62670c3b76cSNeilBrown 			}
62770c3b76cSNeilBrown 			vers += len + 1;
62870c3b76cSNeilBrown 		} while ((len = qword_get(&mesg, vers, size)) > 0);
62970c3b76cSNeilBrown 		/* If all get turned off, turn them back on, as
63070c3b76cSNeilBrown 		 * having no versions is BAD
63170c3b76cSNeilBrown 		 */
632e333f3bbSTrond Myklebust 		nfsd_reset_versions(nn);
63370c3b76cSNeilBrown 	}
634261758b5SChuck Lever 
63570c3b76cSNeilBrown 	/* Now write current state into reply buffer */
63670c3b76cSNeilBrown 	len = 0;
63770c3b76cSNeilBrown 	sep = "";
638261758b5SChuck Lever 	remaining = SIMPLE_TRANSACTION_LIMIT;
639ff7d1179STrond Myklebust 	for (num=2 ; num <= 4 ; num++) {
640abcb4dacSNeilBrown 		int minor;
641e333f3bbSTrond Myklebust 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
642e35659f1SNeilBrown 			continue;
643abcb4dacSNeilBrown 
644abcb4dacSNeilBrown 		minor = -1;
645ff7d1179STrond Myklebust 		do {
646e333f3bbSTrond Myklebust 			len = nfsd_print_version_support(nn, buf, remaining,
647ff7d1179STrond Myklebust 					sep, num, minor);
648818f2f57SDan Carpenter 			if (len >= remaining)
649ff7d1179STrond Myklebust 				goto out;
650261758b5SChuck Lever 			remaining -= len;
651261758b5SChuck Lever 			buf += len;
652261758b5SChuck Lever 			tlen += len;
653ff7d1179STrond Myklebust 			minor++;
654abcb4dacSNeilBrown 			if (len)
655ff7d1179STrond Myklebust 				sep = " ";
656ff7d1179STrond Myklebust 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
657261758b5SChuck Lever 	}
658ff7d1179STrond Myklebust out:
659261758b5SChuck Lever 	len = snprintf(buf, remaining, "\n");
660818f2f57SDan Carpenter 	if (len >= remaining)
661261758b5SChuck Lever 		return -EINVAL;
662261758b5SChuck Lever 	return tlen + len;
66370c3b76cSNeilBrown }
66470c3b76cSNeilBrown 
665f2453978SChuck Lever /*
666262a0982SChuck Lever  * write_versions - Set or report the available NFS protocol versions
667262a0982SChuck Lever  *
668262a0982SChuck Lever  * Input:
669262a0982SChuck Lever  *			buf:		ignored
670262a0982SChuck Lever  *			size:		zero
671262a0982SChuck Lever  * Output:
672262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
673262a0982SChuck Lever  *			string containing positive or negative integer
674262a0982SChuck Lever  *			values representing the current status of each
675262a0982SChuck Lever  *			protocol version;
676262a0982SChuck Lever  *			return code is the size in bytes of the string
677262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
678262a0982SChuck Lever  *
679262a0982SChuck Lever  * OR
680262a0982SChuck Lever  *
681262a0982SChuck Lever  * Input:
682262a0982SChuck Lever  * 			buf:		C string containing whitespace-
683262a0982SChuck Lever  * 					separated positive or negative
684262a0982SChuck Lever  * 					integer values representing NFS
685262a0982SChuck Lever  * 					protocol versions to enable ("+n")
686262a0982SChuck Lever  * 					or disable ("-n")
687262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
688262a0982SChuck Lever  * Output:
689262a0982SChuck Lever  *	On success:	status of zero or more protocol versions has
690262a0982SChuck Lever  *			been updated; passed-in buffer filled with
691262a0982SChuck Lever  *			'\n'-terminated C string containing positive
692262a0982SChuck Lever  *			or negative integer values representing the
693262a0982SChuck Lever  *			current status of each protocol version;
694262a0982SChuck Lever  *			return code is the size in bytes of the string
695262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
696262a0982SChuck Lever  */
6973dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size)
6983dd98a3bSJeff Layton {
6993dd98a3bSJeff Layton 	ssize_t rv;
7003dd98a3bSJeff Layton 
7013dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
7023dd98a3bSJeff Layton 	rv = __write_versions(file, buf, size);
7033dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
7043dd98a3bSJeff Layton 	return rv;
7053dd98a3bSJeff Layton }
7063dd98a3bSJeff Layton 
7074cd5dc75SChuck Lever /*
7080a5372d8SChuck Lever  * Zero-length write.  Return a list of NFSD's current listener
7090a5372d8SChuck Lever  * transports.
7100a5372d8SChuck Lever  */
7119dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net)
7120a5372d8SChuck Lever {
7139dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7149dd9845fSStanislav Kinsbursky 
7159dd9845fSStanislav Kinsbursky 	if (nn->nfsd_serv == NULL)
7160a5372d8SChuck Lever 		return 0;
7179dd9845fSStanislav Kinsbursky 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
7180a5372d8SChuck Lever }
7190a5372d8SChuck Lever 
7200a5372d8SChuck Lever /*
7210b7c2f6fSChuck Lever  * A single 'fd' number was written, in which case it must be for
7220b7c2f6fSChuck Lever  * a socket of a supported family/protocol, and we use it as an
7230b7c2f6fSChuck Lever  * nfsd listener.
7240b7c2f6fSChuck Lever  */
7254df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
7260b7c2f6fSChuck Lever {
7270b7c2f6fSChuck Lever 	char *mesg = buf;
7280b7c2f6fSChuck Lever 	int fd, err;
7299dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7300b7c2f6fSChuck Lever 
7310b7c2f6fSChuck Lever 	err = get_int(&mesg, &fd);
7320b7c2f6fSChuck Lever 	if (err != 0 || fd < 0)
7330b7c2f6fSChuck Lever 		return -EINVAL;
7340b7c2f6fSChuck Lever 
73530646394SStanislav Kinsbursky 	if (svc_alien_sock(net, fd)) {
73630646394SStanislav Kinsbursky 		printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
73730646394SStanislav Kinsbursky 		return -EINVAL;
73830646394SStanislav Kinsbursky 	}
73930646394SStanislav Kinsbursky 
7406777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7410b7c2f6fSChuck Lever 	if (err != 0)
7420b7c2f6fSChuck Lever 		return err;
7430b7c2f6fSChuck Lever 
7444df493a2STrond Myklebust 	err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
745ea068badSChuck Lever 
746ec52361dSNeilBrown 	if (err >= 0 &&
747ec52361dSNeilBrown 	    !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
748ec52361dSNeilBrown 		svc_get(nn->nfsd_serv);
749ec52361dSNeilBrown 
750ec52361dSNeilBrown 	nfsd_put(net);
751ea068badSChuck Lever 	return err;
7520b7c2f6fSChuck Lever }
7530b7c2f6fSChuck Lever 
7540b7c2f6fSChuck Lever /*
7554eb68c26SChuck Lever  * A transport listener is added by writing it's transport name and
7564eb68c26SChuck Lever  * a port number.
7574eb68c26SChuck Lever  */
7584df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
7594eb68c26SChuck Lever {
7604eb68c26SChuck Lever 	char transport[16];
76137498292SChuck Lever 	struct svc_xprt *xprt;
7624eb68c26SChuck Lever 	int port, err;
7639dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7644eb68c26SChuck Lever 
765a10fded1SJ. Bruce Fields 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
7664eb68c26SChuck Lever 		return -EINVAL;
7674eb68c26SChuck Lever 
7684be929beSAlexey Dobriyan 	if (port < 1 || port > USHRT_MAX)
7694eb68c26SChuck Lever 		return -EINVAL;
7704eb68c26SChuck Lever 
7716777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7724eb68c26SChuck Lever 	if (err != 0)
7734eb68c26SChuck Lever 		return err;
7744eb68c26SChuck Lever 
7759dd9845fSStanislav Kinsbursky 	err = svc_create_xprt(nn->nfsd_serv, transport, net,
7764df493a2STrond Myklebust 				PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
77768717908SChuck Lever 	if (err < 0)
77837498292SChuck Lever 		goto out_err;
77937498292SChuck Lever 
7809dd9845fSStanislav Kinsbursky 	err = svc_create_xprt(nn->nfsd_serv, transport, net,
7814df493a2STrond Myklebust 				PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
78237498292SChuck Lever 	if (err < 0 && err != -EAFNOSUPPORT)
78337498292SChuck Lever 		goto out_close;
7840cd14a06SJeff Layton 
785ec52361dSNeilBrown 	if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
786ec52361dSNeilBrown 		svc_get(nn->nfsd_serv);
787ec52361dSNeilBrown 
788ec52361dSNeilBrown 	nfsd_put(net);
7894eb68c26SChuck Lever 	return 0;
79037498292SChuck Lever out_close:
7919dd9845fSStanislav Kinsbursky 	xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
79237498292SChuck Lever 	if (xprt != NULL) {
79337498292SChuck Lever 		svc_close_xprt(xprt);
79437498292SChuck Lever 		svc_xprt_put(xprt);
79537498292SChuck Lever 	}
79637498292SChuck Lever out_err:
7978c62d127SNeilBrown 	nfsd_put(net);
79837498292SChuck Lever 	return err;
7994eb68c26SChuck Lever }
8004eb68c26SChuck Lever 
80108160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size,
80208160352SStanislav Kinsbursky 			     struct net *net)
80380212d59SNeilBrown {
8040a5372d8SChuck Lever 	if (size == 0)
8059dd9845fSStanislav Kinsbursky 		return __write_ports_names(buf, net);
8060b7c2f6fSChuck Lever 
8070b7c2f6fSChuck Lever 	if (isdigit(buf[0]))
8084df493a2STrond Myklebust 		return __write_ports_addfd(buf, net, file->f_cred);
80982d56591SChuck Lever 
8104eb68c26SChuck Lever 	if (isalpha(buf[0]))
8114df493a2STrond Myklebust 		return __write_ports_addxprt(buf, net, file->f_cred);
8124cd5dc75SChuck Lever 
813b41b66d6SNeilBrown 	return -EINVAL;
814b41b66d6SNeilBrown }
81580212d59SNeilBrown 
816f2453978SChuck Lever /*
817262a0982SChuck Lever  * write_ports - Pass a socket file descriptor or transport name to listen on
818262a0982SChuck Lever  *
819262a0982SChuck Lever  * Input:
820262a0982SChuck Lever  *			buf:		ignored
821262a0982SChuck Lever  *			size:		zero
822262a0982SChuck Lever  * Output:
823262a0982SChuck Lever  *	On success:	passed-in buffer filled with a '\n'-terminated C
824262a0982SChuck Lever  *			string containing a whitespace-separated list of
825262a0982SChuck Lever  *			named NFSD listeners;
826262a0982SChuck Lever  *			return code is the size in bytes of the string
827262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
828262a0982SChuck Lever  *
829262a0982SChuck Lever  * OR
830262a0982SChuck Lever  *
831262a0982SChuck Lever  * Input:
832262a0982SChuck Lever  *			buf:		C string containing an unsigned
833262a0982SChuck Lever  *					integer value representing a bound
834262a0982SChuck Lever  *					but unconnected socket that is to be
835c71206a7SChuck Lever  *					used as an NFSD listener; listen(3)
836c71206a7SChuck Lever  *					must be called for a SOCK_STREAM
837c71206a7SChuck Lever  *					socket, otherwise it is ignored
838262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
839262a0982SChuck Lever  * Output:
840262a0982SChuck Lever  *	On success:	NFS service is started;
841262a0982SChuck Lever  *			passed-in buffer filled with a '\n'-terminated C
842262a0982SChuck Lever  *			string containing a unique alphanumeric name of
843262a0982SChuck Lever  *			the listener;
844262a0982SChuck Lever  *			return code is the size in bytes of the string
845262a0982SChuck Lever  *	On error:	return code is a negative errno value
846262a0982SChuck Lever  *
847262a0982SChuck Lever  * OR
848262a0982SChuck Lever  *
849262a0982SChuck Lever  * Input:
850262a0982SChuck Lever  *			buf:		C string containing a transport
851262a0982SChuck Lever  *					name and an unsigned integer value
852262a0982SChuck Lever  *					representing the port to listen on,
853262a0982SChuck Lever  *					separated by whitespace
854262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
855262a0982SChuck Lever  * Output:
856262a0982SChuck Lever  *	On success:	returns zero; NFS service is started
857262a0982SChuck Lever  *	On error:	return code is a negative errno value
858262a0982SChuck Lever  */
859bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size)
860bedbdd8bSNeil Brown {
861bedbdd8bSNeil Brown 	ssize_t rv;
8623dd98a3bSJeff Layton 
863bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
864244c7d44SAl Viro 	rv = __write_ports(file, buf, size, netns(file));
865bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
866bedbdd8bSNeil Brown 	return rv;
867bedbdd8bSNeil Brown }
868bedbdd8bSNeil Brown 
869bedbdd8bSNeil Brown 
870596bbe53SNeilBrown int nfsd_max_blksize;
871596bbe53SNeilBrown 
872f2453978SChuck Lever /*
873262a0982SChuck Lever  * write_maxblksize - Set or report the current NFS blksize
874262a0982SChuck Lever  *
875262a0982SChuck Lever  * Input:
876262a0982SChuck Lever  *			buf:		ignored
877262a0982SChuck Lever  *			size:		zero
878262a0982SChuck Lever  *
879262a0982SChuck Lever  * OR
880262a0982SChuck Lever  *
881262a0982SChuck Lever  * Input:
882262a0982SChuck Lever  * 			buf:		C string containing an unsigned
883262a0982SChuck Lever  * 					integer value representing the new
884262a0982SChuck Lever  * 					NFS blksize
885262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
886262a0982SChuck Lever  * Output:
887262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
888262a0982SChuck Lever  *			containing numeric value of the current NFS blksize
889262a0982SChuck Lever  *			setting;
890262a0982SChuck Lever  *			return code is the size in bytes of the string
891262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
892262a0982SChuck Lever  */
893596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
894596bbe53SNeilBrown {
895596bbe53SNeilBrown 	char *mesg = buf;
896244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
8979dd9845fSStanislav Kinsbursky 
898596bbe53SNeilBrown 	if (size > 0) {
899596bbe53SNeilBrown 		int bsize;
900596bbe53SNeilBrown 		int rv = get_int(&mesg, &bsize);
901596bbe53SNeilBrown 		if (rv)
902596bbe53SNeilBrown 			return rv;
903596bbe53SNeilBrown 		/* force bsize into allowed range and
904596bbe53SNeilBrown 		 * required alignment.
905596bbe53SNeilBrown 		 */
9063c7aa15dSKinglong Mee 		bsize = max_t(int, bsize, 1024);
9073c7aa15dSKinglong Mee 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
908596bbe53SNeilBrown 		bsize &= ~(1024-1);
909bedbdd8bSNeil Brown 		mutex_lock(&nfsd_mutex);
9109dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv) {
911bedbdd8bSNeil Brown 			mutex_unlock(&nfsd_mutex);
912596bbe53SNeilBrown 			return -EBUSY;
913596bbe53SNeilBrown 		}
914596bbe53SNeilBrown 		nfsd_max_blksize = bsize;
915bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
916596bbe53SNeilBrown 	}
917e06b6405SChuck Lever 
918e06b6405SChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
919e06b6405SChuck Lever 							nfsd_max_blksize);
920596bbe53SNeilBrown }
921596bbe53SNeilBrown 
922f2453978SChuck Lever /*
9235b8db00bSJeff Layton  * write_maxconn - Set or report the current max number of connections
9245b8db00bSJeff Layton  *
9255b8db00bSJeff Layton  * Input:
9265b8db00bSJeff Layton  *			buf:		ignored
9275b8db00bSJeff Layton  *			size:		zero
9285b8db00bSJeff Layton  * OR
9295b8db00bSJeff Layton  *
9305b8db00bSJeff Layton  * Input:
9315b8db00bSJeff Layton  * 			buf:		C string containing an unsigned
9325b8db00bSJeff Layton  * 					integer value representing the new
9335b8db00bSJeff Layton  * 					number of max connections
9345b8db00bSJeff Layton  *			size:		non-zero length of C string in @buf
9355b8db00bSJeff Layton  * Output:
9365b8db00bSJeff Layton  *	On success:	passed-in buffer filled with '\n'-terminated C string
9375b8db00bSJeff Layton  *			containing numeric value of max_connections setting
9385b8db00bSJeff Layton  *			for this net namespace;
9395b8db00bSJeff Layton  *			return code is the size in bytes of the string
9405b8db00bSJeff Layton  *	On error:	return code is zero or a negative errno value
9415b8db00bSJeff Layton  */
9425b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
9435b8db00bSJeff Layton {
9445b8db00bSJeff Layton 	char *mesg = buf;
945244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9465b8db00bSJeff Layton 	unsigned int maxconn = nn->max_connections;
9475b8db00bSJeff Layton 
9485b8db00bSJeff Layton 	if (size > 0) {
9495b8db00bSJeff Layton 		int rv = get_uint(&mesg, &maxconn);
9505b8db00bSJeff Layton 
9515b8db00bSJeff Layton 		if (rv)
9525b8db00bSJeff Layton 			return rv;
9535b8db00bSJeff Layton 		nn->max_connections = maxconn;
9545b8db00bSJeff Layton 	}
9555b8db00bSJeff Layton 
9565b8db00bSJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
9575b8db00bSJeff Layton }
9585b8db00bSJeff Layton 
95970c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
9609dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
96120b7d86fSArnd Bergmann 				  time64_t *time, struct nfsd_net *nn)
9621da177e4SLinus Torvalds {
9631da177e4SLinus Torvalds 	char *mesg = buf;
964f0135740SJ. Bruce Fields 	int rv, i;
9651da177e4SLinus Torvalds 
9661da177e4SLinus Torvalds 	if (size > 0) {
9679dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
9683dd98a3bSJeff Layton 			return -EBUSY;
969f0135740SJ. Bruce Fields 		rv = get_int(&mesg, &i);
9701da177e4SLinus Torvalds 		if (rv)
9711da177e4SLinus Torvalds 			return rv;
972e7b184f1SJ. Bruce Fields 		/*
973e7b184f1SJ. Bruce Fields 		 * Some sanity checking.  We don't have a reason for
974e7b184f1SJ. Bruce Fields 		 * these particular numbers, but problems with the
975e7b184f1SJ. Bruce Fields 		 * extremes are:
976e7b184f1SJ. Bruce Fields 		 *	- Too short: the briefest network outage may
977e7b184f1SJ. Bruce Fields 		 *	  cause clients to lose all their locks.  Also,
978e7b184f1SJ. Bruce Fields 		 *	  the frequent polling may be wasteful.
979e7b184f1SJ. Bruce Fields 		 *	- Too long: do you really want reboot recovery
980e7b184f1SJ. Bruce Fields 		 *	  to take more than an hour?  Or to make other
981e7b184f1SJ. Bruce Fields 		 *	  clients wait an hour before being able to
982e7b184f1SJ. Bruce Fields 		 *	  revoke a dead client's locks?
983e7b184f1SJ. Bruce Fields 		 */
984f0135740SJ. Bruce Fields 		if (i < 10 || i > 3600)
9851da177e4SLinus Torvalds 			return -EINVAL;
986f0135740SJ. Bruce Fields 		*time = i;
9871da177e4SLinus Torvalds 	}
988e06b6405SChuck Lever 
98920b7d86fSArnd Bergmann 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
990f0135740SJ. Bruce Fields }
991f0135740SJ. Bruce Fields 
9929dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
99320b7d86fSArnd Bergmann 				time64_t *time, struct nfsd_net *nn)
994f0135740SJ. Bruce Fields {
995f0135740SJ. Bruce Fields 	ssize_t rv;
996f0135740SJ. Bruce Fields 
997f0135740SJ. Bruce Fields 	mutex_lock(&nfsd_mutex);
9989dd9845fSStanislav Kinsbursky 	rv = __nfsd4_write_time(file, buf, size, time, nn);
999f0135740SJ. Bruce Fields 	mutex_unlock(&nfsd_mutex);
1000f0135740SJ. Bruce Fields 	return rv;
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
1003f2453978SChuck Lever /*
1004262a0982SChuck Lever  * write_leasetime - Set or report the current NFSv4 lease time
1005262a0982SChuck Lever  *
1006262a0982SChuck Lever  * Input:
1007262a0982SChuck Lever  *			buf:		ignored
1008262a0982SChuck Lever  *			size:		zero
1009262a0982SChuck Lever  *
1010262a0982SChuck Lever  * OR
1011262a0982SChuck Lever  *
1012262a0982SChuck Lever  * Input:
1013262a0982SChuck Lever  *			buf:		C string containing an unsigned
1014262a0982SChuck Lever  *					integer value representing the new
1015262a0982SChuck Lever  *					NFSv4 lease expiry time
1016262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1017262a0982SChuck Lever  * Output:
1018262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
1019262a0982SChuck Lever  *			string containing unsigned integer value of the
1020262a0982SChuck Lever  *			current lease expiry time;
1021262a0982SChuck Lever  *			return code is the size in bytes of the string
1022262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1023262a0982SChuck Lever  */
10243dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
10253dd98a3bSJeff Layton {
1026244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10279dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
10283dd98a3bSJeff Layton }
10293dd98a3bSJeff Layton 
1030f2453978SChuck Lever /*
1031efc4bb4fSJ. Bruce Fields  * write_gracetime - Set or report current NFSv4 grace period time
1032efc4bb4fSJ. Bruce Fields  *
1033efc4bb4fSJ. Bruce Fields  * As above, but sets the time of the NFSv4 grace period.
1034efc4bb4fSJ. Bruce Fields  *
1035efc4bb4fSJ. Bruce Fields  * Note this should never be set to less than the *previous*
1036efc4bb4fSJ. Bruce Fields  * lease-period time, but we don't try to enforce this.  (In the common
1037efc4bb4fSJ. Bruce Fields  * case (a new boot), we don't know what the previous lease time was
1038efc4bb4fSJ. Bruce Fields  * anyway.)
1039efc4bb4fSJ. Bruce Fields  */
1040efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1041efc4bb4fSJ. Bruce Fields {
1042244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10439dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1044efc4bb4fSJ. Bruce Fields }
1045efc4bb4fSJ. Bruce Fields 
10469dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
10479dd9845fSStanislav Kinsbursky 				   struct nfsd_net *nn)
10480964a3d3SNeilBrown {
10490964a3d3SNeilBrown 	char *mesg = buf;
10500964a3d3SNeilBrown 	char *recdir;
10510964a3d3SNeilBrown 	int len, status;
10520964a3d3SNeilBrown 
10533dd98a3bSJeff Layton 	if (size > 0) {
10549dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
10553dd98a3bSJeff Layton 			return -EBUSY;
10563dd98a3bSJeff Layton 		if (size > PATH_MAX || buf[size-1] != '\n')
10570964a3d3SNeilBrown 			return -EINVAL;
10580964a3d3SNeilBrown 		buf[size-1] = 0;
10590964a3d3SNeilBrown 
10600964a3d3SNeilBrown 		recdir = mesg;
10610964a3d3SNeilBrown 		len = qword_get(&mesg, recdir, size);
10620964a3d3SNeilBrown 		if (len <= 0)
10630964a3d3SNeilBrown 			return -EINVAL;
10640964a3d3SNeilBrown 
10650964a3d3SNeilBrown 		status = nfs4_reset_recoverydir(recdir);
106669049961SAndi Kleen 		if (status)
106769049961SAndi Kleen 			return status;
10683dd98a3bSJeff Layton 	}
10693d72ab8fSChuck Lever 
10703d72ab8fSChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
10713d72ab8fSChuck Lever 							nfs4_recoverydir());
10720964a3d3SNeilBrown }
10733dd98a3bSJeff Layton 
1074f2453978SChuck Lever /*
1075262a0982SChuck Lever  * write_recoverydir - Set or report the pathname of the recovery directory
1076262a0982SChuck Lever  *
1077262a0982SChuck Lever  * Input:
1078262a0982SChuck Lever  *			buf:		ignored
1079262a0982SChuck Lever  *			size:		zero
1080262a0982SChuck Lever  *
1081262a0982SChuck Lever  * OR
1082262a0982SChuck Lever  *
1083262a0982SChuck Lever  * Input:
1084262a0982SChuck Lever  *			buf:		C string containing the pathname
1085262a0982SChuck Lever  *					of the directory on a local file
1086262a0982SChuck Lever  *					system containing permanent NFSv4
1087262a0982SChuck Lever  *					recovery data
1088262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1089262a0982SChuck Lever  * Output:
1090262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
1091262a0982SChuck Lever  *			containing the current recovery pathname setting;
1092262a0982SChuck Lever  *			return code is the size in bytes of the string
1093262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1094262a0982SChuck Lever  */
10953dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
10963dd98a3bSJeff Layton {
10973dd98a3bSJeff Layton 	ssize_t rv;
1098244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10993dd98a3bSJeff Layton 
11003dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
11019dd9845fSStanislav Kinsbursky 	rv = __write_recoverydir(file, buf, size, nn);
11023dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
11033dd98a3bSJeff Layton 	return rv;
11043dd98a3bSJeff Layton }
11053dd98a3bSJeff Layton 
1106f2453978SChuck Lever /*
11077f5ef2e9SJeff Layton  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
11087f5ef2e9SJeff Layton  *
11097f5ef2e9SJeff Layton  * Input:
11107f5ef2e9SJeff Layton  *			buf:		ignored
11117f5ef2e9SJeff Layton  *			size:		zero
11127f5ef2e9SJeff Layton  * OR
11137f5ef2e9SJeff Layton  *
11147f5ef2e9SJeff Layton  * Input:
11157f5ef2e9SJeff Layton  * 			buf:		any value
11167f5ef2e9SJeff Layton  *			size:		non-zero length of C string in @buf
11177f5ef2e9SJeff Layton  * Output:
11187f5ef2e9SJeff Layton  *			passed-in buffer filled with "Y" or "N" with a newline
11197f5ef2e9SJeff Layton  *			and NULL-terminated C string. This indicates whether
11207f5ef2e9SJeff Layton  *			the grace period has ended in the current net
11217f5ef2e9SJeff Layton  *			namespace. Return code is the size in bytes of the
11227f5ef2e9SJeff Layton  *			string. Writing a string that starts with 'Y', 'y', or
11237f5ef2e9SJeff Layton  *			'1' to the file will end the grace period for nfsd's v4
11247f5ef2e9SJeff Layton  *			lock manager.
11257f5ef2e9SJeff Layton  */
11267f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
11277f5ef2e9SJeff Layton {
1128244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
11297f5ef2e9SJeff Layton 
11307f5ef2e9SJeff Layton 	if (size > 0) {
11317f5ef2e9SJeff Layton 		switch(buf[0]) {
11327f5ef2e9SJeff Layton 		case 'Y':
11337f5ef2e9SJeff Layton 		case 'y':
11347f5ef2e9SJeff Layton 		case '1':
1135dd838821SYihao Wu 			if (!nn->nfsd_serv)
113662a063b8SJ. Bruce Fields 				return -EBUSY;
11377f5ef2e9SJeff Layton 			nfsd4_end_grace(nn);
11387f5ef2e9SJeff Layton 			break;
11397f5ef2e9SJeff Layton 		default:
11407f5ef2e9SJeff Layton 			return -EINVAL;
11417f5ef2e9SJeff Layton 		}
11427f5ef2e9SJeff Layton 	}
11437f5ef2e9SJeff Layton 
11447f5ef2e9SJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
11457f5ef2e9SJeff Layton 			 nn->grace_ended ? 'Y' : 'N');
11467f5ef2e9SJeff Layton }
11477f5ef2e9SJeff Layton 
114870c3b76cSNeilBrown #endif
11490964a3d3SNeilBrown 
11501da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
11511da177e4SLinus Torvalds /*
11521da177e4SLinus Torvalds  *	populating the filesystem.
11531da177e4SLinus Torvalds  */
11541da177e4SLinus Torvalds 
1155e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */
1156e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1157e8a79fb1SJ. Bruce Fields {
1158e8a79fb1SJ. Bruce Fields 	struct inode *inode = new_inode(sb);
1159e8a79fb1SJ. Bruce Fields 	if (!inode)
1160e8a79fb1SJ. Bruce Fields 		return NULL;
1161e8a79fb1SJ. Bruce Fields 	/* Following advice from simple_fill_super documentation: */
1162e8a79fb1SJ. Bruce Fields 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1163e8a79fb1SJ. Bruce Fields 	inode->i_mode = mode;
1164e8a79fb1SJ. Bruce Fields 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1165e8a79fb1SJ. Bruce Fields 	switch (mode & S_IFMT) {
1166e8a79fb1SJ. Bruce Fields 	case S_IFDIR:
1167e8a79fb1SJ. Bruce Fields 		inode->i_fop = &simple_dir_operations;
1168e8a79fb1SJ. Bruce Fields 		inode->i_op = &simple_dir_inode_operations;
1169e8a79fb1SJ. Bruce Fields 		inc_nlink(inode);
117076c50eb7SGustavo A. R. Silva 		break;
1171e8a79fb1SJ. Bruce Fields 	default:
1172e8a79fb1SJ. Bruce Fields 		break;
1173e8a79fb1SJ. Bruce Fields 	}
1174e8a79fb1SJ. Bruce Fields 	return inode;
1175e8a79fb1SJ. Bruce Fields }
1176e8a79fb1SJ. Bruce Fields 
1177bebd6997SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1178e8a79fb1SJ. Bruce Fields {
1179e8a79fb1SJ. Bruce Fields 	struct inode *inode;
1180e8a79fb1SJ. Bruce Fields 
1181e8a79fb1SJ. Bruce Fields 	inode = nfsd_get_inode(dir->i_sb, mode);
1182e8a79fb1SJ. Bruce Fields 	if (!inode)
1183e8a79fb1SJ. Bruce Fields 		return -ENOMEM;
1184bebd6997SJ. Bruce Fields 	if (ncl) {
1185bebd6997SJ. Bruce Fields 		inode->i_private = ncl;
1186bebd6997SJ. Bruce Fields 		kref_get(&ncl->cl_ref);
1187bebd6997SJ. Bruce Fields 	}
1188e8a79fb1SJ. Bruce Fields 	d_add(dentry, inode);
1189e8a79fb1SJ. Bruce Fields 	inc_nlink(dir);
1190e8a79fb1SJ. Bruce Fields 	fsnotify_mkdir(dir, dentry);
1191e8a79fb1SJ. Bruce Fields 	return 0;
1192e8a79fb1SJ. Bruce Fields }
1193e8a79fb1SJ. Bruce Fields 
1194e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1195e8a79fb1SJ. Bruce Fields {
1196e8a79fb1SJ. Bruce Fields 	struct inode *dir = parent->d_inode;
1197e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1198e8a79fb1SJ. Bruce Fields 	int ret = -ENOMEM;
1199e8a79fb1SJ. Bruce Fields 
1200e8a79fb1SJ. Bruce Fields 	inode_lock(dir);
1201e8a79fb1SJ. Bruce Fields 	dentry = d_alloc_name(parent, name);
1202e8a79fb1SJ. Bruce Fields 	if (!dentry)
1203e8a79fb1SJ. Bruce Fields 		goto out_err;
1204bebd6997SJ. Bruce Fields 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1205e8a79fb1SJ. Bruce Fields 	if (ret)
1206e8a79fb1SJ. Bruce Fields 		goto out_err;
1207e8a79fb1SJ. Bruce Fields out:
1208e8a79fb1SJ. Bruce Fields 	inode_unlock(dir);
1209e8a79fb1SJ. Bruce Fields 	return dentry;
1210e8a79fb1SJ. Bruce Fields out_err:
1211d6846bfbSTetsuo Handa 	dput(dentry);
1212e8a79fb1SJ. Bruce Fields 	dentry = ERR_PTR(ret);
1213e8a79fb1SJ. Bruce Fields 	goto out;
1214e8a79fb1SJ. Bruce Fields }
1215e8a79fb1SJ. Bruce Fields 
121697ad4031SJ. Bruce Fields static void clear_ncl(struct inode *inode)
1217e8a79fb1SJ. Bruce Fields {
121897ad4031SJ. Bruce Fields 	struct nfsdfs_client *ncl = inode->i_private;
121997ad4031SJ. Bruce Fields 
122097ad4031SJ. Bruce Fields 	inode->i_private = NULL;
122197ad4031SJ. Bruce Fields 	kref_put(&ncl->cl_ref, ncl->cl_release);
122297ad4031SJ. Bruce Fields }
122397ad4031SJ. Bruce Fields 
1224b78fa45dSYueHaibing static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
122597ad4031SJ. Bruce Fields {
122697ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc = inode->i_private;
122797ad4031SJ. Bruce Fields 
122897ad4031SJ. Bruce Fields 	if (nc)
122997ad4031SJ. Bruce Fields 		kref_get(&nc->cl_ref);
123097ad4031SJ. Bruce Fields 	return nc;
123197ad4031SJ. Bruce Fields }
123297ad4031SJ. Bruce Fields 
123397ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
123497ad4031SJ. Bruce Fields {
123597ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc;
123697ad4031SJ. Bruce Fields 
1237dc46bba7SJ. Bruce Fields 	inode_lock_shared(inode);
123897ad4031SJ. Bruce Fields 	nc = __get_nfsdfs_client(inode);
1239dc46bba7SJ. Bruce Fields 	inode_unlock_shared(inode);
124097ad4031SJ. Bruce Fields 	return nc;
124197ad4031SJ. Bruce Fields }
124297ad4031SJ. Bruce Fields /* from __rpc_unlink */
124397ad4031SJ. Bruce Fields static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
124497ad4031SJ. Bruce Fields {
124597ad4031SJ. Bruce Fields 	int ret;
124697ad4031SJ. Bruce Fields 
124797ad4031SJ. Bruce Fields 	clear_ncl(d_inode(dentry));
124897ad4031SJ. Bruce Fields 	dget(dentry);
124997ad4031SJ. Bruce Fields 	ret = simple_unlink(dir, dentry);
1250*29044daeSAmir Goldstein 	d_drop(dentry);
1251*29044daeSAmir Goldstein 	fsnotify_unlink(dir, dentry);
125297ad4031SJ. Bruce Fields 	dput(dentry);
125397ad4031SJ. Bruce Fields 	WARN_ON_ONCE(ret);
125497ad4031SJ. Bruce Fields }
125597ad4031SJ. Bruce Fields 
125697ad4031SJ. Bruce Fields static void nfsdfs_remove_files(struct dentry *root)
125797ad4031SJ. Bruce Fields {
125897ad4031SJ. Bruce Fields 	struct dentry *dentry, *tmp;
125997ad4031SJ. Bruce Fields 
126097ad4031SJ. Bruce Fields 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
126197ad4031SJ. Bruce Fields 		if (!simple_positive(dentry)) {
126297ad4031SJ. Bruce Fields 			WARN_ON_ONCE(1); /* I think this can't happen? */
126397ad4031SJ. Bruce Fields 			continue;
126497ad4031SJ. Bruce Fields 		}
126597ad4031SJ. Bruce Fields 		nfsdfs_remove_file(d_inode(root), dentry);
126697ad4031SJ. Bruce Fields 	}
126797ad4031SJ. Bruce Fields }
126897ad4031SJ. Bruce Fields 
126997ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
127097ad4031SJ. Bruce Fields  * code instead. */
127197ad4031SJ. Bruce Fields static  int nfsdfs_create_files(struct dentry *root,
1272472d155aSNeilBrown 				const struct tree_descr *files,
1273472d155aSNeilBrown 				struct dentry **fdentries)
127497ad4031SJ. Bruce Fields {
127597ad4031SJ. Bruce Fields 	struct inode *dir = d_inode(root);
127697ad4031SJ. Bruce Fields 	struct inode *inode;
127797ad4031SJ. Bruce Fields 	struct dentry *dentry;
127897ad4031SJ. Bruce Fields 	int i;
127997ad4031SJ. Bruce Fields 
128097ad4031SJ. Bruce Fields 	inode_lock(dir);
128197ad4031SJ. Bruce Fields 	for (i = 0; files->name && files->name[0]; i++, files++) {
128297ad4031SJ. Bruce Fields 		dentry = d_alloc_name(root, files->name);
128397ad4031SJ. Bruce Fields 		if (!dentry)
128497ad4031SJ. Bruce Fields 			goto out;
128597ad4031SJ. Bruce Fields 		inode = nfsd_get_inode(d_inode(root)->i_sb,
128697ad4031SJ. Bruce Fields 					S_IFREG | files->mode);
128797ad4031SJ. Bruce Fields 		if (!inode) {
128897ad4031SJ. Bruce Fields 			dput(dentry);
128997ad4031SJ. Bruce Fields 			goto out;
129097ad4031SJ. Bruce Fields 		}
129197ad4031SJ. Bruce Fields 		inode->i_fop = files->ops;
129297ad4031SJ. Bruce Fields 		inode->i_private = __get_nfsdfs_client(dir);
129397ad4031SJ. Bruce Fields 		d_add(dentry, inode);
129497ad4031SJ. Bruce Fields 		fsnotify_create(dir, dentry);
1295472d155aSNeilBrown 		if (fdentries)
1296472d155aSNeilBrown 			fdentries[i] = dentry;
129797ad4031SJ. Bruce Fields 	}
129897ad4031SJ. Bruce Fields 	inode_unlock(dir);
129997ad4031SJ. Bruce Fields 	return 0;
130097ad4031SJ. Bruce Fields out:
130197ad4031SJ. Bruce Fields 	nfsdfs_remove_files(root);
130297ad4031SJ. Bruce Fields 	inode_unlock(dir);
130397ad4031SJ. Bruce Fields 	return -ENOMEM;
130497ad4031SJ. Bruce Fields }
130597ad4031SJ. Bruce Fields 
130697ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */
130797ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
130897ad4031SJ. Bruce Fields 				 struct nfsdfs_client *ncl, u32 id,
1309472d155aSNeilBrown 				 const struct tree_descr *files,
1310472d155aSNeilBrown 				 struct dentry **fdentries)
131197ad4031SJ. Bruce Fields {
131297ad4031SJ. Bruce Fields 	struct dentry *dentry;
1313e8a79fb1SJ. Bruce Fields 	char name[11];
131497ad4031SJ. Bruce Fields 	int ret;
1315e8a79fb1SJ. Bruce Fields 
1316bf5ed3e3SJ. Bruce Fields 	sprintf(name, "%u", id);
1317e8a79fb1SJ. Bruce Fields 
131897ad4031SJ. Bruce Fields 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
131997ad4031SJ. Bruce Fields 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
132097ad4031SJ. Bruce Fields 		return NULL;
1321472d155aSNeilBrown 	ret = nfsdfs_create_files(dentry, files, fdentries);
132297ad4031SJ. Bruce Fields 	if (ret) {
132397ad4031SJ. Bruce Fields 		nfsd_client_rmdir(dentry);
132497ad4031SJ. Bruce Fields 		return NULL;
132597ad4031SJ. Bruce Fields 	}
132697ad4031SJ. Bruce Fields 	return dentry;
1327e8a79fb1SJ. Bruce Fields }
1328e8a79fb1SJ. Bruce Fields 
1329e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */
1330e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry)
1331e8a79fb1SJ. Bruce Fields {
1332e8a79fb1SJ. Bruce Fields 	struct inode *dir = d_inode(dentry->d_parent);
1333e8a79fb1SJ. Bruce Fields 	struct inode *inode = d_inode(dentry);
1334e8a79fb1SJ. Bruce Fields 	int ret;
1335e8a79fb1SJ. Bruce Fields 
133697ad4031SJ. Bruce Fields 	inode_lock(dir);
133797ad4031SJ. Bruce Fields 	nfsdfs_remove_files(dentry);
133897ad4031SJ. Bruce Fields 	clear_ncl(inode);
1339e8a79fb1SJ. Bruce Fields 	dget(dentry);
1340e8a79fb1SJ. Bruce Fields 	ret = simple_rmdir(dir, dentry);
1341e8a79fb1SJ. Bruce Fields 	WARN_ON_ONCE(ret);
1342*29044daeSAmir Goldstein 	d_drop(dentry);
134369afd267SJ. Bruce Fields 	fsnotify_rmdir(dir, dentry);
1344bf265401SJ. Bruce Fields 	dput(dentry);
134597ad4031SJ. Bruce Fields 	inode_unlock(dir);
1346e8a79fb1SJ. Bruce Fields }
1347e8a79fb1SJ. Bruce Fields 
134896a374a3SDavid Howells static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
13491da177e4SLinus Torvalds {
1350e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1351e8a79fb1SJ. Bruce Fields 							nfsd_net_id);
1352e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1353e8a79fb1SJ. Bruce Fields 	int ret;
1354e8a79fb1SJ. Bruce Fields 
1355cda37124SEric Biggers 	static const struct tree_descr nfsd_files[] = {
135696d851c4SStanislav Kinsbursky 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
135720ad856eSAmir Goldstein 		/* Per-export io stats use same ops as exports file */
135820ad856eSAmir Goldstein 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1359e8e8753fSJ. Bruce Fields 		[NFSD_Export_features] = {"export_features",
1360e8e8753fSJ. Bruce Fields 					&export_features_operations, S_IRUGO},
13614373ea84SWendy Cheng 		[NFSD_FO_UnlockIP] = {"unlock_ip",
13624373ea84SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
136317efa372SWendy Cheng 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
136417efa372SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
13651da177e4SLinus Torvalds 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
13661da177e4SLinus Torvalds 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1367eed2965aSGreg Banks 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
136803cf6c9fSGreg Banks 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1369a2f999a3SJeff Layton 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
137070c3b76cSNeilBrown 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
137180212d59SNeilBrown 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1372596bbe53SNeilBrown 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
13735b8db00bSJeff Layton 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1374b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
1375b0b0c0a2SKevin Coffman 		[NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
1376b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
13771da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4
13781da177e4SLinus Torvalds 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1379efc4bb4fSJ. Bruce Fields 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
13800964a3d3SNeilBrown 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
13817f5ef2e9SJeff Layton 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
13821da177e4SLinus Torvalds #endif
13831da177e4SLinus Torvalds 		/* last one */ {""}
13841da177e4SLinus Torvalds 	};
138596a374a3SDavid Howells 
1386e8a79fb1SJ. Bruce Fields 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1387e8a79fb1SJ. Bruce Fields 	if (ret)
1388e8a79fb1SJ. Bruce Fields 		return ret;
1389e8a79fb1SJ. Bruce Fields 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1390e8a79fb1SJ. Bruce Fields 	if (IS_ERR(dentry))
1391e8a79fb1SJ. Bruce Fields 		return PTR_ERR(dentry);
1392e8a79fb1SJ. Bruce Fields 	nn->nfsd_client_dir = dentry;
1393e8a79fb1SJ. Bruce Fields 	return 0;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds 
139696a374a3SDavid Howells static int nfsd_fs_get_tree(struct fs_context *fc)
13971da177e4SLinus Torvalds {
1398533770ccSAl Viro 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
139996a374a3SDavid Howells }
140096a374a3SDavid Howells 
140196a374a3SDavid Howells static void nfsd_fs_free_fc(struct fs_context *fc)
140296a374a3SDavid Howells {
140396a374a3SDavid Howells 	if (fc->s_fs_info)
140496a374a3SDavid Howells 		put_net(fc->s_fs_info);
140596a374a3SDavid Howells }
140696a374a3SDavid Howells 
140796a374a3SDavid Howells static const struct fs_context_operations nfsd_fs_context_ops = {
140896a374a3SDavid Howells 	.free		= nfsd_fs_free_fc,
140996a374a3SDavid Howells 	.get_tree	= nfsd_fs_get_tree,
141096a374a3SDavid Howells };
141196a374a3SDavid Howells 
141296a374a3SDavid Howells static int nfsd_init_fs_context(struct fs_context *fc)
141396a374a3SDavid Howells {
141496a374a3SDavid Howells 	put_user_ns(fc->user_ns);
141596a374a3SDavid Howells 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
141696a374a3SDavid Howells 	fc->ops = &nfsd_fs_context_ops;
141796a374a3SDavid Howells 	return 0;
141811f77942SStanislav Kinsbursky }
141911f77942SStanislav Kinsbursky 
142011f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb)
142111f77942SStanislav Kinsbursky {
142211f77942SStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
142311f77942SStanislav Kinsbursky 
1424c6c7f2a8STrond Myklebust 	nfsd_shutdown_threads(net);
1425c6c7f2a8STrond Myklebust 
142611f77942SStanislav Kinsbursky 	kill_litter_super(sb);
142711f77942SStanislav Kinsbursky 	put_net(net);
14281da177e4SLinus Torvalds }
14291da177e4SLinus Torvalds 
14301da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = {
14311da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
14321da177e4SLinus Torvalds 	.name		= "nfsd",
143396a374a3SDavid Howells 	.init_fs_context = nfsd_init_fs_context,
143411f77942SStanislav Kinsbursky 	.kill_sb	= nfsd_umount,
14351da177e4SLinus Torvalds };
14367f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd");
14371da177e4SLinus Torvalds 
1438e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS
1439e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1440e331f606SJ. Bruce Fields {
1441e331f606SJ. Bruce Fields 	struct proc_dir_entry *entry;
1442e331f606SJ. Bruce Fields 
1443e331f606SJ. Bruce Fields 	entry = proc_mkdir("fs/nfs", NULL);
1444e331f606SJ. Bruce Fields 	if (!entry)
1445e331f606SJ. Bruce Fields 		return -ENOMEM;
144697a32539SAlexey Dobriyan 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1447ff7c4b36Sfanchaoting 	if (!entry) {
1448ff7c4b36Sfanchaoting 		remove_proc_entry("fs/nfs", NULL);
1449e331f606SJ. Bruce Fields 		return -ENOMEM;
1450ff7c4b36Sfanchaoting 	}
1451e331f606SJ. Bruce Fields 	return 0;
1452e331f606SJ. Bruce Fields }
1453e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */
1454e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1455e331f606SJ. Bruce Fields {
1456e331f606SJ. Bruce Fields 	return 0;
1457e331f606SJ. Bruce Fields }
1458e331f606SJ. Bruce Fields #endif
1459e331f606SJ. Bruce Fields 
1460c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id;
14615717e012SStanislav Kinsbursky 
14625717e012SStanislav Kinsbursky static __net_init int nfsd_init_net(struct net *net)
14635717e012SStanislav Kinsbursky {
14645717e012SStanislav Kinsbursky 	int retval;
14653d733711SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14665717e012SStanislav Kinsbursky 
14675717e012SStanislav Kinsbursky 	retval = nfsd_export_init(net);
14685717e012SStanislav Kinsbursky 	if (retval)
14695717e012SStanislav Kinsbursky 		goto out_export_error;
1470f69adb2fSStanislav Kinsbursky 	retval = nfsd_idmap_init(net);
1471f69adb2fSStanislav Kinsbursky 	if (retval)
1472f69adb2fSStanislav Kinsbursky 		goto out_idmap_error;
1473e333f3bbSTrond Myklebust 	nn->nfsd_versions = NULL;
1474e333f3bbSTrond Myklebust 	nn->nfsd4_minorversions = NULL;
14753ba75830SJ. Bruce Fields 	retval = nfsd_reply_cache_init(nn);
14763ba75830SJ. Bruce Fields 	if (retval)
14773ba75830SJ. Bruce Fields 		goto out_drc_error;
14783bf6b57eSJ. Bruce Fields 	nn->nfsd4_lease = 90;	/* default lease time */
14793bf6b57eSJ. Bruce Fields 	nn->nfsd4_grace = 90;
148003f318caSJ. Bruce Fields 	nn->somebody_reclaimed = false;
1481362063a5SScott Mayhew 	nn->track_reclaim_completes = false;
1482ebd7c72cSJ. Bruce Fields 	nn->clverifier_counter = prandom_u32();
1483bf5ed3e3SJ. Bruce Fields 	nn->clientid_base = prandom_u32();
1484bf5ed3e3SJ. Bruce Fields 	nn->clientid_counter = nn->clientid_base + 1;
1485e0639dc5SOlga Kornievskaia 	nn->s2s_cp_cl_id = nn->clientid_counter++;
14862317dc55SVasily Averin 
148791d2e9b5SChuck Lever 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
148891d2e9b5SChuck Lever 	seqlock_init(&nn->writeverf_lock);
14892c830dd7SJ. Bruce Fields 
14905717e012SStanislav Kinsbursky 	return 0;
14915717e012SStanislav Kinsbursky 
14923ba75830SJ. Bruce Fields out_drc_error:
14933ba75830SJ. Bruce Fields 	nfsd_idmap_shutdown(net);
1494f69adb2fSStanislav Kinsbursky out_idmap_error:
1495f69adb2fSStanislav Kinsbursky 	nfsd_export_shutdown(net);
14965717e012SStanislav Kinsbursky out_export_error:
14975717e012SStanislav Kinsbursky 	return retval;
14985717e012SStanislav Kinsbursky }
14995717e012SStanislav Kinsbursky 
15005717e012SStanislav Kinsbursky static __net_exit void nfsd_exit_net(struct net *net)
15015717e012SStanislav Kinsbursky {
15023ba75830SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
15033ba75830SJ. Bruce Fields 
15043ba75830SJ. Bruce Fields 	nfsd_reply_cache_shutdown(nn);
1505f69adb2fSStanislav Kinsbursky 	nfsd_idmap_shutdown(net);
15065717e012SStanislav Kinsbursky 	nfsd_export_shutdown(net);
1507e333f3bbSTrond Myklebust 	nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
15085717e012SStanislav Kinsbursky }
15095717e012SStanislav Kinsbursky 
15107ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = {
15115717e012SStanislav Kinsbursky 	.init = nfsd_init_net,
15125717e012SStanislav Kinsbursky 	.exit = nfsd_exit_net,
15137ea34ac1SJeff Layton 	.id   = &nfsd_net_id,
15147ea34ac1SJeff Layton 	.size = sizeof(struct nfsd_net),
15157ea34ac1SJeff Layton };
15167ea34ac1SJeff Layton 
15171da177e4SLinus Torvalds static int __init init_nfsd(void)
15181da177e4SLinus Torvalds {
15191da177e4SLinus Torvalds 	int retval;
15201da177e4SLinus Torvalds 	printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
15211da177e4SLinus Torvalds 
1522bb7ffbf2SGiuseppe Cantavenera 	retval = nfsd4_init_slabs();
1523bb7ffbf2SGiuseppe Cantavenera 	if (retval)
1524b10252c7SAlexander Sverdlin 		return retval;
15259cf514ccSChristoph Hellwig 	retval = nfsd4_init_pnfs();
152665178db4SBryan Schumaker 	if (retval)
152765178db4SBryan Schumaker 		goto out_free_slabs;
1528e567b98cSAmir Goldstein 	retval = nfsd_stat_init();	/* Statistics */
1529e567b98cSAmir Goldstein 	if (retval)
1530e567b98cSAmir Goldstein 		goto out_free_pnfs;
1531027690c7SJ. Bruce Fields 	retval = nfsd_drc_slab_create();
1532027690c7SJ. Bruce Fields 	if (retval)
1533027690c7SJ. Bruce Fields 		goto out_free_stat;
15341da177e4SLinus Torvalds 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1535e331f606SJ. Bruce Fields 	retval = create_proc_exports_entry();
1536e331f606SJ. Bruce Fields 	if (retval)
1537f69adb2fSStanislav Kinsbursky 		goto out_free_lockd;
15381da177e4SLinus Torvalds 	retval = register_filesystem(&nfsd_fs_type);
153926808d3fSJ. Bruce Fields 	if (retval)
1540bd5ae928SJ. Bruce Fields 		goto out_free_exports;
1541bd5ae928SJ. Bruce Fields 	retval = register_pernet_subsys(&nfsd_net_ops);
1542bd5ae928SJ. Bruce Fields 	if (retval < 0)
1543b10252c7SAlexander Sverdlin 		goto out_free_filesystem;
1544b10252c7SAlexander Sverdlin 	retval = register_cld_notifier();
1545b10252c7SAlexander Sverdlin 	if (retval)
154626808d3fSJ. Bruce Fields 		goto out_free_all;
154726808d3fSJ. Bruce Fields 	return 0;
154826808d3fSJ. Bruce Fields out_free_all:
1549b10252c7SAlexander Sverdlin 	unregister_pernet_subsys(&nfsd_net_ops);
1550b10252c7SAlexander Sverdlin out_free_filesystem:
15511d625050SPatrick Ho 	unregister_filesystem(&nfsd_fs_type);
1552bd5ae928SJ. Bruce Fields out_free_exports:
15531da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15541da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
1555dbf847ecSJ. Bruce Fields out_free_lockd:
15561da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1557027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
1558027690c7SJ. Bruce Fields out_free_stat:
1559d5c3428bSJ. Bruce Fields 	nfsd_stat_shutdown();
1560e567b98cSAmir Goldstein out_free_pnfs:
15619cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
156265178db4SBryan Schumaker out_free_slabs:
156346b25895SJ. Bruce Fields 	nfsd4_free_slabs();
15641da177e4SLinus Torvalds 	return retval;
15651da177e4SLinus Torvalds }
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds static void __exit exit_nfsd(void)
15681da177e4SLinus Torvalds {
1569b10252c7SAlexander Sverdlin 	unregister_cld_notifier();
1570bd5ae928SJ. Bruce Fields 	unregister_pernet_subsys(&nfsd_net_ops);
1571027690c7SJ. Bruce Fields 	nfsd_drc_slab_free();
15721da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15731da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
15741da177e4SLinus Torvalds 	nfsd_stat_shutdown();
15751da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1576e8ff2a84SJ. Bruce Fields 	nfsd4_free_slabs();
15779cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
15781da177e4SLinus Torvalds 	unregister_filesystem(&nfsd_fs_type);
15791da177e4SLinus Torvalds }
15801da177e4SLinus Torvalds 
15811da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
15821da177e4SLinus Torvalds MODULE_LICENSE("GPL");
15831da177e4SLinus Torvalds module_init(init_nfsd)
15841da177e4SLinus Torvalds module_exit(exit_nfsd)
1585