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>
27 #include <linux/nfs.h>
28 #include <linux/nfsd_idmap.h>
29 #include <linux/sunrpc/svc.h>
30 #include <linux/nfsd/nfsd.h>
31 #include <linux/nfsd/cache.h>
32 #include <linux/nfsd/xdr.h>
33 #include <linux/nfsd/syscall.h>
34 #include <linux/nfsd/interface.h>
36 #include <asm/uaccess.h>
39 * We have a single directory with 9 nodes in it.
55 * The below MUST come last. Otherwise we leave a hole in nfsd_files[]
56 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
65 * write() for these nodes.
67 static ssize_t write_svc(struct file *file, char *buf, size_t size);
68 static ssize_t write_add(struct file *file, char *buf, size_t size);
69 static ssize_t write_del(struct file *file, char *buf, size_t size);
70 static ssize_t write_export(struct file *file, char *buf, size_t size);
71 static ssize_t write_unexport(struct file *file, char *buf, size_t size);
72 static ssize_t write_getfd(struct file *file, char *buf, size_t size);
73 static ssize_t write_getfs(struct file *file, char *buf, size_t size);
74 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
75 static ssize_t write_threads(struct file *file, char *buf, size_t size);
76 static ssize_t write_versions(struct file *file, char *buf, size_t size);
78 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
79 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
82 static ssize_t (*write_op[])(struct file *, char *, size_t) = {
83 [NFSD_Svc] = write_svc,
84 [NFSD_Add] = write_add,
85 [NFSD_Del] = write_del,
86 [NFSD_Export] = write_export,
87 [NFSD_Unexport] = write_unexport,
88 [NFSD_Getfd] = write_getfd,
89 [NFSD_Getfs] = write_getfs,
90 [NFSD_Fh] = write_filehandle,
91 [NFSD_Threads] = write_threads,
92 [NFSD_Versions] = write_versions,
94 [NFSD_Leasetime] = write_leasetime,
95 [NFSD_RecoveryDir] = write_recoverydir,
99 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
101 ino_t ino = file->f_dentry->d_inode->i_ino;
105 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
108 data = simple_transaction_get(file, buf, size);
110 return PTR_ERR(data);
112 rv = write_op[ino](file, data, size);
114 simple_transaction_set(file, rv);
120 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
122 if (! file->private_data) {
123 /* An attempt to read a transaction file without writing
124 * causes a 0-byte write so that the file can return
127 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
131 return simple_transaction_read(file, buf, size, pos);
134 static const struct file_operations transaction_ops = {
135 .write = nfsctl_transaction_write,
136 .read = nfsctl_transaction_read,
137 .release = simple_transaction_release,
140 extern struct seq_operations nfs_exports_op;
141 static int exports_open(struct inode *inode, struct file *file)
143 return seq_open(file, &nfs_exports_op);
146 static const struct file_operations exports_operations = {
147 .open = exports_open,
150 .release = seq_release,
153 /*----------------------------------------------------------------------------*/
155 * payload - write methods
156 * If the method has a response, the response should be put in buf,
157 * and the length returned. Otherwise return 0 or and -error.
160 static ssize_t write_svc(struct file *file, char *buf, size_t size)
162 struct nfsctl_svc *data;
163 if (size < sizeof(*data))
165 data = (struct nfsctl_svc*) buf;
166 return nfsd_svc(data->svc_port, data->svc_nthreads);
169 static ssize_t write_add(struct file *file, char *buf, size_t size)
171 struct nfsctl_client *data;
172 if (size < sizeof(*data))
174 data = (struct nfsctl_client *)buf;
175 return exp_addclient(data);
178 static ssize_t write_del(struct file *file, char *buf, size_t size)
180 struct nfsctl_client *data;
181 if (size < sizeof(*data))
183 data = (struct nfsctl_client *)buf;
184 return exp_delclient(data);
187 static ssize_t write_export(struct file *file, char *buf, size_t size)
189 struct nfsctl_export *data;
190 if (size < sizeof(*data))
192 data = (struct nfsctl_export*)buf;
193 return exp_export(data);
196 static ssize_t write_unexport(struct file *file, char *buf, size_t size)
198 struct nfsctl_export *data;
200 if (size < sizeof(*data))
202 data = (struct nfsctl_export*)buf;
203 return exp_unexport(data);
206 static ssize_t write_getfs(struct file *file, char *buf, size_t size)
208 struct nfsctl_fsparm *data;
209 struct sockaddr_in *sin;
210 struct auth_domain *clp;
212 struct knfsd_fh *res;
214 if (size < sizeof(*data))
216 data = (struct nfsctl_fsparm*)buf;
217 err = -EPROTONOSUPPORT;
218 if (data->gd_addr.sa_family != AF_INET)
220 sin = (struct sockaddr_in *)&data->gd_addr;
221 if (data->gd_maxlen > NFS3_FHSIZE)
222 data->gd_maxlen = NFS3_FHSIZE;
224 res = (struct knfsd_fh*)buf;
227 if (!(clp = auth_unix_lookup(sin->sin_addr)))
230 err = exp_rootfh(clp, data->gd_path, res, data->gd_maxlen);
231 auth_domain_put(clp);
235 err = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
240 static ssize_t write_getfd(struct file *file, char *buf, size_t size)
242 struct nfsctl_fdparm *data;
243 struct sockaddr_in *sin;
244 struct auth_domain *clp;
249 if (size < sizeof(*data))
251 data = (struct nfsctl_fdparm*)buf;
252 err = -EPROTONOSUPPORT;
253 if (data->gd_addr.sa_family != AF_INET)
256 if (data->gd_version < 2 || data->gd_version > NFSSVC_MAXVERS)
260 sin = (struct sockaddr_in *)&data->gd_addr;
262 if (!(clp = auth_unix_lookup(sin->sin_addr)))
265 err = exp_rootfh(clp, data->gd_path, &fh, NFS_FHSIZE);
266 auth_domain_put(clp);
271 memset(res,0, NFS_FHSIZE);
272 memcpy(res, &fh.fh_base, fh.fh_size);
279 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
282 * domain path maxsize
286 * qword quoting is used, so filehandle will be \x....
292 struct auth_domain *dom;
295 if (buf[size-1] != '\n')
300 len = qword_get(&mesg, dname, size);
301 if (len <= 0) return -EINVAL;
304 len = qword_get(&mesg, path, size);
305 if (len <= 0) return -EINVAL;
307 len = get_int(&mesg, &maxsize);
311 if (maxsize < NFS_FHSIZE)
313 if (maxsize > NFS3_FHSIZE)
314 maxsize = NFS3_FHSIZE;
316 if (qword_get(&mesg, mesg, size)>0)
319 /* we have all the words, they are in buf.. */
320 dom = unix_domain_find(dname);
324 len = exp_rootfh(dom, path, &fh, maxsize);
325 auth_domain_put(dom);
329 mesg = buf; len = SIMPLE_TRANSACTION_LIMIT;
330 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size);
335 extern int nfsd_nrthreads(void);
337 static ssize_t write_threads(struct file *file, char *buf, size_t size)
339 /* if size > 0, look for a number of threads and call nfsd_svc
340 * then write out number of threads as reply
346 rv = get_int(&mesg, &newthreads);
351 rv = nfsd_svc(2049, newthreads);
355 sprintf(buf, "%d\n", nfsd_nrthreads());
359 static ssize_t write_versions(struct file *file, char *buf, size_t size)
363 * [-/+]vers [-/+]vers ...
373 /* Cannot change versions without updating
374 * nfsd_serv->sv_xdrsize, and reallocing
375 * rq_argp and rq_resp
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);
395 nfsd_vers(num, sign == '-' ? NFSD_CLEAR : NFSD_SET);
402 } while ((len = qword_get(&mesg, vers, size)) > 0);
403 /* If all get turned off, turn them back on, as
404 * having no versions is BAD
406 nfsd_reset_versions();
408 /* Now write current state into reply buffer */
411 for (num=2 ; num <= 4 ; num++)
412 if (nfsd_vers(num, NFSD_AVAIL)) {
413 len += sprintf(buf+len, "%s%c%d", sep,
414 nfsd_vers(num, NFSD_TEST)?'+':'-',
418 len += sprintf(buf+len, "\n");
422 #ifdef CONFIG_NFSD_V4
423 extern time_t nfs4_leasetime(void);
425 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
427 /* if size > 10 seconds, call
428 * nfs4_reset_lease() then write out the new lease (seconds) as reply
435 rv = get_int(&mesg, &lease);
438 if (lease < 10 || lease > 3600)
440 nfs4_reset_lease(lease);
442 sprintf(buf, "%ld\n", nfs4_lease_time());
446 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
452 if (size > PATH_MAX || buf[size-1] != '\n')
457 len = qword_get(&mesg, recdir, size);
461 status = nfs4_reset_recoverydir(recdir);
466 /*----------------------------------------------------------------------------*/
468 * populating the filesystem.
471 static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
473 static struct tree_descr nfsd_files[] = {
474 [NFSD_Svc] = {".svc", &transaction_ops, S_IWUSR},
475 [NFSD_Add] = {".add", &transaction_ops, S_IWUSR},
476 [NFSD_Del] = {".del", &transaction_ops, S_IWUSR},
477 [NFSD_Export] = {".export", &transaction_ops, S_IWUSR},
478 [NFSD_Unexport] = {".unexport", &transaction_ops, S_IWUSR},
479 [NFSD_Getfd] = {".getfd", &transaction_ops, S_IWUSR|S_IRUSR},
480 [NFSD_Getfs] = {".getfs", &transaction_ops, S_IWUSR|S_IRUSR},
481 [NFSD_List] = {"exports", &exports_operations, S_IRUGO},
482 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
483 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
484 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
485 #ifdef CONFIG_NFSD_V4
486 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
487 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
491 return simple_fill_super(sb, 0x6e667364, nfsd_files);
494 static int nfsd_get_sb(struct file_system_type *fs_type,
495 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
497 return get_sb_single(fs_type, flags, data, nfsd_fill_super, mnt);
500 static struct file_system_type nfsd_fs_type = {
501 .owner = THIS_MODULE,
503 .get_sb = nfsd_get_sb,
504 .kill_sb = kill_litter_super,
507 static int __init init_nfsd(void)
510 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
512 nfsd_stat_init(); /* Statistics */
513 nfsd_cache_init(); /* RPC reply cache */
514 nfsd_export_init(); /* Exports table */
515 nfsd_lockd_init(); /* lockd->nfsd callbacks */
516 nfs4_state_init(); /* NFSv4 locking state */
517 nfsd_idmap_init(); /* Name to ID mapping */
518 if (proc_mkdir("fs/nfs", NULL)) {
519 struct proc_dir_entry *entry;
520 entry = create_proc_entry("fs/nfs/exports", 0, NULL);
522 entry->proc_fops = &exports_operations;
524 retval = register_filesystem(&nfsd_fs_type);
526 nfsd_export_shutdown();
527 nfsd_cache_shutdown();
528 remove_proc_entry("fs/nfs/exports", NULL);
529 remove_proc_entry("fs/nfs", NULL);
530 nfsd_stat_shutdown();
531 nfsd_lockd_shutdown();
536 static void __exit exit_nfsd(void)
538 nfsd_export_shutdown();
539 nfsd_cache_shutdown();
540 remove_proc_entry("fs/nfs/exports", NULL);
541 remove_proc_entry("fs/nfs", NULL);
542 nfsd_stat_shutdown();
543 nfsd_lockd_shutdown();
544 nfsd_idmap_shutdown();
545 unregister_filesystem(&nfsd_fs_type);
548 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
549 MODULE_LICENSE("GPL");
550 module_init(init_nfsd)
551 module_exit(exit_nfsd)