2 * net/sunrpc/rpc_pipe.c
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/pagemap.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/dnotify.h>
19 #include <linux/kernel.h>
21 #include <asm/ioctls.h>
23 #include <linux/poll.h>
24 #include <linux/wait.h>
25 #include <linux/seq_file.h>
27 #include <linux/sunrpc/clnt.h>
28 #include <linux/workqueue.h>
29 #include <linux/sunrpc/rpc_pipe_fs.h>
31 static struct vfsmount *rpc_mount __read_mostly;
32 static int rpc_mount_count;
34 static struct file_system_type rpc_pipe_fs_type;
37 static kmem_cache_t *rpc_inode_cachep __read_mostly;
39 #define RPC_UPCALL_TIMEOUT (30*HZ)
42 __rpc_purge_upcall(struct inode *inode, int err)
44 struct rpc_inode *rpci = RPC_I(inode);
45 struct rpc_pipe_msg *msg;
47 while (!list_empty(&rpci->pipe)) {
48 msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
49 list_del_init(&msg->list);
51 rpci->ops->destroy_msg(msg);
53 while (!list_empty(&rpci->in_upcall)) {
54 msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
55 list_del_init(&msg->list);
57 rpci->ops->destroy_msg(msg);
60 wake_up(&rpci->waitq);
64 rpc_timeout_upcall_queue(void *data)
66 struct rpc_inode *rpci = (struct rpc_inode *)data;
67 struct inode *inode = &rpci->vfs_inode;
70 if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
71 __rpc_purge_upcall(inode, -ETIMEDOUT);
76 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
78 struct rpc_inode *rpci = RPC_I(inode);
82 if (rpci->ops == NULL)
85 list_add_tail(&msg->list, &rpci->pipe);
86 rpci->pipelen += msg->len;
88 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
89 if (list_empty(&rpci->pipe))
90 schedule_delayed_work(&rpci->queue_timeout,
92 list_add_tail(&msg->list, &rpci->pipe);
93 rpci->pipelen += msg->len;
98 wake_up(&rpci->waitq);
103 rpc_inode_setowner(struct inode *inode, void *private)
105 RPC_I(inode)->private = private;
109 rpc_close_pipes(struct inode *inode)
111 struct rpc_inode *rpci = RPC_I(inode);
113 cancel_delayed_work(&rpci->queue_timeout);
114 flush_scheduled_work();
116 if (rpci->ops != NULL) {
118 __rpc_purge_upcall(inode, -EPIPE);
120 if (rpci->ops->release_pipe)
121 rpci->ops->release_pipe(inode);
124 rpc_inode_setowner(inode, NULL);
128 static struct inode *
129 rpc_alloc_inode(struct super_block *sb)
131 struct rpc_inode *rpci;
132 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
135 return &rpci->vfs_inode;
139 rpc_destroy_inode(struct inode *inode)
141 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
145 rpc_pipe_open(struct inode *inode, struct file *filp)
147 struct rpc_inode *rpci = RPC_I(inode);
151 if (rpci->ops != NULL) {
152 if (filp->f_mode & FMODE_READ)
154 if (filp->f_mode & FMODE_WRITE)
163 rpc_pipe_release(struct inode *inode, struct file *filp)
165 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
166 struct rpc_pipe_msg *msg;
169 if (rpci->ops == NULL)
171 msg = (struct rpc_pipe_msg *)filp->private_data;
174 list_del_init(&msg->list);
175 rpci->ops->destroy_msg(msg);
177 if (filp->f_mode & FMODE_WRITE)
179 if (filp->f_mode & FMODE_READ)
182 __rpc_purge_upcall(inode, -EPIPE);
183 if (rpci->ops->release_pipe)
184 rpci->ops->release_pipe(inode);
191 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
193 struct inode *inode = filp->f_dentry->d_inode;
194 struct rpc_inode *rpci = RPC_I(inode);
195 struct rpc_pipe_msg *msg;
199 if (rpci->ops == NULL) {
203 msg = filp->private_data;
205 if (!list_empty(&rpci->pipe)) {
206 msg = list_entry(rpci->pipe.next,
209 list_move(&msg->list, &rpci->in_upcall);
210 rpci->pipelen -= msg->len;
211 filp->private_data = msg;
217 /* NOTE: it is up to the callback to update msg->copied */
218 res = rpci->ops->upcall(filp, msg, buf, len);
219 if (res < 0 || msg->len == msg->copied) {
220 filp->private_data = NULL;
221 list_del_init(&msg->list);
222 rpci->ops->destroy_msg(msg);
230 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
232 struct inode *inode = filp->f_dentry->d_inode;
233 struct rpc_inode *rpci = RPC_I(inode);
238 if (rpci->ops != NULL)
239 res = rpci->ops->downcall(filp, buf, len);
245 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
247 struct rpc_inode *rpci;
248 unsigned int mask = 0;
250 rpci = RPC_I(filp->f_dentry->d_inode);
251 poll_wait(filp, &rpci->waitq, wait);
253 mask = POLLOUT | POLLWRNORM;
254 if (rpci->ops == NULL)
255 mask |= POLLERR | POLLHUP;
256 if (!list_empty(&rpci->pipe))
257 mask |= POLLIN | POLLRDNORM;
262 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
263 unsigned int cmd, unsigned long arg)
265 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
270 if (rpci->ops == NULL)
273 if (filp->private_data) {
274 struct rpc_pipe_msg *msg;
275 msg = (struct rpc_pipe_msg *)filp->private_data;
276 len += msg->len - msg->copied;
278 return put_user(len, (int __user *)arg);
284 static struct file_operations rpc_pipe_fops = {
285 .owner = THIS_MODULE,
287 .read = rpc_pipe_read,
288 .write = rpc_pipe_write,
289 .poll = rpc_pipe_poll,
290 .ioctl = rpc_pipe_ioctl,
291 .open = rpc_pipe_open,
292 .release = rpc_pipe_release,
296 rpc_show_info(struct seq_file *m, void *v)
298 struct rpc_clnt *clnt = m->private;
300 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
301 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
302 clnt->cl_prog, clnt->cl_vers);
303 seq_printf(m, "address: %u.%u.%u.%u\n",
304 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
305 seq_printf(m, "protocol: %s\n",
306 clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
311 rpc_info_open(struct inode *inode, struct file *file)
313 struct rpc_clnt *clnt;
314 int ret = single_open(file, rpc_show_info, NULL);
317 struct seq_file *m = file->private_data;
319 clnt = RPC_I(inode)->private;
321 atomic_inc(&clnt->cl_users);
324 single_release(inode, file);
333 rpc_info_release(struct inode *inode, struct file *file)
335 struct seq_file *m = file->private_data;
336 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
339 rpc_release_client(clnt);
340 return single_release(inode, file);
343 static struct file_operations rpc_info_operations = {
344 .owner = THIS_MODULE,
345 .open = rpc_info_open,
348 .release = rpc_info_release,
353 * We have a single directory with 1 node in it.
366 * Description of fs contents.
368 struct rpc_filelist {
370 struct file_operations *i_fop;
374 static struct rpc_filelist files[] = {
377 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
381 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
385 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
387 [RPCAUTH_portmap] = {
389 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
393 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
402 static struct rpc_filelist authfiles[] = {
405 .i_fop = &rpc_info_operations,
406 .mode = S_IFREG | S_IRUSR,
413 return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
419 simple_release_fs(&rpc_mount, &rpc_mount_count);
423 rpc_lookup_parent(char *path, struct nameidata *nd)
427 if (rpc_get_mount()) {
428 printk(KERN_WARNING "%s: %s failed to mount "
429 "pseudofilesystem \n", __FILE__, __FUNCTION__);
432 nd->mnt = mntget(rpc_mount);
433 nd->dentry = dget(rpc_mount->mnt_root);
434 nd->last_type = LAST_ROOT;
435 nd->flags = LOOKUP_PARENT;
438 if (path_walk(path, nd)) {
439 printk(KERN_WARNING "%s: %s failed to find path %s\n",
440 __FILE__, __FUNCTION__, path);
448 rpc_release_path(struct nameidata *nd)
454 static struct inode *
455 rpc_get_inode(struct super_block *sb, int mode)
457 struct inode *inode = new_inode(sb);
460 inode->i_mode = mode;
461 inode->i_uid = inode->i_gid = 0;
462 inode->i_blksize = PAGE_CACHE_SIZE;
464 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
465 switch(mode & S_IFMT) {
467 inode->i_fop = &simple_dir_operations;
468 inode->i_op = &simple_dir_inode_operations;
477 * FIXME: This probably has races.
480 rpc_depopulate(struct dentry *parent)
482 struct inode *dir = parent->d_inode;
483 struct list_head *pos, *next;
484 struct dentry *dentry, *dvec[10];
489 spin_lock(&dcache_lock);
490 list_for_each_safe(pos, next, &parent->d_subdirs) {
491 dentry = list_entry(pos, struct dentry, d_child);
492 spin_lock(&dentry->d_lock);
493 if (!d_unhashed(dentry)) {
496 spin_unlock(&dentry->d_lock);
498 if (n == ARRAY_SIZE(dvec))
501 spin_unlock(&dentry->d_lock);
503 spin_unlock(&dcache_lock);
507 if (dentry->d_inode) {
508 rpc_close_pipes(dentry->d_inode);
509 simple_unlink(dir, dentry);
519 rpc_populate(struct dentry *parent,
520 struct rpc_filelist *files,
523 struct inode *inode, *dir = parent->d_inode;
524 void *private = RPC_I(dir)->private;
525 struct dentry *dentry;
529 for (i = start; i < eof; i++) {
530 dentry = d_alloc_name(parent, files[i].name);
533 mode = files[i].mode;
534 inode = rpc_get_inode(dir->i_sb, mode);
541 inode->i_fop = files[i].i_fop;
543 rpc_inode_setowner(inode, private);
546 d_add(dentry, inode);
552 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
553 __FILE__, __FUNCTION__, parent->d_name.name);
558 __rpc_mkdir(struct inode *dir, struct dentry *dentry)
562 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
565 inode->i_ino = iunique(dir->i_sb, 100);
566 d_instantiate(dentry, inode);
568 inode_dir_notify(dir, DN_CREATE);
572 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
573 __FILE__, __FUNCTION__, dentry->d_name.name);
578 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
582 shrink_dcache_parent(dentry);
584 rpc_close_pipes(dentry->d_inode);
585 if ((error = simple_rmdir(dir, dentry)) != 0)
588 inode_dir_notify(dir, DN_DELETE);
595 static struct dentry *
596 rpc_lookup_negative(char *path, struct nameidata *nd)
598 struct dentry *dentry;
602 if ((error = rpc_lookup_parent(path, nd)) != 0)
603 return ERR_PTR(error);
604 dir = nd->dentry->d_inode;
606 dentry = lookup_hash(&nd->last, nd->dentry);
609 if (dentry->d_inode) {
611 dentry = ERR_PTR(-EEXIST);
617 rpc_release_path(nd);
623 rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
626 struct dentry *dentry;
630 dentry = rpc_lookup_negative(path, &nd);
633 dir = nd.dentry->d_inode;
634 if ((error = __rpc_mkdir(dir, dentry)) != 0)
636 RPC_I(dentry->d_inode)->private = rpc_client;
637 error = rpc_populate(dentry, authfiles,
638 RPCAUTH_info, RPCAUTH_EOF);
643 rpc_release_path(&nd);
646 rpc_depopulate(dentry);
647 __rpc_rmdir(dir, dentry);
650 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
651 __FILE__, __FUNCTION__, path, error);
652 dentry = ERR_PTR(error);
657 rpc_rmdir(char *path)
660 struct dentry *dentry;
664 if ((error = rpc_lookup_parent(path, &nd)) != 0)
666 dir = nd.dentry->d_inode;
668 dentry = lookup_hash(&nd.last, nd.dentry);
669 if (IS_ERR(dentry)) {
670 error = PTR_ERR(dentry);
673 rpc_depopulate(dentry);
674 error = __rpc_rmdir(dir, dentry);
678 rpc_release_path(&nd);
683 rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
686 struct dentry *dentry;
687 struct inode *dir, *inode;
688 struct rpc_inode *rpci;
690 dentry = rpc_lookup_negative(path, &nd);
693 dir = nd.dentry->d_inode;
694 inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
697 inode->i_ino = iunique(dir->i_sb, 100);
698 inode->i_fop = &rpc_pipe_fops;
699 d_instantiate(dentry, inode);
701 rpci->private = private;
704 inode_dir_notify(dir, DN_CREATE);
707 rpc_release_path(&nd);
711 dentry = ERR_PTR(-ENOMEM);
712 printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
713 __FILE__, __FUNCTION__, path, -ENOMEM);
718 rpc_unlink(char *path)
721 struct dentry *dentry;
725 if ((error = rpc_lookup_parent(path, &nd)) != 0)
727 dir = nd.dentry->d_inode;
729 dentry = lookup_hash(&nd.last, nd.dentry);
730 if (IS_ERR(dentry)) {
731 error = PTR_ERR(dentry);
735 if (dentry->d_inode) {
736 rpc_close_pipes(dentry->d_inode);
737 error = simple_unlink(dir, dentry);
740 inode_dir_notify(dir, DN_DELETE);
743 rpc_release_path(&nd);
748 * populate the filesystem
750 static struct super_operations s_ops = {
751 .alloc_inode = rpc_alloc_inode,
752 .destroy_inode = rpc_destroy_inode,
753 .statfs = simple_statfs,
756 #define RPCAUTH_GSSMAGIC 0x67596969
759 rpc_fill_super(struct super_block *sb, void *data, int silent)
764 sb->s_blocksize = PAGE_CACHE_SIZE;
765 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
766 sb->s_magic = RPCAUTH_GSSMAGIC;
770 inode = rpc_get_inode(sb, S_IFDIR | 0755);
773 root = d_alloc_root(inode);
778 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
788 static struct super_block *
789 rpc_get_sb(struct file_system_type *fs_type,
790 int flags, const char *dev_name, void *data)
792 return get_sb_single(fs_type, flags, data, rpc_fill_super);
795 static struct file_system_type rpc_pipe_fs_type = {
796 .owner = THIS_MODULE,
797 .name = "rpc_pipefs",
798 .get_sb = rpc_get_sb,
799 .kill_sb = kill_litter_super,
803 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
805 struct rpc_inode *rpci = (struct rpc_inode *) foo;
807 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
808 SLAB_CTOR_CONSTRUCTOR) {
809 inode_init_once(&rpci->vfs_inode);
810 rpci->private = NULL;
813 INIT_LIST_HEAD(&rpci->in_upcall);
814 INIT_LIST_HEAD(&rpci->pipe);
816 init_waitqueue_head(&rpci->waitq);
817 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
822 int register_rpc_pipefs(void)
824 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
825 sizeof(struct rpc_inode),
826 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
828 if (!rpc_inode_cachep)
830 register_filesystem(&rpc_pipe_fs_type);
834 void unregister_rpc_pipefs(void)
836 if (kmem_cache_destroy(rpc_inode_cachep))
837 printk(KERN_WARNING "RPC: unable to free inode cache\n");
838 unregister_filesystem(&rpc_pipe_fs_type);