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>
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
;
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
);
50 rpci
->ops
->destroy_msg(msg
);
53 wake_up(&rpci
->waitq
);
57 rpc_purge_upcall(struct inode
*inode
, int err
)
60 __rpc_purge_upcall(inode
, err
);
65 * XXX should only be called in ->downcall
68 __rpc_purge_current_upcall(struct file
*filp
)
70 struct rpc_pipe_msg
*msg
;
72 msg
= filp
->private_data
;
73 filp
->private_data
= NULL
;
80 rpc_queue_upcall(struct inode
*inode
, struct rpc_pipe_msg
*msg
)
82 struct rpc_inode
*rpci
= RPC_I(inode
);
87 list_add_tail(&msg
->list
, &rpci
->pipe
);
88 rpci
->pipelen
+= msg
->len
;
92 wake_up(&rpci
->waitq
);
97 rpc_inode_setowner(struct inode
*inode
, void *private)
99 struct rpc_inode
*rpci
= RPC_I(inode
);
101 rpci
->private = private;
103 __rpc_purge_upcall(inode
, -EPIPE
);
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
);
114 return &rpci
->vfs_inode
;
118 rpc_destroy_inode(struct inode
*inode
)
120 kmem_cache_free(rpc_inode_cachep
, RPC_I(inode
));
124 rpc_pipe_open(struct inode
*inode
, struct file
*filp
)
126 struct rpc_inode
*rpci
= RPC_I(inode
);
130 if (rpci
->private != NULL
) {
131 if (filp
->f_mode
& FMODE_READ
)
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
;
148 rpci
->ops
->destroy_msg(msg
);
151 if (filp
->f_mode
& FMODE_READ
)
154 __rpc_purge_upcall(inode
, -EPIPE
);
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
;
168 if (!rpci
->private) {
172 msg
= filp
->private_data
;
174 if (!list_empty(&rpci
->pipe
)) {
175 msg
= list_entry(rpci
->pipe
.next
,
178 list_del_init(&msg
->list
);
179 rpci
->pipelen
-= msg
->len
;
180 filp
->private_data
= msg
;
185 res
= rpci
->ops
->upcall(filp
, msg
, buf
, len
);
186 if (res
< 0 || msg
->len
== msg
->copied
) {
187 filp
->private_data
= NULL
;
189 rpci
->ops
->destroy_msg(msg
);
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
);
205 if (rpci
->private != NULL
)
206 res
= rpci
->ops
->downcall(filp
, buf
, len
);
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
;
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
);
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
);
251 struct inode_operations rpc_pipe_iops
= {
252 .lookup
= simple_lookup
,
256 struct file_operations rpc_pipe_fops
= {
257 .owner
= THIS_MODULE
,
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
,
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
));
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
);
287 struct seq_file
*m
= file
->private_data
;
289 clnt
= RPC_I(inode
)->private;
291 atomic_inc(&clnt
->cl_users
);
294 single_release(inode
, file
);
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;
309 rpc_release_client(clnt
);
310 return single_release(inode
, file
);
313 static struct file_operations rpc_info_operations
= {
314 .open
= rpc_info_open
,
317 .release
= rpc_info_release
,
322 * We have a single directory with 1 node in it.
335 * Description of fs contents.
337 struct rpc_filelist
{
339 struct file_operations
*i_fop
;
343 static struct rpc_filelist files
[] = {
346 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
350 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
354 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
356 [RPCAUTH_portmap
] = {
358 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
362 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
371 static struct rpc_filelist authfiles
[] = {
374 .i_fop
= &rpc_info_operations
,
375 .mode
= S_IFREG
| S_IRUSR
,
382 struct vfsmount
* mnt
= NULL
;
384 spin_lock(&rpc_mount_lock
);
387 spin_unlock(&rpc_mount_lock
);
388 mnt
= kern_mount(&rpc_pipe_fs_type
);
391 spin_lock(&rpc_mount_lock
);
401 spin_unlock(&rpc_mount_lock
);
410 struct vfsmount
*mnt
;
412 spin_lock(&rpc_mount_lock
);
415 if (rpc_mount_count
== 0)
419 spin_unlock(&rpc_mount_lock
);
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__
);
432 nd
->mnt
= mntget(rpc_mount
);
433 nd
->dentry
= dget(rpc_mount
->mnt_root
);
434 nd
->last_type
= LAST_ROOT
;
437 if (path_walk(path
, nd
)) {
438 printk(KERN_WARNING
"%s: %s failed to find path %s\n",
439 __FILE__
, __FUNCTION__
, path
);
447 rpc_release_path(struct nameidata
*nd
)
453 static struct inode
*
454 rpc_get_inode(struct super_block
*sb
, int mode
)
456 struct inode
*inode
= new_inode(sb
);
459 inode
->i_mode
= mode
;
460 inode
->i_uid
= inode
->i_gid
= 0;
461 inode
->i_blksize
= PAGE_CACHE_SIZE
;
463 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
464 switch(mode
& S_IFMT
) {
466 inode
->i_fop
= &simple_dir_operations
;
467 inode
->i_op
= &simple_dir_inode_operations
;
476 * FIXME: This probably has races.
479 rpc_depopulate(struct dentry
*dir
)
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
)) {
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
);
505 up(&dir
->d_inode
->i_sem
);
509 rpc_populate(struct dentry
*dir
,
510 struct rpc_filelist
*files
,
513 void *private = RPC_I(dir
->d_inode
)->private;
515 struct dentry
*dentry
;
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
);
525 mode
= files
[i
].mode
;
526 inode
= rpc_get_inode(dir
->d_inode
->i_sb
, mode
);
533 inode
->i_fop
= files
[i
].i_fop
;
535 rpc_inode_setowner(inode
, private);
537 dir
->d_inode
->i_nlink
++;
538 d_add(dentry
, inode
);
542 printk(KERN_WARNING
"%s: %s failed to populate directory %s\n",
543 __FILE__
, __FUNCTION__
, dir
->d_name
.name
);
548 __rpc_mkdir(struct inode
*dir
, struct dentry
*dentry
)
552 inode
= rpc_get_inode(dir
->i_sb
, S_IFDIR
| S_IRUSR
| S_IXUSR
);
555 inode
->i_ino
= iunique(dir
->i_sb
, 100);
556 d_instantiate(dentry
, inode
);
558 inode_dir_notify(dir
, DN_CREATE
);
562 printk(KERN_WARNING
"%s: %s failed to allocate inode for dentry %s\n",
563 __FILE__
, __FUNCTION__
, dentry
->d_name
.name
);
568 __rpc_rmdir(struct inode
*dir
, struct dentry
*dentry
)
572 rpc_inode_setowner(dentry
->d_inode
, NULL
);
573 if ((error
= simple_rmdir(dir
, dentry
)) != 0)
576 inode_dir_notify(dir
, DN_DELETE
);
584 rpc_lookup_negative(char *path
, struct nameidata
*nd
)
586 struct dentry
*dentry
;
590 if ((error
= rpc_lookup_path(path
, nd
, LOOKUP_PARENT
)) != 0)
591 return ERR_PTR(error
);
592 dir
= nd
->dentry
->d_inode
;
594 dentry
= lookup_hash(&nd
->last
, nd
->dentry
);
597 if (dentry
->d_inode
) {
599 dentry
= ERR_PTR(-EEXIST
);
605 rpc_release_path(nd
);
611 rpc_mkdir(char *path
, struct rpc_clnt
*rpc_client
)
614 struct dentry
*dentry
;
618 dentry
= rpc_lookup_negative(path
, &nd
);
621 dir
= nd
.dentry
->d_inode
;
622 if ((error
= __rpc_mkdir(dir
, dentry
)) != 0)
624 RPC_I(dentry
->d_inode
)->private = rpc_client
;
625 error
= rpc_populate(dentry
, authfiles
,
626 RPCAUTH_info
, RPCAUTH_EOF
);
631 rpc_release_path(&nd
);
634 rpc_depopulate(dentry
);
635 __rpc_rmdir(dir
, 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
);
645 rpc_rmdir(char *path
)
648 struct dentry
*dentry
;
652 if ((error
= rpc_lookup_path(path
, &nd
, LOOKUP_PARENT
)) != 0)
654 dir
= nd
.dentry
->d_inode
;
656 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
657 if (IS_ERR(dentry
)) {
658 error
= PTR_ERR(dentry
);
661 rpc_depopulate(dentry
);
662 error
= __rpc_rmdir(dir
, dentry
);
666 rpc_release_path(&nd
);
671 rpc_mkpipe(char *path
, void *private, struct rpc_pipe_ops
*ops
)
674 struct dentry
*dentry
;
675 struct inode
*dir
, *inode
;
676 struct rpc_inode
*rpci
;
678 dentry
= rpc_lookup_negative(path
, &nd
);
681 dir
= nd
.dentry
->d_inode
;
682 inode
= rpc_get_inode(dir
->i_sb
, S_IFSOCK
| S_IRUSR
| S_IXUSR
);
685 inode
->i_ino
= iunique(dir
->i_sb
, 100);
686 inode
->i_fop
= &rpc_pipe_fops
;
687 d_instantiate(dentry
, inode
);
689 rpci
->private = private;
691 inode_dir_notify(dir
, DN_CREATE
);
694 rpc_release_path(&nd
);
698 dentry
= ERR_PTR(-ENOMEM
);
699 printk(KERN_WARNING
"%s: %s() failed to create pipe %s (errno = %d)\n",
700 __FILE__
, __FUNCTION__
, path
, -ENOMEM
);
705 rpc_unlink(char *path
)
708 struct dentry
*dentry
;
712 if ((error
= rpc_lookup_path(path
, &nd
, LOOKUP_PARENT
)) != 0)
714 dir
= nd
.dentry
->d_inode
;
716 dentry
= lookup_hash(&nd
.last
, nd
.dentry
);
717 if (IS_ERR(dentry
)) {
718 error
= PTR_ERR(dentry
);
722 if (dentry
->d_inode
) {
723 rpc_inode_setowner(dentry
->d_inode
, NULL
);
724 error
= simple_unlink(dir
, dentry
);
727 inode_dir_notify(dir
, DN_DELETE
);
730 rpc_release_path(&nd
);
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
746 rpc_fill_super(struct super_block
*sb
, void *data
, int silent
)
751 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
752 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
753 sb
->s_magic
= RPCAUTH_GSSMAGIC
;
756 inode
= rpc_get_inode(sb
, S_IFDIR
| 0755);
759 root
= d_alloc_root(inode
);
764 if (rpc_populate(root
, files
, RPCAUTH_Root
+ 1, RPCAUTH_RootEOF
))
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
,
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
;
798 INIT_LIST_HEAD(&rpci
->pipe
);
800 init_waitqueue_head(&rpci
->waitq
);
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
,
811 if (!rpc_inode_cachep
)
813 register_filesystem(&rpc_pipe_fs_type
);
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
);