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"
30 * We have a single directory with several nodes in it.
43 NFSD_Reply_Cache_Stats,
50 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
51 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
63 * write() for these nodes.
65 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
66 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
67 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
68 static ssize_t write_threads(struct file *file, char *buf, size_t size);
69 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
70 static ssize_t write_versions(struct file *file, char *buf, size_t size);
71 static ssize_t write_ports(struct file *file, char *buf, size_t size);
72 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
73 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
75 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
76 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
77 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 [NFSD_RecoveryDir] = write_recoverydir,
95 [NFSD_V4EndGrace] = write_v4_end_grace,
99 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
101 ino_t ino = file_inode(file)->i_ino;
105 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
108 data = simple_transaction_get(file, buf, size);
110 return PTR_ERR(data);
112 rv = write_op[ino](file, data, size);
114 simple_transaction_set(file, rv);
120 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
122 if (! file->private_data) {
123 /* An attempt to read a transaction file without writing
124 * causes a 0-byte write so that the file can return
127 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
131 return simple_transaction_read(file, buf, size, pos);
134 static const struct file_operations transaction_ops = {
135 .write = nfsctl_transaction_write,
136 .read = nfsctl_transaction_read,
137 .release = simple_transaction_release,
138 .llseek = default_llseek,
141 static int exports_net_open(struct net *net, struct file *file)
144 struct seq_file *seq;
145 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
147 err = seq_open(file, &nfs_exports_op);
151 seq = file->private_data;
152 seq->private = nn->svc_export_cache;
156 static int exports_nfsd_open(struct inode *inode, struct file *file)
158 return exports_net_open(inode->i_sb->s_fs_info, file);
161 static const struct file_operations exports_nfsd_operations = {
162 .open = exports_nfsd_open,
165 .release = seq_release,
168 static int export_features_show(struct seq_file *m, void *v)
170 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
174 DEFINE_SHOW_ATTRIBUTE(export_features);
176 static const struct file_operations pool_stats_operations = {
177 .open = nfsd_pool_stats_open,
180 .release = nfsd_pool_stats_release,
183 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
185 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
187 /*----------------------------------------------------------------------------*/
189 * payload - write methods
192 static inline struct net *netns(struct file *file)
194 return file_inode(file)->i_sb->s_fs_info;
198 * write_unlock_ip - Release all locks used by a client
203 * buf: '\n'-terminated C string containing a
204 * presentation format IP address
205 * size: length of C string in @buf
207 * On success: returns zero if all specified locks were released;
208 * returns one if one or more locks were not released
209 * On error: return code is negative errno value
211 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
213 struct sockaddr_storage address;
214 struct sockaddr *sap = (struct sockaddr *)&address;
215 size_t salen = sizeof(address);
217 struct net *net = netns(file);
223 if (buf[size-1] != '\n')
227 if (qword_get(&buf, fo_path, size) < 0)
230 if (rpc_pton(net, fo_path, size, sap, salen) == 0)
233 return nlmsvc_unlock_all_by_ip(sap);
237 * write_unlock_fs - Release all locks on a local file system
242 * buf: '\n'-terminated C string containing the
243 * absolute pathname of a local file system
244 * size: length of C string in @buf
246 * On success: returns zero if all specified locks were released;
247 * returns one if one or more locks were not released
248 * On error: return code is negative errno value
250 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
260 if (buf[size-1] != '\n')
264 if (qword_get(&buf, fo_path, size) < 0)
267 error = kern_path(fo_path, 0, &path);
272 * XXX: Needs better sanity checking. Otherwise we could end up
273 * releasing locks on the wrong file system.
276 * 1. Does the path refer to a directory?
277 * 2. Is that directory a mount point, or
278 * 3. Is that directory the root of an exported file system?
280 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
287 * write_filehandle - Get a variable-length NFS file handle by path
289 * On input, the buffer contains a '\n'-terminated C string comprised of
290 * three alphanumeric words separated by whitespace. The string may
291 * contain escape sequences.
295 * domain: client domain name
296 * path: export pathname
297 * maxsize: numeric maximum size of
299 * size: length of C string in @buf
301 * On success: passed-in buffer filled with '\n'-terminated C
302 * string containing a ASCII hex text version
303 * of the NFS file handle;
304 * return code is the size in bytes of the string
305 * On error: return code is negative errno value
307 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
313 struct auth_domain *dom;
319 if (buf[size-1] != '\n')
324 len = qword_get(&mesg, dname, size);
329 len = qword_get(&mesg, path, size);
333 len = get_int(&mesg, &maxsize);
337 if (maxsize < NFS_FHSIZE)
339 maxsize = min(maxsize, NFS3_FHSIZE);
341 if (qword_get(&mesg, mesg, size)>0)
344 /* we have all the words, they are in buf.. */
345 dom = unix_domain_find(dname);
349 len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
350 auth_domain_put(dom);
355 len = SIMPLE_TRANSACTION_LIMIT;
356 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
362 * write_threads - Start NFSD, or report the current number of running threads
368 * On success: passed-in buffer filled with '\n'-terminated C
369 * string numeric value representing the number of
370 * running NFSD threads;
371 * return code is the size in bytes of the string
372 * On error: return code is zero
377 * buf: C string containing an unsigned
378 * integer value representing the
379 * number of NFSD threads to start
380 * size: non-zero length of C string in @buf
382 * On success: NFS service is started;
383 * passed-in buffer filled with '\n'-terminated C
384 * string numeric value representing the number of
385 * running NFSD threads;
386 * return code is the size in bytes of the string
387 * On error: return code is zero or a negative errno value
389 static ssize_t write_threads(struct file *file, char *buf, size_t size)
393 struct net *net = netns(file);
397 rv = get_int(&mesg, &newthreads);
402 rv = nfsd_svc(newthreads, net, file->f_cred);
406 rv = nfsd_nrthreads(net);
408 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
412 * write_pool_threads - Set or report the current number of threads per pool
421 * buf: C string containing whitespace-
422 * separated unsigned integer values
423 * representing the number of NFSD
424 * threads to start in each pool
425 * size: non-zero length of C string in @buf
427 * On success: passed-in buffer filled with '\n'-terminated C
428 * string containing integer values representing the
429 * number of NFSD threads in each pool;
430 * return code is the size in bytes of the string
431 * On error: return code is zero or a negative errno value
433 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
435 /* if size > 0, look for an array of number of threads per node
436 * and apply them then write out number of threads per node as reply
444 struct net *net = netns(file);
446 mutex_lock(&nfsd_mutex);
447 npools = nfsd_nrpools(net);
450 * NFS is shut down. The admin can start it by
451 * writing to the threads file but NOT the pool_threads
452 * file, sorry. Report zero threads.
454 mutex_unlock(&nfsd_mutex);
459 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
461 if (nthreads == NULL)
465 for (i = 0; i < npools; i++) {
466 rv = get_int(&mesg, &nthreads[i]);
468 break; /* fewer numbers than pools */
470 goto out_free; /* syntax error */
475 rv = nfsd_set_nrthreads(i, nthreads, net);
480 rv = nfsd_get_nrthreads(npools, nthreads, net);
485 size = SIMPLE_TRANSACTION_LIMIT;
486 for (i = 0; i < npools && size > 0; i++) {
487 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
495 mutex_unlock(&nfsd_mutex);
500 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
501 const char *sep, unsigned vers, int minor)
503 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
504 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
506 if (vers == 4 && minor >= 0 &&
507 !nfsd_minorversion(nn, minor, NFSD_TEST))
509 if (minor == 0 && supported)
511 * special case for backward compatability.
512 * +4.0 is never reported, it is implied by
513 * +4, unless -4.0 is present.
516 return snprintf(buf, remaining, format, sep,
517 supported ? '+' : '-', vers, minor);
520 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
523 char *vers, *minorp, sign;
524 int len, num, remaining;
527 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
531 /* Cannot change versions without updating
532 * nn->nfsd_serv->sv_xdrsize, and reallocing
533 * rq_argp and rq_resp
536 if (buf[size-1] != '\n')
541 len = qword_get(&mesg, vers, size);
542 if (len <= 0) return -EINVAL;
547 if (sign == '+' || sign == '-')
548 num = simple_strtol((vers+1), &minorp, 0);
550 num = simple_strtol(vers, &minorp, 0);
551 if (*minorp == '.') {
554 if (kstrtouint(minorp+1, 0, &minor) < 0)
558 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
560 #ifdef CONFIG_NFSD_V2
564 nfsd_vers(nn, num, cmd);
567 if (*minorp == '.') {
568 if (nfsd_minorversion(nn, minor, cmd) < 0)
570 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
572 * Either we have +4 and no minors are enabled,
573 * or we have -4 and at least one minor is enabled.
574 * In either case, propagate 'cmd' to all minors.
577 while (nfsd_minorversion(nn, minor, cmd) >= 0)
582 /* Ignore requests to disable non-existent versions */
587 } while ((len = qword_get(&mesg, vers, size)) > 0);
588 /* If all get turned off, turn them back on, as
589 * having no versions is BAD
591 nfsd_reset_versions(nn);
594 /* Now write current state into reply buffer */
596 remaining = SIMPLE_TRANSACTION_LIMIT;
597 for (num=2 ; num <= 4 ; num++) {
599 if (!nfsd_vers(nn, num, NFSD_AVAIL))
604 len = nfsd_print_version_support(nn, buf, remaining,
606 if (len >= remaining)
614 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
617 len = snprintf(buf, remaining, "\n");
618 if (len >= remaining)
624 * write_versions - Set or report the available NFS protocol versions
630 * On success: passed-in buffer filled with '\n'-terminated C
631 * string containing positive or negative integer
632 * values representing the current status of each
634 * return code is the size in bytes of the string
635 * On error: return code is zero or a negative errno value
640 * buf: C string containing whitespace-
641 * separated positive or negative
642 * integer values representing NFS
643 * protocol versions to enable ("+n")
645 * size: non-zero length of C string in @buf
647 * On success: status of zero or more protocol versions has
648 * been updated; passed-in buffer filled with
649 * '\n'-terminated C string containing positive
650 * or negative integer values representing the
651 * current status of each protocol version;
652 * return code is the size in bytes of the string
653 * On error: return code is zero or a negative errno value
655 static ssize_t write_versions(struct file *file, char *buf, size_t size)
659 mutex_lock(&nfsd_mutex);
660 rv = __write_versions(file, buf, size);
661 mutex_unlock(&nfsd_mutex);
666 * Zero-length write. Return a list of NFSD's current listener
669 static ssize_t __write_ports_names(char *buf, struct net *net)
671 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
673 if (nn->nfsd_serv == NULL)
675 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
679 * A single 'fd' number was written, in which case it must be for
680 * a socket of a supported family/protocol, and we use it as an
683 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
687 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
689 err = get_int(&mesg, &fd);
690 if (err != 0 || fd < 0)
693 err = nfsd_create_serv(net);
697 err = svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
700 !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
701 svc_get(nn->nfsd_serv);
708 * A transport listener is added by writing it's transport name and
711 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
714 struct svc_xprt *xprt;
716 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
718 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
721 if (port < 1 || port > USHRT_MAX)
724 err = nfsd_create_serv(net);
728 err = svc_xprt_create(nn->nfsd_serv, transport, net,
729 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
733 err = svc_xprt_create(nn->nfsd_serv, transport, net,
734 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
735 if (err < 0 && err != -EAFNOSUPPORT)
738 if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
739 svc_get(nn->nfsd_serv);
744 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
746 svc_xprt_close(xprt);
754 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
758 return __write_ports_names(buf, net);
761 return __write_ports_addfd(buf, net, file->f_cred);
764 return __write_ports_addxprt(buf, net, file->f_cred);
770 * write_ports - Pass a socket file descriptor or transport name to listen on
776 * On success: passed-in buffer filled with a '\n'-terminated C
777 * string containing a whitespace-separated list of
778 * named NFSD listeners;
779 * return code is the size in bytes of the string
780 * On error: return code is zero or a negative errno value
785 * buf: C string containing an unsigned
786 * integer value representing a bound
787 * but unconnected socket that is to be
788 * used as an NFSD listener; listen(3)
789 * must be called for a SOCK_STREAM
790 * socket, otherwise it is ignored
791 * size: non-zero length of C string in @buf
793 * On success: NFS service is started;
794 * passed-in buffer filled with a '\n'-terminated C
795 * string containing a unique alphanumeric name of
797 * return code is the size in bytes of the string
798 * On error: return code is a negative errno value
803 * buf: C string containing a transport
804 * name and an unsigned integer value
805 * representing the port to listen on,
806 * separated by whitespace
807 * size: non-zero length of C string in @buf
809 * On success: returns zero; NFS service is started
810 * On error: return code is a negative errno value
812 static ssize_t write_ports(struct file *file, char *buf, size_t size)
816 mutex_lock(&nfsd_mutex);
817 rv = __write_ports(file, buf, size, netns(file));
818 mutex_unlock(&nfsd_mutex);
823 int nfsd_max_blksize;
826 * write_maxblksize - Set or report the current NFS blksize
835 * buf: C string containing an unsigned
836 * integer value representing the new
838 * size: non-zero length of C string in @buf
840 * On success: passed-in buffer filled with '\n'-terminated C string
841 * containing numeric value of the current NFS blksize
843 * return code is the size in bytes of the string
844 * On error: return code is zero or a negative errno value
846 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
849 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
853 int rv = get_int(&mesg, &bsize);
856 /* force bsize into allowed range and
857 * required alignment.
859 bsize = max_t(int, bsize, 1024);
860 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
862 mutex_lock(&nfsd_mutex);
864 mutex_unlock(&nfsd_mutex);
867 nfsd_max_blksize = bsize;
868 mutex_unlock(&nfsd_mutex);
871 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
876 * write_maxconn - Set or report the current max number of connections
884 * buf: C string containing an unsigned
885 * integer value representing the new
886 * number of max connections
887 * size: non-zero length of C string in @buf
889 * On success: passed-in buffer filled with '\n'-terminated C string
890 * containing numeric value of max_connections setting
891 * for this net namespace;
892 * return code is the size in bytes of the string
893 * On error: return code is zero or a negative errno value
895 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
898 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
899 unsigned int maxconn = nn->max_connections;
902 int rv = get_uint(&mesg, &maxconn);
906 nn->max_connections = maxconn;
909 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
912 #ifdef CONFIG_NFSD_V4
913 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
914 time64_t *time, struct nfsd_net *nn)
922 rv = get_int(&mesg, &i);
926 * Some sanity checking. We don't have a reason for
927 * these particular numbers, but problems with the
929 * - Too short: the briefest network outage may
930 * cause clients to lose all their locks. Also,
931 * the frequent polling may be wasteful.
932 * - Too long: do you really want reboot recovery
933 * to take more than an hour? Or to make other
934 * clients wait an hour before being able to
935 * revoke a dead client's locks?
937 if (i < 10 || i > 3600)
942 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
945 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
946 time64_t *time, struct nfsd_net *nn)
950 mutex_lock(&nfsd_mutex);
951 rv = __nfsd4_write_time(file, buf, size, time, nn);
952 mutex_unlock(&nfsd_mutex);
957 * write_leasetime - Set or report the current NFSv4 lease time
966 * buf: C string containing an unsigned
967 * integer value representing the new
968 * NFSv4 lease expiry time
969 * size: non-zero length of C string in @buf
971 * On success: passed-in buffer filled with '\n'-terminated C
972 * string containing unsigned integer value of the
973 * current lease expiry time;
974 * return code is the size in bytes of the string
975 * On error: return code is zero or a negative errno value
977 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
979 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
980 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
984 * write_gracetime - Set or report current NFSv4 grace period time
986 * As above, but sets the time of the NFSv4 grace period.
988 * Note this should never be set to less than the *previous*
989 * lease-period time, but we don't try to enforce this. (In the common
990 * case (a new boot), we don't know what the previous lease time was
993 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
995 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
996 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
999 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1000 struct nfsd_net *nn)
1009 if (size > PATH_MAX || buf[size-1] != '\n')
1014 len = qword_get(&mesg, recdir, size);
1018 status = nfs4_reset_recoverydir(recdir);
1023 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1024 nfs4_recoverydir());
1028 * write_recoverydir - Set or report the pathname of the recovery directory
1037 * buf: C string containing the pathname
1038 * of the directory on a local file
1039 * system containing permanent NFSv4
1041 * size: non-zero length of C string in @buf
1043 * On success: passed-in buffer filled with '\n'-terminated C string
1044 * containing the current recovery pathname setting;
1045 * return code is the size in bytes of the string
1046 * On error: return code is zero or a negative errno value
1048 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1051 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1053 mutex_lock(&nfsd_mutex);
1054 rv = __write_recoverydir(file, buf, size, nn);
1055 mutex_unlock(&nfsd_mutex);
1060 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1069 * size: non-zero length of C string in @buf
1071 * passed-in buffer filled with "Y" or "N" with a newline
1072 * and NULL-terminated C string. This indicates whether
1073 * the grace period has ended in the current net
1074 * namespace. Return code is the size in bytes of the
1075 * string. Writing a string that starts with 'Y', 'y', or
1076 * '1' to the file will end the grace period for nfsd's v4
1079 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1081 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1090 nfsd4_end_grace(nn);
1097 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1098 nn->grace_ended ? 'Y' : 'N');
1103 /*----------------------------------------------------------------------------*/
1105 * populating the filesystem.
1108 /* Basically copying rpc_get_inode. */
1109 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1111 struct inode *inode = new_inode(sb);
1114 /* Following advice from simple_fill_super documentation: */
1115 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1116 inode->i_mode = mode;
1117 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1118 switch (mode & S_IFMT) {
1120 inode->i_fop = &simple_dir_operations;
1121 inode->i_op = &simple_dir_inode_operations;
1125 inode->i_op = &simple_symlink_inode_operations;
1133 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1135 struct inode *inode;
1137 inode = nfsd_get_inode(dir->i_sb, mode);
1141 inode->i_private = ncl;
1142 kref_get(&ncl->cl_ref);
1144 d_add(dentry, inode);
1146 fsnotify_mkdir(dir, dentry);
1150 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1152 struct inode *dir = parent->d_inode;
1153 struct dentry *dentry;
1157 dentry = d_alloc_name(parent, name);
1160 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1168 dentry = ERR_PTR(ret);
1172 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1173 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1174 umode_t mode, const char *content)
1176 struct inode *inode;
1178 inode = nfsd_get_inode(dir->i_sb, mode);
1182 inode->i_link = (char *)content;
1183 inode->i_size = strlen(content);
1185 d_add(dentry, inode);
1187 fsnotify_create(dir, dentry);
1192 * @content is assumed to be a NUL-terminated string that lives
1193 * longer than the symlink itself.
1195 static void nfsd_symlink(struct dentry *parent, const char *name,
1196 const char *content)
1198 struct inode *dir = parent->d_inode;
1199 struct dentry *dentry;
1203 dentry = d_alloc_name(parent, name);
1206 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1213 static inline void nfsd_symlink(struct dentry *parent, const char *name,
1214 const char *content)
1220 static void clear_ncl(struct inode *inode)
1222 struct nfsdfs_client *ncl = inode->i_private;
1224 inode->i_private = NULL;
1225 kref_put(&ncl->cl_ref, ncl->cl_release);
1228 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1230 struct nfsdfs_client *nc = inode->i_private;
1233 kref_get(&nc->cl_ref);
1237 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1239 struct nfsdfs_client *nc;
1241 inode_lock_shared(inode);
1242 nc = __get_nfsdfs_client(inode);
1243 inode_unlock_shared(inode);
1246 /* from __rpc_unlink */
1247 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1251 clear_ncl(d_inode(dentry));
1253 ret = simple_unlink(dir, dentry);
1255 fsnotify_unlink(dir, dentry);
1260 static void nfsdfs_remove_files(struct dentry *root)
1262 struct dentry *dentry, *tmp;
1264 list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1265 if (!simple_positive(dentry)) {
1266 WARN_ON_ONCE(1); /* I think this can't happen? */
1269 nfsdfs_remove_file(d_inode(root), dentry);
1273 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1275 static int nfsdfs_create_files(struct dentry *root,
1276 const struct tree_descr *files,
1277 struct dentry **fdentries)
1279 struct inode *dir = d_inode(root);
1280 struct inode *inode;
1281 struct dentry *dentry;
1285 for (i = 0; files->name && files->name[0]; i++, files++) {
1286 dentry = d_alloc_name(root, files->name);
1289 inode = nfsd_get_inode(d_inode(root)->i_sb,
1290 S_IFREG | files->mode);
1295 inode->i_fop = files->ops;
1296 inode->i_private = __get_nfsdfs_client(dir);
1297 d_add(dentry, inode);
1298 fsnotify_create(dir, dentry);
1300 fdentries[i] = dentry;
1305 nfsdfs_remove_files(root);
1310 /* on success, returns positive number unique to that client. */
1311 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1312 struct nfsdfs_client *ncl, u32 id,
1313 const struct tree_descr *files,
1314 struct dentry **fdentries)
1316 struct dentry *dentry;
1320 sprintf(name, "%u", id);
1322 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1323 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1325 ret = nfsdfs_create_files(dentry, files, fdentries);
1327 nfsd_client_rmdir(dentry);
1333 /* Taken from __rpc_rmdir: */
1334 void nfsd_client_rmdir(struct dentry *dentry)
1336 struct inode *dir = d_inode(dentry->d_parent);
1337 struct inode *inode = d_inode(dentry);
1341 nfsdfs_remove_files(dentry);
1344 ret = simple_rmdir(dir, dentry);
1347 fsnotify_rmdir(dir, dentry);
1352 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1354 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1356 struct dentry *dentry;
1359 static const struct tree_descr nfsd_files[] = {
1360 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1361 /* Per-export io stats use same ops as exports file */
1362 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1363 [NFSD_Export_features] = {"export_features",
1364 &export_features_fops, S_IRUGO},
1365 [NFSD_FO_UnlockIP] = {"unlock_ip",
1366 &transaction_ops, S_IWUSR|S_IRUSR},
1367 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1368 &transaction_ops, S_IWUSR|S_IRUSR},
1369 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1370 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1371 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1372 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1373 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1374 &nfsd_reply_cache_stats_fops, S_IRUGO},
1375 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1376 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1377 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1378 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1379 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1380 #ifdef CONFIG_NFSD_V4
1381 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1382 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1383 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1384 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1389 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1392 nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1393 "/proc/net/rpc/gss_krb5_enctypes");
1394 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1396 return PTR_ERR(dentry);
1397 nn->nfsd_client_dir = dentry;
1401 static int nfsd_fs_get_tree(struct fs_context *fc)
1403 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1406 static void nfsd_fs_free_fc(struct fs_context *fc)
1409 put_net(fc->s_fs_info);
1412 static const struct fs_context_operations nfsd_fs_context_ops = {
1413 .free = nfsd_fs_free_fc,
1414 .get_tree = nfsd_fs_get_tree,
1417 static int nfsd_init_fs_context(struct fs_context *fc)
1419 put_user_ns(fc->user_ns);
1420 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1421 fc->ops = &nfsd_fs_context_ops;
1425 static void nfsd_umount(struct super_block *sb)
1427 struct net *net = sb->s_fs_info;
1429 nfsd_shutdown_threads(net);
1431 kill_litter_super(sb);
1435 static struct file_system_type nfsd_fs_type = {
1436 .owner = THIS_MODULE,
1438 .init_fs_context = nfsd_init_fs_context,
1439 .kill_sb = nfsd_umount,
1441 MODULE_ALIAS_FS("nfsd");
1443 #ifdef CONFIG_PROC_FS
1445 static int exports_proc_open(struct inode *inode, struct file *file)
1447 return exports_net_open(current->nsproxy->net_ns, file);
1450 static const struct proc_ops exports_proc_ops = {
1451 .proc_open = exports_proc_open,
1452 .proc_read = seq_read,
1453 .proc_lseek = seq_lseek,
1454 .proc_release = seq_release,
1457 static int create_proc_exports_entry(void)
1459 struct proc_dir_entry *entry;
1461 entry = proc_mkdir("fs/nfs", NULL);
1464 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1466 remove_proc_entry("fs/nfs", NULL);
1471 #else /* CONFIG_PROC_FS */
1472 static int create_proc_exports_entry(void)
1478 unsigned int nfsd_net_id;
1480 static __net_init int nfsd_init_net(struct net *net)
1483 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1485 retval = nfsd_export_init(net);
1487 goto out_export_error;
1488 retval = nfsd_idmap_init(net);
1490 goto out_idmap_error;
1491 nn->nfsd_versions = NULL;
1492 nn->nfsd4_minorversions = NULL;
1493 nfsd4_init_leases_net(nn);
1494 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1495 seqlock_init(&nn->writeverf_lock);
1500 nfsd_export_shutdown(net);
1505 static __net_exit void nfsd_exit_net(struct net *net)
1507 nfsd_idmap_shutdown(net);
1508 nfsd_export_shutdown(net);
1509 nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1512 static struct pernet_operations nfsd_net_ops = {
1513 .init = nfsd_init_net,
1514 .exit = nfsd_exit_net,
1516 .size = sizeof(struct nfsd_net),
1519 static int __init init_nfsd(void)
1523 retval = nfsd4_init_slabs();
1526 retval = nfsd4_init_pnfs();
1528 goto out_free_slabs;
1529 retval = nfsd_stat_init(); /* Statistics */
1532 retval = nfsd_drc_slab_create();
1535 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1536 retval = create_proc_exports_entry();
1538 goto out_free_lockd;
1539 retval = register_pernet_subsys(&nfsd_net_ops);
1541 goto out_free_exports;
1542 retval = register_cld_notifier();
1544 goto out_free_subsys;
1545 retval = nfsd4_create_laundry_wq();
1548 retval = register_filesystem(&nfsd_fs_type);
1553 nfsd4_destroy_laundry_wq();
1555 unregister_cld_notifier();
1557 unregister_pernet_subsys(&nfsd_net_ops);
1559 remove_proc_entry("fs/nfs/exports", NULL);
1560 remove_proc_entry("fs/nfs", NULL);
1562 nfsd_lockd_shutdown();
1563 nfsd_drc_slab_free();
1565 nfsd_stat_shutdown();
1573 static void __exit exit_nfsd(void)
1575 unregister_filesystem(&nfsd_fs_type);
1576 nfsd4_destroy_laundry_wq();
1577 unregister_cld_notifier();
1578 unregister_pernet_subsys(&nfsd_net_ops);
1579 nfsd_drc_slab_free();
1580 remove_proc_entry("fs/nfs/exports", NULL);
1581 remove_proc_entry("fs/nfs", NULL);
1582 nfsd_stat_shutdown();
1583 nfsd_lockd_shutdown();
1588 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1589 MODULE_LICENSE("GPL");
1590 module_init(init_nfsd)
1591 module_exit(exit_nfsd)