1 // SPDX-License-Identifier: GPL-2.0-only
3 * Syscall interface to knfsd.
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>
28 #include "filecache.h"
33 * We have a single directory with several nodes in it.
46 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);
70 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
72 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
73 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
74 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
75 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
77 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
80 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
81 [NFSD_Fh] = write_filehandle,
82 [NFSD_FO_UnlockIP] = write_unlock_ip,
83 [NFSD_FO_UnlockFS] = write_unlock_fs,
84 [NFSD_Threads] = write_threads,
85 [NFSD_Pool_Threads] = write_pool_threads,
86 [NFSD_Versions] = write_versions,
87 [NFSD_Ports] = write_ports,
88 [NFSD_MaxBlkSize] = write_maxblksize,
89 [NFSD_MaxConnections] = write_maxconn,
91 [NFSD_Leasetime] = write_leasetime,
92 [NFSD_Gracetime] = write_gracetime,
93 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
94 [NFSD_RecoveryDir] = write_recoverydir,
96 [NFSD_V4EndGrace] = write_v4_end_grace,
100 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
102 ino_t ino = file_inode(file)->i_ino;
106 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
109 data = simple_transaction_get(file, buf, size);
111 return PTR_ERR(data);
113 rv = write_op[ino](file, data, size);
117 simple_transaction_set(file, rv);
121 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
123 if (! file->private_data) {
124 /* An attempt to read a transaction file without writing
125 * causes a 0-byte write so that the file can return
128 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
132 return simple_transaction_read(file, buf, size, pos);
135 static const struct file_operations transaction_ops = {
136 .write = nfsctl_transaction_write,
137 .read = nfsctl_transaction_read,
138 .release = simple_transaction_release,
139 .llseek = default_llseek,
142 static int exports_net_open(struct net *net, struct file *file)
145 struct seq_file *seq;
146 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
148 err = seq_open(file, &nfs_exports_op);
152 seq = file->private_data;
153 seq->private = nn->svc_export_cache;
157 static int exports_nfsd_open(struct inode *inode, struct file *file)
159 return exports_net_open(inode->i_sb->s_fs_info, file);
162 static const struct file_operations exports_nfsd_operations = {
163 .open = exports_nfsd_open,
166 .release = seq_release,
169 static int export_features_show(struct seq_file *m, void *v)
171 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
175 DEFINE_SHOW_ATTRIBUTE(export_features);
177 static const struct file_operations pool_stats_operations = {
178 .open = nfsd_pool_stats_open,
181 .release = seq_release,
184 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
186 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
188 /*----------------------------------------------------------------------------*/
190 * payload - write methods
193 static inline struct net *netns(struct file *file)
195 return file_inode(file)->i_sb->s_fs_info;
199 * write_unlock_ip - Release all locks used by a client
204 * buf: '\n'-terminated C string containing a
205 * presentation format IP address
206 * size: length of C string in @buf
208 * On success: returns zero if all specified locks were released;
209 * returns one if one or more locks were not released
210 * On error: return code is negative errno value
212 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
214 struct sockaddr_storage address;
215 struct sockaddr *sap = (struct sockaddr *)&address;
216 size_t salen = sizeof(address);
218 struct net *net = netns(file);
224 if (buf[size-1] != '\n')
228 if (qword_get(&buf, fo_path, size) < 0)
231 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
234 trace_nfsd_ctl_unlock_ip(net, buf);
235 return nlmsvc_unlock_all_by_ip(sap);
239 * write_unlock_fs - Release all locks on a local file system
244 * buf: '\n'-terminated C string containing the
245 * absolute pathname of a local file system
246 * size: length of C string in @buf
248 * On success: returns zero if all specified locks were released;
249 * returns one if one or more locks were not released
250 * On error: return code is negative errno value
252 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
262 if (buf[size-1] != '\n')
266 if (qword_get(&buf, fo_path, size) < 0)
268 trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
269 error = kern_path(fo_path, 0, &path);
274 * XXX: Needs better sanity checking. Otherwise we could end up
275 * releasing locks on the wrong file system.
278 * 1. Does the path refer to a directory?
279 * 2. Is that directory a mount point, or
280 * 3. Is that directory the root of an exported file system?
282 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
283 nfsd4_revoke_states(netns(file), path.dentry->d_sb);
290 * write_filehandle - Get a variable-length NFS file handle by path
292 * On input, the buffer contains a '\n'-terminated C string comprised of
293 * three alphanumeric words separated by whitespace. The string may
294 * contain escape sequences.
298 * domain: client domain name
299 * path: export pathname
300 * maxsize: numeric maximum size of
302 * size: length of C string in @buf
304 * On success: passed-in buffer filled with '\n'-terminated C
305 * string containing a ASCII hex text version
306 * of the NFS file handle;
307 * return code is the size in bytes of the string
308 * On error: return code is negative errno value
310 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
316 struct auth_domain *dom;
322 if (buf[size-1] != '\n')
327 len = qword_get(&mesg, dname, size);
332 len = qword_get(&mesg, path, size);
336 len = get_int(&mesg, &maxsize);
340 if (maxsize < NFS_FHSIZE)
342 maxsize = min(maxsize, NFS3_FHSIZE);
344 if (qword_get(&mesg, mesg, size) > 0)
347 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
349 /* we have all the words, they are in buf.. */
350 dom = unix_domain_find(dname);
354 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
355 auth_domain_put(dom);
360 len = SIMPLE_TRANSACTION_LIMIT;
361 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
367 * write_threads - Start NFSD, or report the current number of running threads
373 * On success: passed-in buffer filled with '\n'-terminated C
374 * string numeric value representing the number of
375 * running NFSD threads;
376 * return code is the size in bytes of the string
377 * On error: return code is zero
382 * buf: C string containing an unsigned
383 * integer value representing the
384 * number of NFSD threads to start
385 * size: non-zero length of C string in @buf
387 * On success: NFS service is started;
388 * passed-in buffer filled with '\n'-terminated C
389 * string numeric value representing the number of
390 * running NFSD threads;
391 * return code is the size in bytes of the string
392 * On error: return code is zero or a negative errno value
394 static ssize_t write_threads(struct file *file, char *buf, size_t size)
398 struct net *net = netns(file);
402 rv = get_int(&mesg, &newthreads);
407 trace_nfsd_ctl_threads(net, newthreads);
408 mutex_lock(&nfsd_mutex);
409 rv = nfsd_svc(newthreads, net, file->f_cred, NULL);
410 mutex_unlock(&nfsd_mutex);
414 rv = nfsd_nrthreads(net);
416 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
420 * write_pool_threads - Set or report the current number of threads per pool
429 * buf: C string containing whitespace-
430 * separated unsigned integer values
431 * representing the number of NFSD
432 * threads to start in each pool
433 * size: non-zero length of C string in @buf
435 * On success: passed-in buffer filled with '\n'-terminated C
436 * string containing integer values representing the
437 * number of NFSD threads in each pool;
438 * return code is the size in bytes of the string
439 * On error: return code is zero or a negative errno value
441 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
443 /* if size > 0, look for an array of number of threads per node
444 * and apply them then write out number of threads per node as reply
452 struct net *net = netns(file);
454 mutex_lock(&nfsd_mutex);
455 npools = nfsd_nrpools(net);
458 * NFS is shut down. The admin can start it by
459 * writing to the threads file but NOT the pool_threads
460 * file, sorry. Report zero threads.
462 mutex_unlock(&nfsd_mutex);
467 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
469 if (nthreads == NULL)
473 for (i = 0; i < npools; i++) {
474 rv = get_int(&mesg, &nthreads[i]);
476 break; /* fewer numbers than pools */
478 goto out_free; /* syntax error */
482 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
484 rv = nfsd_set_nrthreads(i, nthreads, net);
489 rv = nfsd_get_nrthreads(npools, nthreads, net);
494 size = SIMPLE_TRANSACTION_LIMIT;
495 for (i = 0; i < npools && size > 0; i++) {
496 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
504 mutex_unlock(&nfsd_mutex);
509 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
510 const char *sep, unsigned vers, int minor)
512 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
513 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
515 if (vers == 4 && minor >= 0 &&
516 !nfsd_minorversion(nn, minor, NFSD_TEST))
518 if (minor == 0 && supported)
520 * special case for backward compatability.
521 * +4.0 is never reported, it is implied by
522 * +4, unless -4.0 is present.
525 return snprintf(buf, remaining, format, sep,
526 supported ? '+' : '-', vers, minor);
529 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
532 char *vers, *minorp, sign;
533 int len, num, remaining;
536 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
540 /* Cannot change versions without updating
541 * nn->nfsd_serv->sv_xdrsize, and reallocing
542 * rq_argp and rq_resp
545 if (buf[size-1] != '\n')
548 trace_nfsd_ctl_version(netns(file), buf);
551 len = qword_get(&mesg, vers, size);
552 if (len <= 0) return -EINVAL;
557 if (sign == '+' || sign == '-')
558 num = simple_strtol((vers+1), &minorp, 0);
560 num = simple_strtol(vers, &minorp, 0);
561 if (*minorp == '.') {
564 if (kstrtouint(minorp+1, 0, &minor) < 0)
568 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
570 #ifdef CONFIG_NFSD_V2
574 nfsd_vers(nn, num, cmd);
577 if (*minorp == '.') {
578 if (nfsd_minorversion(nn, minor, cmd) < 0)
580 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
582 * Either we have +4 and no minors are enabled,
583 * or we have -4 and at least one minor is enabled.
584 * In either case, propagate 'cmd' to all minors.
587 while (nfsd_minorversion(nn, minor, cmd) >= 0)
592 /* Ignore requests to disable non-existent versions */
597 } while ((len = qword_get(&mesg, vers, size)) > 0);
598 /* If all get turned off, turn them back on, as
599 * having no versions is BAD
601 nfsd_reset_versions(nn);
604 /* Now write current state into reply buffer */
606 remaining = SIMPLE_TRANSACTION_LIMIT;
607 for (num=2 ; num <= 4 ; num++) {
609 if (!nfsd_vers(nn, num, NFSD_AVAIL))
614 len = nfsd_print_version_support(nn, buf, remaining,
616 if (len >= remaining)
624 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
627 len = snprintf(buf, remaining, "\n");
628 if (len >= remaining)
634 * write_versions - Set or report the available NFS protocol versions
640 * On success: passed-in buffer filled with '\n'-terminated C
641 * string containing positive or negative integer
642 * values representing the current status of each
644 * return code is the size in bytes of the string
645 * On error: return code is zero or a negative errno value
650 * buf: C string containing whitespace-
651 * separated positive or negative
652 * integer values representing NFS
653 * protocol versions to enable ("+n")
655 * size: non-zero length of C string in @buf
657 * On success: status of zero or more protocol versions has
658 * been updated; passed-in buffer filled with
659 * '\n'-terminated C string containing positive
660 * or negative integer values representing the
661 * current status of each protocol version;
662 * return code is the size in bytes of the string
663 * On error: return code is zero or a negative errno value
665 static ssize_t write_versions(struct file *file, char *buf, size_t size)
669 mutex_lock(&nfsd_mutex);
670 rv = __write_versions(file, buf, size);
671 mutex_unlock(&nfsd_mutex);
676 * Zero-length write. Return a list of NFSD's current listener
679 static ssize_t __write_ports_names(char *buf, struct net *net)
681 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
683 if (nn->nfsd_serv == NULL)
685 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
689 * A single 'fd' number was written, in which case it must be for
690 * a socket of a supported family/protocol, and we use it as an
693 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
697 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
698 struct svc_serv *serv;
700 err = get_int(&mesg, &fd);
701 if (err != 0 || fd < 0)
703 trace_nfsd_ctl_ports_addfd(net, fd);
705 err = nfsd_create_serv(net);
709 serv = nn->nfsd_serv;
710 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
712 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
713 nfsd_destroy_serv(net);
719 * A transport listener is added by writing its transport name and
722 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
725 struct svc_xprt *xprt;
727 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
728 struct svc_serv *serv;
730 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
733 if (port < 1 || port > USHRT_MAX)
735 trace_nfsd_ctl_ports_addxprt(net, transport, port);
737 err = nfsd_create_serv(net);
741 serv = nn->nfsd_serv;
742 err = svc_xprt_create(serv, transport, net,
743 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
747 err = svc_xprt_create(serv, transport, net,
748 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
749 if (err < 0 && err != -EAFNOSUPPORT)
754 xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
756 svc_xprt_close(xprt);
760 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
761 nfsd_destroy_serv(net);
766 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
770 return __write_ports_names(buf, net);
773 return __write_ports_addfd(buf, net, file->f_cred);
776 return __write_ports_addxprt(buf, net, file->f_cred);
782 * write_ports - Pass a socket file descriptor or transport name to listen on
788 * On success: passed-in buffer filled with a '\n'-terminated C
789 * string containing a whitespace-separated list of
790 * named NFSD listeners;
791 * return code is the size in bytes of the string
792 * On error: return code is zero or a negative errno value
797 * buf: C string containing an unsigned
798 * integer value representing a bound
799 * but unconnected socket that is to be
800 * used as an NFSD listener; listen(3)
801 * must be called for a SOCK_STREAM
802 * socket, otherwise it is ignored
803 * size: non-zero length of C string in @buf
805 * On success: NFS service is started;
806 * passed-in buffer filled with a '\n'-terminated C
807 * string containing a unique alphanumeric name of
809 * return code is the size in bytes of the string
810 * On error: return code is a negative errno value
815 * buf: C string containing a transport
816 * name and an unsigned integer value
817 * representing the port to listen on,
818 * separated by whitespace
819 * size: non-zero length of C string in @buf
821 * On success: returns zero; NFS service is started
822 * On error: return code is a negative errno value
824 static ssize_t write_ports(struct file *file, char *buf, size_t size)
828 mutex_lock(&nfsd_mutex);
829 rv = __write_ports(file, buf, size, netns(file));
830 mutex_unlock(&nfsd_mutex);
835 int nfsd_max_blksize;
838 * write_maxblksize - Set or report the current NFS blksize
847 * buf: C string containing an unsigned
848 * integer value representing the new
850 * size: non-zero length of C string in @buf
852 * On success: passed-in buffer filled with '\n'-terminated C string
853 * containing numeric value of the current NFS blksize
855 * return code is the size in bytes of the string
856 * On error: return code is zero or a negative errno value
858 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
861 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
865 int rv = get_int(&mesg, &bsize);
868 trace_nfsd_ctl_maxblksize(netns(file), bsize);
870 /* force bsize into allowed range and
871 * required alignment.
873 bsize = max_t(int, bsize, 1024);
874 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
876 mutex_lock(&nfsd_mutex);
878 mutex_unlock(&nfsd_mutex);
881 nfsd_max_blksize = bsize;
882 mutex_unlock(&nfsd_mutex);
885 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
890 * write_maxconn - Set or report the current max number of connections
898 * buf: C string containing an unsigned
899 * integer value representing the new
900 * number of max connections
901 * size: non-zero length of C string in @buf
903 * On success: passed-in buffer filled with '\n'-terminated C string
904 * containing numeric value of max_connections setting
905 * for this net namespace;
906 * return code is the size in bytes of the string
907 * On error: return code is zero or a negative errno value
909 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
912 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
913 unsigned int maxconn = nn->max_connections;
916 int rv = get_uint(&mesg, &maxconn);
920 trace_nfsd_ctl_maxconn(netns(file), maxconn);
921 nn->max_connections = maxconn;
924 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
927 #ifdef CONFIG_NFSD_V4
928 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
929 time64_t *time, struct nfsd_net *nn)
931 struct dentry *dentry = file_dentry(file);
938 rv = get_int(&mesg, &i);
941 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
942 dentry->d_name.len, i);
945 * Some sanity checking. We don't have a reason for
946 * these particular numbers, but problems with the
948 * - Too short: the briefest network outage may
949 * cause clients to lose all their locks. Also,
950 * the frequent polling may be wasteful.
951 * - Too long: do you really want reboot recovery
952 * to take more than an hour? Or to make other
953 * clients wait an hour before being able to
954 * revoke a dead client's locks?
956 if (i < 10 || i > 3600)
961 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
964 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
965 time64_t *time, struct nfsd_net *nn)
969 mutex_lock(&nfsd_mutex);
970 rv = __nfsd4_write_time(file, buf, size, time, nn);
971 mutex_unlock(&nfsd_mutex);
976 * write_leasetime - Set or report the current NFSv4 lease time
985 * buf: C string containing an unsigned
986 * integer value representing the new
987 * NFSv4 lease expiry time
988 * size: non-zero length of C string in @buf
990 * On success: passed-in buffer filled with '\n'-terminated C
991 * string containing unsigned integer value of the
992 * current lease expiry time;
993 * return code is the size in bytes of the string
994 * On error: return code is zero or a negative errno value
996 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
998 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
999 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1003 * write_gracetime - Set or report current NFSv4 grace period time
1005 * As above, but sets the time of the NFSv4 grace period.
1007 * Note this should never be set to less than the *previous*
1008 * lease-period time, but we don't try to enforce this. (In the common
1009 * case (a new boot), we don't know what the previous lease time was
1012 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1014 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1015 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1018 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1019 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1020 struct nfsd_net *nn)
1029 if (size > PATH_MAX || buf[size-1] != '\n')
1034 len = qword_get(&mesg, recdir, size);
1037 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1039 status = nfs4_reset_recoverydir(recdir);
1044 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1045 nfs4_recoverydir());
1049 * write_recoverydir - Set or report the pathname of the recovery directory
1058 * buf: C string containing the pathname
1059 * of the directory on a local file
1060 * system containing permanent NFSv4
1062 * size: non-zero length of C string in @buf
1064 * On success: passed-in buffer filled with '\n'-terminated C string
1065 * containing the current recovery pathname setting;
1066 * return code is the size in bytes of the string
1067 * On error: return code is zero or a negative errno value
1069 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1072 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1074 mutex_lock(&nfsd_mutex);
1075 rv = __write_recoverydir(file, buf, size, nn);
1076 mutex_unlock(&nfsd_mutex);
1082 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1091 * size: non-zero length of C string in @buf
1093 * passed-in buffer filled with "Y" or "N" with a newline
1094 * and NULL-terminated C string. This indicates whether
1095 * the grace period has ended in the current net
1096 * namespace. Return code is the size in bytes of the
1097 * string. Writing a string that starts with 'Y', 'y', or
1098 * '1' to the file will end the grace period for nfsd's v4
1101 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1103 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1112 trace_nfsd_end_grace(netns(file));
1113 nfsd4_end_grace(nn);
1120 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1121 nn->grace_ended ? 'Y' : 'N');
1126 /*----------------------------------------------------------------------------*/
1128 * populating the filesystem.
1131 /* Basically copying rpc_get_inode. */
1132 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1134 struct inode *inode = new_inode(sb);
1137 /* Following advice from simple_fill_super documentation: */
1138 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1139 inode->i_mode = mode;
1140 simple_inode_init_ts(inode);
1141 switch (mode & S_IFMT) {
1143 inode->i_fop = &simple_dir_operations;
1144 inode->i_op = &simple_dir_inode_operations;
1148 inode->i_op = &simple_symlink_inode_operations;
1156 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1158 struct inode *inode;
1160 inode = nfsd_get_inode(dir->i_sb, mode);
1164 inode->i_private = ncl;
1165 kref_get(&ncl->cl_ref);
1167 d_add(dentry, inode);
1169 fsnotify_mkdir(dir, dentry);
1173 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1175 struct inode *dir = parent->d_inode;
1176 struct dentry *dentry;
1180 dentry = d_alloc_name(parent, name);
1183 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1191 dentry = ERR_PTR(ret);
1195 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1196 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1197 umode_t mode, const char *content)
1199 struct inode *inode;
1201 inode = nfsd_get_inode(dir->i_sb, mode);
1205 inode->i_link = (char *)content;
1206 inode->i_size = strlen(content);
1208 d_add(dentry, inode);
1210 fsnotify_create(dir, dentry);
1215 * @content is assumed to be a NUL-terminated string that lives
1216 * longer than the symlink itself.
1218 static void _nfsd_symlink(struct dentry *parent, const char *name,
1219 const char *content)
1221 struct inode *dir = parent->d_inode;
1222 struct dentry *dentry;
1226 dentry = d_alloc_name(parent, name);
1229 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1236 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1237 const char *content)
1243 static void clear_ncl(struct dentry *dentry)
1245 struct inode *inode = d_inode(dentry);
1246 struct nfsdfs_client *ncl = inode->i_private;
1248 spin_lock(&inode->i_lock);
1249 inode->i_private = NULL;
1250 spin_unlock(&inode->i_lock);
1251 kref_put(&ncl->cl_ref, ncl->cl_release);
1254 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1256 struct nfsdfs_client *nc;
1258 spin_lock(&inode->i_lock);
1259 nc = inode->i_private;
1261 kref_get(&nc->cl_ref);
1262 spin_unlock(&inode->i_lock);
1266 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1268 static int nfsdfs_create_files(struct dentry *root,
1269 const struct tree_descr *files,
1270 struct nfsdfs_client *ncl,
1271 struct dentry **fdentries)
1273 struct inode *dir = d_inode(root);
1274 struct inode *inode;
1275 struct dentry *dentry;
1279 for (i = 0; files->name && files->name[0]; i++, files++) {
1280 dentry = d_alloc_name(root, files->name);
1283 inode = nfsd_get_inode(d_inode(root)->i_sb,
1284 S_IFREG | files->mode);
1289 kref_get(&ncl->cl_ref);
1290 inode->i_fop = files->ops;
1291 inode->i_private = ncl;
1292 d_add(dentry, inode);
1293 fsnotify_create(dir, dentry);
1295 fdentries[i] = dentry;
1304 /* on success, returns positive number unique to that client. */
1305 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1306 struct nfsdfs_client *ncl, u32 id,
1307 const struct tree_descr *files,
1308 struct dentry **fdentries)
1310 struct dentry *dentry;
1314 sprintf(name, "%u", id);
1316 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1317 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1319 ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1321 nfsd_client_rmdir(dentry);
1327 /* Taken from __rpc_rmdir: */
1328 void nfsd_client_rmdir(struct dentry *dentry)
1330 simple_recursive_removal(dentry, clear_ncl);
1333 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1335 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1337 struct dentry *dentry;
1340 static const struct tree_descr nfsd_files[] = {
1341 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1342 /* Per-export io stats use same ops as exports file */
1343 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1344 [NFSD_Export_features] = {"export_features",
1345 &export_features_fops, S_IRUGO},
1346 [NFSD_FO_UnlockIP] = {"unlock_ip",
1347 &transaction_ops, S_IWUSR|S_IRUSR},
1348 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1349 &transaction_ops, S_IWUSR|S_IRUSR},
1350 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1351 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1352 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1353 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1354 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1355 &nfsd_reply_cache_stats_fops, S_IRUGO},
1356 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1357 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1358 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1359 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1360 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1361 #ifdef CONFIG_NFSD_V4
1362 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1363 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1364 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1365 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1367 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1372 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1375 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1376 "/proc/net/rpc/gss_krb5_enctypes");
1377 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1379 return PTR_ERR(dentry);
1380 nn->nfsd_client_dir = dentry;
1384 static int nfsd_fs_get_tree(struct fs_context *fc)
1386 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1389 static void nfsd_fs_free_fc(struct fs_context *fc)
1392 put_net(fc->s_fs_info);
1395 static const struct fs_context_operations nfsd_fs_context_ops = {
1396 .free = nfsd_fs_free_fc,
1397 .get_tree = nfsd_fs_get_tree,
1400 static int nfsd_init_fs_context(struct fs_context *fc)
1402 put_user_ns(fc->user_ns);
1403 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1404 fc->ops = &nfsd_fs_context_ops;
1408 static void nfsd_umount(struct super_block *sb)
1410 struct net *net = sb->s_fs_info;
1412 nfsd_shutdown_threads(net);
1414 kill_litter_super(sb);
1418 static struct file_system_type nfsd_fs_type = {
1419 .owner = THIS_MODULE,
1421 .init_fs_context = nfsd_init_fs_context,
1422 .kill_sb = nfsd_umount,
1424 MODULE_ALIAS_FS("nfsd");
1426 #ifdef CONFIG_PROC_FS
1428 static int exports_proc_open(struct inode *inode, struct file *file)
1430 return exports_net_open(current->nsproxy->net_ns, file);
1433 static const struct proc_ops exports_proc_ops = {
1434 .proc_open = exports_proc_open,
1435 .proc_read = seq_read,
1436 .proc_lseek = seq_lseek,
1437 .proc_release = seq_release,
1440 static int create_proc_exports_entry(void)
1442 struct proc_dir_entry *entry;
1444 entry = proc_mkdir("fs/nfs", NULL);
1447 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1449 remove_proc_entry("fs/nfs", NULL);
1454 #else /* CONFIG_PROC_FS */
1455 static int create_proc_exports_entry(void)
1461 unsigned int nfsd_net_id;
1464 * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit
1465 * @cb: netlink metadata and command arguments
1468 * %0: The rpc_status_get command may proceed
1469 * %-ENODEV: There is no NFSD running in this namespace
1471 int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb)
1473 struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id);
1476 mutex_lock(&nfsd_mutex);
1480 mutex_unlock(&nfsd_mutex);
1485 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1486 struct netlink_callback *cb,
1487 struct nfsd_genl_rqstp *rqstp)
1492 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1493 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1497 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1498 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1499 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1500 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1501 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1502 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1503 ktime_to_us(rqstp->rq_stime),
1504 NFSD_A_RPC_STATUS_PAD))
1507 switch (rqstp->rq_saddr.sa_family) {
1509 const struct sockaddr_in *s_in, *d_in;
1511 s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1512 d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1513 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1514 s_in->sin_addr.s_addr) ||
1515 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1516 d_in->sin_addr.s_addr) ||
1517 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1519 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1525 const struct sockaddr_in6 *s_in, *d_in;
1527 s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1528 d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1529 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1530 &s_in->sin6_addr) ||
1531 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1532 &d_in->sin6_addr) ||
1533 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1535 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1542 for (i = 0; i < rqstp->rq_opcnt; i++)
1543 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1544 rqstp->rq_opnum[i]))
1547 genlmsg_end(skb, hdr);
1552 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1553 * @skb: reply buffer
1554 * @cb: netlink metadata and command arguments
1556 * Returns the size of the reply or a negative errno.
1558 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1559 struct netlink_callback *cb)
1561 struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1562 int i, ret, rqstp_index = 0;
1566 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1567 struct svc_rqst *rqstp;
1569 if (i < cb->args[0]) /* already consumed */
1573 list_for_each_entry_rcu(rqstp,
1574 &nn->nfsd_serv->sv_pools[i].sp_all_threads,
1576 struct nfsd_genl_rqstp genl_rqstp;
1577 unsigned int status_counter;
1579 if (rqstp_index++ < cb->args[1]) /* already consumed */
1582 * Acquire rq_status_counter before parsing the rqst
1583 * fields. rq_status_counter is set to an odd value in
1584 * order to notify the consumers the rqstp fields are
1588 smp_load_acquire(&rqstp->rq_status_counter);
1589 if (!(status_counter & 1))
1592 genl_rqstp.rq_xid = rqstp->rq_xid;
1593 genl_rqstp.rq_flags = rqstp->rq_flags;
1594 genl_rqstp.rq_vers = rqstp->rq_vers;
1595 genl_rqstp.rq_prog = rqstp->rq_prog;
1596 genl_rqstp.rq_proc = rqstp->rq_proc;
1597 genl_rqstp.rq_stime = rqstp->rq_stime;
1598 genl_rqstp.rq_opcnt = 0;
1599 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1600 sizeof(struct sockaddr));
1601 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1602 sizeof(struct sockaddr));
1604 #ifdef CONFIG_NFSD_V4
1605 if (rqstp->rq_vers == NFS4_VERSION &&
1606 rqstp->rq_proc == NFSPROC4_COMPOUND) {
1607 /* NFSv4 compound */
1608 struct nfsd4_compoundargs *args;
1611 args = rqstp->rq_argp;
1612 genl_rqstp.rq_opcnt = args->opcnt;
1613 for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1614 genl_rqstp.rq_opnum[j] =
1617 #endif /* CONFIG_NFSD_V4 */
1620 * Acquire rq_status_counter before reporting the rqst
1621 * fields to the user.
1623 if (smp_load_acquire(&rqstp->rq_status_counter) !=
1627 ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1635 cb->args[1] = rqstp_index;
1644 * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing
1645 * @cb: netlink metadata and command arguments
1650 int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb)
1652 mutex_unlock(&nfsd_mutex);
1658 * nfsd_nl_threads_set_doit - set the number of running threads
1659 * @skb: reply buffer
1660 * @info: netlink metadata and command arguments
1662 * Return 0 on success or a negative errno.
1664 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1666 int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem;
1667 struct net *net = genl_info_net(info);
1668 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1669 const struct nlattr *attr;
1670 const char *scope = NULL;
1672 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1675 /* count number of SERVER_THREADS values */
1676 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1677 if (nla_type(attr) == NFSD_A_SERVER_THREADS)
1681 mutex_lock(&nfsd_mutex);
1683 nrpools = nfsd_nrpools(net);
1684 if (nrpools && count > nrpools)
1687 /* XXX: make this handle non-global pool-modes */
1691 nthreads = nla_get_u32(info->attrs[NFSD_A_SERVER_THREADS]);
1692 if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1693 info->attrs[NFSD_A_SERVER_LEASETIME] ||
1694 info->attrs[NFSD_A_SERVER_SCOPE]) {
1696 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1700 attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1702 u32 gracetime = nla_get_u32(attr);
1704 if (gracetime < 10 || gracetime > 3600)
1707 nn->nfsd4_grace = gracetime;
1710 attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1712 u32 leasetime = nla_get_u32(attr);
1714 if (leasetime < 10 || leasetime > 3600)
1717 nn->nfsd4_lease = leasetime;
1720 attr = info->attrs[NFSD_A_SERVER_SCOPE];
1722 scope = nla_data(attr);
1725 ret = nfsd_svc(nthreads, net, get_current_cred(), scope);
1728 mutex_unlock(&nfsd_mutex);
1730 return ret == nthreads ? 0 : ret;
1734 * nfsd_nl_threads_get_doit - get the number of running threads
1735 * @skb: reply buffer
1736 * @info: netlink metadata and command arguments
1738 * Return 0 on success or a negative errno.
1740 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1742 struct net *net = genl_info_net(info);
1743 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1747 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1751 hdr = genlmsg_iput(skb, info);
1757 mutex_lock(&nfsd_mutex);
1759 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1761 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1763 nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1768 if (nn->nfsd_serv) {
1771 for (i = 0; i < nfsd_nrpools(net); ++i) {
1772 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1774 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1775 atomic_read(&sp->sp_nrthreads));
1780 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1785 mutex_unlock(&nfsd_mutex);
1787 genlmsg_end(skb, hdr);
1789 return genlmsg_reply(skb, info);
1792 mutex_unlock(&nfsd_mutex);
1800 * nfsd_nl_version_set_doit - set the nfs enabled versions
1801 * @skb: reply buffer
1802 * @info: netlink metadata and command arguments
1804 * Return 0 on success or a negative errno.
1806 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1808 const struct nlattr *attr;
1809 struct nfsd_net *nn;
1812 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1815 mutex_lock(&nfsd_mutex);
1817 nn = net_generic(genl_info_net(info), nfsd_net_id);
1818 if (nn->nfsd_serv) {
1819 mutex_unlock(&nfsd_mutex);
1823 /* clear current supported versions. */
1824 nfsd_vers(nn, 2, NFSD_CLEAR);
1825 nfsd_vers(nn, 3, NFSD_CLEAR);
1826 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1827 nfsd_minorversion(nn, i, NFSD_CLEAR);
1829 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1830 struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1831 u32 major, minor = 0;
1834 if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION)
1837 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1838 nfsd_version_nl_policy, info->extack) < 0)
1841 if (!tb[NFSD_A_VERSION_MAJOR])
1844 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1845 if (tb[NFSD_A_VERSION_MINOR])
1846 minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1848 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1852 nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1857 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1864 mutex_unlock(&nfsd_mutex);
1870 * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1871 * @skb: reply buffer
1872 * @info: netlink metadata and command arguments
1874 * Return 0 on success or a negative errno.
1876 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1878 struct nfsd_net *nn;
1882 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1886 hdr = genlmsg_iput(skb, info);
1892 mutex_lock(&nfsd_mutex);
1893 nn = net_generic(genl_info_net(info), nfsd_net_id);
1895 for (i = 2; i <= 4; i++) {
1898 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1899 struct nlattr *attr;
1901 /* Don't record any versions the kernel doesn't have
1904 if (!nfsd_support_version(i))
1907 /* NFSv{2,3} does not support minor numbers */
1911 attr = nla_nest_start(skb,
1912 NFSD_A_SERVER_PROTO_VERSION);
1915 goto err_nfsd_unlock;
1918 if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1919 nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1921 goto err_nfsd_unlock;
1924 /* Set the enabled flag if the version is enabled */
1925 if (nfsd_vers(nn, i, NFSD_TEST) &&
1926 (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1927 nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1929 goto err_nfsd_unlock;
1932 nla_nest_end(skb, attr);
1936 mutex_unlock(&nfsd_mutex);
1937 genlmsg_end(skb, hdr);
1939 return genlmsg_reply(skb, info);
1942 mutex_unlock(&nfsd_mutex);
1950 * nfsd_nl_listener_set_doit - set the nfs running sockets
1951 * @skb: reply buffer
1952 * @info: netlink metadata and command arguments
1954 * Return 0 on success or a negative errno.
1956 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1958 struct net *net = genl_info_net(info);
1959 struct svc_xprt *xprt, *tmp;
1960 const struct nlattr *attr;
1961 struct svc_serv *serv;
1962 LIST_HEAD(permsocks);
1963 struct nfsd_net *nn;
1966 mutex_lock(&nfsd_mutex);
1968 err = nfsd_create_serv(net);
1970 mutex_unlock(&nfsd_mutex);
1974 nn = net_generic(net, nfsd_net_id);
1975 serv = nn->nfsd_serv;
1977 spin_lock_bh(&serv->sv_lock);
1979 /* Move all of the old listener sockets to a temp list */
1980 list_splice_init(&serv->sv_permsocks, &permsocks);
1983 * Walk the list of server_socks from userland and move any that match
1984 * back to sv_permsocks
1986 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1987 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1988 const char *xcl_name;
1989 struct sockaddr *sa;
1991 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
1994 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1995 nfsd_sock_nl_policy, info->extack) < 0)
1998 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2001 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2004 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2005 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2007 /* Put back any matching sockets */
2008 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
2009 /* This shouldn't be possible */
2010 if (WARN_ON_ONCE(xprt->xpt_net != net)) {
2011 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2015 /* If everything matches, put it back */
2016 if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
2017 rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
2018 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2024 /* For now, no removing old sockets while server is running */
2025 if (serv->sv_nrthreads && !list_empty(&permsocks)) {
2026 list_splice_init(&permsocks, &serv->sv_permsocks);
2027 spin_unlock_bh(&serv->sv_lock);
2029 goto out_unlock_mtx;
2032 /* Close the remaining sockets on the permsocks list */
2033 while (!list_empty(&permsocks)) {
2034 xprt = list_first_entry(&permsocks, struct svc_xprt, xpt_list);
2035 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2038 * Newly-created sockets are born with the BUSY bit set. Clear
2039 * it if there are no threads, since nothing can pick it up
2042 if (!serv->sv_nrthreads)
2043 clear_bit(XPT_BUSY, &xprt->xpt_flags);
2045 set_bit(XPT_CLOSE, &xprt->xpt_flags);
2046 spin_unlock_bh(&serv->sv_lock);
2047 svc_xprt_close(xprt);
2048 spin_lock_bh(&serv->sv_lock);
2051 spin_unlock_bh(&serv->sv_lock);
2053 /* walk list of addrs again, open any that still don't exist */
2054 nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
2055 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2056 const char *xcl_name;
2057 struct sockaddr *sa;
2060 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
2063 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2064 nfsd_sock_nl_policy, info->extack) < 0)
2067 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2070 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2073 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2074 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2076 xprt = svc_find_listener(serv, xcl_name, net, sa);
2082 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa,
2084 get_current_cred());
2085 /* always save the latest error */
2090 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2091 nfsd_destroy_serv(net);
2094 mutex_unlock(&nfsd_mutex);
2100 * nfsd_nl_listener_get_doit - get the nfs running listeners
2101 * @skb: reply buffer
2102 * @info: netlink metadata and command arguments
2104 * Return 0 on success or a negative errno.
2106 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2108 struct svc_xprt *xprt;
2109 struct svc_serv *serv;
2110 struct nfsd_net *nn;
2114 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2118 hdr = genlmsg_iput(skb, info);
2124 mutex_lock(&nfsd_mutex);
2125 nn = net_generic(genl_info_net(info), nfsd_net_id);
2127 /* no nfs server? Just send empty socket list */
2129 goto out_unlock_mtx;
2131 serv = nn->nfsd_serv;
2132 spin_lock_bh(&serv->sv_lock);
2133 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2134 struct nlattr *attr;
2136 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2139 goto err_serv_unlock;
2142 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2143 xprt->xpt_class->xcl_name) ||
2144 nla_put(skb, NFSD_A_SOCK_ADDR,
2145 sizeof(struct sockaddr_storage),
2146 &xprt->xpt_local)) {
2148 goto err_serv_unlock;
2151 nla_nest_end(skb, attr);
2153 spin_unlock_bh(&serv->sv_lock);
2155 mutex_unlock(&nfsd_mutex);
2156 genlmsg_end(skb, hdr);
2158 return genlmsg_reply(skb, info);
2161 spin_unlock_bh(&serv->sv_lock);
2162 mutex_unlock(&nfsd_mutex);
2170 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2171 * @net: a freshly-created network namespace
2173 * This information stays around as long as the network namespace is
2174 * alive whether or not there is an NFSD instance running in the
2177 * Returns zero on success, or a negative errno otherwise.
2179 static __net_init int nfsd_net_init(struct net *net)
2182 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2184 retval = nfsd_export_init(net);
2186 goto out_export_error;
2187 retval = nfsd_idmap_init(net);
2189 goto out_idmap_error;
2190 retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2191 NFSD_STATS_COUNTERS_NUM);
2193 goto out_repcache_error;
2194 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2195 nn->nfsd_svcstats.program = &nfsd_program;
2196 nn->nfsd_versions = NULL;
2197 nn->nfsd4_minorversions = NULL;
2198 nfsd4_init_leases_net(nn);
2199 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2200 seqlock_init(&nn->writeverf_lock);
2201 nfsd_proc_stat_init(net);
2206 nfsd_idmap_shutdown(net);
2208 nfsd_export_shutdown(net);
2214 * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2215 * @net: a network namespace that is about to be destroyed
2218 static __net_exit void nfsd_net_exit(struct net *net)
2220 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2222 nfsd_proc_stat_shutdown(net);
2223 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2224 nfsd_idmap_shutdown(net);
2225 nfsd_export_shutdown(net);
2226 nfsd_netns_free_versions(nn);
2229 static struct pernet_operations nfsd_net_ops = {
2230 .init = nfsd_net_init,
2231 .exit = nfsd_net_exit,
2233 .size = sizeof(struct nfsd_net),
2236 static int __init init_nfsd(void)
2240 retval = nfsd4_init_slabs();
2243 retval = nfsd4_init_pnfs();
2245 goto out_free_slabs;
2246 retval = nfsd_drc_slab_create();
2249 nfsd_lockd_init(); /* lockd->nfsd callbacks */
2250 retval = create_proc_exports_entry();
2252 goto out_free_lockd;
2253 retval = register_pernet_subsys(&nfsd_net_ops);
2255 goto out_free_exports;
2256 retval = register_cld_notifier();
2258 goto out_free_subsys;
2259 retval = nfsd4_create_laundry_wq();
2262 retval = register_filesystem(&nfsd_fs_type);
2265 retval = genl_register_family(&nfsd_nl_family);
2271 nfsd4_destroy_laundry_wq();
2273 unregister_cld_notifier();
2275 unregister_pernet_subsys(&nfsd_net_ops);
2277 remove_proc_entry("fs/nfs/exports", NULL);
2278 remove_proc_entry("fs/nfs", NULL);
2280 nfsd_lockd_shutdown();
2281 nfsd_drc_slab_free();
2289 static void __exit exit_nfsd(void)
2291 genl_unregister_family(&nfsd_nl_family);
2292 unregister_filesystem(&nfsd_fs_type);
2293 nfsd4_destroy_laundry_wq();
2294 unregister_cld_notifier();
2295 unregister_pernet_subsys(&nfsd_net_ops);
2296 nfsd_drc_slab_free();
2297 remove_proc_entry("fs/nfs/exports", NULL);
2298 remove_proc_entry("fs/nfs", NULL);
2299 nfsd_lockd_shutdown();
2305 MODULE_DESCRIPTION("In-kernel NFS server");
2306 MODULE_LICENSE("GPL");
2307 module_init(init_nfsd)
2308 module_exit(exit_nfsd)