2 * linux/fs/nfsd/nfsctl.c
4 * Syscall interface to knfsd.
6 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
9 #include <linux/config.h>
10 #include <linux/module.h>
12 #include <linux/linkage.h>
13 #include <linux/time.h>
14 #include <linux/errno.h>
16 #include <linux/fcntl.h>
17 #include <linux/net.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>
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>
37 #include <asm/uaccess.h>
40 unsigned int nfsd_portbits = 0;
41 unsigned int nfsd_versbits = ~0;
44 * We have a single directory with 9 nodes in it.
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
71 * write() for these nodes.
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);
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);
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,
107 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
109 ino_t ino = file->f_dentry->d_inode->i_ino;
113 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
116 data = simple_transaction_get(file, buf, size);
118 return PTR_ERR(data);
120 rv = write_op[ino](file, data, size);
122 simple_transaction_set(file, rv);
128 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
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
135 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
139 return simple_transaction_read(file, buf, size, pos);
142 static const struct file_operations transaction_ops = {
143 .write = nfsctl_transaction_write,
144 .read = nfsctl_transaction_read,
145 .release = simple_transaction_release,
148 extern struct seq_operations nfs_exports_op;
149 static int exports_open(struct inode *inode, struct file *file)
151 return seq_open(file, &nfs_exports_op);
154 static const struct file_operations exports_operations = {
155 .open = exports_open,
158 .release = seq_release,
161 /*----------------------------------------------------------------------------*/
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.
168 static ssize_t write_svc(struct file *file, char *buf, size_t size)
170 struct nfsctl_svc *data;
171 if (size < sizeof(*data))
173 data = (struct nfsctl_svc*) buf;
174 return nfsd_svc(data->svc_port, data->svc_nthreads);
177 static ssize_t write_add(struct file *file, char *buf, size_t size)
179 struct nfsctl_client *data;
180 if (size < sizeof(*data))
182 data = (struct nfsctl_client *)buf;
183 return exp_addclient(data);
186 static ssize_t write_del(struct file *file, char *buf, size_t size)
188 struct nfsctl_client *data;
189 if (size < sizeof(*data))
191 data = (struct nfsctl_client *)buf;
192 return exp_delclient(data);
195 static ssize_t write_export(struct file *file, char *buf, size_t size)
197 struct nfsctl_export *data;
198 if (size < sizeof(*data))
200 data = (struct nfsctl_export*)buf;
201 return exp_export(data);
204 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
206 struct nfsctl_export *data;
208 if (size < sizeof(*data))
210 data = (struct nfsctl_export*)buf;
211 return exp_unexport(data);
214 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
216 struct nfsctl_fsparm *data;
217 struct sockaddr_in *sin;
218 struct auth_domain *clp;
220 struct knfsd_fh *res;
222 if (size < sizeof(*data))
224 data = (struct nfsctl_fsparm*)buf;
225 err = -EPROTONOSUPPORT;
226 if (data->gd_addr.sa_family != AF_INET)
228 sin = (struct sockaddr_in *)&data->gd_addr;
229 if (data->gd_maxlen > NFS3_FHSIZE)
230 data->gd_maxlen = NFS3_FHSIZE;
232 res = (struct knfsd_fh*)buf;
235 if (!(clp = auth_unix_lookup(sin->sin_addr)))
238 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
239 auth_domain_put(clp);
243 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
248 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
250 struct nfsctl_fdparm *data;
251 struct sockaddr_in *sin;
252 struct auth_domain *clp;
257 if (size < sizeof(*data))
259 data = (struct nfsctl_fdparm*)buf;
260 err = -EPROTONOSUPPORT;
261 if (data->gd_addr.sa_family != AF_INET)
264 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
268 sin = (struct sockaddr_in *)&data->gd_addr;
270 if (!(clp = auth_unix_lookup(sin->sin_addr)))
273 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
274 auth_domain_put(clp);
279 memset(res,0, NFS_FHSIZE);
280 memcpy(res, &fh.fh_base, fh.fh_size);
287 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
290 * domain path maxsize
294 * qword quoting is used, so filehandle will be \x....
300 struct auth_domain *dom;
303 if (buf[size-1] != '\n')
308 len = qword_get(&mesg, dname, size);
309 if (len <= 0) return -EINVAL;
312 len = qword_get(&mesg, path, size);
313 if (len <= 0) return -EINVAL;
315 len = get_int(&mesg, &maxsize);
319 if (maxsize < NFS_FHSIZE)
321 if (maxsize > NFS3_FHSIZE)
322 maxsize = NFS3_FHSIZE;
324 if (qword_get(&mesg, mesg, size)>0)
327 /* we have all the words, they are in buf.. */
328 dom = unix_domain_find(dname);
332 len = exp_rootfh(dom, path, &fh, maxsize);
333 auth_domain_put(dom);
337 mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
338 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
343 extern int nfsd_nrthreads(void);
345 static ssize_t write_threads(struct file *file, char *buf, size_t size)
347 /* if size > 0, look for a number of threads and call nfsd_svc
348 * then write out number of threads as reply
354 rv = get_int(&mesg, &newthreads);
359 rv = nfsd_svc(2049, newthreads);
363 sprintf(buf, "%d\n", nfsd_nrthreads());
366 static ssize_t write_ports(struct file *file, char *buf, size_t size)
370 * family proto proto address port
373 char *family, *udp, *tcp, *addr;
377 if (buf[size-1] != '\n')
382 len = qword_get(&mesg, family, size);
383 if (len <= 0) return -EINVAL;
387 len = qword_get(&mesg, udp, size);
388 if (len <= 0) return -EINVAL;
392 len = qword_get(&mesg, tcp, size);
393 if (len <= 0) return -EINVAL;
397 len = qword_get(&mesg, addr, size);
398 if (len <= 0) return -EINVAL;
400 len = get_int(&mesg, &port);
404 tlen += sizeof(port);
408 if (strcmp(tcp, "tcp") == 0 || strcmp(tcp, "TCP") == 0)
409 NFSCTL_TCPSET(nfsd_portbits);
411 NFSCTL_TCPUNSET(nfsd_portbits);
413 if (strcmp(udp, "udp") == 0 || strcmp(udp, "UDP") == 0)
414 NFSCTL_UDPSET(nfsd_portbits);
416 NFSCTL_UDPUNSET(nfsd_portbits);
420 static ssize_t write_versions(struct file *file, char *buf, size_t size)
424 * [-/+]vers [-/+]vers ...
435 if (buf[size-1] != '\n')
440 len = qword_get(&mesg, vers, size);
441 if (len <= 0) return -EINVAL;
444 if (sign == '+' || sign == '-')
445 num = simple_strtol((vers+1), NULL, 0);
447 num = simple_strtol(vers, NULL, 0);
453 NFSCTL_VERSET(nfsd_versbits, num);
455 NFSCTL_VERUNSET(nfsd_versbits, num);
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
466 if ((nfsd_versbits & NFSCTL_VERALL)==0)
467 nfsd_versbits = NFSCTL_VERALL;
469 /* Now write current state into reply buffer */
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)?'+':'-',
479 len += sprintf(buf+len, "\n");
483 #ifdef CONFIG_NFSD_V4
484 extern time_t nfs4_leasetime(void);
486 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
488 /* if size > 10 seconds, call
489 * nfs4_reset_lease() then write out the new lease (seconds) as reply
496 rv = get_int(&mesg, &lease);
499 if (lease < 10 || lease > 3600)
501 nfs4_reset_lease(lease);
503 sprintf(buf, "%ld\n", nfs4_lease_time());
507 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
513 if (size > PATH_MAX || buf[size-1] != '\n')
518 len = qword_get(&mesg, recdir, size);
522 status = nfs4_reset_recoverydir(recdir);
527 /*----------------------------------------------------------------------------*/
529 * populating the filesystem.
532 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
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},
553 return simple_fill_super(sb, 0x6e667364, nfsd_files);
556 static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
557 int flags, const char *dev_name, void *data)
559 return get_sb_single(fs_type, flags, data, nfsd_fill_super);
562 static struct file_system_type nfsd_fs_type = {
563 .owner = THIS_MODULE,
565 .get_sb = nfsd_get_sb,
566 .kill_sb = kill_litter_super,
569 static int __init init_nfsd(void)
572 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
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);
584 entry->proc_fops = &exports_operations;
586 retval = register_filesystem(&nfsd_fs_type);
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();
598 static void __exit exit_nfsd(void)
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);
610 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
611 MODULE_LICENSE("GPL");
612 module_init(init_nfsd)
613 module_exit(exit_nfsd)