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);
83 list_add_tail(&msg->list, &rpci->pipe);
84 rpci->pipelen += msg->len;
85 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
86 if (list_empty(&rpci->pipe))
87 schedule_delayed_work(&rpci->queue_timeout,
89 list_add_tail(&msg->list, &rpci->pipe);
90 rpci->pipelen += msg->len;
94 wake_up(&rpci->waitq);
99 rpc_close_pipes(struct inode *inode)
101 struct rpc_inode *rpci = RPC_I(inode);
103 cancel_delayed_work(&rpci->queue_timeout);
104 flush_scheduled_work();
106 if (rpci->ops != NULL) {
108 __rpc_purge_upcall(inode, -EPIPE);
110 if (rpci->ops->release_pipe)
111 rpci->ops->release_pipe(inode);
118 rpc_inode_setowner(struct inode *inode, void *private)
120 RPC_I(inode)->private = private;
123 static struct inode *
124 rpc_alloc_inode(struct super_block *sb)
126 struct rpc_inode *rpci;
127 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
130 return &rpci->vfs_inode;
134 rpc_destroy_inode(struct inode *inode)
136 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
140 rpc_pipe_open(struct inode *inode, struct file *filp)
142 struct rpc_inode *rpci = RPC_I(inode);
146 if (rpci->ops != NULL) {
147 if (filp->f_mode & FMODE_READ)
149 if (filp->f_mode & FMODE_WRITE)
158 rpc_pipe_release(struct inode *inode, struct file *filp)
160 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
161 struct rpc_pipe_msg *msg;
164 if (rpci->ops == NULL)
166 msg = (struct rpc_pipe_msg *)filp->private_data;
169 list_del_init(&msg->list);
170 rpci->ops->destroy_msg(msg);
172 if (filp->f_mode & FMODE_WRITE)
174 if (filp->f_mode & FMODE_READ)
177 __rpc_purge_upcall(inode, -EPIPE);
178 if (rpci->ops->release_pipe)
179 rpci->ops->release_pipe(inode);
186 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
188 struct inode *inode = filp->f_dentry->d_inode;
189 struct rpc_inode *rpci = RPC_I(inode);
190 struct rpc_pipe_msg *msg;
194 if (rpci->ops == NULL) {
198 msg = filp->private_data;
200 if (!list_empty(&rpci->pipe)) {
201 msg = list_entry(rpci->pipe.next,
204 list_move(&msg->list, &rpci->in_upcall);
205 rpci->pipelen -= msg->len;
206 filp->private_data = msg;
212 /* NOTE: it is up to the callback to update msg->copied */
213 res = rpci->ops->upcall(filp, msg, buf, len);
214 if (res < 0 || msg->len == msg->copied) {
215 filp->private_data = NULL;
216 list_del_init(&msg->list);
217 rpci->ops->destroy_msg(msg);
225 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
227 struct inode *inode = filp->f_dentry->d_inode;
228 struct rpc_inode *rpci = RPC_I(inode);
233 if (rpci->ops != NULL)
234 res = rpci->ops->downcall(filp, buf, len);
240 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
242 struct rpc_inode *rpci;
243 unsigned int mask = 0;
245 rpci = RPC_I(filp->f_dentry->d_inode);
246 poll_wait(filp, &rpci->waitq, wait);
248 mask = POLLOUT | POLLWRNORM;
249 if (rpci->ops == NULL)
250 mask |= POLLERR | POLLHUP;
251 if (!list_empty(&rpci->pipe))
252 mask |= POLLIN | POLLRDNORM;
257 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
258 unsigned int cmd, unsigned long arg)
260 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
265 if (rpci->ops == NULL)
268 if (filp->private_data) {
269 struct rpc_pipe_msg *msg;
270 msg = (struct rpc_pipe_msg *)filp->private_data;
271 len += msg->len - msg->copied;
273 return put_user(len, (int __user *)arg);
279 static struct file_operations rpc_pipe_fops = {
280 .owner = THIS_MODULE,
282 .read = rpc_pipe_read,
283 .write = rpc_pipe_write,
284 .poll = rpc_pipe_poll,
285 .ioctl = rpc_pipe_ioctl,
286 .open = rpc_pipe_open,
287 .release = rpc_pipe_release,
291 rpc_show_info(struct seq_file *m, void *v)
293 struct rpc_clnt *clnt = m->private;
295 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
296 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
297 clnt->cl_prog, clnt->cl_vers);
298 seq_printf(m, "address: %u.%u.%u.%u\n",
299 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
300 seq_printf(m, "protocol: %s\n",
301 clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
306 rpc_info_open(struct inode *inode, struct file *file)
308 struct rpc_clnt *clnt;
309 int ret = single_open(file, rpc_show_info, NULL);
312 struct seq_file *m = file->private_data;
314 clnt = RPC_I(inode)->private;
316 atomic_inc(&clnt->cl_users);
319 single_release(inode, file);
328 rpc_info_release(struct inode *inode, struct file *file)
330 struct seq_file *m = file->private_data;
331 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
334 rpc_release_client(clnt);
335 return single_release(inode, file);
338 static struct file_operations rpc_info_operations = {
339 .owner = THIS_MODULE,
340 .open = rpc_info_open,
343 .release = rpc_info_release,
348 * We have a single directory with 1 node in it.
361 * Description of fs contents.
363 struct rpc_filelist {
365 struct file_operations *i_fop;
369 static struct rpc_filelist files[] = {
372 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
376 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
380 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
382 [RPCAUTH_portmap] = {
384 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
388 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
397 static struct rpc_filelist authfiles[] = {
400 .i_fop = &rpc_info_operations,
401 .mode = S_IFREG | S_IRUSR,
408 return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
414 simple_release_fs(&rpc_mount, &rpc_mount_count);
417 static struct inode *
418 rpc_get_inode(struct super_block *sb, int mode)
420 struct inode *inode = new_inode(sb);
423 inode->i_mode = mode;
424 inode->i_uid = inode->i_gid = 0;
425 inode->i_blksize = PAGE_CACHE_SIZE;
427 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
428 switch(mode & S_IFMT) {
430 inode->i_fop = &simple_dir_operations;
431 inode->i_op = &simple_dir_inode_operations;
440 * FIXME: This probably has races.
443 rpc_depopulate(struct dentry *parent)
445 struct inode *dir = parent->d_inode;
446 struct list_head *pos, *next;
447 struct dentry *dentry, *dvec[10];
452 spin_lock(&dcache_lock);
453 list_for_each_safe(pos, next, &parent->d_subdirs) {
454 dentry = list_entry(pos, struct dentry, d_child);
455 spin_lock(&dentry->d_lock);
456 if (!d_unhashed(dentry)) {
459 spin_unlock(&dentry->d_lock);
461 if (n == ARRAY_SIZE(dvec))
464 spin_unlock(&dentry->d_lock);
466 spin_unlock(&dcache_lock);
470 if (dentry->d_inode) {
471 rpc_close_pipes(dentry->d_inode);
472 rpc_inode_setowner(dentry->d_inode, NULL);
473 simple_unlink(dir, dentry);
483 rpc_populate(struct dentry *parent,
484 struct rpc_filelist *files,
487 struct inode *inode, *dir = parent->d_inode;
488 void *private = RPC_I(dir)->private;
489 struct dentry *dentry;
493 for (i = start; i < eof; i++) {
494 dentry = d_alloc_name(parent, files[i].name);
497 mode = files[i].mode;
498 inode = rpc_get_inode(dir->i_sb, mode);
505 inode->i_fop = files[i].i_fop;
507 rpc_inode_setowner(inode, private);
510 d_add(dentry, inode);
516 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
517 __FILE__, __FUNCTION__, parent->d_name.name);
522 rpc_mkdir(struct dentry *parent, char *name, struct rpc_clnt *rpc_client)
525 struct dentry *dentry;
530 parent = rpc_mount->mnt_root;
532 dir = parent->d_inode;
534 error = rpc_get_mount();
536 return ERR_PTR(error);
539 dentry = lookup_one_len(name, parent, strlen(name));
542 if (dentry->d_inode) {
543 dentry = ERR_PTR(-EEXIST);
547 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
550 inode->i_ino = iunique(dir->i_sb, 100);
552 RPC_I(dentry->d_inode)->private = rpc_client;
554 d_instantiate(dentry, inode);
558 inode_dir_notify(dir, DN_CREATE);
560 error = rpc_populate(dentry, authfiles,
561 RPCAUTH_info, RPCAUTH_EOF);
578 rpc_rmdir(struct dentry *dentry)
580 struct dentry *parent = dentry->d_parent;
582 rpc_depopulate(dentry);
584 down(&parent->d_inode->i_sem);
585 if (dentry->d_inode) {
586 rpc_close_pipes(dentry->d_inode);
587 rpc_inode_setowner(dentry->d_inode, NULL);
588 simple_rmdir(parent->d_inode, dentry);
590 up(&parent->d_inode->i_sem);
592 inode_dir_notify(parent->d_inode, DN_DELETE);
597 rpc_mkpipe(struct dentry *parent, char *name, void *private,
598 struct rpc_pipe_ops *ops, int flags)
600 struct inode *dir = parent->d_inode;
601 struct dentry *dentry;
603 struct rpc_inode *rpci;
606 error = rpc_get_mount();
608 return ERR_PTR(error);
610 down(&parent->d_inode->i_sem);
611 dentry = lookup_one_len(name, parent, strlen(name));
614 if (dentry->d_inode) {
615 dentry = ERR_PTR(-EEXIST);
619 inode = rpc_get_inode(parent->d_inode->i_sb,
620 S_IFSOCK | S_IRUSR | S_IWUSR);
622 dentry = ERR_PTR(-ENOMEM);
626 inode->i_ino = iunique(dir->i_sb, 100);
627 inode->i_fop = &rpc_pipe_fops;
630 rpci->private = private;
634 d_instantiate(dentry, inode);
636 up(&parent->d_inode->i_sem);
638 inode_dir_notify(dir, DN_CREATE);
644 up(&parent->d_inode->i_sem);
650 rpc_unlink(struct dentry *dentry)
652 struct dentry *parent = dentry->d_parent;
654 down(&parent->d_inode->i_sem);
655 if (dentry->d_inode) {
656 rpc_close_pipes(dentry->d_inode);
657 rpc_inode_setowner(dentry->d_inode, NULL);
658 simple_unlink(parent->d_inode, dentry);
660 up(&parent->d_inode->i_sem);
662 inode_dir_notify(parent->d_inode, DN_DELETE);
667 * populate the filesystem
669 static struct super_operations s_ops = {
670 .alloc_inode = rpc_alloc_inode,
671 .destroy_inode = rpc_destroy_inode,
672 .statfs = simple_statfs,
675 #define RPCAUTH_GSSMAGIC 0x67596969
678 rpc_fill_super(struct super_block *sb, void *data, int silent)
683 sb->s_blocksize = PAGE_CACHE_SIZE;
684 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
685 sb->s_magic = RPCAUTH_GSSMAGIC;
689 inode = rpc_get_inode(sb, S_IFDIR | 0755);
692 root = d_alloc_root(inode);
697 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
707 static struct super_block *
708 rpc_get_sb(struct file_system_type *fs_type,
709 int flags, const char *dev_name, void *data)
711 return get_sb_single(fs_type, flags, data, rpc_fill_super);
714 static struct file_system_type rpc_pipe_fs_type = {
715 .owner = THIS_MODULE,
716 .name = "rpc_pipefs",
717 .get_sb = rpc_get_sb,
718 .kill_sb = kill_litter_super,
722 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
724 struct rpc_inode *rpci = (struct rpc_inode *) foo;
726 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
727 SLAB_CTOR_CONSTRUCTOR) {
728 inode_init_once(&rpci->vfs_inode);
729 rpci->private = NULL;
732 INIT_LIST_HEAD(&rpci->in_upcall);
733 INIT_LIST_HEAD(&rpci->pipe);
735 init_waitqueue_head(&rpci->waitq);
736 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
741 int register_rpc_pipefs(void)
743 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
744 sizeof(struct rpc_inode),
745 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
747 if (!rpc_inode_cachep)
749 register_filesystem(&rpc_pipe_fs_type);
753 void unregister_rpc_pipefs(void)
755 if (kmem_cache_destroy(rpc_inode_cachep))
756 printk(KERN_WARNING "RPC: unable to free inode cache\n");
757 unregister_filesystem(&rpc_pipe_fs_type);