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"
32 * We have a single directory with several nodes in it.
45 NFSD_Reply_Cache_Stats,
61 * write() for these nodes.
63 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
64 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
65 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
66 static ssize_t write_threads(struct file *file, char *buf, size_t size);
67 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
68 static ssize_t write_versions(struct file *file, char *buf, size_t size);
69 static ssize_t write_ports(struct file *file, char *buf, size_t size);
70 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
71 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
73 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
74 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
75 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
76 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
78 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
81 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
82 [NFSD_Fh] = write_filehandle,
83 [NFSD_FO_UnlockIP] = write_unlock_ip,
84 [NFSD_FO_UnlockFS] = write_unlock_fs,
85 [NFSD_Threads] = write_threads,
86 [NFSD_Pool_Threads] = write_pool_threads,
87 [NFSD_Versions] = write_versions,
88 [NFSD_Ports] = write_ports,
89 [NFSD_MaxBlkSize] = write_maxblksize,
90 [NFSD_MaxConnections] = write_maxconn,
92 [NFSD_Leasetime] = write_leasetime,
93 [NFSD_Gracetime] = write_gracetime,
94 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
95 [NFSD_RecoveryDir] = write_recoverydir,
97 [NFSD_V4EndGrace] = write_v4_end_grace,
101 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
103 ino_t ino = file_inode(file)->i_ino;
107 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
110 data = simple_transaction_get(file, buf, size);
112 return PTR_ERR(data);
114 rv = write_op[ino](file, data, size);
118 simple_transaction_set(file, rv);
122 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
124 if (! file->private_data) {
125 /* An attempt to read a transaction file without writing
126 * causes a 0-byte write so that the file can return
129 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
133 return simple_transaction_read(file, buf, size, pos);
136 static const struct file_operations transaction_ops = {
137 .write = nfsctl_transaction_write,
138 .read = nfsctl_transaction_read,
139 .release = simple_transaction_release,
140 .llseek = default_llseek,
143 static int exports_net_open(struct net *net, struct file *file)
146 struct seq_file *seq;
147 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
149 err = seq_open(file, &nfs_exports_op);
153 seq = file->private_data;
154 seq->private = nn->svc_export_cache;
158 static int exports_nfsd_open(struct inode *inode, struct file *file)
160 return exports_net_open(inode->i_sb->s_fs_info, file);
163 static const struct file_operations exports_nfsd_operations = {
164 .open = exports_nfsd_open,
167 .release = seq_release,
170 static int export_features_show(struct seq_file *m, void *v)
172 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
176 DEFINE_SHOW_ATTRIBUTE(export_features);
178 static const struct file_operations pool_stats_operations = {
179 .open = nfsd_pool_stats_open,
182 .release = seq_release,
185 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
187 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
189 /*----------------------------------------------------------------------------*/
191 * payload - write methods
194 static inline struct net *netns(struct file *file)
196 return file_inode(file)->i_sb->s_fs_info;
200 * write_unlock_ip - Release all locks used by a client
205 * buf: '\n'-terminated C string containing a
206 * presentation format IP address
207 * size: length of C string in @buf
209 * On success: returns zero if all specified locks were released;
210 * returns one if one or more locks were not released
211 * On error: return code is negative errno value
213 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
215 struct sockaddr_storage address;
216 struct sockaddr *sap = (struct sockaddr *)&address;
217 size_t salen = sizeof(address);
219 struct net *net = netns(file);
225 if (buf[size-1] != '\n')
229 if (qword_get(&buf, fo_path, size) < 0)
232 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
235 trace_nfsd_ctl_unlock_ip(net, buf);
236 return nlmsvc_unlock_all_by_ip(sap);
240 * write_unlock_fs - Release all locks on a local file system
245 * buf: '\n'-terminated C string containing the
246 * absolute pathname of a local file system
247 * size: length of C string in @buf
249 * On success: returns zero if all specified locks were released;
250 * returns one if one or more locks were not released
251 * On error: return code is negative errno value
253 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
263 if (buf[size-1] != '\n')
267 if (qword_get(&buf, fo_path, size) < 0)
269 trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
270 error = kern_path(fo_path, 0, &path);
275 * XXX: Needs better sanity checking. Otherwise we could end up
276 * releasing locks on the wrong file system.
279 * 1. Does the path refer to a directory?
280 * 2. Is that directory a mount point, or
281 * 3. Is that directory the root of an exported file system?
283 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
284 nfsd4_revoke_states(netns(file), path.dentry->d_sb);
291 * write_filehandle - Get a variable-length NFS file handle by path
293 * On input, the buffer contains a '\n'-terminated C string comprised of
294 * three alphanumeric words separated by whitespace. The string may
295 * contain escape sequences.
299 * domain: client domain name
300 * path: export pathname
301 * maxsize: numeric maximum size of
303 * size: length of C string in @buf
305 * On success: passed-in buffer filled with '\n'-terminated C
306 * string containing a ASCII hex text version
307 * of the NFS file handle;
308 * return code is the size in bytes of the string
309 * On error: return code is negative errno value
311 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
317 struct auth_domain *dom;
323 if (buf[size-1] != '\n')
328 len = qword_get(&mesg, dname, size);
333 len = qword_get(&mesg, path, size);
337 len = get_int(&mesg, &maxsize);
341 if (maxsize < NFS_FHSIZE)
343 maxsize = min(maxsize, NFS3_FHSIZE);
345 if (qword_get(&mesg, mesg, size) > 0)
348 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
350 /* we have all the words, they are in buf.. */
351 dom = unix_domain_find(dname);
355 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
356 auth_domain_put(dom);
361 len = SIMPLE_TRANSACTION_LIMIT;
362 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
368 * write_threads - Start NFSD, or report the current number of running threads
374 * On success: passed-in buffer filled with '\n'-terminated C
375 * string numeric value representing the number of
376 * running NFSD threads;
377 * return code is the size in bytes of the string
378 * On error: return code is zero
383 * buf: C string containing an unsigned
384 * integer value representing the
385 * number of NFSD threads to start
386 * size: non-zero length of C string in @buf
388 * On success: NFS service is started;
389 * passed-in buffer filled with '\n'-terminated C
390 * string numeric value representing the number of
391 * running NFSD threads;
392 * return code is the size in bytes of the string
393 * On error: return code is zero or a negative errno value
395 static ssize_t write_threads(struct file *file, char *buf, size_t size)
399 struct net *net = netns(file);
403 rv = get_int(&mesg, &newthreads);
408 trace_nfsd_ctl_threads(net, newthreads);
409 rv = nfsd_svc(newthreads, net, file->f_cred);
413 rv = nfsd_nrthreads(net);
415 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
419 * write_pool_threads - Set or report the current number of threads per pool
428 * buf: C string containing whitespace-
429 * separated unsigned integer values
430 * representing the number of NFSD
431 * threads to start in each pool
432 * size: non-zero length of C string in @buf
434 * On success: passed-in buffer filled with '\n'-terminated C
435 * string containing integer values representing the
436 * number of NFSD threads in each pool;
437 * return code is the size in bytes of the string
438 * On error: return code is zero or a negative errno value
440 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
442 /* if size > 0, look for an array of number of threads per node
443 * and apply them then write out number of threads per node as reply
451 struct net *net = netns(file);
453 mutex_lock(&nfsd_mutex);
454 npools = nfsd_nrpools(net);
457 * NFS is shut down. The admin can start it by
458 * writing to the threads file but NOT the pool_threads
459 * file, sorry. Report zero threads.
461 mutex_unlock(&nfsd_mutex);
466 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
468 if (nthreads == NULL)
472 for (i = 0; i < npools; i++) {
473 rv = get_int(&mesg, &nthreads[i]);
475 break; /* fewer numbers than pools */
477 goto out_free; /* syntax error */
481 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
483 rv = nfsd_set_nrthreads(i, nthreads, net);
488 rv = nfsd_get_nrthreads(npools, nthreads, net);
493 size = SIMPLE_TRANSACTION_LIMIT;
494 for (i = 0; i < npools && size > 0; i++) {
495 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
503 mutex_unlock(&nfsd_mutex);
508 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
509 const char *sep, unsigned vers, int minor)
511 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
512 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
514 if (vers == 4 && minor >= 0 &&
515 !nfsd_minorversion(nn, minor, NFSD_TEST))
517 if (minor == 0 && supported)
519 * special case for backward compatability.
520 * +4.0 is never reported, it is implied by
521 * +4, unless -4.0 is present.
524 return snprintf(buf, remaining, format, sep,
525 supported ? '+' : '-', vers, minor);
528 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
531 char *vers, *minorp, sign;
532 int len, num, remaining;
535 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
539 /* Cannot change versions without updating
540 * nn->nfsd_serv->sv_xdrsize, and reallocing
541 * rq_argp and rq_resp
544 if (buf[size-1] != '\n')
547 trace_nfsd_ctl_version(netns(file), buf);
550 len = qword_get(&mesg, vers, size);
551 if (len <= 0) return -EINVAL;
556 if (sign == '+' || sign == '-')
557 num = simple_strtol((vers+1), &minorp, 0);
559 num = simple_strtol(vers, &minorp, 0);
560 if (*minorp == '.') {
563 if (kstrtouint(minorp+1, 0, &minor) < 0)
567 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
569 #ifdef CONFIG_NFSD_V2
573 nfsd_vers(nn, num, cmd);
576 if (*minorp == '.') {
577 if (nfsd_minorversion(nn, minor, cmd) < 0)
579 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
581 * Either we have +4 and no minors are enabled,
582 * or we have -4 and at least one minor is enabled.
583 * In either case, propagate 'cmd' to all minors.
586 while (nfsd_minorversion(nn, minor, cmd) >= 0)
591 /* Ignore requests to disable non-existent versions */
596 } while ((len = qword_get(&mesg, vers, size)) > 0);
597 /* If all get turned off, turn them back on, as
598 * having no versions is BAD
600 nfsd_reset_versions(nn);
603 /* Now write current state into reply buffer */
605 remaining = SIMPLE_TRANSACTION_LIMIT;
606 for (num=2 ; num <= 4 ; num++) {
608 if (!nfsd_vers(nn, num, NFSD_AVAIL))
613 len = nfsd_print_version_support(nn, buf, remaining,
615 if (len >= remaining)
623 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
626 len = snprintf(buf, remaining, "\n");
627 if (len >= remaining)
633 * write_versions - Set or report the available NFS protocol versions
639 * On success: passed-in buffer filled with '\n'-terminated C
640 * string containing positive or negative integer
641 * values representing the current status of each
643 * return code is the size in bytes of the string
644 * On error: return code is zero or a negative errno value
649 * buf: C string containing whitespace-
650 * separated positive or negative
651 * integer values representing NFS
652 * protocol versions to enable ("+n")
654 * size: non-zero length of C string in @buf
656 * On success: status of zero or more protocol versions has
657 * been updated; passed-in buffer filled with
658 * '\n'-terminated C string containing positive
659 * or negative integer values representing the
660 * current status of each protocol version;
661 * return code is the size in bytes of the string
662 * On error: return code is zero or a negative errno value
664 static ssize_t write_versions(struct file *file, char *buf, size_t size)
668 mutex_lock(&nfsd_mutex);
669 rv = __write_versions(file, buf, size);
670 mutex_unlock(&nfsd_mutex);
675 * Zero-length write. Return a list of NFSD's current listener
678 static ssize_t __write_ports_names(char *buf, struct net *net)
680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
682 if (nn->nfsd_serv == NULL)
684 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
688 * A single 'fd' number was written, in which case it must be for
689 * a socket of a supported family/protocol, and we use it as an
692 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
696 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
697 struct svc_serv *serv;
699 err = get_int(&mesg, &fd);
700 if (err != 0 || fd < 0)
702 trace_nfsd_ctl_ports_addfd(net, fd);
704 err = nfsd_create_serv(net);
708 serv = nn->nfsd_serv;
709 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
711 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
712 nfsd_destroy_serv(net);
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);
727 struct svc_serv *serv;
729 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
732 if (port < 1 || port > USHRT_MAX)
734 trace_nfsd_ctl_ports_addxprt(net, transport, port);
736 err = nfsd_create_serv(net);
740 serv = nn->nfsd_serv;
741 err = svc_xprt_create(serv, transport, net,
742 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
746 err = svc_xprt_create(serv, transport, net,
747 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
748 if (err < 0 && err != -EAFNOSUPPORT)
753 xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
755 svc_xprt_close(xprt);
759 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
760 nfsd_destroy_serv(net);
765 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
769 return __write_ports_names(buf, net);
772 return __write_ports_addfd(buf, net, file->f_cred);
775 return __write_ports_addxprt(buf, net, file->f_cred);
781 * write_ports - Pass a socket file descriptor or transport name to listen on
787 * On success: passed-in buffer filled with a '\n'-terminated C
788 * string containing a whitespace-separated list of
789 * named NFSD listeners;
790 * return code is the size in bytes of the string
791 * On error: return code is zero or a negative errno value
796 * buf: C string containing an unsigned
797 * integer value representing a bound
798 * but unconnected socket that is to be
799 * used as an NFSD listener; listen(3)
800 * must be called for a SOCK_STREAM
801 * socket, otherwise it is ignored
802 * size: non-zero length of C string in @buf
804 * On success: NFS service is started;
805 * passed-in buffer filled with a '\n'-terminated C
806 * string containing a unique alphanumeric name of
808 * return code is the size in bytes of the string
809 * On error: return code is a negative errno value
814 * buf: C string containing a transport
815 * name and an unsigned integer value
816 * representing the port to listen on,
817 * separated by whitespace
818 * size: non-zero length of C string in @buf
820 * On success: returns zero; NFS service is started
821 * On error: return code is a negative errno value
823 static ssize_t write_ports(struct file *file, char *buf, size_t size)
827 mutex_lock(&nfsd_mutex);
828 rv = __write_ports(file, buf, size, netns(file));
829 mutex_unlock(&nfsd_mutex);
834 int nfsd_max_blksize;
837 * write_maxblksize - Set or report the current NFS blksize
846 * buf: C string containing an unsigned
847 * integer value representing the new
849 * size: non-zero length of C string in @buf
851 * On success: passed-in buffer filled with '\n'-terminated C string
852 * containing numeric value of the current NFS blksize
854 * return code is the size in bytes of the string
855 * On error: return code is zero or a negative errno value
857 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
860 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
864 int rv = get_int(&mesg, &bsize);
867 trace_nfsd_ctl_maxblksize(netns(file), bsize);
869 /* force bsize into allowed range and
870 * required alignment.
872 bsize = max_t(int, bsize, 1024);
873 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
875 mutex_lock(&nfsd_mutex);
877 mutex_unlock(&nfsd_mutex);
880 nfsd_max_blksize = bsize;
881 mutex_unlock(&nfsd_mutex);
884 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
889 * write_maxconn - Set or report the current max number of connections
897 * buf: C string containing an unsigned
898 * integer value representing the new
899 * number of max connections
900 * size: non-zero length of C string in @buf
902 * On success: passed-in buffer filled with '\n'-terminated C string
903 * containing numeric value of max_connections setting
904 * for this net namespace;
905 * return code is the size in bytes of the string
906 * On error: return code is zero or a negative errno value
908 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
911 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
912 unsigned int maxconn = nn->max_connections;
915 int rv = get_uint(&mesg, &maxconn);
919 trace_nfsd_ctl_maxconn(netns(file), maxconn);
920 nn->max_connections = maxconn;
923 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
926 #ifdef CONFIG_NFSD_V4
927 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
928 time64_t *time, struct nfsd_net *nn)
930 struct dentry *dentry = file_dentry(file);
937 rv = get_int(&mesg, &i);
940 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
941 dentry->d_name.len, i);
944 * Some sanity checking. We don't have a reason for
945 * these particular numbers, but problems with the
947 * - Too short: the briefest network outage may
948 * cause clients to lose all their locks. Also,
949 * the frequent polling may be wasteful.
950 * - Too long: do you really want reboot recovery
951 * to take more than an hour? Or to make other
952 * clients wait an hour before being able to
953 * revoke a dead client's locks?
955 if (i < 10 || i > 3600)
960 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
963 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
964 time64_t *time, struct nfsd_net *nn)
968 mutex_lock(&nfsd_mutex);
969 rv = __nfsd4_write_time(file, buf, size, time, nn);
970 mutex_unlock(&nfsd_mutex);
975 * write_leasetime - Set or report the current NFSv4 lease time
984 * buf: C string containing an unsigned
985 * integer value representing the new
986 * NFSv4 lease expiry time
987 * size: non-zero length of C string in @buf
989 * On success: passed-in buffer filled with '\n'-terminated C
990 * string containing unsigned integer value of the
991 * current lease expiry time;
992 * return code is the size in bytes of the string
993 * On error: return code is zero or a negative errno value
995 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
997 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
998 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1002 * write_gracetime - Set or report current NFSv4 grace period time
1004 * As above, but sets the time of the NFSv4 grace period.
1006 * Note this should never be set to less than the *previous*
1007 * lease-period time, but we don't try to enforce this. (In the common
1008 * case (a new boot), we don't know what the previous lease time was
1011 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1013 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1014 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1017 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1018 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1019 struct nfsd_net *nn)
1028 if (size > PATH_MAX || buf[size-1] != '\n')
1033 len = qword_get(&mesg, recdir, size);
1036 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1038 status = nfs4_reset_recoverydir(recdir);
1043 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1044 nfs4_recoverydir());
1048 * write_recoverydir - Set or report the pathname of the recovery directory
1057 * buf: C string containing the pathname
1058 * of the directory on a local file
1059 * system containing permanent NFSv4
1061 * size: non-zero length of C string in @buf
1063 * On success: passed-in buffer filled with '\n'-terminated C string
1064 * containing the current recovery pathname setting;
1065 * return code is the size in bytes of the string
1066 * On error: return code is zero or a negative errno value
1068 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1071 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1073 mutex_lock(&nfsd_mutex);
1074 rv = __write_recoverydir(file, buf, size, nn);
1075 mutex_unlock(&nfsd_mutex);
1081 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1090 * size: non-zero length of C string in @buf
1092 * passed-in buffer filled with "Y" or "N" with a newline
1093 * and NULL-terminated C string. This indicates whether
1094 * the grace period has ended in the current net
1095 * namespace. Return code is the size in bytes of the
1096 * string. Writing a string that starts with 'Y', 'y', or
1097 * '1' to the file will end the grace period for nfsd's v4
1100 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1102 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1111 trace_nfsd_end_grace(netns(file));
1112 nfsd4_end_grace(nn);
1119 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1120 nn->grace_ended ? 'Y' : 'N');
1125 /*----------------------------------------------------------------------------*/
1127 * populating the filesystem.
1130 /* Basically copying rpc_get_inode. */
1131 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1133 struct inode *inode = new_inode(sb);
1136 /* Following advice from simple_fill_super documentation: */
1137 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1138 inode->i_mode = mode;
1139 simple_inode_init_ts(inode);
1140 switch (mode & S_IFMT) {
1142 inode->i_fop = &simple_dir_operations;
1143 inode->i_op = &simple_dir_inode_operations;
1147 inode->i_op = &simple_symlink_inode_operations;
1155 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1157 struct inode *inode;
1159 inode = nfsd_get_inode(dir->i_sb, mode);
1163 inode->i_private = ncl;
1164 kref_get(&ncl->cl_ref);
1166 d_add(dentry, inode);
1168 fsnotify_mkdir(dir, dentry);
1172 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1174 struct inode *dir = parent->d_inode;
1175 struct dentry *dentry;
1179 dentry = d_alloc_name(parent, name);
1182 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1190 dentry = ERR_PTR(ret);
1194 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1195 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1196 umode_t mode, const char *content)
1198 struct inode *inode;
1200 inode = nfsd_get_inode(dir->i_sb, mode);
1204 inode->i_link = (char *)content;
1205 inode->i_size = strlen(content);
1207 d_add(dentry, inode);
1209 fsnotify_create(dir, dentry);
1214 * @content is assumed to be a NUL-terminated string that lives
1215 * longer than the symlink itself.
1217 static void _nfsd_symlink(struct dentry *parent, const char *name,
1218 const char *content)
1220 struct inode *dir = parent->d_inode;
1221 struct dentry *dentry;
1225 dentry = d_alloc_name(parent, name);
1228 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1235 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1236 const char *content)
1242 static void clear_ncl(struct dentry *dentry)
1244 struct inode *inode = d_inode(dentry);
1245 struct nfsdfs_client *ncl = inode->i_private;
1247 spin_lock(&inode->i_lock);
1248 inode->i_private = NULL;
1249 spin_unlock(&inode->i_lock);
1250 kref_put(&ncl->cl_ref, ncl->cl_release);
1253 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1255 struct nfsdfs_client *nc;
1257 spin_lock(&inode->i_lock);
1258 nc = inode->i_private;
1260 kref_get(&nc->cl_ref);
1261 spin_unlock(&inode->i_lock);
1265 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1267 static int nfsdfs_create_files(struct dentry *root,
1268 const struct tree_descr *files,
1269 struct nfsdfs_client *ncl,
1270 struct dentry **fdentries)
1272 struct inode *dir = d_inode(root);
1273 struct inode *inode;
1274 struct dentry *dentry;
1278 for (i = 0; files->name && files->name[0]; i++, files++) {
1279 dentry = d_alloc_name(root, files->name);
1282 inode = nfsd_get_inode(d_inode(root)->i_sb,
1283 S_IFREG | files->mode);
1288 kref_get(&ncl->cl_ref);
1289 inode->i_fop = files->ops;
1290 inode->i_private = ncl;
1291 d_add(dentry, inode);
1292 fsnotify_create(dir, dentry);
1294 fdentries[i] = dentry;
1303 /* on success, returns positive number unique to that client. */
1304 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1305 struct nfsdfs_client *ncl, u32 id,
1306 const struct tree_descr *files,
1307 struct dentry **fdentries)
1309 struct dentry *dentry;
1313 sprintf(name, "%u", id);
1315 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1316 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1318 ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1320 nfsd_client_rmdir(dentry);
1326 /* Taken from __rpc_rmdir: */
1327 void nfsd_client_rmdir(struct dentry *dentry)
1329 simple_recursive_removal(dentry, clear_ncl);
1332 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1334 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1336 struct dentry *dentry;
1339 static const struct tree_descr nfsd_files[] = {
1340 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1341 /* Per-export io stats use same ops as exports file */
1342 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1343 [NFSD_Export_features] = {"export_features",
1344 &export_features_fops, S_IRUGO},
1345 [NFSD_FO_UnlockIP] = {"unlock_ip",
1346 &transaction_ops, S_IWUSR|S_IRUSR},
1347 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1348 &transaction_ops, S_IWUSR|S_IRUSR},
1349 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1350 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1351 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1352 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1353 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1354 &nfsd_reply_cache_stats_fops, S_IRUGO},
1355 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1356 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1357 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1358 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1359 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1360 #ifdef CONFIG_NFSD_V4
1361 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1362 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1363 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1364 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1369 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1372 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1373 "/proc/net/rpc/gss_krb5_enctypes");
1374 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1376 return PTR_ERR(dentry);
1377 nn->nfsd_client_dir = dentry;
1381 static int nfsd_fs_get_tree(struct fs_context *fc)
1383 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1386 static void nfsd_fs_free_fc(struct fs_context *fc)
1389 put_net(fc->s_fs_info);
1392 static const struct fs_context_operations nfsd_fs_context_ops = {
1393 .free = nfsd_fs_free_fc,
1394 .get_tree = nfsd_fs_get_tree,
1397 static int nfsd_init_fs_context(struct fs_context *fc)
1399 put_user_ns(fc->user_ns);
1400 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1401 fc->ops = &nfsd_fs_context_ops;
1405 static void nfsd_umount(struct super_block *sb)
1407 struct net *net = sb->s_fs_info;
1409 nfsd_shutdown_threads(net);
1411 kill_litter_super(sb);
1415 static struct file_system_type nfsd_fs_type = {
1416 .owner = THIS_MODULE,
1418 .init_fs_context = nfsd_init_fs_context,
1419 .kill_sb = nfsd_umount,
1421 MODULE_ALIAS_FS("nfsd");
1423 #ifdef CONFIG_PROC_FS
1425 static int exports_proc_open(struct inode *inode, struct file *file)
1427 return exports_net_open(current->nsproxy->net_ns, file);
1430 static const struct proc_ops exports_proc_ops = {
1431 .proc_open = exports_proc_open,
1432 .proc_read = seq_read,
1433 .proc_lseek = seq_lseek,
1434 .proc_release = seq_release,
1437 static int create_proc_exports_entry(void)
1439 struct proc_dir_entry *entry;
1441 entry = proc_mkdir("fs/nfs", NULL);
1444 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1446 remove_proc_entry("fs/nfs", NULL);
1451 #else /* CONFIG_PROC_FS */
1452 static int create_proc_exports_entry(void)
1458 unsigned int nfsd_net_id;
1461 * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit
1462 * @cb: netlink metadata and command arguments
1465 * %0: The rpc_status_get command may proceed
1466 * %-ENODEV: There is no NFSD running in this namespace
1468 int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb)
1470 struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id);
1473 mutex_lock(&nfsd_mutex);
1477 mutex_unlock(&nfsd_mutex);
1482 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1483 struct netlink_callback *cb,
1484 struct nfsd_genl_rqstp *rqstp)
1489 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1490 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1494 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1495 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1496 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1497 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1498 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1499 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1500 ktime_to_us(rqstp->rq_stime),
1501 NFSD_A_RPC_STATUS_PAD))
1504 switch (rqstp->rq_saddr.sa_family) {
1506 const struct sockaddr_in *s_in, *d_in;
1508 s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1509 d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1510 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1511 s_in->sin_addr.s_addr) ||
1512 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1513 d_in->sin_addr.s_addr) ||
1514 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1516 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1522 const struct sockaddr_in6 *s_in, *d_in;
1524 s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1525 d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1526 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1527 &s_in->sin6_addr) ||
1528 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1529 &d_in->sin6_addr) ||
1530 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1532 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1539 for (i = 0; i < rqstp->rq_opcnt; i++)
1540 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1541 rqstp->rq_opnum[i]))
1544 genlmsg_end(skb, hdr);
1549 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1550 * @skb: reply buffer
1551 * @cb: netlink metadata and command arguments
1553 * Returns the size of the reply or a negative errno.
1555 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1556 struct netlink_callback *cb)
1558 struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1559 int i, ret, rqstp_index = 0;
1563 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1564 struct svc_rqst *rqstp;
1566 if (i < cb->args[0]) /* already consumed */
1570 list_for_each_entry_rcu(rqstp,
1571 &nn->nfsd_serv->sv_pools[i].sp_all_threads,
1573 struct nfsd_genl_rqstp genl_rqstp;
1574 unsigned int status_counter;
1576 if (rqstp_index++ < cb->args[1]) /* already consumed */
1579 * Acquire rq_status_counter before parsing the rqst
1580 * fields. rq_status_counter is set to an odd value in
1581 * order to notify the consumers the rqstp fields are
1585 smp_load_acquire(&rqstp->rq_status_counter);
1586 if (!(status_counter & 1))
1589 genl_rqstp.rq_xid = rqstp->rq_xid;
1590 genl_rqstp.rq_flags = rqstp->rq_flags;
1591 genl_rqstp.rq_vers = rqstp->rq_vers;
1592 genl_rqstp.rq_prog = rqstp->rq_prog;
1593 genl_rqstp.rq_proc = rqstp->rq_proc;
1594 genl_rqstp.rq_stime = rqstp->rq_stime;
1595 genl_rqstp.rq_opcnt = 0;
1596 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1597 sizeof(struct sockaddr));
1598 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1599 sizeof(struct sockaddr));
1601 #ifdef CONFIG_NFSD_V4
1602 if (rqstp->rq_vers == NFS4_VERSION &&
1603 rqstp->rq_proc == NFSPROC4_COMPOUND) {
1604 /* NFSv4 compound */
1605 struct nfsd4_compoundargs *args;
1608 args = rqstp->rq_argp;
1609 genl_rqstp.rq_opcnt = args->opcnt;
1610 for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1611 genl_rqstp.rq_opnum[j] =
1614 #endif /* CONFIG_NFSD_V4 */
1617 * Acquire rq_status_counter before reporting the rqst
1618 * fields to the user.
1620 if (smp_load_acquire(&rqstp->rq_status_counter) !=
1624 ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1632 cb->args[1] = rqstp_index;
1641 * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing
1642 * @cb: netlink metadata and command arguments
1647 int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb)
1649 mutex_unlock(&nfsd_mutex);
1655 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
1656 * @net: a freshly-created network namespace
1658 * This information stays around as long as the network namespace is
1659 * alive whether or not there is an NFSD instance running in the
1662 * Returns zero on success, or a negative errno otherwise.
1664 static __net_init int nfsd_net_init(struct net *net)
1667 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1669 retval = nfsd_export_init(net);
1671 goto out_export_error;
1672 retval = nfsd_idmap_init(net);
1674 goto out_idmap_error;
1675 retval = nfsd_stat_counters_init(nn);
1677 goto out_repcache_error;
1678 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
1679 nn->nfsd_svcstats.program = &nfsd_program;
1680 nn->nfsd_versions = NULL;
1681 nn->nfsd4_minorversions = NULL;
1682 nfsd4_init_leases_net(nn);
1683 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1684 seqlock_init(&nn->writeverf_lock);
1685 nfsd_proc_stat_init(net);
1690 nfsd_idmap_shutdown(net);
1692 nfsd_export_shutdown(net);
1698 * nfsd_net_exit - Release the nfsd_net portion of a net namespace
1699 * @net: a network namespace that is about to be destroyed
1702 static __net_exit void nfsd_net_exit(struct net *net)
1704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1706 nfsd_proc_stat_shutdown(net);
1707 nfsd_stat_counters_destroy(nn);
1708 nfsd_idmap_shutdown(net);
1709 nfsd_export_shutdown(net);
1710 nfsd_netns_free_versions(nn);
1713 static struct pernet_operations nfsd_net_ops = {
1714 .init = nfsd_net_init,
1715 .exit = nfsd_net_exit,
1717 .size = sizeof(struct nfsd_net),
1720 static int __init init_nfsd(void)
1724 retval = nfsd4_init_slabs();
1727 retval = nfsd4_init_pnfs();
1729 goto out_free_slabs;
1730 retval = nfsd_drc_slab_create();
1733 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1734 retval = create_proc_exports_entry();
1736 goto out_free_lockd;
1737 retval = register_pernet_subsys(&nfsd_net_ops);
1739 goto out_free_exports;
1740 retval = register_cld_notifier();
1742 goto out_free_subsys;
1743 retval = nfsd4_create_laundry_wq();
1746 retval = register_filesystem(&nfsd_fs_type);
1749 retval = genl_register_family(&nfsd_nl_family);
1755 nfsd4_destroy_laundry_wq();
1757 unregister_cld_notifier();
1759 unregister_pernet_subsys(&nfsd_net_ops);
1761 remove_proc_entry("fs/nfs/exports", NULL);
1762 remove_proc_entry("fs/nfs", NULL);
1764 nfsd_lockd_shutdown();
1765 nfsd_drc_slab_free();
1773 static void __exit exit_nfsd(void)
1775 genl_unregister_family(&nfsd_nl_family);
1776 unregister_filesystem(&nfsd_fs_type);
1777 nfsd4_destroy_laundry_wq();
1778 unregister_cld_notifier();
1779 unregister_pernet_subsys(&nfsd_net_ops);
1780 nfsd_drc_slab_free();
1781 remove_proc_entry("fs/nfs/exports", NULL);
1782 remove_proc_entry("fs/nfs", NULL);
1783 nfsd_lockd_shutdown();
1788 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1789 MODULE_DESCRIPTION("In-kernel NFS server");
1790 MODULE_LICENSE("GPL");
1791 module_init(init_nfsd)
1792 module_exit(exit_nfsd)