1 // SPDX-License-Identifier: GPL-2.0-only
3 * Syscall interface to knfsd.
5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/fs_context.h>
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/rpc_pipe_fs.h>
18 #include <linux/sunrpc/svc.h>
19 #include <linux/module.h>
20 #include <linux/fsnotify.h>
21 #include <linux/nfslocalio.h>
29 #include "filecache.h"
34 * We have a single directory with several nodes in it.
47 NFSD_Reply_Cache_Stats,
60 * write() for these nodes.
62 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
63 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
64 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
65 static ssize_t write_threads(struct file *file, char *buf, size_t size);
66 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
67 static ssize_t write_versions(struct file *file, char *buf, size_t size);
68 static ssize_t write_ports(struct file *file, char *buf, size_t size);
69 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
71 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
72 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
73 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
74 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
76 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
79 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
80 [NFSD_Fh] = write_filehandle,
81 [NFSD_FO_UnlockIP] = write_unlock_ip,
82 [NFSD_FO_UnlockFS] = write_unlock_fs,
83 [NFSD_Threads] = write_threads,
84 [NFSD_Pool_Threads] = write_pool_threads,
85 [NFSD_Versions] = write_versions,
86 [NFSD_Ports] = write_ports,
87 [NFSD_MaxBlkSize] = write_maxblksize,
89 [NFSD_Leasetime] = write_leasetime,
90 [NFSD_Gracetime] = write_gracetime,
91 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
92 [NFSD_RecoveryDir] = write_recoverydir,
94 [NFSD_V4EndGrace] = write_v4_end_grace,
98 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
100 ino_t ino = file_inode(file)->i_ino;
104 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
107 data = simple_transaction_get(file, buf, size);
109 return PTR_ERR(data);
111 rv = write_op[ino](file, data, size);
115 simple_transaction_set(file, rv);
119 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
121 if (! file->private_data) {
122 /* An attempt to read a transaction file without writing
123 * causes a 0-byte write so that the file can return
126 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
130 return simple_transaction_read(file, buf, size, pos);
133 static const struct file_operations transaction_ops = {
134 .write = nfsctl_transaction_write,
135 .read = nfsctl_transaction_read,
136 .release = simple_transaction_release,
137 .llseek = default_llseek,
140 static int exports_net_open(struct net *net, struct file *file)
143 struct seq_file *seq;
144 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
146 err = seq_open(file, &nfs_exports_op);
150 seq = file->private_data;
151 seq->private = nn->svc_export_cache;
155 static int exports_nfsd_open(struct inode *inode, struct file *file)
157 return exports_net_open(inode->i_sb->s_fs_info, file);
160 static const struct file_operations exports_nfsd_operations = {
161 .open = exports_nfsd_open,
164 .release = seq_release,
167 static int export_features_show(struct seq_file *m, void *v)
169 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
173 DEFINE_SHOW_ATTRIBUTE(export_features);
175 static int nfsd_pool_stats_open(struct inode *inode, struct file *file)
177 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
179 return svc_pool_stats_open(&nn->nfsd_info, file);
182 static const struct file_operations pool_stats_operations = {
183 .open = nfsd_pool_stats_open,
186 .release = seq_release,
189 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
191 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
193 /*----------------------------------------------------------------------------*/
195 * payload - write methods
198 static inline struct net *netns(struct file *file)
200 return file_inode(file)->i_sb->s_fs_info;
204 * write_unlock_ip - Release all locks used by a client
209 * buf: '\n'-terminated C string containing a
210 * presentation format IP address
211 * size: length of C string in @buf
213 * On success: returns zero if all specified locks were released;
214 * returns one if one or more locks were not released
215 * On error: return code is negative errno value
217 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
219 struct sockaddr_storage address;
220 struct sockaddr *sap = (struct sockaddr *)&address;
221 size_t salen = sizeof(address);
223 struct net *net = netns(file);
229 if (buf[size-1] != '\n')
233 if (qword_get(&buf, fo_path, size) < 0)
236 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
239 trace_nfsd_ctl_unlock_ip(net, buf);
240 return nlmsvc_unlock_all_by_ip(sap);
244 * write_unlock_fs - Release all locks on a local file system
249 * buf: '\n'-terminated C string containing the
250 * absolute pathname of a local file system
251 * size: length of C string in @buf
253 * On success: returns zero if all specified locks were released;
254 * returns one if one or more locks were not released
255 * On error: return code is negative errno value
257 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
267 if (buf[size-1] != '\n')
271 if (qword_get(&buf, fo_path, size) < 0)
273 trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
274 error = kern_path(fo_path, 0, &path);
279 * XXX: Needs better sanity checking. Otherwise we could end up
280 * releasing locks on the wrong file system.
283 * 1. Does the path refer to a directory?
284 * 2. Is that directory a mount point, or
285 * 3. Is that directory the root of an exported file system?
287 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
288 nfsd4_revoke_states(netns(file), path.dentry->d_sb);
295 * write_filehandle - Get a variable-length NFS file handle by path
297 * On input, the buffer contains a '\n'-terminated C string comprised of
298 * three alphanumeric words separated by whitespace. The string may
299 * contain escape sequences.
303 * domain: client domain name
304 * path: export pathname
305 * maxsize: numeric maximum size of
307 * size: length of C string in @buf
309 * On success: passed-in buffer filled with '\n'-terminated C
310 * string containing a ASCII hex text version
311 * of the NFS file handle;
312 * return code is the size in bytes of the string
313 * On error: return code is negative errno value
315 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
321 struct auth_domain *dom;
327 if (buf[size-1] != '\n')
332 len = qword_get(&mesg, dname, size);
337 len = qword_get(&mesg, path, size);
341 len = get_int(&mesg, &maxsize);
345 if (maxsize < NFS_FHSIZE)
347 maxsize = min(maxsize, NFS3_FHSIZE);
349 if (qword_get(&mesg, mesg, size) > 0)
352 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
354 /* we have all the words, they are in buf.. */
355 dom = unix_domain_find(dname);
359 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
360 auth_domain_put(dom);
365 len = SIMPLE_TRANSACTION_LIMIT;
366 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
372 * write_threads - Start NFSD, or report the current number of running threads
378 * On success: passed-in buffer filled with '\n'-terminated C
379 * string numeric value representing the number of
380 * running NFSD threads;
381 * return code is the size in bytes of the string
382 * On error: return code is zero
387 * buf: C string containing an unsigned
388 * integer value representing the
389 * number of NFSD threads to start
390 * size: non-zero length of C string in @buf
392 * On success: NFS service is started;
393 * passed-in buffer filled with '\n'-terminated C
394 * string numeric value representing the number of
395 * running NFSD threads;
396 * return code is the size in bytes of the string
397 * On error: return code is zero or a negative errno value
399 static ssize_t write_threads(struct file *file, char *buf, size_t size)
403 struct net *net = netns(file);
407 rv = get_int(&mesg, &newthreads);
412 trace_nfsd_ctl_threads(net, newthreads);
413 mutex_lock(&nfsd_mutex);
414 rv = nfsd_svc(1, &newthreads, net, file->f_cred, NULL);
415 mutex_unlock(&nfsd_mutex);
419 rv = nfsd_nrthreads(net);
421 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
425 * write_pool_threads - Set or report the current number of threads per pool
434 * buf: C string containing whitespace-
435 * separated unsigned integer values
436 * representing the number of NFSD
437 * threads to start in each pool
438 * size: non-zero length of C string in @buf
440 * On success: passed-in buffer filled with '\n'-terminated C
441 * string containing integer values representing the
442 * number of NFSD threads in each pool;
443 * return code is the size in bytes of the string
444 * On error: return code is zero or a negative errno value
446 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
448 /* if size > 0, look for an array of number of threads per node
449 * and apply them then write out number of threads per node as reply
457 struct net *net = netns(file);
459 mutex_lock(&nfsd_mutex);
460 npools = nfsd_nrpools(net);
463 * NFS is shut down. The admin can start it by
464 * writing to the threads file but NOT the pool_threads
465 * file, sorry. Report zero threads.
467 mutex_unlock(&nfsd_mutex);
472 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
474 if (nthreads == NULL)
478 for (i = 0; i < npools; i++) {
479 rv = get_int(&mesg, &nthreads[i]);
481 break; /* fewer numbers than pools */
483 goto out_free; /* syntax error */
487 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
491 * There must always be a thread in pool 0; the admin
492 * can't shut down NFS completely using pool_threads.
494 if (nthreads[0] == 0)
497 rv = nfsd_set_nrthreads(i, nthreads, net);
502 rv = nfsd_get_nrthreads(npools, nthreads, net);
507 size = SIMPLE_TRANSACTION_LIMIT;
508 for (i = 0; i < npools && size > 0; i++) {
509 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
517 mutex_unlock(&nfsd_mutex);
522 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
523 const char *sep, unsigned vers, int minor)
525 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
526 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
528 if (vers == 4 && minor >= 0 &&
529 !nfsd_minorversion(nn, minor, NFSD_TEST))
531 if (minor == 0 && supported)
533 * special case for backward compatability.
534 * +4.0 is never reported, it is implied by
535 * +4, unless -4.0 is present.
538 return snprintf(buf, remaining, format, sep,
539 supported ? '+' : '-', vers, minor);
542 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
545 char *vers, *minorp, sign;
546 int len, num, remaining;
549 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
553 /* Cannot change versions without updating
554 * nn->nfsd_serv->sv_xdrsize, and reallocing
555 * rq_argp and rq_resp
558 if (buf[size-1] != '\n')
561 trace_nfsd_ctl_version(netns(file), buf);
564 len = qword_get(&mesg, vers, size);
565 if (len <= 0) return -EINVAL;
570 if (sign == '+' || sign == '-')
571 num = simple_strtol((vers+1), &minorp, 0);
573 num = simple_strtol(vers, &minorp, 0);
574 if (*minorp == '.') {
577 if (kstrtouint(minorp+1, 0, &minor) < 0)
581 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
583 #ifdef CONFIG_NFSD_V2
587 nfsd_vers(nn, num, cmd);
590 if (*minorp == '.') {
591 if (nfsd_minorversion(nn, minor, cmd) < 0)
593 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
595 * Either we have +4 and no minors are enabled,
596 * or we have -4 and at least one minor is enabled.
597 * In either case, propagate 'cmd' to all minors.
600 while (nfsd_minorversion(nn, minor, cmd) >= 0)
605 /* Ignore requests to disable non-existent versions */
610 } while ((len = qword_get(&mesg, vers, size)) > 0);
611 /* If all get turned off, turn them back on, as
612 * having no versions is BAD
614 nfsd_reset_versions(nn);
617 /* Now write current state into reply buffer */
619 remaining = SIMPLE_TRANSACTION_LIMIT;
620 for (num=2 ; num <= 4 ; num++) {
622 if (!nfsd_vers(nn, num, NFSD_AVAIL))
627 len = nfsd_print_version_support(nn, buf, remaining,
629 if (len >= remaining)
637 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
640 len = snprintf(buf, remaining, "\n");
641 if (len >= remaining)
647 * write_versions - Set or report the available NFS protocol versions
653 * On success: passed-in buffer filled with '\n'-terminated C
654 * string containing positive or negative integer
655 * values representing the current status of each
657 * return code is the size in bytes of the string
658 * On error: return code is zero or a negative errno value
663 * buf: C string containing whitespace-
664 * separated positive or negative
665 * integer values representing NFS
666 * protocol versions to enable ("+n")
668 * size: non-zero length of C string in @buf
670 * On success: status of zero or more protocol versions has
671 * been updated; passed-in buffer filled with
672 * '\n'-terminated C string containing positive
673 * or negative integer values representing the
674 * current status of each protocol version;
675 * return code is the size in bytes of the string
676 * On error: return code is zero or a negative errno value
678 static ssize_t write_versions(struct file *file, char *buf, size_t size)
682 mutex_lock(&nfsd_mutex);
683 rv = __write_versions(file, buf, size);
684 mutex_unlock(&nfsd_mutex);
689 * Zero-length write. Return a list of NFSD's current listener
692 static ssize_t __write_ports_names(char *buf, struct net *net)
694 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
696 if (nn->nfsd_serv == NULL)
698 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
702 * A single 'fd' number was written, in which case it must be for
703 * a socket of a supported family/protocol, and we use it as an
706 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
710 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
711 struct svc_serv *serv;
713 err = get_int(&mesg, &fd);
714 if (err != 0 || fd < 0)
716 trace_nfsd_ctl_ports_addfd(net, fd);
718 err = nfsd_create_serv(net);
722 serv = nn->nfsd_serv;
723 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
725 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
726 nfsd_destroy_serv(net);
732 * A transport listener is added by writing its transport name and
735 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
738 struct svc_xprt *xprt;
740 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
741 struct svc_serv *serv;
743 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
746 if (port < 1 || port > USHRT_MAX)
748 trace_nfsd_ctl_ports_addxprt(net, transport, port);
750 err = nfsd_create_serv(net);
754 serv = nn->nfsd_serv;
755 err = svc_xprt_create(serv, transport, net,
756 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
760 err = svc_xprt_create(serv, transport, net,
761 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
762 if (err < 0 && err != -EAFNOSUPPORT)
767 xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
769 svc_xprt_close(xprt);
773 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
774 nfsd_destroy_serv(net);
779 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
783 return __write_ports_names(buf, net);
786 return __write_ports_addfd(buf, net, file->f_cred);
789 return __write_ports_addxprt(buf, net, file->f_cred);
795 * write_ports - Pass a socket file descriptor or transport name to listen on
801 * On success: passed-in buffer filled with a '\n'-terminated C
802 * string containing a whitespace-separated list of
803 * named NFSD listeners;
804 * return code is the size in bytes of the string
805 * On error: return code is zero or a negative errno value
810 * buf: C string containing an unsigned
811 * integer value representing a bound
812 * but unconnected socket that is to be
813 * used as an NFSD listener; listen(3)
814 * must be called for a SOCK_STREAM
815 * socket, otherwise it is ignored
816 * size: non-zero length of C string in @buf
818 * On success: NFS service is started;
819 * passed-in buffer filled with a '\n'-terminated C
820 * string containing a unique alphanumeric name of
822 * return code is the size in bytes of the string
823 * On error: return code is a negative errno value
828 * buf: C string containing a transport
829 * name and an unsigned integer value
830 * representing the port to listen on,
831 * separated by whitespace
832 * size: non-zero length of C string in @buf
834 * On success: returns zero; NFS service is started
835 * On error: return code is a negative errno value
837 static ssize_t write_ports(struct file *file, char *buf, size_t size)
841 mutex_lock(&nfsd_mutex);
842 rv = __write_ports(file, buf, size, netns(file));
843 mutex_unlock(&nfsd_mutex);
848 int nfsd_max_blksize;
851 * write_maxblksize - Set or report the current NFS blksize
860 * buf: C string containing an unsigned
861 * integer value representing the new
863 * size: non-zero length of C string in @buf
865 * On success: passed-in buffer filled with '\n'-terminated C string
866 * containing numeric value of the current NFS blksize
868 * return code is the size in bytes of the string
869 * On error: return code is zero or a negative errno value
871 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
874 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
878 int rv = get_int(&mesg, &bsize);
881 trace_nfsd_ctl_maxblksize(netns(file), bsize);
883 /* force bsize into allowed range and
884 * required alignment.
886 bsize = max_t(int, bsize, 1024);
887 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
889 mutex_lock(&nfsd_mutex);
891 mutex_unlock(&nfsd_mutex);
894 nfsd_max_blksize = bsize;
895 mutex_unlock(&nfsd_mutex);
898 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
902 #ifdef CONFIG_NFSD_V4
903 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
904 time64_t *time, struct nfsd_net *nn)
906 struct dentry *dentry = file_dentry(file);
913 rv = get_int(&mesg, &i);
916 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
917 dentry->d_name.len, i);
920 * Some sanity checking. We don't have a reason for
921 * these particular numbers, but problems with the
923 * - Too short: the briefest network outage may
924 * cause clients to lose all their locks. Also,
925 * the frequent polling may be wasteful.
926 * - Too long: do you really want reboot recovery
927 * to take more than an hour? Or to make other
928 * clients wait an hour before being able to
929 * revoke a dead client's locks?
931 if (i < 10 || i > 3600)
936 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
939 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
940 time64_t *time, struct nfsd_net *nn)
944 mutex_lock(&nfsd_mutex);
945 rv = __nfsd4_write_time(file, buf, size, time, nn);
946 mutex_unlock(&nfsd_mutex);
951 * write_leasetime - Set or report the current NFSv4 lease time
960 * buf: C string containing an unsigned
961 * integer value representing the new
962 * NFSv4 lease expiry time
963 * size: non-zero length of C string in @buf
965 * On success: passed-in buffer filled with '\n'-terminated C
966 * string containing unsigned integer value of the
967 * current lease expiry time;
968 * return code is the size in bytes of the string
969 * On error: return code is zero or a negative errno value
971 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
973 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
974 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
978 * write_gracetime - Set or report current NFSv4 grace period time
980 * As above, but sets the time of the NFSv4 grace period.
982 * Note this should never be set to less than the *previous*
983 * lease-period time, but we don't try to enforce this. (In the common
984 * case (a new boot), we don't know what the previous lease time was
987 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
989 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
990 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
993 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
994 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1004 if (size > PATH_MAX || buf[size-1] != '\n')
1009 len = qword_get(&mesg, recdir, size);
1012 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1014 status = nfs4_reset_recoverydir(recdir);
1019 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1020 nfs4_recoverydir());
1024 * write_recoverydir - Set or report the pathname of the recovery directory
1033 * buf: C string containing the pathname
1034 * of the directory on a local file
1035 * system containing permanent NFSv4
1037 * size: non-zero length of C string in @buf
1039 * On success: passed-in buffer filled with '\n'-terminated C string
1040 * containing the current recovery pathname setting;
1041 * return code is the size in bytes of the string
1042 * On error: return code is zero or a negative errno value
1044 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1047 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1049 mutex_lock(&nfsd_mutex);
1050 rv = __write_recoverydir(file, buf, size, nn);
1051 mutex_unlock(&nfsd_mutex);
1057 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1066 * size: non-zero length of C string in @buf
1068 * passed-in buffer filled with "Y" or "N" with a newline
1069 * and NULL-terminated C string. This indicates whether
1070 * the grace period has ended in the current net
1071 * namespace. Return code is the size in bytes of the
1072 * string. Writing a string that starts with 'Y', 'y', or
1073 * '1' to the file will end the grace period for nfsd's v4
1076 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1078 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1087 trace_nfsd_end_grace(netns(file));
1088 nfsd4_end_grace(nn);
1095 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1096 nn->grace_ended ? 'Y' : 'N');
1101 /*----------------------------------------------------------------------------*/
1103 * populating the filesystem.
1106 /* Basically copying rpc_get_inode. */
1107 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1109 struct inode *inode = new_inode(sb);
1112 /* Following advice from simple_fill_super documentation: */
1113 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1114 inode->i_mode = mode;
1115 simple_inode_init_ts(inode);
1116 switch (mode & S_IFMT) {
1118 inode->i_fop = &simple_dir_operations;
1119 inode->i_op = &simple_dir_inode_operations;
1123 inode->i_op = &simple_symlink_inode_operations;
1131 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1133 struct inode *inode;
1135 inode = nfsd_get_inode(dir->i_sb, mode);
1139 inode->i_private = ncl;
1140 kref_get(&ncl->cl_ref);
1142 d_add(dentry, inode);
1144 fsnotify_mkdir(dir, dentry);
1148 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1150 struct inode *dir = parent->d_inode;
1151 struct dentry *dentry;
1155 dentry = d_alloc_name(parent, name);
1158 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1166 dentry = ERR_PTR(ret);
1170 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1171 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1172 umode_t mode, const char *content)
1174 struct inode *inode;
1176 inode = nfsd_get_inode(dir->i_sb, mode);
1180 inode->i_link = (char *)content;
1181 inode->i_size = strlen(content);
1183 d_add(dentry, inode);
1185 fsnotify_create(dir, dentry);
1190 * @content is assumed to be a NUL-terminated string that lives
1191 * longer than the symlink itself.
1193 static void _nfsd_symlink(struct dentry *parent, const char *name,
1194 const char *content)
1196 struct inode *dir = parent->d_inode;
1197 struct dentry *dentry;
1201 dentry = d_alloc_name(parent, name);
1204 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1211 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1212 const char *content)
1218 static void clear_ncl(struct dentry *dentry)
1220 struct inode *inode = d_inode(dentry);
1221 struct nfsdfs_client *ncl = inode->i_private;
1223 spin_lock(&inode->i_lock);
1224 inode->i_private = NULL;
1225 spin_unlock(&inode->i_lock);
1226 kref_put(&ncl->cl_ref, ncl->cl_release);
1229 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1231 struct nfsdfs_client *nc;
1233 spin_lock(&inode->i_lock);
1234 nc = inode->i_private;
1236 kref_get(&nc->cl_ref);
1237 spin_unlock(&inode->i_lock);
1241 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1243 static int nfsdfs_create_files(struct dentry *root,
1244 const struct tree_descr *files,
1245 struct nfsdfs_client *ncl,
1246 struct dentry **fdentries)
1248 struct inode *dir = d_inode(root);
1249 struct inode *inode;
1250 struct dentry *dentry;
1254 for (i = 0; files->name && files->name[0]; i++, files++) {
1255 dentry = d_alloc_name(root, files->name);
1258 inode = nfsd_get_inode(d_inode(root)->i_sb,
1259 S_IFREG | files->mode);
1264 kref_get(&ncl->cl_ref);
1265 inode->i_fop = files->ops;
1266 inode->i_private = ncl;
1267 d_add(dentry, inode);
1268 fsnotify_create(dir, dentry);
1270 fdentries[i] = dentry;
1279 /* on success, returns positive number unique to that client. */
1280 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1281 struct nfsdfs_client *ncl, u32 id,
1282 const struct tree_descr *files,
1283 struct dentry **fdentries)
1285 struct dentry *dentry;
1289 sprintf(name, "%u", id);
1291 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1292 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1294 ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1296 nfsd_client_rmdir(dentry);
1302 /* Taken from __rpc_rmdir: */
1303 void nfsd_client_rmdir(struct dentry *dentry)
1305 simple_recursive_removal(dentry, clear_ncl);
1308 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1310 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1312 struct dentry *dentry;
1315 static const struct tree_descr nfsd_files[] = {
1316 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1317 /* Per-export io stats use same ops as exports file */
1318 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1319 [NFSD_Export_features] = {"export_features",
1320 &export_features_fops, S_IRUGO},
1321 [NFSD_FO_UnlockIP] = {"unlock_ip",
1322 &transaction_ops, S_IWUSR|S_IRUSR},
1323 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1324 &transaction_ops, S_IWUSR|S_IRUSR},
1325 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1326 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1327 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1328 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1329 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1330 &nfsd_reply_cache_stats_fops, S_IRUGO},
1331 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1332 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1333 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1334 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1335 #ifdef CONFIG_NFSD_V4
1336 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1337 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1338 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1339 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1341 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1346 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1349 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1350 "/proc/net/rpc/gss_krb5_enctypes");
1351 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1353 return PTR_ERR(dentry);
1354 nn->nfsd_client_dir = dentry;
1358 static int nfsd_fs_get_tree(struct fs_context *fc)
1360 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1363 static void nfsd_fs_free_fc(struct fs_context *fc)
1366 put_net(fc->s_fs_info);
1369 static const struct fs_context_operations nfsd_fs_context_ops = {
1370 .free = nfsd_fs_free_fc,
1371 .get_tree = nfsd_fs_get_tree,
1374 static int nfsd_init_fs_context(struct fs_context *fc)
1376 put_user_ns(fc->user_ns);
1377 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1378 fc->ops = &nfsd_fs_context_ops;
1382 static void nfsd_umount(struct super_block *sb)
1384 struct net *net = sb->s_fs_info;
1386 nfsd_shutdown_threads(net);
1388 kill_litter_super(sb);
1392 static struct file_system_type nfsd_fs_type = {
1393 .owner = THIS_MODULE,
1395 .init_fs_context = nfsd_init_fs_context,
1396 .kill_sb = nfsd_umount,
1398 MODULE_ALIAS_FS("nfsd");
1400 #ifdef CONFIG_PROC_FS
1402 static int exports_proc_open(struct inode *inode, struct file *file)
1404 return exports_net_open(current->nsproxy->net_ns, file);
1407 static const struct proc_ops exports_proc_ops = {
1408 .proc_open = exports_proc_open,
1409 .proc_read = seq_read,
1410 .proc_lseek = seq_lseek,
1411 .proc_release = seq_release,
1414 static int create_proc_exports_entry(void)
1416 struct proc_dir_entry *entry;
1418 entry = proc_mkdir("fs/nfs", NULL);
1421 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1423 remove_proc_entry("fs/nfs", NULL);
1428 #else /* CONFIG_PROC_FS */
1429 static int create_proc_exports_entry(void)
1435 unsigned int nfsd_net_id;
1437 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1438 struct netlink_callback *cb,
1439 struct nfsd_genl_rqstp *rqstp)
1444 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1445 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1449 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1450 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1451 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1452 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1453 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1454 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1455 ktime_to_us(rqstp->rq_stime),
1456 NFSD_A_RPC_STATUS_PAD))
1459 switch (rqstp->rq_saddr.sa_family) {
1461 const struct sockaddr_in *s_in, *d_in;
1463 s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1464 d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1465 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1466 s_in->sin_addr.s_addr) ||
1467 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1468 d_in->sin_addr.s_addr) ||
1469 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1471 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1477 const struct sockaddr_in6 *s_in, *d_in;
1479 s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1480 d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1481 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1482 &s_in->sin6_addr) ||
1483 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1484 &d_in->sin6_addr) ||
1485 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1487 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1494 for (i = 0; i < rqstp->rq_opcnt; i++)
1495 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1496 rqstp->rq_opnum[i]))
1499 genlmsg_end(skb, hdr);
1504 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1505 * @skb: reply buffer
1506 * @cb: netlink metadata and command arguments
1508 * Returns the size of the reply or a negative errno.
1510 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1511 struct netlink_callback *cb)
1513 int i, ret, rqstp_index = 0;
1514 struct nfsd_net *nn;
1516 mutex_lock(&nfsd_mutex);
1518 nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1519 if (!nn->nfsd_serv) {
1526 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1527 struct svc_rqst *rqstp;
1529 if (i < cb->args[0]) /* already consumed */
1533 list_for_each_entry_rcu(rqstp,
1534 &nn->nfsd_serv->sv_pools[i].sp_all_threads,
1536 struct nfsd_genl_rqstp genl_rqstp;
1537 unsigned int status_counter;
1539 if (rqstp_index++ < cb->args[1]) /* already consumed */
1542 * Acquire rq_status_counter before parsing the rqst
1543 * fields. rq_status_counter is set to an odd value in
1544 * order to notify the consumers the rqstp fields are
1548 smp_load_acquire(&rqstp->rq_status_counter);
1549 if (!(status_counter & 1))
1552 genl_rqstp.rq_xid = rqstp->rq_xid;
1553 genl_rqstp.rq_flags = rqstp->rq_flags;
1554 genl_rqstp.rq_vers = rqstp->rq_vers;
1555 genl_rqstp.rq_prog = rqstp->rq_prog;
1556 genl_rqstp.rq_proc = rqstp->rq_proc;
1557 genl_rqstp.rq_stime = rqstp->rq_stime;
1558 genl_rqstp.rq_opcnt = 0;
1559 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1560 sizeof(struct sockaddr));
1561 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1562 sizeof(struct sockaddr));
1564 #ifdef CONFIG_NFSD_V4
1565 if (rqstp->rq_vers == NFS4_VERSION &&
1566 rqstp->rq_proc == NFSPROC4_COMPOUND) {
1567 /* NFSv4 compound */
1568 struct nfsd4_compoundargs *args;
1571 args = rqstp->rq_argp;
1572 genl_rqstp.rq_opcnt = args->opcnt;
1573 for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1574 genl_rqstp.rq_opnum[j] =
1577 #endif /* CONFIG_NFSD_V4 */
1580 * Acquire rq_status_counter before reporting the rqst
1581 * fields to the user.
1583 if (smp_load_acquire(&rqstp->rq_status_counter) !=
1587 ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1595 cb->args[1] = rqstp_index;
1600 mutex_unlock(&nfsd_mutex);
1606 * nfsd_nl_threads_set_doit - set the number of running threads
1607 * @skb: reply buffer
1608 * @info: netlink metadata and command arguments
1610 * Return 0 on success or a negative errno.
1612 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1614 int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem;
1615 struct net *net = genl_info_net(info);
1616 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1617 const struct nlattr *attr;
1618 const char *scope = NULL;
1620 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1623 /* count number of SERVER_THREADS values */
1624 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1625 if (nla_type(attr) == NFSD_A_SERVER_THREADS)
1629 mutex_lock(&nfsd_mutex);
1631 nthreads = kcalloc(nrpools, sizeof(int), GFP_KERNEL);
1638 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1639 if (nla_type(attr) == NFSD_A_SERVER_THREADS) {
1640 nthreads[i++] = nla_get_u32(attr);
1646 if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1647 info->attrs[NFSD_A_SERVER_LEASETIME] ||
1648 info->attrs[NFSD_A_SERVER_SCOPE]) {
1650 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1654 attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1656 u32 gracetime = nla_get_u32(attr);
1658 if (gracetime < 10 || gracetime > 3600)
1661 nn->nfsd4_grace = gracetime;
1664 attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1666 u32 leasetime = nla_get_u32(attr);
1668 if (leasetime < 10 || leasetime > 3600)
1671 nn->nfsd4_lease = leasetime;
1674 attr = info->attrs[NFSD_A_SERVER_SCOPE];
1676 scope = nla_data(attr);
1679 ret = nfsd_svc(nrpools, nthreads, net, get_current_cred(), scope);
1683 mutex_unlock(&nfsd_mutex);
1689 * nfsd_nl_threads_get_doit - get the number of running threads
1690 * @skb: reply buffer
1691 * @info: netlink metadata and command arguments
1693 * Return 0 on success or a negative errno.
1695 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1697 struct net *net = genl_info_net(info);
1698 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1702 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1706 hdr = genlmsg_iput(skb, info);
1712 mutex_lock(&nfsd_mutex);
1714 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1716 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1718 nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1723 if (nn->nfsd_serv) {
1726 for (i = 0; i < nfsd_nrpools(net); ++i) {
1727 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1729 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1735 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1740 mutex_unlock(&nfsd_mutex);
1742 genlmsg_end(skb, hdr);
1744 return genlmsg_reply(skb, info);
1747 mutex_unlock(&nfsd_mutex);
1755 * nfsd_nl_version_set_doit - set the nfs enabled versions
1756 * @skb: reply buffer
1757 * @info: netlink metadata and command arguments
1759 * Return 0 on success or a negative errno.
1761 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1763 const struct nlattr *attr;
1764 struct nfsd_net *nn;
1767 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1770 mutex_lock(&nfsd_mutex);
1772 nn = net_generic(genl_info_net(info), nfsd_net_id);
1773 if (nn->nfsd_serv) {
1774 mutex_unlock(&nfsd_mutex);
1778 /* clear current supported versions. */
1779 nfsd_vers(nn, 2, NFSD_CLEAR);
1780 nfsd_vers(nn, 3, NFSD_CLEAR);
1781 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1782 nfsd_minorversion(nn, i, NFSD_CLEAR);
1784 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1785 struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1786 u32 major, minor = 0;
1789 if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION)
1792 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1793 nfsd_version_nl_policy, info->extack) < 0)
1796 if (!tb[NFSD_A_VERSION_MAJOR])
1799 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1800 if (tb[NFSD_A_VERSION_MINOR])
1801 minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1803 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1807 nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1812 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1819 mutex_unlock(&nfsd_mutex);
1825 * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1826 * @skb: reply buffer
1827 * @info: netlink metadata and command arguments
1829 * Return 0 on success or a negative errno.
1831 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1833 struct nfsd_net *nn;
1837 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1841 hdr = genlmsg_iput(skb, info);
1847 mutex_lock(&nfsd_mutex);
1848 nn = net_generic(genl_info_net(info), nfsd_net_id);
1850 for (i = 2; i <= 4; i++) {
1853 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1854 struct nlattr *attr;
1856 /* Don't record any versions the kernel doesn't have
1859 if (!nfsd_support_version(i))
1862 /* NFSv{2,3} does not support minor numbers */
1866 attr = nla_nest_start(skb,
1867 NFSD_A_SERVER_PROTO_VERSION);
1870 goto err_nfsd_unlock;
1873 if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1874 nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1876 goto err_nfsd_unlock;
1879 /* Set the enabled flag if the version is enabled */
1880 if (nfsd_vers(nn, i, NFSD_TEST) &&
1881 (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1882 nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1884 goto err_nfsd_unlock;
1887 nla_nest_end(skb, attr);
1891 mutex_unlock(&nfsd_mutex);
1892 genlmsg_end(skb, hdr);
1894 return genlmsg_reply(skb, info);
1897 mutex_unlock(&nfsd_mutex);
1905 * nfsd_nl_listener_set_doit - set the nfs running sockets
1906 * @skb: reply buffer
1907 * @info: netlink metadata and command arguments
1909 * Return 0 on success or a negative errno.
1911 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1913 struct net *net = genl_info_net(info);
1914 struct svc_xprt *xprt, *tmp;
1915 const struct nlattr *attr;
1916 struct svc_serv *serv;
1917 LIST_HEAD(permsocks);
1918 struct nfsd_net *nn;
1919 bool delete = false;
1922 mutex_lock(&nfsd_mutex);
1924 err = nfsd_create_serv(net);
1926 mutex_unlock(&nfsd_mutex);
1930 nn = net_generic(net, nfsd_net_id);
1931 serv = nn->nfsd_serv;
1933 spin_lock_bh(&serv->sv_lock);
1935 /* Move all of the old listener sockets to a temp list */
1936 list_splice_init(&serv->sv_permsocks, &permsocks);
1939 * Walk the list of server_socks from userland and move any that match
1940 * back to sv_permsocks
1942 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1943 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1944 const char *xcl_name;
1945 struct sockaddr *sa;
1947 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
1950 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1951 nfsd_sock_nl_policy, info->extack) < 0)
1954 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
1957 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
1960 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
1961 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
1963 /* Put back any matching sockets */
1964 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
1965 /* This shouldn't be possible */
1966 if (WARN_ON_ONCE(xprt->xpt_net != net)) {
1967 list_move(&xprt->xpt_list, &serv->sv_permsocks);
1971 /* If everything matches, put it back */
1972 if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
1973 rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
1974 list_move(&xprt->xpt_list, &serv->sv_permsocks);
1981 * If there are listener transports remaining on the permsocks list,
1982 * it means we were asked to remove a listener.
1984 if (!list_empty(&permsocks)) {
1985 list_splice_init(&permsocks, &serv->sv_permsocks);
1988 spin_unlock_bh(&serv->sv_lock);
1990 /* Do not remove listeners while there are active threads. */
1991 if (serv->sv_nrthreads) {
1993 goto out_unlock_mtx;
1997 * Since we can't delete an arbitrary llist entry, destroy the
1998 * remaining listeners and recreate the list.
2001 svc_xprt_destroy_all(serv, net);
2003 /* walk list of addrs again, open any that still don't exist */
2004 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
2005 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2006 const char *xcl_name;
2007 struct sockaddr *sa;
2010 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
2013 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2014 nfsd_sock_nl_policy, info->extack) < 0)
2017 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2020 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2023 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2024 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2026 xprt = svc_find_listener(serv, xcl_name, net, sa);
2029 WARN_ONCE(1, "Transport type=%s already exists\n",
2035 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, 0,
2036 get_current_cred());
2037 /* always save the latest error */
2042 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2043 nfsd_destroy_serv(net);
2046 mutex_unlock(&nfsd_mutex);
2052 * nfsd_nl_listener_get_doit - get the nfs running listeners
2053 * @skb: reply buffer
2054 * @info: netlink metadata and command arguments
2056 * Return 0 on success or a negative errno.
2058 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2060 struct svc_xprt *xprt;
2061 struct svc_serv *serv;
2062 struct nfsd_net *nn;
2066 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2070 hdr = genlmsg_iput(skb, info);
2076 mutex_lock(&nfsd_mutex);
2077 nn = net_generic(genl_info_net(info), nfsd_net_id);
2079 /* no nfs server? Just send empty socket list */
2081 goto out_unlock_mtx;
2083 serv = nn->nfsd_serv;
2084 spin_lock_bh(&serv->sv_lock);
2085 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2086 struct nlattr *attr;
2088 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2091 goto err_serv_unlock;
2094 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2095 xprt->xpt_class->xcl_name) ||
2096 nla_put(skb, NFSD_A_SOCK_ADDR,
2097 sizeof(struct sockaddr_storage),
2098 &xprt->xpt_local)) {
2100 goto err_serv_unlock;
2103 nla_nest_end(skb, attr);
2105 spin_unlock_bh(&serv->sv_lock);
2107 mutex_unlock(&nfsd_mutex);
2108 genlmsg_end(skb, hdr);
2110 return genlmsg_reply(skb, info);
2113 spin_unlock_bh(&serv->sv_lock);
2114 mutex_unlock(&nfsd_mutex);
2122 * nfsd_nl_pool_mode_set_doit - set the number of running threads
2123 * @skb: reply buffer
2124 * @info: netlink metadata and command arguments
2126 * Return 0 on success or a negative errno.
2128 int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info)
2130 const struct nlattr *attr;
2132 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE))
2135 attr = info->attrs[NFSD_A_POOL_MODE_MODE];
2136 return sunrpc_set_pool_mode(nla_data(attr));
2140 * nfsd_nl_pool_mode_get_doit - get info about pool_mode
2141 * @skb: reply buffer
2142 * @info: netlink metadata and command arguments
2144 * Return 0 on success or a negative errno.
2146 int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info)
2148 struct net *net = genl_info_net(info);
2153 if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
2156 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2161 hdr = genlmsg_iput(skb, info);
2165 err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) |
2166 nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net));
2170 genlmsg_end(skb, hdr);
2171 return genlmsg_reply(skb, info);
2179 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2180 * @net: a freshly-created network namespace
2182 * This information stays around as long as the network namespace is
2183 * alive whether or not there is an NFSD instance running in the
2186 * Returns zero on success, or a negative errno otherwise.
2188 static __net_init int nfsd_net_init(struct net *net)
2190 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2194 retval = nfsd_export_init(net);
2196 goto out_export_error;
2197 retval = nfsd_idmap_init(net);
2199 goto out_idmap_error;
2200 retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2201 NFSD_STATS_COUNTERS_NUM);
2203 goto out_repcache_error;
2205 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2206 nn->nfsd_svcstats.program = &nfsd_programs[0];
2207 if (!nfsd_proc_stat_init(net)) {
2209 goto out_proc_error;
2212 for (i = 0; i < sizeof(nn->nfsd_versions); i++)
2213 nn->nfsd_versions[i] = nfsd_support_version(i);
2214 for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++)
2215 nn->nfsd4_minorversions[i] = nfsd_support_version(4);
2216 nn->nfsd_info.mutex = &nfsd_mutex;
2217 nn->nfsd_serv = NULL;
2218 nfsd4_init_leases_net(nn);
2219 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2220 seqlock_init(&nn->writeverf_lock);
2221 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2222 spin_lock_init(&nn->local_clients_lock);
2223 INIT_LIST_HEAD(&nn->local_clients);
2228 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2230 nfsd_idmap_shutdown(net);
2232 nfsd_export_shutdown(net);
2237 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2239 * nfsd_net_pre_exit - Disconnect localio clients from net namespace
2240 * @net: a network namespace that is about to be destroyed
2242 * This invalidates ->net pointers held by localio clients
2243 * while they can still safely access nn->counter.
2245 static __net_exit void nfsd_net_pre_exit(struct net *net)
2247 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2249 nfs_localio_invalidate_clients(&nn->local_clients,
2250 &nn->local_clients_lock);
2255 * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2256 * @net: a network namespace that is about to be destroyed
2259 static __net_exit void nfsd_net_exit(struct net *net)
2261 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2263 nfsd_proc_stat_shutdown(net);
2264 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2265 nfsd_idmap_shutdown(net);
2266 nfsd_export_shutdown(net);
2269 static struct pernet_operations nfsd_net_ops = {
2270 .init = nfsd_net_init,
2271 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2272 .pre_exit = nfsd_net_pre_exit,
2274 .exit = nfsd_net_exit,
2276 .size = sizeof(struct nfsd_net),
2279 static int __init init_nfsd(void)
2283 nfsd_debugfs_init();
2285 retval = nfsd4_init_slabs();
2288 retval = nfsd4_init_pnfs();
2290 goto out_free_slabs;
2291 retval = nfsd_drc_slab_create();
2294 nfsd_lockd_init(); /* lockd->nfsd callbacks */
2295 retval = register_pernet_subsys(&nfsd_net_ops);
2297 goto out_free_lockd;
2298 retval = register_cld_notifier();
2300 goto out_free_subsys;
2301 retval = nfsd4_create_laundry_wq();
2304 retval = register_filesystem(&nfsd_fs_type);
2306 goto out_free_nfsd4;
2307 retval = genl_register_family(&nfsd_nl_family);
2309 goto out_free_filesystem;
2310 retval = create_proc_exports_entry();
2313 nfsd_localio_ops_init();
2317 genl_unregister_family(&nfsd_nl_family);
2318 out_free_filesystem:
2319 unregister_filesystem(&nfsd_fs_type);
2321 nfsd4_destroy_laundry_wq();
2323 unregister_cld_notifier();
2325 unregister_pernet_subsys(&nfsd_net_ops);
2327 nfsd_lockd_shutdown();
2328 nfsd_drc_slab_free();
2333 nfsd_debugfs_exit();
2337 static void __exit exit_nfsd(void)
2339 remove_proc_entry("fs/nfs/exports", NULL);
2340 remove_proc_entry("fs/nfs", NULL);
2341 genl_unregister_family(&nfsd_nl_family);
2342 unregister_filesystem(&nfsd_fs_type);
2343 nfsd4_destroy_laundry_wq();
2344 unregister_cld_notifier();
2345 unregister_pernet_subsys(&nfsd_net_ops);
2346 nfsd_drc_slab_free();
2347 nfsd_lockd_shutdown();
2350 nfsd_debugfs_exit();
2353 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2354 MODULE_DESCRIPTION("In-kernel NFS server");
2355 MODULE_LICENSE("GPL");
2356 module_init(init_nfsd)
2357 module_exit(exit_nfsd)