1 // SPDX-License-Identifier: GPL-2.0-only
3 * Syscall interface to knfsd.
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/fs_context.h>
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/rpc_pipe_fs.h>
18 #include <linux/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 if (svc_alien_sock(net, fd)) {
694 printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
698 err = nfsd_create_serv(net);
702 err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
705 !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
706 svc_get(nn->nfsd_serv);
713 * A transport listener is added by writing it's transport name and
716 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
719 struct svc_xprt *xprt;
721 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
723 if (sscanf(buf, "%15s %5u", transport, &port) != 2)
726 if (port < 1 || port > USHRT_MAX)
729 err = nfsd_create_serv(net);
733 err = svc_xprt_create(nn->nfsd_serv, transport, net,
734 PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
738 err = svc_xprt_create(nn->nfsd_serv, transport, net,
739 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
740 if (err < 0 && err != -EAFNOSUPPORT)
743 if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
744 svc_get(nn->nfsd_serv);
749 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
751 svc_xprt_close(xprt);
759 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
763 return __write_ports_names(buf, net);
766 return __write_ports_addfd(buf, net, file->f_cred);
769 return __write_ports_addxprt(buf, net, file->f_cred);
775 * write_ports - Pass a socket file descriptor or transport name to listen on
781 * On success: passed-in buffer filled with a '\n'-terminated C
782 * string containing a whitespace-separated list of
783 * named NFSD listeners;
784 * return code is the size in bytes of the string
785 * On error: return code is zero or a negative errno value
790 * buf: C string containing an unsigned
791 * integer value representing a bound
792 * but unconnected socket that is to be
793 * used as an NFSD listener; listen(3)
794 * must be called for a SOCK_STREAM
795 * socket, otherwise it is ignored
796 * size: non-zero length of C string in @buf
798 * On success: NFS service is started;
799 * passed-in buffer filled with a '\n'-terminated C
800 * string containing a unique alphanumeric name of
802 * return code is the size in bytes of the string
803 * On error: return code is a negative errno value
808 * buf: C string containing a transport
809 * name and an unsigned integer value
810 * representing the port to listen on,
811 * separated by whitespace
812 * size: non-zero length of C string in @buf
814 * On success: returns zero; NFS service is started
815 * On error: return code is a negative errno value
817 static ssize_t write_ports(struct file *file, char *buf, size_t size)
821 mutex_lock(&nfsd_mutex);
822 rv = __write_ports(file, buf, size, netns(file));
823 mutex_unlock(&nfsd_mutex);
828 int nfsd_max_blksize;
831 * write_maxblksize - Set or report the current NFS blksize
840 * buf: C string containing an unsigned
841 * integer value representing the new
843 * size: non-zero length of C string in @buf
845 * On success: passed-in buffer filled with '\n'-terminated C string
846 * containing numeric value of the current NFS blksize
848 * return code is the size in bytes of the string
849 * On error: return code is zero or a negative errno value
851 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
854 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
858 int rv = get_int(&mesg, &bsize);
861 /* force bsize into allowed range and
862 * required alignment.
864 bsize = max_t(int, bsize, 1024);
865 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
867 mutex_lock(&nfsd_mutex);
869 mutex_unlock(&nfsd_mutex);
872 nfsd_max_blksize = bsize;
873 mutex_unlock(&nfsd_mutex);
876 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
881 * write_maxconn - Set or report the current max number of connections
889 * buf: C string containing an unsigned
890 * integer value representing the new
891 * number of max connections
892 * size: non-zero length of C string in @buf
894 * On success: passed-in buffer filled with '\n'-terminated C string
895 * containing numeric value of max_connections setting
896 * for this net namespace;
897 * return code is the size in bytes of the string
898 * On error: return code is zero or a negative errno value
900 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
903 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
904 unsigned int maxconn = nn->max_connections;
907 int rv = get_uint(&mesg, &maxconn);
911 nn->max_connections = maxconn;
914 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
917 #ifdef CONFIG_NFSD_V4
918 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
919 time64_t *time, struct nfsd_net *nn)
927 rv = get_int(&mesg, &i);
931 * Some sanity checking. We don't have a reason for
932 * these particular numbers, but problems with the
934 * - Too short: the briefest network outage may
935 * cause clients to lose all their locks. Also,
936 * the frequent polling may be wasteful.
937 * - Too long: do you really want reboot recovery
938 * to take more than an hour? Or to make other
939 * clients wait an hour before being able to
940 * revoke a dead client's locks?
942 if (i < 10 || i > 3600)
947 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
950 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
951 time64_t *time, struct nfsd_net *nn)
955 mutex_lock(&nfsd_mutex);
956 rv = __nfsd4_write_time(file, buf, size, time, nn);
957 mutex_unlock(&nfsd_mutex);
962 * write_leasetime - Set or report the current NFSv4 lease time
971 * buf: C string containing an unsigned
972 * integer value representing the new
973 * NFSv4 lease expiry time
974 * size: non-zero length of C string in @buf
976 * On success: passed-in buffer filled with '\n'-terminated C
977 * string containing unsigned integer value of the
978 * current lease expiry time;
979 * return code is the size in bytes of the string
980 * On error: return code is zero or a negative errno value
982 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
984 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
985 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
989 * write_gracetime - Set or report current NFSv4 grace period time
991 * As above, but sets the time of the NFSv4 grace period.
993 * Note this should never be set to less than the *previous*
994 * lease-period time, but we don't try to enforce this. (In the common
995 * case (a new boot), we don't know what the previous lease time was
998 static ssize_t write_gracetime(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_grace, nn);
1004 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1005 struct nfsd_net *nn)
1014 if (size > PATH_MAX || buf[size-1] != '\n')
1019 len = qword_get(&mesg, recdir, size);
1023 status = nfs4_reset_recoverydir(recdir);
1028 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1029 nfs4_recoverydir());
1033 * write_recoverydir - Set or report the pathname of the recovery directory
1042 * buf: C string containing the pathname
1043 * of the directory on a local file
1044 * system containing permanent NFSv4
1046 * size: non-zero length of C string in @buf
1048 * On success: passed-in buffer filled with '\n'-terminated C string
1049 * containing the current recovery pathname setting;
1050 * return code is the size in bytes of the string
1051 * On error: return code is zero or a negative errno value
1053 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1056 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1058 mutex_lock(&nfsd_mutex);
1059 rv = __write_recoverydir(file, buf, size, nn);
1060 mutex_unlock(&nfsd_mutex);
1065 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1074 * size: non-zero length of C string in @buf
1076 * passed-in buffer filled with "Y" or "N" with a newline
1077 * and NULL-terminated C string. This indicates whether
1078 * the grace period has ended in the current net
1079 * namespace. Return code is the size in bytes of the
1080 * string. Writing a string that starts with 'Y', 'y', or
1081 * '1' to the file will end the grace period for nfsd's v4
1084 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1086 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1095 nfsd4_end_grace(nn);
1102 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1103 nn->grace_ended ? 'Y' : 'N');
1108 /*----------------------------------------------------------------------------*/
1110 * populating the filesystem.
1113 /* Basically copying rpc_get_inode. */
1114 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1116 struct inode *inode = new_inode(sb);
1119 /* Following advice from simple_fill_super documentation: */
1120 inode->i_ino = iunique(sb, NFSD_MaxReserved);
1121 inode->i_mode = mode;
1122 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1123 switch (mode & S_IFMT) {
1125 inode->i_fop = &simple_dir_operations;
1126 inode->i_op = &simple_dir_inode_operations;
1130 inode->i_op = &simple_symlink_inode_operations;
1138 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1140 struct inode *inode;
1142 inode = nfsd_get_inode(dir->i_sb, mode);
1146 inode->i_private = ncl;
1147 kref_get(&ncl->cl_ref);
1149 d_add(dentry, inode);
1151 fsnotify_mkdir(dir, dentry);
1155 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1157 struct inode *dir = parent->d_inode;
1158 struct dentry *dentry;
1162 dentry = d_alloc_name(parent, name);
1165 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1173 dentry = ERR_PTR(ret);
1177 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1178 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1179 umode_t mode, const char *content)
1181 struct inode *inode;
1183 inode = nfsd_get_inode(dir->i_sb, mode);
1187 inode->i_link = (char *)content;
1188 inode->i_size = strlen(content);
1190 d_add(dentry, inode);
1192 fsnotify_create(dir, dentry);
1197 * @content is assumed to be a NUL-terminated string that lives
1198 * longer than the symlink itself.
1200 static void nfsd_symlink(struct dentry *parent, const char *name,
1201 const char *content)
1203 struct inode *dir = parent->d_inode;
1204 struct dentry *dentry;
1208 dentry = d_alloc_name(parent, name);
1211 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1218 static inline void nfsd_symlink(struct dentry *parent, const char *name,
1219 const char *content)
1225 static void clear_ncl(struct inode *inode)
1227 struct nfsdfs_client *ncl = inode->i_private;
1229 inode->i_private = NULL;
1230 kref_put(&ncl->cl_ref, ncl->cl_release);
1233 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1235 struct nfsdfs_client *nc = inode->i_private;
1238 kref_get(&nc->cl_ref);
1242 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1244 struct nfsdfs_client *nc;
1246 inode_lock_shared(inode);
1247 nc = __get_nfsdfs_client(inode);
1248 inode_unlock_shared(inode);
1251 /* from __rpc_unlink */
1252 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1256 clear_ncl(d_inode(dentry));
1258 ret = simple_unlink(dir, dentry);
1260 fsnotify_unlink(dir, dentry);
1265 static void nfsdfs_remove_files(struct dentry *root)
1267 struct dentry *dentry, *tmp;
1269 list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1270 if (!simple_positive(dentry)) {
1271 WARN_ON_ONCE(1); /* I think this can't happen? */
1274 nfsdfs_remove_file(d_inode(root), dentry);
1278 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1280 static int nfsdfs_create_files(struct dentry *root,
1281 const struct tree_descr *files,
1282 struct dentry **fdentries)
1284 struct inode *dir = d_inode(root);
1285 struct inode *inode;
1286 struct dentry *dentry;
1290 for (i = 0; files->name && files->name[0]; i++, files++) {
1291 dentry = d_alloc_name(root, files->name);
1294 inode = nfsd_get_inode(d_inode(root)->i_sb,
1295 S_IFREG | files->mode);
1300 inode->i_fop = files->ops;
1301 inode->i_private = __get_nfsdfs_client(dir);
1302 d_add(dentry, inode);
1303 fsnotify_create(dir, dentry);
1305 fdentries[i] = dentry;
1310 nfsdfs_remove_files(root);
1315 /* on success, returns positive number unique to that client. */
1316 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1317 struct nfsdfs_client *ncl, u32 id,
1318 const struct tree_descr *files,
1319 struct dentry **fdentries)
1321 struct dentry *dentry;
1325 sprintf(name, "%u", id);
1327 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1328 if (IS_ERR(dentry)) /* XXX: tossing errors? */
1330 ret = nfsdfs_create_files(dentry, files, fdentries);
1332 nfsd_client_rmdir(dentry);
1338 /* Taken from __rpc_rmdir: */
1339 void nfsd_client_rmdir(struct dentry *dentry)
1341 struct inode *dir = d_inode(dentry->d_parent);
1342 struct inode *inode = d_inode(dentry);
1346 nfsdfs_remove_files(dentry);
1349 ret = simple_rmdir(dir, dentry);
1352 fsnotify_rmdir(dir, dentry);
1357 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1359 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1361 struct dentry *dentry;
1364 static const struct tree_descr nfsd_files[] = {
1365 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1366 /* Per-export io stats use same ops as exports file */
1367 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1368 [NFSD_Export_features] = {"export_features",
1369 &export_features_fops, S_IRUGO},
1370 [NFSD_FO_UnlockIP] = {"unlock_ip",
1371 &transaction_ops, S_IWUSR|S_IRUSR},
1372 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1373 &transaction_ops, S_IWUSR|S_IRUSR},
1374 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1375 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1376 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1377 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1378 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1379 &nfsd_reply_cache_stats_fops, S_IRUGO},
1380 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1381 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1382 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1383 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1384 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1385 #ifdef CONFIG_NFSD_V4
1386 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1387 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1388 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1389 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1394 ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1397 nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1398 "/proc/net/rpc/gss_krb5_enctypes");
1399 dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1401 return PTR_ERR(dentry);
1402 nn->nfsd_client_dir = dentry;
1406 static int nfsd_fs_get_tree(struct fs_context *fc)
1408 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1411 static void nfsd_fs_free_fc(struct fs_context *fc)
1414 put_net(fc->s_fs_info);
1417 static const struct fs_context_operations nfsd_fs_context_ops = {
1418 .free = nfsd_fs_free_fc,
1419 .get_tree = nfsd_fs_get_tree,
1422 static int nfsd_init_fs_context(struct fs_context *fc)
1424 put_user_ns(fc->user_ns);
1425 fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1426 fc->ops = &nfsd_fs_context_ops;
1430 static void nfsd_umount(struct super_block *sb)
1432 struct net *net = sb->s_fs_info;
1434 nfsd_shutdown_threads(net);
1436 kill_litter_super(sb);
1440 static struct file_system_type nfsd_fs_type = {
1441 .owner = THIS_MODULE,
1443 .init_fs_context = nfsd_init_fs_context,
1444 .kill_sb = nfsd_umount,
1446 MODULE_ALIAS_FS("nfsd");
1448 #ifdef CONFIG_PROC_FS
1450 static int exports_proc_open(struct inode *inode, struct file *file)
1452 return exports_net_open(current->nsproxy->net_ns, file);
1455 static const struct proc_ops exports_proc_ops = {
1456 .proc_open = exports_proc_open,
1457 .proc_read = seq_read,
1458 .proc_lseek = seq_lseek,
1459 .proc_release = seq_release,
1462 static int create_proc_exports_entry(void)
1464 struct proc_dir_entry *entry;
1466 entry = proc_mkdir("fs/nfs", NULL);
1469 entry = proc_create("exports", 0, entry, &exports_proc_ops);
1471 remove_proc_entry("fs/nfs", NULL);
1476 #else /* CONFIG_PROC_FS */
1477 static int create_proc_exports_entry(void)
1483 unsigned int nfsd_net_id;
1485 static __net_init int nfsd_init_net(struct net *net)
1488 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1490 retval = nfsd_export_init(net);
1492 goto out_export_error;
1493 retval = nfsd_idmap_init(net);
1495 goto out_idmap_error;
1496 nn->nfsd_versions = NULL;
1497 nn->nfsd4_minorversions = NULL;
1498 nfsd4_init_leases_net(nn);
1499 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1500 seqlock_init(&nn->writeverf_lock);
1505 nfsd_export_shutdown(net);
1510 static __net_exit void nfsd_exit_net(struct net *net)
1512 nfsd_idmap_shutdown(net);
1513 nfsd_export_shutdown(net);
1514 nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1517 static struct pernet_operations nfsd_net_ops = {
1518 .init = nfsd_init_net,
1519 .exit = nfsd_exit_net,
1521 .size = sizeof(struct nfsd_net),
1524 static int __init init_nfsd(void)
1528 retval = nfsd4_init_slabs();
1531 retval = nfsd4_init_pnfs();
1533 goto out_free_slabs;
1534 retval = nfsd_stat_init(); /* Statistics */
1537 retval = nfsd_drc_slab_create();
1540 nfsd_lockd_init(); /* lockd->nfsd callbacks */
1541 retval = create_proc_exports_entry();
1543 goto out_free_lockd;
1544 retval = register_pernet_subsys(&nfsd_net_ops);
1546 goto out_free_exports;
1547 retval = register_cld_notifier();
1549 goto out_free_subsys;
1550 retval = nfsd4_create_laundry_wq();
1553 retval = register_filesystem(&nfsd_fs_type);
1558 nfsd4_destroy_laundry_wq();
1560 unregister_cld_notifier();
1562 unregister_pernet_subsys(&nfsd_net_ops);
1564 remove_proc_entry("fs/nfs/exports", NULL);
1565 remove_proc_entry("fs/nfs", NULL);
1567 nfsd_lockd_shutdown();
1568 nfsd_drc_slab_free();
1570 nfsd_stat_shutdown();
1578 static void __exit exit_nfsd(void)
1580 unregister_filesystem(&nfsd_fs_type);
1581 nfsd4_destroy_laundry_wq();
1582 unregister_cld_notifier();
1583 unregister_pernet_subsys(&nfsd_net_ops);
1584 nfsd_drc_slab_free();
1585 remove_proc_entry("fs/nfs/exports", NULL);
1586 remove_proc_entry("fs/nfs", NULL);
1587 nfsd_stat_shutdown();
1588 nfsd_lockd_shutdown();
1594 MODULE_LICENSE("GPL");
1595 module_init(init_nfsd)
1596 module_exit(exit_nfsd)