Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / net / sunrpc / rpc_pipe.c
blob0f906e481ea2a8100d097f8f5d07d7a4fa6f76c4
1 /*
2 * net/sunrpc/rpc_pipe.c
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
6 * and fs/driverfs/inode.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/version.h>
14 #include <linux/slab.h>
15 #include <linux/string.h>
16 #include <linux/pagemap.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/dnotify.h>
20 #include <linux/kernel.h>
22 #include <asm/ioctls.h>
23 #include <linux/fs.h>
24 #include <linux/poll.h>
25 #include <linux/wait.h>
26 #include <linux/seq_file.h>
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/sunrpc/rpc_pipe_fs.h>
31 static struct vfsmount *rpc_mount;
32 static spinlock_t rpc_mount_lock = SPIN_LOCK_UNLOCKED;
33 static int rpc_mount_count;
35 static struct file_system_type rpc_pipe_fs_type;
38 static kmem_cache_t *rpc_inode_cachep;
40 static void
41 __rpc_purge_upcall(struct inode *inode, int err)
43 struct rpc_inode *rpci = RPC_I(inode);
44 struct rpc_pipe_msg *msg;
46 while (!list_empty(&rpci->pipe)) {
47 msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
48 list_del_init(&msg->list);
49 msg->errno = err;
50 rpci->ops->destroy_msg(msg);
52 rpci->pipelen = 0;
53 wake_up(&rpci->waitq);
56 void
57 rpc_purge_upcall(struct inode *inode, int err)
59 down(&inode->i_sem);
60 __rpc_purge_upcall(inode, err);
61 up(&inode->i_sem);
65 * XXX should only be called in ->downcall
67 void
68 __rpc_purge_current_upcall(struct file *filp)
70 struct rpc_pipe_msg *msg;
72 msg = filp->private_data;
73 filp->private_data = NULL;
75 if (msg != NULL)
76 msg->errno = 0;
79 int
80 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
82 struct rpc_inode *rpci = RPC_I(inode);
83 int res = 0;
85 down(&inode->i_sem);
86 if (rpci->nreaders) {
87 list_add_tail(&msg->list, &rpci->pipe);
88 rpci->pipelen += msg->len;
89 } else
90 res = -EPIPE;
91 up(&inode->i_sem);
92 wake_up(&rpci->waitq);
93 return res;
96 void
97 rpc_inode_setowner(struct inode *inode, void *private)
99 struct rpc_inode *rpci = RPC_I(inode);
100 down(&inode->i_sem);
101 rpci->private = private;
102 if (!private)
103 __rpc_purge_upcall(inode, -EPIPE);
104 up(&inode->i_sem);
107 static struct inode *
108 rpc_alloc_inode(struct super_block *sb)
110 struct rpc_inode *rpci;
111 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
112 if (!rpci)
113 return NULL;
114 return &rpci->vfs_inode;
117 static void
118 rpc_destroy_inode(struct inode *inode)
120 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
123 static int
124 rpc_pipe_open(struct inode *inode, struct file *filp)
126 struct rpc_inode *rpci = RPC_I(inode);
127 int res = -ENXIO;
129 down(&inode->i_sem);
130 if (rpci->private != NULL) {
131 if (filp->f_mode & FMODE_READ)
132 rpci->nreaders ++;
133 res = 0;
135 up(&inode->i_sem);
136 return res;
139 static int
140 rpc_pipe_release(struct inode *inode, struct file *filp)
142 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
143 struct rpc_pipe_msg *msg;
145 msg = (struct rpc_pipe_msg *)filp->private_data;
146 if (msg != NULL) {
147 msg->errno = -EPIPE;
148 rpci->ops->destroy_msg(msg);
150 down(&inode->i_sem);
151 if (filp->f_mode & FMODE_READ)
152 rpci->nreaders --;
153 if (!rpci->nreaders)
154 __rpc_purge_upcall(inode, -EPIPE);
155 up(&inode->i_sem);
156 return 0;
159 static ssize_t
160 rpc_pipe_read(struct file *filp, char *buf, size_t len, loff_t *offset)
162 struct inode *inode = filp->f_dentry->d_inode;
163 struct rpc_inode *rpci = RPC_I(inode);
164 struct rpc_pipe_msg *msg;
165 int res = 0;
167 down(&inode->i_sem);
168 if (!rpci->private) {
169 res = -EPIPE;
170 goto out_unlock;
172 msg = filp->private_data;
173 if (msg == NULL) {
174 if (!list_empty(&rpci->pipe)) {
175 msg = list_entry(rpci->pipe.next,
176 struct rpc_pipe_msg,
177 list);
178 list_del_init(&msg->list);
179 rpci->pipelen -= msg->len;
180 filp->private_data = msg;
182 if (msg == NULL)
183 goto out_unlock;
185 res = rpci->ops->upcall(filp, msg, buf, len);
186 if (res < 0 || msg->len == msg->copied) {
187 filp->private_data = NULL;
188 msg->errno = 0;
189 rpci->ops->destroy_msg(msg);
191 out_unlock:
192 up(&inode->i_sem);
193 return res;
196 static ssize_t
197 rpc_pipe_write(struct file *filp, const char *buf, size_t len, loff_t *offset)
199 struct inode *inode = filp->f_dentry->d_inode;
200 struct rpc_inode *rpci = RPC_I(inode);
201 int res;
203 down(&inode->i_sem);
204 res = -EPIPE;
205 if (rpci->private != NULL)
206 res = rpci->ops->downcall(filp, buf, len);
207 up(&inode->i_sem);
208 return res;
211 static unsigned int
212 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
214 struct rpc_inode *rpci;
215 unsigned int mask = 0;
217 rpci = RPC_I(filp->f_dentry->d_inode);
218 poll_wait(filp, &rpci->waitq, wait);
220 mask = POLLOUT | POLLWRNORM;
221 if (rpci->private == NULL)
222 mask |= POLLERR | POLLHUP;
223 if (!list_empty(&rpci->pipe))
224 mask |= POLLIN | POLLRDNORM;
225 return mask;
228 static int
229 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
230 unsigned int cmd, unsigned long arg)
232 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
233 int len;
235 switch (cmd) {
236 case FIONREAD:
237 if (!rpci->private)
238 return -EPIPE;
239 len = rpci->pipelen;
240 if (filp->private_data) {
241 struct rpc_pipe_msg *msg;
242 msg = (struct rpc_pipe_msg *)filp->private_data;
243 len += msg->len - msg->copied;
245 return put_user(len, (int *)arg);
246 default:
247 return -EINVAL;
251 struct inode_operations rpc_pipe_iops = {
252 .lookup = simple_lookup,
256 struct file_operations rpc_pipe_fops = {
257 .owner = THIS_MODULE,
258 .llseek = no_llseek,
259 .read = rpc_pipe_read,
260 .write = rpc_pipe_write,
261 .poll = rpc_pipe_poll,
262 .ioctl = rpc_pipe_ioctl,
263 .open = rpc_pipe_open,
264 .release = rpc_pipe_release,
267 static int
268 rpc_show_info(struct seq_file *m, void *v)
270 struct rpc_clnt *clnt = m->private;
272 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
273 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
274 clnt->cl_prog, clnt->cl_vers);
275 seq_printf(m, "address: %u.%u.%u.%u\n",
276 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
277 return 0;
280 static int
281 rpc_info_open(struct inode *inode, struct file *file)
283 struct rpc_clnt *clnt;
284 int ret = single_open(file, rpc_show_info, NULL);
286 if (!ret) {
287 struct seq_file *m = file->private_data;
288 down(&inode->i_sem);
289 clnt = RPC_I(inode)->private;
290 if (clnt) {
291 atomic_inc(&clnt->cl_users);
292 m->private = clnt;
293 } else {
294 single_release(inode, file);
295 ret = -EINVAL;
297 up(&inode->i_sem);
299 return ret;
302 static int
303 rpc_info_release(struct inode *inode, struct file *file)
305 struct seq_file *m = file->private_data;
306 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
308 if (clnt)
309 rpc_release_client(clnt);
310 return single_release(inode, file);
313 static struct file_operations rpc_info_operations = {
314 .open = rpc_info_open,
315 .read = seq_read,
316 .llseek = seq_lseek,
317 .release = rpc_info_release,
322 * We have a single directory with 1 node in it.
324 enum {
325 RPCAUTH_Root = 1,
326 RPCAUTH_lockd,
327 RPCAUTH_mount,
328 RPCAUTH_nfs,
329 RPCAUTH_portmap,
330 RPCAUTH_statd,
331 RPCAUTH_RootEOF
335 * Description of fs contents.
337 struct rpc_filelist {
338 char *name;
339 struct file_operations *i_fop;
340 int mode;
343 static struct rpc_filelist files[] = {
344 [RPCAUTH_lockd] = {
345 .name = "lockd",
346 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
348 [RPCAUTH_mount] = {
349 .name = "mount",
350 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
352 [RPCAUTH_nfs] = {
353 .name = "nfs",
354 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
356 [RPCAUTH_portmap] = {
357 .name = "portmap",
358 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
360 [RPCAUTH_statd] = {
361 .name = "statd",
362 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
366 enum {
367 RPCAUTH_info = 2,
368 RPCAUTH_EOF
371 static struct rpc_filelist authfiles[] = {
372 [RPCAUTH_info] = {
373 .name = "info",
374 .i_fop = &rpc_info_operations,
375 .mode = S_IFREG | S_IRUSR,
379 static int
380 rpc_get_mount(void)
382 struct vfsmount * mnt = NULL;
384 spin_lock(&rpc_mount_lock);
385 if (rpc_mount)
386 goto out_get;
387 spin_unlock(&rpc_mount_lock);
388 mnt = kern_mount(&rpc_pipe_fs_type);
389 if (IS_ERR(mnt))
390 return -ENODEV;
391 spin_lock(&rpc_mount_lock);
392 if (!rpc_mount) {
393 rpc_mount = mnt;
394 mnt = NULL;
395 goto out_dontget;
397 out_get:
398 mntget(rpc_mount);
399 out_dontget:
400 ++rpc_mount_count;
401 spin_unlock(&rpc_mount_lock);
402 if (mnt)
403 mntput(mnt);
404 return 0;
407 static void
408 rpc_put_mount(void)
410 struct vfsmount *mnt;
412 spin_lock(&rpc_mount_lock);
413 mnt = rpc_mount;
414 --rpc_mount_count;
415 if (rpc_mount_count == 0)
416 rpc_mount = NULL;
417 else
418 mnt = NULL;
419 spin_unlock(&rpc_mount_lock);
420 if (mnt)
421 mntput(mnt);
424 static int
425 rpc_lookup_path(char *path, struct nameidata *nd, int flags)
427 if (rpc_get_mount()) {
428 printk(KERN_WARNING "%s: %s failed to mount "
429 "pseudofilesystem \n", __FILE__, __FUNCTION__);
430 return -ENODEV;
432 nd->mnt = mntget(rpc_mount);
433 nd->dentry = dget(rpc_mount->mnt_root);
434 nd->last_type = LAST_ROOT;
435 nd->flags = flags;
437 if (path_walk(path, nd)) {
438 printk(KERN_WARNING "%s: %s failed to find path %s\n",
439 __FILE__, __FUNCTION__, path);
440 rpc_put_mount();
441 return -ENOENT;
443 return 0;
446 static void
447 rpc_release_path(struct nameidata *nd)
449 path_release(nd);
450 rpc_put_mount();
453 static struct inode *
454 rpc_get_inode(struct super_block *sb, int mode)
456 struct inode *inode = new_inode(sb);
457 if (!inode)
458 return NULL;
459 inode->i_mode = mode;
460 inode->i_uid = inode->i_gid = 0;
461 inode->i_blksize = PAGE_CACHE_SIZE;
462 inode->i_blocks = 0;
463 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
464 switch(mode & S_IFMT) {
465 case S_IFDIR:
466 inode->i_fop = &simple_dir_operations;
467 inode->i_op = &simple_dir_inode_operations;
468 inode->i_nlink++;
469 default:
470 break;
472 return inode;
476 * FIXME: This probably has races.
478 static void
479 rpc_depopulate(struct dentry *dir)
481 LIST_HEAD(head);
482 struct list_head *pos, *next;
483 struct dentry *dentry;
485 down(&dir->d_inode->i_sem);
486 spin_lock(&dcache_lock);
487 list_for_each_safe(pos, next, &dir->d_subdirs) {
488 dentry = list_entry(pos, struct dentry, d_child);
489 if (!d_unhashed(dentry)) {
490 dget_locked(dentry);
491 list_del(&dentry->d_hash);
492 list_add(&dentry->d_hash, &head);
495 spin_unlock(&dcache_lock);
496 while (!list_empty(&head)) {
497 dentry = list_entry(head.next, struct dentry, d_hash);
498 list_del_init(&dentry->d_hash);
499 if (dentry->d_inode) {
500 rpc_inode_setowner(dentry->d_inode, NULL);
501 simple_unlink(dir->d_inode, dentry);
503 dput(dentry);
505 up(&dir->d_inode->i_sem);
508 static int
509 rpc_populate(struct dentry *dir,
510 struct rpc_filelist *files,
511 int start, int eof)
513 void *private = RPC_I(dir->d_inode)->private;
514 struct qstr name;
515 struct dentry *dentry;
516 struct inode *inode;
517 int mode, i;
518 for (i = start; i < eof; i++) {
519 name.name = files[i].name;
520 name.len = strlen(name.name);
521 name.hash = full_name_hash(name.name, name.len);
522 dentry = d_alloc(dir, &name);
523 if (!dentry)
524 goto out_bad;
525 mode = files[i].mode;
526 inode = rpc_get_inode(dir->d_inode->i_sb, mode);
527 if (!inode) {
528 dput(dentry);
529 goto out_bad;
531 inode->i_ino = i;
532 if (files[i].i_fop)
533 inode->i_fop = files[i].i_fop;
534 if (private)
535 rpc_inode_setowner(inode, private);
536 if (S_ISDIR(mode))
537 dir->d_inode->i_nlink++;
538 d_add(dentry, inode);
540 return 0;
541 out_bad:
542 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
543 __FILE__, __FUNCTION__, dir->d_name.name);
544 return -ENOMEM;
547 static int
548 __rpc_mkdir(struct inode *dir, struct dentry *dentry)
550 struct inode *inode;
552 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
553 if (!inode)
554 goto out_err;
555 inode->i_ino = iunique(dir->i_sb, 100);
556 d_instantiate(dentry, inode);
557 dir->i_nlink++;
558 inode_dir_notify(dir, DN_CREATE);
559 rpc_get_mount();
560 return 0;
561 out_err:
562 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
563 __FILE__, __FUNCTION__, dentry->d_name.name);
564 return -ENOMEM;
567 static int
568 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
570 int error;
572 rpc_inode_setowner(dentry->d_inode, NULL);
573 if ((error = simple_rmdir(dir, dentry)) != 0)
574 return error;
575 if (!error) {
576 inode_dir_notify(dir, DN_DELETE);
577 d_drop(dentry);
578 rpc_put_mount();
580 return 0;
583 struct dentry *
584 rpc_lookup_negative(char *path, struct nameidata *nd)
586 struct dentry *dentry;
587 struct inode *dir;
588 int error;
590 if ((error = rpc_lookup_path(path, nd, LOOKUP_PARENT)) != 0)
591 return ERR_PTR(error);
592 dir = nd->dentry->d_inode;
593 down(&dir->i_sem);
594 dentry = lookup_hash(&nd->last, nd->dentry);
595 if (IS_ERR(dentry))
596 goto out_err;
597 if (dentry->d_inode) {
598 dput(dentry);
599 dentry = ERR_PTR(-EEXIST);
600 goto out_err;
602 return dentry;
603 out_err:
604 up(&dir->i_sem);
605 rpc_release_path(nd);
606 return dentry;
610 struct dentry *
611 rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
613 struct nameidata nd;
614 struct dentry *dentry;
615 struct inode *dir;
616 int error;
618 dentry = rpc_lookup_negative(path, &nd);
619 if (IS_ERR(dentry))
620 return dentry;
621 dir = nd.dentry->d_inode;
622 if ((error = __rpc_mkdir(dir, dentry)) != 0)
623 goto err_dput;
624 RPC_I(dentry->d_inode)->private = rpc_client;
625 error = rpc_populate(dentry, authfiles,
626 RPCAUTH_info, RPCAUTH_EOF);
627 if (error)
628 goto err_depopulate;
629 out:
630 up(&dir->i_sem);
631 rpc_release_path(&nd);
632 return dentry;
633 err_depopulate:
634 rpc_depopulate(dentry);
635 __rpc_rmdir(dir, dentry);
636 err_dput:
637 dput(dentry);
638 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
639 __FILE__, __FUNCTION__, path, error);
640 dentry = ERR_PTR(error);
641 goto out;
645 rpc_rmdir(char *path)
647 struct nameidata nd;
648 struct dentry *dentry;
649 struct inode *dir;
650 int error;
652 if ((error = rpc_lookup_path(path, &nd, LOOKUP_PARENT)) != 0)
653 return error;
654 dir = nd.dentry->d_inode;
655 down(&dir->i_sem);
656 dentry = lookup_hash(&nd.last, nd.dentry);
657 if (IS_ERR(dentry)) {
658 error = PTR_ERR(dentry);
659 goto out_release;
661 rpc_depopulate(dentry);
662 error = __rpc_rmdir(dir, dentry);
663 dput(dentry);
664 out_release:
665 up(&dir->i_sem);
666 rpc_release_path(&nd);
667 return error;
670 struct dentry *
671 rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops)
673 struct nameidata nd;
674 struct dentry *dentry;
675 struct inode *dir, *inode;
676 struct rpc_inode *rpci;
678 dentry = rpc_lookup_negative(path, &nd);
679 if (IS_ERR(dentry))
680 return dentry;
681 dir = nd.dentry->d_inode;
682 inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IXUSR);
683 if (!inode)
684 goto err_dput;
685 inode->i_ino = iunique(dir->i_sb, 100);
686 inode->i_fop = &rpc_pipe_fops;
687 d_instantiate(dentry, inode);
688 rpci = RPC_I(inode);
689 rpci->private = private;
690 rpci->ops = ops;
691 inode_dir_notify(dir, DN_CREATE);
692 out:
693 up(&dir->i_sem);
694 rpc_release_path(&nd);
695 return dentry;
696 err_dput:
697 dput(dentry);
698 dentry = ERR_PTR(-ENOMEM);
699 printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
700 __FILE__, __FUNCTION__, path, -ENOMEM);
701 goto out;
705 rpc_unlink(char *path)
707 struct nameidata nd;
708 struct dentry *dentry;
709 struct inode *dir;
710 int error;
712 if ((error = rpc_lookup_path(path, &nd, LOOKUP_PARENT)) != 0)
713 return error;
714 dir = nd.dentry->d_inode;
715 down(&dir->i_sem);
716 dentry = lookup_hash(&nd.last, nd.dentry);
717 if (IS_ERR(dentry)) {
718 error = PTR_ERR(dentry);
719 goto out_release;
721 d_drop(dentry);
722 if (dentry->d_inode) {
723 rpc_inode_setowner(dentry->d_inode, NULL);
724 error = simple_unlink(dir, dentry);
726 dput(dentry);
727 inode_dir_notify(dir, DN_DELETE);
728 out_release:
729 up(&dir->i_sem);
730 rpc_release_path(&nd);
731 return error;
735 * populate the filesystem
737 static struct super_operations s_ops = {
738 .alloc_inode = rpc_alloc_inode,
739 .destroy_inode = rpc_destroy_inode,
740 .statfs = simple_statfs,
743 #define RPCAUTH_GSSMAGIC 0x67596969
745 static int
746 rpc_fill_super(struct super_block *sb, void *data, int silent)
748 struct inode *inode;
749 struct dentry *root;
751 sb->s_blocksize = PAGE_CACHE_SIZE;
752 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
753 sb->s_magic = RPCAUTH_GSSMAGIC;
754 sb->s_op = &s_ops;
756 inode = rpc_get_inode(sb, S_IFDIR | 0755);
757 if (!inode)
758 return -ENOMEM;
759 root = d_alloc_root(inode);
760 if (!root) {
761 iput(inode);
762 return -ENOMEM;
764 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
765 goto out;
766 sb->s_root = root;
767 return 0;
768 out:
769 d_genocide(root);
770 dput(root);
771 return -ENOMEM;
774 static struct super_block *
775 rpc_get_sb(struct file_system_type *fs_type,
776 int flags, char *dev_name, void *data)
778 return get_sb_single(fs_type, flags, data, rpc_fill_super);
781 static struct file_system_type rpc_pipe_fs_type = {
782 .owner = THIS_MODULE,
783 .name = "rpc_pipefs",
784 .get_sb = rpc_get_sb,
785 .kill_sb = kill_litter_super,
788 static void
789 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
791 struct rpc_inode *rpci = (struct rpc_inode *) foo;
793 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
794 SLAB_CTOR_CONSTRUCTOR) {
795 inode_init_once(&rpci->vfs_inode);
796 rpci->private = NULL;
797 rpci->nreaders = 0;
798 INIT_LIST_HEAD(&rpci->pipe);
799 rpci->pipelen = 0;
800 init_waitqueue_head(&rpci->waitq);
801 rpci->ops = NULL;
805 int register_rpc_pipefs(void)
807 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
808 sizeof(struct rpc_inode),
809 0, SLAB_HWCACHE_ALIGN,
810 init_once, NULL);
811 if (!rpc_inode_cachep)
812 return -ENOMEM;
813 register_filesystem(&rpc_pipe_fs_type);
814 return 0;
817 void unregister_rpc_pipefs(void)
819 if (kmem_cache_destroy(rpc_inode_cachep))
820 printk(KERN_WARNING "RPC: unable to free inode cache\n");
821 unregister_filesystem(&rpc_pipe_fs_type);