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
->nreaders
== 0 && !list_empty(&rpci
->pipe
))
74 __rpc_purge_upcall(inode
, -ETIMEDOUT
);
79 rpc_queue_upcall(struct inode
*inode
, struct rpc_pipe_msg
*msg
)
81 struct rpc_inode
*rpci
= RPC_I(inode
);
85 if (rpci
->ops
== NULL
)
88 list_add_tail(&msg
->list
, &rpci
->pipe
);
89 rpci
->pipelen
+= msg
->len
;
91 } else if (rpci
->flags
& RPC_PIPE_WAIT_FOR_OPEN
) {
92 if (list_empty(&rpci
->pipe
))
93 schedule_delayed_work(&rpci
->queue_timeout
,
95 list_add_tail(&msg
->list
, &rpci
->pipe
);
96 rpci
->pipelen
+= msg
->len
;
101 wake_up(&rpci
->waitq
);
106 rpc_inode_setowner(struct inode
*inode
, void *private)
108 RPC_I(inode
)->private = private;
112 rpc_close_pipes(struct inode
*inode
)
114 struct rpc_inode
*rpci
= RPC_I(inode
);
116 cancel_delayed_work(&rpci
->queue_timeout
);
117 flush_scheduled_work();
119 if (rpci
->ops
!= NULL
) {
121 __rpc_purge_list(rpci
, &rpci
->in_upcall
, -EPIPE
);
122 __rpc_purge_upcall(inode
, -EPIPE
);
124 if (rpci
->ops
->release_pipe
)
125 rpci
->ops
->release_pipe(inode
);
128 rpc_inode_setowner(inode
, NULL
);
132 static struct inode
*
133 rpc_alloc_inode(struct super_block
*sb
)
135 struct rpc_inode
*rpci
;
136 rpci
= (struct rpc_inode
*)kmem_cache_alloc(rpc_inode_cachep
, SLAB_KERNEL
);
139 return &rpci
->vfs_inode
;
143 rpc_destroy_inode(struct inode
*inode
)
145 kmem_cache_free(rpc_inode_cachep
, RPC_I(inode
));
149 rpc_pipe_open(struct inode
*inode
, struct file
*filp
)
151 struct rpc_inode
*rpci
= RPC_I(inode
);
155 if (rpci
->ops
!= NULL
) {
156 if (filp
->f_mode
& FMODE_READ
)
158 if (filp
->f_mode
& FMODE_WRITE
)
167 rpc_pipe_release(struct inode
*inode
, struct file
*filp
)
169 struct rpc_inode
*rpci
= RPC_I(filp
->f_dentry
->d_inode
);
170 struct rpc_pipe_msg
*msg
;
173 if (rpci
->ops
== NULL
)
175 msg
= (struct rpc_pipe_msg
*)filp
->private_data
;
177 msg
->errno
= -EAGAIN
;
178 list_del_init(&msg
->list
);
179 rpci
->ops
->destroy_msg(msg
);
181 if (filp
->f_mode
& FMODE_WRITE
)
183 if (filp
->f_mode
& FMODE_READ
)
186 __rpc_purge_upcall(inode
, -EAGAIN
);
187 if (rpci
->ops
->release_pipe
)
188 rpci
->ops
->release_pipe(inode
);
195 rpc_pipe_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*offset
)
197 struct inode
*inode
= filp
->f_dentry
->d_inode
;
198 struct rpc_inode
*rpci
= RPC_I(inode
);
199 struct rpc_pipe_msg
*msg
;
203 if (rpci
->ops
== NULL
) {
207 msg
= filp
->private_data
;
209 if (!list_empty(&rpci
->pipe
)) {
210 msg
= list_entry(rpci
->pipe
.next
,
213 list_move(&msg
->list
, &rpci
->in_upcall
);
214 rpci
->pipelen
-= msg
->len
;
215 filp
->private_data
= msg
;
221 /* NOTE: it is up to the callback to update msg->copied */
222 res
= rpci
->ops
->upcall(filp
, msg
, buf
, len
);
223 if (res
< 0 || msg
->len
== msg
->copied
) {
224 filp
->private_data
= NULL
;
225 list_del_init(&msg
->list
);
226 rpci
->ops
->destroy_msg(msg
);
234 rpc_pipe_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*offset
)
236 struct inode
*inode
= filp
->f_dentry
->d_inode
;
237 struct rpc_inode
*rpci
= RPC_I(inode
);
242 if (rpci
->ops
!= NULL
)
243 res
= rpci
->ops
->downcall(filp
, buf
, len
);
249 rpc_pipe_poll(struct file
*filp
, struct poll_table_struct
*wait
)
251 struct rpc_inode
*rpci
;
252 unsigned int mask
= 0;
254 rpci
= RPC_I(filp
->f_dentry
->d_inode
);
255 poll_wait(filp
, &rpci
->waitq
, wait
);
257 mask
= POLLOUT
| POLLWRNORM
;
258 if (rpci
->ops
== NULL
)
259 mask
|= POLLERR
| POLLHUP
;
260 if (!list_empty(&rpci
->pipe
))
261 mask
|= POLLIN
| POLLRDNORM
;
266 rpc_pipe_ioctl(struct inode
*ino
, struct file
*filp
,
267 unsigned int cmd
, unsigned long arg
)
269 struct rpc_inode
*rpci
= RPC_I(filp
->f_dentry
->d_inode
);
274 if (rpci
->ops
== NULL
)
277 if (filp
->private_data
) {
278 struct rpc_pipe_msg
*msg
;
279 msg
= (struct rpc_pipe_msg
*)filp
->private_data
;
280 len
+= msg
->len
- msg
->copied
;
282 return put_user(len
, (int __user
*)arg
);
288 static struct file_operations rpc_pipe_fops
= {
289 .owner
= THIS_MODULE
,
291 .read
= rpc_pipe_read
,
292 .write
= rpc_pipe_write
,
293 .poll
= rpc_pipe_poll
,
294 .ioctl
= rpc_pipe_ioctl
,
295 .open
= rpc_pipe_open
,
296 .release
= rpc_pipe_release
,
300 rpc_show_info(struct seq_file
*m
, void *v
)
302 struct rpc_clnt
*clnt
= m
->private;
304 seq_printf(m
, "RPC server: %s\n", clnt
->cl_server
);
305 seq_printf(m
, "service: %s (%d) version %d\n", clnt
->cl_protname
,
306 clnt
->cl_prog
, clnt
->cl_vers
);
307 seq_printf(m
, "address: %u.%u.%u.%u\n",
308 NIPQUAD(clnt
->cl_xprt
->addr
.sin_addr
.s_addr
));
309 seq_printf(m
, "protocol: %s\n",
310 clnt
->cl_xprt
->prot
== IPPROTO_UDP
? "udp" : "tcp");
315 rpc_info_open(struct inode
*inode
, struct file
*file
)
317 struct rpc_clnt
*clnt
;
318 int ret
= single_open(file
, rpc_show_info
, NULL
);
321 struct seq_file
*m
= file
->private_data
;
323 clnt
= RPC_I(inode
)->private;
325 atomic_inc(&clnt
->cl_users
);
328 single_release(inode
, file
);
337 rpc_info_release(struct inode
*inode
, struct file
*file
)
339 struct seq_file
*m
= file
->private_data
;
340 struct rpc_clnt
*clnt
= (struct rpc_clnt
*)m
->private;
343 rpc_release_client(clnt
);
344 return single_release(inode
, file
);
347 static struct file_operations rpc_info_operations
= {
348 .owner
= THIS_MODULE
,
349 .open
= rpc_info_open
,
352 .release
= rpc_info_release
,
357 * We have a single directory with 1 node in it.
370 * Description of fs contents.
372 struct rpc_filelist
{
374 struct file_operations
*i_fop
;
378 static struct rpc_filelist files
[] = {
381 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
385 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
389 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
391 [RPCAUTH_portmap
] = {
393 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
397 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
406 static struct rpc_filelist authfiles
[] = {
409 .i_fop
= &rpc_info_operations
,
410 .mode
= S_IFREG
| S_IRUSR
,
417 return simple_pin_fs("rpc_pipefs", &rpc_mount
, &rpc_mount_count
);
423 simple_release_fs(&rpc_mount
, &rpc_mount_count
);
427 rpc_lookup_parent(char *path
, struct nameidata
*nd
)
431 if (rpc_get_mount()) {
432 printk(KERN_WARNING
"%s: %s failed to mount "
433 "pseudofilesystem \n", __FILE__
, __FUNCTION__
);
436 nd
->mnt
= mntget(rpc_mount
);
437 nd
->dentry
= dget(rpc_mount
->mnt_root
);
438 nd
->last_type
= LAST_ROOT
;
439 nd
->flags
= LOOKUP_PARENT
;
442 if (path_walk(path
, nd
)) {
443 printk(KERN_WARNING
"%s: %s failed to find path %s\n",
444 __FILE__
, __FUNCTION__
, path
);
452 rpc_release_path(struct nameidata
*nd
)
458 static struct inode
*
459 rpc_get_inode(struct super_block
*sb
, int mode
)
461 struct inode
*inode
= new_inode(sb
);
464 inode
->i_mode
= mode
;
465 inode
->i_uid
= inode
->i_gid
= 0;
466 inode
->i_blksize
= PAGE_CACHE_SIZE
;
468 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
469 switch(mode
& S_IFMT
) {
471 inode
->i_fop
= &simple_dir_operations
;
472 inode
->i_op
= &simple_dir_inode_operations
;
481 * FIXME: This probably has races.
484 rpc_depopulate(struct dentry
*parent
)
486 struct inode
*dir
= parent
->d_inode
;
487 struct list_head
*pos
, *next
;
488 struct dentry
*dentry
, *dvec
[10];
493 spin_lock(&dcache_lock
);
494 list_for_each_safe(pos
, next
, &parent
->d_subdirs
) {
495 dentry
= list_entry(pos
, struct dentry
, d_child
);
496 spin_lock(&dentry
->d_lock
);
497 if (!d_unhashed(dentry
)) {
500 spin_unlock(&dentry
->d_lock
);
502 if (n
== ARRAY_SIZE(dvec
))
505 spin_unlock(&dentry
->d_lock
);
507 spin_unlock(&dcache_lock
);
511 if (dentry
->d_inode
) {
512 rpc_close_pipes(dentry
->d_inode
);
513 simple_unlink(dir
, dentry
);
523 rpc_populate(struct dentry
*parent
,
524 struct rpc_filelist
*files
,
527 struct inode
*inode
, *dir
= parent
->d_inode
;
528 void *private = RPC_I(dir
)->private;
529 struct dentry
*dentry
;
533 for (i
= start
; i
< eof
; i
++) {
534 dentry
= d_alloc_name(parent
, files
[i
].name
);
537 mode
= files
[i
].mode
;
538 inode
= rpc_get_inode(dir
->i_sb
, mode
);
545 inode
->i_fop
= files
[i
].i_fop
;
547 rpc_inode_setowner(inode
, private);
550 d_add(dentry
, inode
);
556 printk(KERN_WARNING
"%s: %s failed to populate directory %s\n",
557 __FILE__
, __FUNCTION__
, parent
->d_name
.name
);
562 __rpc_mkdir(struct inode
*dir
, struct dentry
*dentry
)
566 inode
= rpc_get_inode(dir
->i_sb
, S_IFDIR
| S_IRUSR
| S_IXUSR
);
569 inode
->i_ino
= iunique(dir
->i_sb
, 100);
570 d_instantiate(dentry
, inode
);
572 inode_dir_notify(dir
, DN_CREATE
);
576 printk(KERN_WARNING
"%s: %s failed to allocate inode for dentry %s\n",
577 __FILE__
, __FUNCTION__
, dentry
->d_name
.name
);
582 __rpc_rmdir(struct inode
*dir
, struct dentry
*dentry
)
586 shrink_dcache_parent(dentry
);
588 rpc_close_pipes(dentry
->d_inode
);
589 if ((error
= simple_rmdir(dir
, dentry
)) != 0)
592 inode_dir_notify(dir
, DN_DELETE
);
599 static struct dentry
*
600 rpc_lookup_negative(char *path
, struct nameidata
*nd
)
602 struct dentry
*dentry
;
606 if ((error
= rpc_lookup_parent(path
, nd
)) != 0)
607 return ERR_PTR(error
);
608 dir
= nd
->dentry
->d_inode
;
610 dentry
= lookup_hash(nd
);
613 if (dentry
->d_inode
) {
615 dentry
= ERR_PTR(-EEXIST
);
621 rpc_release_path(nd
);
627 rpc_mkdir(char *path
, struct rpc_clnt
*rpc_client
)
630 struct dentry
*dentry
;
634 dentry
= rpc_lookup_negative(path
, &nd
);
637 dir
= nd
.dentry
->d_inode
;
638 if ((error
= __rpc_mkdir(dir
, dentry
)) != 0)
640 RPC_I(dentry
->d_inode
)->private = rpc_client
;
641 error
= rpc_populate(dentry
, authfiles
,
642 RPCAUTH_info
, RPCAUTH_EOF
);
647 rpc_release_path(&nd
);
650 rpc_depopulate(dentry
);
651 __rpc_rmdir(dir
, dentry
);
654 printk(KERN_WARNING
"%s: %s() failed to create directory %s (errno = %d)\n",
655 __FILE__
, __FUNCTION__
, path
, error
);
656 dentry
= ERR_PTR(error
);
661 rpc_rmdir(char *path
)
664 struct dentry
*dentry
;
668 if ((error
= rpc_lookup_parent(path
, &nd
)) != 0)
670 dir
= nd
.dentry
->d_inode
;
672 dentry
= lookup_hash(&nd
);
673 if (IS_ERR(dentry
)) {
674 error
= PTR_ERR(dentry
);
677 rpc_depopulate(dentry
);
678 error
= __rpc_rmdir(dir
, dentry
);
682 rpc_release_path(&nd
);
687 rpc_mkpipe(char *path
, void *private, struct rpc_pipe_ops
*ops
, int flags
)
690 struct dentry
*dentry
;
691 struct inode
*dir
, *inode
;
692 struct rpc_inode
*rpci
;
694 dentry
= rpc_lookup_negative(path
, &nd
);
697 dir
= nd
.dentry
->d_inode
;
698 inode
= rpc_get_inode(dir
->i_sb
, S_IFSOCK
| S_IRUSR
| S_IWUSR
);
701 inode
->i_ino
= iunique(dir
->i_sb
, 100);
702 inode
->i_fop
= &rpc_pipe_fops
;
703 d_instantiate(dentry
, inode
);
705 rpci
->private = private;
708 inode_dir_notify(dir
, DN_CREATE
);
711 rpc_release_path(&nd
);
715 dentry
= ERR_PTR(-ENOMEM
);
716 printk(KERN_WARNING
"%s: %s() failed to create pipe %s (errno = %d)\n",
717 __FILE__
, __FUNCTION__
, path
, -ENOMEM
);
722 rpc_unlink(char *path
)
725 struct dentry
*dentry
;
729 if ((error
= rpc_lookup_parent(path
, &nd
)) != 0)
731 dir
= nd
.dentry
->d_inode
;
733 dentry
= lookup_hash(&nd
);
734 if (IS_ERR(dentry
)) {
735 error
= PTR_ERR(dentry
);
739 if (dentry
->d_inode
) {
740 rpc_close_pipes(dentry
->d_inode
);
741 error
= simple_unlink(dir
, dentry
);
744 inode_dir_notify(dir
, DN_DELETE
);
747 rpc_release_path(&nd
);
752 * populate the filesystem
754 static struct super_operations s_ops
= {
755 .alloc_inode
= rpc_alloc_inode
,
756 .destroy_inode
= rpc_destroy_inode
,
757 .statfs
= simple_statfs
,
760 #define RPCAUTH_GSSMAGIC 0x67596969
763 rpc_fill_super(struct super_block
*sb
, void *data
, int silent
)
768 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
769 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
770 sb
->s_magic
= RPCAUTH_GSSMAGIC
;
774 inode
= rpc_get_inode(sb
, S_IFDIR
| 0755);
777 root
= d_alloc_root(inode
);
782 if (rpc_populate(root
, files
, RPCAUTH_Root
+ 1, RPCAUTH_RootEOF
))
792 static struct super_block
*
793 rpc_get_sb(struct file_system_type
*fs_type
,
794 int flags
, const char *dev_name
, void *data
)
796 return get_sb_single(fs_type
, flags
, data
, rpc_fill_super
);
799 static struct file_system_type rpc_pipe_fs_type
= {
800 .owner
= THIS_MODULE
,
801 .name
= "rpc_pipefs",
802 .get_sb
= rpc_get_sb
,
803 .kill_sb
= kill_litter_super
,
807 init_once(void * foo
, kmem_cache_t
* cachep
, unsigned long flags
)
809 struct rpc_inode
*rpci
= (struct rpc_inode
*) foo
;
811 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
812 SLAB_CTOR_CONSTRUCTOR
) {
813 inode_init_once(&rpci
->vfs_inode
);
814 rpci
->private = NULL
;
817 INIT_LIST_HEAD(&rpci
->in_upcall
);
818 INIT_LIST_HEAD(&rpci
->pipe
);
820 init_waitqueue_head(&rpci
->waitq
);
821 INIT_WORK(&rpci
->queue_timeout
, rpc_timeout_upcall_queue
, rpci
);
826 int register_rpc_pipefs(void)
828 rpc_inode_cachep
= kmem_cache_create("rpc_inode_cache",
829 sizeof(struct rpc_inode
),
830 0, SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
832 if (!rpc_inode_cachep
)
834 register_filesystem(&rpc_pipe_fs_type
);
838 void unregister_rpc_pipefs(void)
840 if (kmem_cache_destroy(rpc_inode_cachep
))
841 printk(KERN_WARNING
"RPC: unable to free inode cache\n");
842 unregister_filesystem(&rpc_pipe_fs_type
);