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