xref: /openbmc/linux/fs/nfsd/nfsctl.c (revision b78fa45d)
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>
111da177e4SLinus Torvalds 
1280212d59SNeilBrown #include <linux/sunrpc/svcsock.h>
134373ea84SWendy Cheng #include <linux/lockd/lockd.h>
145976687aSJeff Layton #include <linux/sunrpc/addr.h>
15b0b0c0a2SKevin Coffman #include <linux/sunrpc/gss_api.h>
16b084f598SJ. Bruce Fields #include <linux/sunrpc/gss_krb5_enctypes.h>
17813fd320SJeff Layton #include <linux/sunrpc/rpc_pipe_fs.h>
18143cb494SPaul Gortmaker #include <linux/module.h>
19e8a79fb1SJ. Bruce Fields #include <linux/fsnotify.h>
201da177e4SLinus Torvalds 
212ca72e17SJ. Bruce Fields #include "idmap.h"
229a74af21SBoaz Harrosh #include "nfsd.h"
239a74af21SBoaz Harrosh #include "cache.h"
24f3c7521fSBryan Schumaker #include "state.h"
257ea34ac1SJeff Layton #include "netns.h"
269cf514ccSChristoph Hellwig #include "pnfs.h"
279a74af21SBoaz Harrosh 
281da177e4SLinus Torvalds /*
29b0b0c0a2SKevin Coffman  *	We have a single directory with several nodes in it.
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds enum {
321da177e4SLinus Torvalds 	NFSD_Root = 1,
331da177e4SLinus Torvalds 	NFSD_List,
34e8e8753fSJ. Bruce Fields 	NFSD_Export_features,
351da177e4SLinus Torvalds 	NFSD_Fh,
364373ea84SWendy Cheng 	NFSD_FO_UnlockIP,
3717efa372SWendy Cheng 	NFSD_FO_UnlockFS,
381da177e4SLinus Torvalds 	NFSD_Threads,
39eed2965aSGreg Banks 	NFSD_Pool_Threads,
4003cf6c9fSGreg Banks 	NFSD_Pool_Stats,
41a2f999a3SJeff Layton 	NFSD_Reply_Cache_Stats,
4270c3b76cSNeilBrown 	NFSD_Versions,
4380212d59SNeilBrown 	NFSD_Ports,
44596bbe53SNeilBrown 	NFSD_MaxBlkSize,
455b8db00bSJeff Layton 	NFSD_MaxConnections,
46b0b0c0a2SKevin Coffman 	NFSD_SupportedEnctypes,
4770c3b76cSNeilBrown 	/*
4870c3b76cSNeilBrown 	 * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
4970c3b76cSNeilBrown 	 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
5070c3b76cSNeilBrown 	 */
5170c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
521da177e4SLinus Torvalds 	NFSD_Leasetime,
53efc4bb4fSJ. Bruce Fields 	NFSD_Gracetime,
540964a3d3SNeilBrown 	NFSD_RecoveryDir,
557f5ef2e9SJeff Layton 	NFSD_V4EndGrace,
5670c3b76cSNeilBrown #endif
57e8a79fb1SJ. Bruce Fields 	NFSD_MaxReserved
581da177e4SLinus Torvalds };
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds  * write() for these nodes.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
64b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
65b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
661da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size);
67eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
6870c3b76cSNeilBrown static ssize_t write_versions(struct file *file, char *buf, size_t size);
6980212d59SNeilBrown static ssize_t write_ports(struct file *file, char *buf, size_t size);
70596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
715b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
7270c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
731da177e4SLinus Torvalds static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
74efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
750964a3d3SNeilBrown static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
767f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
7770c3b76cSNeilBrown #endif
781da177e4SLinus Torvalds 
79c2cdc2abSEric Biggers static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
801da177e4SLinus Torvalds 	[NFSD_Fh] = write_filehandle,
81b046ccdcSChuck Lever 	[NFSD_FO_UnlockIP] = write_unlock_ip,
82b046ccdcSChuck Lever 	[NFSD_FO_UnlockFS] = write_unlock_fs,
831da177e4SLinus Torvalds 	[NFSD_Threads] = write_threads,
84eed2965aSGreg Banks 	[NFSD_Pool_Threads] = write_pool_threads,
8570c3b76cSNeilBrown 	[NFSD_Versions] = write_versions,
8680212d59SNeilBrown 	[NFSD_Ports] = write_ports,
87596bbe53SNeilBrown 	[NFSD_MaxBlkSize] = write_maxblksize,
885b8db00bSJeff Layton 	[NFSD_MaxConnections] = write_maxconn,
8970c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
901da177e4SLinus Torvalds 	[NFSD_Leasetime] = write_leasetime,
91efc4bb4fSJ. Bruce Fields 	[NFSD_Gracetime] = write_gracetime,
920964a3d3SNeilBrown 	[NFSD_RecoveryDir] = write_recoverydir,
937f5ef2e9SJeff Layton 	[NFSD_V4EndGrace] = write_v4_end_grace,
9470c3b76cSNeilBrown #endif
951da177e4SLinus Torvalds };
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
981da177e4SLinus Torvalds {
99496ad9aaSAl Viro 	ino_t ino =  file_inode(file)->i_ino;
1001da177e4SLinus Torvalds 	char *data;
1011da177e4SLinus Torvalds 	ssize_t rv;
1021da177e4SLinus Torvalds 
103e8c96f8cSTobias Klauser 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1041da177e4SLinus Torvalds 		return -EINVAL;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	data = simple_transaction_get(file, buf, size);
1071da177e4SLinus Torvalds 	if (IS_ERR(data))
1081da177e4SLinus Torvalds 		return PTR_ERR(data);
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds 	rv =  write_op[ino](file, data, size);
1118971a101SNeilBrown 	if (rv >= 0) {
1121da177e4SLinus Torvalds 		simple_transaction_set(file, rv);
1131da177e4SLinus Torvalds 		rv = size;
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 	return rv;
1161da177e4SLinus Torvalds }
1171da177e4SLinus Torvalds 
1187390022dSNeilBrown static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1197390022dSNeilBrown {
1207390022dSNeilBrown 	if (! file->private_data) {
1217390022dSNeilBrown 		/* An attempt to read a transaction file without writing
1227390022dSNeilBrown 		 * causes a 0-byte write so that the file can return
1237390022dSNeilBrown 		 * state information
1247390022dSNeilBrown 		 */
1257390022dSNeilBrown 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
1267390022dSNeilBrown 		if (rv < 0)
1277390022dSNeilBrown 			return rv;
1287390022dSNeilBrown 	}
1297390022dSNeilBrown 	return simple_transaction_read(file, buf, size, pos);
1307390022dSNeilBrown }
1317390022dSNeilBrown 
1324b6f5d20SArjan van de Ven static const struct file_operations transaction_ops = {
1331da177e4SLinus Torvalds 	.write		= nfsctl_transaction_write,
1347390022dSNeilBrown 	.read		= nfsctl_transaction_read,
1351da177e4SLinus Torvalds 	.release	= simple_transaction_release,
1366038f373SArnd Bergmann 	.llseek		= default_llseek,
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
13996d851c4SStanislav Kinsbursky static int exports_net_open(struct net *net, struct file *file)
1401da177e4SLinus Torvalds {
141f2c7ea10SStanislav Kinsbursky 	int err;
142f2c7ea10SStanislav Kinsbursky 	struct seq_file *seq;
14396d851c4SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
144f2c7ea10SStanislav Kinsbursky 
145f2c7ea10SStanislav Kinsbursky 	err = seq_open(file, &nfs_exports_op);
146f2c7ea10SStanislav Kinsbursky 	if (err)
147f2c7ea10SStanislav Kinsbursky 		return err;
148f2c7ea10SStanislav Kinsbursky 
149f2c7ea10SStanislav Kinsbursky 	seq = file->private_data;
150e5f06f72SStanislav Kinsbursky 	seq->private = nn->svc_export_cache;
151f2c7ea10SStanislav Kinsbursky 	return 0;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
15496d851c4SStanislav Kinsbursky static int exports_proc_open(struct inode *inode, struct file *file)
15596d851c4SStanislav Kinsbursky {
15696d851c4SStanislav Kinsbursky 	return exports_net_open(current->nsproxy->net_ns, file);
15796d851c4SStanislav Kinsbursky }
15896d851c4SStanislav Kinsbursky 
15996d851c4SStanislav Kinsbursky static const struct file_operations exports_proc_operations = {
16096d851c4SStanislav Kinsbursky 	.open		= exports_proc_open,
16196d851c4SStanislav Kinsbursky 	.read		= seq_read,
16296d851c4SStanislav Kinsbursky 	.llseek		= seq_lseek,
16396d851c4SStanislav Kinsbursky 	.release	= seq_release,
16496d851c4SStanislav Kinsbursky };
16596d851c4SStanislav Kinsbursky 
16696d851c4SStanislav Kinsbursky static int exports_nfsd_open(struct inode *inode, struct file *file)
16796d851c4SStanislav Kinsbursky {
16896d851c4SStanislav Kinsbursky 	return exports_net_open(inode->i_sb->s_fs_info, file);
16996d851c4SStanislav Kinsbursky }
17096d851c4SStanislav Kinsbursky 
17196d851c4SStanislav Kinsbursky static const struct file_operations exports_nfsd_operations = {
17296d851c4SStanislav Kinsbursky 	.open		= exports_nfsd_open,
1731da177e4SLinus Torvalds 	.read		= seq_read,
1741da177e4SLinus Torvalds 	.llseek		= seq_lseek,
1751da177e4SLinus Torvalds 	.release	= seq_release,
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
178e8e8753fSJ. Bruce Fields static int export_features_show(struct seq_file *m, void *v)
179e8e8753fSJ. Bruce Fields {
180e8e8753fSJ. Bruce Fields 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
181e8e8753fSJ. Bruce Fields 	return 0;
182e8e8753fSJ. Bruce Fields }
183e8e8753fSJ. Bruce Fields 
184e8e8753fSJ. Bruce Fields static int export_features_open(struct inode *inode, struct file *file)
185e8e8753fSJ. Bruce Fields {
186e8e8753fSJ. Bruce Fields 	return single_open(file, export_features_show, NULL);
187e8e8753fSJ. Bruce Fields }
188e8e8753fSJ. Bruce Fields 
18975ef9de1SAl Viro static const struct file_operations export_features_operations = {
190e8e8753fSJ. Bruce Fields 	.open		= export_features_open,
191e8e8753fSJ. Bruce Fields 	.read		= seq_read,
192e8e8753fSJ. Bruce Fields 	.llseek		= seq_lseek,
193e8e8753fSJ. Bruce Fields 	.release	= single_release,
194e8e8753fSJ. Bruce Fields };
195e8e8753fSJ. Bruce Fields 
196b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
197b0b0c0a2SKevin Coffman static int supported_enctypes_show(struct seq_file *m, void *v)
198b0b0c0a2SKevin Coffman {
199b084f598SJ. Bruce Fields 	seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
200b0b0c0a2SKevin Coffman 	return 0;
201b0b0c0a2SKevin Coffman }
202b0b0c0a2SKevin Coffman 
203b0b0c0a2SKevin Coffman static int supported_enctypes_open(struct inode *inode, struct file *file)
204b0b0c0a2SKevin Coffman {
205b0b0c0a2SKevin Coffman 	return single_open(file, supported_enctypes_show, NULL);
206b0b0c0a2SKevin Coffman }
207b0b0c0a2SKevin Coffman 
20875ef9de1SAl Viro static const struct file_operations supported_enctypes_ops = {
209b0b0c0a2SKevin Coffman 	.open		= supported_enctypes_open,
210b0b0c0a2SKevin Coffman 	.read		= seq_read,
211b0b0c0a2SKevin Coffman 	.llseek		= seq_lseek,
212b0b0c0a2SKevin Coffman 	.release	= single_release,
213b0b0c0a2SKevin Coffman };
214b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
215b0b0c0a2SKevin Coffman 
216828c0950SAlexey Dobriyan static const struct file_operations pool_stats_operations = {
21703cf6c9fSGreg Banks 	.open		= nfsd_pool_stats_open,
21803cf6c9fSGreg Banks 	.read		= seq_read,
21903cf6c9fSGreg Banks 	.llseek		= seq_lseek,
220ed2d8aedSRyusei Yamaguchi 	.release	= nfsd_pool_stats_release,
22103cf6c9fSGreg Banks };
22203cf6c9fSGreg Banks 
2237ba630f5SJulia Lawall static const struct file_operations reply_cache_stats_operations = {
224a2f999a3SJeff Layton 	.open		= nfsd_reply_cache_stats_open,
225a2f999a3SJeff Layton 	.read		= seq_read,
226a2f999a3SJeff Layton 	.llseek		= seq_lseek,
227a2f999a3SJeff Layton 	.release	= single_release,
228a2f999a3SJeff Layton };
229a2f999a3SJeff Layton 
2301da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
2311da177e4SLinus Torvalds /*
2321da177e4SLinus Torvalds  * payload - write methods
2331da177e4SLinus Torvalds  */
2341da177e4SLinus Torvalds 
235244c7d44SAl Viro static inline struct net *netns(struct file *file)
236244c7d44SAl Viro {
237244c7d44SAl Viro 	return file_inode(file)->i_sb->s_fs_info;
238244c7d44SAl Viro }
2391da177e4SLinus Torvalds 
240262a0982SChuck Lever /**
241262a0982SChuck Lever  * write_unlock_ip - Release all locks used by a client
242262a0982SChuck Lever  *
243262a0982SChuck Lever  * Experimental.
244262a0982SChuck Lever  *
245262a0982SChuck Lever  * Input:
246262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing a
2474116092bSChuck Lever  *				presentation format IP address
248262a0982SChuck Lever  *			size:	length of C string in @buf
249262a0982SChuck Lever  * Output:
250262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
251262a0982SChuck Lever  *			returns one if one or more locks were not released
252262a0982SChuck Lever  *	On error:	return code is negative errno value
253262a0982SChuck Lever  */
254b046ccdcSChuck Lever static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
2554373ea84SWendy Cheng {
2564116092bSChuck Lever 	struct sockaddr_storage address;
2574116092bSChuck Lever 	struct sockaddr *sap = (struct sockaddr *)&address;
2584116092bSChuck Lever 	size_t salen = sizeof(address);
259367c8c7bSChuck Lever 	char *fo_path;
260244c7d44SAl Viro 	struct net *net = netns(file);
2614373ea84SWendy Cheng 
2624373ea84SWendy Cheng 	/* sanity check */
2634373ea84SWendy Cheng 	if (size == 0)
2644373ea84SWendy Cheng 		return -EINVAL;
2654373ea84SWendy Cheng 
2664373ea84SWendy Cheng 	if (buf[size-1] != '\n')
2674373ea84SWendy Cheng 		return -EINVAL;
2684373ea84SWendy Cheng 
2694373ea84SWendy Cheng 	fo_path = buf;
2704373ea84SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
2714373ea84SWendy Cheng 		return -EINVAL;
2724373ea84SWendy Cheng 
27311f77942SStanislav Kinsbursky 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
2744373ea84SWendy Cheng 		return -EINVAL;
2754373ea84SWendy Cheng 
2764116092bSChuck Lever 	return nlmsvc_unlock_all_by_ip(sap);
2774373ea84SWendy Cheng }
2784373ea84SWendy Cheng 
279262a0982SChuck Lever /**
280262a0982SChuck Lever  * write_unlock_fs - Release all locks on a local file system
281262a0982SChuck Lever  *
282262a0982SChuck Lever  * Experimental.
283262a0982SChuck Lever  *
284262a0982SChuck Lever  * Input:
285262a0982SChuck Lever  *			buf:	'\n'-terminated C string containing the
286262a0982SChuck Lever  *				absolute pathname of a local file system
287262a0982SChuck Lever  *			size:	length of C string in @buf
288262a0982SChuck Lever  * Output:
289262a0982SChuck Lever  *	On success:	returns zero if all specified locks were released;
290262a0982SChuck Lever  *			returns one if one or more locks were not released
291262a0982SChuck Lever  *	On error:	return code is negative errno value
292262a0982SChuck Lever  */
293b046ccdcSChuck Lever static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
29417efa372SWendy Cheng {
295a63bb996SAl Viro 	struct path path;
29617efa372SWendy Cheng 	char *fo_path;
29717efa372SWendy Cheng 	int error;
29817efa372SWendy Cheng 
29917efa372SWendy Cheng 	/* sanity check */
30017efa372SWendy Cheng 	if (size == 0)
30117efa372SWendy Cheng 		return -EINVAL;
30217efa372SWendy Cheng 
30317efa372SWendy Cheng 	if (buf[size-1] != '\n')
30417efa372SWendy Cheng 		return -EINVAL;
30517efa372SWendy Cheng 
30617efa372SWendy Cheng 	fo_path = buf;
30717efa372SWendy Cheng 	if (qword_get(&buf, fo_path, size) < 0)
30817efa372SWendy Cheng 		return -EINVAL;
30917efa372SWendy Cheng 
310a63bb996SAl Viro 	error = kern_path(fo_path, 0, &path);
31117efa372SWendy Cheng 	if (error)
31217efa372SWendy Cheng 		return error;
31317efa372SWendy Cheng 
314262a0982SChuck Lever 	/*
315262a0982SChuck Lever 	 * XXX: Needs better sanity checking.  Otherwise we could end up
316262a0982SChuck Lever 	 * releasing locks on the wrong file system.
317262a0982SChuck Lever 	 *
318262a0982SChuck Lever 	 * For example:
319262a0982SChuck Lever 	 * 1.  Does the path refer to a directory?
320262a0982SChuck Lever 	 * 2.  Is that directory a mount point, or
321262a0982SChuck Lever 	 * 3.  Is that directory the root of an exported file system?
322262a0982SChuck Lever 	 */
323d8c9584eSAl Viro 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
32417efa372SWendy Cheng 
325a63bb996SAl Viro 	path_put(&path);
32617efa372SWendy Cheng 	return error;
32717efa372SWendy Cheng }
32817efa372SWendy Cheng 
329262a0982SChuck Lever /**
330262a0982SChuck Lever  * write_filehandle - Get a variable-length NFS file handle by path
331262a0982SChuck Lever  *
332262a0982SChuck Lever  * On input, the buffer contains a '\n'-terminated C string comprised of
333262a0982SChuck Lever  * three alphanumeric words separated by whitespace.  The string may
334262a0982SChuck Lever  * contain escape sequences.
335262a0982SChuck Lever  *
336262a0982SChuck Lever  * Input:
337262a0982SChuck Lever  *			buf:
338262a0982SChuck Lever  *				domain:		client domain name
339262a0982SChuck Lever  *				path:		export pathname
340262a0982SChuck Lever  *				maxsize:	numeric maximum size of
341262a0982SChuck Lever  *						@buf
342262a0982SChuck Lever  *			size:	length of C string in @buf
343262a0982SChuck Lever  * Output:
344262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
345262a0982SChuck Lever  *			string containing a ASCII hex text version
346262a0982SChuck Lever  *			of the NFS file handle;
347262a0982SChuck Lever  *			return code is the size in bytes of the string
348262a0982SChuck Lever  *	On error:	return code is negative errno value
349262a0982SChuck Lever  */
3501da177e4SLinus Torvalds static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	char *dname, *path;
353246d95baSAndrew Morton 	int uninitialized_var(maxsize);
3541da177e4SLinus Torvalds 	char *mesg = buf;
3551da177e4SLinus Torvalds 	int len;
3561da177e4SLinus Torvalds 	struct auth_domain *dom;
3571da177e4SLinus Torvalds 	struct knfsd_fh fh;
3581da177e4SLinus Torvalds 
35987d26ea7SJ. Bruce Fields 	if (size == 0)
36087d26ea7SJ. Bruce Fields 		return -EINVAL;
36187d26ea7SJ. Bruce Fields 
3621da177e4SLinus Torvalds 	if (buf[size-1] != '\n')
3631da177e4SLinus Torvalds 		return -EINVAL;
3641da177e4SLinus Torvalds 	buf[size-1] = 0;
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	dname = mesg;
3671da177e4SLinus Torvalds 	len = qword_get(&mesg, dname, size);
36854224f04SChuck Lever 	if (len <= 0)
36954224f04SChuck Lever 		return -EINVAL;
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds 	path = dname+len+1;
3721da177e4SLinus Torvalds 	len = qword_get(&mesg, path, size);
37354224f04SChuck Lever 	if (len <= 0)
37454224f04SChuck Lever 		return -EINVAL;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	len = get_int(&mesg, &maxsize);
3771da177e4SLinus Torvalds 	if (len)
3781da177e4SLinus Torvalds 		return len;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	if (maxsize < NFS_FHSIZE)
3811da177e4SLinus Torvalds 		return -EINVAL;
3823c7aa15dSKinglong Mee 	maxsize = min(maxsize, NFS3_FHSIZE);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	if (qword_get(&mesg, mesg, size)>0)
3851da177e4SLinus Torvalds 		return -EINVAL;
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	/* we have all the words, they are in buf.. */
3881da177e4SLinus Torvalds 	dom = unix_domain_find(dname);
3891da177e4SLinus Torvalds 	if (!dom)
3901da177e4SLinus Torvalds 		return -ENOMEM;
3911da177e4SLinus Torvalds 
392244c7d44SAl Viro 	len = exp_rootfh(netns(file), dom, path, &fh,  maxsize);
3931da177e4SLinus Torvalds 	auth_domain_put(dom);
3941da177e4SLinus Torvalds 	if (len)
3951da177e4SLinus Torvalds 		return len;
3961da177e4SLinus Torvalds 
39754224f04SChuck Lever 	mesg = buf;
39854224f04SChuck Lever 	len = SIMPLE_TRANSACTION_LIMIT;
3991da177e4SLinus Torvalds 	qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
4001da177e4SLinus Torvalds 	mesg[-1] = '\n';
4011da177e4SLinus Torvalds 	return mesg - buf;
4021da177e4SLinus Torvalds }
4031da177e4SLinus Torvalds 
404262a0982SChuck Lever /**
405262a0982SChuck Lever  * write_threads - Start NFSD, or report the current number of running threads
406262a0982SChuck Lever  *
407262a0982SChuck Lever  * Input:
408262a0982SChuck Lever  *			buf:		ignored
409262a0982SChuck Lever  *			size:		zero
410262a0982SChuck Lever  * Output:
411262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
412262a0982SChuck Lever  *			string numeric value representing the number of
413262a0982SChuck Lever  *			running NFSD threads;
414262a0982SChuck Lever  *			return code is the size in bytes of the string
415262a0982SChuck Lever  *	On error:	return code is zero
416262a0982SChuck Lever  *
417262a0982SChuck Lever  * OR
418262a0982SChuck Lever  *
419262a0982SChuck Lever  * Input:
420262a0982SChuck Lever  *			buf:		C string containing an unsigned
421262a0982SChuck Lever  *					integer value representing the
422262a0982SChuck Lever  *					number of NFSD threads to start
423262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
424262a0982SChuck Lever  * Output:
425262a0982SChuck Lever  *	On success:	NFS service is started;
426262a0982SChuck Lever  *			passed-in buffer filled with '\n'-terminated C
427262a0982SChuck Lever  *			string numeric value representing the number of
428262a0982SChuck Lever  *			running NFSD threads;
429262a0982SChuck Lever  *			return code is the size in bytes of the string
430262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
431262a0982SChuck Lever  */
4321da177e4SLinus Torvalds static ssize_t write_threads(struct file *file, char *buf, size_t size)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	char *mesg = buf;
4351da177e4SLinus Torvalds 	int rv;
436244c7d44SAl Viro 	struct net *net = netns(file);
437d41a9417SStanislav Kinsbursky 
4381da177e4SLinus Torvalds 	if (size > 0) {
4391da177e4SLinus Torvalds 		int newthreads;
4401da177e4SLinus Torvalds 		rv = get_int(&mesg, &newthreads);
4411da177e4SLinus Torvalds 		if (rv)
4421da177e4SLinus Torvalds 			return rv;
4431da177e4SLinus Torvalds 		if (newthreads < 0)
4441da177e4SLinus Torvalds 			return -EINVAL;
4454df493a2STrond Myklebust 		rv = nfsd_svc(newthreads, net, file->f_cred);
44682e12fe9SNeilBrown 		if (rv < 0)
4471da177e4SLinus Torvalds 			return rv;
44882e12fe9SNeilBrown 	} else
4499dd9845fSStanislav Kinsbursky 		rv = nfsd_nrthreads(net);
450e06b6405SChuck Lever 
45182e12fe9SNeilBrown 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
4521da177e4SLinus Torvalds }
4531da177e4SLinus Torvalds 
454262a0982SChuck Lever /**
455262a0982SChuck Lever  * write_pool_threads - Set or report the current number of threads per pool
456262a0982SChuck Lever  *
457262a0982SChuck Lever  * Input:
458262a0982SChuck Lever  *			buf:		ignored
459262a0982SChuck Lever  *			size:		zero
460262a0982SChuck Lever  *
461262a0982SChuck Lever  * OR
462262a0982SChuck Lever  *
463262a0982SChuck Lever  * Input:
464262a0982SChuck Lever  * 			buf:		C string containing whitespace-
465262a0982SChuck Lever  * 					separated unsigned integer values
466262a0982SChuck Lever  *					representing the number of NFSD
467262a0982SChuck Lever  *					threads to start in each pool
468262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
469262a0982SChuck Lever  * Output:
470262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
471262a0982SChuck Lever  *			string containing integer values representing the
472262a0982SChuck Lever  *			number of NFSD threads in each pool;
473262a0982SChuck Lever  *			return code is the size in bytes of the string
474262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
475262a0982SChuck Lever  */
476eed2965aSGreg Banks static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
477eed2965aSGreg Banks {
478eed2965aSGreg Banks 	/* if size > 0, look for an array of number of threads per node
479eed2965aSGreg Banks 	 * and apply them  then write out number of threads per node as reply
480eed2965aSGreg Banks 	 */
481eed2965aSGreg Banks 	char *mesg = buf;
482eed2965aSGreg Banks 	int i;
483eed2965aSGreg Banks 	int rv;
484eed2965aSGreg Banks 	int len;
485bedbdd8bSNeil Brown 	int npools;
486eed2965aSGreg Banks 	int *nthreads;
487244c7d44SAl Viro 	struct net *net = netns(file);
488eed2965aSGreg Banks 
489bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
4909dd9845fSStanislav Kinsbursky 	npools = nfsd_nrpools(net);
491eed2965aSGreg Banks 	if (npools == 0) {
492eed2965aSGreg Banks 		/*
493eed2965aSGreg Banks 		 * NFS is shut down.  The admin can start it by
494eed2965aSGreg Banks 		 * writing to the threads file but NOT the pool_threads
495eed2965aSGreg Banks 		 * file, sorry.  Report zero threads.
496eed2965aSGreg Banks 		 */
497bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
498eed2965aSGreg Banks 		strcpy(buf, "0\n");
499eed2965aSGreg Banks 		return strlen(buf);
500eed2965aSGreg Banks 	}
501eed2965aSGreg Banks 
502eed2965aSGreg Banks 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
503bedbdd8bSNeil Brown 	rv = -ENOMEM;
504eed2965aSGreg Banks 	if (nthreads == NULL)
505bedbdd8bSNeil Brown 		goto out_free;
506eed2965aSGreg Banks 
507eed2965aSGreg Banks 	if (size > 0) {
508eed2965aSGreg Banks 		for (i = 0; i < npools; i++) {
509eed2965aSGreg Banks 			rv = get_int(&mesg, &nthreads[i]);
510eed2965aSGreg Banks 			if (rv == -ENOENT)
511eed2965aSGreg Banks 				break;		/* fewer numbers than pools */
512eed2965aSGreg Banks 			if (rv)
513eed2965aSGreg Banks 				goto out_free;	/* syntax error */
514eed2965aSGreg Banks 			rv = -EINVAL;
515eed2965aSGreg Banks 			if (nthreads[i] < 0)
516eed2965aSGreg Banks 				goto out_free;
517eed2965aSGreg Banks 		}
5183938a0d5SStanislav Kinsbursky 		rv = nfsd_set_nrthreads(i, nthreads, net);
519eed2965aSGreg Banks 		if (rv)
520eed2965aSGreg Banks 			goto out_free;
521eed2965aSGreg Banks 	}
522eed2965aSGreg Banks 
5239dd9845fSStanislav Kinsbursky 	rv = nfsd_get_nrthreads(npools, nthreads, net);
524eed2965aSGreg Banks 	if (rv)
525eed2965aSGreg Banks 		goto out_free;
526eed2965aSGreg Banks 
527eed2965aSGreg Banks 	mesg = buf;
528eed2965aSGreg Banks 	size = SIMPLE_TRANSACTION_LIMIT;
529eed2965aSGreg Banks 	for (i = 0; i < npools && size > 0; i++) {
530eed2965aSGreg Banks 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
531eed2965aSGreg Banks 		len = strlen(mesg);
532eed2965aSGreg Banks 		size -= len;
533eed2965aSGreg Banks 		mesg += len;
534eed2965aSGreg Banks 	}
535413d63d7SJ. Bruce Fields 	rv = mesg - buf;
536eed2965aSGreg Banks out_free:
537eed2965aSGreg Banks 	kfree(nthreads);
538bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
539eed2965aSGreg Banks 	return rv;
540eed2965aSGreg Banks }
541eed2965aSGreg Banks 
542ff7d1179STrond Myklebust static ssize_t
543e333f3bbSTrond Myklebust nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
544e333f3bbSTrond Myklebust 		const char *sep, unsigned vers, int minor)
545ff7d1179STrond Myklebust {
546abcb4dacSNeilBrown 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
547e333f3bbSTrond Myklebust 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
548ff7d1179STrond Myklebust 
549abcb4dacSNeilBrown 	if (vers == 4 && minor >= 0 &&
550e333f3bbSTrond Myklebust 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
551ff7d1179STrond Myklebust 		supported = false;
552abcb4dacSNeilBrown 	if (minor == 0 && supported)
553abcb4dacSNeilBrown 		/*
554abcb4dacSNeilBrown 		 * special case for backward compatability.
555abcb4dacSNeilBrown 		 * +4.0 is never reported, it is implied by
556abcb4dacSNeilBrown 		 * +4, unless -4.0 is present.
557abcb4dacSNeilBrown 		 */
558abcb4dacSNeilBrown 		return 0;
559ff7d1179STrond Myklebust 	return snprintf(buf, remaining, format, sep,
560ff7d1179STrond Myklebust 			supported ? '+' : '-', vers, minor);
561ff7d1179STrond Myklebust }
562ff7d1179STrond Myklebust 
5633dd98a3bSJeff Layton static ssize_t __write_versions(struct file *file, char *buf, size_t size)
56470c3b76cSNeilBrown {
56570c3b76cSNeilBrown 	char *mesg = buf;
5668daf220aSBenny Halevy 	char *vers, *minorp, sign;
567261758b5SChuck Lever 	int len, num, remaining;
56870c3b76cSNeilBrown 	ssize_t tlen = 0;
56970c3b76cSNeilBrown 	char *sep;
570244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
57170c3b76cSNeilBrown 
57270c3b76cSNeilBrown 	if (size>0) {
5739dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
5746658d3a7SNeilBrown 			/* Cannot change versions without updating
5759dd9845fSStanislav Kinsbursky 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
5766658d3a7SNeilBrown 			 * rq_argp and rq_resp
5776658d3a7SNeilBrown 			 */
57870c3b76cSNeilBrown 			return -EBUSY;
57970c3b76cSNeilBrown 		if (buf[size-1] != '\n')
58070c3b76cSNeilBrown 			return -EINVAL;
58170c3b76cSNeilBrown 		buf[size-1] = 0;
58270c3b76cSNeilBrown 
58370c3b76cSNeilBrown 		vers = mesg;
58470c3b76cSNeilBrown 		len = qword_get(&mesg, vers, size);
58570c3b76cSNeilBrown 		if (len <= 0) return -EINVAL;
58670c3b76cSNeilBrown 		do {
587d3635ff0STrond Myklebust 			enum vers_op cmd;
588abcb4dacSNeilBrown 			unsigned minor;
58970c3b76cSNeilBrown 			sign = *vers;
59070c3b76cSNeilBrown 			if (sign == '+' || sign == '-')
5918daf220aSBenny Halevy 				num = simple_strtol((vers+1), &minorp, 0);
59270c3b76cSNeilBrown 			else
5938daf220aSBenny Halevy 				num = simple_strtol(vers, &minorp, 0);
5948daf220aSBenny Halevy 			if (*minorp == '.') {
595ff89be87SJ. Bruce Fields 				if (num != 4)
5968daf220aSBenny Halevy 					return -EINVAL;
597e35659f1SNeilBrown 				if (kstrtouint(minorp+1, 0, &minor) < 0)
5988daf220aSBenny Halevy 					return -EINVAL;
599abcb4dacSNeilBrown 			}
600abcb4dacSNeilBrown 
601d3635ff0STrond Myklebust 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
60270c3b76cSNeilBrown 			switch(num) {
60370c3b76cSNeilBrown 			case 2:
60470c3b76cSNeilBrown 			case 3:
605e333f3bbSTrond Myklebust 				nfsd_vers(nn, num, cmd);
606d3635ff0STrond Myklebust 				break;
60770c3b76cSNeilBrown 			case 4:
608abcb4dacSNeilBrown 				if (*minorp == '.') {
609e333f3bbSTrond Myklebust 					if (nfsd_minorversion(nn, minor, cmd) < 0)
610abcb4dacSNeilBrown 						return -EINVAL;
611e333f3bbSTrond Myklebust 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
612abcb4dacSNeilBrown 					/*
613abcb4dacSNeilBrown 					 * Either we have +4 and no minors are enabled,
614abcb4dacSNeilBrown 					 * or we have -4 and at least one minor is enabled.
615abcb4dacSNeilBrown 					 * In either case, propagate 'cmd' to all minors.
616abcb4dacSNeilBrown 					 */
617abcb4dacSNeilBrown 					minor = 0;
618e333f3bbSTrond Myklebust 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
619abcb4dacSNeilBrown 						minor++;
620abcb4dacSNeilBrown 				}
62170c3b76cSNeilBrown 				break;
62270c3b76cSNeilBrown 			default:
62370c3b76cSNeilBrown 				return -EINVAL;
62470c3b76cSNeilBrown 			}
62570c3b76cSNeilBrown 			vers += len + 1;
62670c3b76cSNeilBrown 		} while ((len = qword_get(&mesg, vers, size)) > 0);
62770c3b76cSNeilBrown 		/* If all get turned off, turn them back on, as
62870c3b76cSNeilBrown 		 * having no versions is BAD
62970c3b76cSNeilBrown 		 */
630e333f3bbSTrond Myklebust 		nfsd_reset_versions(nn);
63170c3b76cSNeilBrown 	}
632261758b5SChuck Lever 
63370c3b76cSNeilBrown 	/* Now write current state into reply buffer */
63470c3b76cSNeilBrown 	len = 0;
63570c3b76cSNeilBrown 	sep = "";
636261758b5SChuck Lever 	remaining = SIMPLE_TRANSACTION_LIMIT;
637ff7d1179STrond Myklebust 	for (num=2 ; num <= 4 ; num++) {
638abcb4dacSNeilBrown 		int minor;
639e333f3bbSTrond Myklebust 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
640e35659f1SNeilBrown 			continue;
641abcb4dacSNeilBrown 
642abcb4dacSNeilBrown 		minor = -1;
643ff7d1179STrond Myklebust 		do {
644e333f3bbSTrond Myklebust 			len = nfsd_print_version_support(nn, buf, remaining,
645ff7d1179STrond Myklebust 					sep, num, minor);
646818f2f57SDan Carpenter 			if (len >= remaining)
647ff7d1179STrond Myklebust 				goto out;
648261758b5SChuck Lever 			remaining -= len;
649261758b5SChuck Lever 			buf += len;
650261758b5SChuck Lever 			tlen += len;
651ff7d1179STrond Myklebust 			minor++;
652abcb4dacSNeilBrown 			if (len)
653ff7d1179STrond Myklebust 				sep = " ";
654ff7d1179STrond Myklebust 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
655261758b5SChuck Lever 	}
656ff7d1179STrond Myklebust out:
657261758b5SChuck Lever 	len = snprintf(buf, remaining, "\n");
658818f2f57SDan Carpenter 	if (len >= remaining)
659261758b5SChuck Lever 		return -EINVAL;
660261758b5SChuck Lever 	return tlen + len;
66170c3b76cSNeilBrown }
66270c3b76cSNeilBrown 
663262a0982SChuck Lever /**
664262a0982SChuck Lever  * write_versions - Set or report the available NFS protocol versions
665262a0982SChuck Lever  *
666262a0982SChuck Lever  * Input:
667262a0982SChuck Lever  *			buf:		ignored
668262a0982SChuck Lever  *			size:		zero
669262a0982SChuck Lever  * Output:
670262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
671262a0982SChuck Lever  *			string containing positive or negative integer
672262a0982SChuck Lever  *			values representing the current status of each
673262a0982SChuck Lever  *			protocol version;
674262a0982SChuck Lever  *			return code is the size in bytes of the string
675262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
676262a0982SChuck Lever  *
677262a0982SChuck Lever  * OR
678262a0982SChuck Lever  *
679262a0982SChuck Lever  * Input:
680262a0982SChuck Lever  * 			buf:		C string containing whitespace-
681262a0982SChuck Lever  * 					separated positive or negative
682262a0982SChuck Lever  * 					integer values representing NFS
683262a0982SChuck Lever  * 					protocol versions to enable ("+n")
684262a0982SChuck Lever  * 					or disable ("-n")
685262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
686262a0982SChuck Lever  * Output:
687262a0982SChuck Lever  *	On success:	status of zero or more protocol versions has
688262a0982SChuck Lever  *			been updated; passed-in buffer filled with
689262a0982SChuck Lever  *			'\n'-terminated C string containing positive
690262a0982SChuck Lever  *			or negative integer values representing the
691262a0982SChuck Lever  *			current status of each protocol version;
692262a0982SChuck Lever  *			return code is the size in bytes of the string
693262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
694262a0982SChuck Lever  */
6953dd98a3bSJeff Layton static ssize_t write_versions(struct file *file, char *buf, size_t size)
6963dd98a3bSJeff Layton {
6973dd98a3bSJeff Layton 	ssize_t rv;
6983dd98a3bSJeff Layton 
6993dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
7003dd98a3bSJeff Layton 	rv = __write_versions(file, buf, size);
7013dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
7023dd98a3bSJeff Layton 	return rv;
7033dd98a3bSJeff Layton }
7043dd98a3bSJeff Layton 
7054cd5dc75SChuck Lever /*
7060a5372d8SChuck Lever  * Zero-length write.  Return a list of NFSD's current listener
7070a5372d8SChuck Lever  * transports.
7080a5372d8SChuck Lever  */
7099dd9845fSStanislav Kinsbursky static ssize_t __write_ports_names(char *buf, struct net *net)
7100a5372d8SChuck Lever {
7119dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7129dd9845fSStanislav Kinsbursky 
7139dd9845fSStanislav Kinsbursky 	if (nn->nfsd_serv == NULL)
7140a5372d8SChuck Lever 		return 0;
7159dd9845fSStanislav Kinsbursky 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
7160a5372d8SChuck Lever }
7170a5372d8SChuck Lever 
7180a5372d8SChuck Lever /*
7190b7c2f6fSChuck Lever  * A single 'fd' number was written, in which case it must be for
7200b7c2f6fSChuck Lever  * a socket of a supported family/protocol, and we use it as an
7210b7c2f6fSChuck Lever  * nfsd listener.
7220b7c2f6fSChuck Lever  */
7234df493a2STrond Myklebust static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
7240b7c2f6fSChuck Lever {
7250b7c2f6fSChuck Lever 	char *mesg = buf;
7260b7c2f6fSChuck Lever 	int fd, err;
7279dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7280b7c2f6fSChuck Lever 
7290b7c2f6fSChuck Lever 	err = get_int(&mesg, &fd);
7300b7c2f6fSChuck Lever 	if (err != 0 || fd < 0)
7310b7c2f6fSChuck Lever 		return -EINVAL;
7320b7c2f6fSChuck Lever 
73330646394SStanislav Kinsbursky 	if (svc_alien_sock(net, fd)) {
73430646394SStanislav Kinsbursky 		printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
73530646394SStanislav Kinsbursky 		return -EINVAL;
73630646394SStanislav Kinsbursky 	}
73730646394SStanislav Kinsbursky 
7386777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7390b7c2f6fSChuck Lever 	if (err != 0)
7400b7c2f6fSChuck Lever 		return err;
7410b7c2f6fSChuck Lever 
7424df493a2STrond Myklebust 	err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
74378a8d7c8SJeff Layton 	if (err < 0) {
74419f7e2caSStanislav Kinsbursky 		nfsd_destroy(net);
74578a8d7c8SJeff Layton 		return err;
74678a8d7c8SJeff Layton 	}
747ea068badSChuck Lever 
7480b7c2f6fSChuck Lever 	/* Decrease the count, but don't shut down the service */
7499dd9845fSStanislav Kinsbursky 	nn->nfsd_serv->sv_nrthreads--;
750ea068badSChuck Lever 	return err;
7510b7c2f6fSChuck Lever }
7520b7c2f6fSChuck Lever 
7530b7c2f6fSChuck Lever /*
7544eb68c26SChuck Lever  * A transport listener is added by writing it's transport name and
7554eb68c26SChuck Lever  * a port number.
7564eb68c26SChuck Lever  */
7574df493a2STrond Myklebust static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
7584eb68c26SChuck Lever {
7594eb68c26SChuck Lever 	char transport[16];
76037498292SChuck Lever 	struct svc_xprt *xprt;
7614eb68c26SChuck Lever 	int port, err;
7629dd9845fSStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7634eb68c26SChuck Lever 
764a10fded1SJ. Bruce Fields 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
7654eb68c26SChuck Lever 		return -EINVAL;
7664eb68c26SChuck Lever 
7674be929beSAlexey Dobriyan 	if (port < 1 || port > USHRT_MAX)
7684eb68c26SChuck Lever 		return -EINVAL;
7694eb68c26SChuck Lever 
7706777436bSStanislav Kinsbursky 	err = nfsd_create_serv(net);
7714eb68c26SChuck Lever 	if (err != 0)
7724eb68c26SChuck Lever 		return err;
7734eb68c26SChuck Lever 
7749dd9845fSStanislav Kinsbursky 	err = svc_create_xprt(nn->nfsd_serv, transport, net,
7754df493a2STrond Myklebust 				PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
77668717908SChuck Lever 	if (err < 0)
77737498292SChuck Lever 		goto out_err;
77837498292SChuck Lever 
7799dd9845fSStanislav Kinsbursky 	err = svc_create_xprt(nn->nfsd_serv, transport, net,
7804df493a2STrond Myklebust 				PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
78137498292SChuck Lever 	if (err < 0 && err != -EAFNOSUPPORT)
78237498292SChuck Lever 		goto out_close;
7830cd14a06SJeff Layton 
7840cd14a06SJeff Layton 	/* Decrease the count, but don't shut down the service */
7859dd9845fSStanislav Kinsbursky 	nn->nfsd_serv->sv_nrthreads--;
7864eb68c26SChuck Lever 	return 0;
78737498292SChuck Lever out_close:
7889dd9845fSStanislav Kinsbursky 	xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
78937498292SChuck Lever 	if (xprt != NULL) {
79037498292SChuck Lever 		svc_close_xprt(xprt);
79137498292SChuck Lever 		svc_xprt_put(xprt);
79237498292SChuck Lever 	}
79337498292SChuck Lever out_err:
79419f7e2caSStanislav Kinsbursky 	nfsd_destroy(net);
79537498292SChuck Lever 	return err;
7964eb68c26SChuck Lever }
7974eb68c26SChuck Lever 
79808160352SStanislav Kinsbursky static ssize_t __write_ports(struct file *file, char *buf, size_t size,
79908160352SStanislav Kinsbursky 			     struct net *net)
80080212d59SNeilBrown {
8010a5372d8SChuck Lever 	if (size == 0)
8029dd9845fSStanislav Kinsbursky 		return __write_ports_names(buf, net);
8030b7c2f6fSChuck Lever 
8040b7c2f6fSChuck Lever 	if (isdigit(buf[0]))
8054df493a2STrond Myklebust 		return __write_ports_addfd(buf, net, file->f_cred);
80682d56591SChuck Lever 
8074eb68c26SChuck Lever 	if (isalpha(buf[0]))
8084df493a2STrond Myklebust 		return __write_ports_addxprt(buf, net, file->f_cred);
8094cd5dc75SChuck Lever 
810b41b66d6SNeilBrown 	return -EINVAL;
811b41b66d6SNeilBrown }
81280212d59SNeilBrown 
813262a0982SChuck Lever /**
814262a0982SChuck Lever  * write_ports - Pass a socket file descriptor or transport name to listen on
815262a0982SChuck Lever  *
816262a0982SChuck Lever  * Input:
817262a0982SChuck Lever  *			buf:		ignored
818262a0982SChuck Lever  *			size:		zero
819262a0982SChuck Lever  * Output:
820262a0982SChuck Lever  *	On success:	passed-in buffer filled with a '\n'-terminated C
821262a0982SChuck Lever  *			string containing a whitespace-separated list of
822262a0982SChuck Lever  *			named NFSD listeners;
823262a0982SChuck Lever  *			return code is the size in bytes of the string
824262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
825262a0982SChuck Lever  *
826262a0982SChuck Lever  * OR
827262a0982SChuck Lever  *
828262a0982SChuck Lever  * Input:
829262a0982SChuck Lever  *			buf:		C string containing an unsigned
830262a0982SChuck Lever  *					integer value representing a bound
831262a0982SChuck Lever  *					but unconnected socket that is to be
832c71206a7SChuck Lever  *					used as an NFSD listener; listen(3)
833c71206a7SChuck Lever  *					must be called for a SOCK_STREAM
834c71206a7SChuck Lever  *					socket, otherwise it is ignored
835262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
836262a0982SChuck Lever  * Output:
837262a0982SChuck Lever  *	On success:	NFS service is started;
838262a0982SChuck Lever  *			passed-in buffer filled with a '\n'-terminated C
839262a0982SChuck Lever  *			string containing a unique alphanumeric name of
840262a0982SChuck Lever  *			the listener;
841262a0982SChuck Lever  *			return code is the size in bytes of the string
842262a0982SChuck Lever  *	On error:	return code is a negative errno value
843262a0982SChuck Lever  *
844262a0982SChuck Lever  * OR
845262a0982SChuck Lever  *
846262a0982SChuck Lever  * Input:
847262a0982SChuck Lever  *			buf:		C string containing a transport
848262a0982SChuck Lever  *					name and an unsigned integer value
849262a0982SChuck Lever  *					representing the port to listen on,
850262a0982SChuck Lever  *					separated by whitespace
851262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
852262a0982SChuck Lever  * Output:
853262a0982SChuck Lever  *	On success:	returns zero; NFS service is started
854262a0982SChuck Lever  *	On error:	return code is a negative errno value
855262a0982SChuck Lever  */
856bedbdd8bSNeil Brown static ssize_t write_ports(struct file *file, char *buf, size_t size)
857bedbdd8bSNeil Brown {
858bedbdd8bSNeil Brown 	ssize_t rv;
8593dd98a3bSJeff Layton 
860bedbdd8bSNeil Brown 	mutex_lock(&nfsd_mutex);
861244c7d44SAl Viro 	rv = __write_ports(file, buf, size, netns(file));
862bedbdd8bSNeil Brown 	mutex_unlock(&nfsd_mutex);
863bedbdd8bSNeil Brown 	return rv;
864bedbdd8bSNeil Brown }
865bedbdd8bSNeil Brown 
866bedbdd8bSNeil Brown 
867596bbe53SNeilBrown int nfsd_max_blksize;
868596bbe53SNeilBrown 
869262a0982SChuck Lever /**
870262a0982SChuck Lever  * write_maxblksize - Set or report the current NFS blksize
871262a0982SChuck Lever  *
872262a0982SChuck Lever  * Input:
873262a0982SChuck Lever  *			buf:		ignored
874262a0982SChuck Lever  *			size:		zero
875262a0982SChuck Lever  *
876262a0982SChuck Lever  * OR
877262a0982SChuck Lever  *
878262a0982SChuck Lever  * Input:
879262a0982SChuck Lever  * 			buf:		C string containing an unsigned
880262a0982SChuck Lever  * 					integer value representing the new
881262a0982SChuck Lever  * 					NFS blksize
882262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
883262a0982SChuck Lever  * Output:
884262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
885262a0982SChuck Lever  *			containing numeric value of the current NFS blksize
886262a0982SChuck Lever  *			setting;
887262a0982SChuck Lever  *			return code is the size in bytes of the string
888262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
889262a0982SChuck Lever  */
890596bbe53SNeilBrown static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
891596bbe53SNeilBrown {
892596bbe53SNeilBrown 	char *mesg = buf;
893244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
8949dd9845fSStanislav Kinsbursky 
895596bbe53SNeilBrown 	if (size > 0) {
896596bbe53SNeilBrown 		int bsize;
897596bbe53SNeilBrown 		int rv = get_int(&mesg, &bsize);
898596bbe53SNeilBrown 		if (rv)
899596bbe53SNeilBrown 			return rv;
900596bbe53SNeilBrown 		/* force bsize into allowed range and
901596bbe53SNeilBrown 		 * required alignment.
902596bbe53SNeilBrown 		 */
9033c7aa15dSKinglong Mee 		bsize = max_t(int, bsize, 1024);
9043c7aa15dSKinglong Mee 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
905596bbe53SNeilBrown 		bsize &= ~(1024-1);
906bedbdd8bSNeil Brown 		mutex_lock(&nfsd_mutex);
9079dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv) {
908bedbdd8bSNeil Brown 			mutex_unlock(&nfsd_mutex);
909596bbe53SNeilBrown 			return -EBUSY;
910596bbe53SNeilBrown 		}
911596bbe53SNeilBrown 		nfsd_max_blksize = bsize;
912bedbdd8bSNeil Brown 		mutex_unlock(&nfsd_mutex);
913596bbe53SNeilBrown 	}
914e06b6405SChuck Lever 
915e06b6405SChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
916e06b6405SChuck Lever 							nfsd_max_blksize);
917596bbe53SNeilBrown }
918596bbe53SNeilBrown 
9195b8db00bSJeff Layton /**
9205b8db00bSJeff Layton  * write_maxconn - Set or report the current max number of connections
9215b8db00bSJeff Layton  *
9225b8db00bSJeff Layton  * Input:
9235b8db00bSJeff Layton  *			buf:		ignored
9245b8db00bSJeff Layton  *			size:		zero
9255b8db00bSJeff Layton  * OR
9265b8db00bSJeff Layton  *
9275b8db00bSJeff Layton  * Input:
9285b8db00bSJeff Layton  * 			buf:		C string containing an unsigned
9295b8db00bSJeff Layton  * 					integer value representing the new
9305b8db00bSJeff Layton  * 					number of max connections
9315b8db00bSJeff Layton  *			size:		non-zero length of C string in @buf
9325b8db00bSJeff Layton  * Output:
9335b8db00bSJeff Layton  *	On success:	passed-in buffer filled with '\n'-terminated C string
9345b8db00bSJeff Layton  *			containing numeric value of max_connections setting
9355b8db00bSJeff Layton  *			for this net namespace;
9365b8db00bSJeff Layton  *			return code is the size in bytes of the string
9375b8db00bSJeff Layton  *	On error:	return code is zero or a negative errno value
9385b8db00bSJeff Layton  */
9395b8db00bSJeff Layton static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
9405b8db00bSJeff Layton {
9415b8db00bSJeff Layton 	char *mesg = buf;
942244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
9435b8db00bSJeff Layton 	unsigned int maxconn = nn->max_connections;
9445b8db00bSJeff Layton 
9455b8db00bSJeff Layton 	if (size > 0) {
9465b8db00bSJeff Layton 		int rv = get_uint(&mesg, &maxconn);
9475b8db00bSJeff Layton 
9485b8db00bSJeff Layton 		if (rv)
9495b8db00bSJeff Layton 			return rv;
9505b8db00bSJeff Layton 		nn->max_connections = maxconn;
9515b8db00bSJeff Layton 	}
9525b8db00bSJeff Layton 
9535b8db00bSJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
9545b8db00bSJeff Layton }
9555b8db00bSJeff Layton 
95670c3b76cSNeilBrown #ifdef CONFIG_NFSD_V4
9579dd9845fSStanislav Kinsbursky static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
9589dd9845fSStanislav Kinsbursky 				  time_t *time, struct nfsd_net *nn)
9591da177e4SLinus Torvalds {
9601da177e4SLinus Torvalds 	char *mesg = buf;
961f0135740SJ. Bruce Fields 	int rv, i;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	if (size > 0) {
9649dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
9653dd98a3bSJeff Layton 			return -EBUSY;
966f0135740SJ. Bruce Fields 		rv = get_int(&mesg, &i);
9671da177e4SLinus Torvalds 		if (rv)
9681da177e4SLinus Torvalds 			return rv;
969e7b184f1SJ. Bruce Fields 		/*
970e7b184f1SJ. Bruce Fields 		 * Some sanity checking.  We don't have a reason for
971e7b184f1SJ. Bruce Fields 		 * these particular numbers, but problems with the
972e7b184f1SJ. Bruce Fields 		 * extremes are:
973e7b184f1SJ. Bruce Fields 		 *	- Too short: the briefest network outage may
974e7b184f1SJ. Bruce Fields 		 *	  cause clients to lose all their locks.  Also,
975e7b184f1SJ. Bruce Fields 		 *	  the frequent polling may be wasteful.
976e7b184f1SJ. Bruce Fields 		 *	- Too long: do you really want reboot recovery
977e7b184f1SJ. Bruce Fields 		 *	  to take more than an hour?  Or to make other
978e7b184f1SJ. Bruce Fields 		 *	  clients wait an hour before being able to
979e7b184f1SJ. Bruce Fields 		 *	  revoke a dead client's locks?
980e7b184f1SJ. Bruce Fields 		 */
981f0135740SJ. Bruce Fields 		if (i < 10 || i > 3600)
9821da177e4SLinus Torvalds 			return -EINVAL;
983f0135740SJ. Bruce Fields 		*time = i;
9841da177e4SLinus Torvalds 	}
985e06b6405SChuck Lever 
986f0135740SJ. Bruce Fields 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
987f0135740SJ. Bruce Fields }
988f0135740SJ. Bruce Fields 
9899dd9845fSStanislav Kinsbursky static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
9909dd9845fSStanislav Kinsbursky 				time_t *time, struct nfsd_net *nn)
991f0135740SJ. Bruce Fields {
992f0135740SJ. Bruce Fields 	ssize_t rv;
993f0135740SJ. Bruce Fields 
994f0135740SJ. Bruce Fields 	mutex_lock(&nfsd_mutex);
9959dd9845fSStanislav Kinsbursky 	rv = __nfsd4_write_time(file, buf, size, time, nn);
996f0135740SJ. Bruce Fields 	mutex_unlock(&nfsd_mutex);
997f0135740SJ. Bruce Fields 	return rv;
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
1000262a0982SChuck Lever /**
1001262a0982SChuck Lever  * write_leasetime - Set or report the current NFSv4 lease time
1002262a0982SChuck Lever  *
1003262a0982SChuck Lever  * Input:
1004262a0982SChuck Lever  *			buf:		ignored
1005262a0982SChuck Lever  *			size:		zero
1006262a0982SChuck Lever  *
1007262a0982SChuck Lever  * OR
1008262a0982SChuck Lever  *
1009262a0982SChuck Lever  * Input:
1010262a0982SChuck Lever  *			buf:		C string containing an unsigned
1011262a0982SChuck Lever  *					integer value representing the new
1012262a0982SChuck Lever  *					NFSv4 lease expiry time
1013262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1014262a0982SChuck Lever  * Output:
1015262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C
1016262a0982SChuck Lever  *			string containing unsigned integer value of the
1017262a0982SChuck Lever  *			current lease expiry time;
1018262a0982SChuck Lever  *			return code is the size in bytes of the string
1019262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1020262a0982SChuck Lever  */
10213dd98a3bSJeff Layton static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
10223dd98a3bSJeff Layton {
1023244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10249dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
10253dd98a3bSJeff Layton }
10263dd98a3bSJeff Layton 
1027efc4bb4fSJ. Bruce Fields /**
1028efc4bb4fSJ. Bruce Fields  * write_gracetime - Set or report current NFSv4 grace period time
1029efc4bb4fSJ. Bruce Fields  *
1030efc4bb4fSJ. Bruce Fields  * As above, but sets the time of the NFSv4 grace period.
1031efc4bb4fSJ. Bruce Fields  *
1032efc4bb4fSJ. Bruce Fields  * Note this should never be set to less than the *previous*
1033efc4bb4fSJ. Bruce Fields  * lease-period time, but we don't try to enforce this.  (In the common
1034efc4bb4fSJ. Bruce Fields  * case (a new boot), we don't know what the previous lease time was
1035efc4bb4fSJ. Bruce Fields  * anyway.)
1036efc4bb4fSJ. Bruce Fields  */
1037efc4bb4fSJ. Bruce Fields static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1038efc4bb4fSJ. Bruce Fields {
1039244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10409dd9845fSStanislav Kinsbursky 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1041efc4bb4fSJ. Bruce Fields }
1042efc4bb4fSJ. Bruce Fields 
10439dd9845fSStanislav Kinsbursky static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
10449dd9845fSStanislav Kinsbursky 				   struct nfsd_net *nn)
10450964a3d3SNeilBrown {
10460964a3d3SNeilBrown 	char *mesg = buf;
10470964a3d3SNeilBrown 	char *recdir;
10480964a3d3SNeilBrown 	int len, status;
10490964a3d3SNeilBrown 
10503dd98a3bSJeff Layton 	if (size > 0) {
10519dd9845fSStanislav Kinsbursky 		if (nn->nfsd_serv)
10523dd98a3bSJeff Layton 			return -EBUSY;
10533dd98a3bSJeff Layton 		if (size > PATH_MAX || buf[size-1] != '\n')
10540964a3d3SNeilBrown 			return -EINVAL;
10550964a3d3SNeilBrown 		buf[size-1] = 0;
10560964a3d3SNeilBrown 
10570964a3d3SNeilBrown 		recdir = mesg;
10580964a3d3SNeilBrown 		len = qword_get(&mesg, recdir, size);
10590964a3d3SNeilBrown 		if (len <= 0)
10600964a3d3SNeilBrown 			return -EINVAL;
10610964a3d3SNeilBrown 
10620964a3d3SNeilBrown 		status = nfs4_reset_recoverydir(recdir);
106369049961SAndi Kleen 		if (status)
106469049961SAndi Kleen 			return status;
10653dd98a3bSJeff Layton 	}
10663d72ab8fSChuck Lever 
10673d72ab8fSChuck Lever 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
10683d72ab8fSChuck Lever 							nfs4_recoverydir());
10690964a3d3SNeilBrown }
10703dd98a3bSJeff Layton 
1071262a0982SChuck Lever /**
1072262a0982SChuck Lever  * write_recoverydir - Set or report the pathname of the recovery directory
1073262a0982SChuck Lever  *
1074262a0982SChuck Lever  * Input:
1075262a0982SChuck Lever  *			buf:		ignored
1076262a0982SChuck Lever  *			size:		zero
1077262a0982SChuck Lever  *
1078262a0982SChuck Lever  * OR
1079262a0982SChuck Lever  *
1080262a0982SChuck Lever  * Input:
1081262a0982SChuck Lever  *			buf:		C string containing the pathname
1082262a0982SChuck Lever  *					of the directory on a local file
1083262a0982SChuck Lever  *					system containing permanent NFSv4
1084262a0982SChuck Lever  *					recovery data
1085262a0982SChuck Lever  *			size:		non-zero length of C string in @buf
1086262a0982SChuck Lever  * Output:
1087262a0982SChuck Lever  *	On success:	passed-in buffer filled with '\n'-terminated C string
1088262a0982SChuck Lever  *			containing the current recovery pathname setting;
1089262a0982SChuck Lever  *			return code is the size in bytes of the string
1090262a0982SChuck Lever  *	On error:	return code is zero or a negative errno value
1091262a0982SChuck Lever  */
10923dd98a3bSJeff Layton static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
10933dd98a3bSJeff Layton {
10943dd98a3bSJeff Layton 	ssize_t rv;
1095244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
10963dd98a3bSJeff Layton 
10973dd98a3bSJeff Layton 	mutex_lock(&nfsd_mutex);
10989dd9845fSStanislav Kinsbursky 	rv = __write_recoverydir(file, buf, size, nn);
10993dd98a3bSJeff Layton 	mutex_unlock(&nfsd_mutex);
11003dd98a3bSJeff Layton 	return rv;
11013dd98a3bSJeff Layton }
11023dd98a3bSJeff Layton 
11037f5ef2e9SJeff Layton /**
11047f5ef2e9SJeff Layton  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
11057f5ef2e9SJeff Layton  *
11067f5ef2e9SJeff Layton  * Input:
11077f5ef2e9SJeff Layton  *			buf:		ignored
11087f5ef2e9SJeff Layton  *			size:		zero
11097f5ef2e9SJeff Layton  * OR
11107f5ef2e9SJeff Layton  *
11117f5ef2e9SJeff Layton  * Input:
11127f5ef2e9SJeff Layton  * 			buf:		any value
11137f5ef2e9SJeff Layton  *			size:		non-zero length of C string in @buf
11147f5ef2e9SJeff Layton  * Output:
11157f5ef2e9SJeff Layton  *			passed-in buffer filled with "Y" or "N" with a newline
11167f5ef2e9SJeff Layton  *			and NULL-terminated C string. This indicates whether
11177f5ef2e9SJeff Layton  *			the grace period has ended in the current net
11187f5ef2e9SJeff Layton  *			namespace. Return code is the size in bytes of the
11197f5ef2e9SJeff Layton  *			string. Writing a string that starts with 'Y', 'y', or
11207f5ef2e9SJeff Layton  *			'1' to the file will end the grace period for nfsd's v4
11217f5ef2e9SJeff Layton  *			lock manager.
11227f5ef2e9SJeff Layton  */
11237f5ef2e9SJeff Layton static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
11247f5ef2e9SJeff Layton {
1125244c7d44SAl Viro 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
11267f5ef2e9SJeff Layton 
11277f5ef2e9SJeff Layton 	if (size > 0) {
11287f5ef2e9SJeff Layton 		switch(buf[0]) {
11297f5ef2e9SJeff Layton 		case 'Y':
11307f5ef2e9SJeff Layton 		case 'y':
11317f5ef2e9SJeff Layton 		case '1':
1132dd838821SYihao Wu 			if (!nn->nfsd_serv)
113362a063b8SJ. Bruce Fields 				return -EBUSY;
11347f5ef2e9SJeff Layton 			nfsd4_end_grace(nn);
11357f5ef2e9SJeff Layton 			break;
11367f5ef2e9SJeff Layton 		default:
11377f5ef2e9SJeff Layton 			return -EINVAL;
11387f5ef2e9SJeff Layton 		}
11397f5ef2e9SJeff Layton 	}
11407f5ef2e9SJeff Layton 
11417f5ef2e9SJeff Layton 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
11427f5ef2e9SJeff Layton 			 nn->grace_ended ? 'Y' : 'N');
11437f5ef2e9SJeff Layton }
11447f5ef2e9SJeff Layton 
114570c3b76cSNeilBrown #endif
11460964a3d3SNeilBrown 
11471da177e4SLinus Torvalds /*----------------------------------------------------------------------------*/
11481da177e4SLinus Torvalds /*
11491da177e4SLinus Torvalds  *	populating the filesystem.
11501da177e4SLinus Torvalds  */
11511da177e4SLinus Torvalds 
1152e8a79fb1SJ. Bruce Fields /* Basically copying rpc_get_inode. */
1153e8a79fb1SJ. Bruce Fields static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1154e8a79fb1SJ. Bruce Fields {
1155e8a79fb1SJ. Bruce Fields 	struct inode *inode = new_inode(sb);
1156e8a79fb1SJ. Bruce Fields 	if (!inode)
1157e8a79fb1SJ. Bruce Fields 		return NULL;
1158e8a79fb1SJ. Bruce Fields 	/* Following advice from simple_fill_super documentation: */
1159e8a79fb1SJ. Bruce Fields 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1160e8a79fb1SJ. Bruce Fields 	inode->i_mode = mode;
1161e8a79fb1SJ. Bruce Fields 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1162e8a79fb1SJ. Bruce Fields 	switch (mode & S_IFMT) {
1163e8a79fb1SJ. Bruce Fields 	case S_IFDIR:
1164e8a79fb1SJ. Bruce Fields 		inode->i_fop = &simple_dir_operations;
1165e8a79fb1SJ. Bruce Fields 		inode->i_op = &simple_dir_inode_operations;
1166e8a79fb1SJ. Bruce Fields 		inc_nlink(inode);
1167e8a79fb1SJ. Bruce Fields 	default:
1168e8a79fb1SJ. Bruce Fields 		break;
1169e8a79fb1SJ. Bruce Fields 	}
1170e8a79fb1SJ. Bruce Fields 	return inode;
1171e8a79fb1SJ. Bruce Fields }
1172e8a79fb1SJ. Bruce Fields 
1173e8a79fb1SJ. Bruce Fields static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1174e8a79fb1SJ. Bruce Fields {
1175e8a79fb1SJ. Bruce Fields 	struct inode *inode;
1176e8a79fb1SJ. Bruce Fields 
1177e8a79fb1SJ. Bruce Fields 	inode = nfsd_get_inode(dir->i_sb, mode);
1178e8a79fb1SJ. Bruce Fields 	if (!inode)
1179e8a79fb1SJ. Bruce Fields 		return -ENOMEM;
1180e8a79fb1SJ. Bruce Fields 	d_add(dentry, inode);
1181e8a79fb1SJ. Bruce Fields 	inc_nlink(dir);
1182e8a79fb1SJ. Bruce Fields 	fsnotify_mkdir(dir, dentry);
1183e8a79fb1SJ. Bruce Fields 	return 0;
1184e8a79fb1SJ. Bruce Fields }
1185e8a79fb1SJ. Bruce Fields 
1186e8a79fb1SJ. Bruce Fields static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1187e8a79fb1SJ. Bruce Fields {
1188e8a79fb1SJ. Bruce Fields 	struct inode *dir = parent->d_inode;
1189e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1190e8a79fb1SJ. Bruce Fields 	int ret = -ENOMEM;
1191e8a79fb1SJ. Bruce Fields 
1192e8a79fb1SJ. Bruce Fields 	inode_lock(dir);
1193e8a79fb1SJ. Bruce Fields 	dentry = d_alloc_name(parent, name);
1194e8a79fb1SJ. Bruce Fields 	if (!dentry)
1195e8a79fb1SJ. Bruce Fields 		goto out_err;
1196e8a79fb1SJ. Bruce Fields 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600);
1197e8a79fb1SJ. Bruce Fields 	if (ret)
1198e8a79fb1SJ. Bruce Fields 		goto out_err;
1199e8a79fb1SJ. Bruce Fields 	if (ncl) {
1200e8a79fb1SJ. Bruce Fields 		d_inode(dentry)->i_private = ncl;
1201e8a79fb1SJ. Bruce Fields 		kref_get(&ncl->cl_ref);
1202e8a79fb1SJ. Bruce Fields 	}
1203e8a79fb1SJ. Bruce Fields out:
1204e8a79fb1SJ. Bruce Fields 	inode_unlock(dir);
1205e8a79fb1SJ. Bruce Fields 	return dentry;
1206e8a79fb1SJ. Bruce Fields out_err:
1207e8a79fb1SJ. Bruce Fields 	dentry = ERR_PTR(ret);
1208e8a79fb1SJ. Bruce Fields 	goto out;
1209e8a79fb1SJ. Bruce Fields }
1210e8a79fb1SJ. Bruce Fields 
121197ad4031SJ. Bruce Fields static void clear_ncl(struct inode *inode)
1212e8a79fb1SJ. Bruce Fields {
121397ad4031SJ. Bruce Fields 	struct nfsdfs_client *ncl = inode->i_private;
121497ad4031SJ. Bruce Fields 
121597ad4031SJ. Bruce Fields 	inode->i_private = NULL;
121697ad4031SJ. Bruce Fields 	synchronize_rcu();
121797ad4031SJ. Bruce Fields 	kref_put(&ncl->cl_ref, ncl->cl_release);
121897ad4031SJ. Bruce Fields }
121997ad4031SJ. Bruce Fields 
122097ad4031SJ. Bruce Fields 
1221b78fa45dSYueHaibing static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
122297ad4031SJ. Bruce Fields {
122397ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc = inode->i_private;
122497ad4031SJ. Bruce Fields 
122597ad4031SJ. Bruce Fields 	if (nc)
122697ad4031SJ. Bruce Fields 		kref_get(&nc->cl_ref);
122797ad4031SJ. Bruce Fields 	return nc;
122897ad4031SJ. Bruce Fields }
122997ad4031SJ. Bruce Fields 
123097ad4031SJ. Bruce Fields struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
123197ad4031SJ. Bruce Fields {
123297ad4031SJ. Bruce Fields 	struct nfsdfs_client *nc;
123397ad4031SJ. Bruce Fields 
123497ad4031SJ. Bruce Fields 	rcu_read_lock();
123597ad4031SJ. Bruce Fields 	nc = __get_nfsdfs_client(inode);
123697ad4031SJ. Bruce Fields 	rcu_read_unlock();
123797ad4031SJ. Bruce Fields 	return nc;
123897ad4031SJ. Bruce Fields }
123997ad4031SJ. Bruce Fields /* from __rpc_unlink */
124097ad4031SJ. Bruce Fields static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
124197ad4031SJ. Bruce Fields {
124297ad4031SJ. Bruce Fields 	int ret;
124397ad4031SJ. Bruce Fields 
124497ad4031SJ. Bruce Fields 	clear_ncl(d_inode(dentry));
124597ad4031SJ. Bruce Fields 	dget(dentry);
124697ad4031SJ. Bruce Fields 	ret = simple_unlink(dir, dentry);
124797ad4031SJ. Bruce Fields 	d_delete(dentry);
124897ad4031SJ. Bruce Fields 	dput(dentry);
124997ad4031SJ. Bruce Fields 	WARN_ON_ONCE(ret);
125097ad4031SJ. Bruce Fields }
125197ad4031SJ. Bruce Fields 
125297ad4031SJ. Bruce Fields static void nfsdfs_remove_files(struct dentry *root)
125397ad4031SJ. Bruce Fields {
125497ad4031SJ. Bruce Fields 	struct dentry *dentry, *tmp;
125597ad4031SJ. Bruce Fields 
125697ad4031SJ. Bruce Fields 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
125797ad4031SJ. Bruce Fields 		if (!simple_positive(dentry)) {
125897ad4031SJ. Bruce Fields 			WARN_ON_ONCE(1); /* I think this can't happen? */
125997ad4031SJ. Bruce Fields 			continue;
126097ad4031SJ. Bruce Fields 		}
126197ad4031SJ. Bruce Fields 		nfsdfs_remove_file(d_inode(root), dentry);
126297ad4031SJ. Bruce Fields 	}
126397ad4031SJ. Bruce Fields }
126497ad4031SJ. Bruce Fields 
126597ad4031SJ. Bruce Fields /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
126697ad4031SJ. Bruce Fields  * code instead. */
126797ad4031SJ. Bruce Fields static  int nfsdfs_create_files(struct dentry *root,
126897ad4031SJ. Bruce Fields 					const struct tree_descr *files)
126997ad4031SJ. Bruce Fields {
127097ad4031SJ. Bruce Fields 	struct inode *dir = d_inode(root);
127197ad4031SJ. Bruce Fields 	struct inode *inode;
127297ad4031SJ. Bruce Fields 	struct dentry *dentry;
127397ad4031SJ. Bruce Fields 	int i;
127497ad4031SJ. Bruce Fields 
127597ad4031SJ. Bruce Fields 	inode_lock(dir);
127697ad4031SJ. Bruce Fields 	for (i = 0; files->name && files->name[0]; i++, files++) {
127797ad4031SJ. Bruce Fields 		if (!files->name)
127897ad4031SJ. Bruce Fields 			continue;
127997ad4031SJ. Bruce Fields 		dentry = d_alloc_name(root, files->name);
128097ad4031SJ. Bruce Fields 		if (!dentry)
128197ad4031SJ. Bruce Fields 			goto out;
128297ad4031SJ. Bruce Fields 		inode = nfsd_get_inode(d_inode(root)->i_sb,
128397ad4031SJ. Bruce Fields 					S_IFREG | files->mode);
128497ad4031SJ. Bruce Fields 		if (!inode) {
128597ad4031SJ. Bruce Fields 			dput(dentry);
128697ad4031SJ. Bruce Fields 			goto out;
128797ad4031SJ. Bruce Fields 		}
128897ad4031SJ. Bruce Fields 		inode->i_fop = files->ops;
128997ad4031SJ. Bruce Fields 		inode->i_private = __get_nfsdfs_client(dir);
129097ad4031SJ. Bruce Fields 		d_add(dentry, inode);
129197ad4031SJ. Bruce Fields 		fsnotify_create(dir, dentry);
129297ad4031SJ. Bruce Fields 	}
129397ad4031SJ. Bruce Fields 	inode_unlock(dir);
129497ad4031SJ. Bruce Fields 	return 0;
129597ad4031SJ. Bruce Fields out:
129697ad4031SJ. Bruce Fields 	nfsdfs_remove_files(root);
129797ad4031SJ. Bruce Fields 	inode_unlock(dir);
129897ad4031SJ. Bruce Fields 	return -ENOMEM;
129997ad4031SJ. Bruce Fields }
130097ad4031SJ. Bruce Fields 
130197ad4031SJ. Bruce Fields /* on success, returns positive number unique to that client. */
130297ad4031SJ. Bruce Fields struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
130397ad4031SJ. Bruce Fields 		struct nfsdfs_client *ncl, u32 id,
130497ad4031SJ. Bruce Fields 		const struct tree_descr *files)
130597ad4031SJ. Bruce Fields {
130697ad4031SJ. Bruce Fields 	struct dentry *dentry;
1307e8a79fb1SJ. Bruce Fields 	char name[11];
130897ad4031SJ. Bruce Fields 	int ret;
1309e8a79fb1SJ. Bruce Fields 
1310bf5ed3e3SJ. Bruce Fields 	sprintf(name, "%u", id);
1311e8a79fb1SJ. Bruce Fields 
131297ad4031SJ. Bruce Fields 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
131397ad4031SJ. Bruce Fields 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
131497ad4031SJ. Bruce Fields 		return NULL;
131597ad4031SJ. Bruce Fields 	ret = nfsdfs_create_files(dentry, files);
131697ad4031SJ. Bruce Fields 	if (ret) {
131797ad4031SJ. Bruce Fields 		nfsd_client_rmdir(dentry);
131897ad4031SJ. Bruce Fields 		return NULL;
131997ad4031SJ. Bruce Fields 	}
132097ad4031SJ. Bruce Fields 	return dentry;
1321e8a79fb1SJ. Bruce Fields }
1322e8a79fb1SJ. Bruce Fields 
1323e8a79fb1SJ. Bruce Fields /* Taken from __rpc_rmdir: */
1324e8a79fb1SJ. Bruce Fields void nfsd_client_rmdir(struct dentry *dentry)
1325e8a79fb1SJ. Bruce Fields {
1326e8a79fb1SJ. Bruce Fields 	struct inode *dir = d_inode(dentry->d_parent);
1327e8a79fb1SJ. Bruce Fields 	struct inode *inode = d_inode(dentry);
1328e8a79fb1SJ. Bruce Fields 	int ret;
1329e8a79fb1SJ. Bruce Fields 
133097ad4031SJ. Bruce Fields 	inode_lock(dir);
133197ad4031SJ. Bruce Fields 	nfsdfs_remove_files(dentry);
133297ad4031SJ. Bruce Fields 	clear_ncl(inode);
1333e8a79fb1SJ. Bruce Fields 	dget(dentry);
1334e8a79fb1SJ. Bruce Fields 	ret = simple_rmdir(dir, dentry);
1335e8a79fb1SJ. Bruce Fields 	WARN_ON_ONCE(ret);
1336e8a79fb1SJ. Bruce Fields 	d_delete(dentry);
133797ad4031SJ. Bruce Fields 	inode_unlock(dir);
1338e8a79fb1SJ. Bruce Fields }
1339e8a79fb1SJ. Bruce Fields 
13401da177e4SLinus Torvalds static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
13411da177e4SLinus Torvalds {
1342e8a79fb1SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1343e8a79fb1SJ. Bruce Fields 							nfsd_net_id);
1344e8a79fb1SJ. Bruce Fields 	struct dentry *dentry;
1345e8a79fb1SJ. Bruce Fields 	int ret;
1346e8a79fb1SJ. Bruce Fields 
1347cda37124SEric Biggers 	static const struct tree_descr nfsd_files[] = {
134896d851c4SStanislav Kinsbursky 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1349e8e8753fSJ. Bruce Fields 		[NFSD_Export_features] = {"export_features",
1350e8e8753fSJ. Bruce Fields 					&export_features_operations, S_IRUGO},
13514373ea84SWendy Cheng 		[NFSD_FO_UnlockIP] = {"unlock_ip",
13524373ea84SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
135317efa372SWendy Cheng 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
135417efa372SWendy Cheng 					&transaction_ops, S_IWUSR|S_IRUSR},
13551da177e4SLinus Torvalds 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
13561da177e4SLinus Torvalds 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1357eed2965aSGreg Banks 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
135803cf6c9fSGreg Banks 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1359a2f999a3SJeff Layton 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO},
136070c3b76cSNeilBrown 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
136180212d59SNeilBrown 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1362596bbe53SNeilBrown 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
13635b8db00bSJeff Layton 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1364b084f598SJ. Bruce Fields #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
1365b0b0c0a2SKevin Coffman 		[NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
1366b084f598SJ. Bruce Fields #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
13671da177e4SLinus Torvalds #ifdef CONFIG_NFSD_V4
13681da177e4SLinus Torvalds 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1369efc4bb4fSJ. Bruce Fields 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
13700964a3d3SNeilBrown 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
13717f5ef2e9SJeff Layton 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
13721da177e4SLinus Torvalds #endif
13731da177e4SLinus Torvalds 		/* last one */ {""}
13741da177e4SLinus Torvalds 	};
1375d91ee87dSEric W. Biederman 	get_net(sb->s_fs_info);
1376e8a79fb1SJ. Bruce Fields 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1377e8a79fb1SJ. Bruce Fields 	if (ret)
1378e8a79fb1SJ. Bruce Fields 		return ret;
1379e8a79fb1SJ. Bruce Fields 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1380e8a79fb1SJ. Bruce Fields 	if (IS_ERR(dentry))
1381e8a79fb1SJ. Bruce Fields 		return PTR_ERR(dentry);
1382e8a79fb1SJ. Bruce Fields 	nn->nfsd_client_dir = dentry;
1383e8a79fb1SJ. Bruce Fields 	return 0;
1384e8a79fb1SJ. Bruce Fields 
13851da177e4SLinus Torvalds }
13861da177e4SLinus Torvalds 
1387fc14f2feSAl Viro static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1388fc14f2feSAl Viro 	int flags, const char *dev_name, void *data)
13891da177e4SLinus Torvalds {
1390d91ee87dSEric W. Biederman 	struct net *net = current->nsproxy->net_ns;
1391d91ee87dSEric W. Biederman 	return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super);
139211f77942SStanislav Kinsbursky }
139311f77942SStanislav Kinsbursky 
139411f77942SStanislav Kinsbursky static void nfsd_umount(struct super_block *sb)
139511f77942SStanislav Kinsbursky {
139611f77942SStanislav Kinsbursky 	struct net *net = sb->s_fs_info;
139711f77942SStanislav Kinsbursky 
139811f77942SStanislav Kinsbursky 	kill_litter_super(sb);
139911f77942SStanislav Kinsbursky 	put_net(net);
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds static struct file_system_type nfsd_fs_type = {
14031da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
14041da177e4SLinus Torvalds 	.name		= "nfsd",
1405fc14f2feSAl Viro 	.mount		= nfsd_mount,
140611f77942SStanislav Kinsbursky 	.kill_sb	= nfsd_umount,
14071da177e4SLinus Torvalds };
14087f78e035SEric W. Biederman MODULE_ALIAS_FS("nfsd");
14091da177e4SLinus Torvalds 
1410e331f606SJ. Bruce Fields #ifdef CONFIG_PROC_FS
1411e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1412e331f606SJ. Bruce Fields {
1413e331f606SJ. Bruce Fields 	struct proc_dir_entry *entry;
1414e331f606SJ. Bruce Fields 
1415e331f606SJ. Bruce Fields 	entry = proc_mkdir("fs/nfs", NULL);
1416e331f606SJ. Bruce Fields 	if (!entry)
1417e331f606SJ. Bruce Fields 		return -ENOMEM;
141896d851c4SStanislav Kinsbursky 	entry = proc_create("exports", 0, entry,
141996d851c4SStanislav Kinsbursky 				 &exports_proc_operations);
1420ff7c4b36Sfanchaoting 	if (!entry) {
1421ff7c4b36Sfanchaoting 		remove_proc_entry("fs/nfs", NULL);
1422e331f606SJ. Bruce Fields 		return -ENOMEM;
1423ff7c4b36Sfanchaoting 	}
1424e331f606SJ. Bruce Fields 	return 0;
1425e331f606SJ. Bruce Fields }
1426e331f606SJ. Bruce Fields #else /* CONFIG_PROC_FS */
1427e331f606SJ. Bruce Fields static int create_proc_exports_entry(void)
1428e331f606SJ. Bruce Fields {
1429e331f606SJ. Bruce Fields 	return 0;
1430e331f606SJ. Bruce Fields }
1431e331f606SJ. Bruce Fields #endif
1432e331f606SJ. Bruce Fields 
1433c7d03a00SAlexey Dobriyan unsigned int nfsd_net_id;
14345717e012SStanislav Kinsbursky 
14355717e012SStanislav Kinsbursky static __net_init int nfsd_init_net(struct net *net)
14365717e012SStanislav Kinsbursky {
14375717e012SStanislav Kinsbursky 	int retval;
14382c830dd7SJ. Bruce Fields 	struct vfsmount *mnt;
14393d733711SStanislav Kinsbursky 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14405717e012SStanislav Kinsbursky 
14415717e012SStanislav Kinsbursky 	retval = nfsd_export_init(net);
14425717e012SStanislav Kinsbursky 	if (retval)
14435717e012SStanislav Kinsbursky 		goto out_export_error;
1444f69adb2fSStanislav Kinsbursky 	retval = nfsd_idmap_init(net);
1445f69adb2fSStanislav Kinsbursky 	if (retval)
1446f69adb2fSStanislav Kinsbursky 		goto out_idmap_error;
1447e333f3bbSTrond Myklebust 	nn->nfsd_versions = NULL;
1448e333f3bbSTrond Myklebust 	nn->nfsd4_minorversions = NULL;
14493ba75830SJ. Bruce Fields 	retval = nfsd_reply_cache_init(nn);
14503ba75830SJ. Bruce Fields 	if (retval)
14513ba75830SJ. Bruce Fields 		goto out_drc_error;
14523bf6b57eSJ. Bruce Fields 	nn->nfsd4_lease = 90;	/* default lease time */
14533bf6b57eSJ. Bruce Fields 	nn->nfsd4_grace = 90;
145403f318caSJ. Bruce Fields 	nn->somebody_reclaimed = false;
1455362063a5SScott Mayhew 	nn->track_reclaim_completes = false;
1456ebd7c72cSJ. Bruce Fields 	nn->clverifier_counter = prandom_u32();
1457bf5ed3e3SJ. Bruce Fields 	nn->clientid_base = prandom_u32();
1458bf5ed3e3SJ. Bruce Fields 	nn->clientid_counter = nn->clientid_base + 1;
1459e0639dc5SOlga Kornievskaia 	nn->s2s_cp_cl_id = nn->clientid_counter++;
14602317dc55SVasily Averin 
14612317dc55SVasily Averin 	atomic_set(&nn->ntf_refcnt, 0);
14622317dc55SVasily Averin 	init_waitqueue_head(&nn->ntf_wq);
14632c830dd7SJ. Bruce Fields 
14642c830dd7SJ. Bruce Fields 	mnt =  vfs_kern_mount(&nfsd_fs_type, SB_KERNMOUNT, "nfsd", NULL);
14652c830dd7SJ. Bruce Fields 	if (IS_ERR(mnt)) {
14662c830dd7SJ. Bruce Fields 		retval = PTR_ERR(mnt);
14672c830dd7SJ. Bruce Fields 		goto out_mount_err;
14682c830dd7SJ. Bruce Fields 	}
14692c830dd7SJ. Bruce Fields 	nn->nfsd_mnt = mnt;
14705717e012SStanislav Kinsbursky 	return 0;
14715717e012SStanislav Kinsbursky 
14722c830dd7SJ. Bruce Fields out_mount_err:
14732c830dd7SJ. Bruce Fields 	nfsd_reply_cache_shutdown(nn);
14743ba75830SJ. Bruce Fields out_drc_error:
14753ba75830SJ. Bruce Fields 	nfsd_idmap_shutdown(net);
1476f69adb2fSStanislav Kinsbursky out_idmap_error:
1477f69adb2fSStanislav Kinsbursky 	nfsd_export_shutdown(net);
14785717e012SStanislav Kinsbursky out_export_error:
14795717e012SStanislav Kinsbursky 	return retval;
14805717e012SStanislav Kinsbursky }
14815717e012SStanislav Kinsbursky 
14825717e012SStanislav Kinsbursky static __net_exit void nfsd_exit_net(struct net *net)
14835717e012SStanislav Kinsbursky {
14843ba75830SJ. Bruce Fields 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
14853ba75830SJ. Bruce Fields 
14862c830dd7SJ. Bruce Fields 	mntput(nn->nfsd_mnt);
14873ba75830SJ. Bruce Fields 	nfsd_reply_cache_shutdown(nn);
1488f69adb2fSStanislav Kinsbursky 	nfsd_idmap_shutdown(net);
14895717e012SStanislav Kinsbursky 	nfsd_export_shutdown(net);
1490e333f3bbSTrond Myklebust 	nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
14915717e012SStanislav Kinsbursky }
14925717e012SStanislav Kinsbursky 
14937ea34ac1SJeff Layton static struct pernet_operations nfsd_net_ops = {
14945717e012SStanislav Kinsbursky 	.init = nfsd_init_net,
14955717e012SStanislav Kinsbursky 	.exit = nfsd_exit_net,
14967ea34ac1SJeff Layton 	.id   = &nfsd_net_id,
14977ea34ac1SJeff Layton 	.size = sizeof(struct nfsd_net),
14987ea34ac1SJeff Layton };
14997ea34ac1SJeff Layton 
15001da177e4SLinus Torvalds static int __init init_nfsd(void)
15011da177e4SLinus Torvalds {
15021da177e4SLinus Torvalds 	int retval;
15031da177e4SLinus Torvalds 	printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
15041da177e4SLinus Torvalds 
15057ea34ac1SJeff Layton 	retval = register_pernet_subsys(&nfsd_net_ops);
15067ea34ac1SJeff Layton 	if (retval < 0)
1507bb7ffbf2SGiuseppe Cantavenera 		return retval;
1508bb7ffbf2SGiuseppe Cantavenera 	retval = register_cld_notifier();
1509e8ff2a84SJ. Bruce Fields 	if (retval)
15107ea34ac1SJeff Layton 		goto out_unregister_pernet;
1511bb7ffbf2SGiuseppe Cantavenera 	retval = nfsd4_init_slabs();
1512bb7ffbf2SGiuseppe Cantavenera 	if (retval)
1513bb7ffbf2SGiuseppe Cantavenera 		goto out_unregister_notifier;
15149cf514ccSChristoph Hellwig 	retval = nfsd4_init_pnfs();
151565178db4SBryan Schumaker 	if (retval)
151665178db4SBryan Schumaker 		goto out_free_slabs;
15179cf514ccSChristoph Hellwig 	retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */
15189cf514ccSChristoph Hellwig 	if (retval)
15199cf514ccSChristoph Hellwig 		goto out_exit_pnfs;
15201da177e4SLinus Torvalds 	nfsd_stat_init();	/* Statistics */
15211da177e4SLinus Torvalds 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1522e331f606SJ. Bruce Fields 	retval = create_proc_exports_entry();
1523e331f606SJ. Bruce Fields 	if (retval)
1524f69adb2fSStanislav Kinsbursky 		goto out_free_lockd;
15251da177e4SLinus Torvalds 	retval = register_filesystem(&nfsd_fs_type);
152626808d3fSJ. Bruce Fields 	if (retval)
152726808d3fSJ. Bruce Fields 		goto out_free_all;
152826808d3fSJ. Bruce Fields 	return 0;
152926808d3fSJ. Bruce Fields out_free_all:
15301da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15311da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
1532dbf847ecSJ. Bruce Fields out_free_lockd:
15331da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1534d5c3428bSJ. Bruce Fields 	nfsd_stat_shutdown();
153565178db4SBryan Schumaker 	nfsd_fault_inject_cleanup();
15369cf514ccSChristoph Hellwig out_exit_pnfs:
15379cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
153865178db4SBryan Schumaker out_free_slabs:
153946b25895SJ. Bruce Fields 	nfsd4_free_slabs();
1540813fd320SJeff Layton out_unregister_notifier:
1541797a9d79SJeff Layton 	unregister_cld_notifier();
1542bb7ffbf2SGiuseppe Cantavenera out_unregister_pernet:
1543bb7ffbf2SGiuseppe Cantavenera 	unregister_pernet_subsys(&nfsd_net_ops);
15441da177e4SLinus Torvalds 	return retval;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds static void __exit exit_nfsd(void)
15481da177e4SLinus Torvalds {
15491da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs/exports", NULL);
15501da177e4SLinus Torvalds 	remove_proc_entry("fs/nfs", NULL);
15511da177e4SLinus Torvalds 	nfsd_stat_shutdown();
15521da177e4SLinus Torvalds 	nfsd_lockd_shutdown();
1553e8ff2a84SJ. Bruce Fields 	nfsd4_free_slabs();
15549cf514ccSChristoph Hellwig 	nfsd4_exit_pnfs();
155565178db4SBryan Schumaker 	nfsd_fault_inject_cleanup();
15561da177e4SLinus Torvalds 	unregister_filesystem(&nfsd_fs_type);
1557797a9d79SJeff Layton 	unregister_cld_notifier();
1558bb7ffbf2SGiuseppe Cantavenera 	unregister_pernet_subsys(&nfsd_net_ops);
15591da177e4SLinus Torvalds }
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
15621da177e4SLinus Torvalds MODULE_LICENSE("GPL");
15631da177e4SLinus Torvalds module_init(init_nfsd)
15641da177e4SLinus Torvalds module_exit(exit_nfsd)
1565