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