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_list(struct rpc_inode *rpci, struct list_head *head, int err)
44 struct rpc_pipe_msg *msg;
45 void (*destroy_msg)(struct rpc_pipe_msg *);
47 destroy_msg = rpci->ops->destroy_msg;
48 while (!list_empty(head)) {
49 msg = list_entry(head->next, struct rpc_pipe_msg, list);
50 list_del_init(&msg->list);
57 __rpc_purge_upcall(struct inode *inode, int err)
59 struct rpc_inode *rpci = RPC_I(inode);
61 __rpc_purge_list(rpci, &rpci->pipe, err);
63 wake_up(&rpci->waitq);
67 rpc_timeout_upcall_queue(void *data)
69 struct rpc_inode *rpci = (struct rpc_inode *)data;
70 struct inode *inode = &rpci->vfs_inode;
73 if (rpci->ops == NULL)
75 if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
76 __rpc_purge_upcall(inode, -ETIMEDOUT);
82 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
84 struct rpc_inode *rpci = RPC_I(inode);
88 if (rpci->ops == NULL)
91 list_add_tail(&msg->list, &rpci->pipe);
92 rpci->pipelen += msg->len;
94 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
95 if (list_empty(&rpci->pipe))
96 schedule_delayed_work(&rpci->queue_timeout,
98 list_add_tail(&msg->list, &rpci->pipe);
99 rpci->pipelen += msg->len;
104 wake_up(&rpci->waitq);
109 rpc_inode_setowner(struct inode *inode, void *private)
111 RPC_I(inode)->private = private;
115 rpc_close_pipes(struct inode *inode)
117 struct rpc_inode *rpci = RPC_I(inode);
120 if (rpci->ops != NULL) {
122 __rpc_purge_list(rpci, &rpci->in_upcall, -EPIPE);
123 __rpc_purge_upcall(inode, -EPIPE);
125 if (rpci->ops->release_pipe)
126 rpci->ops->release_pipe(inode);
129 rpc_inode_setowner(inode, NULL);
131 cancel_delayed_work(&rpci->queue_timeout);
132 flush_scheduled_work();
135 static struct inode *
136 rpc_alloc_inode(struct super_block *sb)
138 struct rpc_inode *rpci;
139 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
142 return &rpci->vfs_inode;
146 rpc_destroy_inode(struct inode *inode)
148 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
152 rpc_pipe_open(struct inode *inode, struct file *filp)
154 struct rpc_inode *rpci = RPC_I(inode);
158 if (rpci->ops != NULL) {
159 if (filp->f_mode & FMODE_READ)
161 if (filp->f_mode & FMODE_WRITE)
170 rpc_pipe_release(struct inode *inode, struct file *filp)
172 struct rpc_inode *rpci = RPC_I(inode);
173 struct rpc_pipe_msg *msg;
176 if (rpci->ops == NULL)
178 msg = (struct rpc_pipe_msg *)filp->private_data;
180 msg->errno = -EAGAIN;
181 list_del_init(&msg->list);
182 rpci->ops->destroy_msg(msg);
184 if (filp->f_mode & FMODE_WRITE)
186 if (filp->f_mode & FMODE_READ)
189 __rpc_purge_upcall(inode, -EAGAIN);
190 if (rpci->ops->release_pipe)
191 rpci->ops->release_pipe(inode);
198 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
200 struct inode *inode = filp->f_dentry->d_inode;
201 struct rpc_inode *rpci = RPC_I(inode);
202 struct rpc_pipe_msg *msg;
206 if (rpci->ops == NULL) {
210 msg = filp->private_data;
212 if (!list_empty(&rpci->pipe)) {
213 msg = list_entry(rpci->pipe.next,
216 list_move(&msg->list, &rpci->in_upcall);
217 rpci->pipelen -= msg->len;
218 filp->private_data = msg;
224 /* NOTE: it is up to the callback to update msg->copied */
225 res = rpci->ops->upcall(filp, msg, buf, len);
226 if (res < 0 || msg->len == msg->copied) {
227 filp->private_data = NULL;
228 list_del_init(&msg->list);
229 rpci->ops->destroy_msg(msg);
237 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
239 struct inode *inode = filp->f_dentry->d_inode;
240 struct rpc_inode *rpci = RPC_I(inode);
245 if (rpci->ops != NULL)
246 res = rpci->ops->downcall(filp, buf, len);
252 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
254 struct rpc_inode *rpci;
255 unsigned int mask = 0;
257 rpci = RPC_I(filp->f_dentry->d_inode);
258 poll_wait(filp, &rpci->waitq, wait);
260 mask = POLLOUT | POLLWRNORM;
261 if (rpci->ops == NULL)
262 mask |= POLLERR | POLLHUP;
263 if (!list_empty(&rpci->pipe))
264 mask |= POLLIN | POLLRDNORM;
269 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
270 unsigned int cmd, unsigned long arg)
272 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
277 if (rpci->ops == NULL)
280 if (filp->private_data) {
281 struct rpc_pipe_msg *msg;
282 msg = (struct rpc_pipe_msg *)filp->private_data;
283 len += msg->len - msg->copied;
285 return put_user(len, (int __user *)arg);
291 static struct file_operations rpc_pipe_fops = {
292 .owner = THIS_MODULE,
294 .read = rpc_pipe_read,
295 .write = rpc_pipe_write,
296 .poll = rpc_pipe_poll,
297 .ioctl = rpc_pipe_ioctl,
298 .open = rpc_pipe_open,
299 .release = rpc_pipe_release,
303 rpc_show_info(struct seq_file *m, void *v)
305 struct rpc_clnt *clnt = m->private;
307 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
308 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
309 clnt->cl_prog, clnt->cl_vers);
310 seq_printf(m, "address: %u.%u.%u.%u\n",
311 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
312 seq_printf(m, "protocol: %s\n",
313 clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
318 rpc_info_open(struct inode *inode, struct file *file)
320 struct rpc_clnt *clnt;
321 int ret = single_open(file, rpc_show_info, NULL);
324 struct seq_file *m = file->private_data;
326 clnt = RPC_I(inode)->private;
328 atomic_inc(&clnt->cl_users);
331 single_release(inode, file);
340 rpc_info_release(struct inode *inode, struct file *file)
342 struct seq_file *m = file->private_data;
343 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
346 rpc_release_client(clnt);
347 return single_release(inode, file);
350 static struct file_operations rpc_info_operations = {
351 .owner = THIS_MODULE,
352 .open = rpc_info_open,
355 .release = rpc_info_release,
360 * We have a single directory with 1 node in it.
373 * Description of fs contents.
375 struct rpc_filelist {
377 struct file_operations *i_fop;
381 static struct rpc_filelist files[] = {
384 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
388 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
392 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
394 [RPCAUTH_portmap] = {
396 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
400 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
409 static struct rpc_filelist authfiles[] = {
412 .i_fop = &rpc_info_operations,
413 .mode = S_IFREG | S_IRUSR,
420 return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
426 simple_release_fs(&rpc_mount, &rpc_mount_count);
430 rpc_lookup_parent(char *path, struct nameidata *nd)
434 if (rpc_get_mount()) {
435 printk(KERN_WARNING "%s: %s failed to mount "
436 "pseudofilesystem \n", __FILE__, __FUNCTION__);
439 nd->mnt = mntget(rpc_mount);
440 nd->dentry = dget(rpc_mount->mnt_root);
441 nd->last_type = LAST_ROOT;
442 nd->flags = LOOKUP_PARENT;
445 if (path_walk(path, nd)) {
446 printk(KERN_WARNING "%s: %s failed to find path %s\n",
447 __FILE__, __FUNCTION__, path);
455 rpc_release_path(struct nameidata *nd)
461 static struct inode *
462 rpc_get_inode(struct super_block *sb, int mode)
464 struct inode *inode = new_inode(sb);
467 inode->i_mode = mode;
468 inode->i_uid = inode->i_gid = 0;
469 inode->i_blksize = PAGE_CACHE_SIZE;
471 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
472 switch(mode & S_IFMT) {
474 inode->i_fop = &simple_dir_operations;
475 inode->i_op = &simple_dir_inode_operations;
484 * FIXME: This probably has races.
487 rpc_depopulate(struct dentry *parent)
489 struct inode *dir = parent->d_inode;
490 struct list_head *pos, *next;
491 struct dentry *dentry, *dvec[10];
496 spin_lock(&dcache_lock);
497 list_for_each_safe(pos, next, &parent->d_subdirs) {
498 dentry = list_entry(pos, struct dentry, d_child);
499 spin_lock(&dentry->d_lock);
500 if (!d_unhashed(dentry)) {
503 spin_unlock(&dentry->d_lock);
505 if (n == ARRAY_SIZE(dvec))
508 spin_unlock(&dentry->d_lock);
510 spin_unlock(&dcache_lock);
514 if (dentry->d_inode) {
515 rpc_close_pipes(dentry->d_inode);
516 simple_unlink(dir, dentry);
526 rpc_populate(struct dentry *parent,
527 struct rpc_filelist *files,
530 struct inode *inode, *dir = parent->d_inode;
531 void *private = RPC_I(dir)->private;
532 struct dentry *dentry;
536 for (i = start; i < eof; i++) {
537 dentry = d_alloc_name(parent, files[i].name);
540 mode = files[i].mode;
541 inode = rpc_get_inode(dir->i_sb, mode);
548 inode->i_fop = files[i].i_fop;
550 rpc_inode_setowner(inode, private);
553 d_add(dentry, inode);
559 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
560 __FILE__, __FUNCTION__, parent->d_name.name);
565 __rpc_mkdir(struct inode *dir, struct dentry *dentry)
569 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
572 inode->i_ino = iunique(dir->i_sb, 100);
573 d_instantiate(dentry, inode);
575 inode_dir_notify(dir, DN_CREATE);
579 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
580 __FILE__, __FUNCTION__, dentry->d_name.name);
585 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
589 shrink_dcache_parent(dentry);
591 rpc_close_pipes(dentry->d_inode);
592 if ((error = simple_rmdir(dir, dentry)) != 0)
595 inode_dir_notify(dir, DN_DELETE);
602 static struct dentry *
603 rpc_lookup_negative(char *path, struct nameidata *nd)
605 struct dentry *dentry;
609 if ((error = rpc_lookup_parent(path, nd)) != 0)
610 return ERR_PTR(error);
611 dir = nd->dentry->d_inode;
613 dentry = lookup_hash(nd);
616 if (dentry->d_inode) {
618 dentry = ERR_PTR(-EEXIST);
624 rpc_release_path(nd);
630 rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
633 struct dentry *dentry;
637 dentry = rpc_lookup_negative(path, &nd);
640 dir = nd.dentry->d_inode;
641 if ((error = __rpc_mkdir(dir, dentry)) != 0)
643 RPC_I(dentry->d_inode)->private = rpc_client;
644 error = rpc_populate(dentry, authfiles,
645 RPCAUTH_info, RPCAUTH_EOF);
650 rpc_release_path(&nd);
653 rpc_depopulate(dentry);
654 __rpc_rmdir(dir, dentry);
657 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
658 __FILE__, __FUNCTION__, path, error);
659 dentry = ERR_PTR(error);
664 rpc_rmdir(char *path)
667 struct dentry *dentry;
671 if ((error = rpc_lookup_parent(path, &nd)) != 0)
673 dir = nd.dentry->d_inode;
675 dentry = lookup_hash(&nd);
676 if (IS_ERR(dentry)) {
677 error = PTR_ERR(dentry);
680 rpc_depopulate(dentry);
681 error = __rpc_rmdir(dir, dentry);
685 rpc_release_path(&nd);
690 rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
693 struct dentry *dentry;
694 struct inode *dir, *inode;
695 struct rpc_inode *rpci;
697 dentry = rpc_lookup_negative(path, &nd);
700 dir = nd.dentry->d_inode;
701 inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
704 inode->i_ino = iunique(dir->i_sb, 100);
705 inode->i_fop = &rpc_pipe_fops;
706 d_instantiate(dentry, inode);
708 rpci->private = private;
711 inode_dir_notify(dir, DN_CREATE);
714 rpc_release_path(&nd);
718 dentry = ERR_PTR(-ENOMEM);
719 printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
720 __FILE__, __FUNCTION__, path, -ENOMEM);
725 rpc_unlink(char *path)
728 struct dentry *dentry;
732 if ((error = rpc_lookup_parent(path, &nd)) != 0)
734 dir = nd.dentry->d_inode;
736 dentry = lookup_hash(&nd);
737 if (IS_ERR(dentry)) {
738 error = PTR_ERR(dentry);
742 if (dentry->d_inode) {
743 rpc_close_pipes(dentry->d_inode);
744 error = simple_unlink(dir, dentry);
747 inode_dir_notify(dir, DN_DELETE);
750 rpc_release_path(&nd);
755 * populate the filesystem
757 static struct super_operations s_ops = {
758 .alloc_inode = rpc_alloc_inode,
759 .destroy_inode = rpc_destroy_inode,
760 .statfs = simple_statfs,
763 #define RPCAUTH_GSSMAGIC 0x67596969
766 rpc_fill_super(struct super_block *sb, void *data, int silent)
771 sb->s_blocksize = PAGE_CACHE_SIZE;
772 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
773 sb->s_magic = RPCAUTH_GSSMAGIC;
777 inode = rpc_get_inode(sb, S_IFDIR | 0755);
780 root = d_alloc_root(inode);
785 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
795 static struct super_block *
796 rpc_get_sb(struct file_system_type *fs_type,
797 int flags, const char *dev_name, void *data)
799 return get_sb_single(fs_type, flags, data, rpc_fill_super);
802 static struct file_system_type rpc_pipe_fs_type = {
803 .owner = THIS_MODULE,
804 .name = "rpc_pipefs",
805 .get_sb = rpc_get_sb,
806 .kill_sb = kill_litter_super,
810 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
812 struct rpc_inode *rpci = (struct rpc_inode *) foo;
814 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
815 SLAB_CTOR_CONSTRUCTOR) {
816 inode_init_once(&rpci->vfs_inode);
817 rpci->private = NULL;
820 INIT_LIST_HEAD(&rpci->in_upcall);
821 INIT_LIST_HEAD(&rpci->pipe);
823 init_waitqueue_head(&rpci->waitq);
824 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
829 int register_rpc_pipefs(void)
831 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
832 sizeof(struct rpc_inode),
833 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
835 if (!rpc_inode_cachep)
837 register_filesystem(&rpc_pipe_fs_type);
841 void unregister_rpc_pipefs(void)
843 if (kmem_cache_destroy(rpc_inode_cachep))
844 printk(KERN_WARNING "RPC: unable to free inode cache\n");
845 unregister_filesystem(&rpc_pipe_fs_type);