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