svcrpc: svc_tcp_sendto XPT_DEAD check is redundant
[cascardo/linux.git] / fs / nfsd / nfsctl.c
1 /*
2  * Syscall interface to knfsd.
3  *
4  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
5  */
6
7 #include <linux/slab.h>
8 #include <linux/namei.h>
9 #include <linux/ctype.h>
10
11 #include <linux/nfsd_idmap.h>
12 #include <linux/sunrpc/svcsock.h>
13 #include <linux/nfsd/syscall.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/clnt.h>
16
17 #include "nfsd.h"
18 #include "cache.h"
19
20 /*
21  *      We have a single directory with 9 nodes in it.
22  */
23 enum {
24         NFSD_Root = 1,
25 #ifdef CONFIG_NFSD_DEPRECATED
26         NFSD_Svc,
27         NFSD_Add,
28         NFSD_Del,
29         NFSD_Export,
30         NFSD_Unexport,
31         NFSD_Getfd,
32         NFSD_Getfs,
33 #endif
34         NFSD_List,
35         NFSD_Export_features,
36         NFSD_Fh,
37         NFSD_FO_UnlockIP,
38         NFSD_FO_UnlockFS,
39         NFSD_Threads,
40         NFSD_Pool_Threads,
41         NFSD_Pool_Stats,
42         NFSD_Versions,
43         NFSD_Ports,
44         NFSD_MaxBlkSize,
45         /*
46          * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
47          * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
48          */
49 #ifdef CONFIG_NFSD_V4
50         NFSD_Leasetime,
51         NFSD_Gracetime,
52         NFSD_RecoveryDir,
53 #endif
54 };
55
56 /*
57  * write() for these nodes.
58  */
59 #ifdef CONFIG_NFSD_DEPRECATED
60 static ssize_t write_svc(struct file *file, char *buf, size_t size);
61 static ssize_t write_add(struct file *file, char *buf, size_t size);
62 static ssize_t write_del(struct file *file, char *buf, size_t size);
63 static ssize_t write_export(struct file *file, char *buf, size_t size);
64 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
65 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
66 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
67 #endif
68 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
69 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
70 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
71 static ssize_t write_threads(struct file *file, char *buf, size_t size);
72 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
73 static ssize_t write_versions(struct file *file, char *buf, size_t size);
74 static ssize_t write_ports(struct file *file, char *buf, size_t size);
75 static ssize_t write_maxblksize(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 #endif
81
82 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
83 #ifdef CONFIG_NFSD_DEPRECATED
84         [NFSD_Svc] = write_svc,
85         [NFSD_Add] = write_add,
86         [NFSD_Del] = write_del,
87         [NFSD_Export] = write_export,
88         [NFSD_Unexport] = write_unexport,
89         [NFSD_Getfd] = write_getfd,
90         [NFSD_Getfs] = write_getfs,
91 #endif
92         [NFSD_Fh] = write_filehandle,
93         [NFSD_FO_UnlockIP] = write_unlock_ip,
94         [NFSD_FO_UnlockFS] = write_unlock_fs,
95         [NFSD_Threads] = write_threads,
96         [NFSD_Pool_Threads] = write_pool_threads,
97         [NFSD_Versions] = write_versions,
98         [NFSD_Ports] = write_ports,
99         [NFSD_MaxBlkSize] = write_maxblksize,
100 #ifdef CONFIG_NFSD_V4
101         [NFSD_Leasetime] = write_leasetime,
102         [NFSD_Gracetime] = write_gracetime,
103         [NFSD_RecoveryDir] = write_recoverydir,
104 #endif
105 };
106
107 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
108 {
109         ino_t ino =  file->f_path.dentry->d_inode->i_ino;
110         char *data;
111         ssize_t rv;
112
113         if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
114                 return -EINVAL;
115
116         data = simple_transaction_get(file, buf, size);
117         if (IS_ERR(data))
118                 return PTR_ERR(data);
119
120         rv =  write_op[ino](file, data, size);
121         if (rv >= 0) {
122                 simple_transaction_set(file, rv);
123                 rv = size;
124         }
125         return rv;
126 }
127
128 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
129 {
130         static int warned;
131         if (file->f_dentry->d_name.name[0] == '.' && !warned) {
132                 printk(KERN_INFO
133                        "Warning: \"%s\" uses deprecated NFSD interface: %s."
134                        "  This will be removed in 2.6.40\n",
135                        current->comm, file->f_dentry->d_name.name);
136                 warned = 1;
137         }
138         if (! file->private_data) {
139                 /* An attempt to read a transaction file without writing
140                  * causes a 0-byte write so that the file can return
141                  * state information
142                  */
143                 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
144                 if (rv < 0)
145                         return rv;
146         }
147         return simple_transaction_read(file, buf, size, pos);
148 }
149
150 static const struct file_operations transaction_ops = {
151         .write          = nfsctl_transaction_write,
152         .read           = nfsctl_transaction_read,
153         .release        = simple_transaction_release,
154 };
155
156 static int exports_open(struct inode *inode, struct file *file)
157 {
158         return seq_open(file, &nfs_exports_op);
159 }
160
161 static const struct file_operations exports_operations = {
162         .open           = exports_open,
163         .read           = seq_read,
164         .llseek         = seq_lseek,
165         .release        = seq_release,
166         .owner          = THIS_MODULE,
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 static int export_features_open(struct inode *inode, struct file *file)
176 {
177         return single_open(file, export_features_show, NULL);
178 }
179
180 static struct file_operations export_features_operations = {
181         .open           = export_features_open,
182         .read           = seq_read,
183         .llseek         = seq_lseek,
184         .release        = single_release,
185 };
186
187 extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
188 extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
189
190 static const struct file_operations pool_stats_operations = {
191         .open           = nfsd_pool_stats_open,
192         .read           = seq_read,
193         .llseek         = seq_lseek,
194         .release        = nfsd_pool_stats_release,
195         .owner          = THIS_MODULE,
196 };
197
198 /*----------------------------------------------------------------------------*/
199 /*
200  * payload - write methods
201  */
202
203 #ifdef CONFIG_NFSD_DEPRECATED
204 /**
205  * write_svc - Start kernel's NFSD server
206  *
207  * Deprecated.  /proc/fs/nfsd/threads is preferred.
208  * Function remains to support old versions of nfs-utils.
209  *
210  * Input:
211  *                      buf:    struct nfsctl_svc
212  *                              svc_port:       port number of this
213  *                                              server's listener
214  *                              svc_nthreads:   number of threads to start
215  *                      size:   size in bytes of passed in nfsctl_svc
216  * Output:
217  *      On success:     returns zero
218  *      On error:       return code is negative errno value
219  */
220 static ssize_t write_svc(struct file *file, char *buf, size_t size)
221 {
222         struct nfsctl_svc *data;
223         int err;
224         if (size < sizeof(*data))
225                 return -EINVAL;
226         data = (struct nfsctl_svc*) buf;
227         err = nfsd_svc(data->svc_port, data->svc_nthreads);
228         if (err < 0)
229                 return err;
230         return 0;
231 }
232
233 /**
234  * write_add - Add or modify client entry in auth unix cache
235  *
236  * Deprecated.  /proc/net/rpc/auth.unix.ip is preferred.
237  * Function remains to support old versions of nfs-utils.
238  *
239  * Input:
240  *                      buf:    struct nfsctl_client
241  *                              cl_ident:       '\0'-terminated C string
242  *                                              containing domain name
243  *                                              of client
244  *                              cl_naddr:       no. of items in cl_addrlist
245  *                              cl_addrlist:    array of client addresses
246  *                              cl_fhkeytype:   ignored
247  *                              cl_fhkeylen:    ignored
248  *                              cl_fhkey:       ignored
249  *                      size:   size in bytes of passed in nfsctl_client
250  * Output:
251  *      On success:     returns zero
252  *      On error:       return code is negative errno value
253  *
254  * Note: Only AF_INET client addresses are passed in, since
255  * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
256  */
257 static ssize_t write_add(struct file *file, char *buf, size_t size)
258 {
259         struct nfsctl_client *data;
260         if (size < sizeof(*data))
261                 return -EINVAL;
262         data = (struct nfsctl_client *)buf;
263         return exp_addclient(data);
264 }
265
266 /**
267  * write_del - Remove client from auth unix cache
268  *
269  * Deprecated.  /proc/net/rpc/auth.unix.ip is preferred.
270  * Function remains to support old versions of nfs-utils.
271  *
272  * Input:
273  *                      buf:    struct nfsctl_client
274  *                              cl_ident:       '\0'-terminated C string
275  *                                              containing domain name
276  *                                              of client
277  *                              cl_naddr:       ignored
278  *                              cl_addrlist:    ignored
279  *                              cl_fhkeytype:   ignored
280  *                              cl_fhkeylen:    ignored
281  *                              cl_fhkey:       ignored
282  *                      size:   size in bytes of passed in nfsctl_client
283  * Output:
284  *      On success:     returns zero
285  *      On error:       return code is negative errno value
286  *
287  * Note: Only AF_INET client addresses are passed in, since
288  * nfsctl_client.cl_addrlist contains only in_addr fields for addresses.
289  */
290 static ssize_t write_del(struct file *file, char *buf, size_t size)
291 {
292         struct nfsctl_client *data;
293         if (size < sizeof(*data))
294                 return -EINVAL;
295         data = (struct nfsctl_client *)buf;
296         return exp_delclient(data);
297 }
298
299 /**
300  * write_export - Export part or all of a local file system
301  *
302  * Deprecated.  /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
303  * Function remains to support old versions of nfs-utils.
304  *
305  * Input:
306  *                      buf:    struct nfsctl_export
307  *                              ex_client:      '\0'-terminated C string
308  *                                              containing domain name
309  *                                              of client allowed to access
310  *                                              this export
311  *                              ex_path:        '\0'-terminated C string
312  *                                              containing pathname of
313  *                                              directory in local file system
314  *                              ex_dev:         fsid to use for this export
315  *                              ex_ino:         ignored
316  *                              ex_flags:       export flags for this export
317  *                              ex_anon_uid:    UID to use for anonymous
318  *                                              requests
319  *                              ex_anon_gid:    GID to use for anonymous
320  *                                              requests
321  *                      size:   size in bytes of passed in nfsctl_export
322  * Output:
323  *      On success:     returns zero
324  *      On error:       return code is negative errno value
325  */
326 static ssize_t write_export(struct file *file, char *buf, size_t size)
327 {
328         struct nfsctl_export *data;
329         if (size < sizeof(*data))
330                 return -EINVAL;
331         data = (struct nfsctl_export*)buf;
332         return exp_export(data);
333 }
334
335 /**
336  * write_unexport - Unexport a previously exported file system
337  *
338  * Deprecated.  /proc/net/rpc/{nfsd.export,nfsd.fh} are preferred.
339  * Function remains to support old versions of nfs-utils.
340  *
341  * Input:
342  *                      buf:    struct nfsctl_export
343  *                              ex_client:      '\0'-terminated C string
344  *                                              containing domain name
345  *                                              of client no longer allowed
346  *                                              to access this export
347  *                              ex_path:        '\0'-terminated C string
348  *                                              containing pathname of
349  *                                              directory in local file system
350  *                              ex_dev:         ignored
351  *                              ex_ino:         ignored
352  *                              ex_flags:       ignored
353  *                              ex_anon_uid:    ignored
354  *                              ex_anon_gid:    ignored
355  *                      size:   size in bytes of passed in nfsctl_export
356  * Output:
357  *      On success:     returns zero
358  *      On error:       return code is negative errno value
359  */
360 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
361 {
362         struct nfsctl_export *data;
363
364         if (size < sizeof(*data))
365                 return -EINVAL;
366         data = (struct nfsctl_export*)buf;
367         return exp_unexport(data);
368 }
369
370 /**
371  * write_getfs - Get a variable-length NFS file handle by path
372  *
373  * Deprecated.  /proc/fs/nfsd/filehandle is preferred.
374  * Function remains to support old versions of nfs-utils.
375  *
376  * Input:
377  *                      buf:    struct nfsctl_fsparm
378  *                              gd_addr:        socket address of client
379  *                              gd_path:        '\0'-terminated C string
380  *                                              containing pathname of
381  *                                              directory in local file system
382  *                              gd_maxlen:      maximum size of returned file
383  *                                              handle
384  *                      size:   size in bytes of passed in nfsctl_fsparm
385  * Output:
386  *      On success:     passed-in buffer filled with a knfsd_fh structure
387  *                      (a variable-length raw NFS file handle);
388  *                      return code is the size in bytes of the file handle
389  *      On error:       return code is negative errno value
390  *
391  * Note: Only AF_INET client addresses are passed in, since gd_addr
392  * is the same size as a struct sockaddr_in.
393  */
394 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
395 {
396         struct nfsctl_fsparm *data;
397         struct sockaddr_in *sin;
398         struct auth_domain *clp;
399         int err = 0;
400         struct knfsd_fh *res;
401         struct in6_addr in6;
402
403         if (size < sizeof(*data))
404                 return -EINVAL;
405         data = (struct nfsctl_fsparm*)buf;
406         err = -EPROTONOSUPPORT;
407         if (data->gd_addr.sa_family != AF_INET)
408                 goto out;
409         sin = (struct sockaddr_in *)&data->gd_addr;
410         if (data->gd_maxlen > NFS3_FHSIZE)
411                 data->gd_maxlen = NFS3_FHSIZE;
412
413         res = (struct knfsd_fh*)buf;
414
415         exp_readlock();
416
417         ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
418
419         clp = auth_unix_lookup(&init_net, &in6);
420         if (!clp)
421                 err = -EPERM;
422         else {
423                 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
424                 auth_domain_put(clp);
425         }
426         exp_readunlock();
427         if (err == 0)
428                 err = res->fh_size + offsetof(struct knfsd_fh, fh_base);
429  out:
430         return err;
431 }
432
433 /**
434  * write_getfd - Get a fixed-length NFS file handle by path (used by mountd)
435  *
436  * Deprecated.  /proc/fs/nfsd/filehandle is preferred.
437  * Function remains to support old versions of nfs-utils.
438  *
439  * Input:
440  *                      buf:    struct nfsctl_fdparm
441  *                              gd_addr:        socket address of client
442  *                              gd_path:        '\0'-terminated C string
443  *                                              containing pathname of
444  *                                              directory in local file system
445  *                              gd_version:     fdparm structure version
446  *                      size:   size in bytes of passed in nfsctl_fdparm
447  * Output:
448  *      On success:     passed-in buffer filled with nfsctl_res
449  *                      (a fixed-length raw NFS file handle);
450  *                      return code is the size in bytes of the file handle
451  *      On error:       return code is negative errno value
452  *
453  * Note: Only AF_INET client addresses are passed in, since gd_addr
454  * is the same size as a struct sockaddr_in.
455  */
456 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
457 {
458         struct nfsctl_fdparm *data;
459         struct sockaddr_in *sin;
460         struct auth_domain *clp;
461         int err = 0;
462         struct knfsd_fh fh;
463         char *res;
464         struct in6_addr in6;
465
466         if (size < sizeof(*data))
467                 return -EINVAL;
468         data = (struct nfsctl_fdparm*)buf;
469         err = -EPROTONOSUPPORT;
470         if (data->gd_addr.sa_family != AF_INET)
471                 goto out;
472         err = -EINVAL;
473         if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
474                 goto out;
475
476         res = buf;
477         sin = (struct sockaddr_in *)&data->gd_addr;
478         exp_readlock();
479
480         ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &in6);
481
482         clp = auth_unix_lookup(&init_net, &in6);
483         if (!clp)
484                 err = -EPERM;
485         else {
486                 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
487                 auth_domain_put(clp);
488         }
489         exp_readunlock();
490
491         if (err == 0) {
492                 memset(res,0, NFS_FHSIZE);
493                 memcpy(res, &fh.fh_base, fh.fh_size);
494                 err = NFS_FHSIZE;
495         }
496  out:
497         return err;
498 }
499 #endif /* CONFIG_NFSD_DEPRECATED */
500
501 /**
502  * write_unlock_ip - Release all locks used by a client
503  *
504  * Experimental.
505  *
506  * Input:
507  *                      buf:    '\n'-terminated C string containing a
508  *                              presentation format IP address
509  *                      size:   length of C string in @buf
510  * Output:
511  *      On success:     returns zero if all specified locks were released;
512  *                      returns one if one or more locks were not released
513  *      On error:       return code is negative errno value
514  */
515 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
516 {
517         struct sockaddr_storage address;
518         struct sockaddr *sap = (struct sockaddr *)&address;
519         size_t salen = sizeof(address);
520         char *fo_path;
521
522         /* sanity check */
523         if (size == 0)
524                 return -EINVAL;
525
526         if (buf[size-1] != '\n')
527                 return -EINVAL;
528
529         fo_path = buf;
530         if (qword_get(&buf, fo_path, size) < 0)
531                 return -EINVAL;
532
533         if (rpc_pton(fo_path, size, sap, salen) == 0)
534                 return -EINVAL;
535
536         return nlmsvc_unlock_all_by_ip(sap);
537 }
538
539 /**
540  * write_unlock_fs - Release all locks on a local file system
541  *
542  * Experimental.
543  *
544  * Input:
545  *                      buf:    '\n'-terminated C string containing the
546  *                              absolute pathname of a local file system
547  *                      size:   length of C string in @buf
548  * Output:
549  *      On success:     returns zero if all specified locks were released;
550  *                      returns one if one or more locks were not released
551  *      On error:       return code is negative errno value
552  */
553 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
554 {
555         struct path path;
556         char *fo_path;
557         int error;
558
559         /* sanity check */
560         if (size == 0)
561                 return -EINVAL;
562
563         if (buf[size-1] != '\n')
564                 return -EINVAL;
565
566         fo_path = buf;
567         if (qword_get(&buf, fo_path, size) < 0)
568                 return -EINVAL;
569
570         error = kern_path(fo_path, 0, &path);
571         if (error)
572                 return error;
573
574         /*
575          * XXX: Needs better sanity checking.  Otherwise we could end up
576          * releasing locks on the wrong file system.
577          *
578          * For example:
579          * 1.  Does the path refer to a directory?
580          * 2.  Is that directory a mount point, or
581          * 3.  Is that directory the root of an exported file system?
582          */
583         error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
584
585         path_put(&path);
586         return error;
587 }
588
589 /**
590  * write_filehandle - Get a variable-length NFS file handle by path
591  *
592  * On input, the buffer contains a '\n'-terminated C string comprised of
593  * three alphanumeric words separated by whitespace.  The string may
594  * contain escape sequences.
595  *
596  * Input:
597  *                      buf:
598  *                              domain:         client domain name
599  *                              path:           export pathname
600  *                              maxsize:        numeric maximum size of
601  *                                              @buf
602  *                      size:   length of C string in @buf
603  * Output:
604  *      On success:     passed-in buffer filled with '\n'-terminated C
605  *                      string containing a ASCII hex text version
606  *                      of the NFS file handle;
607  *                      return code is the size in bytes of the string
608  *      On error:       return code is negative errno value
609  */
610 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
611 {
612         char *dname, *path;
613         int uninitialized_var(maxsize);
614         char *mesg = buf;
615         int len;
616         struct auth_domain *dom;
617         struct knfsd_fh fh;
618
619         if (size == 0)
620                 return -EINVAL;
621
622         if (buf[size-1] != '\n')
623                 return -EINVAL;
624         buf[size-1] = 0;
625
626         dname = mesg;
627         len = qword_get(&mesg, dname, size);
628         if (len <= 0)
629                 return -EINVAL;
630         
631         path = dname+len+1;
632         len = qword_get(&mesg, path, size);
633         if (len <= 0)
634                 return -EINVAL;
635
636         len = get_int(&mesg, &maxsize);
637         if (len)
638                 return len;
639
640         if (maxsize < NFS_FHSIZE)
641                 return -EINVAL;
642         if (maxsize > NFS3_FHSIZE)
643                 maxsize = NFS3_FHSIZE;
644
645         if (qword_get(&mesg, mesg, size)>0)
646                 return -EINVAL;
647
648         /* we have all the words, they are in buf.. */
649         dom = unix_domain_find(dname);
650         if (!dom)
651                 return -ENOMEM;
652
653         len = exp_rootfh(dom, path, &fh,  maxsize);
654         auth_domain_put(dom);
655         if (len)
656                 return len;
657         
658         mesg = buf;
659         len = SIMPLE_TRANSACTION_LIMIT;
660         qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
661         mesg[-1] = '\n';
662         return mesg - buf;      
663 }
664
665 /**
666  * write_threads - Start NFSD, or report the current number of running threads
667  *
668  * Input:
669  *                      buf:            ignored
670  *                      size:           zero
671  * Output:
672  *      On success:     passed-in buffer filled with '\n'-terminated C
673  *                      string numeric value representing the number of
674  *                      running NFSD threads;
675  *                      return code is the size in bytes of the string
676  *      On error:       return code is zero
677  *
678  * OR
679  *
680  * Input:
681  *                      buf:            C string containing an unsigned
682  *                                      integer value representing the
683  *                                      number of NFSD threads to start
684  *                      size:           non-zero length of C string in @buf
685  * Output:
686  *      On success:     NFS service is started;
687  *                      passed-in buffer filled with '\n'-terminated C
688  *                      string numeric value representing the number of
689  *                      running NFSD threads;
690  *                      return code is the size in bytes of the string
691  *      On error:       return code is zero or a negative errno value
692  */
693 static ssize_t write_threads(struct file *file, char *buf, size_t size)
694 {
695         char *mesg = buf;
696         int rv;
697         if (size > 0) {
698                 int newthreads;
699                 rv = get_int(&mesg, &newthreads);
700                 if (rv)
701                         return rv;
702                 if (newthreads < 0)
703                         return -EINVAL;
704                 rv = nfsd_svc(NFS_PORT, newthreads);
705                 if (rv < 0)
706                         return rv;
707         } else
708                 rv = nfsd_nrthreads();
709
710         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
711 }
712
713 /**
714  * write_pool_threads - Set or report the current number of threads per pool
715  *
716  * Input:
717  *                      buf:            ignored
718  *                      size:           zero
719  *
720  * OR
721  *
722  * Input:
723  *                      buf:            C string containing whitespace-
724  *                                      separated unsigned integer values
725  *                                      representing the number of NFSD
726  *                                      threads to start in each pool
727  *                      size:           non-zero length of C string in @buf
728  * Output:
729  *      On success:     passed-in buffer filled with '\n'-terminated C
730  *                      string containing integer values representing the
731  *                      number of NFSD threads in each pool;
732  *                      return code is the size in bytes of the string
733  *      On error:       return code is zero or a negative errno value
734  */
735 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
736 {
737         /* if size > 0, look for an array of number of threads per node
738          * and apply them  then write out number of threads per node as reply
739          */
740         char *mesg = buf;
741         int i;
742         int rv;
743         int len;
744         int npools;
745         int *nthreads;
746
747         mutex_lock(&nfsd_mutex);
748         npools = nfsd_nrpools();
749         if (npools == 0) {
750                 /*
751                  * NFS is shut down.  The admin can start it by
752                  * writing to the threads file but NOT the pool_threads
753                  * file, sorry.  Report zero threads.
754                  */
755                 mutex_unlock(&nfsd_mutex);
756                 strcpy(buf, "0\n");
757                 return strlen(buf);
758         }
759
760         nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
761         rv = -ENOMEM;
762         if (nthreads == NULL)
763                 goto out_free;
764
765         if (size > 0) {
766                 for (i = 0; i < npools; i++) {
767                         rv = get_int(&mesg, &nthreads[i]);
768                         if (rv == -ENOENT)
769                                 break;          /* fewer numbers than pools */
770                         if (rv)
771                                 goto out_free;  /* syntax error */
772                         rv = -EINVAL;
773                         if (nthreads[i] < 0)
774                                 goto out_free;
775                 }
776                 rv = nfsd_set_nrthreads(i, nthreads);
777                 if (rv)
778                         goto out_free;
779         }
780
781         rv = nfsd_get_nrthreads(npools, nthreads);
782         if (rv)
783                 goto out_free;
784
785         mesg = buf;
786         size = SIMPLE_TRANSACTION_LIMIT;
787         for (i = 0; i < npools && size > 0; i++) {
788                 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
789                 len = strlen(mesg);
790                 size -= len;
791                 mesg += len;
792         }
793         rv = mesg - buf;
794 out_free:
795         kfree(nthreads);
796         mutex_unlock(&nfsd_mutex);
797         return rv;
798 }
799
800 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
801 {
802         char *mesg = buf;
803         char *vers, *minorp, sign;
804         int len, num, remaining;
805         unsigned minor;
806         ssize_t tlen = 0;
807         char *sep;
808
809         if (size>0) {
810                 if (nfsd_serv)
811                         /* Cannot change versions without updating
812                          * nfsd_serv->sv_xdrsize, and reallocing
813                          * rq_argp and rq_resp
814                          */
815                         return -EBUSY;
816                 if (buf[size-1] != '\n')
817                         return -EINVAL;
818                 buf[size-1] = 0;
819
820                 vers = mesg;
821                 len = qword_get(&mesg, vers, size);
822                 if (len <= 0) return -EINVAL;
823                 do {
824                         sign = *vers;
825                         if (sign == '+' || sign == '-')
826                                 num = simple_strtol((vers+1), &minorp, 0);
827                         else
828                                 num = simple_strtol(vers, &minorp, 0);
829                         if (*minorp == '.') {
830                                 if (num < 4)
831                                         return -EINVAL;
832                                 minor = simple_strtoul(minorp+1, NULL, 0);
833                                 if (minor == 0)
834                                         return -EINVAL;
835                                 if (nfsd_minorversion(minor, sign == '-' ?
836                                                      NFSD_CLEAR : NFSD_SET) < 0)
837                                         return -EINVAL;
838                                 goto next;
839                         }
840                         switch(num) {
841                         case 2:
842                         case 3:
843                         case 4:
844                                 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
845                                 break;
846                         default:
847                                 return -EINVAL;
848                         }
849                 next:
850                         vers += len + 1;
851                 } while ((len = qword_get(&mesg, vers, size)) > 0);
852                 /* If all get turned off, turn them back on, as
853                  * having no versions is BAD
854                  */
855                 nfsd_reset_versions();
856         }
857
858         /* Now write current state into reply buffer */
859         len = 0;
860         sep = "";
861         remaining = SIMPLE_TRANSACTION_LIMIT;
862         for (num=2 ; num <= 4 ; num++)
863                 if (nfsd_vers(num, NFSD_AVAIL)) {
864                         len = snprintf(buf, remaining, "%s%c%d", sep,
865                                        nfsd_vers(num, NFSD_TEST)?'+':'-',
866                                        num);
867                         sep = " ";
868
869                         if (len > remaining)
870                                 break;
871                         remaining -= len;
872                         buf += len;
873                         tlen += len;
874                 }
875         if (nfsd_vers(4, NFSD_AVAIL))
876                 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
877                      minor++) {
878                         len = snprintf(buf, remaining, " %c4.%u",
879                                         (nfsd_vers(4, NFSD_TEST) &&
880                                          nfsd_minorversion(minor, NFSD_TEST)) ?
881                                                 '+' : '-',
882                                         minor);
883
884                         if (len > remaining)
885                                 break;
886                         remaining -= len;
887                         buf += len;
888                         tlen += len;
889                 }
890
891         len = snprintf(buf, remaining, "\n");
892         if (len > remaining)
893                 return -EINVAL;
894         return tlen + len;
895 }
896
897 /**
898  * write_versions - Set or report the available NFS protocol versions
899  *
900  * Input:
901  *                      buf:            ignored
902  *                      size:           zero
903  * Output:
904  *      On success:     passed-in buffer filled with '\n'-terminated C
905  *                      string containing positive or negative integer
906  *                      values representing the current status of each
907  *                      protocol version;
908  *                      return code is the size in bytes of the string
909  *      On error:       return code is zero or a negative errno value
910  *
911  * OR
912  *
913  * Input:
914  *                      buf:            C string containing whitespace-
915  *                                      separated positive or negative
916  *                                      integer values representing NFS
917  *                                      protocol versions to enable ("+n")
918  *                                      or disable ("-n")
919  *                      size:           non-zero length of C string in @buf
920  * Output:
921  *      On success:     status of zero or more protocol versions has
922  *                      been updated; passed-in buffer filled with
923  *                      '\n'-terminated C string containing positive
924  *                      or negative integer values representing the
925  *                      current status of each protocol version;
926  *                      return code is the size in bytes of the string
927  *      On error:       return code is zero or a negative errno value
928  */
929 static ssize_t write_versions(struct file *file, char *buf, size_t size)
930 {
931         ssize_t rv;
932
933         mutex_lock(&nfsd_mutex);
934         rv = __write_versions(file, buf, size);
935         mutex_unlock(&nfsd_mutex);
936         return rv;
937 }
938
939 /*
940  * Zero-length write.  Return a list of NFSD's current listener
941  * transports.
942  */
943 static ssize_t __write_ports_names(char *buf)
944 {
945         if (nfsd_serv == NULL)
946                 return 0;
947         return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
948 }
949
950 /*
951  * A single 'fd' number was written, in which case it must be for
952  * a socket of a supported family/protocol, and we use it as an
953  * nfsd listener.
954  */
955 static ssize_t __write_ports_addfd(char *buf)
956 {
957         char *mesg = buf;
958         int fd, err;
959
960         err = get_int(&mesg, &fd);
961         if (err != 0 || fd < 0)
962                 return -EINVAL;
963
964         err = nfsd_create_serv();
965         if (err != 0)
966                 return err;
967
968         err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
969         if (err < 0) {
970                 svc_destroy(nfsd_serv);
971                 return err;
972         }
973
974         /* Decrease the count, but don't shut down the service */
975         nfsd_serv->sv_nrthreads--;
976         return err;
977 }
978
979 /*
980  * A '-' followed by the 'name' of a socket means we close the socket.
981  */
982 static ssize_t __write_ports_delfd(char *buf)
983 {
984         char *toclose;
985         int len = 0;
986
987         toclose = kstrdup(buf + 1, GFP_KERNEL);
988         if (toclose == NULL)
989                 return -ENOMEM;
990
991         if (nfsd_serv != NULL)
992                 len = svc_sock_names(nfsd_serv, buf,
993                                         SIMPLE_TRANSACTION_LIMIT, toclose);
994         kfree(toclose);
995         return len;
996 }
997
998 /*
999  * A transport listener is added by writing it's transport name and
1000  * a port number.
1001  */
1002 static ssize_t __write_ports_addxprt(char *buf)
1003 {
1004         char transport[16];
1005         struct svc_xprt *xprt;
1006         int port, err;
1007
1008         if (sscanf(buf, "%15s %4u", transport, &port) != 2)
1009                 return -EINVAL;
1010
1011         if (port < 1 || port > USHRT_MAX)
1012                 return -EINVAL;
1013
1014         err = nfsd_create_serv();
1015         if (err != 0)
1016                 return err;
1017
1018         err = svc_create_xprt(nfsd_serv, transport, &init_net,
1019                                 PF_INET, port, SVC_SOCK_ANONYMOUS);
1020         if (err < 0)
1021                 goto out_err;
1022
1023         err = svc_create_xprt(nfsd_serv, transport, &init_net,
1024                                 PF_INET6, port, SVC_SOCK_ANONYMOUS);
1025         if (err < 0 && err != -EAFNOSUPPORT)
1026                 goto out_close;
1027
1028         /* Decrease the count, but don't shut down the service */
1029         nfsd_serv->sv_nrthreads--;
1030         return 0;
1031 out_close:
1032         xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
1033         if (xprt != NULL) {
1034                 svc_close_xprt(xprt);
1035                 svc_xprt_put(xprt);
1036         }
1037 out_err:
1038         svc_destroy(nfsd_serv);
1039         return err;
1040 }
1041
1042 /*
1043  * A transport listener is removed by writing a "-", it's transport
1044  * name, and it's port number.
1045  */
1046 static ssize_t __write_ports_delxprt(char *buf)
1047 {
1048         struct svc_xprt *xprt;
1049         char transport[16];
1050         int port;
1051
1052         if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
1053                 return -EINVAL;
1054
1055         if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
1056                 return -EINVAL;
1057
1058         xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
1059         if (xprt == NULL)
1060                 return -ENOTCONN;
1061
1062         svc_close_xprt(xprt);
1063         svc_xprt_put(xprt);
1064         return 0;
1065 }
1066
1067 static ssize_t __write_ports(struct file *file, char *buf, size_t size)
1068 {
1069         if (size == 0)
1070                 return __write_ports_names(buf);
1071
1072         if (isdigit(buf[0]))
1073                 return __write_ports_addfd(buf);
1074
1075         if (buf[0] == '-' && isdigit(buf[1]))
1076                 return __write_ports_delfd(buf);
1077
1078         if (isalpha(buf[0]))
1079                 return __write_ports_addxprt(buf);
1080
1081         if (buf[0] == '-' && isalpha(buf[1]))
1082                 return __write_ports_delxprt(buf);
1083
1084         return -EINVAL;
1085 }
1086
1087 /**
1088  * write_ports - Pass a socket file descriptor or transport name to listen on
1089  *
1090  * Input:
1091  *                      buf:            ignored
1092  *                      size:           zero
1093  * Output:
1094  *      On success:     passed-in buffer filled with a '\n'-terminated C
1095  *                      string containing a whitespace-separated list of
1096  *                      named NFSD listeners;
1097  *                      return code is the size in bytes of the string
1098  *      On error:       return code is zero or a negative errno value
1099  *
1100  * OR
1101  *
1102  * Input:
1103  *                      buf:            C string containing an unsigned
1104  *                                      integer value representing a bound
1105  *                                      but unconnected socket that is to be
1106  *                                      used as an NFSD listener; listen(3)
1107  *                                      must be called for a SOCK_STREAM
1108  *                                      socket, otherwise it is ignored
1109  *                      size:           non-zero length of C string in @buf
1110  * Output:
1111  *      On success:     NFS service is started;
1112  *                      passed-in buffer filled with a '\n'-terminated C
1113  *                      string containing a unique alphanumeric name of
1114  *                      the listener;
1115  *                      return code is the size in bytes of the string
1116  *      On error:       return code is a negative errno value
1117  *
1118  * OR
1119  *
1120  * Input:
1121  *                      buf:            C string containing a "-" followed
1122  *                                      by an integer value representing a
1123  *                                      previously passed in socket file
1124  *                                      descriptor
1125  *                      size:           non-zero length of C string in @buf
1126  * Output:
1127  *      On success:     NFS service no longer listens on that socket;
1128  *                      passed-in buffer filled with a '\n'-terminated C
1129  *                      string containing a unique name of the listener;
1130  *                      return code is the size in bytes of the string
1131  *      On error:       return code is a negative errno value
1132  *
1133  * OR
1134  *
1135  * Input:
1136  *                      buf:            C string containing a transport
1137  *                                      name and an unsigned integer value
1138  *                                      representing the port to listen on,
1139  *                                      separated by whitespace
1140  *                      size:           non-zero length of C string in @buf
1141  * Output:
1142  *      On success:     returns zero; NFS service is started
1143  *      On error:       return code is a negative errno value
1144  *
1145  * OR
1146  *
1147  * Input:
1148  *                      buf:            C string containing a "-" followed
1149  *                                      by a transport name and an unsigned
1150  *                                      integer value representing the port
1151  *                                      to listen on, separated by whitespace
1152  *                      size:           non-zero length of C string in @buf
1153  * Output:
1154  *      On success:     returns zero; NFS service no longer listens
1155  *                      on that transport
1156  *      On error:       return code is a negative errno value
1157  */
1158 static ssize_t write_ports(struct file *file, char *buf, size_t size)
1159 {
1160         ssize_t rv;
1161
1162         mutex_lock(&nfsd_mutex);
1163         rv = __write_ports(file, buf, size);
1164         mutex_unlock(&nfsd_mutex);
1165         return rv;
1166 }
1167
1168
1169 int nfsd_max_blksize;
1170
1171 /**
1172  * write_maxblksize - Set or report the current NFS blksize
1173  *
1174  * Input:
1175  *                      buf:            ignored
1176  *                      size:           zero
1177  *
1178  * OR
1179  *
1180  * Input:
1181  *                      buf:            C string containing an unsigned
1182  *                                      integer value representing the new
1183  *                                      NFS blksize
1184  *                      size:           non-zero length of C string in @buf
1185  * Output:
1186  *      On success:     passed-in buffer filled with '\n'-terminated C string
1187  *                      containing numeric value of the current NFS blksize
1188  *                      setting;
1189  *                      return code is the size in bytes of the string
1190  *      On error:       return code is zero or a negative errno value
1191  */
1192 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
1193 {
1194         char *mesg = buf;
1195         if (size > 0) {
1196                 int bsize;
1197                 int rv = get_int(&mesg, &bsize);
1198                 if (rv)
1199                         return rv;
1200                 /* force bsize into allowed range and
1201                  * required alignment.
1202                  */
1203                 if (bsize < 1024)
1204                         bsize = 1024;
1205                 if (bsize > NFSSVC_MAXBLKSIZE)
1206                         bsize = NFSSVC_MAXBLKSIZE;
1207                 bsize &= ~(1024-1);
1208                 mutex_lock(&nfsd_mutex);
1209                 if (nfsd_serv) {
1210                         mutex_unlock(&nfsd_mutex);
1211                         return -EBUSY;
1212                 }
1213                 nfsd_max_blksize = bsize;
1214                 mutex_unlock(&nfsd_mutex);
1215         }
1216
1217         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
1218                                                         nfsd_max_blksize);
1219 }
1220
1221 #ifdef CONFIG_NFSD_V4
1222 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1223 {
1224         char *mesg = buf;
1225         int rv, i;
1226
1227         if (size > 0) {
1228                 if (nfsd_serv)
1229                         return -EBUSY;
1230                 rv = get_int(&mesg, &i);
1231                 if (rv)
1232                         return rv;
1233                 /*
1234                  * Some sanity checking.  We don't have a reason for
1235                  * these particular numbers, but problems with the
1236                  * extremes are:
1237                  *      - Too short: the briefest network outage may
1238                  *        cause clients to lose all their locks.  Also,
1239                  *        the frequent polling may be wasteful.
1240                  *      - Too long: do you really want reboot recovery
1241                  *        to take more than an hour?  Or to make other
1242                  *        clients wait an hour before being able to
1243                  *        revoke a dead client's locks?
1244                  */
1245                 if (i < 10 || i > 3600)
1246                         return -EINVAL;
1247                 *time = i;
1248         }
1249
1250         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
1251 }
1252
1253 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
1254 {
1255         ssize_t rv;
1256
1257         mutex_lock(&nfsd_mutex);
1258         rv = __nfsd4_write_time(file, buf, size, time);
1259         mutex_unlock(&nfsd_mutex);
1260         return rv;
1261 }
1262
1263 /**
1264  * write_leasetime - Set or report the current NFSv4 lease time
1265  *
1266  * Input:
1267  *                      buf:            ignored
1268  *                      size:           zero
1269  *
1270  * OR
1271  *
1272  * Input:
1273  *                      buf:            C string containing an unsigned
1274  *                                      integer value representing the new
1275  *                                      NFSv4 lease expiry time
1276  *                      size:           non-zero length of C string in @buf
1277  * Output:
1278  *      On success:     passed-in buffer filled with '\n'-terminated C
1279  *                      string containing unsigned integer value of the
1280  *                      current lease expiry time;
1281  *                      return code is the size in bytes of the string
1282  *      On error:       return code is zero or a negative errno value
1283  */
1284 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
1285 {
1286         return nfsd4_write_time(file, buf, size, &nfsd4_lease);
1287 }
1288
1289 /**
1290  * write_gracetime - Set or report current NFSv4 grace period time
1291  *
1292  * As above, but sets the time of the NFSv4 grace period.
1293  *
1294  * Note this should never be set to less than the *previous*
1295  * lease-period time, but we don't try to enforce this.  (In the common
1296  * case (a new boot), we don't know what the previous lease time was
1297  * anyway.)
1298  */
1299 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1300 {
1301         return nfsd4_write_time(file, buf, size, &nfsd4_grace);
1302 }
1303
1304 extern char *nfs4_recoverydir(void);
1305
1306 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
1307 {
1308         char *mesg = buf;
1309         char *recdir;
1310         int len, status;
1311
1312         if (size > 0) {
1313                 if (nfsd_serv)
1314                         return -EBUSY;
1315                 if (size > PATH_MAX || buf[size-1] != '\n')
1316                         return -EINVAL;
1317                 buf[size-1] = 0;
1318
1319                 recdir = mesg;
1320                 len = qword_get(&mesg, recdir, size);
1321                 if (len <= 0)
1322                         return -EINVAL;
1323
1324                 status = nfs4_reset_recoverydir(recdir);
1325                 if (status)
1326                         return status;
1327         }
1328
1329         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1330                                                         nfs4_recoverydir());
1331 }
1332
1333 /**
1334  * write_recoverydir - Set or report the pathname of the recovery directory
1335  *
1336  * Input:
1337  *                      buf:            ignored
1338  *                      size:           zero
1339  *
1340  * OR
1341  *
1342  * Input:
1343  *                      buf:            C string containing the pathname
1344  *                                      of the directory on a local file
1345  *                                      system containing permanent NFSv4
1346  *                                      recovery data
1347  *                      size:           non-zero length of C string in @buf
1348  * Output:
1349  *      On success:     passed-in buffer filled with '\n'-terminated C string
1350  *                      containing the current recovery pathname setting;
1351  *                      return code is the size in bytes of the string
1352  *      On error:       return code is zero or a negative errno value
1353  */
1354 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1355 {
1356         ssize_t rv;
1357
1358         mutex_lock(&nfsd_mutex);
1359         rv = __write_recoverydir(file, buf, size);
1360         mutex_unlock(&nfsd_mutex);
1361         return rv;
1362 }
1363
1364 #endif
1365
1366 /*----------------------------------------------------------------------------*/
1367 /*
1368  *      populating the filesystem.
1369  */
1370
1371 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1372 {
1373         static struct tree_descr nfsd_files[] = {
1374 #ifdef CONFIG_NFSD_DEPRECATED
1375                 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
1376                 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
1377                 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
1378                 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
1379                 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
1380                 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
1381                 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
1382 #endif
1383                 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
1384                 [NFSD_Export_features] = {"export_features",
1385                                         &export_features_operations, S_IRUGO},
1386                 [NFSD_FO_UnlockIP] = {"unlock_ip",
1387                                         &transaction_ops, S_IWUSR|S_IRUSR},
1388                 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1389                                         &transaction_ops, S_IWUSR|S_IRUSR},
1390                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1391                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1392                 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1393                 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1394                 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1395                 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1396                 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1397 #ifdef CONFIG_NFSD_V4
1398                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1399                 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1400                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1401 #endif
1402                 /* last one */ {""}
1403         };
1404         return simple_fill_super(sb, 0x6e667364, nfsd_files);
1405 }
1406
1407 static int nfsd_get_sb(struct file_system_type *fs_type,
1408         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1409 {
1410         return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
1411 }
1412
1413 static struct file_system_type nfsd_fs_type = {
1414         .owner          = THIS_MODULE,
1415         .name           = "nfsd",
1416         .get_sb         = nfsd_get_sb,
1417         .kill_sb        = kill_litter_super,
1418 };
1419
1420 #ifdef CONFIG_PROC_FS
1421 static int create_proc_exports_entry(void)
1422 {
1423         struct proc_dir_entry *entry;
1424
1425         entry = proc_mkdir("fs/nfs", NULL);
1426         if (!entry)
1427                 return -ENOMEM;
1428         entry = proc_create("exports", 0, entry, &exports_operations);
1429         if (!entry)
1430                 return -ENOMEM;
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 static int __init init_nfsd(void)
1441 {
1442         int retval;
1443         printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1444
1445         retval = nfs4_state_init(); /* nfs4 locking state */
1446         if (retval)
1447                 return retval;
1448         nfsd_stat_init();       /* Statistics */
1449         retval = nfsd_reply_cache_init();
1450         if (retval)
1451                 goto out_free_stat;
1452         retval = nfsd_export_init();
1453         if (retval)
1454                 goto out_free_cache;
1455         nfsd_lockd_init();      /* lockd->nfsd callbacks */
1456         retval = nfsd_idmap_init();
1457         if (retval)
1458                 goto out_free_lockd;
1459         retval = create_proc_exports_entry();
1460         if (retval)
1461                 goto out_free_idmap;
1462         retval = register_filesystem(&nfsd_fs_type);
1463         if (retval)
1464                 goto out_free_all;
1465         return 0;
1466 out_free_all:
1467         remove_proc_entry("fs/nfs/exports", NULL);
1468         remove_proc_entry("fs/nfs", NULL);
1469 out_free_idmap:
1470         nfsd_idmap_shutdown();
1471 out_free_lockd:
1472         nfsd_lockd_shutdown();
1473         nfsd_export_shutdown();
1474 out_free_cache:
1475         nfsd_reply_cache_shutdown();
1476 out_free_stat:
1477         nfsd_stat_shutdown();
1478         nfsd4_free_slabs();
1479         return retval;
1480 }
1481
1482 static void __exit exit_nfsd(void)
1483 {
1484         nfsd_export_shutdown();
1485         nfsd_reply_cache_shutdown();
1486         remove_proc_entry("fs/nfs/exports", NULL);
1487         remove_proc_entry("fs/nfs", NULL);
1488         nfsd_stat_shutdown();
1489         nfsd_lockd_shutdown();
1490         nfsd_idmap_shutdown();
1491         nfsd4_free_slabs();
1492         unregister_filesystem(&nfsd_fs_type);
1493 }
1494
1495 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1496 MODULE_LICENSE("GPL");
1497 module_init(init_nfsd)
1498 module_exit(exit_nfsd)