GNU Linux-libre 6.1.86-gnu
[releases.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 <okir@monad.swb.de>
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/gss_krb5_enctypes.h>
18 #include <linux/sunrpc/rpc_pipe_fs.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
30 /*
31  *      We have a single directory with several nodes in it.
32  */
33 enum {
34         NFSD_Root = 1,
35         NFSD_List,
36         NFSD_Export_Stats,
37         NFSD_Export_features,
38         NFSD_Fh,
39         NFSD_FO_UnlockIP,
40         NFSD_FO_UnlockFS,
41         NFSD_Threads,
42         NFSD_Pool_Threads,
43         NFSD_Pool_Stats,
44         NFSD_Reply_Cache_Stats,
45         NFSD_Versions,
46         NFSD_Ports,
47         NFSD_MaxBlkSize,
48         NFSD_MaxConnections,
49         NFSD_Filecache,
50         NFSD_SupportedEnctypes,
51         /*
52          * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
53          * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
54          */
55 #ifdef CONFIG_NFSD_V4
56         NFSD_Leasetime,
57         NFSD_Gracetime,
58         NFSD_RecoveryDir,
59         NFSD_V4EndGrace,
60 #endif
61         NFSD_MaxReserved
62 };
63
64 /*
65  * write() for these nodes.
66  */
67 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
68 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
69 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
70 static ssize_t write_threads(struct file *file, char *buf, size_t size);
71 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
72 static ssize_t write_versions(struct file *file, char *buf, size_t size);
73 static ssize_t write_ports(struct file *file, char *buf, size_t size);
74 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
75 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
76 #ifdef CONFIG_NFSD_V4
77 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
78 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
79 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
80 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
81 #endif
82
83 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
84         [NFSD_Fh] = write_filehandle,
85         [NFSD_FO_UnlockIP] = write_unlock_ip,
86         [NFSD_FO_UnlockFS] = write_unlock_fs,
87         [NFSD_Threads] = write_threads,
88         [NFSD_Pool_Threads] = write_pool_threads,
89         [NFSD_Versions] = write_versions,
90         [NFSD_Ports] = write_ports,
91         [NFSD_MaxBlkSize] = write_maxblksize,
92         [NFSD_MaxConnections] = write_maxconn,
93 #ifdef CONFIG_NFSD_V4
94         [NFSD_Leasetime] = write_leasetime,
95         [NFSD_Gracetime] = write_gracetime,
96         [NFSD_RecoveryDir] = write_recoverydir,
97         [NFSD_V4EndGrace] = write_v4_end_grace,
98 #endif
99 };
100
101 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
102 {
103         ino_t ino =  file_inode(file)->i_ino;
104         char *data;
105         ssize_t rv;
106
107         if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
108                 return -EINVAL;
109
110         data = simple_transaction_get(file, buf, size);
111         if (IS_ERR(data))
112                 return PTR_ERR(data);
113
114         rv =  write_op[ino](file, data, size);
115         if (rv >= 0) {
116                 simple_transaction_set(file, rv);
117                 rv = size;
118         }
119         return rv;
120 }
121
122 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
123 {
124         if (! file->private_data) {
125                 /* An attempt to read a transaction file without writing
126                  * causes a 0-byte write so that the file can return
127                  * state information
128                  */
129                 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
130                 if (rv < 0)
131                         return rv;
132         }
133         return simple_transaction_read(file, buf, size, pos);
134 }
135
136 static const struct file_operations transaction_ops = {
137         .write          = nfsctl_transaction_write,
138         .read           = nfsctl_transaction_read,
139         .release        = simple_transaction_release,
140         .llseek         = default_llseek,
141 };
142
143 static int exports_net_open(struct net *net, struct file *file)
144 {
145         int err;
146         struct seq_file *seq;
147         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
148
149         err = seq_open(file, &nfs_exports_op);
150         if (err)
151                 return err;
152
153         seq = file->private_data;
154         seq->private = nn->svc_export_cache;
155         return 0;
156 }
157
158 static int exports_proc_open(struct inode *inode, struct file *file)
159 {
160         return exports_net_open(current->nsproxy->net_ns, file);
161 }
162
163 static const struct proc_ops exports_proc_ops = {
164         .proc_open      = exports_proc_open,
165         .proc_read      = seq_read,
166         .proc_lseek     = seq_lseek,
167         .proc_release   = seq_release,
168 };
169
170 static int exports_nfsd_open(struct inode *inode, struct file *file)
171 {
172         return exports_net_open(inode->i_sb->s_fs_info, file);
173 }
174
175 static const struct file_operations exports_nfsd_operations = {
176         .open           = exports_nfsd_open,
177         .read           = seq_read,
178         .llseek         = seq_lseek,
179         .release        = seq_release,
180 };
181
182 static int export_features_show(struct seq_file *m, void *v)
183 {
184         seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
185         return 0;
186 }
187
188 DEFINE_SHOW_ATTRIBUTE(export_features);
189
190 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
191 static int supported_enctypes_show(struct seq_file *m, void *v)
192 {
193         seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
194         return 0;
195 }
196
197 DEFINE_SHOW_ATTRIBUTE(supported_enctypes);
198 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
199
200 static const struct file_operations pool_stats_operations = {
201         .open           = nfsd_pool_stats_open,
202         .read           = seq_read,
203         .llseek         = seq_lseek,
204         .release        = nfsd_pool_stats_release,
205 };
206
207 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
208
209 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
210
211 /*----------------------------------------------------------------------------*/
212 /*
213  * payload - write methods
214  */
215
216 static inline struct net *netns(struct file *file)
217 {
218         return file_inode(file)->i_sb->s_fs_info;
219 }
220
221 /*
222  * write_unlock_ip - Release all locks used by a client
223  *
224  * Experimental.
225  *
226  * Input:
227  *                      buf:    '\n'-terminated C string containing a
228  *                              presentation format IP address
229  *                      size:   length of C string in @buf
230  * Output:
231  *      On success:     returns zero if all specified locks were released;
232  *                      returns one if one or more locks were not released
233  *      On error:       return code is negative errno value
234  */
235 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
236 {
237         struct sockaddr_storage address;
238         struct sockaddr *sap = (struct sockaddr *)&address;
239         size_t salen = sizeof(address);
240         char *fo_path;
241         struct net *net = netns(file);
242
243         /* sanity check */
244         if (size == 0)
245                 return -EINVAL;
246
247         if (buf[size-1] != '\n')
248                 return -EINVAL;
249
250         fo_path = buf;
251         if (qword_get(&buf, fo_path, size) < 0)
252                 return -EINVAL;
253
254         if (rpc_pton(net, fo_path, size, sap, salen) == 0)
255                 return -EINVAL;
256
257         return nlmsvc_unlock_all_by_ip(sap);
258 }
259
260 /*
261  * write_unlock_fs - Release all locks on a local file system
262  *
263  * Experimental.
264  *
265  * Input:
266  *                      buf:    '\n'-terminated C string containing the
267  *                              absolute pathname of a local file system
268  *                      size:   length of C string in @buf
269  * Output:
270  *      On success:     returns zero if all specified locks were released;
271  *                      returns one if one or more locks were not released
272  *      On error:       return code is negative errno value
273  */
274 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
275 {
276         struct path path;
277         char *fo_path;
278         int error;
279
280         /* sanity check */
281         if (size == 0)
282                 return -EINVAL;
283
284         if (buf[size-1] != '\n')
285                 return -EINVAL;
286
287         fo_path = buf;
288         if (qword_get(&buf, fo_path, size) < 0)
289                 return -EINVAL;
290
291         error = kern_path(fo_path, 0, &path);
292         if (error)
293                 return error;
294
295         /*
296          * XXX: Needs better sanity checking.  Otherwise we could end up
297          * releasing locks on the wrong file system.
298          *
299          * For example:
300          * 1.  Does the path refer to a directory?
301          * 2.  Is that directory a mount point, or
302          * 3.  Is that directory the root of an exported file system?
303          */
304         error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
305
306         path_put(&path);
307         return error;
308 }
309
310 /*
311  * write_filehandle - Get a variable-length NFS file handle by path
312  *
313  * On input, the buffer contains a '\n'-terminated C string comprised of
314  * three alphanumeric words separated by whitespace.  The string may
315  * contain escape sequences.
316  *
317  * Input:
318  *                      buf:
319  *                              domain:         client domain name
320  *                              path:           export pathname
321  *                              maxsize:        numeric maximum size of
322  *                                              @buf
323  *                      size:   length of C string in @buf
324  * Output:
325  *      On success:     passed-in buffer filled with '\n'-terminated C
326  *                      string containing a ASCII hex text version
327  *                      of the NFS file handle;
328  *                      return code is the size in bytes of the string
329  *      On error:       return code is negative errno value
330  */
331 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
332 {
333         char *dname, *path;
334         int maxsize;
335         char *mesg = buf;
336         int len;
337         struct auth_domain *dom;
338         struct knfsd_fh fh;
339
340         if (size == 0)
341                 return -EINVAL;
342
343         if (buf[size-1] != '\n')
344                 return -EINVAL;
345         buf[size-1] = 0;
346
347         dname = mesg;
348         len = qword_get(&mesg, dname, size);
349         if (len <= 0)
350                 return -EINVAL;
351         
352         path = dname+len+1;
353         len = qword_get(&mesg, path, size);
354         if (len <= 0)
355                 return -EINVAL;
356
357         len = get_int(&mesg, &maxsize);
358         if (len)
359                 return len;
360
361         if (maxsize < NFS_FHSIZE)
362                 return -EINVAL;
363         maxsize = min(maxsize, NFS3_FHSIZE);
364
365         if (qword_get(&mesg, mesg, size)>0)
366                 return -EINVAL;
367
368         /* we have all the words, they are in buf.. */
369         dom = unix_domain_find(dname);
370         if (!dom)
371                 return -ENOMEM;
372
373         len = exp_rootfh(netns(file), dom, path, &fh,  maxsize);
374         auth_domain_put(dom);
375         if (len)
376                 return len;
377
378         mesg = buf;
379         len = SIMPLE_TRANSACTION_LIMIT;
380         qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
381         mesg[-1] = '\n';
382         return mesg - buf;
383 }
384
385 /*
386  * write_threads - Start NFSD, or report the current number of running threads
387  *
388  * Input:
389  *                      buf:            ignored
390  *                      size:           zero
391  * Output:
392  *      On success:     passed-in buffer filled with '\n'-terminated C
393  *                      string numeric value representing the number of
394  *                      running NFSD threads;
395  *                      return code is the size in bytes of the string
396  *      On error:       return code is zero
397  *
398  * OR
399  *
400  * Input:
401  *                      buf:            C string containing an unsigned
402  *                                      integer value representing the
403  *                                      number of NFSD threads to start
404  *                      size:           non-zero length of C string in @buf
405  * Output:
406  *      On success:     NFS service is started;
407  *                      passed-in buffer filled with '\n'-terminated C
408  *                      string numeric value representing the number of
409  *                      running NFSD threads;
410  *                      return code is the size in bytes of the string
411  *      On error:       return code is zero or a negative errno value
412  */
413 static ssize_t write_threads(struct file *file, char *buf, size_t size)
414 {
415         char *mesg = buf;
416         int rv;
417         struct net *net = netns(file);
418
419         if (size > 0) {
420                 int newthreads;
421                 rv = get_int(&mesg, &newthreads);
422                 if (rv)
423                         return rv;
424                 if (newthreads < 0)
425                         return -EINVAL;
426                 rv = nfsd_svc(newthreads, net, file->f_cred);
427                 if (rv < 0)
428                         return rv;
429         } else
430                 rv = nfsd_nrthreads(net);
431
432         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
433 }
434
435 /*
436  * write_pool_threads - Set or report the current number of threads per pool
437  *
438  * Input:
439  *                      buf:            ignored
440  *                      size:           zero
441  *
442  * OR
443  *
444  * Input:
445  *                      buf:            C string containing whitespace-
446  *                                      separated unsigned integer values
447  *                                      representing the number of NFSD
448  *                                      threads to start in each pool
449  *                      size:           non-zero length of C string in @buf
450  * Output:
451  *      On success:     passed-in buffer filled with '\n'-terminated C
452  *                      string containing integer values representing the
453  *                      number of NFSD threads in each pool;
454  *                      return code is the size in bytes of the string
455  *      On error:       return code is zero or a negative errno value
456  */
457 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
458 {
459         /* if size > 0, look for an array of number of threads per node
460          * and apply them  then write out number of threads per node as reply
461          */
462         char *mesg = buf;
463         int i;
464         int rv;
465         int len;
466         int npools;
467         int *nthreads;
468         struct net *net = netns(file);
469
470         mutex_lock(&nfsd_mutex);
471         npools = nfsd_nrpools(net);
472         if (npools == 0) {
473                 /*
474                  * NFS is shut down.  The admin can start it by
475                  * writing to the threads file but NOT the pool_threads
476                  * file, sorry.  Report zero threads.
477                  */
478                 mutex_unlock(&nfsd_mutex);
479                 strcpy(buf, "0\n");
480                 return strlen(buf);
481         }
482
483         nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
484         rv = -ENOMEM;
485         if (nthreads == NULL)
486                 goto out_free;
487
488         if (size > 0) {
489                 for (i = 0; i < npools; i++) {
490                         rv = get_int(&mesg, &nthreads[i]);
491                         if (rv == -ENOENT)
492                                 break;          /* fewer numbers than pools */
493                         if (rv)
494                                 goto out_free;  /* syntax error */
495                         rv = -EINVAL;
496                         if (nthreads[i] < 0)
497                                 goto out_free;
498                 }
499                 rv = nfsd_set_nrthreads(i, nthreads, net);
500                 if (rv)
501                         goto out_free;
502         }
503
504         rv = nfsd_get_nrthreads(npools, nthreads, net);
505         if (rv)
506                 goto out_free;
507
508         mesg = buf;
509         size = SIMPLE_TRANSACTION_LIMIT;
510         for (i = 0; i < npools && size > 0; i++) {
511                 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
512                 len = strlen(mesg);
513                 size -= len;
514                 mesg += len;
515         }
516         rv = mesg - buf;
517 out_free:
518         kfree(nthreads);
519         mutex_unlock(&nfsd_mutex);
520         return rv;
521 }
522
523 static ssize_t
524 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
525                 const char *sep, unsigned vers, int minor)
526 {
527         const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
528         bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
529
530         if (vers == 4 && minor >= 0 &&
531             !nfsd_minorversion(nn, minor, NFSD_TEST))
532                 supported = false;
533         if (minor == 0 && supported)
534                 /*
535                  * special case for backward compatability.
536                  * +4.0 is never reported, it is implied by
537                  * +4, unless -4.0 is present.
538                  */
539                 return 0;
540         return snprintf(buf, remaining, format, sep,
541                         supported ? '+' : '-', vers, minor);
542 }
543
544 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
545 {
546         char *mesg = buf;
547         char *vers, *minorp, sign;
548         int len, num, remaining;
549         ssize_t tlen = 0;
550         char *sep;
551         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
552
553         if (size>0) {
554                 if (nn->nfsd_serv)
555                         /* Cannot change versions without updating
556                          * nn->nfsd_serv->sv_xdrsize, and reallocing
557                          * rq_argp and rq_resp
558                          */
559                         return -EBUSY;
560                 if (buf[size-1] != '\n')
561                         return -EINVAL;
562                 buf[size-1] = 0;
563
564                 vers = mesg;
565                 len = qword_get(&mesg, vers, size);
566                 if (len <= 0) return -EINVAL;
567                 do {
568                         enum vers_op cmd;
569                         unsigned minor;
570                         sign = *vers;
571                         if (sign == '+' || sign == '-')
572                                 num = simple_strtol((vers+1), &minorp, 0);
573                         else
574                                 num = simple_strtol(vers, &minorp, 0);
575                         if (*minorp == '.') {
576                                 if (num != 4)
577                                         return -EINVAL;
578                                 if (kstrtouint(minorp+1, 0, &minor) < 0)
579                                         return -EINVAL;
580                         }
581
582                         cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
583                         switch(num) {
584 #ifdef CONFIG_NFSD_V2
585                         case 2:
586 #endif
587                         case 3:
588                                 nfsd_vers(nn, num, cmd);
589                                 break;
590                         case 4:
591                                 if (*minorp == '.') {
592                                         if (nfsd_minorversion(nn, minor, cmd) < 0)
593                                                 return -EINVAL;
594                                 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
595                                         /*
596                                          * Either we have +4 and no minors are enabled,
597                                          * or we have -4 and at least one minor is enabled.
598                                          * In either case, propagate 'cmd' to all minors.
599                                          */
600                                         minor = 0;
601                                         while (nfsd_minorversion(nn, minor, cmd) >= 0)
602                                                 minor++;
603                                 }
604                                 break;
605                         default:
606                                 /* Ignore requests to disable non-existent versions */
607                                 if (cmd == NFSD_SET)
608                                         return -EINVAL;
609                         }
610                         vers += len + 1;
611                 } while ((len = qword_get(&mesg, vers, size)) > 0);
612                 /* If all get turned off, turn them back on, as
613                  * having no versions is BAD
614                  */
615                 nfsd_reset_versions(nn);
616         }
617
618         /* Now write current state into reply buffer */
619         sep = "";
620         remaining = SIMPLE_TRANSACTION_LIMIT;
621         for (num=2 ; num <= 4 ; num++) {
622                 int minor;
623                 if (!nfsd_vers(nn, num, NFSD_AVAIL))
624                         continue;
625
626                 minor = -1;
627                 do {
628                         len = nfsd_print_version_support(nn, buf, remaining,
629                                         sep, num, minor);
630                         if (len >= remaining)
631                                 goto out;
632                         remaining -= len;
633                         buf += len;
634                         tlen += len;
635                         minor++;
636                         if (len)
637                                 sep = " ";
638                 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
639         }
640 out:
641         len = snprintf(buf, remaining, "\n");
642         if (len >= remaining)
643                 return -EINVAL;
644         return tlen + len;
645 }
646
647 /*
648  * write_versions - Set or report the available NFS protocol versions
649  *
650  * Input:
651  *                      buf:            ignored
652  *                      size:           zero
653  * Output:
654  *      On success:     passed-in buffer filled with '\n'-terminated C
655  *                      string containing positive or negative integer
656  *                      values representing the current status of each
657  *                      protocol version;
658  *                      return code is the size in bytes of the string
659  *      On error:       return code is zero or a negative errno value
660  *
661  * OR
662  *
663  * Input:
664  *                      buf:            C string containing whitespace-
665  *                                      separated positive or negative
666  *                                      integer values representing NFS
667  *                                      protocol versions to enable ("+n")
668  *                                      or disable ("-n")
669  *                      size:           non-zero length of C string in @buf
670  * Output:
671  *      On success:     status of zero or more protocol versions has
672  *                      been updated; passed-in buffer filled with
673  *                      '\n'-terminated C string containing positive
674  *                      or negative integer values representing the
675  *                      current status of each protocol version;
676  *                      return code is the size in bytes of the string
677  *      On error:       return code is zero or a negative errno value
678  */
679 static ssize_t write_versions(struct file *file, char *buf, size_t size)
680 {
681         ssize_t rv;
682
683         mutex_lock(&nfsd_mutex);
684         rv = __write_versions(file, buf, size);
685         mutex_unlock(&nfsd_mutex);
686         return rv;
687 }
688
689 /*
690  * Zero-length write.  Return a list of NFSD's current listener
691  * transports.
692  */
693 static ssize_t __write_ports_names(char *buf, struct net *net)
694 {
695         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
696
697         if (nn->nfsd_serv == NULL)
698                 return 0;
699         return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
700 }
701
702 /*
703  * A single 'fd' number was written, in which case it must be for
704  * a socket of a supported family/protocol, and we use it as an
705  * nfsd listener.
706  */
707 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
708 {
709         char *mesg = buf;
710         int fd, err;
711         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
712
713         err = get_int(&mesg, &fd);
714         if (err != 0 || fd < 0)
715                 return -EINVAL;
716
717         err = nfsd_create_serv(net);
718         if (err != 0)
719                 return err;
720
721         err = svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
722
723         if (err >= 0 &&
724             !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
725                 svc_get(nn->nfsd_serv);
726
727         nfsd_put(net);
728         return err;
729 }
730
731 /*
732  * A transport listener is added by writing it's transport name and
733  * a port number.
734  */
735 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
736 {
737         char transport[16];
738         struct svc_xprt *xprt;
739         int port, err;
740         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
741
742         if (sscanf(buf, "%15s %5u", transport, &port) != 2)
743                 return -EINVAL;
744
745         if (port < 1 || port > USHRT_MAX)
746                 return -EINVAL;
747
748         err = nfsd_create_serv(net);
749         if (err != 0)
750                 return err;
751
752         err = svc_xprt_create(nn->nfsd_serv, transport, net,
753                               PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
754         if (err < 0)
755                 goto out_err;
756
757         err = svc_xprt_create(nn->nfsd_serv, transport, net,
758                               PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
759         if (err < 0 && err != -EAFNOSUPPORT)
760                 goto out_close;
761
762         if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
763                 svc_get(nn->nfsd_serv);
764
765         nfsd_put(net);
766         return 0;
767 out_close:
768         xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
769         if (xprt != NULL) {
770                 svc_xprt_close(xprt);
771                 svc_xprt_put(xprt);
772         }
773 out_err:
774         nfsd_put(net);
775         return err;
776 }
777
778 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
779                              struct net *net)
780 {
781         if (size == 0)
782                 return __write_ports_names(buf, net);
783
784         if (isdigit(buf[0]))
785                 return __write_ports_addfd(buf, net, file->f_cred);
786
787         if (isalpha(buf[0]))
788                 return __write_ports_addxprt(buf, net, file->f_cred);
789
790         return -EINVAL;
791 }
792
793 /*
794  * write_ports - Pass a socket file descriptor or transport name to listen on
795  *
796  * Input:
797  *                      buf:            ignored
798  *                      size:           zero
799  * Output:
800  *      On success:     passed-in buffer filled with a '\n'-terminated C
801  *                      string containing a whitespace-separated list of
802  *                      named NFSD listeners;
803  *                      return code is the size in bytes of the string
804  *      On error:       return code is zero or a negative errno value
805  *
806  * OR
807  *
808  * Input:
809  *                      buf:            C string containing an unsigned
810  *                                      integer value representing a bound
811  *                                      but unconnected socket that is to be
812  *                                      used as an NFSD listener; listen(3)
813  *                                      must be called for a SOCK_STREAM
814  *                                      socket, otherwise it is ignored
815  *                      size:           non-zero length of C string in @buf
816  * Output:
817  *      On success:     NFS service is started;
818  *                      passed-in buffer filled with a '\n'-terminated C
819  *                      string containing a unique alphanumeric name of
820  *                      the listener;
821  *                      return code is the size in bytes of the string
822  *      On error:       return code is a negative errno value
823  *
824  * OR
825  *
826  * Input:
827  *                      buf:            C string containing a transport
828  *                                      name and an unsigned integer value
829  *                                      representing the port to listen on,
830  *                                      separated by whitespace
831  *                      size:           non-zero length of C string in @buf
832  * Output:
833  *      On success:     returns zero; NFS service is started
834  *      On error:       return code is a negative errno value
835  */
836 static ssize_t write_ports(struct file *file, char *buf, size_t size)
837 {
838         ssize_t rv;
839
840         mutex_lock(&nfsd_mutex);
841         rv = __write_ports(file, buf, size, netns(file));
842         mutex_unlock(&nfsd_mutex);
843         return rv;
844 }
845
846
847 int nfsd_max_blksize;
848
849 /*
850  * write_maxblksize - Set or report the current NFS blksize
851  *
852  * Input:
853  *                      buf:            ignored
854  *                      size:           zero
855  *
856  * OR
857  *
858  * Input:
859  *                      buf:            C string containing an unsigned
860  *                                      integer value representing the new
861  *                                      NFS blksize
862  *                      size:           non-zero length of C string in @buf
863  * Output:
864  *      On success:     passed-in buffer filled with '\n'-terminated C string
865  *                      containing numeric value of the current NFS blksize
866  *                      setting;
867  *                      return code is the size in bytes of the string
868  *      On error:       return code is zero or a negative errno value
869  */
870 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
871 {
872         char *mesg = buf;
873         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
874
875         if (size > 0) {
876                 int bsize;
877                 int rv = get_int(&mesg, &bsize);
878                 if (rv)
879                         return rv;
880                 /* force bsize into allowed range and
881                  * required alignment.
882                  */
883                 bsize = max_t(int, bsize, 1024);
884                 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
885                 bsize &= ~(1024-1);
886                 mutex_lock(&nfsd_mutex);
887                 if (nn->nfsd_serv) {
888                         mutex_unlock(&nfsd_mutex);
889                         return -EBUSY;
890                 }
891                 nfsd_max_blksize = bsize;
892                 mutex_unlock(&nfsd_mutex);
893         }
894
895         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
896                                                         nfsd_max_blksize);
897 }
898
899 /*
900  * write_maxconn - Set or report the current max number of connections
901  *
902  * Input:
903  *                      buf:            ignored
904  *                      size:           zero
905  * OR
906  *
907  * Input:
908  *                      buf:            C string containing an unsigned
909  *                                      integer value representing the new
910  *                                      number of max connections
911  *                      size:           non-zero length of C string in @buf
912  * Output:
913  *      On success:     passed-in buffer filled with '\n'-terminated C string
914  *                      containing numeric value of max_connections setting
915  *                      for this net namespace;
916  *                      return code is the size in bytes of the string
917  *      On error:       return code is zero or a negative errno value
918  */
919 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
920 {
921         char *mesg = buf;
922         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
923         unsigned int maxconn = nn->max_connections;
924
925         if (size > 0) {
926                 int rv = get_uint(&mesg, &maxconn);
927
928                 if (rv)
929                         return rv;
930                 nn->max_connections = maxconn;
931         }
932
933         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
934 }
935
936 #ifdef CONFIG_NFSD_V4
937 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
938                                   time64_t *time, struct nfsd_net *nn)
939 {
940         char *mesg = buf;
941         int rv, i;
942
943         if (size > 0) {
944                 if (nn->nfsd_serv)
945                         return -EBUSY;
946                 rv = get_int(&mesg, &i);
947                 if (rv)
948                         return rv;
949                 /*
950                  * Some sanity checking.  We don't have a reason for
951                  * these particular numbers, but problems with the
952                  * extremes are:
953                  *      - Too short: the briefest network outage may
954                  *        cause clients to lose all their locks.  Also,
955                  *        the frequent polling may be wasteful.
956                  *      - Too long: do you really want reboot recovery
957                  *        to take more than an hour?  Or to make other
958                  *        clients wait an hour before being able to
959                  *        revoke a dead client's locks?
960                  */
961                 if (i < 10 || i > 3600)
962                         return -EINVAL;
963                 *time = i;
964         }
965
966         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
967 }
968
969 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
970                                 time64_t *time, struct nfsd_net *nn)
971 {
972         ssize_t rv;
973
974         mutex_lock(&nfsd_mutex);
975         rv = __nfsd4_write_time(file, buf, size, time, nn);
976         mutex_unlock(&nfsd_mutex);
977         return rv;
978 }
979
980 /*
981  * write_leasetime - Set or report the current NFSv4 lease time
982  *
983  * Input:
984  *                      buf:            ignored
985  *                      size:           zero
986  *
987  * OR
988  *
989  * Input:
990  *                      buf:            C string containing an unsigned
991  *                                      integer value representing the new
992  *                                      NFSv4 lease expiry time
993  *                      size:           non-zero length of C string in @buf
994  * Output:
995  *      On success:     passed-in buffer filled with '\n'-terminated C
996  *                      string containing unsigned integer value of the
997  *                      current lease expiry time;
998  *                      return code is the size in bytes of the string
999  *      On error:       return code is zero or a negative errno value
1000  */
1001 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1002 {
1003         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1004         return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1005 }
1006
1007 /*
1008  * write_gracetime - Set or report current NFSv4 grace period time
1009  *
1010  * As above, but sets the time of the NFSv4 grace period.
1011  *
1012  * Note this should never be set to less than the *previous*
1013  * lease-period time, but we don't try to enforce this.  (In the common
1014  * case (a new boot), we don't know what the previous lease time was
1015  * anyway.)
1016  */
1017 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1018 {
1019         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1020         return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1021 }
1022
1023 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1024                                    struct nfsd_net *nn)
1025 {
1026         char *mesg = buf;
1027         char *recdir;
1028         int len, status;
1029
1030         if (size > 0) {
1031                 if (nn->nfsd_serv)
1032                         return -EBUSY;
1033                 if (size > PATH_MAX || buf[size-1] != '\n')
1034                         return -EINVAL;
1035                 buf[size-1] = 0;
1036
1037                 recdir = mesg;
1038                 len = qword_get(&mesg, recdir, size);
1039                 if (len <= 0)
1040                         return -EINVAL;
1041
1042                 status = nfs4_reset_recoverydir(recdir);
1043                 if (status)
1044                         return status;
1045         }
1046
1047         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1048                                                         nfs4_recoverydir());
1049 }
1050
1051 /*
1052  * write_recoverydir - Set or report the pathname of the recovery directory
1053  *
1054  * Input:
1055  *                      buf:            ignored
1056  *                      size:           zero
1057  *
1058  * OR
1059  *
1060  * Input:
1061  *                      buf:            C string containing the pathname
1062  *                                      of the directory on a local file
1063  *                                      system containing permanent NFSv4
1064  *                                      recovery data
1065  *                      size:           non-zero length of C string in @buf
1066  * Output:
1067  *      On success:     passed-in buffer filled with '\n'-terminated C string
1068  *                      containing the current recovery pathname setting;
1069  *                      return code is the size in bytes of the string
1070  *      On error:       return code is zero or a negative errno value
1071  */
1072 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1073 {
1074         ssize_t rv;
1075         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1076
1077         mutex_lock(&nfsd_mutex);
1078         rv = __write_recoverydir(file, buf, size, nn);
1079         mutex_unlock(&nfsd_mutex);
1080         return rv;
1081 }
1082
1083 /*
1084  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1085  *
1086  * Input:
1087  *                      buf:            ignored
1088  *                      size:           zero
1089  * OR
1090  *
1091  * Input:
1092  *                      buf:            any value
1093  *                      size:           non-zero length of C string in @buf
1094  * Output:
1095  *                      passed-in buffer filled with "Y" or "N" with a newline
1096  *                      and NULL-terminated C string. This indicates whether
1097  *                      the grace period has ended in the current net
1098  *                      namespace. Return code is the size in bytes of the
1099  *                      string. Writing a string that starts with 'Y', 'y', or
1100  *                      '1' to the file will end the grace period for nfsd's v4
1101  *                      lock manager.
1102  */
1103 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1104 {
1105         struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1106
1107         if (size > 0) {
1108                 switch(buf[0]) {
1109                 case 'Y':
1110                 case 'y':
1111                 case '1':
1112                         if (!nn->nfsd_serv)
1113                                 return -EBUSY;
1114                         nfsd4_end_grace(nn);
1115                         break;
1116                 default:
1117                         return -EINVAL;
1118                 }
1119         }
1120
1121         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1122                          nn->grace_ended ? 'Y' : 'N');
1123 }
1124
1125 #endif
1126
1127 /*----------------------------------------------------------------------------*/
1128 /*
1129  *      populating the filesystem.
1130  */
1131
1132 /* Basically copying rpc_get_inode. */
1133 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1134 {
1135         struct inode *inode = new_inode(sb);
1136         if (!inode)
1137                 return NULL;
1138         /* Following advice from simple_fill_super documentation: */
1139         inode->i_ino = iunique(sb, NFSD_MaxReserved);
1140         inode->i_mode = mode;
1141         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1142         switch (mode & S_IFMT) {
1143         case S_IFDIR:
1144                 inode->i_fop = &simple_dir_operations;
1145                 inode->i_op = &simple_dir_inode_operations;
1146                 inc_nlink(inode);
1147                 break;
1148         default:
1149                 break;
1150         }
1151         return inode;
1152 }
1153
1154 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1155 {
1156         struct inode *inode;
1157
1158         inode = nfsd_get_inode(dir->i_sb, mode);
1159         if (!inode)
1160                 return -ENOMEM;
1161         if (ncl) {
1162                 inode->i_private = ncl;
1163                 kref_get(&ncl->cl_ref);
1164         }
1165         d_add(dentry, inode);
1166         inc_nlink(dir);
1167         fsnotify_mkdir(dir, dentry);
1168         return 0;
1169 }
1170
1171 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1172 {
1173         struct inode *dir = parent->d_inode;
1174         struct dentry *dentry;
1175         int ret = -ENOMEM;
1176
1177         inode_lock(dir);
1178         dentry = d_alloc_name(parent, name);
1179         if (!dentry)
1180                 goto out_err;
1181         ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1182         if (ret)
1183                 goto out_err;
1184 out:
1185         inode_unlock(dir);
1186         return dentry;
1187 out_err:
1188         dput(dentry);
1189         dentry = ERR_PTR(ret);
1190         goto out;
1191 }
1192
1193 static void clear_ncl(struct inode *inode)
1194 {
1195         struct nfsdfs_client *ncl = inode->i_private;
1196
1197         inode->i_private = NULL;
1198         kref_put(&ncl->cl_ref, ncl->cl_release);
1199 }
1200
1201 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1202 {
1203         struct nfsdfs_client *nc = inode->i_private;
1204
1205         if (nc)
1206                 kref_get(&nc->cl_ref);
1207         return nc;
1208 }
1209
1210 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1211 {
1212         struct nfsdfs_client *nc;
1213
1214         inode_lock_shared(inode);
1215         nc = __get_nfsdfs_client(inode);
1216         inode_unlock_shared(inode);
1217         return nc;
1218 }
1219 /* from __rpc_unlink */
1220 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1221 {
1222         int ret;
1223
1224         clear_ncl(d_inode(dentry));
1225         dget(dentry);
1226         ret = simple_unlink(dir, dentry);
1227         d_drop(dentry);
1228         fsnotify_unlink(dir, dentry);
1229         dput(dentry);
1230         WARN_ON_ONCE(ret);
1231 }
1232
1233 static void nfsdfs_remove_files(struct dentry *root)
1234 {
1235         struct dentry *dentry, *tmp;
1236
1237         list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1238                 if (!simple_positive(dentry)) {
1239                         WARN_ON_ONCE(1); /* I think this can't happen? */
1240                         continue;
1241                 }
1242                 nfsdfs_remove_file(d_inode(root), dentry);
1243         }
1244 }
1245
1246 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1247  * code instead. */
1248 static  int nfsdfs_create_files(struct dentry *root,
1249                                 const struct tree_descr *files,
1250                                 struct dentry **fdentries)
1251 {
1252         struct inode *dir = d_inode(root);
1253         struct inode *inode;
1254         struct dentry *dentry;
1255         int i;
1256
1257         inode_lock(dir);
1258         for (i = 0; files->name && files->name[0]; i++, files++) {
1259                 dentry = d_alloc_name(root, files->name);
1260                 if (!dentry)
1261                         goto out;
1262                 inode = nfsd_get_inode(d_inode(root)->i_sb,
1263                                         S_IFREG | files->mode);
1264                 if (!inode) {
1265                         dput(dentry);
1266                         goto out;
1267                 }
1268                 inode->i_fop = files->ops;
1269                 inode->i_private = __get_nfsdfs_client(dir);
1270                 d_add(dentry, inode);
1271                 fsnotify_create(dir, dentry);
1272                 if (fdentries)
1273                         fdentries[i] = dentry;
1274         }
1275         inode_unlock(dir);
1276         return 0;
1277 out:
1278         nfsdfs_remove_files(root);
1279         inode_unlock(dir);
1280         return -ENOMEM;
1281 }
1282
1283 /* on success, returns positive number unique to that client. */
1284 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1285                                  struct nfsdfs_client *ncl, u32 id,
1286                                  const struct tree_descr *files,
1287                                  struct dentry **fdentries)
1288 {
1289         struct dentry *dentry;
1290         char name[11];
1291         int ret;
1292
1293         sprintf(name, "%u", id);
1294
1295         dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1296         if (IS_ERR(dentry)) /* XXX: tossing errors? */
1297                 return NULL;
1298         ret = nfsdfs_create_files(dentry, files, fdentries);
1299         if (ret) {
1300                 nfsd_client_rmdir(dentry);
1301                 return NULL;
1302         }
1303         return dentry;
1304 }
1305
1306 /* Taken from __rpc_rmdir: */
1307 void nfsd_client_rmdir(struct dentry *dentry)
1308 {
1309         struct inode *dir = d_inode(dentry->d_parent);
1310         struct inode *inode = d_inode(dentry);
1311         int ret;
1312
1313         inode_lock(dir);
1314         nfsdfs_remove_files(dentry);
1315         clear_ncl(inode);
1316         dget(dentry);
1317         ret = simple_rmdir(dir, dentry);
1318         WARN_ON_ONCE(ret);
1319         d_drop(dentry);
1320         fsnotify_rmdir(dir, dentry);
1321         dput(dentry);
1322         inode_unlock(dir);
1323 }
1324
1325 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1326 {
1327         struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1328                                                         nfsd_net_id);
1329         struct dentry *dentry;
1330         int ret;
1331
1332         static const struct tree_descr nfsd_files[] = {
1333                 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1334                 /* Per-export io stats use same ops as exports file */
1335                 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1336                 [NFSD_Export_features] = {"export_features",
1337                                         &export_features_fops, S_IRUGO},
1338                 [NFSD_FO_UnlockIP] = {"unlock_ip",
1339                                         &transaction_ops, S_IWUSR|S_IRUSR},
1340                 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1341                                         &transaction_ops, S_IWUSR|S_IRUSR},
1342                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1343                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1344                 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1345                 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1346                 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1347                                         &nfsd_reply_cache_stats_fops, S_IRUGO},
1348                 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1349                 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1350                 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1351                 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1352                 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1353 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
1354                 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes",
1355                                         &supported_enctypes_fops, S_IRUGO},
1356 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
1357 #ifdef CONFIG_NFSD_V4
1358                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1359                 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1360                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1361                 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1362 #endif
1363                 /* last one */ {""}
1364         };
1365
1366         ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1367         if (ret)
1368                 return ret;
1369         dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1370         if (IS_ERR(dentry))
1371                 return PTR_ERR(dentry);
1372         nn->nfsd_client_dir = dentry;
1373         return 0;
1374 }
1375
1376 static int nfsd_fs_get_tree(struct fs_context *fc)
1377 {
1378         return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1379 }
1380
1381 static void nfsd_fs_free_fc(struct fs_context *fc)
1382 {
1383         if (fc->s_fs_info)
1384                 put_net(fc->s_fs_info);
1385 }
1386
1387 static const struct fs_context_operations nfsd_fs_context_ops = {
1388         .free           = nfsd_fs_free_fc,
1389         .get_tree       = nfsd_fs_get_tree,
1390 };
1391
1392 static int nfsd_init_fs_context(struct fs_context *fc)
1393 {
1394         put_user_ns(fc->user_ns);
1395         fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1396         fc->ops = &nfsd_fs_context_ops;
1397         return 0;
1398 }
1399
1400 static void nfsd_umount(struct super_block *sb)
1401 {
1402         struct net *net = sb->s_fs_info;
1403
1404         nfsd_shutdown_threads(net);
1405
1406         kill_litter_super(sb);
1407         put_net(net);
1408 }
1409
1410 static struct file_system_type nfsd_fs_type = {
1411         .owner          = THIS_MODULE,
1412         .name           = "nfsd",
1413         .init_fs_context = nfsd_init_fs_context,
1414         .kill_sb        = nfsd_umount,
1415 };
1416 MODULE_ALIAS_FS("nfsd");
1417
1418 #ifdef CONFIG_PROC_FS
1419 static int create_proc_exports_entry(void)
1420 {
1421         struct proc_dir_entry *entry;
1422
1423         entry = proc_mkdir("fs/nfs", NULL);
1424         if (!entry)
1425                 return -ENOMEM;
1426         entry = proc_create("exports", 0, entry, &exports_proc_ops);
1427         if (!entry) {
1428                 remove_proc_entry("fs/nfs", NULL);
1429                 return -ENOMEM;
1430         }
1431         return 0;
1432 }
1433 #else /* CONFIG_PROC_FS */
1434 static int create_proc_exports_entry(void)
1435 {
1436         return 0;
1437 }
1438 #endif
1439
1440 unsigned int nfsd_net_id;
1441
1442 static __net_init int nfsd_init_net(struct net *net)
1443 {
1444         int retval;
1445         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1446
1447         retval = nfsd_export_init(net);
1448         if (retval)
1449                 goto out_export_error;
1450         retval = nfsd_idmap_init(net);
1451         if (retval)
1452                 goto out_idmap_error;
1453         nn->nfsd_versions = NULL;
1454         nn->nfsd4_minorversions = NULL;
1455         nfsd4_init_leases_net(nn);
1456         retval = nfsd_reply_cache_init(nn);
1457         if (retval)
1458                 goto out_cache_error;
1459         get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1460         seqlock_init(&nn->writeverf_lock);
1461
1462         return 0;
1463
1464 out_cache_error:
1465         nfsd_idmap_shutdown(net);
1466 out_idmap_error:
1467         nfsd_export_shutdown(net);
1468 out_export_error:
1469         return retval;
1470 }
1471
1472 static __net_exit void nfsd_exit_net(struct net *net)
1473 {
1474         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1475
1476         nfsd_reply_cache_shutdown(nn);
1477         nfsd_idmap_shutdown(net);
1478         nfsd_export_shutdown(net);
1479         nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1480 }
1481
1482 static struct pernet_operations nfsd_net_ops = {
1483         .init = nfsd_init_net,
1484         .exit = nfsd_exit_net,
1485         .id   = &nfsd_net_id,
1486         .size = sizeof(struct nfsd_net),
1487 };
1488
1489 static int __init init_nfsd(void)
1490 {
1491         int retval;
1492
1493         retval = nfsd4_init_slabs();
1494         if (retval)
1495                 return retval;
1496         retval = nfsd4_init_pnfs();
1497         if (retval)
1498                 goto out_free_slabs;
1499         retval = nfsd_stat_init();      /* Statistics */
1500         if (retval)
1501                 goto out_free_pnfs;
1502         retval = nfsd_drc_slab_create();
1503         if (retval)
1504                 goto out_free_stat;
1505         nfsd_lockd_init();      /* lockd->nfsd callbacks */
1506         retval = create_proc_exports_entry();
1507         if (retval)
1508                 goto out_free_lockd;
1509         retval = register_pernet_subsys(&nfsd_net_ops);
1510         if (retval < 0)
1511                 goto out_free_exports;
1512         retval = register_cld_notifier();
1513         if (retval)
1514                 goto out_free_subsys;
1515         retval = nfsd4_create_laundry_wq();
1516         if (retval)
1517                 goto out_free_cld;
1518         retval = register_filesystem(&nfsd_fs_type);
1519         if (retval)
1520                 goto out_free_all;
1521         return 0;
1522 out_free_all:
1523         nfsd4_destroy_laundry_wq();
1524 out_free_cld:
1525         unregister_cld_notifier();
1526 out_free_subsys:
1527         unregister_pernet_subsys(&nfsd_net_ops);
1528 out_free_exports:
1529         remove_proc_entry("fs/nfs/exports", NULL);
1530         remove_proc_entry("fs/nfs", NULL);
1531 out_free_lockd:
1532         nfsd_lockd_shutdown();
1533         nfsd_drc_slab_free();
1534 out_free_stat:
1535         nfsd_stat_shutdown();
1536 out_free_pnfs:
1537         nfsd4_exit_pnfs();
1538 out_free_slabs:
1539         nfsd4_free_slabs();
1540         return retval;
1541 }
1542
1543 static void __exit exit_nfsd(void)
1544 {
1545         unregister_filesystem(&nfsd_fs_type);
1546         nfsd4_destroy_laundry_wq();
1547         unregister_cld_notifier();
1548         unregister_pernet_subsys(&nfsd_net_ops);
1549         nfsd_drc_slab_free();
1550         remove_proc_entry("fs/nfs/exports", NULL);
1551         remove_proc_entry("fs/nfs", NULL);
1552         nfsd_stat_shutdown();
1553         nfsd_lockd_shutdown();
1554         nfsd4_free_slabs();
1555         nfsd4_exit_pnfs();
1556 }
1557
1558 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1559 MODULE_LICENSE("GPL");
1560 module_init(init_nfsd)
1561 module_exit(exit_nfsd)