Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / 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/config.h>
10 #include <linux/module.h>
11
12 #include <linux/linkage.h>
13 #include <linux/time.h>
14 #include <linux/errno.h>
15 #include <linux/fs.h>
16 #include <linux/fcntl.h>
17 #include <linux/net.h>
18 #include <linux/in.h>
19 #include <linux/syscalls.h>
20 #include <linux/unistd.h>
21 #include <linux/slab.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
24 #include <linux/pagemap.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27
28 #include <linux/nfs.h>
29 #include <linux/nfsd_idmap.h>
30 #include <linux/sunrpc/svc.h>
31 #include <linux/nfsd/nfsd.h>
32 #include <linux/nfsd/cache.h>
33 #include <linux/nfsd/xdr.h>
34 #include <linux/nfsd/syscall.h>
35 #include <linux/nfsd/interface.h>
36
37 #include <asm/uaccess.h>
38
39 int nfsd_port = 2049;
40 unsigned int nfsd_portbits = 0;
41 unsigned int nfsd_versbits = ~0;
42
43 /*
44  *      We have a single directory with 9 nodes in it.
45  */
46 enum {
47         NFSD_Root = 1,
48         NFSD_Svc,
49         NFSD_Add,
50         NFSD_Del,
51         NFSD_Export,
52         NFSD_Unexport,
53         NFSD_Getfd,
54         NFSD_Getfs,
55         NFSD_List,
56         NFSD_Fh,
57         NFSD_Threads,
58         NFSD_Versions,
59         NFSD_Ports,
60         /*
61          * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
62          * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
63          */
64 #ifdef CONFIG_NFSD_V4
65         NFSD_Leasetime,
66         NFSD_RecoveryDir,
67 #endif
68 };
69
70 /*
71  * write() for these nodes.
72  */
73 static ssize_t write_svc(struct file *file, char *buf, size_t size);
74 static ssize_t write_add(struct file *file, char *buf, size_t size);
75 static ssize_t write_del(struct file *file, char *buf, size_t size);
76 static ssize_t write_export(struct file *file, char *buf, size_t size);
77 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
78 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
79 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
80 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
81 static ssize_t write_threads(struct file *file, char *buf, size_t size);
82 static ssize_t write_versions(struct file *file, char *buf, size_t size);
83 static ssize_t write_ports(struct file *file, char *buf, size_t size);
84 #ifdef CONFIG_NFSD_V4
85 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
86 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
87 #endif
88
89 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
90         [NFSD_Svc] = write_svc,
91         [NFSD_Add] = write_add,
92         [NFSD_Del] = write_del,
93         [NFSD_Export] = write_export,
94         [NFSD_Unexport] = write_unexport,
95         [NFSD_Getfd] = write_getfd,
96         [NFSD_Getfs] = write_getfs,
97         [NFSD_Fh] = write_filehandle,
98         [NFSD_Threads] = write_threads,
99         [NFSD_Versions] = write_versions,
100         [NFSD_Ports] = write_ports,
101 #ifdef CONFIG_NFSD_V4
102         [NFSD_Leasetime] = write_leasetime,
103         [NFSD_RecoveryDir] = write_recoverydir,
104 #endif
105 };
106
107 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
108 {
109         ino_t ino =  file->f_dentry->d_inode->i_ino;
110         char *data;
111         ssize_t rv;
112
113         if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
114                 return -EINVAL;
115
116         data = simple_transaction_get(file, buf, size);
117         if (IS_ERR(data))
118                 return PTR_ERR(data);
119
120         rv =  write_op[ino](file, data, size);
121         if (rv>0) {
122                 simple_transaction_set(file, rv);
123                 rv = size;
124         }
125         return rv;
126 }
127
128 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
129 {
130         if (! file->private_data) {
131                 /* An attempt to read a transaction file without writing
132                  * causes a 0-byte write so that the file can return
133                  * state information
134                  */
135                 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
136                 if (rv < 0)
137                         return rv;
138         }
139         return simple_transaction_read(file, buf, size, pos);
140 }
141
142 static const struct file_operations transaction_ops = {
143         .write          = nfsctl_transaction_write,
144         .read           = nfsctl_transaction_read,
145         .release        = simple_transaction_release,
146 };
147
148 extern struct seq_operations nfs_exports_op;
149 static int exports_open(struct inode *inode, struct file *file)
150 {
151         return seq_open(file, &nfs_exports_op);
152 }
153
154 static const struct file_operations exports_operations = {
155         .open           = exports_open,
156         .read           = seq_read,
157         .llseek         = seq_lseek,
158         .release        = seq_release,
159 };
160
161 /*----------------------------------------------------------------------------*/
162 /*
163  * payload - write methods
164  * If the method has a response, the response should be put in buf,
165  * and the length returned.  Otherwise return 0 or and -error.
166  */
167
168 static ssize_t write_svc(struct file *file, char *buf, size_t size)
169 {
170         struct nfsctl_svc *data;
171         if (size < sizeof(*data))
172                 return -EINVAL;
173         data = (struct nfsctl_svc*) buf;
174         return nfsd_svc(data->svc_port, data->svc_nthreads);
175 }
176
177 static ssize_t write_add(struct file *file, char *buf, size_t size)
178 {
179         struct nfsctl_client *data;
180         if (size < sizeof(*data))
181                 return -EINVAL;
182         data = (struct nfsctl_client *)buf;
183         return exp_addclient(data);
184 }
185
186 static ssize_t write_del(struct file *file, char *buf, size_t size)
187 {
188         struct nfsctl_client *data;
189         if (size < sizeof(*data))
190                 return -EINVAL;
191         data = (struct nfsctl_client *)buf;
192         return exp_delclient(data);
193 }
194
195 static ssize_t write_export(struct file *file, char *buf, size_t size)
196 {
197         struct nfsctl_export *data;
198         if (size < sizeof(*data))
199                 return -EINVAL;
200         data = (struct nfsctl_export*)buf;
201         return exp_export(data);
202 }
203
204 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
205 {
206         struct nfsctl_export *data;
207
208         if (size < sizeof(*data))
209                 return -EINVAL;
210         data = (struct nfsctl_export*)buf;
211         return exp_unexport(data);
212 }
213
214 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
215 {
216         struct nfsctl_fsparm *data;
217         struct sockaddr_in *sin;
218         struct auth_domain *clp;
219         int err = 0;
220         struct knfsd_fh *res;
221
222         if (size < sizeof(*data))
223                 return -EINVAL;
224         data = (struct nfsctl_fsparm*)buf;
225         err = -EPROTONOSUPPORT;
226         if (data->gd_addr.sa_family != AF_INET)
227                 goto out;
228         sin = (struct sockaddr_in *)&data->gd_addr;
229         if (data->gd_maxlen > NFS3_FHSIZE)
230                 data->gd_maxlen = NFS3_FHSIZE;
231
232         res = (struct knfsd_fh*)buf;
233
234         exp_readlock();
235         if (!(clp = auth_unix_lookup(sin->sin_addr)))
236                 err = -EPERM;
237         else {
238                 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
239                 auth_domain_put(clp);
240         }
241         exp_readunlock();
242         if (err == 0)
243                 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
244  out:
245         return err;
246 }
247
248 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
249 {
250         struct nfsctl_fdparm *data;
251         struct sockaddr_in *sin;
252         struct auth_domain *clp;
253         int err = 0;
254         struct knfsd_fh fh;
255         char *res;
256
257         if (size < sizeof(*data))
258                 return -EINVAL;
259         data = (struct nfsctl_fdparm*)buf;
260         err = -EPROTONOSUPPORT;
261         if (data->gd_addr.sa_family != AF_INET)
262                 goto out;
263         err = -EINVAL;
264         if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
265                 goto out;
266
267         res = buf;
268         sin = (struct sockaddr_in *)&data->gd_addr;
269         exp_readlock();
270         if (!(clp = auth_unix_lookup(sin->sin_addr)))
271                 err = -EPERM;
272         else {
273                 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
274                 auth_domain_put(clp);
275         }
276         exp_readunlock();
277
278         if (err == 0) {
279                 memset(res,0, NFS_FHSIZE);
280                 memcpy(res, &fh.fh_base, fh.fh_size);
281                 err = NFS_FHSIZE;
282         }
283  out:
284         return err;
285 }
286
287 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
288 {
289         /* request is:
290          *   domain path maxsize
291          * response is
292          *   filehandle
293          *
294          * qword quoting is used, so filehandle will be \x....
295          */
296         char *dname, *path;
297         int maxsize;
298         char *mesg = buf;
299         int len;
300         struct auth_domain *dom;
301         struct knfsd_fh fh;
302
303         if (buf[size-1] != '\n')
304                 return -EINVAL;
305         buf[size-1] = 0;
306
307         dname = mesg;
308         len = qword_get(&mesg, dname, size);
309         if (len <= 0) return -EINVAL;
310         
311         path = dname+len+1;
312         len = qword_get(&mesg, path, size);
313         if (len <= 0) return -EINVAL;
314
315         len = get_int(&mesg, &maxsize);
316         if (len)
317                 return len;
318
319         if (maxsize < NFS_FHSIZE)
320                 return -EINVAL;
321         if (maxsize > NFS3_FHSIZE)
322                 maxsize = NFS3_FHSIZE;
323
324         if (qword_get(&mesg, mesg, size)>0)
325                 return -EINVAL;
326
327         /* we have all the words, they are in buf.. */
328         dom = unix_domain_find(dname);
329         if (!dom)
330                 return -ENOMEM;
331
332         len = exp_rootfh(dom, path, &fh,  maxsize);
333         auth_domain_put(dom);
334         if (len)
335                 return len;
336         
337         mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
338         qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
339         mesg[-1] = '\n';
340         return mesg - buf;      
341 }
342
343 extern int nfsd_nrthreads(void);
344
345 static ssize_t write_threads(struct file *file, char *buf, size_t size)
346 {
347         /* if size > 0, look for a number of threads and call nfsd_svc
348          * then write out number of threads as reply
349          */
350         char *mesg = buf;
351         int rv;
352         if (size > 0) {
353                 int newthreads;
354                 rv = get_int(&mesg, &newthreads);
355                 if (rv)
356                         return rv;
357                 if (newthreads <0)
358                         return -EINVAL;
359                 rv = nfsd_svc(2049, newthreads);
360                 if (rv)
361                         return rv;
362         }
363         sprintf(buf, "%d\n", nfsd_nrthreads());
364         return strlen(buf);
365 }
366 static ssize_t write_ports(struct file *file, char *buf, size_t size)
367 {
368         /*
369          * Format:
370          *   family proto proto address port
371          */
372         char *mesg = buf;
373         char *family, *udp, *tcp, *addr; 
374         int len, port = 0;
375         ssize_t tlen = 0;
376
377         if (buf[size-1] != '\n')
378                 return -EINVAL;
379         buf[size-1] = 0;
380
381         family = mesg;
382         len = qword_get(&mesg, family, size);
383         if (len <= 0) return -EINVAL;
384
385         tlen += len;
386         udp = family+len+1;
387         len = qword_get(&mesg, udp, size);
388         if (len <= 0) return -EINVAL;
389
390         tlen += len;
391         tcp = udp+len+1;
392         len = qword_get(&mesg, tcp, size);
393         if (len <= 0) return -EINVAL;
394
395         tlen += len;
396         addr = tcp+len+1;
397         len = qword_get(&mesg, addr, size);
398         if (len <= 0) return -EINVAL;
399
400         len = get_int(&mesg, &port);
401         if (len)
402                 return len;
403
404         tlen += sizeof(port);
405         if (port)
406                 nfsd_port = port;
407
408         if (strcmp(tcp, "tcp") == 0 || strcmp(tcp, "TCP") == 0)
409                 NFSCTL_TCPSET(nfsd_portbits);
410         else
411                 NFSCTL_TCPUNSET(nfsd_portbits);
412
413         if (strcmp(udp, "udp") == 0 || strcmp(udp, "UDP") == 0)
414                 NFSCTL_UDPSET(nfsd_portbits);
415         else
416                 NFSCTL_UDPUNSET(nfsd_portbits);
417
418         return tlen;
419 }
420 static ssize_t write_versions(struct file *file, char *buf, size_t size)
421 {
422         /*
423          * Format:
424          *   [-/+]vers [-/+]vers ...
425          */
426         char *mesg = buf;
427         char *vers, sign;
428         int len, num;
429         ssize_t tlen = 0;
430         char *sep;
431
432         if (size>0) {
433                 if (nfsd_serv)
434                         return -EBUSY;
435                 if (buf[size-1] != '\n')
436                         return -EINVAL;
437                 buf[size-1] = 0;
438
439                 vers = mesg;
440                 len = qword_get(&mesg, vers, size);
441                 if (len <= 0) return -EINVAL;
442                 do {
443                         sign = *vers;
444                         if (sign == '+' || sign == '-')
445                                 num = simple_strtol((vers+1), NULL, 0);
446                         else
447                                 num = simple_strtol(vers, NULL, 0);
448                         switch(num) {
449                         case 2:
450                         case 3:
451                         case 4:
452                                 if (sign != '-')
453                                         NFSCTL_VERSET(nfsd_versbits, num);
454                                 else
455                                         NFSCTL_VERUNSET(nfsd_versbits, num);
456                                 break;
457                         default:
458                                 return -EINVAL;
459                         }
460                         vers += len + 1;
461                         tlen += len;
462                 } while ((len = qword_get(&mesg, vers, size)) > 0);
463                 /* If all get turned off, turn them back on, as
464                  * having no versions is BAD
465                  */
466                 if ((nfsd_versbits & NFSCTL_VERALL)==0)
467                         nfsd_versbits = NFSCTL_VERALL;
468         }
469         /* Now write current state into reply buffer */
470         len = 0;
471         sep = "";
472         for (num=2 ; num <= 4 ; num++)
473                 if (NFSCTL_VERISSET(NFSCTL_VERALL, num)) {
474                         len += sprintf(buf+len, "%s%c%d", sep,
475                                        NFSCTL_VERISSET(nfsd_versbits, num)?'+':'-',
476                                        num);
477                         sep = " ";
478                 }
479         len += sprintf(buf+len, "\n");
480         return len;
481 }
482
483 #ifdef CONFIG_NFSD_V4
484 extern time_t nfs4_leasetime(void);
485
486 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
487 {
488         /* if size > 10 seconds, call
489          * nfs4_reset_lease() then write out the new lease (seconds) as reply
490          */
491         char *mesg = buf;
492         int rv;
493
494         if (size > 0) {
495                 int lease;
496                 rv = get_int(&mesg, &lease);
497                 if (rv)
498                         return rv;
499                 if (lease < 10 || lease > 3600)
500                         return -EINVAL;
501                 nfs4_reset_lease(lease);
502         }
503         sprintf(buf, "%ld\n", nfs4_lease_time());
504         return strlen(buf);
505 }
506
507 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
508 {
509         char *mesg = buf;
510         char *recdir;
511         int len, status;
512
513         if (size > PATH_MAX || buf[size-1] != '\n')
514                 return -EINVAL;
515         buf[size-1] = 0;
516
517         recdir = mesg;
518         len = qword_get(&mesg, recdir, size);
519         if (len <= 0)
520                 return -EINVAL;
521
522         status = nfs4_reset_recoverydir(recdir);
523         return strlen(buf);
524 }
525 #endif
526
527 /*----------------------------------------------------------------------------*/
528 /*
529  *      populating the filesystem.
530  */
531
532 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
533 {
534         static struct tree_descr nfsd_files[] = {
535                 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
536                 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
537                 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
538                 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
539                 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
540                 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
541                 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
542                 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
543                 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
544                 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
545                 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
546                 [NFSD_Ports] = {"ports", &transaction_ops, S_IWUSR|S_IRUSR},
547 #ifdef CONFIG_NFSD_V4
548                 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
549                 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
550 #endif
551                 /* last one */ {""}
552         };
553         return simple_fill_super(sb, 0x6e667364, nfsd_files);
554 }
555
556 static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
557         int flags, const char *dev_name, void *data)
558 {
559         return get_sb_single(fs_type, flags, data, nfsd_fill_super);
560 }
561
562 static struct file_system_type nfsd_fs_type = {
563         .owner          = THIS_MODULE,
564         .name           = "nfsd",
565         .get_sb         = nfsd_get_sb,
566         .kill_sb        = kill_litter_super,
567 };
568
569 static int __init init_nfsd(void)
570 {
571         int retval;
572         printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
573
574         nfsd_stat_init();       /* Statistics */
575         nfsd_cache_init();      /* RPC reply cache */
576         nfsd_export_init();     /* Exports table */
577         nfsd_lockd_init();      /* lockd->nfsd callbacks */
578         nfs4_state_init();      /* NFSv4 locking state */
579         nfsd_idmap_init();      /* Name to ID mapping */
580         if (proc_mkdir("fs/nfs", NULL)) {
581                 struct proc_dir_entry *entry;
582                 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
583                 if (entry)
584                         entry->proc_fops =  &exports_operations;
585         }
586         retval = register_filesystem(&nfsd_fs_type);
587         if (retval) {
588                 nfsd_export_shutdown();
589                 nfsd_cache_shutdown();
590                 remove_proc_entry("fs/nfs/exports", NULL);
591                 remove_proc_entry("fs/nfs", NULL);
592                 nfsd_stat_shutdown();
593                 nfsd_lockd_shutdown();
594         }
595         return retval;
596 }
597
598 static void __exit exit_nfsd(void)
599 {
600         nfsd_export_shutdown();
601         nfsd_cache_shutdown();
602         remove_proc_entry("fs/nfs/exports", NULL);
603         remove_proc_entry("fs/nfs", NULL);
604         nfsd_stat_shutdown();
605         nfsd_lockd_shutdown();
606         nfsd_idmap_shutdown();
607         unregister_filesystem(&nfsd_fs_type);
608 }
609
610 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
611 MODULE_LICENSE("GPL");
612 module_init(init_nfsd)
613 module_exit(exit_nfsd)