[PATCH] knfsd: allow sockets to be passed to nfsd via 'portlist'
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / nfsd / nfsctl.c
CommitLineData
1da177e4
LT
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
1da177e4
LT
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>
70c3b76c 25#include <linux/string.h>
80212d59 26#include <linux/smp_lock.h>
b41b66d6 27#include <linux/ctype.h>
1da177e4
LT
28
29#include <linux/nfs.h>
30#include <linux/nfsd_idmap.h>
b41b66d6 31#include <linux/lockd/bind.h>
1da177e4 32#include <linux/sunrpc/svc.h>
80212d59 33#include <linux/sunrpc/svcsock.h>
1da177e4
LT
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 */
45enum {
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,
70c3b76c 57 NFSD_Versions,
80212d59 58 NFSD_Ports,
70c3b76c
N
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
1da177e4 64 NFSD_Leasetime,
0964a3d3 65 NFSD_RecoveryDir,
70c3b76c 66#endif
1da177e4
LT
67};
68
69/*
70 * write() for these nodes.
71 */
72static ssize_t write_svc(struct file *file, char *buf, size_t size);
73static ssize_t write_add(struct file *file, char *buf, size_t size);
74static ssize_t write_del(struct file *file, char *buf, size_t size);
75static ssize_t write_export(struct file *file, char *buf, size_t size);
76static ssize_t write_unexport(struct file *file, char *buf, size_t size);
77static ssize_t write_getfd(struct file *file, char *buf, size_t size);
78static ssize_t write_getfs(struct file *file, char *buf, size_t size);
79static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
80static ssize_t write_threads(struct file *file, char *buf, size_t size);
70c3b76c 81static ssize_t write_versions(struct file *file, char *buf, size_t size);
80212d59 82static ssize_t write_ports(struct file *file, char *buf, size_t size);
70c3b76c 83#ifdef CONFIG_NFSD_V4
1da177e4 84static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
0964a3d3 85static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
70c3b76c 86#endif
1da177e4
LT
87
88static 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,
70c3b76c 98 [NFSD_Versions] = write_versions,
80212d59 99 [NFSD_Ports] = write_ports,
70c3b76c 100#ifdef CONFIG_NFSD_V4
1da177e4 101 [NFSD_Leasetime] = write_leasetime,
0964a3d3 102 [NFSD_RecoveryDir] = write_recoverydir,
70c3b76c 103#endif
1da177e4
LT
104};
105
106static 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
e8c96f8c 112 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
1da177e4
LT
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
7390022d
N
127static 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
4b6f5d20 141static const struct file_operations transaction_ops = {
1da177e4 142 .write = nfsctl_transaction_write,
7390022d 143 .read = nfsctl_transaction_read,
1da177e4
LT
144 .release = simple_transaction_release,
145};
146
147extern struct seq_operations nfs_exports_op;
148static int exports_open(struct inode *inode, struct file *file)
149{
150 return seq_open(file, &nfs_exports_op);
151}
152
4b6f5d20 153static const struct file_operations exports_operations = {
1da177e4
LT
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
167static 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
176static 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
185static 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
194static 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
203static 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
213static 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
247static 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
286static 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
342extern int nfsd_nrthreads(void);
343
344static 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
70c3b76c
N
366static 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)
6658d3a7
N
380 /* Cannot change versions without updating
381 * nfsd_serv->sv_xdrsize, and reallocing
382 * rq_argp and rq_resp
383 */
70c3b76c
N
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:
6658d3a7 402 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
70c3b76c
N
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 */
6658d3a7 413 nfsd_reset_versions();
70c3b76c
N
414 }
415 /* Now write current state into reply buffer */
416 len = 0;
417 sep = "";
418 for (num=2 ; num <= 4 ; num++)
6658d3a7 419 if (nfsd_vers(num, NFSD_AVAIL)) {
70c3b76c 420 len += sprintf(buf+len, "%s%c%d", sep,
6658d3a7 421 nfsd_vers(num, NFSD_TEST)?'+':'-',
70c3b76c
N
422 num);
423 sep = " ";
424 }
425 len += sprintf(buf+len, "\n");
426 return len;
427}
428
80212d59
N
429static ssize_t write_ports(struct file *file, char *buf, size_t size)
430{
b41b66d6
N
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.
80212d59 444 */
b41b66d6
N
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 = svc_addsock(nfsd_serv, fd, buf, &proto);
458 /* Decrease the count, but don't shutdown the
459 * the service
460 */
461 if (err >= 0)
462 lockd_up(proto);
463 nfsd_serv->sv_nrthreads--;
464 }
465 return err;
466 }
467 if (buf[0] == '-') {
468 char *toclose = kstrdup(buf+1, GFP_KERNEL);
469 int len = 0;
470 if (!toclose)
471 return -ENOMEM;
472 lock_kernel();
473 if (nfsd_serv)
474 len = svc_sock_names(buf, nfsd_serv, toclose);
475 unlock_kernel();
476 kfree(toclose);
477 return len;
478 }
479 return -EINVAL;
80212d59
N
480}
481
70c3b76c 482#ifdef CONFIG_NFSD_V4
1da177e4
LT
483extern time_t nfs4_leasetime(void);
484
485static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
486{
487 /* if size > 10 seconds, call
488 * nfs4_reset_lease() then write out the new lease (seconds) as reply
489 */
490 char *mesg = buf;
491 int rv;
492
493 if (size > 0) {
494 int lease;
495 rv = get_int(&mesg, &lease);
496 if (rv)
497 return rv;
498 if (lease < 10 || lease > 3600)
499 return -EINVAL;
500 nfs4_reset_lease(lease);
501 }
502 sprintf(buf, "%ld\n", nfs4_lease_time());
503 return strlen(buf);
504}
505
0964a3d3
N
506static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
507{
508 char *mesg = buf;
509 char *recdir;
510 int len, status;
511
512 if (size > PATH_MAX || buf[size-1] != '\n')
513 return -EINVAL;
514 buf[size-1] = 0;
515
516 recdir = mesg;
517 len = qword_get(&mesg, recdir, size);
518 if (len <= 0)
519 return -EINVAL;
520
521 status = nfs4_reset_recoverydir(recdir);
522 return strlen(buf);
523}
70c3b76c 524#endif
0964a3d3 525
1da177e4
LT
526/*----------------------------------------------------------------------------*/
527/*
528 * populating the filesystem.
529 */
530
531static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
532{
533 static struct tree_descr nfsd_files[] = {
534 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
535 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
536 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
537 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
538 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
539 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
540 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
541 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
542 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
543 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
70c3b76c 544 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
80212d59 545 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1da177e4
LT
546#ifdef CONFIG_NFSD_V4
547 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
0964a3d3 548 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1da177e4
LT
549#endif
550 /* last one */ {""}
551 };
552 return simple_fill_super(sb, 0x6e667364, nfsd_files);
553}
554
454e2398
DH
555static int nfsd_get_sb(struct file_system_type *fs_type,
556 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
1da177e4 557{
454e2398 558 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
1da177e4
LT
559}
560
561static struct file_system_type nfsd_fs_type = {
562 .owner = THIS_MODULE,
563 .name = "nfsd",
564 .get_sb = nfsd_get_sb,
565 .kill_sb = kill_litter_super,
566};
567
568static int __init init_nfsd(void)
569{
570 int retval;
571 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
572
573 nfsd_stat_init(); /* Statistics */
574 nfsd_cache_init(); /* RPC reply cache */
575 nfsd_export_init(); /* Exports table */
576 nfsd_lockd_init(); /* lockd->nfsd callbacks */
ac4d8ff2 577 nfs4_state_init(); /* NFSv4 locking state */
1da177e4 578 nfsd_idmap_init(); /* Name to ID mapping */
1da177e4
LT
579 if (proc_mkdir("fs/nfs", NULL)) {
580 struct proc_dir_entry *entry;
581 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
582 if (entry)
583 entry->proc_fops = &exports_operations;
584 }
585 retval = register_filesystem(&nfsd_fs_type);
586 if (retval) {
587 nfsd_export_shutdown();
588 nfsd_cache_shutdown();
589 remove_proc_entry("fs/nfs/exports", NULL);
590 remove_proc_entry("fs/nfs", NULL);
591 nfsd_stat_shutdown();
592 nfsd_lockd_shutdown();
593 }
594 return retval;
595}
596
597static void __exit exit_nfsd(void)
598{
599 nfsd_export_shutdown();
600 nfsd_cache_shutdown();
601 remove_proc_entry("fs/nfs/exports", NULL);
602 remove_proc_entry("fs/nfs", NULL);
603 nfsd_stat_shutdown();
604 nfsd_lockd_shutdown();
1da177e4 605 nfsd_idmap_shutdown();
1da177e4
LT
606 unregister_filesystem(&nfsd_fs_type);
607}
608
609MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
610MODULE_LICENSE("GPL");
611module_init(init_nfsd)
612module_exit(exit_nfsd)