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/module.h>
19 #include <linux/fsnotify.h>
27 #include "filecache.h"
31 * We have a single directory with several nodes in it.
44 NFSD_Reply_Cache_Stats,
51 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
52 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
64 * write() for these nodes.
66 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
67 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
68 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
69 static ssize_t write_threads(struct file *file, char *buf, size_t size);
70 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
71 static ssize_t write_versions(struct file *file, char *buf, size_t size);
72 static ssize_t write_ports(struct file *file, char *buf, size_t size);
73 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
74 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
76 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
77 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
78 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
79 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
82 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
83 [NFSD_Fh] = write_filehandle,
84 [NFSD_FO_UnlockIP] = write_unlock_ip,
85 [NFSD_FO_UnlockFS] = write_unlock_fs,
86 [NFSD_Threads] = write_threads,
87 [NFSD_Pool_Threads] = write_pool_threads,
88 [NFSD_Versions] = write_versions,
89 [NFSD_Ports] = write_ports,
90 [NFSD_MaxBlkSize] = write_maxblksize,
91 [NFSD_MaxConnections] = write_maxconn,
93 [NFSD_Leasetime] = write_leasetime,
94 [NFSD_Gracetime] = write_gracetime,
95 [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 = nfsd_pool_stats_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);
289 * write_filehandle - Get a variable-length NFS file handle by path
291 * On input, the buffer contains a '\n'-terminated C string comprised of
292 * three alphanumeric words separated by whitespace. The string may
293 * contain escape sequences.
297 * domain: client domain name
298 * path: export pathname
299 * maxsize: numeric maximum size of
301 * size: length of C string in @buf
303 * On success: passed-in buffer filled with '\n'-terminated C
304 * string containing a ASCII hex text version
305 * of the NFS file handle;
306 * return code is the size in bytes of the string
307 * On error: return code is negative errno value
309 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
315 struct auth_domain *dom;
321 if (buf[size-1] != '\n')
326 len = qword_get(&mesg, dname, size);
331 len = qword_get(&mesg, path, size);
335 len = get_int(&mesg, &maxsize);
339 if (maxsize < NFS_FHSIZE)
341 maxsize = min(maxsize, NFS3_FHSIZE);
343 if (qword_get(&mesg, mesg, size) > 0)
346 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
348 /* we have all the words, they are in buf.. */
349 dom = unix_domain_find(dname);
353 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
354 auth_domain_put(dom);
359 len = SIMPLE_TRANSACTION_LIMIT;
360 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
366 * write_threads - Start NFSD, or report the current number of running threads
372 * On success: passed-in buffer filled with '\n'-terminated C
373 * string numeric value representing the number of
374 * running NFSD threads;
375 * return code is the size in bytes of the string
376 * On error: return code is zero
381 * buf: C string containing an unsigned
382 * integer value representing the
383 * number of NFSD threads to start
384 * size: non-zero length of C string in @buf
386 * On success: NFS service is started;
387 * passed-in buffer filled with '\n'-terminated C
388 * string numeric value representing the number of
389 * running NFSD threads;
390 * return code is the size in bytes of the string
391 * On error: return code is zero or a negative errno value
393 static ssize_t write_threads(struct file *file, char *buf, size_t size)
397 struct net *net = netns(file);
401 rv = get_int(&mesg, &newthreads);
406 trace_nfsd_ctl_threads(net, newthreads);
407 rv = nfsd_svc(newthreads, net, file->f_cred);
411 rv = nfsd_nrthreads(net);
413 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
417 * write_pool_threads - Set or report the current number of threads per pool
426 * buf: C string containing whitespace-
427 * separated unsigned integer values
428 * representing the number of NFSD
429 * threads to start in each pool
430 * size: non-zero length of C string in @buf
432 * On success: passed-in buffer filled with '\n'-terminated C
433 * string containing integer values representing the
434 * number of NFSD threads in each pool;
435 * return code is the size in bytes of the string
436 * On error: return code is zero or a negative errno value
438 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
440 /* if size > 0, look for an array of number of threads per node
441 * and apply them then write out number of threads per node as reply
449 struct net *net = netns(file);
451 mutex_lock(&nfsd_mutex);
452 npools = nfsd_nrpools(net);
455 * NFS is shut down. The admin can start it by
456 * writing to the threads file but NOT the pool_threads
457 * file, sorry. Report zero threads.
459 mutex_unlock(&nfsd_mutex);
464 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
466 if (nthreads == NULL)
470 for (i = 0; i < npools; i++) {
471 rv = get_int(&mesg, &nthreads[i]);
473 break; /* fewer numbers than pools */
475 goto out_free; /* syntax error */
479 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
481 rv = nfsd_set_nrthreads(i, nthreads, net);
486 rv = nfsd_get_nrthreads(npools, nthreads, net);
491 size = SIMPLE_TRANSACTION_LIMIT;
492 for (i = 0; i < npools && size > 0; i++) {
493 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
501 mutex_unlock(&nfsd_mutex);
506 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
507 const char *sep, unsigned vers, int minor)
509 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
510 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
512 if (vers == 4 && minor >= 0 &&
513 !nfsd_minorversion(nn, minor, NFSD_TEST))
515 if (minor == 0 && supported)
517 * special case for backward compatability.
518 * +4.0 is never reported, it is implied by
519 * +4, unless -4.0 is present.
522 return snprintf(buf, remaining, format, sep,
523 supported ? '+' : '-', vers, minor);
526 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
529 char *vers, *minorp, sign;
530 int len, num, remaining;
533 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
537 /* Cannot change versions without updating
538 * nn->nfsd_serv->sv_xdrsize, and reallocing
539 * rq_argp and rq_resp
542 if (buf[size-1] != '\n')
545 trace_nfsd_ctl_version(netns(file), buf);
548 len = qword_get(&mesg, vers, size);
549 if (len <= 0) return -EINVAL;
554 if (sign == '+' || sign == '-')
555 num = simple_strtol((vers+1), &minorp, 0);
557 num = simple_strtol(vers, &minorp, 0);
558 if (*minorp == '.') {
561 if (kstrtouint(minorp+1, 0, &minor) < 0)
565 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
567 #ifdef CONFIG_NFSD_V2
571 nfsd_vers(nn, num, cmd);
574 if (*minorp == '.') {
575 if (nfsd_minorversion(nn, minor, cmd) < 0)
577 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
579 * Either we have +4 and no minors are enabled,
580 * or we have -4 and at least one minor is enabled.
581 * In either case, propagate 'cmd' to all minors.
584 while (nfsd_minorversion(nn, minor, cmd) >= 0)
589 /* Ignore requests to disable non-existent versions */
594 } while ((len = qword_get(&mesg, vers, size)) > 0);
595 /* If all get turned off, turn them back on, as
596 * having no versions is BAD
598 nfsd_reset_versions(nn);
601 /* Now write current state into reply buffer */
603 remaining = SIMPLE_TRANSACTION_LIMIT;
604 for (num=2 ; num <= 4 ; num++) {
606 if (!nfsd_vers(nn, num, NFSD_AVAIL))
611 len = nfsd_print_version_support(nn, buf, remaining,
613 if (len >= remaining)
621 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
624 len = snprintf(buf, remaining, "\n");
625 if (len >= remaining)
631 * write_versions - Set or report the available NFS protocol versions
637 * On success: passed-in buffer filled with '\n'-terminated C
638 * string containing positive or negative integer
639 * values representing the current status of each
641 * return code is the size in bytes of the string
642 * On error: return code is zero or a negative errno value
647 * buf: C string containing whitespace-
648 * separated positive or negative
649 * integer values representing NFS
650 * protocol versions to enable ("+n")
652 * size: non-zero length of C string in @buf
654 * On success: status of zero or more protocol versions has
655 * been updated; passed-in buffer filled with
656 * '\n'-terminated C string containing positive
657 * or negative integer values representing the
658 * current status of each protocol version;
659 * return code is the size in bytes of the string
660 * On error: return code is zero or a negative errno value
662 static ssize_t write_versions(struct file *file, char *buf, size_t size)
666 mutex_lock(&nfsd_mutex);
667 rv = __write_versions(file, buf, size);
668 mutex_unlock(&nfsd_mutex);
673 * Zero-length write. Return a list of NFSD's current listener
676 static ssize_t __write_ports_names(char *buf, struct net *net)
678 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
680 if (nn->nfsd_serv == NULL)
682 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
686 * A single 'fd' number was written, in which case it must be for
687 * a socket of a supported family/protocol, and we use it as an
690 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
694 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
696 err = get_int(&mesg, &fd);
697 if (err != 0 || fd < 0)
699 trace_nfsd_ctl_ports_addfd(net, fd);
701 err = nfsd_create_serv(net);
705 err = svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
707 if (err < 0 && !nn->nfsd_serv->sv_nrthreads && !nn->keep_active)
708 nfsd_last_thread(net);
710 !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
711 svc_get(nn->nfsd_serv);
718 * A transport listener is added by writing its transport name and
721 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
724 struct svc_xprt *xprt;
726 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
728 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
731 if (port < 1 || port > USHRT_MAX)
733 trace_nfsd_ctl_ports_addxprt(net, transport, port);
735 err = nfsd_create_serv(net);
739 err = svc_xprt_create(nn->nfsd_serv, transport, net,
740 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
744 err = svc_xprt_create(nn->nfsd_serv, transport, net,
745 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
746 if (err < 0 && err != -EAFNOSUPPORT)
749 if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
750 svc_get(nn->nfsd_serv);
755 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
757 svc_xprt_close(xprt);
761 if (!nn->nfsd_serv->sv_nrthreads && !nn->keep_active)
762 nfsd_last_thread(net);
768 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
772 return __write_ports_names(buf, net);
775 return __write_ports_addfd(buf, net, file->f_cred);
778 return __write_ports_addxprt(buf, net, file->f_cred);
784 * write_ports - Pass a socket file descriptor or transport name to listen on
790 * On success: passed-in buffer filled with a '\n'-terminated C
791 * string containing a whitespace-separated list of
792 * named NFSD listeners;
793 * return code is the size in bytes of the string
794 * On error: return code is zero or a negative errno value
799 * buf: C string containing an unsigned
800 * integer value representing a bound
801 * but unconnected socket that is to be
802 * used as an NFSD listener; listen(3)
803 * must be called for a SOCK_STREAM
804 * socket, otherwise it is ignored
805 * size: non-zero length of C string in @buf
807 * On success: NFS service is started;
808 * passed-in buffer filled with a '\n'-terminated C
809 * string containing a unique alphanumeric name of
811 * return code is the size in bytes of the string
812 * On error: return code is a negative errno value
817 * buf: C string containing a transport
818 * name and an unsigned integer value
819 * representing the port to listen on,
820 * separated by whitespace
821 * size: non-zero length of C string in @buf
823 * On success: returns zero; NFS service is started
824 * On error: return code is a negative errno value
826 static ssize_t write_ports(struct file *file, char *buf, size_t size)
830 mutex_lock(&nfsd_mutex);
831 rv = __write_ports(file, buf, size, netns(file));
832 mutex_unlock(&nfsd_mutex);
837 int nfsd_max_blksize;
840 * write_maxblksize - Set or report the current NFS blksize
849 * buf: C string containing an unsigned
850 * integer value representing the new
852 * size: non-zero length of C string in @buf
854 * On success: passed-in buffer filled with '\n'-terminated C string
855 * containing numeric value of the current NFS blksize
857 * return code is the size in bytes of the string
858 * On error: return code is zero or a negative errno value
860 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
863 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
867 int rv = get_int(&mesg, &bsize);
870 trace_nfsd_ctl_maxblksize(netns(file), bsize);
872 /* force bsize into allowed range and
873 * required alignment.
875 bsize = max_t(int, bsize, 1024);
876 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
878 mutex_lock(&nfsd_mutex);
880 mutex_unlock(&nfsd_mutex);
883 nfsd_max_blksize = bsize;
884 mutex_unlock(&nfsd_mutex);
887 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
892 * write_maxconn - Set or report the current max number of connections
900 * buf: C string containing an unsigned
901 * integer value representing the new
902 * number of max connections
903 * size: non-zero length of C string in @buf
905 * On success: passed-in buffer filled with '\n'-terminated C string
906 * containing numeric value of max_connections setting
907 * for this net namespace;
908 * return code is the size in bytes of the string
909 * On error: return code is zero or a negative errno value
911 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
914 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
915 unsigned int maxconn = nn->max_connections;
918 int rv = get_uint(&mesg, &maxconn);
922 trace_nfsd_ctl_maxconn(netns(file), maxconn);
923 nn->max_connections = maxconn;
926 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
929 #ifdef CONFIG_NFSD_V4
930 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
931 time64_t *time, struct nfsd_net *nn)
933 struct dentry *dentry = file_dentry(file);
940 rv = get_int(&mesg, &i);
943 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
944 dentry->d_name.len, i);
947 * Some sanity checking. We don't have a reason for
948 * these particular numbers, but problems with the
950 * - Too short: the briefest network outage may
951 * cause clients to lose all their locks. Also,
952 * the frequent polling may be wasteful.
953 * - Too long: do you really want reboot recovery
954 * to take more than an hour? Or to make other
955 * clients wait an hour before being able to
956 * revoke a dead client's locks?
958 if (i < 10 || i > 3600)
963 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
966 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
967 time64_t *time, struct nfsd_net *nn)
971 mutex_lock(&nfsd_mutex);
972 rv = __nfsd4_write_time(file, buf, size, time, nn);
973 mutex_unlock(&nfsd_mutex);
978 * write_leasetime - Set or report the current NFSv4 lease time
987 * buf: C string containing an unsigned
988 * integer value representing the new
989 * NFSv4 lease expiry time
990 * size: non-zero length of C string in @buf
992 * On success: passed-in buffer filled with '\n'-terminated C
993 * string containing unsigned integer value of the
994 * current lease expiry time;
995 * return code is the size in bytes of the string
996 * On error: return code is zero or a negative errno value
998 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1000 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1001 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1005 * write_gracetime - Set or report current NFSv4 grace period time
1007 * As above, but sets the time of the NFSv4 grace period.
1009 * Note this should never be set to less than the *previous*
1010 * lease-period time, but we don't try to enforce this. (In the common
1011 * case (a new boot), we don't know what the previous lease time was
1014 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1016 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1017 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1020 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1021 struct nfsd_net *nn)
1030 if (size > PATH_MAX || buf[size-1] != '\n')
1035 len = qword_get(&mesg, recdir, size);
1038 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1040 status = nfs4_reset_recoverydir(recdir);
1045 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1046 nfs4_recoverydir());
1050 * write_recoverydir - Set or report the pathname of the recovery directory
1059 * buf: C string containing the pathname
1060 * of the directory on a local file
1061 * system containing permanent NFSv4
1063 * size: non-zero length of C string in @buf
1065 * On success: passed-in buffer filled with '\n'-terminated C string
1066 * containing the current recovery pathname setting;
1067 * return code is the size in bytes of the string
1068 * On error: return code is zero or a negative errno value
1070 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1073 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1075 mutex_lock(&nfsd_mutex);
1076 rv = __write_recoverydir(file, buf, size, nn);
1077 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 inode->i_atime = inode->i_mtime = inode_set_ctime_current(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 inode *inode)
1245 struct nfsdfs_client *ncl = inode->i_private;
1247 inode->i_private = NULL;
1248 kref_put(&ncl->cl_ref, ncl->cl_release);
1251 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1253 struct nfsdfs_client *nc = inode->i_private;
1256 kref_get(&nc->cl_ref);
1260 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1262 struct nfsdfs_client *nc;
1264 inode_lock_shared(inode);
1265 nc = __get_nfsdfs_client(inode);
1266 inode_unlock_shared(inode);
1269 /* from __rpc_unlink */
1270 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1274 clear_ncl(d_inode(dentry));
1276 ret = simple_unlink(dir, dentry);
1278 fsnotify_unlink(dir, dentry);
1283 static void nfsdfs_remove_files(struct dentry *root)
1285 struct dentry *dentry, *tmp;
1287 list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1288 if (!simple_positive(dentry)) {
1289 WARN_ON_ONCE(1); /* I think this can't happen? */
1292 nfsdfs_remove_file(d_inode(root), dentry);
1296 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1298 static int nfsdfs_create_files(struct dentry *root,
1299 const struct tree_descr *files,
1300 struct dentry **fdentries)
1302 struct inode *dir = d_inode(root);
1303 struct inode *inode;
1304 struct dentry *dentry;
1308 for (i = 0; files->name && files->name[0]; i++, files++) {
1309 dentry = d_alloc_name(root, files->name);
1312 inode = nfsd_get_inode(d_inode(root)->i_sb,
1313 S_IFREG | files->mode);
1318 inode->i_fop = files->ops;
1319 inode->i_private = __get_nfsdfs_client(dir);
1320 d_add(dentry, inode);
1321 fsnotify_create(dir, dentry);
1323 fdentries[i] = dentry;
1328 nfsdfs_remove_files(root);
1333 /* on success, returns positive number unique to that client. */
1334 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1335 struct nfsdfs_client *ncl, u32 id,
1336 const struct tree_descr *files,
1337 struct dentry **fdentries)
1339 struct dentry *dentry;
1343 sprintf(name, "%u", id);
1345 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1346 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1348 ret = nfsdfs_create_files(dentry, files, fdentries);
1350 nfsd_client_rmdir(dentry);
1356 /* Taken from __rpc_rmdir: */
1357 void nfsd_client_rmdir(struct dentry *dentry)
1359 struct inode *dir = d_inode(dentry->d_parent);
1360 struct inode *inode = d_inode(dentry);
1364 nfsdfs_remove_files(dentry);
1367 ret = simple_rmdir(dir, dentry);
1370 fsnotify_rmdir(dir, dentry);
1375 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1377 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1379 struct dentry *dentry;
1382 static const struct tree_descr nfsd_files[] = {
1383 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1384 /* Per-export io stats use same ops as exports file */
1385 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1386 [NFSD_Export_features] = {"export_features",
1387 &export_features_fops, S_IRUGO},
1388 [NFSD_FO_UnlockIP] = {"unlock_ip",
1389 &transaction_ops, S_IWUSR|S_IRUSR},
1390 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1391 &transaction_ops, S_IWUSR|S_IRUSR},
1392 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1393 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1394 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1395 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1396 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1397 &nfsd_reply_cache_stats_fops, S_IRUGO},
1398 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1399 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1400 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1401 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1402 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1403 #ifdef CONFIG_NFSD_V4
1404 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1405 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1406 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1407 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1412 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1415 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1416 "/proc/net/rpc/gss_krb5_enctypes");
1417 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1419 return PTR_ERR(dentry);
1420 nn->nfsd_client_dir = dentry;
1424 static int nfsd_fs_get_tree(struct fs_context *fc)
1426 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1429 static void nfsd_fs_free_fc(struct fs_context *fc)
1432 put_net(fc->s_fs_info);
1435 static const struct fs_context_operations nfsd_fs_context_ops = {
1436 .free = nfsd_fs_free_fc,
1437 .get_tree = nfsd_fs_get_tree,
1440 static int nfsd_init_fs_context(struct fs_context *fc)
1442 put_user_ns(fc->user_ns);
1443 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1444 fc->ops = &nfsd_fs_context_ops;
1448 static void nfsd_umount(struct super_block *sb)
1450 struct net *net = sb->s_fs_info;
1452 nfsd_shutdown_threads(net);
1454 kill_litter_super(sb);
1458 static struct file_system_type nfsd_fs_type = {
1459 .owner = THIS_MODULE,
1461 .init_fs_context = nfsd_init_fs_context,
1462 .kill_sb = nfsd_umount,
1464 MODULE_ALIAS_FS("nfsd");
1466 #ifdef CONFIG_PROC_FS
1468 static int exports_proc_open(struct inode *inode, struct file *file)
1470 return exports_net_open(current->nsproxy->net_ns, file);
1473 static const struct proc_ops exports_proc_ops = {
1474 .proc_open = exports_proc_open,
1475 .proc_read = seq_read,
1476 .proc_lseek = seq_lseek,
1477 .proc_release = seq_release,
1480 static int create_proc_exports_entry(void)
1482 struct proc_dir_entry *entry;
1484 entry = proc_mkdir("fs/nfs", NULL);
1487 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1489 remove_proc_entry("fs/nfs", NULL);
1494 #else /* CONFIG_PROC_FS */
1495 static int create_proc_exports_entry(void)
1501 unsigned int nfsd_net_id;
1504 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
1505 * @net: a freshly-created network namespace
1507 * This information stays around as long as the network namespace is
1508 * alive whether or not there is an NFSD instance running in the
1511 * Returns zero on success, or a negative errno otherwise.
1513 static __net_init int nfsd_net_init(struct net *net)
1516 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1518 retval = nfsd_export_init(net);
1520 goto out_export_error;
1521 retval = nfsd_idmap_init(net);
1523 goto out_idmap_error;
1524 retval = nfsd_net_reply_cache_init(nn);
1526 goto out_repcache_error;
1527 nn->nfsd_versions = NULL;
1528 nn->nfsd4_minorversions = NULL;
1529 nfsd4_init_leases_net(nn);
1530 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1531 seqlock_init(&nn->writeverf_lock);
1536 nfsd_idmap_shutdown(net);
1538 nfsd_export_shutdown(net);
1544 * nfsd_net_exit - Release the nfsd_net portion of a net namespace
1545 * @net: a network namespace that is about to be destroyed
1548 static __net_exit void nfsd_net_exit(struct net *net)
1550 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1552 nfsd_net_reply_cache_destroy(nn);
1553 nfsd_idmap_shutdown(net);
1554 nfsd_export_shutdown(net);
1555 nfsd_netns_free_versions(nn);
1558 static struct pernet_operations nfsd_net_ops = {
1559 .init = nfsd_net_init,
1560 .exit = nfsd_net_exit,
1562 .size = sizeof(struct nfsd_net),
1565 static int __init init_nfsd(void)
1569 retval = nfsd4_init_slabs();
1572 retval = nfsd4_init_pnfs();
1574 goto out_free_slabs;
1575 retval = nfsd_stat_init(); /* Statistics */
1578 retval = nfsd_drc_slab_create();
1581 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1582 retval = create_proc_exports_entry();
1584 goto out_free_lockd;
1585 retval = register_pernet_subsys(&nfsd_net_ops);
1587 goto out_free_exports;
1588 retval = register_cld_notifier();
1590 goto out_free_subsys;
1591 retval = nfsd4_create_laundry_wq();
1594 retval = register_filesystem(&nfsd_fs_type);
1599 nfsd4_destroy_laundry_wq();
1601 unregister_cld_notifier();
1603 unregister_pernet_subsys(&nfsd_net_ops);
1605 remove_proc_entry("fs/nfs/exports", NULL);
1606 remove_proc_entry("fs/nfs", NULL);
1608 nfsd_lockd_shutdown();
1609 nfsd_drc_slab_free();
1611 nfsd_stat_shutdown();
1619 static void __exit exit_nfsd(void)
1621 unregister_filesystem(&nfsd_fs_type);
1622 nfsd4_destroy_laundry_wq();
1623 unregister_cld_notifier();
1624 unregister_pernet_subsys(&nfsd_net_ops);
1625 nfsd_drc_slab_free();
1626 remove_proc_entry("fs/nfs/exports", NULL);
1627 remove_proc_entry("fs/nfs", NULL);
1628 nfsd_stat_shutdown();
1629 nfsd_lockd_shutdown();
1634 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1635 MODULE_DESCRIPTION("In-kernel NFS server");
1636 MODULE_LICENSE("GPL");
1637 module_init(init_nfsd)
1638 module_exit(exit_nfsd)