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/xdr3.h>
35 #include <linux/nfsd/syscall.h>
36 #include <linux/nfsd/interface.h>
38 #include <asm/uaccess.h>
40 unsigned int nfsd_versbits = ~0;
43 * We have a single directory with 9 nodes in it.
59 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
60 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
69 * write() for these nodes.
71 static ssize_t write_svc(struct file *file, char *buf, size_t size);
72 static ssize_t write_add(struct file *file, char *buf, size_t size);
73 static ssize_t write_del(struct file *file, char *buf, size_t size);
74 static ssize_t write_export(struct file *file, char *buf, size_t size);
75 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
76 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
77 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
78 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
79 static ssize_t write_threads(struct file *file, char *buf, size_t size);
80 static ssize_t write_versions(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,
98 [NFSD_Leasetime] = write_leasetime,
99 [NFSD_RecoveryDir] = write_recoverydir,
103 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
105 ino_t ino = file->f_dentry->d_inode->i_ino;
109 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
112 data = simple_transaction_get(file, buf, size);
114 return PTR_ERR(data);
116 rv = write_op[ino](file, data, size);
118 simple_transaction_set(file, rv);
124 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
126 if (! file->private_data) {
127 /* An attempt to read a transaction file without writing
128 * causes a 0-byte write so that the file can return
131 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
135 return simple_transaction_read(file, buf, size, pos);
138 static const struct file_operations transaction_ops = {
139 .write = nfsctl_transaction_write,
140 .read = nfsctl_transaction_read,
141 .release = simple_transaction_release,
144 extern struct seq_operations nfs_exports_op;
145 static int exports_open(struct inode *inode, struct file *file)
147 return seq_open(file, &nfs_exports_op);
150 static const struct file_operations exports_operations = {
151 .open = exports_open,
154 .release = seq_release,
157 /*----------------------------------------------------------------------------*/
159 * payload - write methods
160 * If the method has a response, the response should be put in buf,
161 * and the length returned. Otherwise return 0 or and -error.
164 static ssize_t write_svc(struct file *file, char *buf, size_t size)
166 struct nfsctl_svc *data;
167 if (size < sizeof(*data))
169 data = (struct nfsctl_svc*) buf;
170 return nfsd_svc(data->svc_port, data->svc_nthreads);
173 static ssize_t write_add(struct file *file, char *buf, size_t size)
175 struct nfsctl_client *data;
176 if (size < sizeof(*data))
178 data = (struct nfsctl_client *)buf;
179 return exp_addclient(data);
182 static ssize_t write_del(struct file *file, char *buf, size_t size)
184 struct nfsctl_client *data;
185 if (size < sizeof(*data))
187 data = (struct nfsctl_client *)buf;
188 return exp_delclient(data);
191 static ssize_t write_export(struct file *file, char *buf, size_t size)
193 struct nfsctl_export *data;
194 if (size < sizeof(*data))
196 data = (struct nfsctl_export*)buf;
197 return exp_export(data);
200 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
202 struct nfsctl_export *data;
204 if (size < sizeof(*data))
206 data = (struct nfsctl_export*)buf;
207 return exp_unexport(data);
210 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
212 struct nfsctl_fsparm *data;
213 struct sockaddr_in *sin;
214 struct auth_domain *clp;
216 struct knfsd_fh *res;
218 if (size < sizeof(*data))
220 data = (struct nfsctl_fsparm*)buf;
221 err = -EPROTONOSUPPORT;
222 if (data->gd_addr.sa_family != AF_INET)
224 sin = (struct sockaddr_in *)&data->gd_addr;
225 if (data->gd_maxlen > NFS3_FHSIZE)
226 data->gd_maxlen = NFS3_FHSIZE;
228 res = (struct knfsd_fh*)buf;
231 if (!(clp = auth_unix_lookup(sin->sin_addr)))
234 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
235 auth_domain_put(clp);
239 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
244 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
246 struct nfsctl_fdparm *data;
247 struct sockaddr_in *sin;
248 struct auth_domain *clp;
253 if (size < sizeof(*data))
255 data = (struct nfsctl_fdparm*)buf;
256 err = -EPROTONOSUPPORT;
257 if (data->gd_addr.sa_family != AF_INET)
260 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
264 sin = (struct sockaddr_in *)&data->gd_addr;
266 if (!(clp = auth_unix_lookup(sin->sin_addr)))
269 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
270 auth_domain_put(clp);
275 memset(res,0, NFS_FHSIZE);
276 memcpy(res, &fh.fh_base, fh.fh_size);
283 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
286 * domain path maxsize
290 * qword quoting is used, so filehandle will be \x....
296 struct auth_domain *dom;
299 if (buf[size-1] != '\n')
304 len = qword_get(&mesg, dname, size);
305 if (len <= 0) return -EINVAL;
308 len = qword_get(&mesg, path, size);
309 if (len <= 0) return -EINVAL;
311 len = get_int(&mesg, &maxsize);
315 if (maxsize < NFS_FHSIZE)
317 if (maxsize > NFS3_FHSIZE)
318 maxsize = NFS3_FHSIZE;
320 if (qword_get(&mesg, mesg, size)>0)
323 /* we have all the words, they are in buf.. */
324 dom = unix_domain_find(dname);
328 len = exp_rootfh(dom, path, &fh, maxsize);
329 auth_domain_put(dom);
333 mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
334 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
339 extern int nfsd_nrthreads(void);
341 static ssize_t write_threads(struct file *file, char *buf, size_t size)
343 /* if size > 0, look for a number of threads and call nfsd_svc
344 * then write out number of threads as reply
350 rv = get_int(&mesg, &newthreads);
355 rv = nfsd_svc(2049, newthreads);
359 sprintf(buf, "%d\n", nfsd_nrthreads());
363 static ssize_t write_versions(struct file *file, char *buf, size_t size)
367 * [-/+]vers [-/+]vers ...
378 if (buf[size-1] != '\n')
383 len = qword_get(&mesg, vers, size);
384 if (len <= 0) return -EINVAL;
387 if (sign == '+' || sign == '-')
388 num = simple_strtol((vers+1), NULL, 0);
390 num = simple_strtol(vers, NULL, 0);
396 NFSCTL_VERSET(nfsd_versbits, num);
398 NFSCTL_VERUNSET(nfsd_versbits, num);
405 } while ((len = qword_get(&mesg, vers, size)) > 0);
406 /* If all get turned off, turn them back on, as
407 * having no versions is BAD
409 if ((nfsd_versbits & NFSCTL_VERALL)==0)
410 nfsd_versbits = NFSCTL_VERALL;
412 /* Now write current state into reply buffer */
415 for (num=2 ; num <= 4 ; num++)
416 if (NFSCTL_VERISSET(NFSCTL_VERALL, num)) {
417 len += sprintf(buf+len, "%s%c%d", sep,
418 NFSCTL_VERISSET(nfsd_versbits, num)?'+':'-',
422 len += sprintf(buf+len, "\n");
426 #ifdef CONFIG_NFSD_V4
427 extern time_t nfs4_leasetime(void);
429 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
431 /* if size > 10 seconds, call
432 * nfs4_reset_lease() then write out the new lease (seconds) as reply
439 rv = get_int(&mesg, &lease);
442 if (lease < 10 || lease > 3600)
444 nfs4_reset_lease(lease);
446 sprintf(buf, "%ld\n", nfs4_lease_time());
450 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
456 if (size > PATH_MAX || buf[size-1] != '\n')
461 len = qword_get(&mesg, recdir, size);
465 status = nfs4_reset_recoverydir(recdir);
470 /*----------------------------------------------------------------------------*/
472 * populating the filesystem.
475 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
477 static struct tree_descr nfsd_files[] = {
478 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
479 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
480 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
481 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
482 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
483 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
484 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
485 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
486 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
487 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
488 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
489 #ifdef CONFIG_NFSD_V4
490 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
491 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
495 return simple_fill_super(sb, 0x6e667364, nfsd_files);
498 static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
499 int flags, const char *dev_name, void *data)
501 return get_sb_single(fs_type, flags, data, nfsd_fill_super);
504 static struct file_system_type nfsd_fs_type = {
505 .owner = THIS_MODULE,
507 .get_sb = nfsd_get_sb,
508 .kill_sb = kill_litter_super,
514 static struct ctl_table_header *nfsd_sysctl_table;
516 static ctl_table nfsd_sysctls[] = {
519 .procname = "nfsd_readdirplus_max",
520 .data = &nfsd_readdirplus_max,
521 .maxlen = sizeof(nfsd_readdirplus_max),
523 .proc_handler = &proc_dointvec_minmax,
524 .extra1 = &nfsd_readdirplus_max_lb,
525 .extra2 = &nfsd_readdirplus_max_ub,
530 static int __init init_nfsd(void)
532 struct ctl_path ctl_path[] = { { CTL_FS, "fs", 0555 }, { -2, "nfs", 0555 }, { 0 } };
534 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
536 nfsd_stat_init(); /* Statistics */
537 nfsd_cache_init(); /* RPC reply cache */
538 nfsd_export_init(); /* Exports table */
539 nfsd_lockd_init(); /* lockd->nfsd callbacks */
540 nfs4_state_init(); /* NFSv4 locking state */
541 nfsd_idmap_init(); /* Name to ID mapping */
542 nfsd_sysctl_table = register_sysctl_table_path(nfsd_sysctls, ctl_path);
543 if (proc_mkdir("fs/nfs", NULL)) {
544 struct proc_dir_entry *entry;
545 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
547 entry->proc_fops = &exports_operations;
549 retval = register_filesystem(&nfsd_fs_type);
551 nfsd_export_shutdown();
552 nfsd_cache_shutdown();
553 remove_proc_entry("fs/nfs/exports", NULL);
554 remove_proc_entry("fs/nfs", NULL);
555 nfsd_stat_shutdown();
556 nfsd_lockd_shutdown();
557 if (nfsd_sysctl_table)
558 unregister_sysctl_table(nfsd_sysctl_table);
559 nfsd_sysctl_table = NULL;
564 static void __exit exit_nfsd(void)
566 nfsd_export_shutdown();
567 nfsd_cache_shutdown();
568 remove_proc_entry("fs/nfs/exports", NULL);
569 remove_proc_entry("fs/nfs", NULL);
570 nfsd_stat_shutdown();
571 nfsd_lockd_shutdown();
572 nfsd_idmap_shutdown();
573 unregister_filesystem(&nfsd_fs_type);
574 if (nfsd_sysctl_table)
575 unregister_sysctl_table(nfsd_sysctl_table);
576 nfsd_sysctl_table = NULL;
579 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
580 MODULE_LICENSE("GPL");
581 module_init(init_nfsd)
582 module_exit(exit_nfsd)