[media] s5p-fimc: Fail driver probing when sensor configuration is wrong
[pandora-kernel.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/sunrpc/svcsock.h>
12 #include <linux/lockd/lockd.h>
13 #include <linux/sunrpc/clnt.h>
14 #include <linux/sunrpc/gss_api.h>
15 #include <linux/sunrpc/gss_krb5_enctypes.h>
16
17 #include "idmap.h"
18 #include "nfsd.h"
19 #include "cache.h"
20
21 /*
22  *      We have a single directory with several nodes in it.
23  */
24 enum {
25         NFSD_Root = 1,
26         NFSD_List,
27         NFSD_Export_features,
28         NFSD_Fh,
29         NFSD_FO_UnlockIP,
30         NFSD_FO_UnlockFS,
31         NFSD_Threads,
32         NFSD_Pool_Threads,
33         NFSD_Pool_Stats,
34         NFSD_Versions,
35         NFSD_Ports,
36         NFSD_MaxBlkSize,
37         NFSD_SupportedEnctypes,
38         /*
39          * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
40          * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
41          */
42 #ifdef CONFIG_NFSD_V4
43         NFSD_Leasetime,
44         NFSD_Gracetime,
45         NFSD_RecoveryDir,
46 #endif
47 };
48
49 /*
50  * write() for these nodes.
51  */
52 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
53 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
54 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
55 static ssize_t write_threads(struct file *file, char *buf, size_t size);
56 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
57 static ssize_t write_versions(struct file *file, char *buf, size_t size);
58 static ssize_t write_ports(struct file *file, char *buf, size_t size);
59 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
60 #ifdef CONFIG_NFSD_V4
61 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
62 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
63 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
64 #endif
65
66 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
67         [NFSD_Fh] = write_filehandle,
68         [NFSD_FO_UnlockIP] = write_unlock_ip,
69         [NFSD_FO_UnlockFS] = write_unlock_fs,
70         [NFSD_Threads] = write_threads,
71         [NFSD_Pool_Threads] = write_pool_threads,
72         [NFSD_Versions] = write_versions,
73         [NFSD_Ports] = write_ports,
74         [NFSD_MaxBlkSize] = write_maxblksize,
75 #ifdef CONFIG_NFSD_V4
76         [NFSD_Leasetime] = write_leasetime,
77         [NFSD_Gracetime] = write_gracetime,
78         [NFSD_RecoveryDir] = write_recoverydir,
79 #endif
80 };
81
82 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
83 {
84         ino_t ino =  file->f_path.dentry->d_inode->i_ino;
85         char *data;
86         ssize_t rv;
87
88         if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
89                 return -EINVAL;
90
91         data = simple_transaction_get(file, buf, size);
92         if (IS_ERR(data))
93                 return PTR_ERR(data);
94
95         rv =  write_op[ino](file, data, size);
96         if (rv >= 0) {
97                 simple_transaction_set(file, rv);
98                 rv = size;
99         }
100         return rv;
101 }
102
103 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
104 {
105         if (! file->private_data) {
106                 /* An attempt to read a transaction file without writing
107                  * causes a 0-byte write so that the file can return
108                  * state information
109                  */
110                 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
111                 if (rv < 0)
112                         return rv;
113         }
114         return simple_transaction_read(file, buf, size, pos);
115 }
116
117 static const struct file_operations transaction_ops = {
118         .write          = nfsctl_transaction_write,
119         .read           = nfsctl_transaction_read,
120         .release        = simple_transaction_release,
121         .llseek         = default_llseek,
122 };
123
124 static int exports_open(struct inode *inode, struct file *file)
125 {
126         return seq_open(file, &nfs_exports_op);
127 }
128
129 static const struct file_operations exports_operations = {
130         .open           = exports_open,
131         .read           = seq_read,
132         .llseek         = seq_lseek,
133         .release        = seq_release,
134         .owner          = THIS_MODULE,
135 };
136
137 static int export_features_show(struct seq_file *m, void *v)
138 {
139         seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
140         return 0;
141 }
142
143 static int export_features_open(struct inode *inode, struct file *file)
144 {
145         return single_open(file, export_features_show, NULL);
146 }
147
148 static struct file_operations export_features_operations = {
149         .open           = export_features_open,
150         .read           = seq_read,
151         .llseek         = seq_lseek,
152         .release        = single_release,
153 };
154
155 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
156 static int supported_enctypes_show(struct seq_file *m, void *v)
157 {
158         seq_printf(m, KRB5_SUPPORTED_ENCTYPES);
159         return 0;
160 }
161
162 static int supported_enctypes_open(struct inode *inode, struct file *file)
163 {
164         return single_open(file, supported_enctypes_show, NULL);
165 }
166
167 static struct file_operations supported_enctypes_ops = {
168         .open           = supported_enctypes_open,
169         .read           = seq_read,
170         .llseek         = seq_lseek,
171         .release        = single_release,
172 };
173 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
174
175 extern int nfsd_pool_stats_open(struct inode *inode, struct file *file);
176 extern int nfsd_pool_stats_release(struct inode *inode, struct file *file);
177
178 static const struct file_operations pool_stats_operations = {
179         .open           = nfsd_pool_stats_open,
180         .read           = seq_read,
181         .llseek         = seq_lseek,
182         .release        = nfsd_pool_stats_release,
183         .owner          = THIS_MODULE,
184 };
185
186 /*----------------------------------------------------------------------------*/
187 /*
188  * payload - write methods
189  */
190
191
192 /**
193  * write_unlock_ip - Release all locks used by a client
194  *
195  * Experimental.
196  *
197  * Input:
198  *                      buf:    '\n'-terminated C string containing a
199  *                              presentation format IP address
200  *                      size:   length of C string in @buf
201  * Output:
202  *      On success:     returns zero if all specified locks were released;
203  *                      returns one if one or more locks were not released
204  *      On error:       return code is negative errno value
205  */
206 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
207 {
208         struct sockaddr_storage address;
209         struct sockaddr *sap = (struct sockaddr *)&address;
210         size_t salen = sizeof(address);
211         char *fo_path;
212
213         /* sanity check */
214         if (size == 0)
215                 return -EINVAL;
216
217         if (buf[size-1] != '\n')
218                 return -EINVAL;
219
220         fo_path = buf;
221         if (qword_get(&buf, fo_path, size) < 0)
222                 return -EINVAL;
223
224         if (rpc_pton(fo_path, size, sap, salen) == 0)
225                 return -EINVAL;
226
227         return nlmsvc_unlock_all_by_ip(sap);
228 }
229
230 /**
231  * write_unlock_fs - Release all locks on a local file system
232  *
233  * Experimental.
234  *
235  * Input:
236  *                      buf:    '\n'-terminated C string containing the
237  *                              absolute pathname of a local file system
238  *                      size:   length of C string in @buf
239  * Output:
240  *      On success:     returns zero if all specified locks were released;
241  *                      returns one if one or more locks were not released
242  *      On error:       return code is negative errno value
243  */
244 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
245 {
246         struct path path;
247         char *fo_path;
248         int error;
249
250         /* sanity check */
251         if (size == 0)
252                 return -EINVAL;
253
254         if (buf[size-1] != '\n')
255                 return -EINVAL;
256
257         fo_path = buf;
258         if (qword_get(&buf, fo_path, size) < 0)
259                 return -EINVAL;
260
261         error = kern_path(fo_path, 0, &path);
262         if (error)
263                 return error;
264
265         /*
266          * XXX: Needs better sanity checking.  Otherwise we could end up
267          * releasing locks on the wrong file system.
268          *
269          * For example:
270          * 1.  Does the path refer to a directory?
271          * 2.  Is that directory a mount point, or
272          * 3.  Is that directory the root of an exported file system?
273          */
274         error = nlmsvc_unlock_all_by_sb(path.mnt->mnt_sb);
275
276         path_put(&path);
277         return error;
278 }
279
280 /**
281  * write_filehandle - Get a variable-length NFS file handle by path
282  *
283  * On input, the buffer contains a '\n'-terminated C string comprised of
284  * three alphanumeric words separated by whitespace.  The string may
285  * contain escape sequences.
286  *
287  * Input:
288  *                      buf:
289  *                              domain:         client domain name
290  *                              path:           export pathname
291  *                              maxsize:        numeric maximum size of
292  *                                              @buf
293  *                      size:   length of C string in @buf
294  * Output:
295  *      On success:     passed-in buffer filled with '\n'-terminated C
296  *                      string containing a ASCII hex text version
297  *                      of the NFS file handle;
298  *                      return code is the size in bytes of the string
299  *      On error:       return code is negative errno value
300  */
301 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
302 {
303         char *dname, *path;
304         int uninitialized_var(maxsize);
305         char *mesg = buf;
306         int len;
307         struct auth_domain *dom;
308         struct knfsd_fh fh;
309
310         if (size == 0)
311                 return -EINVAL;
312
313         if (buf[size-1] != '\n')
314                 return -EINVAL;
315         buf[size-1] = 0;
316
317         dname = mesg;
318         len = qword_get(&mesg, dname, size);
319         if (len <= 0)
320                 return -EINVAL;
321         
322         path = dname+len+1;
323         len = qword_get(&mesg, path, size);
324         if (len <= 0)
325                 return -EINVAL;
326
327         len = get_int(&mesg, &maxsize);
328         if (len)
329                 return len;
330
331         if (maxsize < NFS_FHSIZE)
332                 return -EINVAL;
333         if (maxsize > NFS3_FHSIZE)
334                 maxsize = NFS3_FHSIZE;
335
336         if (qword_get(&mesg, mesg, size)>0)
337                 return -EINVAL;
338
339         /* we have all the words, they are in buf.. */
340         dom = unix_domain_find(dname);
341         if (!dom)
342                 return -ENOMEM;
343
344         len = exp_rootfh(dom, path, &fh,  maxsize);
345         auth_domain_put(dom);
346         if (len)
347                 return len;
348         
349         mesg = buf;
350         len = SIMPLE_TRANSACTION_LIMIT;
351         qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
352         mesg[-1] = '\n';
353         return mesg - buf;      
354 }
355
356 /**
357  * write_threads - Start NFSD, or report the current number of running threads
358  *
359  * Input:
360  *                      buf:            ignored
361  *                      size:           zero
362  * Output:
363  *      On success:     passed-in buffer filled with '\n'-terminated C
364  *                      string numeric value representing the number of
365  *                      running NFSD threads;
366  *                      return code is the size in bytes of the string
367  *      On error:       return code is zero
368  *
369  * OR
370  *
371  * Input:
372  *                      buf:            C string containing an unsigned
373  *                                      integer value representing the
374  *                                      number of NFSD threads to start
375  *                      size:           non-zero length of C string in @buf
376  * Output:
377  *      On success:     NFS service is started;
378  *                      passed-in buffer filled with '\n'-terminated C
379  *                      string numeric value representing the number of
380  *                      running NFSD threads;
381  *                      return code is the size in bytes of the string
382  *      On error:       return code is zero or a negative errno value
383  */
384 static ssize_t write_threads(struct file *file, char *buf, size_t size)
385 {
386         char *mesg = buf;
387         int rv;
388         if (size > 0) {
389                 int newthreads;
390                 rv = get_int(&mesg, &newthreads);
391                 if (rv)
392                         return rv;
393                 if (newthreads < 0)
394                         return -EINVAL;
395                 rv = nfsd_svc(NFS_PORT, newthreads);
396                 if (rv < 0)
397                         return rv;
398         } else
399                 rv = nfsd_nrthreads();
400
401         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
402 }
403
404 /**
405  * write_pool_threads - Set or report the current number of threads per pool
406  *
407  * Input:
408  *                      buf:            ignored
409  *                      size:           zero
410  *
411  * OR
412  *
413  * Input:
414  *                      buf:            C string containing whitespace-
415  *                                      separated unsigned integer values
416  *                                      representing the number of NFSD
417  *                                      threads to start in each pool
418  *                      size:           non-zero length of C string in @buf
419  * Output:
420  *      On success:     passed-in buffer filled with '\n'-terminated C
421  *                      string containing integer values representing the
422  *                      number of NFSD threads in each pool;
423  *                      return code is the size in bytes of the string
424  *      On error:       return code is zero or a negative errno value
425  */
426 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
427 {
428         /* if size > 0, look for an array of number of threads per node
429          * and apply them  then write out number of threads per node as reply
430          */
431         char *mesg = buf;
432         int i;
433         int rv;
434         int len;
435         int npools;
436         int *nthreads;
437
438         mutex_lock(&nfsd_mutex);
439         npools = nfsd_nrpools();
440         if (npools == 0) {
441                 /*
442                  * NFS is shut down.  The admin can start it by
443                  * writing to the threads file but NOT the pool_threads
444                  * file, sorry.  Report zero threads.
445                  */
446                 mutex_unlock(&nfsd_mutex);
447                 strcpy(buf, "0\n");
448                 return strlen(buf);
449         }
450
451         nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
452         rv = -ENOMEM;
453         if (nthreads == NULL)
454                 goto out_free;
455
456         if (size > 0) {
457                 for (i = 0; i < npools; i++) {
458                         rv = get_int(&mesg, &nthreads[i]);
459                         if (rv == -ENOENT)
460                                 break;          /* fewer numbers than pools */
461                         if (rv)
462                                 goto out_free;  /* syntax error */
463                         rv = -EINVAL;
464                         if (nthreads[i] < 0)
465                                 goto out_free;
466                 }
467                 rv = nfsd_set_nrthreads(i, nthreads);
468                 if (rv)
469                         goto out_free;
470         }
471
472         rv = nfsd_get_nrthreads(npools, nthreads);
473         if (rv)
474                 goto out_free;
475
476         mesg = buf;
477         size = SIMPLE_TRANSACTION_LIMIT;
478         for (i = 0; i < npools && size > 0; i++) {
479                 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
480                 len = strlen(mesg);
481                 size -= len;
482                 mesg += len;
483         }
484         rv = mesg - buf;
485 out_free:
486         kfree(nthreads);
487         mutex_unlock(&nfsd_mutex);
488         return rv;
489 }
490
491 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
492 {
493         char *mesg = buf;
494         char *vers, *minorp, sign;
495         int len, num, remaining;
496         unsigned minor;
497         ssize_t tlen = 0;
498         char *sep;
499
500         if (size>0) {
501                 if (nfsd_serv)
502                         /* Cannot change versions without updating
503                          * nfsd_serv->sv_xdrsize, and reallocing
504                          * rq_argp and rq_resp
505                          */
506                         return -EBUSY;
507                 if (buf[size-1] != '\n')
508                         return -EINVAL;
509                 buf[size-1] = 0;
510
511                 vers = mesg;
512                 len = qword_get(&mesg, vers, size);
513                 if (len <= 0) return -EINVAL;
514                 do {
515                         sign = *vers;
516                         if (sign == '+' || sign == '-')
517                                 num = simple_strtol((vers+1), &minorp, 0);
518                         else
519                                 num = simple_strtol(vers, &minorp, 0);
520                         if (*minorp == '.') {
521                                 if (num < 4)
522                                         return -EINVAL;
523                                 minor = simple_strtoul(minorp+1, NULL, 0);
524                                 if (minor == 0)
525                                         return -EINVAL;
526                                 if (nfsd_minorversion(minor, sign == '-' ?
527                                                      NFSD_CLEAR : NFSD_SET) < 0)
528                                         return -EINVAL;
529                                 goto next;
530                         }
531                         switch(num) {
532                         case 2:
533                         case 3:
534                         case 4:
535                                 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
536                                 break;
537                         default:
538                                 return -EINVAL;
539                         }
540                 next:
541                         vers += len + 1;
542                 } while ((len = qword_get(&mesg, vers, size)) > 0);
543                 /* If all get turned off, turn them back on, as
544                  * having no versions is BAD
545                  */
546                 nfsd_reset_versions();
547         }
548
549         /* Now write current state into reply buffer */
550         len = 0;
551         sep = "";
552         remaining = SIMPLE_TRANSACTION_LIMIT;
553         for (num=2 ; num <= 4 ; num++)
554                 if (nfsd_vers(num, NFSD_AVAIL)) {
555                         len = snprintf(buf, remaining, "%s%c%d", sep,
556                                        nfsd_vers(num, NFSD_TEST)?'+':'-',
557                                        num);
558                         sep = " ";
559
560                         if (len > remaining)
561                                 break;
562                         remaining -= len;
563                         buf += len;
564                         tlen += len;
565                 }
566         if (nfsd_vers(4, NFSD_AVAIL))
567                 for (minor = 1; minor <= NFSD_SUPPORTED_MINOR_VERSION;
568                      minor++) {
569                         len = snprintf(buf, remaining, " %c4.%u",
570                                         (nfsd_vers(4, NFSD_TEST) &&
571                                          nfsd_minorversion(minor, NFSD_TEST)) ?
572                                                 '+' : '-',
573                                         minor);
574
575                         if (len > remaining)
576                                 break;
577                         remaining -= len;
578                         buf += len;
579                         tlen += len;
580                 }
581
582         len = snprintf(buf, remaining, "\n");
583         if (len > remaining)
584                 return -EINVAL;
585         return tlen + len;
586 }
587
588 /**
589  * write_versions - Set or report the available NFS protocol versions
590  *
591  * Input:
592  *                      buf:            ignored
593  *                      size:           zero
594  * Output:
595  *      On success:     passed-in buffer filled with '\n'-terminated C
596  *                      string containing positive or negative integer
597  *                      values representing the current status of each
598  *                      protocol version;
599  *                      return code is the size in bytes of the string
600  *      On error:       return code is zero or a negative errno value
601  *
602  * OR
603  *
604  * Input:
605  *                      buf:            C string containing whitespace-
606  *                                      separated positive or negative
607  *                                      integer values representing NFS
608  *                                      protocol versions to enable ("+n")
609  *                                      or disable ("-n")
610  *                      size:           non-zero length of C string in @buf
611  * Output:
612  *      On success:     status of zero or more protocol versions has
613  *                      been updated; passed-in buffer filled with
614  *                      '\n'-terminated C string containing positive
615  *                      or negative integer values representing the
616  *                      current status of each protocol version;
617  *                      return code is the size in bytes of the string
618  *      On error:       return code is zero or a negative errno value
619  */
620 static ssize_t write_versions(struct file *file, char *buf, size_t size)
621 {
622         ssize_t rv;
623
624         mutex_lock(&nfsd_mutex);
625         rv = __write_versions(file, buf, size);
626         mutex_unlock(&nfsd_mutex);
627         return rv;
628 }
629
630 /*
631  * Zero-length write.  Return a list of NFSD's current listener
632  * transports.
633  */
634 static ssize_t __write_ports_names(char *buf)
635 {
636         if (nfsd_serv == NULL)
637                 return 0;
638         return svc_xprt_names(nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
639 }
640
641 /*
642  * A single 'fd' number was written, in which case it must be for
643  * a socket of a supported family/protocol, and we use it as an
644  * nfsd listener.
645  */
646 static ssize_t __write_ports_addfd(char *buf)
647 {
648         char *mesg = buf;
649         int fd, err;
650
651         err = get_int(&mesg, &fd);
652         if (err != 0 || fd < 0)
653                 return -EINVAL;
654
655         err = nfsd_create_serv();
656         if (err != 0)
657                 return err;
658
659         err = svc_addsock(nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT);
660         if (err < 0) {
661                 svc_destroy(nfsd_serv);
662                 return err;
663         }
664
665         /* Decrease the count, but don't shut down the service */
666         nfsd_serv->sv_nrthreads--;
667         return err;
668 }
669
670 /*
671  * A '-' followed by the 'name' of a socket means we close the socket.
672  */
673 static ssize_t __write_ports_delfd(char *buf)
674 {
675         char *toclose;
676         int len = 0;
677
678         toclose = kstrdup(buf + 1, GFP_KERNEL);
679         if (toclose == NULL)
680                 return -ENOMEM;
681
682         if (nfsd_serv != NULL)
683                 len = svc_sock_names(nfsd_serv, buf,
684                                         SIMPLE_TRANSACTION_LIMIT, toclose);
685         kfree(toclose);
686         return len;
687 }
688
689 /*
690  * A transport listener is added by writing it's transport name and
691  * a port number.
692  */
693 static ssize_t __write_ports_addxprt(char *buf)
694 {
695         char transport[16];
696         struct svc_xprt *xprt;
697         int port, err;
698
699         if (sscanf(buf, "%15s %4u", transport, &port) != 2)
700                 return -EINVAL;
701
702         if (port < 1 || port > USHRT_MAX)
703                 return -EINVAL;
704
705         err = nfsd_create_serv();
706         if (err != 0)
707                 return err;
708
709         err = svc_create_xprt(nfsd_serv, transport, &init_net,
710                                 PF_INET, port, SVC_SOCK_ANONYMOUS);
711         if (err < 0)
712                 goto out_err;
713
714         err = svc_create_xprt(nfsd_serv, transport, &init_net,
715                                 PF_INET6, port, SVC_SOCK_ANONYMOUS);
716         if (err < 0 && err != -EAFNOSUPPORT)
717                 goto out_close;
718
719         /* Decrease the count, but don't shut down the service */
720         nfsd_serv->sv_nrthreads--;
721         return 0;
722 out_close:
723         xprt = svc_find_xprt(nfsd_serv, transport, PF_INET, port);
724         if (xprt != NULL) {
725                 svc_close_xprt(xprt);
726                 svc_xprt_put(xprt);
727         }
728 out_err:
729         svc_destroy(nfsd_serv);
730         return err;
731 }
732
733 /*
734  * A transport listener is removed by writing a "-", it's transport
735  * name, and it's port number.
736  */
737 static ssize_t __write_ports_delxprt(char *buf)
738 {
739         struct svc_xprt *xprt;
740         char transport[16];
741         int port;
742
743         if (sscanf(&buf[1], "%15s %4u", transport, &port) != 2)
744                 return -EINVAL;
745
746         if (port < 1 || port > USHRT_MAX || nfsd_serv == NULL)
747                 return -EINVAL;
748
749         xprt = svc_find_xprt(nfsd_serv, transport, AF_UNSPEC, port);
750         if (xprt == NULL)
751                 return -ENOTCONN;
752
753         svc_close_xprt(xprt);
754         svc_xprt_put(xprt);
755         return 0;
756 }
757
758 static ssize_t __write_ports(struct file *file, char *buf, size_t size)
759 {
760         if (size == 0)
761                 return __write_ports_names(buf);
762
763         if (isdigit(buf[0]))
764                 return __write_ports_addfd(buf);
765
766         if (buf[0] == '-' && isdigit(buf[1]))
767                 return __write_ports_delfd(buf);
768
769         if (isalpha(buf[0]))
770                 return __write_ports_addxprt(buf);
771
772         if (buf[0] == '-' && isalpha(buf[1]))
773                 return __write_ports_delxprt(buf);
774
775         return -EINVAL;
776 }
777
778 /**
779  * write_ports - Pass a socket file descriptor or transport name to listen on
780  *
781  * Input:
782  *                      buf:            ignored
783  *                      size:           zero
784  * Output:
785  *      On success:     passed-in buffer filled with a '\n'-terminated C
786  *                      string containing a whitespace-separated list of
787  *                      named NFSD listeners;
788  *                      return code is the size in bytes of the string
789  *      On error:       return code is zero or a negative errno value
790  *
791  * OR
792  *
793  * Input:
794  *                      buf:            C string containing an unsigned
795  *                                      integer value representing a bound
796  *                                      but unconnected socket that is to be
797  *                                      used as an NFSD listener; listen(3)
798  *                                      must be called for a SOCK_STREAM
799  *                                      socket, otherwise it is ignored
800  *                      size:           non-zero length of C string in @buf
801  * Output:
802  *      On success:     NFS service is started;
803  *                      passed-in buffer filled with a '\n'-terminated C
804  *                      string containing a unique alphanumeric name of
805  *                      the listener;
806  *                      return code is the size in bytes of the string
807  *      On error:       return code is a negative errno value
808  *
809  * OR
810  *
811  * Input:
812  *                      buf:            C string containing a "-" followed
813  *                                      by an integer value representing a
814  *                                      previously passed in socket file
815  *                                      descriptor
816  *                      size:           non-zero length of C string in @buf
817  * Output:
818  *      On success:     NFS service no longer listens on that socket;
819  *                      passed-in buffer filled with a '\n'-terminated C
820  *                      string containing a unique name of the listener;
821  *                      return code is the size in bytes of the string
822  *      On error:       return code is a negative errno value
823  *
824  * OR
825  *
826  * Input:
827  *                      buf:            C string containing a transport
828  *                                      name and an unsigned integer value
829  *                                      representing the port to listen on,
830  *                                      separated by whitespace
831  *                      size:           non-zero length of C string in @buf
832  * Output:
833  *      On success:     returns zero; NFS service is started
834  *      On error:       return code is a negative errno value
835  *
836  * OR
837  *
838  * Input:
839  *                      buf:            C string containing a "-" followed
840  *                                      by a transport name and an unsigned
841  *                                      integer value representing the port
842  *                                      to listen on, separated by whitespace
843  *                      size:           non-zero length of C string in @buf
844  * Output:
845  *      On success:     returns zero; NFS service no longer listens
846  *                      on that transport
847  *      On error:       return code is a negative errno value
848  */
849 static ssize_t write_ports(struct file *file, char *buf, size_t size)
850 {
851         ssize_t rv;
852
853         mutex_lock(&nfsd_mutex);
854         rv = __write_ports(file, buf, size);
855         mutex_unlock(&nfsd_mutex);
856         return rv;
857 }
858
859
860 int nfsd_max_blksize;
861
862 /**
863  * write_maxblksize - Set or report the current NFS blksize
864  *
865  * Input:
866  *                      buf:            ignored
867  *                      size:           zero
868  *
869  * OR
870  *
871  * Input:
872  *                      buf:            C string containing an unsigned
873  *                                      integer value representing the new
874  *                                      NFS blksize
875  *                      size:           non-zero length of C string in @buf
876  * Output:
877  *      On success:     passed-in buffer filled with '\n'-terminated C string
878  *                      containing numeric value of the current NFS blksize
879  *                      setting;
880  *                      return code is the size in bytes of the string
881  *      On error:       return code is zero or a negative errno value
882  */
883 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
884 {
885         char *mesg = buf;
886         if (size > 0) {
887                 int bsize;
888                 int rv = get_int(&mesg, &bsize);
889                 if (rv)
890                         return rv;
891                 /* force bsize into allowed range and
892                  * required alignment.
893                  */
894                 if (bsize < 1024)
895                         bsize = 1024;
896                 if (bsize > NFSSVC_MAXBLKSIZE)
897                         bsize = NFSSVC_MAXBLKSIZE;
898                 bsize &= ~(1024-1);
899                 mutex_lock(&nfsd_mutex);
900                 if (nfsd_serv) {
901                         mutex_unlock(&nfsd_mutex);
902                         return -EBUSY;
903                 }
904                 nfsd_max_blksize = bsize;
905                 mutex_unlock(&nfsd_mutex);
906         }
907
908         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
909                                                         nfsd_max_blksize);
910 }
911
912 #ifdef CONFIG_NFSD_V4
913 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
914 {
915         char *mesg = buf;
916         int rv, i;
917
918         if (size > 0) {
919                 if (nfsd_serv)
920                         return -EBUSY;
921                 rv = get_int(&mesg, &i);
922                 if (rv)
923                         return rv;
924                 /*
925                  * Some sanity checking.  We don't have a reason for
926                  * these particular numbers, but problems with the
927                  * extremes are:
928                  *      - Too short: the briefest network outage may
929                  *        cause clients to lose all their locks.  Also,
930                  *        the frequent polling may be wasteful.
931                  *      - Too long: do you really want reboot recovery
932                  *        to take more than an hour?  Or to make other
933                  *        clients wait an hour before being able to
934                  *        revoke a dead client's locks?
935                  */
936                 if (i < 10 || i > 3600)
937                         return -EINVAL;
938                 *time = i;
939         }
940
941         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time);
942 }
943
944 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, time_t *time)
945 {
946         ssize_t rv;
947
948         mutex_lock(&nfsd_mutex);
949         rv = __nfsd4_write_time(file, buf, size, time);
950         mutex_unlock(&nfsd_mutex);
951         return rv;
952 }
953
954 /**
955  * write_leasetime - Set or report the current NFSv4 lease time
956  *
957  * Input:
958  *                      buf:            ignored
959  *                      size:           zero
960  *
961  * OR
962  *
963  * Input:
964  *                      buf:            C string containing an unsigned
965  *                                      integer value representing the new
966  *                                      NFSv4 lease expiry time
967  *                      size:           non-zero length of C string in @buf
968  * Output:
969  *      On success:     passed-in buffer filled with '\n'-terminated C
970  *                      string containing unsigned integer value of the
971  *                      current lease expiry time;
972  *                      return code is the size in bytes of the string
973  *      On error:       return code is zero or a negative errno value
974  */
975 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
976 {
977         return nfsd4_write_time(file, buf, size, &nfsd4_lease);
978 }
979
980 /**
981  * write_gracetime - Set or report current NFSv4 grace period time
982  *
983  * As above, but sets the time of the NFSv4 grace period.
984  *
985  * Note this should never be set to less than the *previous*
986  * lease-period time, but we don't try to enforce this.  (In the common
987  * case (a new boot), we don't know what the previous lease time was
988  * anyway.)
989  */
990 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
991 {
992         return nfsd4_write_time(file, buf, size, &nfsd4_grace);
993 }
994
995 extern char *nfs4_recoverydir(void);
996
997 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size)
998 {
999         char *mesg = buf;
1000         char *recdir;
1001         int len, status;
1002
1003         if (size > 0) {
1004                 if (nfsd_serv)
1005                         return -EBUSY;
1006                 if (size > PATH_MAX || buf[size-1] != '\n')
1007                         return -EINVAL;
1008                 buf[size-1] = 0;
1009
1010                 recdir = mesg;
1011                 len = qword_get(&mesg, recdir, size);
1012                 if (len <= 0)
1013                         return -EINVAL;
1014
1015                 status = nfs4_reset_recoverydir(recdir);
1016                 if (status)
1017                         return status;
1018         }
1019
1020         return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1021                                                         nfs4_recoverydir());
1022 }
1023
1024 /**
1025  * write_recoverydir - Set or report the pathname of the recovery directory
1026  *
1027  * Input:
1028  *                      buf:            ignored
1029  *                      size:           zero
1030  *
1031  * OR
1032  *
1033  * Input:
1034  *                      buf:            C string containing the pathname
1035  *                                      of the directory on a local file
1036  *                                      system containing permanent NFSv4
1037  *                                      recovery data
1038  *                      size:           non-zero length of C string in @buf
1039  * Output:
1040  *      On success:     passed-in buffer filled with '\n'-terminated C string
1041  *                      containing the current recovery pathname setting;
1042  *                      return code is the size in bytes of the string
1043  *      On error:       return code is zero or a negative errno value
1044  */
1045 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1046 {
1047         ssize_t rv;
1048
1049         mutex_lock(&nfsd_mutex);
1050         rv = __write_recoverydir(file, buf, size);
1051         mutex_unlock(&nfsd_mutex);
1052         return rv;
1053 }
1054
1055 #endif
1056
1057 /*----------------------------------------------------------------------------*/
1058 /*
1059  *      populating the filesystem.
1060  */
1061
1062 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
1063 {
1064         static struct tree_descr nfsd_files[] = {
1065                 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
1066                 [NFSD_Export_features] = {"export_features",
1067                                         &export_features_operations, S_IRUGO},
1068                 [NFSD_FO_UnlockIP] = {"unlock_ip",
1069                                         &transaction_ops, S_IWUSR|S_IRUSR},
1070                 [NFSD_FO_UnlockFS] = {"unlock_filesystem",
1071                                         &transaction_ops, S_IWUSR|S_IRUSR},
1072                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1073                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1074                 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1075                 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1076                 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1077                 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1078                 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1079 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE)
1080                 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO},
1081 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */
1082 #ifdef CONFIG_NFSD_V4
1083                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1084                 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1085                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1086 #endif
1087                 /* last one */ {""}
1088         };
1089         return simple_fill_super(sb, 0x6e667364, nfsd_files);
1090 }
1091
1092 static struct dentry *nfsd_mount(struct file_system_type *fs_type,
1093         int flags, const char *dev_name, void *data)
1094 {
1095         return mount_single(fs_type, flags, data, nfsd_fill_super);
1096 }
1097
1098 static struct file_system_type nfsd_fs_type = {
1099         .owner          = THIS_MODULE,
1100         .name           = "nfsd",
1101         .mount          = nfsd_mount,
1102         .kill_sb        = kill_litter_super,
1103 };
1104
1105 #ifdef CONFIG_PROC_FS
1106 static int create_proc_exports_entry(void)
1107 {
1108         struct proc_dir_entry *entry;
1109
1110         entry = proc_mkdir("fs/nfs", NULL);
1111         if (!entry)
1112                 return -ENOMEM;
1113         entry = proc_create("exports", 0, entry, &exports_operations);
1114         if (!entry)
1115                 return -ENOMEM;
1116         return 0;
1117 }
1118 #else /* CONFIG_PROC_FS */
1119 static int create_proc_exports_entry(void)
1120 {
1121         return 0;
1122 }
1123 #endif
1124
1125 static int __init init_nfsd(void)
1126 {
1127         int retval;
1128         printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
1129
1130         retval = nfs4_state_init(); /* nfs4 locking state */
1131         if (retval)
1132                 return retval;
1133         nfsd_stat_init();       /* Statistics */
1134         retval = nfsd_reply_cache_init();
1135         if (retval)
1136                 goto out_free_stat;
1137         retval = nfsd_export_init();
1138         if (retval)
1139                 goto out_free_cache;
1140         nfsd_lockd_init();      /* lockd->nfsd callbacks */
1141         retval = nfsd_idmap_init();
1142         if (retval)
1143                 goto out_free_lockd;
1144         retval = create_proc_exports_entry();
1145         if (retval)
1146                 goto out_free_idmap;
1147         retval = register_filesystem(&nfsd_fs_type);
1148         if (retval)
1149                 goto out_free_all;
1150         return 0;
1151 out_free_all:
1152         remove_proc_entry("fs/nfs/exports", NULL);
1153         remove_proc_entry("fs/nfs", NULL);
1154 out_free_idmap:
1155         nfsd_idmap_shutdown();
1156 out_free_lockd:
1157         nfsd_lockd_shutdown();
1158         nfsd_export_shutdown();
1159 out_free_cache:
1160         nfsd_reply_cache_shutdown();
1161 out_free_stat:
1162         nfsd_stat_shutdown();
1163         nfsd4_free_slabs();
1164         return retval;
1165 }
1166
1167 static void __exit exit_nfsd(void)
1168 {
1169         nfsd_export_shutdown();
1170         nfsd_reply_cache_shutdown();
1171         remove_proc_entry("fs/nfs/exports", NULL);
1172         remove_proc_entry("fs/nfs", NULL);
1173         nfsd_stat_shutdown();
1174         nfsd_lockd_shutdown();
1175         nfsd_idmap_shutdown();
1176         nfsd4_free_slabs();
1177         unregister_filesystem(&nfsd_fs_type);
1178 }
1179
1180 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1181 MODULE_LICENSE("GPL");
1182 module_init(init_nfsd)
1183 module_exit(exit_nfsd)