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/module.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/pagemap.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/fsnotify.h>
18 #include <linux/kernel.h>
20 #include <asm/ioctls.h>
21 #include <linux/poll.h>
22 #include <linux/wait.h>
23 #include <linux/seq_file.h>
25 #include <linux/sunrpc/clnt.h>
26 #include <linux/workqueue.h>
27 #include <linux/sunrpc/rpc_pipe_fs.h>
28 #include <linux/sunrpc/cache.h>
29 #include <linux/nsproxy.h>
30 #include <linux/notifier.h>
35 #define RPCDBG_FACILITY RPCDBG_DEBUG
37 #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
39 static struct file_system_type rpc_pipe_fs_type
;
42 static struct kmem_cache
*rpc_inode_cachep __read_mostly
;
44 #define RPC_UPCALL_TIMEOUT (30*HZ)
46 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list
);
48 int rpc_pipefs_notifier_register(struct notifier_block
*nb
)
50 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list
, nb
);
52 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register
);
54 void rpc_pipefs_notifier_unregister(struct notifier_block
*nb
)
56 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list
, nb
);
58 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister
);
60 static void rpc_purge_list(struct rpc_pipe
*pipe
, struct list_head
*head
,
61 void (*destroy_msg
)(struct rpc_pipe_msg
*), int err
)
63 struct rpc_pipe_msg
*msg
;
68 msg
= list_entry(head
->next
, struct rpc_pipe_msg
, list
);
69 list_del_init(&msg
->list
);
72 } while (!list_empty(head
));
73 wake_up(&pipe
->waitq
);
77 rpc_timeout_upcall_queue(struct work_struct
*work
)
80 struct rpc_pipe
*pipe
=
81 container_of(work
, struct rpc_pipe
, queue_timeout
.work
);
82 void (*destroy_msg
)(struct rpc_pipe_msg
*);
84 spin_lock(&pipe
->lock
);
85 destroy_msg
= pipe
->ops
->destroy_msg
;
86 if (pipe
->nreaders
== 0) {
87 list_splice_init(&pipe
->pipe
, &free_list
);
90 spin_unlock(&pipe
->lock
);
91 rpc_purge_list(pipe
, &free_list
, destroy_msg
, -ETIMEDOUT
);
94 ssize_t
rpc_pipe_generic_upcall(struct file
*filp
, struct rpc_pipe_msg
*msg
,
95 char __user
*dst
, size_t buflen
)
97 char *data
= (char *)msg
->data
+ msg
->copied
;
98 size_t mlen
= min(msg
->len
- msg
->copied
, buflen
);
101 left
= copy_to_user(dst
, data
, mlen
);
103 msg
->errno
= -EFAULT
;
112 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall
);
115 * rpc_queue_upcall - queue an upcall message to userspace
116 * @inode: inode of upcall pipe on which to queue given message
117 * @msg: message to queue
119 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
120 * A userspace process may then later read the upcall by performing a
121 * read on an open file for this inode. It is up to the caller to
122 * initialize the fields of @msg (other than @msg->list) appropriately.
125 rpc_queue_upcall(struct rpc_pipe
*pipe
, struct rpc_pipe_msg
*msg
)
129 spin_lock(&pipe
->lock
);
130 if (pipe
->nreaders
) {
131 list_add_tail(&msg
->list
, &pipe
->pipe
);
132 pipe
->pipelen
+= msg
->len
;
134 } else if (pipe
->flags
& RPC_PIPE_WAIT_FOR_OPEN
) {
135 if (list_empty(&pipe
->pipe
))
136 queue_delayed_work(rpciod_workqueue
,
137 &pipe
->queue_timeout
,
139 list_add_tail(&msg
->list
, &pipe
->pipe
);
140 pipe
->pipelen
+= msg
->len
;
143 spin_unlock(&pipe
->lock
);
144 wake_up(&pipe
->waitq
);
147 EXPORT_SYMBOL_GPL(rpc_queue_upcall
);
150 rpc_inode_setowner(struct inode
*inode
, void *private)
152 RPC_I(inode
)->private = private;
156 rpc_close_pipes(struct inode
*inode
)
158 struct rpc_pipe
*pipe
= RPC_I(inode
)->pipe
;
160 LIST_HEAD(free_list
);
162 mutex_lock(&inode
->i_mutex
);
163 spin_lock(&pipe
->lock
);
164 need_release
= pipe
->nreaders
!= 0 || pipe
->nwriters
!= 0;
166 list_splice_init(&pipe
->in_upcall
, &free_list
);
167 list_splice_init(&pipe
->pipe
, &free_list
);
170 spin_unlock(&pipe
->lock
);
171 rpc_purge_list(pipe
, &free_list
, pipe
->ops
->destroy_msg
, -EPIPE
);
173 if (need_release
&& pipe
->ops
->release_pipe
)
174 pipe
->ops
->release_pipe(inode
);
175 cancel_delayed_work_sync(&pipe
->queue_timeout
);
176 rpc_inode_setowner(inode
, NULL
);
177 RPC_I(inode
)->pipe
= NULL
;
178 mutex_unlock(&inode
->i_mutex
);
181 static struct inode
*
182 rpc_alloc_inode(struct super_block
*sb
)
184 struct rpc_inode
*rpci
;
185 rpci
= (struct rpc_inode
*)kmem_cache_alloc(rpc_inode_cachep
, GFP_KERNEL
);
188 return &rpci
->vfs_inode
;
192 rpc_i_callback(struct rcu_head
*head
)
194 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
195 kmem_cache_free(rpc_inode_cachep
, RPC_I(inode
));
199 rpc_destroy_inode(struct inode
*inode
)
201 call_rcu(&inode
->i_rcu
, rpc_i_callback
);
205 rpc_pipe_open(struct inode
*inode
, struct file
*filp
)
207 struct rpc_pipe
*pipe
;
211 mutex_lock(&inode
->i_mutex
);
212 pipe
= RPC_I(inode
)->pipe
;
215 first_open
= pipe
->nreaders
== 0 && pipe
->nwriters
== 0;
216 if (first_open
&& pipe
->ops
->open_pipe
) {
217 res
= pipe
->ops
->open_pipe(inode
);
221 if (filp
->f_mode
& FMODE_READ
)
223 if (filp
->f_mode
& FMODE_WRITE
)
227 mutex_unlock(&inode
->i_mutex
);
232 rpc_pipe_release(struct inode
*inode
, struct file
*filp
)
234 struct rpc_pipe
*pipe
;
235 struct rpc_pipe_msg
*msg
;
238 mutex_lock(&inode
->i_mutex
);
239 pipe
= RPC_I(inode
)->pipe
;
242 msg
= filp
->private_data
;
244 spin_lock(&pipe
->lock
);
245 msg
->errno
= -EAGAIN
;
246 list_del_init(&msg
->list
);
247 spin_unlock(&pipe
->lock
);
248 pipe
->ops
->destroy_msg(msg
);
250 if (filp
->f_mode
& FMODE_WRITE
)
252 if (filp
->f_mode
& FMODE_READ
) {
254 if (pipe
->nreaders
== 0) {
255 LIST_HEAD(free_list
);
256 spin_lock(&pipe
->lock
);
257 list_splice_init(&pipe
->pipe
, &free_list
);
259 spin_unlock(&pipe
->lock
);
260 rpc_purge_list(pipe
, &free_list
,
261 pipe
->ops
->destroy_msg
, -EAGAIN
);
264 last_close
= pipe
->nwriters
== 0 && pipe
->nreaders
== 0;
265 if (last_close
&& pipe
->ops
->release_pipe
)
266 pipe
->ops
->release_pipe(inode
);
268 mutex_unlock(&inode
->i_mutex
);
273 rpc_pipe_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*offset
)
275 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
276 struct rpc_pipe
*pipe
;
277 struct rpc_pipe_msg
*msg
;
280 mutex_lock(&inode
->i_mutex
);
281 pipe
= RPC_I(inode
)->pipe
;
286 msg
= filp
->private_data
;
288 spin_lock(&pipe
->lock
);
289 if (!list_empty(&pipe
->pipe
)) {
290 msg
= list_entry(pipe
->pipe
.next
,
293 list_move(&msg
->list
, &pipe
->in_upcall
);
294 pipe
->pipelen
-= msg
->len
;
295 filp
->private_data
= msg
;
298 spin_unlock(&pipe
->lock
);
302 /* NOTE: it is up to the callback to update msg->copied */
303 res
= pipe
->ops
->upcall(filp
, msg
, buf
, len
);
304 if (res
< 0 || msg
->len
== msg
->copied
) {
305 filp
->private_data
= NULL
;
306 spin_lock(&pipe
->lock
);
307 list_del_init(&msg
->list
);
308 spin_unlock(&pipe
->lock
);
309 pipe
->ops
->destroy_msg(msg
);
312 mutex_unlock(&inode
->i_mutex
);
317 rpc_pipe_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*offset
)
319 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
322 mutex_lock(&inode
->i_mutex
);
324 if (RPC_I(inode
)->pipe
!= NULL
)
325 res
= RPC_I(inode
)->pipe
->ops
->downcall(filp
, buf
, len
);
326 mutex_unlock(&inode
->i_mutex
);
331 rpc_pipe_poll(struct file
*filp
, struct poll_table_struct
*wait
)
333 struct rpc_pipe
*pipe
= RPC_I(filp
->f_path
.dentry
->d_inode
)->pipe
;
334 unsigned int mask
= 0;
336 poll_wait(filp
, &pipe
->waitq
, wait
);
338 mask
= POLLOUT
| POLLWRNORM
;
339 if (pipe
->dentry
== NULL
)
340 mask
|= POLLERR
| POLLHUP
;
341 if (filp
->private_data
|| !list_empty(&pipe
->pipe
))
342 mask
|= POLLIN
| POLLRDNORM
;
347 rpc_pipe_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
349 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
350 struct rpc_pipe
*pipe
;
355 mutex_lock(&inode
->i_mutex
);
356 pipe
= RPC_I(inode
)->pipe
;
358 mutex_unlock(&inode
->i_mutex
);
361 spin_lock(&pipe
->lock
);
363 if (filp
->private_data
) {
364 struct rpc_pipe_msg
*msg
;
365 msg
= filp
->private_data
;
366 len
+= msg
->len
- msg
->copied
;
368 spin_unlock(&pipe
->lock
);
369 mutex_unlock(&inode
->i_mutex
);
370 return put_user(len
, (int __user
*)arg
);
376 static const struct file_operations rpc_pipe_fops
= {
377 .owner
= THIS_MODULE
,
379 .read
= rpc_pipe_read
,
380 .write
= rpc_pipe_write
,
381 .poll
= rpc_pipe_poll
,
382 .unlocked_ioctl
= rpc_pipe_ioctl
,
383 .open
= rpc_pipe_open
,
384 .release
= rpc_pipe_release
,
388 rpc_show_info(struct seq_file
*m
, void *v
)
390 struct rpc_clnt
*clnt
= m
->private;
392 seq_printf(m
, "RPC server: %s\n", clnt
->cl_server
);
393 seq_printf(m
, "service: %s (%d) version %d\n", clnt
->cl_protname
,
394 clnt
->cl_prog
, clnt
->cl_vers
);
395 seq_printf(m
, "address: %s\n", rpc_peeraddr2str(clnt
, RPC_DISPLAY_ADDR
));
396 seq_printf(m
, "protocol: %s\n", rpc_peeraddr2str(clnt
, RPC_DISPLAY_PROTO
));
397 seq_printf(m
, "port: %s\n", rpc_peeraddr2str(clnt
, RPC_DISPLAY_PORT
));
402 rpc_info_open(struct inode
*inode
, struct file
*file
)
404 struct rpc_clnt
*clnt
= NULL
;
405 int ret
= single_open(file
, rpc_show_info
, NULL
);
408 struct seq_file
*m
= file
->private_data
;
410 spin_lock(&file
->f_path
.dentry
->d_lock
);
411 if (!d_unhashed(file
->f_path
.dentry
))
412 clnt
= RPC_I(inode
)->private;
413 if (clnt
!= NULL
&& atomic_inc_not_zero(&clnt
->cl_count
)) {
414 spin_unlock(&file
->f_path
.dentry
->d_lock
);
417 spin_unlock(&file
->f_path
.dentry
->d_lock
);
418 single_release(inode
, file
);
426 rpc_info_release(struct inode
*inode
, struct file
*file
)
428 struct seq_file
*m
= file
->private_data
;
429 struct rpc_clnt
*clnt
= (struct rpc_clnt
*)m
->private;
432 rpc_release_client(clnt
);
433 return single_release(inode
, file
);
436 static const struct file_operations rpc_info_operations
= {
437 .owner
= THIS_MODULE
,
438 .open
= rpc_info_open
,
441 .release
= rpc_info_release
,
446 * Description of fs contents.
448 struct rpc_filelist
{
450 const struct file_operations
*i_fop
;
454 static int rpc_delete_dentry(const struct dentry
*dentry
)
459 static const struct dentry_operations rpc_dentry_operations
= {
460 .d_delete
= rpc_delete_dentry
,
463 static struct inode
*
464 rpc_get_inode(struct super_block
*sb
, umode_t mode
)
466 struct inode
*inode
= new_inode(sb
);
469 inode
->i_ino
= get_next_ino();
470 inode
->i_mode
= mode
;
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
;
483 static int __rpc_create_common(struct inode
*dir
, struct dentry
*dentry
,
485 const struct file_operations
*i_fop
,
491 inode
= rpc_get_inode(dir
->i_sb
, mode
);
494 inode
->i_ino
= iunique(dir
->i_sb
, 100);
496 inode
->i_fop
= i_fop
;
498 rpc_inode_setowner(inode
, private);
499 d_add(dentry
, inode
);
502 printk(KERN_WARNING
"%s: %s failed to allocate inode for dentry %s\n",
503 __FILE__
, __func__
, dentry
->d_name
.name
);
508 static int __rpc_create(struct inode
*dir
, struct dentry
*dentry
,
510 const struct file_operations
*i_fop
,
515 err
= __rpc_create_common(dir
, dentry
, S_IFREG
| mode
, i_fop
, private);
518 fsnotify_create(dir
, dentry
);
522 static int __rpc_mkdir(struct inode
*dir
, struct dentry
*dentry
,
524 const struct file_operations
*i_fop
,
529 err
= __rpc_create_common(dir
, dentry
, S_IFDIR
| mode
, i_fop
, private);
533 fsnotify_mkdir(dir
, dentry
);
538 init_pipe(struct rpc_pipe
*pipe
)
542 INIT_LIST_HEAD(&pipe
->in_upcall
);
543 INIT_LIST_HEAD(&pipe
->in_downcall
);
544 INIT_LIST_HEAD(&pipe
->pipe
);
546 init_waitqueue_head(&pipe
->waitq
);
547 INIT_DELAYED_WORK(&pipe
->queue_timeout
,
548 rpc_timeout_upcall_queue
);
550 spin_lock_init(&pipe
->lock
);
554 void rpc_destroy_pipe_data(struct rpc_pipe
*pipe
)
558 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data
);
560 struct rpc_pipe
*rpc_mkpipe_data(const struct rpc_pipe_ops
*ops
, int flags
)
562 struct rpc_pipe
*pipe
;
564 pipe
= kzalloc(sizeof(struct rpc_pipe
), GFP_KERNEL
);
566 return ERR_PTR(-ENOMEM
);
572 EXPORT_SYMBOL_GPL(rpc_mkpipe_data
);
574 static int __rpc_mkpipe_dentry(struct inode
*dir
, struct dentry
*dentry
,
576 const struct file_operations
*i_fop
,
578 struct rpc_pipe
*pipe
)
580 struct rpc_inode
*rpci
;
583 err
= __rpc_create_common(dir
, dentry
, S_IFIFO
| mode
, i_fop
, private);
586 rpci
= RPC_I(dentry
->d_inode
);
587 rpci
->private = private;
589 fsnotify_create(dir
, dentry
);
593 static int __rpc_rmdir(struct inode
*dir
, struct dentry
*dentry
)
598 ret
= simple_rmdir(dir
, dentry
);
604 int rpc_rmdir(struct dentry
*dentry
)
606 struct dentry
*parent
;
610 parent
= dget_parent(dentry
);
611 dir
= parent
->d_inode
;
612 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
613 error
= __rpc_rmdir(dir
, dentry
);
614 mutex_unlock(&dir
->i_mutex
);
618 EXPORT_SYMBOL_GPL(rpc_rmdir
);
620 static int __rpc_unlink(struct inode
*dir
, struct dentry
*dentry
)
625 ret
= simple_unlink(dir
, dentry
);
631 static int __rpc_rmpipe(struct inode
*dir
, struct dentry
*dentry
)
633 struct inode
*inode
= dentry
->d_inode
;
635 rpc_close_pipes(inode
);
636 return __rpc_unlink(dir
, dentry
);
639 static struct dentry
*__rpc_lookup_create_exclusive(struct dentry
*parent
,
642 struct dentry
*dentry
;
644 dentry
= d_lookup(parent
, name
);
646 dentry
= d_alloc(parent
, name
);
648 return ERR_PTR(-ENOMEM
);
650 if (dentry
->d_inode
== NULL
) {
651 d_set_d_op(dentry
, &rpc_dentry_operations
);
655 return ERR_PTR(-EEXIST
);
659 * FIXME: This probably has races.
661 static void __rpc_depopulate(struct dentry
*parent
,
662 const struct rpc_filelist
*files
,
665 struct inode
*dir
= parent
->d_inode
;
666 struct dentry
*dentry
;
670 for (i
= start
; i
< eof
; i
++) {
671 name
.name
= files
[i
].name
;
672 name
.len
= strlen(files
[i
].name
);
673 name
.hash
= full_name_hash(name
.name
, name
.len
);
674 dentry
= d_lookup(parent
, &name
);
678 if (dentry
->d_inode
== NULL
)
680 switch (dentry
->d_inode
->i_mode
& S_IFMT
) {
684 __rpc_unlink(dir
, dentry
);
687 __rpc_rmdir(dir
, dentry
);
694 static void rpc_depopulate(struct dentry
*parent
,
695 const struct rpc_filelist
*files
,
698 struct inode
*dir
= parent
->d_inode
;
700 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_CHILD
);
701 __rpc_depopulate(parent
, files
, start
, eof
);
702 mutex_unlock(&dir
->i_mutex
);
705 static int rpc_populate(struct dentry
*parent
,
706 const struct rpc_filelist
*files
,
710 struct inode
*dir
= parent
->d_inode
;
711 struct dentry
*dentry
;
714 mutex_lock(&dir
->i_mutex
);
715 for (i
= start
; i
< eof
; i
++) {
718 q
.name
= files
[i
].name
;
719 q
.len
= strlen(files
[i
].name
);
720 q
.hash
= full_name_hash(q
.name
, q
.len
);
721 dentry
= __rpc_lookup_create_exclusive(parent
, &q
);
722 err
= PTR_ERR(dentry
);
725 switch (files
[i
].mode
& S_IFMT
) {
729 err
= __rpc_create(dir
, dentry
,
735 err
= __rpc_mkdir(dir
, dentry
,
743 mutex_unlock(&dir
->i_mutex
);
746 __rpc_depopulate(parent
, files
, start
, eof
);
747 mutex_unlock(&dir
->i_mutex
);
748 printk(KERN_WARNING
"%s: %s failed to populate directory %s\n",
749 __FILE__
, __func__
, parent
->d_name
.name
);
753 static struct dentry
*rpc_mkdir_populate(struct dentry
*parent
,
754 struct qstr
*name
, umode_t mode
, void *private,
755 int (*populate
)(struct dentry
*, void *), void *args_populate
)
757 struct dentry
*dentry
;
758 struct inode
*dir
= parent
->d_inode
;
761 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
762 dentry
= __rpc_lookup_create_exclusive(parent
, name
);
765 error
= __rpc_mkdir(dir
, dentry
, mode
, NULL
, private);
768 if (populate
!= NULL
) {
769 error
= populate(dentry
, args_populate
);
774 mutex_unlock(&dir
->i_mutex
);
777 __rpc_rmdir(dir
, dentry
);
779 dentry
= ERR_PTR(error
);
783 static int rpc_rmdir_depopulate(struct dentry
*dentry
,
784 void (*depopulate
)(struct dentry
*))
786 struct dentry
*parent
;
790 parent
= dget_parent(dentry
);
791 dir
= parent
->d_inode
;
792 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
793 if (depopulate
!= NULL
)
795 error
= __rpc_rmdir(dir
, dentry
);
796 mutex_unlock(&dir
->i_mutex
);
802 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
803 * @parent: dentry of directory to create new "pipe" in
804 * @name: name of pipe
805 * @private: private data to associate with the pipe, for the caller's use
806 * @ops: operations defining the behavior of the pipe: upcall, downcall,
807 * release_pipe, open_pipe, and destroy_msg.
808 * @flags: rpc_pipe flags
810 * Data is made available for userspace to read by calls to
811 * rpc_queue_upcall(). The actual reads will result in calls to
812 * @ops->upcall, which will be called with the file pointer,
813 * message, and userspace buffer to copy to.
815 * Writes can come at any time, and do not necessarily have to be
816 * responses to upcalls. They will result in calls to @msg->downcall.
818 * The @private argument passed here will be available to all these methods
819 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
821 struct dentry
*rpc_mkpipe_dentry(struct dentry
*parent
, const char *name
,
822 void *private, struct rpc_pipe
*pipe
)
824 struct dentry
*dentry
;
825 struct inode
*dir
= parent
->d_inode
;
826 umode_t umode
= S_IFIFO
| S_IRUSR
| S_IWUSR
;
830 if (pipe
->ops
->upcall
== NULL
)
832 if (pipe
->ops
->downcall
== NULL
)
836 q
.len
= strlen(name
);
837 q
.hash
= full_name_hash(q
.name
, q
.len
),
839 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
840 dentry
= __rpc_lookup_create_exclusive(parent
, &q
);
843 err
= __rpc_mkpipe_dentry(dir
, dentry
, umode
, &rpc_pipe_fops
,
848 mutex_unlock(&dir
->i_mutex
);
851 dentry
= ERR_PTR(err
);
852 printk(KERN_WARNING
"%s: %s() failed to create pipe %s/%s (errno = %d)\n",
853 __FILE__
, __func__
, parent
->d_name
.name
, name
,
857 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry
);
860 * rpc_unlink - remove a pipe
861 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
863 * After this call, lookups will no longer find the pipe, and any
864 * attempts to read or write using preexisting opens of the pipe will
868 rpc_unlink(struct dentry
*dentry
)
870 struct dentry
*parent
;
874 parent
= dget_parent(dentry
);
875 dir
= parent
->d_inode
;
876 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
877 error
= __rpc_rmpipe(dir
, dentry
);
878 mutex_unlock(&dir
->i_mutex
);
882 EXPORT_SYMBOL_GPL(rpc_unlink
);
889 static const struct rpc_filelist authfiles
[] = {
892 .i_fop
= &rpc_info_operations
,
893 .mode
= S_IFREG
| S_IRUSR
,
897 static int rpc_clntdir_populate(struct dentry
*dentry
, void *private)
899 return rpc_populate(dentry
,
900 authfiles
, RPCAUTH_info
, RPCAUTH_EOF
,
904 static void rpc_clntdir_depopulate(struct dentry
*dentry
)
906 rpc_depopulate(dentry
, authfiles
, RPCAUTH_info
, RPCAUTH_EOF
);
910 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
911 * @dentry: dentry from the rpc_pipefs root to the new directory
912 * @name: &struct qstr for the name
913 * @rpc_client: rpc client to associate with this directory
915 * This creates a directory at the given @path associated with
916 * @rpc_clnt, which will contain a file named "info" with some basic
917 * information about the client, together with any "pipes" that may
918 * later be created using rpc_mkpipe().
920 struct dentry
*rpc_create_client_dir(struct dentry
*dentry
,
922 struct rpc_clnt
*rpc_client
)
924 return rpc_mkdir_populate(dentry
, name
, S_IRUGO
| S_IXUGO
, NULL
,
925 rpc_clntdir_populate
, rpc_client
);
929 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
932 int rpc_remove_client_dir(struct dentry
*dentry
)
934 return rpc_rmdir_depopulate(dentry
, rpc_clntdir_depopulate
);
937 static const struct rpc_filelist cache_pipefs_files
[3] = {
940 .i_fop
= &cache_file_operations_pipefs
,
941 .mode
= S_IFREG
|S_IRUSR
|S_IWUSR
,
945 .i_fop
= &content_file_operations_pipefs
,
946 .mode
= S_IFREG
|S_IRUSR
,
950 .i_fop
= &cache_flush_operations_pipefs
,
951 .mode
= S_IFREG
|S_IRUSR
|S_IWUSR
,
955 static int rpc_cachedir_populate(struct dentry
*dentry
, void *private)
957 return rpc_populate(dentry
,
958 cache_pipefs_files
, 0, 3,
962 static void rpc_cachedir_depopulate(struct dentry
*dentry
)
964 rpc_depopulate(dentry
, cache_pipefs_files
, 0, 3);
967 struct dentry
*rpc_create_cache_dir(struct dentry
*parent
, struct qstr
*name
,
968 umode_t umode
, struct cache_detail
*cd
)
970 return rpc_mkdir_populate(parent
, name
, umode
, NULL
,
971 rpc_cachedir_populate
, cd
);
974 void rpc_remove_cache_dir(struct dentry
*dentry
)
976 rpc_rmdir_depopulate(dentry
, rpc_cachedir_depopulate
);
980 * populate the filesystem
982 static const struct super_operations s_ops
= {
983 .alloc_inode
= rpc_alloc_inode
,
984 .destroy_inode
= rpc_destroy_inode
,
985 .statfs
= simple_statfs
,
988 #define RPCAUTH_GSSMAGIC 0x67596969
991 * We have a single directory with 1 node in it.
1004 static const struct rpc_filelist files
[] = {
1007 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1011 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1015 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1017 [RPCAUTH_portmap
] = {
1019 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1023 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1025 [RPCAUTH_nfsd4_cb
] = {
1027 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1031 .mode
= S_IFDIR
| S_IRUGO
| S_IXUGO
,
1036 * This call can be used only in RPC pipefs mount notification hooks.
1038 struct dentry
*rpc_d_lookup_sb(const struct super_block
*sb
,
1039 const unsigned char *dir_name
)
1043 .len
= strlen(dir_name
),
1044 .hash
= full_name_hash(dir_name
, strlen(dir_name
)),
1047 return d_lookup(sb
->s_root
, &dir
);
1049 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb
);
1051 void rpc_pipefs_init_net(struct net
*net
)
1053 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
1055 mutex_init(&sn
->pipefs_sb_lock
);
1059 * This call will be used for per network namespace operations calls.
1060 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1061 * found. This lock have to be released by rpc_put_sb_net() when all operations
1062 * will be completed.
1064 struct super_block
*rpc_get_sb_net(const struct net
*net
)
1066 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
1068 mutex_lock(&sn
->pipefs_sb_lock
);
1070 return sn
->pipefs_sb
;
1071 mutex_unlock(&sn
->pipefs_sb_lock
);
1074 EXPORT_SYMBOL_GPL(rpc_get_sb_net
);
1076 void rpc_put_sb_net(const struct net
*net
)
1078 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
1080 BUG_ON(sn
->pipefs_sb
== NULL
);
1081 mutex_unlock(&sn
->pipefs_sb_lock
);
1083 EXPORT_SYMBOL_GPL(rpc_put_sb_net
);
1086 rpc_fill_super(struct super_block
*sb
, void *data
, int silent
)
1088 struct inode
*inode
;
1089 struct dentry
*root
;
1090 struct net
*net
= data
;
1091 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
1094 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
1095 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
1096 sb
->s_magic
= RPCAUTH_GSSMAGIC
;
1098 sb
->s_time_gran
= 1;
1100 inode
= rpc_get_inode(sb
, S_IFDIR
| 0755);
1103 sb
->s_root
= root
= d_alloc_root(inode
);
1108 if (rpc_populate(root
, files
, RPCAUTH_lockd
, RPCAUTH_RootEOF
, NULL
))
1110 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net
,
1112 err
= blocking_notifier_call_chain(&rpc_pipefs_notifier_list
,
1116 goto err_depopulate
;
1117 sb
->s_fs_info
= get_net(net
);
1122 blocking_notifier_call_chain(&rpc_pipefs_notifier_list
,
1125 __rpc_depopulate(root
, files
, RPCAUTH_lockd
, RPCAUTH_RootEOF
);
1129 static struct dentry
*
1130 rpc_mount(struct file_system_type
*fs_type
,
1131 int flags
, const char *dev_name
, void *data
)
1133 return mount_ns(fs_type
, flags
, current
->nsproxy
->net_ns
, rpc_fill_super
);
1136 void rpc_kill_sb(struct super_block
*sb
)
1138 struct net
*net
= sb
->s_fs_info
;
1139 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
1141 mutex_lock(&sn
->pipefs_sb_lock
);
1142 sn
->pipefs_sb
= NULL
;
1143 mutex_unlock(&sn
->pipefs_sb_lock
);
1145 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net
,
1147 blocking_notifier_call_chain(&rpc_pipefs_notifier_list
,
1150 kill_litter_super(sb
);
1153 static struct file_system_type rpc_pipe_fs_type
= {
1154 .owner
= THIS_MODULE
,
1155 .name
= "rpc_pipefs",
1157 .kill_sb
= rpc_kill_sb
,
1161 init_once(void *foo
)
1163 struct rpc_inode
*rpci
= (struct rpc_inode
*) foo
;
1165 inode_init_once(&rpci
->vfs_inode
);
1166 rpci
->private = NULL
;
1170 int register_rpc_pipefs(void)
1174 rpc_inode_cachep
= kmem_cache_create("rpc_inode_cache",
1175 sizeof(struct rpc_inode
),
1176 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
1179 if (!rpc_inode_cachep
)
1181 err
= rpc_clients_notifier_register();
1184 err
= register_filesystem(&rpc_pipe_fs_type
);
1190 rpc_clients_notifier_unregister();
1192 kmem_cache_destroy(rpc_inode_cachep
);
1196 void unregister_rpc_pipefs(void)
1198 rpc_clients_notifier_unregister();
1199 kmem_cache_destroy(rpc_inode_cachep
);
1200 unregister_filesystem(&rpc_pipe_fs_type
);
1203 /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1204 MODULE_ALIAS("rpc_pipefs");