]> Git Repo - linux.git/blob - fs/nfsd/nfsctl.c
Merge patch series "riscv: Extension parsing fixes"
[linux.git] / fs / nfsd / nfsctl.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Syscall interface to knfsd.
4  *
5  * Copyright (C) 1995, 1996 Olaf Kirch <[email protected]>
6  */
7
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/fs_context.h>
12
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/rpc_pipe_fs.h>
18 #include <linux/sunrpc/svc.h>
19 #include <linux/module.h>
20 #include <linux/fsnotify.h>
21
22 #include "idmap.h"
23 #include "nfsd.h"
24 #include "cache.h"
25 #include "state.h"
26 #include "netns.h"
27 #include "pnfs.h"
28 #include "filecache.h"
29 #include "trace.h"
30 #include "netlink.h"
31
32 /*
33  *      We have a single directory with several nodes in it.
34  */
35 enum {
36         NFSD_Root = 1,
37         NFSD_List,
38         NFSD_Export_Stats,
39         NFSD_Export_features,
40         NFSD_Fh,
41         NFSD_FO_UnlockIP,
42         NFSD_FO_UnlockFS,
43         NFSD_Threads,
44         NFSD_Pool_Threads,
45         NFSD_Pool_Stats,
46         NFSD_Reply_Cache_Stats,
47         NFSD_Versions,
48         NFSD_Ports,
49         NFSD_MaxBlkSize,
50         NFSD_MaxConnections,
51         NFSD_Filecache,
52         NFSD_Leasetime,
53         NFSD_Gracetime,
54         NFSD_RecoveryDir,
55         NFSD_V4EndGrace,
56         NFSD_MaxReserved
57 };
58
59 /*
60  * write() for these nodes.
61  */
62 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
63 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
64 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
65 static ssize_t write_threads(struct file *file, char *buf, size_t size);
66 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
67 static ssize_t write_versions(struct file *file, char *buf, size_t size);
68 static ssize_t write_ports(struct file *file, char *buf, size_t size);
69 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
70 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
71 #ifdef CONFIG_NFSD_V4
72 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
73 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
74 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
75 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
76 #endif
77 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
78 #endif
79
80 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
81         [NFSD_Fh] = write_filehandle,
82         [NFSD_FO_UnlockIP] = write_unlock_ip,
83         [NFSD_FO_UnlockFS] = write_unlock_fs,
84         [NFSD_Threads] = write_threads,
85         [NFSD_Pool_Threads] = write_pool_threads,
86         [NFSD_Versions] = write_versions,
87         [NFSD_Ports] = write_ports,
88         [NFSD_MaxBlkSize] = write_maxblksize,
89         [NFSD_MaxConnections] = write_maxconn,
90 #ifdef CONFIG_NFSD_V4
91         [NFSD_Leasetime] = write_leasetime,
92         [NFSD_Gracetime] = write_gracetime,
93 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
94         [NFSD_RecoveryDir] = write_recoverydir,
95 #endif
96         [NFSD_V4EndGrace] = write_v4_end_grace,
97 #endif
98 };
99
100 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
101 {
102         ino_t ino =  file_inode(file)->i_ino;
103         char *data;
104         ssize_t rv;
105
106         if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
107                 return -EINVAL;
108
109         data = simple_transaction_get(file, buf, size);
110         if (IS_ERR(data))
111                 return PTR_ERR(data);
112
113         rv = write_op[ino](file, data, size);
114         if (rv < 0)
115                 return rv;
116
117         simple_transaction_set(file, rv);
118         return size;
119 }
120
121 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
122 {
123         if (! file->private_data) {
124                 /* An attempt to read a transaction file without writing
125                  * causes a 0-byte write so that the file can return
126                  * state information
127                  */
128                 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
129                 if (rv < 0)
130                         return rv;
131         }
132         return simple_transaction_read(file, buf, size, pos);
133 }
134
135 static const struct file_operations transaction_ops = {
136         .write          = nfsctl_transaction_write,
137         .read           = nfsctl_transaction_read,
138         .release        = simple_transaction_release,
139         .llseek         = default_llseek,
140 };
141
142 static int exports_net_open(struct net *net, struct file *file)
143 {
144         int err;
145         struct seq_file *seq;
146         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
147
148         err = seq_open(file, &nfs_exports_op);
149         if (err)
150                 return err;
151
152         seq = file->private_data;
153         seq->private = nn->svc_export_cache;
154         return 0;
155 }
156
157 static int exports_nfsd_open(struct inode *inode, struct file *file)
158 {
159         return exports_net_open(inode->i_sb->s_fs_info, file);
160 }
161
162 static const struct file_operations exports_nfsd_operations = {
163         .open           = exports_nfsd_open,
164         .read           = seq_read,
165         .llseek         = seq_lseek,
166         .release        = seq_release,
167 };
168
169 static int export_features_show(struct seq_file *m, void *v)
170 {
171         seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
172         return 0;
173 }
174
175 DEFINE_SHOW_ATTRIBUTE(export_features);
176
177 static const struct file_operations pool_stats_operations = {
178         .open           = nfsd_pool_stats_open,
179         .read           = seq_read,
180         .llseek         = seq_lseek,
181         .release        = seq_release,
182 };
183
184 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
185
186 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
187
188 /*----------------------------------------------------------------------------*/
189 /*
190  * payload - write methods
191  */
192
193 static inline struct net *netns(struct file *file)
194 {
195         return file_inode(file)->i_sb->s_fs_info;
196 }
197
198 /*
199  * write_unlock_ip - Release all locks used by a client
200  *
201  * Experimental.
202  *
203  * Input:
204  *                      buf:    '\n'-terminated C string containing a
205  *                              presentation format IP address
206  *                      size:   length of C string in @buf
207  * Output:
208  *      On success:     returns zero if all specified locks were released;
209  *                      returns one if one or more locks were not released
210  *      On error:       return code is negative errno value
211  */
212 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
213 {
214         struct sockaddr_storage address;
215         struct sockaddr *sap = (struct sockaddr *)&address;
216         size_t salen = sizeof(address);
217         char *fo_path;
218         struct net *net = netns(file);
219
220         /* sanity check */
221         if (size == 0)
222                 return -EINVAL;
223
224         if (buf[size-1] != '\n')
225                 return -EINVAL;
226
227         fo_path = buf;
228         if (qword_get(&buf, fo_path, size) < 0)
229                 return -EINVAL;
230
231         if (rpc_pton(net, fo_path, size, sap, salen) == 0)
232                 return -EINVAL;
233
234         trace_nfsd_ctl_unlock_ip(net, buf);
235         return nlmsvc_unlock_all_by_ip(sap);
236 }
237
238 /*
239  * write_unlock_fs - Release all locks on a local file system
240  *
241  * Experimental.
242  *
243  * Input:
244  *                      buf:    '\n'-terminated C string containing the
245  *                              absolute pathname of a local file system
246  *                      size:   length of C string in @buf
247  * Output:
248  *      On success:     returns zero if all specified locks were released;
249  *                      returns one if one or more locks were not released
250  *      On error:       return code is negative errno value
251  */
252 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
253 {
254         struct path path;
255         char *fo_path;
256         int error;
257
258         /* sanity check */
259         if (size == 0)
260                 return -EINVAL;
261
262         if (buf[size-1] != '\n')
263                 return -EINVAL;
264
265         fo_path = buf;
266         if (qword_get(&buf, fo_path, size) < 0)
267                 return -EINVAL;
268         trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
269         error = kern_path(fo_path, 0, &path);
270         if (error)
271                 return error;
272
273         /*
274          * XXX: Needs better sanity checking.  Otherwise we could end up
275          * releasing locks on the wrong file system.
276          *
277          * For example:
278          * 1.  Does the path refer to a directory?
279          * 2.  Is that directory a mount point, or
280          * 3.  Is that directory the root of an exported file system?
281          */
282         error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
283         nfsd4_revoke_states(netns(file), path.dentry->d_sb);
284
285         path_put(&path);
286         return error;
287 }
288
289 /*
290  * write_filehandle - Get a variable-length NFS file handle by path
291  *
292  * On input, the buffer contains a '\n'-terminated C string comprised of
293  * three alphanumeric words separated by whitespace.  The string may
294  * contain escape sequences.
295  *
296  * Input:
297  *                      buf:
298  *                              domain:         client domain name
299  *                              path:           export pathname
300  *                              maxsize:        numeric maximum size of
301  *                                              @buf
302  *                      size:   length of C string in @buf
303  * Output:
304  *      On success:     passed-in buffer filled with '\n'-terminated C
305  *                      string containing a ASCII hex text version
306  *                      of the NFS file handle;
307  *                      return code is the size in bytes of the string
308  *      On error:       return code is negative errno value
309  */
310 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
311 {
312         char *dname, *path;
313         int maxsize;
314         char *mesg = buf;
315         int len;
316         struct auth_domain *dom;
317         struct knfsd_fh fh;
318
319         if (size == 0)
320                 return -EINVAL;
321
322         if (buf[size-1] != '\n')
323                 return -EINVAL;
324         buf[size-1] = 0;
325
326         dname = mesg;
327         len = qword_get(&mesg, dname, size);
328         if (len <= 0)
329                 return -EINVAL;
330
331         path = dname+len+1;
332         len = qword_get(&mesg, path, size);
333         if (len <= 0)
334                 return -EINVAL;
335
336         len = get_int(&mesg, &maxsize);
337         if (len)
338                 return len;
339
340         if (maxsize < NFS_FHSIZE)
341                 return -EINVAL;
342         maxsize = min(maxsize, NFS3_FHSIZE);
343
344         if (qword_get(&mesg, mesg, size) > 0)
345                 return -EINVAL;
346
347         trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
348
349         /* we have all the words, they are in buf.. */
350         dom = unix_domain_find(dname);
351         if (!dom)
352                 return -ENOMEM;
353
354         len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
355         auth_domain_put(dom);
356         if (len)
357                 return len;
358
359         mesg = buf;
360         len = SIMPLE_TRANSACTION_LIMIT;
361         qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
362         mesg[-1] = '\n';
363         return mesg - buf;
364 }
365
366 /*
367  * write_threads - Start NFSD, or report the current number of running threads
368  *
369  * Input:
370  *                      buf:            ignored
371  *                      size:           zero
372  * Output:
373  *      On success:     passed-in buffer filled with '\n'-terminated C
374  *                      string numeric value representing the number of
375  *                      running NFSD threads;
376  *                      return code is the size in bytes of the string
377  *      On error:       return code is zero
378  *
379  * OR
380  *
381  * Input:
382  *                      buf:            C string containing an unsigned
383  *                                      integer value representing the
384  *                                      number of NFSD threads to start
385  *                      size:           non-zero length of C string in @buf
386  * Output:
387  *      On success:     NFS service is started;
388  *                      passed-in buffer filled with '\n'-terminated C
389  *                      string numeric value representing the number of
390  *                      running NFSD threads;
391  *                      return code is the size in bytes of the string
392  *      On error:       return code is zero or a negative errno value
393  */
394 static ssize_t write_threads(struct file *file, char *buf, size_t size)
395 {
396         char *mesg = buf;
397         int rv;
398         struct net *net = netns(file);
399
400         if (size > 0) {
401                 int newthreads;
402                 rv = get_int(&mesg, &newthreads);
403                 if (rv)
404                         return rv;
405                 if (newthreads < 0)
406                         return -EINVAL;
407                 trace_nfsd_ctl_threads(net, newthreads);
408                 mutex_lock(&nfsd_mutex);
409                 rv = nfsd_svc(newthreads, net, file->f_cred, NULL);
410                 mutex_unlock(&nfsd_mutex);
411                 if (rv < 0)
412                         return rv;
413         } else
414                 rv = nfsd_nrthreads(net);
415
416         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
417 }
418
419 /*
420  * write_pool_threads - Set or report the current number of threads per pool
421  *
422  * Input:
423  *                      buf:            ignored
424  *                      size:           zero
425  *
426  * OR
427  *
428  * Input:
429  *                      buf:            C string containing whitespace-
430  *                                      separated unsigned integer values
431  *                                      representing the number of NFSD
432  *                                      threads to start in each pool
433  *                      size:           non-zero length of C string in @buf
434  * Output:
435  *      On success:     passed-in buffer filled with '\n'-terminated C
436  *                      string containing integer values representing the
437  *                      number of NFSD threads in each pool;
438  *                      return code is the size in bytes of the string
439  *      On error:       return code is zero or a negative errno value
440  */
441 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
442 {
443         /* if size > 0, look for an array of number of threads per node
444          * and apply them  then write out number of threads per node as reply
445          */
446         char *mesg = buf;
447         int i;
448         int rv;
449         int len;
450         int npools;
451         int *nthreads;
452         struct net *net = netns(file);
453
454         mutex_lock(&nfsd_mutex);
455         npools = nfsd_nrpools(net);
456         if (npools == 0) {
457                 /*
458                  * NFS is shut down.  The admin can start it by
459                  * writing to the threads file but NOT the pool_threads
460                  * file, sorry.  Report zero threads.
461                  */
462                 mutex_unlock(&nfsd_mutex);
463                 strcpy(buf, "0\n");
464                 return strlen(buf);
465         }
466
467         nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
468         rv = -ENOMEM;
469         if (nthreads == NULL)
470                 goto out_free;
471
472         if (size > 0) {
473                 for (i = 0; i < npools; i++) {
474                         rv = get_int(&mesg, &nthreads[i]);
475                         if (rv == -ENOENT)
476                                 break;          /* fewer numbers than pools */
477                         if (rv)
478                                 goto out_free;  /* syntax error */
479                         rv = -EINVAL;
480                         if (nthreads[i] < 0)
481                                 goto out_free;
482                         trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
483                 }
484                 rv = nfsd_set_nrthreads(i, nthreads, net);
485                 if (rv)
486                         goto out_free;
487         }
488
489         rv = nfsd_get_nrthreads(npools, nthreads, net);
490         if (rv)
491                 goto out_free;
492
493         mesg = buf;
494         size = SIMPLE_TRANSACTION_LIMIT;
495         for (i = 0; i < npools && size > 0; i++) {
496                 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
497                 len = strlen(mesg);
498                 size -= len;
499                 mesg += len;
500         }
501         rv = mesg - buf;
502 out_free:
503         kfree(nthreads);
504         mutex_unlock(&nfsd_mutex);
505         return rv;
506 }
507
508 static ssize_t
509 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
510                 const char *sep, unsigned vers, int minor)
511 {
512         const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
513         bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
514
515         if (vers == 4 && minor >= 0 &&
516             !nfsd_minorversion(nn, minor, NFSD_TEST))
517                 supported = false;
518         if (minor == 0 && supported)
519                 /*
520                  * special case for backward compatability.
521                  * +4.0 is never reported, it is implied by
522                  * +4, unless -4.0 is present.
523                  */
524                 return 0;
525         return snprintf(buf, remaining, format, sep,
526                         supported ? '+' : '-', vers, minor);
527 }
528
529 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
530 {
531         char *mesg = buf;
532         char *vers, *minorp, sign;
533         int len, num, remaining;
534         ssize_t tlen = 0;
535         char *sep;
536         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
537
538         if (size > 0) {
539                 if (nn->nfsd_serv)
540                         /* Cannot change versions without updating
541                          * nn->nfsd_serv->sv_xdrsize, and reallocing
542                          * rq_argp and rq_resp
543                          */
544                         return -EBUSY;
545                 if (buf[size-1] != '\n')
546                         return -EINVAL;
547                 buf[size-1] = 0;
548                 trace_nfsd_ctl_version(netns(file), buf);
549
550                 vers = mesg;
551                 len = qword_get(&mesg, vers, size);
552                 if (len <= 0) return -EINVAL;
553                 do {
554                         enum vers_op cmd;
555                         unsigned minor;
556                         sign = *vers;
557                         if (sign == '+' || sign == '-')
558                                 num = simple_strtol((vers+1), &minorp, 0);
559                         else
560                                 num = simple_strtol(vers, &minorp, 0);
561                         if (*minorp == '.') {
562                                 if (num != 4)
563                                         return -EINVAL;
564                                 if (kstrtouint(minorp+1, 0, &minor) < 0)
565                                         return -EINVAL;
566                         }
567
568                         cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
569                         switch(num) {
570 #ifdef CONFIG_NFSD_V2
571                         case 2:
572 #endif
573                         case 3:
574                                 nfsd_vers(nn, num, cmd);
575                                 break;
576                         case 4:
577                                 if (*minorp == '.') {
578                                         if (nfsd_minorversion(nn, minor, cmd) < 0)
579                                                 return -EINVAL;
580                                 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
581                                         /*
582                                          * Either we have +4 and no minors are enabled,
583                                          * or we have -4 and at least one minor is enabled.
584                                          * In either case, propagate 'cmd' to all minors.
585                                          */
586                                         minor = 0;
587                                         while (nfsd_minorversion(nn, minor, cmd) >= 0)
588                                                 minor++;
589                                 }
590                                 break;
591                         default:
592                                 /* Ignore requests to disable non-existent versions */
593                                 if (cmd == NFSD_SET)
594                                         return -EINVAL;
595                         }
596                         vers += len + 1;
597                 } while ((len = qword_get(&mesg, vers, size)) > 0);
598                 /* If all get turned off, turn them back on, as
599                  * having no versions is BAD
600                  */
601                 nfsd_reset_versions(nn);
602         }
603
604         /* Now write current state into reply buffer */
605         sep = "";
606         remaining = SIMPLE_TRANSACTION_LIMIT;
607         for (num=2 ; num <= 4 ; num++) {
608                 int minor;
609                 if (!nfsd_vers(nn, num, NFSD_AVAIL))
610                         continue;
611
612                 minor = -1;
613                 do {
614                         len = nfsd_print_version_support(nn, buf, remaining,
615                                         sep, num, minor);
616                         if (len >= remaining)
617                                 goto out;
618                         remaining -= len;
619                         buf += len;
620                         tlen += len;
621                         minor++;
622                         if (len)
623                                 sep = " ";
624                 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
625         }
626 out:
627         len = snprintf(buf, remaining, "\n");
628         if (len >= remaining)
629                 return -EINVAL;
630         return tlen + len;
631 }
632
633 /*
634  * write_versions - Set or report the available NFS protocol versions
635  *
636  * Input:
637  *                      buf:            ignored
638  *                      size:           zero
639  * Output:
640  *      On success:     passed-in buffer filled with '\n'-terminated C
641  *                      string containing positive or negative integer
642  *                      values representing the current status of each
643  *                      protocol version;
644  *                      return code is the size in bytes of the string
645  *      On error:       return code is zero or a negative errno value
646  *
647  * OR
648  *
649  * Input:
650  *                      buf:            C string containing whitespace-
651  *                                      separated positive or negative
652  *                                      integer values representing NFS
653  *                                      protocol versions to enable ("+n")
654  *                                      or disable ("-n")
655  *                      size:           non-zero length of C string in @buf
656  * Output:
657  *      On success:     status of zero or more protocol versions has
658  *                      been updated; passed-in buffer filled with
659  *                      '\n'-terminated C string containing positive
660  *                      or negative integer values representing the
661  *                      current status of each protocol version;
662  *                      return code is the size in bytes of the string
663  *      On error:       return code is zero or a negative errno value
664  */
665 static ssize_t write_versions(struct file *file, char *buf, size_t size)
666 {
667         ssize_t rv;
668
669         mutex_lock(&nfsd_mutex);
670         rv = __write_versions(file, buf, size);
671         mutex_unlock(&nfsd_mutex);
672         return rv;
673 }
674
675 /*
676  * Zero-length write.  Return a list of NFSD's current listener
677  * transports.
678  */
679 static ssize_t __write_ports_names(char *buf, struct net *net)
680 {
681         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
682
683         if (nn->nfsd_serv == NULL)
684                 return 0;
685         return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
686 }
687
688 /*
689  * A single 'fd' number was written, in which case it must be for
690  * a socket of a supported family/protocol, and we use it as an
691  * nfsd listener.
692  */
693 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
694 {
695         char *mesg = buf;
696         int fd, err;
697         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
698         struct svc_serv *serv;
699
700         err = get_int(&mesg, &fd);
701         if (err != 0 || fd < 0)
702                 return -EINVAL;
703         trace_nfsd_ctl_ports_addfd(net, fd);
704
705         err = nfsd_create_serv(net);
706         if (err != 0)
707                 return err;
708
709         serv = nn->nfsd_serv;
710         err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
711
712         if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
713                 nfsd_destroy_serv(net);
714
715         return err;
716 }
717
718 /*
719  * A transport listener is added by writing its transport name and
720  * a port number.
721  */
722 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
723 {
724         char transport[16];
725         struct svc_xprt *xprt;
726         int port, err;
727         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
728         struct svc_serv *serv;
729
730         if (sscanf(buf, "%15s %5u", transport, &port) != 2)
731                 return -EINVAL;
732
733         if (port < 1 || port > USHRT_MAX)
734                 return -EINVAL;
735         trace_nfsd_ctl_ports_addxprt(net, transport, port);
736
737         err = nfsd_create_serv(net);
738         if (err != 0)
739                 return err;
740
741         serv = nn->nfsd_serv;
742         err = svc_xprt_create(serv, transport, net,
743                               PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
744         if (err < 0)
745                 goto out_err;
746
747         err = svc_xprt_create(serv, transport, net,
748                               PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
749         if (err < 0 && err != -EAFNOSUPPORT)
750                 goto out_close;
751
752         return 0;
753 out_close:
754         xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
755         if (xprt != NULL) {
756                 svc_xprt_close(xprt);
757                 svc_xprt_put(xprt);
758         }
759 out_err:
760         if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
761                 nfsd_destroy_serv(net);
762
763         return err;
764 }
765
766 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
767                              struct net *net)
768 {
769         if (size == 0)
770                 return __write_ports_names(buf, net);
771
772         if (isdigit(buf[0]))
773                 return __write_ports_addfd(buf, net, file->f_cred);
774
775         if (isalpha(buf[0]))
776                 return __write_ports_addxprt(buf, net, file->f_cred);
777
778         return -EINVAL;
779 }
780
781 /*
782  * write_ports - Pass a socket file descriptor or transport name to listen on
783  *
784  * Input:
785  *                      buf:            ignored
786  *                      size:           zero
787  * Output:
788  *      On success:     passed-in buffer filled with a '\n'-terminated C
789  *                      string containing a whitespace-separated list of
790  *                      named NFSD listeners;
791  *                      return code is the size in bytes of the string
792  *      On error:       return code is zero or a negative errno value
793  *
794  * OR
795  *
796  * Input:
797  *                      buf:            C string containing an unsigned
798  *                                      integer value representing a bound
799  *                                      but unconnected socket that is to be
800  *                                      used as an NFSD listener; listen(3)
801  *                                      must be called for a SOCK_STREAM
802  *                                      socket, otherwise it is ignored
803  *                      size:           non-zero length of C string in @buf
804  * Output:
805  *      On success:     NFS service is started;
806  *                      passed-in buffer filled with a '\n'-terminated C
807  *                      string containing a unique alphanumeric name of
808  *                      the listener;
809  *                      return code is the size in bytes of the string
810  *      On error:       return code is a negative errno value
811  *
812  * OR
813  *
814  * Input:
815  *                      buf:            C string containing a transport
816  *                                      name and an unsigned integer value
817  *                                      representing the port to listen on,
818  *                                      separated by whitespace
819  *                      size:           non-zero length of C string in @buf
820  * Output:
821  *      On success:     returns zero; NFS service is started
822  *      On error:       return code is a negative errno value
823  */
824 static ssize_t write_ports(struct file *file, char *buf, size_t size)
825 {
826         ssize_t rv;
827
828         mutex_lock(&nfsd_mutex);
829         rv = __write_ports(file, buf, size, netns(file));
830         mutex_unlock(&nfsd_mutex);
831         return rv;
832 }
833
834
835 int nfsd_max_blksize;
836
837 /*
838  * write_maxblksize - Set or report the current NFS blksize
839  *
840  * Input:
841  *                      buf:            ignored
842  *                      size:           zero
843  *
844  * OR
845  *
846  * Input:
847  *                      buf:            C string containing an unsigned
848  *                                      integer value representing the new
849  *                                      NFS blksize
850  *                      size:           non-zero length of C string in @buf
851  * Output:
852  *      On success:     passed-in buffer filled with '\n'-terminated C string
853  *                      containing numeric value of the current NFS blksize
854  *                      setting;
855  *                      return code is the size in bytes of the string
856  *      On error:       return code is zero or a negative errno value
857  */
858 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
859 {
860         char *mesg = buf;
861         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
862
863         if (size > 0) {
864                 int bsize;
865                 int rv = get_int(&mesg, &bsize);
866                 if (rv)
867                         return rv;
868                 trace_nfsd_ctl_maxblksize(netns(file), bsize);
869
870                 /* force bsize into allowed range and
871                  * required alignment.
872                  */
873                 bsize = max_t(int, bsize, 1024);
874                 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
875                 bsize &= ~(1024-1);
876                 mutex_lock(&nfsd_mutex);
877                 if (nn->nfsd_serv) {
878                         mutex_unlock(&nfsd_mutex);
879                         return -EBUSY;
880                 }
881                 nfsd_max_blksize = bsize;
882                 mutex_unlock(&nfsd_mutex);
883         }
884
885         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
886                                                         nfsd_max_blksize);
887 }
888
889 /*
890  * write_maxconn - Set or report the current max number of connections
891  *
892  * Input:
893  *                      buf:            ignored
894  *                      size:           zero
895  * OR
896  *
897  * Input:
898  *                      buf:            C string containing an unsigned
899  *                                      integer value representing the new
900  *                                      number of max connections
901  *                      size:           non-zero length of C string in @buf
902  * Output:
903  *      On success:     passed-in buffer filled with '\n'-terminated C string
904  *                      containing numeric value of max_connections setting
905  *                      for this net namespace;
906  *                      return code is the size in bytes of the string
907  *      On error:       return code is zero or a negative errno value
908  */
909 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
910 {
911         char *mesg = buf;
912         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
913         unsigned int maxconn = nn->max_connections;
914
915         if (size > 0) {
916                 int rv = get_uint(&mesg, &maxconn);
917
918                 if (rv)
919                         return rv;
920                 trace_nfsd_ctl_maxconn(netns(file), maxconn);
921                 nn->max_connections = maxconn;
922         }
923
924         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
925 }
926
927 #ifdef CONFIG_NFSD_V4
928 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
929                                   time64_t *time, struct nfsd_net *nn)
930 {
931         struct dentry *dentry = file_dentry(file);
932         char *mesg = buf;
933         int rv, i;
934
935         if (size > 0) {
936                 if (nn->nfsd_serv)
937                         return -EBUSY;
938                 rv = get_int(&mesg, &i);
939                 if (rv)
940                         return rv;
941                 trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
942                                     dentry->d_name.len, i);
943
944                 /*
945                  * Some sanity checking.  We don't have a reason for
946                  * these particular numbers, but problems with the
947                  * extremes are:
948                  *      - Too short: the briefest network outage may
949                  *        cause clients to lose all their locks.  Also,
950                  *        the frequent polling may be wasteful.
951                  *      - Too long: do you really want reboot recovery
952                  *        to take more than an hour?  Or to make other
953                  *        clients wait an hour before being able to
954                  *        revoke a dead client's locks?
955                  */
956                 if (i < 10 || i > 3600)
957                         return -EINVAL;
958                 *time = i;
959         }
960
961         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
962 }
963
964 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
965                                 time64_t *time, struct nfsd_net *nn)
966 {
967         ssize_t rv;
968
969         mutex_lock(&nfsd_mutex);
970         rv = __nfsd4_write_time(file, buf, size, time, nn);
971         mutex_unlock(&nfsd_mutex);
972         return rv;
973 }
974
975 /*
976  * write_leasetime - Set or report the current NFSv4 lease time
977  *
978  * Input:
979  *                      buf:            ignored
980  *                      size:           zero
981  *
982  * OR
983  *
984  * Input:
985  *                      buf:            C string containing an unsigned
986  *                                      integer value representing the new
987  *                                      NFSv4 lease expiry time
988  *                      size:           non-zero length of C string in @buf
989  * Output:
990  *      On success:     passed-in buffer filled with '\n'-terminated C
991  *                      string containing unsigned integer value of the
992  *                      current lease expiry time;
993  *                      return code is the size in bytes of the string
994  *      On error:       return code is zero or a negative errno value
995  */
996 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
997 {
998         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
999         return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1000 }
1001
1002 /*
1003  * write_gracetime - Set or report current NFSv4 grace period time
1004  *
1005  * As above, but sets the time of the NFSv4 grace period.
1006  *
1007  * Note this should never be set to less than the *previous*
1008  * lease-period time, but we don't try to enforce this.  (In the common
1009  * case (a new boot), we don't know what the previous lease time was
1010  * anyway.)
1011  */
1012 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1013 {
1014         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1015         return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1016 }
1017
1018 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1019 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1020                                    struct nfsd_net *nn)
1021 {
1022         char *mesg = buf;
1023         char *recdir;
1024         int len, status;
1025
1026         if (size > 0) {
1027                 if (nn->nfsd_serv)
1028                         return -EBUSY;
1029                 if (size > PATH_MAX || buf[size-1] != '\n')
1030                         return -EINVAL;
1031                 buf[size-1] = 0;
1032
1033                 recdir = mesg;
1034                 len = qword_get(&mesg, recdir, size);
1035                 if (len <= 0)
1036                         return -EINVAL;
1037                 trace_nfsd_ctl_recoverydir(netns(file), recdir);
1038
1039                 status = nfs4_reset_recoverydir(recdir);
1040                 if (status)
1041                         return status;
1042         }
1043
1044         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1045                                                         nfs4_recoverydir());
1046 }
1047
1048 /*
1049  * write_recoverydir - Set or report the pathname of the recovery directory
1050  *
1051  * Input:
1052  *                      buf:            ignored
1053  *                      size:           zero
1054  *
1055  * OR
1056  *
1057  * Input:
1058  *                      buf:            C string containing the pathname
1059  *                                      of the directory on a local file
1060  *                                      system containing permanent NFSv4
1061  *                                      recovery data
1062  *                      size:           non-zero length of C string in @buf
1063  * Output:
1064  *      On success:     passed-in buffer filled with '\n'-terminated C string
1065  *                      containing the current recovery pathname setting;
1066  *                      return code is the size in bytes of the string
1067  *      On error:       return code is zero or a negative errno value
1068  */
1069 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1070 {
1071         ssize_t rv;
1072         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1073
1074         mutex_lock(&nfsd_mutex);
1075         rv = __write_recoverydir(file, buf, size, nn);
1076         mutex_unlock(&nfsd_mutex);
1077         return rv;
1078 }
1079 #endif
1080
1081 /*
1082  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1083  *
1084  * Input:
1085  *                      buf:            ignored
1086  *                      size:           zero
1087  * OR
1088  *
1089  * Input:
1090  *                      buf:            any value
1091  *                      size:           non-zero length of C string in @buf
1092  * Output:
1093  *                      passed-in buffer filled with "Y" or "N" with a newline
1094  *                      and NULL-terminated C string. This indicates whether
1095  *                      the grace period has ended in the current net
1096  *                      namespace. Return code is the size in bytes of the
1097  *                      string. Writing a string that starts with 'Y', 'y', or
1098  *                      '1' to the file will end the grace period for nfsd's v4
1099  *                      lock manager.
1100  */
1101 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1102 {
1103         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1104
1105         if (size > 0) {
1106                 switch(buf[0]) {
1107                 case 'Y':
1108                 case 'y':
1109                 case '1':
1110                         if (!nn->nfsd_serv)
1111                                 return -EBUSY;
1112                         trace_nfsd_end_grace(netns(file));
1113                         nfsd4_end_grace(nn);
1114                         break;
1115                 default:
1116                         return -EINVAL;
1117                 }
1118         }
1119
1120         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1121                          nn->grace_ended ? 'Y' : 'N');
1122 }
1123
1124 #endif
1125
1126 /*----------------------------------------------------------------------------*/
1127 /*
1128  *      populating the filesystem.
1129  */
1130
1131 /* Basically copying rpc_get_inode. */
1132 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1133 {
1134         struct inode *inode = new_inode(sb);
1135         if (!inode)
1136                 return NULL;
1137         /* Following advice from simple_fill_super documentation: */
1138         inode->i_ino = iunique(sb, NFSD_MaxReserved);
1139         inode->i_mode = mode;
1140         simple_inode_init_ts(inode);
1141         switch (mode & S_IFMT) {
1142         case S_IFDIR:
1143                 inode->i_fop = &simple_dir_operations;
1144                 inode->i_op = &simple_dir_inode_operations;
1145                 inc_nlink(inode);
1146                 break;
1147         case S_IFLNK:
1148                 inode->i_op = &simple_symlink_inode_operations;
1149                 break;
1150         default:
1151                 break;
1152         }
1153         return inode;
1154 }
1155
1156 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1157 {
1158         struct inode *inode;
1159
1160         inode = nfsd_get_inode(dir->i_sb, mode);
1161         if (!inode)
1162                 return -ENOMEM;
1163         if (ncl) {
1164                 inode->i_private = ncl;
1165                 kref_get(&ncl->cl_ref);
1166         }
1167         d_add(dentry, inode);
1168         inc_nlink(dir);
1169         fsnotify_mkdir(dir, dentry);
1170         return 0;
1171 }
1172
1173 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1174 {
1175         struct inode *dir = parent->d_inode;
1176         struct dentry *dentry;
1177         int ret = -ENOMEM;
1178
1179         inode_lock(dir);
1180         dentry = d_alloc_name(parent, name);
1181         if (!dentry)
1182                 goto out_err;
1183         ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1184         if (ret)
1185                 goto out_err;
1186 out:
1187         inode_unlock(dir);
1188         return dentry;
1189 out_err:
1190         dput(dentry);
1191         dentry = ERR_PTR(ret);
1192         goto out;
1193 }
1194
1195 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1196 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1197                           umode_t mode, const char *content)
1198 {
1199         struct inode *inode;
1200
1201         inode = nfsd_get_inode(dir->i_sb, mode);
1202         if (!inode)
1203                 return -ENOMEM;
1204
1205         inode->i_link = (char *)content;
1206         inode->i_size = strlen(content);
1207
1208         d_add(dentry, inode);
1209         inc_nlink(dir);
1210         fsnotify_create(dir, dentry);
1211         return 0;
1212 }
1213
1214 /*
1215  * @content is assumed to be a NUL-terminated string that lives
1216  * longer than the symlink itself.
1217  */
1218 static void _nfsd_symlink(struct dentry *parent, const char *name,
1219                           const char *content)
1220 {
1221         struct inode *dir = parent->d_inode;
1222         struct dentry *dentry;
1223         int ret;
1224
1225         inode_lock(dir);
1226         dentry = d_alloc_name(parent, name);
1227         if (!dentry)
1228                 goto out;
1229         ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1230         if (ret)
1231                 dput(dentry);
1232 out:
1233         inode_unlock(dir);
1234 }
1235 #else
1236 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1237                                  const char *content)
1238 {
1239 }
1240
1241 #endif
1242
1243 static void clear_ncl(struct dentry *dentry)
1244 {
1245         struct inode *inode = d_inode(dentry);
1246         struct nfsdfs_client *ncl = inode->i_private;
1247
1248         spin_lock(&inode->i_lock);
1249         inode->i_private = NULL;
1250         spin_unlock(&inode->i_lock);
1251         kref_put(&ncl->cl_ref, ncl->cl_release);
1252 }
1253
1254 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1255 {
1256         struct nfsdfs_client *nc;
1257
1258         spin_lock(&inode->i_lock);
1259         nc = inode->i_private;
1260         if (nc)
1261                 kref_get(&nc->cl_ref);
1262         spin_unlock(&inode->i_lock);
1263         return nc;
1264 }
1265
1266 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1267  * code instead. */
1268 static  int nfsdfs_create_files(struct dentry *root,
1269                                 const struct tree_descr *files,
1270                                 struct nfsdfs_client *ncl,
1271                                 struct dentry **fdentries)
1272 {
1273         struct inode *dir = d_inode(root);
1274         struct inode *inode;
1275         struct dentry *dentry;
1276         int i;
1277
1278         inode_lock(dir);
1279         for (i = 0; files->name && files->name[0]; i++, files++) {
1280                 dentry = d_alloc_name(root, files->name);
1281                 if (!dentry)
1282                         goto out;
1283                 inode = nfsd_get_inode(d_inode(root)->i_sb,
1284                                         S_IFREG | files->mode);
1285                 if (!inode) {
1286                         dput(dentry);
1287                         goto out;
1288                 }
1289                 kref_get(&ncl->cl_ref);
1290                 inode->i_fop = files->ops;
1291                 inode->i_private = ncl;
1292                 d_add(dentry, inode);
1293                 fsnotify_create(dir, dentry);
1294                 if (fdentries)
1295                         fdentries[i] = dentry;
1296         }
1297         inode_unlock(dir);
1298         return 0;
1299 out:
1300         inode_unlock(dir);
1301         return -ENOMEM;
1302 }
1303
1304 /* on success, returns positive number unique to that client. */
1305 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1306                                  struct nfsdfs_client *ncl, u32 id,
1307                                  const struct tree_descr *files,
1308                                  struct dentry **fdentries)
1309 {
1310         struct dentry *dentry;
1311         char name[11];
1312         int ret;
1313
1314         sprintf(name, "%u", id);
1315
1316         dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1317         if (IS_ERR(dentry)) /* XXX: tossing errors? */
1318                 return NULL;
1319         ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1320         if (ret) {
1321                 nfsd_client_rmdir(dentry);
1322                 return NULL;
1323         }
1324         return dentry;
1325 }
1326
1327 /* Taken from __rpc_rmdir: */
1328 void nfsd_client_rmdir(struct dentry *dentry)
1329 {
1330         simple_recursive_removal(dentry, clear_ncl);
1331 }
1332
1333 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1334 {
1335         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1336                                                         nfsd_net_id);
1337         struct dentry *dentry;
1338         int ret;
1339
1340         static const struct tree_descr nfsd_files[] = {
1341                 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1342                 /* Per-export io stats use same ops as exports file */
1343                 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1344                 [NFSD_Export_features] = {"export_features",
1345                                         &export_features_fops, S_IRUGO},
1346                 [NFSD_FO_UnlockIP] = {"unlock_ip",
1347                                         &transaction_ops, S_IWUSR|S_IRUSR},
1348                 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1349                                         &transaction_ops, S_IWUSR|S_IRUSR},
1350                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1351                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1352                 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1353                 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1354                 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1355                                         &nfsd_reply_cache_stats_fops, S_IRUGO},
1356                 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1357                 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1358                 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1359                 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1360                 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1361 #ifdef CONFIG_NFSD_V4
1362                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1363                 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1364 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1365                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1366 #endif
1367                 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1368 #endif
1369                 /* last one */ {""}
1370         };
1371
1372         ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1373         if (ret)
1374                 return ret;
1375         _nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1376                       "/proc/net/rpc/gss_krb5_enctypes");
1377         dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1378         if (IS_ERR(dentry))
1379                 return PTR_ERR(dentry);
1380         nn->nfsd_client_dir = dentry;
1381         return 0;
1382 }
1383
1384 static int nfsd_fs_get_tree(struct fs_context *fc)
1385 {
1386         return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1387 }
1388
1389 static void nfsd_fs_free_fc(struct fs_context *fc)
1390 {
1391         if (fc->s_fs_info)
1392                 put_net(fc->s_fs_info);
1393 }
1394
1395 static const struct fs_context_operations nfsd_fs_context_ops = {
1396         .free           = nfsd_fs_free_fc,
1397         .get_tree       = nfsd_fs_get_tree,
1398 };
1399
1400 static int nfsd_init_fs_context(struct fs_context *fc)
1401 {
1402         put_user_ns(fc->user_ns);
1403         fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1404         fc->ops = &nfsd_fs_context_ops;
1405         return 0;
1406 }
1407
1408 static void nfsd_umount(struct super_block *sb)
1409 {
1410         struct net *net = sb->s_fs_info;
1411
1412         nfsd_shutdown_threads(net);
1413
1414         kill_litter_super(sb);
1415         put_net(net);
1416 }
1417
1418 static struct file_system_type nfsd_fs_type = {
1419         .owner          = THIS_MODULE,
1420         .name           = "nfsd",
1421         .init_fs_context = nfsd_init_fs_context,
1422         .kill_sb        = nfsd_umount,
1423 };
1424 MODULE_ALIAS_FS("nfsd");
1425
1426 #ifdef CONFIG_PROC_FS
1427
1428 static int exports_proc_open(struct inode *inode, struct file *file)
1429 {
1430         return exports_net_open(current->nsproxy->net_ns, file);
1431 }
1432
1433 static const struct proc_ops exports_proc_ops = {
1434         .proc_open      = exports_proc_open,
1435         .proc_read      = seq_read,
1436         .proc_lseek     = seq_lseek,
1437         .proc_release   = seq_release,
1438 };
1439
1440 static int create_proc_exports_entry(void)
1441 {
1442         struct proc_dir_entry *entry;
1443
1444         entry = proc_mkdir("fs/nfs", NULL);
1445         if (!entry)
1446                 return -ENOMEM;
1447         entry = proc_create("exports", 0, entry, &exports_proc_ops);
1448         if (!entry) {
1449                 remove_proc_entry("fs/nfs", NULL);
1450                 return -ENOMEM;
1451         }
1452         return 0;
1453 }
1454 #else /* CONFIG_PROC_FS */
1455 static int create_proc_exports_entry(void)
1456 {
1457         return 0;
1458 }
1459 #endif
1460
1461 unsigned int nfsd_net_id;
1462
1463 /**
1464  * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit
1465  * @cb: netlink metadata and command arguments
1466  *
1467  * Return values:
1468  *   %0: The rpc_status_get command may proceed
1469  *   %-ENODEV: There is no NFSD running in this namespace
1470  */
1471 int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb)
1472 {
1473         struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id);
1474         int ret = -ENODEV;
1475
1476         mutex_lock(&nfsd_mutex);
1477         if (nn->nfsd_serv)
1478                 ret = 0;
1479         else
1480                 mutex_unlock(&nfsd_mutex);
1481
1482         return ret;
1483 }
1484
1485 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1486                                             struct netlink_callback *cb,
1487                                             struct nfsd_genl_rqstp *rqstp)
1488 {
1489         void *hdr;
1490         u32 i;
1491
1492         hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1493                           &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1494         if (!hdr)
1495                 return -ENOBUFS;
1496
1497         if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1498             nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1499             nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1500             nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1501             nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1502             nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1503                         ktime_to_us(rqstp->rq_stime),
1504                         NFSD_A_RPC_STATUS_PAD))
1505                 return -ENOBUFS;
1506
1507         switch (rqstp->rq_saddr.sa_family) {
1508         case AF_INET: {
1509                 const struct sockaddr_in *s_in, *d_in;
1510
1511                 s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1512                 d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1513                 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1514                                     s_in->sin_addr.s_addr) ||
1515                     nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1516                                     d_in->sin_addr.s_addr) ||
1517                     nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1518                                  s_in->sin_port) ||
1519                     nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1520                                  d_in->sin_port))
1521                         return -ENOBUFS;
1522                 break;
1523         }
1524         case AF_INET6: {
1525                 const struct sockaddr_in6 *s_in, *d_in;
1526
1527                 s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1528                 d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1529                 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1530                                      &s_in->sin6_addr) ||
1531                     nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1532                                      &d_in->sin6_addr) ||
1533                     nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1534                                  s_in->sin6_port) ||
1535                     nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1536                                  d_in->sin6_port))
1537                         return -ENOBUFS;
1538                 break;
1539         }
1540         }
1541
1542         for (i = 0; i < rqstp->rq_opcnt; i++)
1543                 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1544                                 rqstp->rq_opnum[i]))
1545                         return -ENOBUFS;
1546
1547         genlmsg_end(skb, hdr);
1548         return 0;
1549 }
1550
1551 /**
1552  * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1553  * @skb: reply buffer
1554  * @cb: netlink metadata and command arguments
1555  *
1556  * Returns the size of the reply or a negative errno.
1557  */
1558 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1559                                   struct netlink_callback *cb)
1560 {
1561         struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1562         int i, ret, rqstp_index = 0;
1563
1564         rcu_read_lock();
1565
1566         for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1567                 struct svc_rqst *rqstp;
1568
1569                 if (i < cb->args[0]) /* already consumed */
1570                         continue;
1571
1572                 rqstp_index = 0;
1573                 list_for_each_entry_rcu(rqstp,
1574                                 &nn->nfsd_serv->sv_pools[i].sp_all_threads,
1575                                 rq_all) {
1576                         struct nfsd_genl_rqstp genl_rqstp;
1577                         unsigned int status_counter;
1578
1579                         if (rqstp_index++ < cb->args[1]) /* already consumed */
1580                                 continue;
1581                         /*
1582                          * Acquire rq_status_counter before parsing the rqst
1583                          * fields. rq_status_counter is set to an odd value in
1584                          * order to notify the consumers the rqstp fields are
1585                          * meaningful.
1586                          */
1587                         status_counter =
1588                                 smp_load_acquire(&rqstp->rq_status_counter);
1589                         if (!(status_counter & 1))
1590                                 continue;
1591
1592                         genl_rqstp.rq_xid = rqstp->rq_xid;
1593                         genl_rqstp.rq_flags = rqstp->rq_flags;
1594                         genl_rqstp.rq_vers = rqstp->rq_vers;
1595                         genl_rqstp.rq_prog = rqstp->rq_prog;
1596                         genl_rqstp.rq_proc = rqstp->rq_proc;
1597                         genl_rqstp.rq_stime = rqstp->rq_stime;
1598                         genl_rqstp.rq_opcnt = 0;
1599                         memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1600                                sizeof(struct sockaddr));
1601                         memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1602                                sizeof(struct sockaddr));
1603
1604 #ifdef CONFIG_NFSD_V4
1605                         if (rqstp->rq_vers == NFS4_VERSION &&
1606                             rqstp->rq_proc == NFSPROC4_COMPOUND) {
1607                                 /* NFSv4 compound */
1608                                 struct nfsd4_compoundargs *args;
1609                                 int j;
1610
1611                                 args = rqstp->rq_argp;
1612                                 genl_rqstp.rq_opcnt = args->opcnt;
1613                                 for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1614                                         genl_rqstp.rq_opnum[j] =
1615                                                 args->ops[j].opnum;
1616                         }
1617 #endif /* CONFIG_NFSD_V4 */
1618
1619                         /*
1620                          * Acquire rq_status_counter before reporting the rqst
1621                          * fields to the user.
1622                          */
1623                         if (smp_load_acquire(&rqstp->rq_status_counter) !=
1624                             status_counter)
1625                                 continue;
1626
1627                         ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1628                                                                &genl_rqstp);
1629                         if (ret)
1630                                 goto out;
1631                 }
1632         }
1633
1634         cb->args[0] = i;
1635         cb->args[1] = rqstp_index;
1636         ret = skb->len;
1637 out:
1638         rcu_read_unlock();
1639
1640         return ret;
1641 }
1642
1643 /**
1644  * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing
1645  * @cb: netlink metadata and command arguments
1646  *
1647  * Return values:
1648  *   %0: Success
1649  */
1650 int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb)
1651 {
1652         mutex_unlock(&nfsd_mutex);
1653
1654         return 0;
1655 }
1656
1657 /**
1658  * nfsd_nl_threads_set_doit - set the number of running threads
1659  * @skb: reply buffer
1660  * @info: netlink metadata and command arguments
1661  *
1662  * Return 0 on success or a negative errno.
1663  */
1664 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1665 {
1666         int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem;
1667         struct net *net = genl_info_net(info);
1668         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1669         const struct nlattr *attr;
1670         const char *scope = NULL;
1671
1672         if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1673                 return -EINVAL;
1674
1675         /* count number of SERVER_THREADS values */
1676         nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1677                 if (nla_type(attr) == NFSD_A_SERVER_THREADS)
1678                         count++;
1679         }
1680
1681         mutex_lock(&nfsd_mutex);
1682
1683         nrpools = nfsd_nrpools(net);
1684         if (nrpools && count > nrpools)
1685                 count = nrpools;
1686
1687         /* XXX: make this handle non-global pool-modes */
1688         if (count > 1)
1689                 goto out_unlock;
1690
1691         nthreads = nla_get_u32(info->attrs[NFSD_A_SERVER_THREADS]);
1692         if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1693             info->attrs[NFSD_A_SERVER_LEASETIME] ||
1694             info->attrs[NFSD_A_SERVER_SCOPE]) {
1695                 ret = -EBUSY;
1696                 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1697                         goto out_unlock;
1698
1699                 ret = -EINVAL;
1700                 attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1701                 if (attr) {
1702                         u32 gracetime = nla_get_u32(attr);
1703
1704                         if (gracetime < 10 || gracetime > 3600)
1705                                 goto out_unlock;
1706
1707                         nn->nfsd4_grace = gracetime;
1708                 }
1709
1710                 attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1711                 if (attr) {
1712                         u32 leasetime = nla_get_u32(attr);
1713
1714                         if (leasetime < 10 || leasetime > 3600)
1715                                 goto out_unlock;
1716
1717                         nn->nfsd4_lease = leasetime;
1718                 }
1719
1720                 attr = info->attrs[NFSD_A_SERVER_SCOPE];
1721                 if (attr)
1722                         scope = nla_data(attr);
1723         }
1724
1725         ret = nfsd_svc(nthreads, net, get_current_cred(), scope);
1726
1727 out_unlock:
1728         mutex_unlock(&nfsd_mutex);
1729
1730         return ret == nthreads ? 0 : ret;
1731 }
1732
1733 /**
1734  * nfsd_nl_threads_get_doit - get the number of running threads
1735  * @skb: reply buffer
1736  * @info: netlink metadata and command arguments
1737  *
1738  * Return 0 on success or a negative errno.
1739  */
1740 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1741 {
1742         struct net *net = genl_info_net(info);
1743         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1744         void *hdr;
1745         int err;
1746
1747         skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1748         if (!skb)
1749                 return -ENOMEM;
1750
1751         hdr = genlmsg_iput(skb, info);
1752         if (!hdr) {
1753                 err = -EMSGSIZE;
1754                 goto err_free_msg;
1755         }
1756
1757         mutex_lock(&nfsd_mutex);
1758
1759         err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1760                           nn->nfsd4_grace) ||
1761               nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1762                           nn->nfsd4_lease) ||
1763               nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1764                           nn->nfsd_name);
1765         if (err)
1766                 goto err_unlock;
1767
1768         if (nn->nfsd_serv) {
1769                 int i;
1770
1771                 for (i = 0; i < nfsd_nrpools(net); ++i) {
1772                         struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1773
1774                         err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1775                                           atomic_read(&sp->sp_nrthreads));
1776                         if (err)
1777                                 goto err_unlock;
1778                 }
1779         } else {
1780                 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1781                 if (err)
1782                         goto err_unlock;
1783         }
1784
1785         mutex_unlock(&nfsd_mutex);
1786
1787         genlmsg_end(skb, hdr);
1788
1789         return genlmsg_reply(skb, info);
1790
1791 err_unlock:
1792         mutex_unlock(&nfsd_mutex);
1793 err_free_msg:
1794         nlmsg_free(skb);
1795
1796         return err;
1797 }
1798
1799 /**
1800  * nfsd_nl_version_set_doit - set the nfs enabled versions
1801  * @skb: reply buffer
1802  * @info: netlink metadata and command arguments
1803  *
1804  * Return 0 on success or a negative errno.
1805  */
1806 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1807 {
1808         const struct nlattr *attr;
1809         struct nfsd_net *nn;
1810         int i, rem;
1811
1812         if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1813                 return -EINVAL;
1814
1815         mutex_lock(&nfsd_mutex);
1816
1817         nn = net_generic(genl_info_net(info), nfsd_net_id);
1818         if (nn->nfsd_serv) {
1819                 mutex_unlock(&nfsd_mutex);
1820                 return -EBUSY;
1821         }
1822
1823         /* clear current supported versions. */
1824         nfsd_vers(nn, 2, NFSD_CLEAR);
1825         nfsd_vers(nn, 3, NFSD_CLEAR);
1826         for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1827                 nfsd_minorversion(nn, i, NFSD_CLEAR);
1828
1829         nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1830                 struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1831                 u32 major, minor = 0;
1832                 bool enabled;
1833
1834                 if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION)
1835                         continue;
1836
1837                 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1838                                      nfsd_version_nl_policy, info->extack) < 0)
1839                         continue;
1840
1841                 if (!tb[NFSD_A_VERSION_MAJOR])
1842                         continue;
1843
1844                 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1845                 if (tb[NFSD_A_VERSION_MINOR])
1846                         minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1847
1848                 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1849
1850                 switch (major) {
1851                 case 4:
1852                         nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1853                         break;
1854                 case 3:
1855                 case 2:
1856                         if (!minor)
1857                                 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1858                         break;
1859                 default:
1860                         break;
1861                 }
1862         }
1863
1864         mutex_unlock(&nfsd_mutex);
1865
1866         return 0;
1867 }
1868
1869 /**
1870  * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1871  * @skb: reply buffer
1872  * @info: netlink metadata and command arguments
1873  *
1874  * Return 0 on success or a negative errno.
1875  */
1876 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1877 {
1878         struct nfsd_net *nn;
1879         int i, err;
1880         void *hdr;
1881
1882         skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1883         if (!skb)
1884                 return -ENOMEM;
1885
1886         hdr = genlmsg_iput(skb, info);
1887         if (!hdr) {
1888                 err = -EMSGSIZE;
1889                 goto err_free_msg;
1890         }
1891
1892         mutex_lock(&nfsd_mutex);
1893         nn = net_generic(genl_info_net(info), nfsd_net_id);
1894
1895         for (i = 2; i <= 4; i++) {
1896                 int j;
1897
1898                 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1899                         struct nlattr *attr;
1900
1901                         /* Don't record any versions the kernel doesn't have
1902                          * compiled in
1903                          */
1904                         if (!nfsd_support_version(i))
1905                                 continue;
1906
1907                         /* NFSv{2,3} does not support minor numbers */
1908                         if (i < 4 && j)
1909                                 continue;
1910
1911                         attr = nla_nest_start(skb,
1912                                               NFSD_A_SERVER_PROTO_VERSION);
1913                         if (!attr) {
1914                                 err = -EINVAL;
1915                                 goto err_nfsd_unlock;
1916                         }
1917
1918                         if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1919                             nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1920                                 err = -EINVAL;
1921                                 goto err_nfsd_unlock;
1922                         }
1923
1924                         /* Set the enabled flag if the version is enabled */
1925                         if (nfsd_vers(nn, i, NFSD_TEST) &&
1926                             (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1927                             nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1928                                 err = -EINVAL;
1929                                 goto err_nfsd_unlock;
1930                         }
1931
1932                         nla_nest_end(skb, attr);
1933                 }
1934         }
1935
1936         mutex_unlock(&nfsd_mutex);
1937         genlmsg_end(skb, hdr);
1938
1939         return genlmsg_reply(skb, info);
1940
1941 err_nfsd_unlock:
1942         mutex_unlock(&nfsd_mutex);
1943 err_free_msg:
1944         nlmsg_free(skb);
1945
1946         return err;
1947 }
1948
1949 /**
1950  * nfsd_nl_listener_set_doit - set the nfs running sockets
1951  * @skb: reply buffer
1952  * @info: netlink metadata and command arguments
1953  *
1954  * Return 0 on success or a negative errno.
1955  */
1956 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1957 {
1958         struct net *net = genl_info_net(info);
1959         struct svc_xprt *xprt, *tmp;
1960         const struct nlattr *attr;
1961         struct svc_serv *serv;
1962         LIST_HEAD(permsocks);
1963         struct nfsd_net *nn;
1964         int err, rem;
1965
1966         mutex_lock(&nfsd_mutex);
1967
1968         err = nfsd_create_serv(net);
1969         if (err) {
1970                 mutex_unlock(&nfsd_mutex);
1971                 return err;
1972         }
1973
1974         nn = net_generic(net, nfsd_net_id);
1975         serv = nn->nfsd_serv;
1976
1977         spin_lock_bh(&serv->sv_lock);
1978
1979         /* Move all of the old listener sockets to a temp list */
1980         list_splice_init(&serv->sv_permsocks, &permsocks);
1981
1982         /*
1983          * Walk the list of server_socks from userland and move any that match
1984          * back to sv_permsocks
1985          */
1986         nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1987                 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1988                 const char *xcl_name;
1989                 struct sockaddr *sa;
1990
1991                 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
1992                         continue;
1993
1994                 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1995                                      nfsd_sock_nl_policy, info->extack) < 0)
1996                         continue;
1997
1998                 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
1999                         continue;
2000
2001                 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2002                         continue;
2003
2004                 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2005                 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2006
2007                 /* Put back any matching sockets */
2008                 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
2009                         /* This shouldn't be possible */
2010                         if (WARN_ON_ONCE(xprt->xpt_net != net)) {
2011                                 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2012                                 continue;
2013                         }
2014
2015                         /* If everything matches, put it back */
2016                         if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
2017                             rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
2018                                 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2019                                 break;
2020                         }
2021                 }
2022         }
2023
2024         /* For now, no removing old sockets while server is running */
2025         if (serv->sv_nrthreads && !list_empty(&permsocks)) {
2026                 list_splice_init(&permsocks, &serv->sv_permsocks);
2027                 spin_unlock_bh(&serv->sv_lock);
2028                 err = -EBUSY;
2029                 goto out_unlock_mtx;
2030         }
2031
2032         /* Close the remaining sockets on the permsocks list */
2033         while (!list_empty(&permsocks)) {
2034                 xprt = list_first_entry(&permsocks, struct svc_xprt, xpt_list);
2035                 list_move(&xprt->xpt_list, &serv->sv_permsocks);
2036
2037                 /*
2038                  * Newly-created sockets are born with the BUSY bit set. Clear
2039                  * it if there are no threads, since nothing can pick it up
2040                  * in that case.
2041                  */
2042                 if (!serv->sv_nrthreads)
2043                         clear_bit(XPT_BUSY, &xprt->xpt_flags);
2044
2045                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
2046                 spin_unlock_bh(&serv->sv_lock);
2047                 svc_xprt_close(xprt);
2048                 spin_lock_bh(&serv->sv_lock);
2049         }
2050
2051         spin_unlock_bh(&serv->sv_lock);
2052
2053         /* walk list of addrs again, open any that still don't exist */
2054         nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
2055                 struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2056                 const char *xcl_name;
2057                 struct sockaddr *sa;
2058                 int ret;
2059
2060                 if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
2061                         continue;
2062
2063                 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2064                                      nfsd_sock_nl_policy, info->extack) < 0)
2065                         continue;
2066
2067                 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2068                         continue;
2069
2070                 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2071                         continue;
2072
2073                 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2074                 sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2075
2076                 xprt = svc_find_listener(serv, xcl_name, net, sa);
2077                 if (xprt) {
2078                         svc_xprt_put(xprt);
2079                         continue;
2080                 }
2081
2082                 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa,
2083                                               SVC_SOCK_ANONYMOUS,
2084                                               get_current_cred());
2085                 /* always save the latest error */
2086                 if (ret < 0)
2087                         err = ret;
2088         }
2089
2090         if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2091                 nfsd_destroy_serv(net);
2092
2093 out_unlock_mtx:
2094         mutex_unlock(&nfsd_mutex);
2095
2096         return err;
2097 }
2098
2099 /**
2100  * nfsd_nl_listener_get_doit - get the nfs running listeners
2101  * @skb: reply buffer
2102  * @info: netlink metadata and command arguments
2103  *
2104  * Return 0 on success or a negative errno.
2105  */
2106 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2107 {
2108         struct svc_xprt *xprt;
2109         struct svc_serv *serv;
2110         struct nfsd_net *nn;
2111         void *hdr;
2112         int err;
2113
2114         skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2115         if (!skb)
2116                 return -ENOMEM;
2117
2118         hdr = genlmsg_iput(skb, info);
2119         if (!hdr) {
2120                 err = -EMSGSIZE;
2121                 goto err_free_msg;
2122         }
2123
2124         mutex_lock(&nfsd_mutex);
2125         nn = net_generic(genl_info_net(info), nfsd_net_id);
2126
2127         /* no nfs server? Just send empty socket list */
2128         if (!nn->nfsd_serv)
2129                 goto out_unlock_mtx;
2130
2131         serv = nn->nfsd_serv;
2132         spin_lock_bh(&serv->sv_lock);
2133         list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2134                 struct nlattr *attr;
2135
2136                 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2137                 if (!attr) {
2138                         err = -EINVAL;
2139                         goto err_serv_unlock;
2140                 }
2141
2142                 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2143                                    xprt->xpt_class->xcl_name) ||
2144                     nla_put(skb, NFSD_A_SOCK_ADDR,
2145                             sizeof(struct sockaddr_storage),
2146                             &xprt->xpt_local)) {
2147                         err = -EINVAL;
2148                         goto err_serv_unlock;
2149                 }
2150
2151                 nla_nest_end(skb, attr);
2152         }
2153         spin_unlock_bh(&serv->sv_lock);
2154 out_unlock_mtx:
2155         mutex_unlock(&nfsd_mutex);
2156         genlmsg_end(skb, hdr);
2157
2158         return genlmsg_reply(skb, info);
2159
2160 err_serv_unlock:
2161         spin_unlock_bh(&serv->sv_lock);
2162         mutex_unlock(&nfsd_mutex);
2163 err_free_msg:
2164         nlmsg_free(skb);
2165
2166         return err;
2167 }
2168
2169 /**
2170  * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2171  * @net: a freshly-created network namespace
2172  *
2173  * This information stays around as long as the network namespace is
2174  * alive whether or not there is an NFSD instance running in the
2175  * namespace.
2176  *
2177  * Returns zero on success, or a negative errno otherwise.
2178  */
2179 static __net_init int nfsd_net_init(struct net *net)
2180 {
2181         int retval;
2182         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2183
2184         retval = nfsd_export_init(net);
2185         if (retval)
2186                 goto out_export_error;
2187         retval = nfsd_idmap_init(net);
2188         if (retval)
2189                 goto out_idmap_error;
2190         retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2191                                           NFSD_STATS_COUNTERS_NUM);
2192         if (retval)
2193                 goto out_repcache_error;
2194         memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2195         nn->nfsd_svcstats.program = &nfsd_program;
2196         nn->nfsd_versions = NULL;
2197         nn->nfsd4_minorversions = NULL;
2198         nfsd4_init_leases_net(nn);
2199         get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2200         seqlock_init(&nn->writeverf_lock);
2201         nfsd_proc_stat_init(net);
2202
2203         return 0;
2204
2205 out_repcache_error:
2206         nfsd_idmap_shutdown(net);
2207 out_idmap_error:
2208         nfsd_export_shutdown(net);
2209 out_export_error:
2210         return retval;
2211 }
2212
2213 /**
2214  * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2215  * @net: a network namespace that is about to be destroyed
2216  *
2217  */
2218 static __net_exit void nfsd_net_exit(struct net *net)
2219 {
2220         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2221
2222         nfsd_proc_stat_shutdown(net);
2223         percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2224         nfsd_idmap_shutdown(net);
2225         nfsd_export_shutdown(net);
2226         nfsd_netns_free_versions(nn);
2227 }
2228
2229 static struct pernet_operations nfsd_net_ops = {
2230         .init = nfsd_net_init,
2231         .exit = nfsd_net_exit,
2232         .id   = &nfsd_net_id,
2233         .size = sizeof(struct nfsd_net),
2234 };
2235
2236 static int __init init_nfsd(void)
2237 {
2238         int retval;
2239
2240         retval = nfsd4_init_slabs();
2241         if (retval)
2242                 return retval;
2243         retval = nfsd4_init_pnfs();
2244         if (retval)
2245                 goto out_free_slabs;
2246         retval = nfsd_drc_slab_create();
2247         if (retval)
2248                 goto out_free_pnfs;
2249         nfsd_lockd_init();      /* lockd->nfsd callbacks */
2250         retval = create_proc_exports_entry();
2251         if (retval)
2252                 goto out_free_lockd;
2253         retval = register_pernet_subsys(&nfsd_net_ops);
2254         if (retval < 0)
2255                 goto out_free_exports;
2256         retval = register_cld_notifier();
2257         if (retval)
2258                 goto out_free_subsys;
2259         retval = nfsd4_create_laundry_wq();
2260         if (retval)
2261                 goto out_free_cld;
2262         retval = register_filesystem(&nfsd_fs_type);
2263         if (retval)
2264                 goto out_free_all;
2265         retval = genl_register_family(&nfsd_nl_family);
2266         if (retval)
2267                 goto out_free_all;
2268
2269         return 0;
2270 out_free_all:
2271         nfsd4_destroy_laundry_wq();
2272 out_free_cld:
2273         unregister_cld_notifier();
2274 out_free_subsys:
2275         unregister_pernet_subsys(&nfsd_net_ops);
2276 out_free_exports:
2277         remove_proc_entry("fs/nfs/exports", NULL);
2278         remove_proc_entry("fs/nfs", NULL);
2279 out_free_lockd:
2280         nfsd_lockd_shutdown();
2281         nfsd_drc_slab_free();
2282 out_free_pnfs:
2283         nfsd4_exit_pnfs();
2284 out_free_slabs:
2285         nfsd4_free_slabs();
2286         return retval;
2287 }
2288
2289 static void __exit exit_nfsd(void)
2290 {
2291         genl_unregister_family(&nfsd_nl_family);
2292         unregister_filesystem(&nfsd_fs_type);
2293         nfsd4_destroy_laundry_wq();
2294         unregister_cld_notifier();
2295         unregister_pernet_subsys(&nfsd_net_ops);
2296         nfsd_drc_slab_free();
2297         remove_proc_entry("fs/nfs/exports", NULL);
2298         remove_proc_entry("fs/nfs", NULL);
2299         nfsd_lockd_shutdown();
2300         nfsd4_free_slabs();
2301         nfsd4_exit_pnfs();
2302 }
2303
2304 MODULE_AUTHOR("Olaf Kirch <[email protected]>");
2305 MODULE_DESCRIPTION("In-kernel NFS server");
2306 MODULE_LICENSE("GPL");
2307 module_init(init_nfsd)
2308 module_exit(exit_nfsd)
This page took 0.170046 seconds and 4 git commands to generate.