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>
27 #include <linux/ctype.h>
29 #include <linux/nfs.h>
30 #include <linux/nfsd_idmap.h>
31 #include <linux/lockd/bind.h>
32 #include <linux/sunrpc/svc.h>
33 #include <linux/sunrpc/svcsock.h>
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>
40 #include <asm/uaccess.h>
43 * We have a single directory with 9 nodes in it.
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
70 * write() for these nodes.
72 static ssize_t
write_svc(struct file
*file
, char *buf
, size_t size
);
73 static ssize_t
write_add(struct file
*file
, char *buf
, size_t size
);
74 static ssize_t
write_del(struct file
*file
, char *buf
, size_t size
);
75 static ssize_t
write_export(struct file
*file
, char *buf
, size_t size
);
76 static ssize_t
write_unexport(struct file
*file
, char *buf
, size_t size
);
77 static ssize_t
write_getfd(struct file
*file
, char *buf
, size_t size
);
78 static ssize_t
write_getfs(struct file
*file
, char *buf
, size_t size
);
79 static ssize_t
write_filehandle(struct file
*file
, char *buf
, size_t size
);
80 static ssize_t
write_threads(struct file
*file
, char *buf
, size_t size
);
81 static ssize_t
write_versions(struct file
*file
, char *buf
, size_t size
);
82 static ssize_t
write_ports(struct file
*file
, char *buf
, size_t size
);
84 static ssize_t
write_leasetime(struct file
*file
, char *buf
, size_t size
);
85 static ssize_t
write_recoverydir(struct file
*file
, char *buf
, size_t size
);
88 static 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
,
98 [NFSD_Versions
] = write_versions
,
99 [NFSD_Ports
] = write_ports
,
100 #ifdef CONFIG_NFSD_V4
101 [NFSD_Leasetime
] = write_leasetime
,
102 [NFSD_RecoveryDir
] = write_recoverydir
,
106 static ssize_t
nfsctl_transaction_write(struct file
*file
, const char __user
*buf
, size_t size
, loff_t
*pos
)
108 ino_t ino
= file
->f_dentry
->d_inode
->i_ino
;
112 if (ino
>= ARRAY_SIZE(write_op
) || !write_op
[ino
])
115 data
= simple_transaction_get(file
, buf
, size
);
117 return PTR_ERR(data
);
119 rv
= write_op
[ino
](file
, data
, size
);
121 simple_transaction_set(file
, rv
);
127 static ssize_t
nfsctl_transaction_read(struct file
*file
, char __user
*buf
, size_t size
, loff_t
*pos
)
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
134 ssize_t rv
= nfsctl_transaction_write(file
, buf
, 0, pos
);
138 return simple_transaction_read(file
, buf
, size
, pos
);
141 static const struct file_operations transaction_ops
= {
142 .write
= nfsctl_transaction_write
,
143 .read
= nfsctl_transaction_read
,
144 .release
= simple_transaction_release
,
147 extern struct seq_operations nfs_exports_op
;
148 static int exports_open(struct inode
*inode
, struct file
*file
)
150 return seq_open(file
, &nfs_exports_op
);
153 static const struct file_operations exports_operations
= {
154 .open
= exports_open
,
157 .release
= seq_release
,
160 /*----------------------------------------------------------------------------*/
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.
167 static ssize_t
write_svc(struct file
*file
, char *buf
, size_t size
)
169 struct nfsctl_svc
*data
;
170 if (size
< sizeof(*data
))
172 data
= (struct nfsctl_svc
*) buf
;
173 return nfsd_svc(data
->svc_port
, data
->svc_nthreads
);
176 static ssize_t
write_add(struct file
*file
, char *buf
, size_t size
)
178 struct nfsctl_client
*data
;
179 if (size
< sizeof(*data
))
181 data
= (struct nfsctl_client
*)buf
;
182 return exp_addclient(data
);
185 static ssize_t
write_del(struct file
*file
, char *buf
, size_t size
)
187 struct nfsctl_client
*data
;
188 if (size
< sizeof(*data
))
190 data
= (struct nfsctl_client
*)buf
;
191 return exp_delclient(data
);
194 static ssize_t
write_export(struct file
*file
, char *buf
, size_t size
)
196 struct nfsctl_export
*data
;
197 if (size
< sizeof(*data
))
199 data
= (struct nfsctl_export
*)buf
;
200 return exp_export(data
);
203 static ssize_t
write_unexport(struct file
*file
, char *buf
, size_t size
)
205 struct nfsctl_export
*data
;
207 if (size
< sizeof(*data
))
209 data
= (struct nfsctl_export
*)buf
;
210 return exp_unexport(data
);
213 static ssize_t
write_getfs(struct file
*file
, char *buf
, size_t size
)
215 struct nfsctl_fsparm
*data
;
216 struct sockaddr_in
*sin
;
217 struct auth_domain
*clp
;
219 struct knfsd_fh
*res
;
221 if (size
< sizeof(*data
))
223 data
= (struct nfsctl_fsparm
*)buf
;
224 err
= -EPROTONOSUPPORT
;
225 if (data
->gd_addr
.sa_family
!= AF_INET
)
227 sin
= (struct sockaddr_in
*)&data
->gd_addr
;
228 if (data
->gd_maxlen
> NFS3_FHSIZE
)
229 data
->gd_maxlen
= NFS3_FHSIZE
;
231 res
= (struct knfsd_fh
*)buf
;
234 if (!(clp
= auth_unix_lookup(sin
->sin_addr
)))
237 err
= exp_rootfh(clp
, data
->gd_path
, res
, data
->gd_maxlen
);
238 auth_domain_put(clp
);
242 err
= res
->fh_size
+ (int)&((struct knfsd_fh
*)0)->fh_base
;
247 static ssize_t
write_getfd(struct file
*file
, char *buf
, size_t size
)
249 struct nfsctl_fdparm
*data
;
250 struct sockaddr_in
*sin
;
251 struct auth_domain
*clp
;
256 if (size
< sizeof(*data
))
258 data
= (struct nfsctl_fdparm
*)buf
;
259 err
= -EPROTONOSUPPORT
;
260 if (data
->gd_addr
.sa_family
!= AF_INET
)
263 if (data
->gd_version
< 2 || data
->gd_version
> NFSSVC_MAXVERS
)
267 sin
= (struct sockaddr_in
*)&data
->gd_addr
;
269 if (!(clp
= auth_unix_lookup(sin
->sin_addr
)))
272 err
= exp_rootfh(clp
, data
->gd_path
, &fh
, NFS_FHSIZE
);
273 auth_domain_put(clp
);
278 memset(res
,0, NFS_FHSIZE
);
279 memcpy(res
, &fh
.fh_base
, fh
.fh_size
);
286 static ssize_t
write_filehandle(struct file
*file
, char *buf
, size_t size
)
289 * domain path maxsize
293 * qword quoting is used, so filehandle will be \x....
299 struct auth_domain
*dom
;
302 if (buf
[size
-1] != '\n')
307 len
= qword_get(&mesg
, dname
, size
);
308 if (len
<= 0) return -EINVAL
;
311 len
= qword_get(&mesg
, path
, size
);
312 if (len
<= 0) return -EINVAL
;
314 len
= get_int(&mesg
, &maxsize
);
318 if (maxsize
< NFS_FHSIZE
)
320 if (maxsize
> NFS3_FHSIZE
)
321 maxsize
= NFS3_FHSIZE
;
323 if (qword_get(&mesg
, mesg
, size
)>0)
326 /* we have all the words, they are in buf.. */
327 dom
= unix_domain_find(dname
);
331 len
= exp_rootfh(dom
, path
, &fh
, maxsize
);
332 auth_domain_put(dom
);
336 mesg
= buf
; len
= SIMPLE_TRANSACTION_LIMIT
;
337 qword_addhex(&mesg
, &len
, (char*)&fh
.fh_base
, fh
.fh_size
);
342 extern int nfsd_nrthreads(void);
344 static ssize_t
write_threads(struct file
*file
, char *buf
, size_t size
)
346 /* if size > 0, look for a number of threads and call nfsd_svc
347 * then write out number of threads as reply
353 rv
= get_int(&mesg
, &newthreads
);
358 rv
= nfsd_svc(2049, newthreads
);
362 sprintf(buf
, "%d\n", nfsd_nrthreads());
366 static ssize_t
write_versions(struct file
*file
, char *buf
, size_t size
)
370 * [-/+]vers [-/+]vers ...
380 /* Cannot change versions without updating
381 * nfsd_serv->sv_xdrsize, and reallocing
382 * rq_argp and rq_resp
385 if (buf
[size
-1] != '\n')
390 len
= qword_get(&mesg
, vers
, size
);
391 if (len
<= 0) return -EINVAL
;
394 if (sign
== '+' || sign
== '-')
395 num
= simple_strtol((vers
+1), NULL
, 0);
397 num
= simple_strtol(vers
, NULL
, 0);
402 nfsd_vers(num
, sign
== '-' ? NFSD_CLEAR
: NFSD_SET
);
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
413 nfsd_reset_versions();
415 /* Now write current state into reply buffer */
418 for (num
=2 ; num
<= 4 ; num
++)
419 if (nfsd_vers(num
, NFSD_AVAIL
)) {
420 len
+= sprintf(buf
+len
, "%s%c%d", sep
,
421 nfsd_vers(num
, NFSD_TEST
)?'+':'-',
425 len
+= sprintf(buf
+len
, "\n");
429 static ssize_t
write_ports(struct file
*file
, char *buf
, size_t size
)
435 len
= svc_sock_names(buf
, nfsd_serv
, NULL
);
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.
445 if (isdigit(buf
[0])) {
449 err
= get_int(&mesg
, &fd
);
454 err
= nfsd_create_serv();
457 err
= svc_addsock(nfsd_serv
, fd
, buf
, &proto
);
458 /* Decrease the count, but don't shutdown the
463 nfsd_serv
->sv_nrthreads
--;
468 char *toclose
= kstrdup(buf
+1, GFP_KERNEL
);
474 len
= svc_sock_names(buf
, nfsd_serv
, toclose
);
482 #ifdef CONFIG_NFSD_V4
483 extern time_t nfs4_leasetime(void);
485 static ssize_t
write_leasetime(struct file
*file
, char *buf
, size_t size
)
487 /* if size > 10 seconds, call
488 * nfs4_reset_lease() then write out the new lease (seconds) as reply
495 rv
= get_int(&mesg
, &lease
);
498 if (lease
< 10 || lease
> 3600)
500 nfs4_reset_lease(lease
);
502 sprintf(buf
, "%ld\n", nfs4_lease_time());
506 static ssize_t
write_recoverydir(struct file
*file
, char *buf
, size_t size
)
512 if (size
> PATH_MAX
|| buf
[size
-1] != '\n')
517 len
= qword_get(&mesg
, recdir
, size
);
521 status
= nfs4_reset_recoverydir(recdir
);
526 /*----------------------------------------------------------------------------*/
528 * populating the filesystem.
531 static int nfsd_fill_super(struct super_block
* sb
, void * data
, int silent
)
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
},
544 [NFSD_Versions
] = {"versions", &transaction_ops
, S_IWUSR
|S_IRUSR
},
545 [NFSD_Ports
] = {"portlist", &transaction_ops
, S_IWUSR
|S_IRUGO
},
546 #ifdef CONFIG_NFSD_V4
547 [NFSD_Leasetime
] = {"nfsv4leasetime", &transaction_ops
, S_IWUSR
|S_IRUSR
},
548 [NFSD_RecoveryDir
] = {"nfsv4recoverydir", &transaction_ops
, S_IWUSR
|S_IRUSR
},
552 return simple_fill_super(sb
, 0x6e667364, nfsd_files
);
555 static int nfsd_get_sb(struct file_system_type
*fs_type
,
556 int flags
, const char *dev_name
, void *data
, struct vfsmount
*mnt
)
558 return get_sb_single(fs_type
, flags
, data
, nfsd_fill_super
, mnt
);
561 static struct file_system_type nfsd_fs_type
= {
562 .owner
= THIS_MODULE
,
564 .get_sb
= nfsd_get_sb
,
565 .kill_sb
= kill_litter_super
,
568 static int __init
init_nfsd(void)
571 printk(KERN_INFO
"Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");
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 */
577 nfs4_state_init(); /* NFSv4 locking state */
578 nfsd_idmap_init(); /* Name to ID mapping */
579 if (proc_mkdir("fs/nfs", NULL
)) {
580 struct proc_dir_entry
*entry
;
581 entry
= create_proc_entry("fs/nfs/exports", 0, NULL
);
583 entry
->proc_fops
= &exports_operations
;
585 retval
= register_filesystem(&nfsd_fs_type
);
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();
597 static void __exit
exit_nfsd(void)
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();
605 nfsd_idmap_shutdown();
606 unregister_filesystem(&nfsd_fs_type
);
609 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
610 MODULE_LICENSE("GPL");
611 module_init(init_nfsd
)
612 module_exit(exit_nfsd
)