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/module.h>
11 #include <linux/linkage.h>
12 #include <linux/time.h>
13 #include <linux/errno.h>
15 #include <linux/fcntl.h>
16 #include <linux/net.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>
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>
38 #include <asm/uaccess.h>
41 * We have a single directory with 9 nodes in it.
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
68 * write() for these nodes.
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);
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);
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,
99 [NFSD_Leasetime] = write_leasetime,
100 [NFSD_RecoveryDir] = write_recoverydir,
104 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
106 ino_t ino = file->f_dentry->d_inode->i_ino;
110 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
113 data = simple_transaction_get(file, buf, size);
115 return PTR_ERR(data);
117 rv = write_op[ino](file, data, size);
119 simple_transaction_set(file, rv);
125 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
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
132 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
136 return simple_transaction_read(file, buf, size, pos);
139 static const struct file_operations transaction_ops = {
140 .write = nfsctl_transaction_write,
141 .read = nfsctl_transaction_read,
142 .release = simple_transaction_release,
145 extern struct seq_operations nfs_exports_op;
146 static int exports_open(struct inode *inode, struct file *file)
148 return seq_open(file, &nfs_exports_op);
151 static const struct file_operations exports_operations = {
152 .open = exports_open,
155 .release = seq_release,
158 /*----------------------------------------------------------------------------*/
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.
165 static ssize_t write_svc(struct file *file, char *buf, size_t size)
167 struct nfsctl_svc *data;
168 if (size < sizeof(*data))
170 data = (struct nfsctl_svc*) buf;
171 return nfsd_svc(data->svc_port, data->svc_nthreads);
174 static ssize_t write_add(struct file *file, char *buf, size_t size)
176 struct nfsctl_client *data;
177 if (size < sizeof(*data))
179 data = (struct nfsctl_client *)buf;
180 return exp_addclient(data);
183 static ssize_t write_del(struct file *file, char *buf, size_t size)
185 struct nfsctl_client *data;
186 if (size < sizeof(*data))
188 data = (struct nfsctl_client *)buf;
189 return exp_delclient(data);
192 static ssize_t write_export(struct file *file, char *buf, size_t size)
194 struct nfsctl_export *data;
195 if (size < sizeof(*data))
197 data = (struct nfsctl_export*)buf;
198 return exp_export(data);
201 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
203 struct nfsctl_export *data;
205 if (size < sizeof(*data))
207 data = (struct nfsctl_export*)buf;
208 return exp_unexport(data);
211 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
213 struct nfsctl_fsparm *data;
214 struct sockaddr_in *sin;
215 struct auth_domain *clp;
217 struct knfsd_fh *res;
219 if (size < sizeof(*data))
221 data = (struct nfsctl_fsparm*)buf;
222 err = -EPROTONOSUPPORT;
223 if (data->gd_addr.sa_family != AF_INET)
225 sin = (struct sockaddr_in *)&data->gd_addr;
226 if (data->gd_maxlen > NFS3_FHSIZE)
227 data->gd_maxlen = NFS3_FHSIZE;
229 res = (struct knfsd_fh*)buf;
232 if (!(clp = auth_unix_lookup(sin->sin_addr)))
235 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
236 auth_domain_put(clp);
240 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
245 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
247 struct nfsctl_fdparm *data;
248 struct sockaddr_in *sin;
249 struct auth_domain *clp;
254 if (size < sizeof(*data))
256 data = (struct nfsctl_fdparm*)buf;
257 err = -EPROTONOSUPPORT;
258 if (data->gd_addr.sa_family != AF_INET)
261 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
265 sin = (struct sockaddr_in *)&data->gd_addr;
267 if (!(clp = auth_unix_lookup(sin->sin_addr)))
270 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
271 auth_domain_put(clp);
276 memset(res,0, NFS_FHSIZE);
277 memcpy(res, &fh.fh_base, fh.fh_size);
284 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
287 * domain path maxsize
291 * qword quoting is used, so filehandle will be \x....
297 struct auth_domain *dom;
300 if (buf[size-1] != '\n')
305 len = qword_get(&mesg, dname, size);
306 if (len <= 0) return -EINVAL;
309 len = qword_get(&mesg, path, size);
310 if (len <= 0) return -EINVAL;
312 len = get_int(&mesg, &maxsize);
316 if (maxsize < NFS_FHSIZE)
318 if (maxsize > NFS3_FHSIZE)
319 maxsize = NFS3_FHSIZE;
321 if (qword_get(&mesg, mesg, size)>0)
324 /* we have all the words, they are in buf.. */
325 dom = unix_domain_find(dname);
329 len = exp_rootfh(dom, path, &fh, maxsize);
330 auth_domain_put(dom);
334 mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
335 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
340 extern int nfsd_nrthreads(void);
342 static ssize_t write_threads(struct file *file, char *buf, size_t size)
344 /* if size > 0, look for a number of threads and call nfsd_svc
345 * then write out number of threads as reply
351 rv = get_int(&mesg, &newthreads);
356 rv = nfsd_svc(2049, newthreads);
360 sprintf(buf, "%d\n", nfsd_nrthreads());
364 static ssize_t write_versions(struct file *file, char *buf, size_t size)
368 * [-/+]vers [-/+]vers ...
378 /* Cannot change versions without updating
379 * nfsd_serv->sv_xdrsize, and reallocing
380 * rq_argp and rq_resp
383 if (buf[size-1] != '\n')
388 len = qword_get(&mesg, vers, size);
389 if (len <= 0) return -EINVAL;
392 if (sign == '+' || sign == '-')
393 num = simple_strtol((vers+1), NULL, 0);
395 num = simple_strtol(vers, NULL, 0);
400 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
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
411 nfsd_reset_versions();
413 /* Now write current state into reply buffer */
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)?'+':'-',
423 len += sprintf(buf+len, "\n");
427 static ssize_t write_ports(struct file *file, char *buf, size_t size)
429 /* for now, ignore what was written and just
431 * AF proto address port
436 len = svc_sock_names(buf, nfsd_serv);
441 #ifdef CONFIG_NFSD_V4
442 extern time_t nfs4_leasetime(void);
444 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
446 /* if size > 10 seconds, call
447 * nfs4_reset_lease() then write out the new lease (seconds) as reply
454 rv = get_int(&mesg, &lease);
457 if (lease < 10 || lease > 3600)
459 nfs4_reset_lease(lease);
461 sprintf(buf, "%ld\n", nfs4_lease_time());
465 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
471 if (size > PATH_MAX || buf[size-1] != '\n')
476 len = qword_get(&mesg, recdir, size);
480 status = nfs4_reset_recoverydir(recdir);
485 /*----------------------------------------------------------------------------*/
487 * populating the filesystem.
490 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
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},
511 return simple_fill_super(sb, 0x6e667364, nfsd_files);
514 static int nfsd_get_sb(struct file_system_type *fs_type,
515 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
517 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
520 static struct file_system_type nfsd_fs_type = {
521 .owner = THIS_MODULE,
523 .get_sb = nfsd_get_sb,
524 .kill_sb = kill_litter_super,
527 static int __init init_nfsd(void)
530 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
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);
542 entry->proc_fops = &exports_operations;
544 retval = register_filesystem(&nfsd_fs_type);
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();
556 static void __exit exit_nfsd(void)
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);
568 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
569 MODULE_LICENSE("GPL");
570 module_init(init_nfsd)
571 module_exit(exit_nfsd)