1 // SPDX-License-Identifier: GPL-2.0-only
3 * net/sunrpc/rpc_pipe.c
5 * Userland/kernel interface for rpcauth_gss.
6 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
9 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
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/fsnotify.h>
19 #include <linux/kernel.h>
20 #include <linux/rcupdate.h>
21 #include <linux/utsname.h>
23 #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/workqueue.h>
30 #include <linux/sunrpc/rpc_pipe_fs.h>
31 #include <linux/sunrpc/cache.h>
32 #include <linux/nsproxy.h>
33 #include <linux/notifier.h>
38 #define RPCDBG_FACILITY RPCDBG_DEBUG
40 #define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
42 static struct file_system_type rpc_pipe_fs_type;
43 static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
45 static struct kmem_cache *rpc_inode_cachep __read_mostly;
47 #define RPC_UPCALL_TIMEOUT (30*HZ)
49 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
51 int rpc_pipefs_notifier_register(struct notifier_block *nb)
53 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
55 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
57 void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
59 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
61 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
63 static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
64 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
66 struct rpc_pipe_msg *msg;
71 msg = list_entry(head->next, struct rpc_pipe_msg, list);
72 list_del_init(&msg->list);
75 } while (!list_empty(head));
82 rpc_timeout_upcall_queue(struct work_struct *work)
85 struct rpc_pipe *pipe =
86 container_of(work, struct rpc_pipe, queue_timeout.work);
87 void (*destroy_msg)(struct rpc_pipe_msg *);
88 struct dentry *dentry;
90 spin_lock(&pipe->lock);
91 destroy_msg = pipe->ops->destroy_msg;
92 if (pipe->nreaders == 0) {
93 list_splice_init(&pipe->pipe, &free_list);
96 dentry = dget(pipe->dentry);
97 spin_unlock(&pipe->lock);
98 rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
99 &free_list, destroy_msg, -ETIMEDOUT);
103 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
104 char __user *dst, size_t buflen)
106 char *data = (char *)msg->data + msg->copied;
107 size_t mlen = min(msg->len - msg->copied, buflen);
110 left = copy_to_user(dst, data, mlen);
112 msg->errno = -EFAULT;
121 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
124 * rpc_queue_upcall - queue an upcall message to userspace
125 * @pipe: upcall pipe on which to queue given message
126 * @msg: message to queue
128 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
129 * A userspace process may then later read the upcall by performing a
130 * read on an open file for this inode. It is up to the caller to
131 * initialize the fields of @msg (other than @msg->list) appropriately.
134 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
137 struct dentry *dentry;
139 spin_lock(&pipe->lock);
140 if (pipe->nreaders) {
141 list_add_tail(&msg->list, &pipe->pipe);
142 pipe->pipelen += msg->len;
144 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
145 if (list_empty(&pipe->pipe))
146 queue_delayed_work(rpciod_workqueue,
147 &pipe->queue_timeout,
149 list_add_tail(&msg->list, &pipe->pipe);
150 pipe->pipelen += msg->len;
153 dentry = dget(pipe->dentry);
154 spin_unlock(&pipe->lock);
156 wake_up(&RPC_I(d_inode(dentry))->waitq);
161 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
164 rpc_inode_setowner(struct inode *inode, void *private)
166 RPC_I(inode)->private = private;
170 rpc_close_pipes(struct inode *inode)
172 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
174 LIST_HEAD(free_list);
177 spin_lock(&pipe->lock);
178 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
180 list_splice_init(&pipe->in_upcall, &free_list);
181 list_splice_init(&pipe->pipe, &free_list);
184 spin_unlock(&pipe->lock);
185 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
187 if (need_release && pipe->ops->release_pipe)
188 pipe->ops->release_pipe(inode);
189 cancel_delayed_work_sync(&pipe->queue_timeout);
190 rpc_inode_setowner(inode, NULL);
191 RPC_I(inode)->pipe = NULL;
195 static struct inode *
196 rpc_alloc_inode(struct super_block *sb)
198 struct rpc_inode *rpci;
199 rpci = kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
202 return &rpci->vfs_inode;
206 rpc_free_inode(struct inode *inode)
208 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
212 rpc_pipe_open(struct inode *inode, struct file *filp)
214 struct rpc_pipe *pipe;
219 pipe = RPC_I(inode)->pipe;
222 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
223 if (first_open && pipe->ops->open_pipe) {
224 res = pipe->ops->open_pipe(inode);
228 if (filp->f_mode & FMODE_READ)
230 if (filp->f_mode & FMODE_WRITE)
239 rpc_pipe_release(struct inode *inode, struct file *filp)
241 struct rpc_pipe *pipe;
242 struct rpc_pipe_msg *msg;
246 pipe = RPC_I(inode)->pipe;
249 msg = filp->private_data;
251 spin_lock(&pipe->lock);
252 msg->errno = -EAGAIN;
253 list_del_init(&msg->list);
254 spin_unlock(&pipe->lock);
255 pipe->ops->destroy_msg(msg);
257 if (filp->f_mode & FMODE_WRITE)
259 if (filp->f_mode & FMODE_READ) {
261 if (pipe->nreaders == 0) {
262 LIST_HEAD(free_list);
263 spin_lock(&pipe->lock);
264 list_splice_init(&pipe->pipe, &free_list);
266 spin_unlock(&pipe->lock);
267 rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
268 pipe->ops->destroy_msg, -EAGAIN);
271 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
272 if (last_close && pipe->ops->release_pipe)
273 pipe->ops->release_pipe(inode);
280 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
282 struct inode *inode = file_inode(filp);
283 struct rpc_pipe *pipe;
284 struct rpc_pipe_msg *msg;
288 pipe = RPC_I(inode)->pipe;
293 msg = filp->private_data;
295 spin_lock(&pipe->lock);
296 if (!list_empty(&pipe->pipe)) {
297 msg = list_entry(pipe->pipe.next,
300 list_move(&msg->list, &pipe->in_upcall);
301 pipe->pipelen -= msg->len;
302 filp->private_data = msg;
305 spin_unlock(&pipe->lock);
309 /* NOTE: it is up to the callback to update msg->copied */
310 res = pipe->ops->upcall(filp, msg, buf, len);
311 if (res < 0 || msg->len == msg->copied) {
312 filp->private_data = NULL;
313 spin_lock(&pipe->lock);
314 list_del_init(&msg->list);
315 spin_unlock(&pipe->lock);
316 pipe->ops->destroy_msg(msg);
324 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
326 struct inode *inode = file_inode(filp);
331 if (RPC_I(inode)->pipe != NULL)
332 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
338 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
340 struct inode *inode = file_inode(filp);
341 struct rpc_inode *rpci = RPC_I(inode);
342 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
344 poll_wait(filp, &rpci->waitq, wait);
347 if (rpci->pipe == NULL)
348 mask |= EPOLLERR | EPOLLHUP;
349 else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
350 mask |= EPOLLIN | EPOLLRDNORM;
356 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
358 struct inode *inode = file_inode(filp);
359 struct rpc_pipe *pipe;
365 pipe = RPC_I(inode)->pipe;
370 spin_lock(&pipe->lock);
372 if (filp->private_data) {
373 struct rpc_pipe_msg *msg;
374 msg = filp->private_data;
375 len += msg->len - msg->copied;
377 spin_unlock(&pipe->lock);
379 return put_user(len, (int __user *)arg);
385 static const struct file_operations rpc_pipe_fops = {
386 .owner = THIS_MODULE,
388 .read = rpc_pipe_read,
389 .write = rpc_pipe_write,
390 .poll = rpc_pipe_poll,
391 .unlocked_ioctl = rpc_pipe_ioctl,
392 .open = rpc_pipe_open,
393 .release = rpc_pipe_release,
397 rpc_show_info(struct seq_file *m, void *v)
399 struct rpc_clnt *clnt = m->private;
402 seq_printf(m, "RPC server: %s\n",
403 rcu_dereference(clnt->cl_xprt)->servername);
404 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
405 clnt->cl_prog, clnt->cl_vers);
406 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
407 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
408 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
414 rpc_info_open(struct inode *inode, struct file *file)
416 struct rpc_clnt *clnt = NULL;
417 int ret = single_open(file, rpc_show_info, NULL);
420 struct seq_file *m = file->private_data;
422 spin_lock(&file->f_path.dentry->d_lock);
423 if (!d_unhashed(file->f_path.dentry))
424 clnt = RPC_I(inode)->private;
425 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426 spin_unlock(&file->f_path.dentry->d_lock);
429 spin_unlock(&file->f_path.dentry->d_lock);
430 single_release(inode, file);
438 rpc_info_release(struct inode *inode, struct file *file)
440 struct seq_file *m = file->private_data;
441 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
444 rpc_release_client(clnt);
445 return single_release(inode, file);
448 static const struct file_operations rpc_info_operations = {
449 .owner = THIS_MODULE,
450 .open = rpc_info_open,
453 .release = rpc_info_release,
458 * Description of fs contents.
460 struct rpc_filelist {
462 const struct file_operations *i_fop;
466 static struct inode *
467 rpc_get_inode(struct super_block *sb, umode_t mode)
469 struct inode *inode = new_inode(sb);
472 inode->i_ino = get_next_ino();
473 inode->i_mode = mode;
474 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
475 switch (mode & S_IFMT) {
477 inode->i_fop = &simple_dir_operations;
478 inode->i_op = &simple_dir_inode_operations;
486 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
488 const struct file_operations *i_fop,
494 inode = rpc_get_inode(dir->i_sb, mode);
497 inode->i_ino = iunique(dir->i_sb, 100);
499 inode->i_fop = i_fop;
501 rpc_inode_setowner(inode, private);
502 d_add(dentry, inode);
505 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
506 __FILE__, __func__, dentry);
511 static int __rpc_create(struct inode *dir, struct dentry *dentry,
513 const struct file_operations *i_fop,
518 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
521 fsnotify_create(dir, dentry);
525 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
527 const struct file_operations *i_fop,
532 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
536 fsnotify_mkdir(dir, dentry);
541 init_pipe(struct rpc_pipe *pipe)
545 INIT_LIST_HEAD(&pipe->in_upcall);
546 INIT_LIST_HEAD(&pipe->in_downcall);
547 INIT_LIST_HEAD(&pipe->pipe);
549 INIT_DELAYED_WORK(&pipe->queue_timeout,
550 rpc_timeout_upcall_queue);
552 spin_lock_init(&pipe->lock);
556 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
560 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
562 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
564 struct rpc_pipe *pipe;
566 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
568 return ERR_PTR(-ENOMEM);
574 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
576 static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
578 const struct file_operations *i_fop,
580 struct rpc_pipe *pipe)
582 struct rpc_inode *rpci;
585 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
588 rpci = RPC_I(d_inode(dentry));
589 rpci->private = private;
591 fsnotify_create(dir, dentry);
595 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
600 ret = simple_rmdir(dir, dentry);
602 fsnotify_rmdir(dir, dentry);
608 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
613 ret = simple_unlink(dir, dentry);
615 fsnotify_unlink(dir, dentry);
621 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
623 struct inode *inode = d_inode(dentry);
625 rpc_close_pipes(inode);
626 return __rpc_unlink(dir, dentry);
629 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
632 struct qstr q = QSTR_INIT(name, strlen(name));
633 struct dentry *dentry = d_hash_and_lookup(parent, &q);
635 dentry = d_alloc(parent, &q);
637 return ERR_PTR(-ENOMEM);
639 if (d_really_is_negative(dentry))
642 return ERR_PTR(-EEXIST);
646 * FIXME: This probably has races.
648 static void __rpc_depopulate(struct dentry *parent,
649 const struct rpc_filelist *files,
652 struct inode *dir = d_inode(parent);
653 struct dentry *dentry;
657 for (i = start; i < eof; i++) {
658 name.name = files[i].name;
659 name.len = strlen(files[i].name);
660 dentry = d_hash_and_lookup(parent, &name);
664 if (d_really_is_negative(dentry))
666 switch (d_inode(dentry)->i_mode & S_IFMT) {
670 __rpc_unlink(dir, dentry);
673 __rpc_rmdir(dir, dentry);
680 static void rpc_depopulate(struct dentry *parent,
681 const struct rpc_filelist *files,
684 struct inode *dir = d_inode(parent);
686 inode_lock_nested(dir, I_MUTEX_CHILD);
687 __rpc_depopulate(parent, files, start, eof);
691 static int rpc_populate(struct dentry *parent,
692 const struct rpc_filelist *files,
696 struct inode *dir = d_inode(parent);
697 struct dentry *dentry;
701 for (i = start; i < eof; i++) {
702 dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
703 err = PTR_ERR(dentry);
706 switch (files[i].mode & S_IFMT) {
710 err = __rpc_create(dir, dentry,
716 err = __rpc_mkdir(dir, dentry,
727 __rpc_depopulate(parent, files, start, eof);
729 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
730 __FILE__, __func__, parent);
734 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
735 const char *name, umode_t mode, void *private,
736 int (*populate)(struct dentry *, void *), void *args_populate)
738 struct dentry *dentry;
739 struct inode *dir = d_inode(parent);
742 inode_lock_nested(dir, I_MUTEX_PARENT);
743 dentry = __rpc_lookup_create_exclusive(parent, name);
746 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
749 if (populate != NULL) {
750 error = populate(dentry, args_populate);
758 __rpc_rmdir(dir, dentry);
760 dentry = ERR_PTR(error);
764 static int rpc_rmdir_depopulate(struct dentry *dentry,
765 void (*depopulate)(struct dentry *))
767 struct dentry *parent;
771 parent = dget_parent(dentry);
772 dir = d_inode(parent);
773 inode_lock_nested(dir, I_MUTEX_PARENT);
774 if (depopulate != NULL)
776 error = __rpc_rmdir(dir, dentry);
783 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
784 * @parent: dentry of directory to create new "pipe" in
785 * @name: name of pipe
786 * @private: private data to associate with the pipe, for the caller's use
787 * @pipe: &rpc_pipe containing input parameters
789 * Data is made available for userspace to read by calls to
790 * rpc_queue_upcall(). The actual reads will result in calls to
791 * @ops->upcall, which will be called with the file pointer,
792 * message, and userspace buffer to copy to.
794 * Writes can come at any time, and do not necessarily have to be
795 * responses to upcalls. They will result in calls to @msg->downcall.
797 * The @private argument passed here will be available to all these methods
798 * from the file pointer, via RPC_I(file_inode(file))->private.
800 struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
801 void *private, struct rpc_pipe *pipe)
803 struct dentry *dentry;
804 struct inode *dir = d_inode(parent);
805 umode_t umode = S_IFIFO | 0600;
808 if (pipe->ops->upcall == NULL)
810 if (pipe->ops->downcall == NULL)
813 inode_lock_nested(dir, I_MUTEX_PARENT);
814 dentry = __rpc_lookup_create_exclusive(parent, name);
817 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
825 dentry = ERR_PTR(err);
826 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
827 __FILE__, __func__, parent, name,
831 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
834 * rpc_unlink - remove a pipe
835 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
837 * After this call, lookups will no longer find the pipe, and any
838 * attempts to read or write using preexisting opens of the pipe will
842 rpc_unlink(struct dentry *dentry)
844 struct dentry *parent;
848 parent = dget_parent(dentry);
849 dir = d_inode(parent);
850 inode_lock_nested(dir, I_MUTEX_PARENT);
851 error = __rpc_rmpipe(dir, dentry);
856 EXPORT_SYMBOL_GPL(rpc_unlink);
859 * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
860 * @pdh: pointer to struct rpc_pipe_dir_head
862 void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
864 INIT_LIST_HEAD(&pdh->pdh_entries);
865 pdh->pdh_dentry = NULL;
867 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
870 * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
871 * @pdo: pointer to struct rpc_pipe_dir_object
872 * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
873 * @pdo_data: pointer to caller-defined data
875 void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
876 const struct rpc_pipe_dir_object_ops *pdo_ops,
879 INIT_LIST_HEAD(&pdo->pdo_head);
880 pdo->pdo_ops = pdo_ops;
881 pdo->pdo_data = pdo_data;
883 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
886 rpc_add_pipe_dir_object_locked(struct net *net,
887 struct rpc_pipe_dir_head *pdh,
888 struct rpc_pipe_dir_object *pdo)
893 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
895 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
900 rpc_remove_pipe_dir_object_locked(struct net *net,
901 struct rpc_pipe_dir_head *pdh,
902 struct rpc_pipe_dir_object *pdo)
905 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
906 list_del_init(&pdo->pdo_head);
910 * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
911 * @net: pointer to struct net
912 * @pdh: pointer to struct rpc_pipe_dir_head
913 * @pdo: pointer to struct rpc_pipe_dir_object
917 rpc_add_pipe_dir_object(struct net *net,
918 struct rpc_pipe_dir_head *pdh,
919 struct rpc_pipe_dir_object *pdo)
923 if (list_empty(&pdo->pdo_head)) {
924 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
926 mutex_lock(&sn->pipefs_sb_lock);
927 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
928 mutex_unlock(&sn->pipefs_sb_lock);
932 EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
935 * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
936 * @net: pointer to struct net
937 * @pdh: pointer to struct rpc_pipe_dir_head
938 * @pdo: pointer to struct rpc_pipe_dir_object
942 rpc_remove_pipe_dir_object(struct net *net,
943 struct rpc_pipe_dir_head *pdh,
944 struct rpc_pipe_dir_object *pdo)
946 if (!list_empty(&pdo->pdo_head)) {
947 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
949 mutex_lock(&sn->pipefs_sb_lock);
950 rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
951 mutex_unlock(&sn->pipefs_sb_lock);
954 EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
957 * rpc_find_or_alloc_pipe_dir_object
958 * @net: pointer to struct net
959 * @pdh: pointer to struct rpc_pipe_dir_head
960 * @match: match struct rpc_pipe_dir_object to data
961 * @alloc: allocate a new struct rpc_pipe_dir_object
962 * @data: user defined data for match() and alloc()
965 struct rpc_pipe_dir_object *
966 rpc_find_or_alloc_pipe_dir_object(struct net *net,
967 struct rpc_pipe_dir_head *pdh,
968 int (*match)(struct rpc_pipe_dir_object *, void *),
969 struct rpc_pipe_dir_object *(*alloc)(void *),
972 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
973 struct rpc_pipe_dir_object *pdo;
975 mutex_lock(&sn->pipefs_sb_lock);
976 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
977 if (!match(pdo, data))
984 rpc_add_pipe_dir_object_locked(net, pdh, pdo);
986 mutex_unlock(&sn->pipefs_sb_lock);
989 EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
992 rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
994 struct rpc_pipe_dir_object *pdo;
995 struct dentry *dir = pdh->pdh_dentry;
997 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
998 pdo->pdo_ops->create(dir, pdo);
1002 rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
1004 struct rpc_pipe_dir_object *pdo;
1005 struct dentry *dir = pdh->pdh_dentry;
1007 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1008 pdo->pdo_ops->destroy(dir, pdo);
1016 static const struct rpc_filelist authfiles[] = {
1019 .i_fop = &rpc_info_operations,
1020 .mode = S_IFREG | 0400,
1024 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1026 return rpc_populate(dentry,
1027 authfiles, RPCAUTH_info, RPCAUTH_EOF,
1031 static void rpc_clntdir_depopulate(struct dentry *dentry)
1033 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1037 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
1038 * @dentry: the parent of new directory
1039 * @name: the name of new directory
1040 * @rpc_client: rpc client to associate with this directory
1042 * This creates a directory at the given @path associated with
1043 * @rpc_clnt, which will contain a file named "info" with some basic
1044 * information about the client, together with any "pipes" that may
1045 * later be created using rpc_mkpipe().
1047 struct dentry *rpc_create_client_dir(struct dentry *dentry,
1049 struct rpc_clnt *rpc_client)
1053 ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1054 rpc_clntdir_populate, rpc_client);
1056 rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1057 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1063 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
1064 * @rpc_client: rpc_client for the pipe
1066 int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
1068 struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1072 rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1073 rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
1074 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
1077 static const struct rpc_filelist cache_pipefs_files[3] = {
1080 .i_fop = &cache_file_operations_pipefs,
1081 .mode = S_IFREG | 0600,
1085 .i_fop = &content_file_operations_pipefs,
1086 .mode = S_IFREG | 0400,
1090 .i_fop = &cache_flush_operations_pipefs,
1091 .mode = S_IFREG | 0600,
1095 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1097 return rpc_populate(dentry,
1098 cache_pipefs_files, 0, 3,
1102 static void rpc_cachedir_depopulate(struct dentry *dentry)
1104 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1107 struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
1108 umode_t umode, struct cache_detail *cd)
1110 return rpc_mkdir_populate(parent, name, umode, NULL,
1111 rpc_cachedir_populate, cd);
1114 void rpc_remove_cache_dir(struct dentry *dentry)
1116 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1120 * populate the filesystem
1122 static const struct super_operations s_ops = {
1123 .alloc_inode = rpc_alloc_inode,
1124 .free_inode = rpc_free_inode,
1125 .statfs = simple_statfs,
1128 #define RPCAUTH_GSSMAGIC 0x67596969
1131 * We have a single directory with 1 node in it.
1146 static const struct rpc_filelist files[] = {
1149 .mode = S_IFDIR | 0555,
1153 .mode = S_IFDIR | 0555,
1157 .mode = S_IFDIR | 0555,
1159 [RPCAUTH_portmap] = {
1161 .mode = S_IFDIR | 0555,
1165 .mode = S_IFDIR | 0555,
1167 [RPCAUTH_nfsd4_cb] = {
1169 .mode = S_IFDIR | 0555,
1173 .mode = S_IFDIR | 0555,
1177 .mode = S_IFDIR | 0555,
1181 .mode = S_IFDIR | 0555,
1186 * This call can be used only in RPC pipefs mount notification hooks.
1188 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1189 const unsigned char *dir_name)
1191 struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
1192 return d_hash_and_lookup(sb->s_root, &dir);
1194 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1196 int rpc_pipefs_init_net(struct net *net)
1198 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1200 sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1201 if (IS_ERR(sn->gssd_dummy))
1202 return PTR_ERR(sn->gssd_dummy);
1204 mutex_init(&sn->pipefs_sb_lock);
1205 sn->pipe_version = -1;
1209 void rpc_pipefs_exit_net(struct net *net)
1211 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1213 rpc_destroy_pipe_data(sn->gssd_dummy);
1217 * This call will be used for per network namespace operations calls.
1218 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1219 * found. This lock have to be released by rpc_put_sb_net() when all operations
1220 * will be completed.
1222 struct super_block *rpc_get_sb_net(const struct net *net)
1224 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1226 mutex_lock(&sn->pipefs_sb_lock);
1228 return sn->pipefs_sb;
1229 mutex_unlock(&sn->pipefs_sb_lock);
1232 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1234 void rpc_put_sb_net(const struct net *net)
1236 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1238 WARN_ON(sn->pipefs_sb == NULL);
1239 mutex_unlock(&sn->pipefs_sb_lock);
1241 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1243 static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1246 .mode = S_IFDIR | 0555,
1251 dummy_downcall(struct file *filp, const char __user *src, size_t len)
1256 static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1257 .upcall = rpc_pipe_generic_upcall,
1258 .downcall = dummy_downcall,
1262 * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1263 * that it will ever use this info to handle an upcall, but rpc.gssd expects
1264 * that this file will be there and have a certain format.
1267 rpc_dummy_info_show(struct seq_file *m, void *v)
1269 seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1270 seq_printf(m, "service: foo (1) version 0\n");
1271 seq_printf(m, "address: 127.0.0.1\n");
1272 seq_printf(m, "protocol: tcp\n");
1273 seq_printf(m, "port: 0\n");
1276 DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
1278 static const struct rpc_filelist gssd_dummy_info_file[] = {
1281 .i_fop = &rpc_dummy_info_fops,
1282 .mode = S_IFREG | 0400,
1287 * rpc_gssd_dummy_populate - create a dummy gssd pipe
1288 * @root: root of the rpc_pipefs filesystem
1289 * @pipe_data: pipe data created when netns is initialized
1291 * Create a dummy set of directories and a pipe that gssd can hold open to
1292 * indicate that it is up and running.
1294 static struct dentry *
1295 rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1298 struct dentry *gssd_dentry;
1299 struct dentry *clnt_dentry = NULL;
1300 struct dentry *pipe_dentry = NULL;
1301 struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1302 strlen(files[RPCAUTH_gssd].name));
1304 /* We should never get this far if "gssd" doesn't exist */
1305 gssd_dentry = d_hash_and_lookup(root, &q);
1307 return ERR_PTR(-ENOENT);
1309 ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1311 pipe_dentry = ERR_PTR(ret);
1315 q.name = gssd_dummy_clnt_dir[0].name;
1316 q.len = strlen(gssd_dummy_clnt_dir[0].name);
1317 clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1319 pipe_dentry = ERR_PTR(-ENOENT);
1323 ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1325 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1326 pipe_dentry = ERR_PTR(ret);
1330 pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
1331 if (IS_ERR(pipe_dentry)) {
1332 __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
1333 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1342 rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1344 struct dentry *clnt_dir = pipe_dentry->d_parent;
1345 struct dentry *gssd_dir = clnt_dir->d_parent;
1348 __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
1349 __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1350 __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1355 rpc_fill_super(struct super_block *sb, void *data, int silent)
1357 struct inode *inode;
1358 struct dentry *root, *gssd_dentry;
1359 struct net *net = get_net(sb->s_fs_info);
1360 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1363 sb->s_blocksize = PAGE_SIZE;
1364 sb->s_blocksize_bits = PAGE_SHIFT;
1365 sb->s_magic = RPCAUTH_GSSMAGIC;
1367 sb->s_d_op = &simple_dentry_operations;
1368 sb->s_time_gran = 1;
1370 inode = rpc_get_inode(sb, S_IFDIR | 0555);
1371 sb->s_root = root = d_make_root(inode);
1374 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1377 gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1378 if (IS_ERR(gssd_dentry)) {
1379 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1380 return PTR_ERR(gssd_dentry);
1383 dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
1384 net->ns.inum, NET_NAME(net));
1385 mutex_lock(&sn->pipefs_sb_lock);
1387 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1391 goto err_depopulate;
1392 mutex_unlock(&sn->pipefs_sb_lock);
1396 rpc_gssd_dummy_depopulate(gssd_dentry);
1397 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1400 sn->pipefs_sb = NULL;
1401 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1402 mutex_unlock(&sn->pipefs_sb_lock);
1407 gssd_running(struct net *net)
1409 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1410 struct rpc_pipe *pipe = sn->gssd_dummy;
1412 return pipe->nreaders || pipe->nwriters;
1414 EXPORT_SYMBOL_GPL(gssd_running);
1416 static struct dentry *
1417 rpc_mount(struct file_system_type *fs_type,
1418 int flags, const char *dev_name, void *data)
1420 struct net *net = current->nsproxy->net_ns;
1421 return mount_ns(fs_type, flags, data, net, net->user_ns, rpc_fill_super);
1424 static void rpc_kill_sb(struct super_block *sb)
1426 struct net *net = sb->s_fs_info;
1427 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1429 mutex_lock(&sn->pipefs_sb_lock);
1430 if (sn->pipefs_sb != sb) {
1431 mutex_unlock(&sn->pipefs_sb_lock);
1434 sn->pipefs_sb = NULL;
1435 dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
1436 net->ns.inum, NET_NAME(net));
1437 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1440 mutex_unlock(&sn->pipefs_sb_lock);
1442 kill_litter_super(sb);
1446 static struct file_system_type rpc_pipe_fs_type = {
1447 .owner = THIS_MODULE,
1448 .name = "rpc_pipefs",
1450 .kill_sb = rpc_kill_sb,
1452 MODULE_ALIAS_FS("rpc_pipefs");
1453 MODULE_ALIAS("rpc_pipefs");
1456 init_once(void *foo)
1458 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1460 inode_init_once(&rpci->vfs_inode);
1461 rpci->private = NULL;
1463 init_waitqueue_head(&rpci->waitq);
1466 int register_rpc_pipefs(void)
1470 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1471 sizeof(struct rpc_inode),
1472 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1473 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1475 if (!rpc_inode_cachep)
1477 err = rpc_clients_notifier_register();
1480 err = register_filesystem(&rpc_pipe_fs_type);
1486 rpc_clients_notifier_unregister();
1488 kmem_cache_destroy(rpc_inode_cachep);
1492 void unregister_rpc_pipefs(void)
1494 rpc_clients_notifier_unregister();
1495 kmem_cache_destroy(rpc_inode_cachep);
1496 unregister_filesystem(&rpc_pipe_fs_type);