blob: 126d31472a995df07b24f777f07181b108618e6b [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * net/sunrpc/rpc_pipe.c
4 *
5 * Userland/kernel interface for rpcauth_gss.
6 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
Rolf Eike Beerd51fe1b2005-09-02 08:59:25 +02007 * and fs/sysfs/inode.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
10 *
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#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>
Trond Myklebust50e437d2007-06-07 22:44:34 -040018#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/kernel.h>
Trond Myklebust2446ab62012-03-01 17:00:56 -050020#include <linux/rcupdate.h>
Jeff Laytone2f0c832013-12-05 07:34:44 -050021#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/ioctls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/poll.h>
25#include <linux/wait.h>
26#include <linux/seq_file.h>
27
28#include <linux/sunrpc/clnt.h>
29#include <linux/workqueue.h>
30#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040031#include <linux/sunrpc/cache.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030032#include <linux/nsproxy.h>
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030033#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030035#include "netns.h"
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030036#include "sunrpc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +030038#define RPCDBG_FACILITY RPCDBG_DEBUG
39
40#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static struct file_system_type rpc_pipe_fs_type;
Jeff Layton4b9a4452013-11-14 07:25:17 -050043static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Christoph Lametere18b8902006-12-06 20:33:20 -080045static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define RPC_UPCALL_TIMEOUT (30*HZ)
48
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030049static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
50
51int rpc_pipefs_notifier_register(struct notifier_block *nb)
52{
53 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
54}
55EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
56
57void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
58{
59 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
60}
61EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
62
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040063static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
Trond Myklebust9842ef32006-02-01 12:18:44 -050064 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050065{
66 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050067
Trond Myklebust9842ef32006-02-01 12:18:44 -050068 if (list_empty(head))
69 return;
70 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050071 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040072 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050073 msg->errno = err;
74 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050075 } while (!list_empty(head));
Jeff Layton92123e02012-06-11 10:03:42 -040076
77 if (waitq)
78 wake_up(waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void
David Howells65f27f32006-11-22 14:55:48 +000082rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Trond Myklebust9842ef32006-02-01 12:18:44 -050084 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030085 struct rpc_pipe *pipe =
86 container_of(work, struct rpc_pipe, queue_timeout.work);
Trond Myklebust9842ef32006-02-01 12:18:44 -050087 void (*destroy_msg)(struct rpc_pipe_msg *);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040088 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030090 spin_lock(&pipe->lock);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030091 destroy_msg = pipe->ops->destroy_msg;
92 if (pipe->nreaders == 0) {
93 list_splice_init(&pipe->pipe, &free_list);
94 pipe->pipelen = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -050095 }
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +040096 dentry = dget(pipe->dentry);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030097 spin_unlock(&pipe->lock);
David Howellsc5ef6032015-03-17 22:26:16 +000098 rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
Jeff Layton92123e02012-06-11 10:03:42 -040099 &free_list, destroy_msg, -ETIMEDOUT);
100 dput(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Peng Taoc1225152011-09-22 21:50:10 -0400103ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
104 char __user *dst, size_t buflen)
105{
106 char *data = (char *)msg->data + msg->copied;
107 size_t mlen = min(msg->len - msg->copied, buflen);
108 unsigned long left;
109
110 left = copy_to_user(dst, data, mlen);
111 if (left == mlen) {
112 msg->errno = -EFAULT;
113 return -EFAULT;
114 }
115
116 mlen -= left;
117 msg->copied += mlen;
118 msg->errno = 0;
119 return mlen;
120}
121EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
122
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500123/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800124 * rpc_queue_upcall - queue an upcall message to userspace
Randy Dunlapbda14602012-05-13 10:35:40 -0700125 * @pipe: upcall pipe on which to queue given message
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500126 * @msg: message to queue
127 *
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.
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133int
Stanislav Kinsburskyd706ed12011-12-26 15:43:49 +0300134rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Trond Myklebust6070fe62005-10-27 22:12:46 -0400136 int res = -EPIPE;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400137 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300139 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300140 if (pipe->nreaders) {
141 list_add_tail(&msg->list, &pipe->pipe);
142 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400143 res = 0;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300144 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
145 if (list_empty(&pipe->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500146 queue_delayed_work(rpciod_workqueue,
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300147 &pipe->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 RPC_UPCALL_TIMEOUT);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300149 list_add_tail(&msg->list, &pipe->pipe);
150 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400151 res = 0;
152 }
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400153 dentry = dget(pipe->dentry);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300154 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400155 if (dentry) {
David Howellsc5ef6032015-03-17 22:26:16 +0000156 wake_up(&RPC_I(d_inode(dentry))->waitq);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400157 dput(dentry);
158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return res;
160}
Trond Myklebust468039e2008-12-23 15:21:31 -0500161EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Trond Myklebust6070fe62005-10-27 22:12:46 -0400163static inline void
164rpc_inode_setowner(struct inode *inode, void *private)
165{
166 RPC_I(inode)->private = private;
167}
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169static void
170rpc_close_pipes(struct inode *inode)
171{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300172 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500173 int need_release;
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400174 LIST_HEAD(free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Al Viro59551022016-01-22 15:40:57 -0500176 inode_lock(inode);
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400177 spin_lock(&pipe->lock);
178 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
179 pipe->nreaders = 0;
180 list_splice_init(&pipe->in_upcall, &free_list);
181 list_splice_init(&pipe->pipe, &free_list);
182 pipe->pipelen = 0;
183 pipe->dentry = NULL;
184 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400185 rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400186 pipe->nwriters = 0;
187 if (need_release && pipe->ops->release_pipe)
188 pipe->ops->release_pipe(inode);
189 cancel_delayed_work_sync(&pipe->queue_timeout);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400190 rpc_inode_setowner(inode, NULL);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400191 RPC_I(inode)->pipe = NULL;
Al Viro59551022016-01-22 15:40:57 -0500192 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195static struct inode *
196rpc_alloc_inode(struct super_block *sb)
197{
198 struct rpc_inode *rpci;
Himangi Saraogiadcda652014-06-10 20:19:17 +0530199 rpci = kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!rpci)
201 return NULL;
202 return &rpci->vfs_inode;
203}
204
205static void
Al Virobef252f2019-04-15 22:32:27 -0400206rpc_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100207{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100208 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static int
212rpc_pipe_open(struct inode *inode, struct file *filp)
213{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400214 struct rpc_pipe *pipe;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500215 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int res = -ENXIO;
217
Al Viro59551022016-01-22 15:40:57 -0500218 inode_lock(inode);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400219 pipe = RPC_I(inode)->pipe;
220 if (pipe == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500221 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300222 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
223 if (first_open && pipe->ops->open_pipe) {
224 res = pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500225 if (res)
226 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500228 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300229 pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500230 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300231 pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500232 res = 0;
233out:
Al Viro59551022016-01-22 15:40:57 -0500234 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return res;
236}
237
238static int
239rpc_pipe_release(struct inode *inode, struct file *filp)
240{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400241 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500243 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Al Viro59551022016-01-22 15:40:57 -0500245 inode_lock(inode);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400246 pipe = RPC_I(inode)->pipe;
247 if (pipe == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700249 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300251 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500252 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400253 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300254 spin_unlock(&pipe->lock);
255 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300258 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500259 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300260 pipe->nreaders --;
261 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500262 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300263 spin_lock(&pipe->lock);
264 list_splice_init(&pipe->pipe, &free_list);
265 pipe->pipelen = 0;
266 spin_unlock(&pipe->lock);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400267 rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300268 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500269 }
270 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300271 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
272 if (last_close && pipe->ops->release_pipe)
273 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274out:
Al Viro59551022016-01-22 15:40:57 -0500275 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
277}
278
279static ssize_t
280rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
281{
Al Viro496ad9a2013-01-23 17:07:38 -0500282 struct inode *inode = file_inode(filp);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400283 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct rpc_pipe_msg *msg;
285 int res = 0;
286
Al Viro59551022016-01-22 15:40:57 -0500287 inode_lock(inode);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400288 pipe = RPC_I(inode)->pipe;
289 if (pipe == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 res = -EPIPE;
291 goto out_unlock;
292 }
293 msg = filp->private_data;
294 if (msg == NULL) {
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300295 spin_lock(&pipe->lock);
296 if (!list_empty(&pipe->pipe)) {
297 msg = list_entry(pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct rpc_pipe_msg,
299 list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300300 list_move(&msg->list, &pipe->in_upcall);
301 pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 filp->private_data = msg;
303 msg->copied = 0;
304 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300305 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (msg == NULL)
307 goto out_unlock;
308 }
309 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300310 res = pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (res < 0 || msg->len == msg->copied) {
312 filp->private_data = NULL;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300313 spin_lock(&pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400314 list_del_init(&msg->list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300315 spin_unlock(&pipe->lock);
316 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318out_unlock:
Al Viro59551022016-01-22 15:40:57 -0500319 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return res;
321}
322
323static ssize_t
324rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
325{
Al Viro496ad9a2013-01-23 17:07:38 -0500326 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int res;
328
Al Viro59551022016-01-22 15:40:57 -0500329 inode_lock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 res = -EPIPE;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400331 if (RPC_I(inode)->pipe != NULL)
332 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
Al Viro59551022016-01-22 15:40:57 -0500333 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return res;
335}
336
Al Viroade994f2017-07-03 00:01:49 -0400337static __poll_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
339{
Al Viro496ad9a2013-01-23 17:07:38 -0500340 struct inode *inode = file_inode(filp);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400341 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800342 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 poll_wait(filp, &rpci->waitq, wait);
345
Al Viro59551022016-01-22 15:40:57 -0500346 inode_lock(inode);
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400347 if (rpci->pipe == NULL)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800348 mask |= EPOLLERR | EPOLLHUP;
Stanislav Kinsbursky591ad7f2012-02-27 22:05:54 +0400349 else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800350 mask |= EPOLLIN | EPOLLRDNORM;
Al Viro59551022016-01-22 15:40:57 -0500351 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return mask;
353}
354
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200355static long
356rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Al Viro496ad9a2013-01-23 17:07:38 -0500358 struct inode *inode = file_inode(filp);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400359 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int len;
361
362 switch (cmd) {
363 case FIONREAD:
Al Viro59551022016-01-22 15:40:57 -0500364 inode_lock(inode);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400365 pipe = RPC_I(inode)->pipe;
366 if (pipe == NULL) {
Al Viro59551022016-01-22 15:40:57 -0500367 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200369 }
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400370 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300371 len = pipe->pipelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (filp->private_data) {
373 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700374 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 len += msg->len - msg->copied;
376 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300377 spin_unlock(&pipe->lock);
Al Viro59551022016-01-22 15:40:57 -0500378 inode_unlock(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return put_user(len, (int __user *)arg);
380 default:
381 return -EINVAL;
382 }
383}
384
Arjan van de Venda7071d2007-02-12 00:55:36 -0800385static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 .owner = THIS_MODULE,
387 .llseek = no_llseek,
388 .read = rpc_pipe_read,
389 .write = rpc_pipe_write,
390 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200391 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 .open = rpc_pipe_open,
393 .release = rpc_pipe_release,
394};
395
396static int
397rpc_show_info(struct seq_file *m, void *v)
398{
399 struct rpc_clnt *clnt = m->private;
400
Trond Myklebust2446ab62012-03-01 17:00:56 -0500401 rcu_read_lock();
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500402 seq_printf(m, "RPC server: %s\n",
403 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust55909f22013-08-23 11:48:15 -0400404 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400406 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));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400408 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500409 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413static int
414rpc_info_open(struct inode *inode, struct file *file)
415{
Trond Myklebust006abe82010-09-12 19:55:25 -0400416 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int ret = single_open(file, rpc_show_info, NULL);
418
419 if (!ret) {
420 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400421
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 m->private = clnt;
428 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400429 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 single_release(inode, file);
431 ret = -EINVAL;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 return ret;
435}
436
437static int
438rpc_info_release(struct inode *inode, struct file *file)
439{
440 struct seq_file *m = file->private_data;
441 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
442
443 if (clnt)
444 rpc_release_client(clnt);
445 return single_release(inode, file);
446}
447
Arjan van de Venda7071d2007-02-12 00:55:36 -0800448static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .owner = THIS_MODULE,
450 .open = rpc_info_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = rpc_info_release,
454};
455
456
457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * Description of fs contents.
459 */
460struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400461 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800462 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400463 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400467rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 struct inode *inode = new_inode(sb);
470 if (!inode)
471 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400472 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 inode->i_mode = mode;
Deepa Dinamani078cd822016-09-14 07:48:04 -0700474 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Joe Perches89f0e4f2011-07-01 09:43:12 +0000475 switch (mode & S_IFMT) {
476 case S_IFDIR:
477 inode->i_fop = &simple_dir_operations;
Jeff Layton275448e2013-07-16 09:16:45 -0400478 inode->i_op = &simple_dir_inode_operations;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000479 inc_nlink(inode);
480 default:
481 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 return inode;
484}
485
Trond Myklebust75898062009-08-09 15:14:17 -0400486static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
487 umode_t mode,
488 const struct file_operations *i_fop,
489 void *private)
490{
491 struct inode *inode;
492
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000493 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400494 inode = rpc_get_inode(dir->i_sb, mode);
495 if (!inode)
496 goto out_err;
497 inode->i_ino = iunique(dir->i_sb, 100);
498 if (i_fop)
499 inode->i_fop = i_fop;
500 if (private)
501 rpc_inode_setowner(inode, private);
502 d_add(dentry, inode);
503 return 0;
504out_err:
Al Viro1e903ed2013-09-16 10:57:41 -0400505 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
506 __FILE__, __func__, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400507 dput(dentry);
508 return -ENOMEM;
509}
510
Trond Myklebustac6fece2009-08-09 15:14:20 -0400511static int __rpc_create(struct inode *dir, struct dentry *dentry,
512 umode_t mode,
513 const struct file_operations *i_fop,
514 void *private)
515{
516 int err;
517
518 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
519 if (err)
520 return err;
521 fsnotify_create(dir, dentry);
522 return 0;
523}
524
Trond Myklebust75898062009-08-09 15:14:17 -0400525static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
526 umode_t mode,
527 const struct file_operations *i_fop,
528 void *private)
529{
530 int err;
531
532 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
533 if (err)
534 return err;
535 inc_nlink(dir);
536 fsnotify_mkdir(dir, dentry);
537 return 0;
538}
539
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300540static void
541init_pipe(struct rpc_pipe *pipe)
542{
543 pipe->nreaders = 0;
544 pipe->nwriters = 0;
545 INIT_LIST_HEAD(&pipe->in_upcall);
546 INIT_LIST_HEAD(&pipe->in_downcall);
547 INIT_LIST_HEAD(&pipe->pipe);
548 pipe->pipelen = 0;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300549 INIT_DELAYED_WORK(&pipe->queue_timeout,
550 rpc_timeout_upcall_queue);
551 pipe->ops = NULL;
552 spin_lock_init(&pipe->lock);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300553 pipe->dentry = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300554}
555
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300556void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
557{
558 kfree(pipe);
559}
560EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
561
562struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
Trond Myklebust75898062009-08-09 15:14:17 -0400563{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300564 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400565
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300566 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
567 if (!pipe)
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300568 return ERR_PTR(-ENOMEM);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300569 init_pipe(pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300570 pipe->ops = ops;
571 pipe->flags = flags;
572 return pipe;
573}
574EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
575
576static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
577 umode_t mode,
578 const struct file_operations *i_fop,
579 void *private,
580 struct rpc_pipe *pipe)
Trond Myklebust75898062009-08-09 15:14:17 -0400581{
582 struct rpc_inode *rpci;
583 int err;
584
585 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
586 if (err)
587 return err;
David Howellsc5ef6032015-03-17 22:26:16 +0000588 rpci = RPC_I(d_inode(dentry));
Trond Myklebust75898062009-08-09 15:14:17 -0400589 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300590 rpci->pipe = pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400591 fsnotify_create(dir, dentry);
592 return 0;
593}
594
Trond Myklebustac6fece2009-08-09 15:14:20 -0400595static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
596{
597 int ret;
598
599 dget(dentry);
600 ret = simple_rmdir(dir, dentry);
601 d_delete(dentry);
602 dput(dentry);
603 return ret;
604}
605
Trond Myklebust810d90b2009-08-09 15:14:18 -0400606static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
607{
608 int ret;
609
610 dget(dentry);
611 ret = simple_unlink(dir, dentry);
612 d_delete(dentry);
613 dput(dentry);
614 return ret;
615}
616
617static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
618{
David Howellsc5ef6032015-03-17 22:26:16 +0000619 struct inode *inode = d_inode(dentry);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400620
Trond Myklebust810d90b2009-08-09 15:14:18 -0400621 rpc_close_pipes(inode);
622 return __rpc_unlink(dir, dentry);
623}
624
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400625static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
Al Virod3db90b2013-07-14 17:09:57 +0400626 const char *name)
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400627{
Al Virod3db90b2013-07-14 17:09:57 +0400628 struct qstr q = QSTR_INIT(name, strlen(name));
629 struct dentry *dentry = d_hash_and_lookup(parent, &q);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300630 if (!dentry) {
Al Virod3db90b2013-07-14 17:09:57 +0400631 dentry = d_alloc(parent, &q);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300632 if (!dentry)
633 return ERR_PTR(-ENOMEM);
634 }
David Howellsc5ef6032015-03-17 22:26:16 +0000635 if (d_really_is_negative(dentry))
Dan Carpenterf1f0abe12010-03-21 12:10:34 -0400636 return dentry;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400637 dput(dentry);
638 return ERR_PTR(-EEXIST);
639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*
642 * FIXME: This probably has races.
643 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400644static void __rpc_depopulate(struct dentry *parent,
645 const struct rpc_filelist *files,
646 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
David Howellsc5ef6032015-03-17 22:26:16 +0000648 struct inode *dir = d_inode(parent);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400649 struct dentry *dentry;
650 struct qstr name;
651 int i;
652
653 for (i = start; i < eof; i++) {
654 name.name = files[i].name;
655 name.len = strlen(files[i].name);
Al Virod3db90b2013-07-14 17:09:57 +0400656 dentry = d_hash_and_lookup(parent, &name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400657
658 if (dentry == NULL)
659 continue;
David Howellsc5ef6032015-03-17 22:26:16 +0000660 if (d_really_is_negative(dentry))
Trond Myklebustac6fece2009-08-09 15:14:20 -0400661 goto next;
David Howellsc5ef6032015-03-17 22:26:16 +0000662 switch (d_inode(dentry)->i_mode & S_IFMT) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400663 default:
664 BUG();
665 case S_IFREG:
666 __rpc_unlink(dir, dentry);
667 break;
668 case S_IFDIR:
669 __rpc_rmdir(dir, dentry);
670 }
671next:
672 dput(dentry);
673 }
674}
675
676static void rpc_depopulate(struct dentry *parent,
677 const struct rpc_filelist *files,
678 int start, int eof)
679{
David Howellsc5ef6032015-03-17 22:26:16 +0000680 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Al Viro59551022016-01-22 15:40:57 -0500682 inode_lock_nested(dir, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400683 __rpc_depopulate(parent, files, start, eof);
Al Viro59551022016-01-22 15:40:57 -0500684 inode_unlock(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Trond Myklebustac6fece2009-08-09 15:14:20 -0400687static int rpc_populate(struct dentry *parent,
688 const struct rpc_filelist *files,
689 int start, int eof,
690 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
David Howellsc5ef6032015-03-17 22:26:16 +0000692 struct inode *dir = d_inode(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400694 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Al Viro59551022016-01-22 15:40:57 -0500696 inode_lock(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 for (i = start; i < eof; i++) {
Al Virod3db90b2013-07-14 17:09:57 +0400698 dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400699 err = PTR_ERR(dentry);
700 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400702 switch (files[i].mode & S_IFMT) {
703 default:
704 BUG();
705 case S_IFREG:
706 err = __rpc_create(dir, dentry,
707 files[i].mode,
708 files[i].i_fop,
709 private);
710 break;
711 case S_IFDIR:
712 err = __rpc_mkdir(dir, dentry,
713 files[i].mode,
714 NULL,
715 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400717 if (err != 0)
718 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Al Viro59551022016-01-22 15:40:57 -0500720 inode_unlock(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400723 __rpc_depopulate(parent, files, start, eof);
Al Viro59551022016-01-22 15:40:57 -0500724 inode_unlock(dir);
Al Viro1e903ed2013-09-16 10:57:41 -0400725 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
726 __FILE__, __func__, parent);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400727 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400728}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100729
Trond Myklebuste57aed72009-08-09 15:14:26 -0400730static struct dentry *rpc_mkdir_populate(struct dentry *parent,
Al Viroa95e6912013-07-14 16:43:54 +0400731 const char *name, umode_t mode, void *private,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400732 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400733{
Trond Myklebustf1345852005-09-23 11:08:25 -0400734 struct dentry *dentry;
David Howellsc5ef6032015-03-17 22:26:16 +0000735 struct inode *dir = d_inode(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400736 int error;
737
Al Viro59551022016-01-22 15:40:57 -0500738 inode_lock_nested(dir, I_MUTEX_PARENT);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400739 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400740 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400741 goto out;
742 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400743 if (error != 0)
744 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400745 if (populate != NULL) {
746 error = populate(dentry, args_populate);
747 if (error)
748 goto err_rmdir;
749 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400750out:
Al Viro59551022016-01-22 15:40:57 -0500751 inode_unlock(dir);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400752 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400753err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400754 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400755out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400756 dentry = ERR_PTR(error);
757 goto out;
758}
759
Trond Myklebuste57aed72009-08-09 15:14:26 -0400760static int rpc_rmdir_depopulate(struct dentry *dentry,
761 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400762{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700763 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400764 struct inode *dir;
765 int error;
766
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700767 parent = dget_parent(dentry);
David Howellsc5ef6032015-03-17 22:26:16 +0000768 dir = d_inode(parent);
Al Viro59551022016-01-22 15:40:57 -0500769 inode_lock_nested(dir, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400770 if (depopulate != NULL)
771 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400772 error = __rpc_rmdir(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -0500773 inode_unlock(dir);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700774 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400775 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500778/**
779 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
780 * @parent: dentry of directory to create new "pipe" in
781 * @name: name of pipe
782 * @private: private data to associate with the pipe, for the caller's use
Randy Dunlapbda14602012-05-13 10:35:40 -0700783 * @pipe: &rpc_pipe containing input parameters
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500784 *
785 * Data is made available for userspace to read by calls to
786 * rpc_queue_upcall(). The actual reads will result in calls to
787 * @ops->upcall, which will be called with the file pointer,
788 * message, and userspace buffer to copy to.
789 *
790 * Writes can come at any time, and do not necessarily have to be
791 * responses to upcalls. They will result in calls to @msg->downcall.
792 *
793 * The @private argument passed here will be available to all these methods
Al Viro496ad9a2013-01-23 17:07:38 -0500794 * from the file pointer, via RPC_I(file_inode(file))->private.
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500795 */
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300796struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
797 void *private, struct rpc_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct dentry *dentry;
David Howellsc5ef6032015-03-17 22:26:16 +0000800 struct inode *dir = d_inode(parent);
Joe Perchesd6444062018-03-23 15:54:38 -0700801 umode_t umode = S_IFIFO | 0600;
Trond Myklebust75898062009-08-09 15:14:17 -0400802 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400803
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300804 if (pipe->ops->upcall == NULL)
Joe Perchesd6444062018-03-23 15:54:38 -0700805 umode &= ~0444;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300806 if (pipe->ops->downcall == NULL)
Joe Perchesd6444062018-03-23 15:54:38 -0700807 umode &= ~0222;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Al Viro59551022016-01-22 15:40:57 -0500809 inode_lock_nested(dir, I_MUTEX_PARENT);
Al Virod3db90b2013-07-14 17:09:57 +0400810 dentry = __rpc_lookup_create_exclusive(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400812 goto out;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300813 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
814 private, pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400815 if (err)
816 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400817out:
Al Viro59551022016-01-22 15:40:57 -0500818 inode_unlock(dir);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400819 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400820out_err:
821 dentry = ERR_PTR(err);
Al Viro1e903ed2013-09-16 10:57:41 -0400822 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
823 __FILE__, __func__, parent, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400824 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400825 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300827EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500829/**
830 * rpc_unlink - remove a pipe
831 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
832 *
833 * After this call, lookups will no longer find the pipe, and any
834 * attempts to read or write using preexisting opens of the pipe will
835 * return -EPIPE.
836 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400837int
Trond Myklebust5d674762006-07-31 14:11:48 -0700838rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Trond Myklebust5d674762006-07-31 14:11:48 -0700840 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400841 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700842 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Trond Myklebust5d674762006-07-31 14:11:48 -0700844 parent = dget_parent(dentry);
David Howellsc5ef6032015-03-17 22:26:16 +0000845 dir = d_inode(parent);
Al Viro59551022016-01-22 15:40:57 -0500846 inode_lock_nested(dir, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400847 error = __rpc_rmpipe(dir, dentry);
Al Viro59551022016-01-22 15:40:57 -0500848 inode_unlock(dir);
Trond Myklebust5d674762006-07-31 14:11:48 -0700849 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400850 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
Trond Myklebust468039e2008-12-23 15:21:31 -0500852EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400854/**
855 * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
856 * @pdh: pointer to struct rpc_pipe_dir_head
857 */
858void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
859{
860 INIT_LIST_HEAD(&pdh->pdh_entries);
861 pdh->pdh_dentry = NULL;
862}
863EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
864
865/**
866 * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
867 * @pdo: pointer to struct rpc_pipe_dir_object
868 * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
869 * @pdo_data: pointer to caller-defined data
870 */
871void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
872 const struct rpc_pipe_dir_object_ops *pdo_ops,
873 void *pdo_data)
874{
875 INIT_LIST_HEAD(&pdo->pdo_head);
876 pdo->pdo_ops = pdo_ops;
877 pdo->pdo_data = pdo_data;
878}
879EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
880
881static int
882rpc_add_pipe_dir_object_locked(struct net *net,
883 struct rpc_pipe_dir_head *pdh,
884 struct rpc_pipe_dir_object *pdo)
885{
886 int ret = 0;
887
888 if (pdh->pdh_dentry)
889 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
890 if (ret == 0)
891 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
892 return ret;
893}
894
895static void
896rpc_remove_pipe_dir_object_locked(struct net *net,
897 struct rpc_pipe_dir_head *pdh,
898 struct rpc_pipe_dir_object *pdo)
899{
900 if (pdh->pdh_dentry)
901 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
902 list_del_init(&pdo->pdo_head);
903}
904
905/**
906 * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
907 * @net: pointer to struct net
908 * @pdh: pointer to struct rpc_pipe_dir_head
909 * @pdo: pointer to struct rpc_pipe_dir_object
910 *
911 */
912int
913rpc_add_pipe_dir_object(struct net *net,
914 struct rpc_pipe_dir_head *pdh,
915 struct rpc_pipe_dir_object *pdo)
916{
917 int ret = 0;
918
919 if (list_empty(&pdo->pdo_head)) {
920 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
921
922 mutex_lock(&sn->pipefs_sb_lock);
923 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
924 mutex_unlock(&sn->pipefs_sb_lock);
925 }
926 return ret;
927}
928EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
929
930/**
931 * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
932 * @net: pointer to struct net
933 * @pdh: pointer to struct rpc_pipe_dir_head
934 * @pdo: pointer to struct rpc_pipe_dir_object
935 *
936 */
937void
938rpc_remove_pipe_dir_object(struct net *net,
939 struct rpc_pipe_dir_head *pdh,
940 struct rpc_pipe_dir_object *pdo)
941{
942 if (!list_empty(&pdo->pdo_head)) {
943 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
944
945 mutex_lock(&sn->pipefs_sb_lock);
946 rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
947 mutex_unlock(&sn->pipefs_sb_lock);
948 }
949}
950EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
951
Trond Myklebust298fc352013-08-27 16:27:04 -0400952/**
953 * rpc_find_or_alloc_pipe_dir_object
954 * @net: pointer to struct net
955 * @pdh: pointer to struct rpc_pipe_dir_head
956 * @match: match struct rpc_pipe_dir_object to data
957 * @alloc: allocate a new struct rpc_pipe_dir_object
958 * @data: user defined data for match() and alloc()
959 *
960 */
961struct rpc_pipe_dir_object *
962rpc_find_or_alloc_pipe_dir_object(struct net *net,
963 struct rpc_pipe_dir_head *pdh,
964 int (*match)(struct rpc_pipe_dir_object *, void *),
965 struct rpc_pipe_dir_object *(*alloc)(void *),
966 void *data)
967{
968 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
969 struct rpc_pipe_dir_object *pdo;
970
971 mutex_lock(&sn->pipefs_sb_lock);
972 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
973 if (!match(pdo, data))
974 continue;
975 goto out;
976 }
977 pdo = alloc(data);
978 if (!pdo)
979 goto out;
980 rpc_add_pipe_dir_object_locked(net, pdh, pdo);
981out:
982 mutex_unlock(&sn->pipefs_sb_lock);
983 return pdo;
984}
985EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
986
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400987static void
988rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
989{
990 struct rpc_pipe_dir_object *pdo;
991 struct dentry *dir = pdh->pdh_dentry;
992
993 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
994 pdo->pdo_ops->create(dir, pdo);
995}
996
997static void
998rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
999{
1000 struct rpc_pipe_dir_object *pdo;
1001 struct dentry *dir = pdh->pdh_dentry;
1002
1003 list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1004 pdo->pdo_ops->destroy(dir, pdo);
1005}
1006
Trond Myklebuste57aed72009-08-09 15:14:26 -04001007enum {
1008 RPCAUTH_info,
1009 RPCAUTH_EOF
1010};
1011
1012static const struct rpc_filelist authfiles[] = {
1013 [RPCAUTH_info] = {
1014 .name = "info",
1015 .i_fop = &rpc_info_operations,
Joe Perchesd6444062018-03-23 15:54:38 -07001016 .mode = S_IFREG | 0400,
Trond Myklebuste57aed72009-08-09 15:14:26 -04001017 },
1018};
1019
1020static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1021{
1022 return rpc_populate(dentry,
1023 authfiles, RPCAUTH_info, RPCAUTH_EOF,
1024 private);
1025}
1026
1027static void rpc_clntdir_depopulate(struct dentry *dentry)
1028{
1029 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1030}
1031
Trond Myklebust7d59d1e2009-08-09 15:14:23 -04001032/**
1033 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Al Viroa95e6912013-07-14 16:43:54 +04001034 * @dentry: the parent of new directory
1035 * @name: the name of new directory
Trond Myklebust7d59d1e2009-08-09 15:14:23 -04001036 * @rpc_client: rpc client to associate with this directory
1037 *
1038 * This creates a directory at the given @path associated with
1039 * @rpc_clnt, which will contain a file named "info" with some basic
1040 * information about the client, together with any "pipes" that may
1041 * later be created using rpc_mkpipe().
1042 */
Trond Myklebust23ac6582009-08-09 15:14:25 -04001043struct dentry *rpc_create_client_dir(struct dentry *dentry,
Al Viroa95e6912013-07-14 16:43:54 +04001044 const char *name,
Trond Myklebuste57aed72009-08-09 15:14:26 -04001045 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -04001046{
Trond Myklebust6739ffb2013-08-26 15:38:11 -04001047 struct dentry *ret;
1048
Joe Perchesd6444062018-03-23 15:54:38 -07001049 ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1050 rpc_clntdir_populate, rpc_client);
Trond Myklebust6739ffb2013-08-26 15:38:11 -04001051 if (!IS_ERR(ret)) {
1052 rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1053 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1054 }
1055 return ret;
Trond Myklebuste57aed72009-08-09 15:14:26 -04001056}
1057
1058/**
1059 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
Trond Myklebust6739ffb2013-08-26 15:38:11 -04001060 * @rpc_client: rpc_client for the pipe
Trond Myklebuste57aed72009-08-09 15:14:26 -04001061 */
Trond Myklebustc36dcfe12013-08-26 17:44:26 -04001062int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
Trond Myklebuste57aed72009-08-09 15:14:26 -04001063{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -04001064 struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1065
1066 if (dentry == NULL)
1067 return 0;
1068 rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1069 rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
Trond Myklebuste57aed72009-08-09 15:14:26 -04001070 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -04001071}
1072
Trond Myklebust8854e822009-08-09 15:14:30 -04001073static const struct rpc_filelist cache_pipefs_files[3] = {
1074 [0] = {
1075 .name = "channel",
1076 .i_fop = &cache_file_operations_pipefs,
Joe Perchesd6444062018-03-23 15:54:38 -07001077 .mode = S_IFREG | 0600,
Trond Myklebust8854e822009-08-09 15:14:30 -04001078 },
1079 [1] = {
1080 .name = "content",
1081 .i_fop = &content_file_operations_pipefs,
Joe Perchesd6444062018-03-23 15:54:38 -07001082 .mode = S_IFREG | 0400,
Trond Myklebust8854e822009-08-09 15:14:30 -04001083 },
1084 [2] = {
1085 .name = "flush",
1086 .i_fop = &cache_flush_operations_pipefs,
Joe Perchesd6444062018-03-23 15:54:38 -07001087 .mode = S_IFREG | 0600,
Trond Myklebust8854e822009-08-09 15:14:30 -04001088 },
1089};
1090
1091static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1092{
1093 return rpc_populate(dentry,
1094 cache_pipefs_files, 0, 3,
1095 private);
1096}
1097
1098static void rpc_cachedir_depopulate(struct dentry *dentry)
1099{
1100 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1101}
1102
Al Viroa95e6912013-07-14 16:43:54 +04001103struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
Al Viro64f14262011-07-25 00:35:13 -04001104 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -04001105{
1106 return rpc_mkdir_populate(parent, name, umode, NULL,
1107 rpc_cachedir_populate, cd);
1108}
1109
1110void rpc_remove_cache_dir(struct dentry *dentry)
1111{
1112 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1113}
1114
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115/*
1116 * populate the filesystem
1117 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -07001118static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 .alloc_inode = rpc_alloc_inode,
Al Virobef252f2019-04-15 22:32:27 -04001120 .free_inode = rpc_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 .statfs = simple_statfs,
1122};
1123
1124#define RPCAUTH_GSSMAGIC 0x67596969
1125
Trond Myklebustbb156742009-08-09 15:14:21 -04001126/*
1127 * We have a single directory with 1 node in it.
1128 */
1129enum {
1130 RPCAUTH_lockd,
1131 RPCAUTH_mount,
1132 RPCAUTH_nfs,
1133 RPCAUTH_portmap,
1134 RPCAUTH_statd,
1135 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001136 RPCAUTH_cache,
Jeff Laytonb3537c32012-03-21 09:52:04 -04001137 RPCAUTH_nfsd,
Jeff Layton4b9a4452013-11-14 07:25:17 -05001138 RPCAUTH_gssd,
Trond Myklebustbb156742009-08-09 15:14:21 -04001139 RPCAUTH_RootEOF
1140};
1141
1142static const struct rpc_filelist files[] = {
1143 [RPCAUTH_lockd] = {
1144 .name = "lockd",
Joe Perchesd6444062018-03-23 15:54:38 -07001145 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001146 },
1147 [RPCAUTH_mount] = {
1148 .name = "mount",
Joe Perchesd6444062018-03-23 15:54:38 -07001149 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001150 },
1151 [RPCAUTH_nfs] = {
1152 .name = "nfs",
Joe Perchesd6444062018-03-23 15:54:38 -07001153 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001154 },
1155 [RPCAUTH_portmap] = {
1156 .name = "portmap",
Joe Perchesd6444062018-03-23 15:54:38 -07001157 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001158 },
1159 [RPCAUTH_statd] = {
1160 .name = "statd",
Joe Perchesd6444062018-03-23 15:54:38 -07001161 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001162 },
1163 [RPCAUTH_nfsd4_cb] = {
1164 .name = "nfsd4_cb",
Joe Perchesd6444062018-03-23 15:54:38 -07001165 .mode = S_IFDIR | 0555,
Trond Myklebustbb156742009-08-09 15:14:21 -04001166 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001167 [RPCAUTH_cache] = {
1168 .name = "cache",
Joe Perchesd6444062018-03-23 15:54:38 -07001169 .mode = S_IFDIR | 0555,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001170 },
Jeff Laytonb3537c32012-03-21 09:52:04 -04001171 [RPCAUTH_nfsd] = {
1172 .name = "nfsd",
Joe Perchesd6444062018-03-23 15:54:38 -07001173 .mode = S_IFDIR | 0555,
Jeff Laytonb3537c32012-03-21 09:52:04 -04001174 },
Jeff Layton4b9a4452013-11-14 07:25:17 -05001175 [RPCAUTH_gssd] = {
1176 .name = "gssd",
Joe Perchesd6444062018-03-23 15:54:38 -07001177 .mode = S_IFDIR | 0555,
Jeff Layton4b9a4452013-11-14 07:25:17 -05001178 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001179};
1180
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001181/*
1182 * This call can be used only in RPC pipefs mount notification hooks.
1183 */
1184struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1185 const unsigned char *dir_name)
1186{
Linus Torvalds26fe5752012-05-10 13:14:12 -07001187 struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
Al Virod3db90b2013-07-14 17:09:57 +04001188 return d_hash_and_lookup(sb->s_root, &dir);
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001189}
1190EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1191
Jeff Layton4b9a4452013-11-14 07:25:17 -05001192int rpc_pipefs_init_net(struct net *net)
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001193{
1194 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1195
Jeff Layton4b9a4452013-11-14 07:25:17 -05001196 sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1197 if (IS_ERR(sn->gssd_dummy))
1198 return PTR_ERR(sn->gssd_dummy);
1199
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001200 mutex_init(&sn->pipefs_sb_lock);
Trond Myklebust2aed8b42013-05-15 10:27:32 -07001201 sn->pipe_version = -1;
Jeff Layton4b9a4452013-11-14 07:25:17 -05001202 return 0;
1203}
1204
1205void rpc_pipefs_exit_net(struct net *net)
1206{
1207 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1208
1209 rpc_destroy_pipe_data(sn->gssd_dummy);
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001210}
1211
1212/*
1213 * This call will be used for per network namespace operations calls.
1214 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1215 * found. This lock have to be released by rpc_put_sb_net() when all operations
1216 * will be completed.
1217 */
1218struct super_block *rpc_get_sb_net(const struct net *net)
1219{
1220 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1221
1222 mutex_lock(&sn->pipefs_sb_lock);
1223 if (sn->pipefs_sb)
1224 return sn->pipefs_sb;
1225 mutex_unlock(&sn->pipefs_sb_lock);
1226 return NULL;
1227}
1228EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1229
1230void rpc_put_sb_net(const struct net *net)
1231{
1232 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1233
Weston Andros Adamson749386e2012-10-23 10:43:37 -04001234 WARN_ON(sn->pipefs_sb == NULL);
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001235 mutex_unlock(&sn->pipefs_sb_lock);
1236}
1237EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1238
Jeff Layton4b9a4452013-11-14 07:25:17 -05001239static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1240 [0] = {
1241 .name = "clntXX",
Joe Perchesd6444062018-03-23 15:54:38 -07001242 .mode = S_IFDIR | 0555,
Jeff Layton4b9a4452013-11-14 07:25:17 -05001243 },
1244};
1245
1246static ssize_t
1247dummy_downcall(struct file *filp, const char __user *src, size_t len)
1248{
1249 return -EINVAL;
1250}
1251
1252static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1253 .upcall = rpc_pipe_generic_upcall,
1254 .downcall = dummy_downcall,
1255};
1256
Jeff Laytone2f0c832013-12-05 07:34:44 -05001257/*
1258 * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1259 * that it will ever use this info to handle an upcall, but rpc.gssd expects
1260 * that this file will be there and have a certain format.
1261 */
1262static int
Yangtao Li260f71e2018-12-21 10:59:36 -05001263rpc_dummy_info_show(struct seq_file *m, void *v)
Jeff Laytone2f0c832013-12-05 07:34:44 -05001264{
1265 seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1266 seq_printf(m, "service: foo (1) version 0\n");
1267 seq_printf(m, "address: 127.0.0.1\n");
1268 seq_printf(m, "protocol: tcp\n");
1269 seq_printf(m, "port: 0\n");
1270 return 0;
1271}
Yangtao Li260f71e2018-12-21 10:59:36 -05001272DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
Jeff Laytone2f0c832013-12-05 07:34:44 -05001273
1274static const struct rpc_filelist gssd_dummy_info_file[] = {
1275 [0] = {
1276 .name = "info",
Yangtao Li260f71e2018-12-21 10:59:36 -05001277 .i_fop = &rpc_dummy_info_fops,
Joe Perchesd6444062018-03-23 15:54:38 -07001278 .mode = S_IFREG | 0400,
Jeff Laytone2f0c832013-12-05 07:34:44 -05001279 },
1280};
1281
Jeff Layton4b9a4452013-11-14 07:25:17 -05001282/**
1283 * rpc_gssd_dummy_populate - create a dummy gssd pipe
1284 * @root: root of the rpc_pipefs filesystem
1285 * @pipe_data: pipe data created when netns is initialized
1286 *
1287 * Create a dummy set of directories and a pipe that gssd can hold open to
1288 * indicate that it is up and running.
1289 */
1290static struct dentry *
1291rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1292{
1293 int ret = 0;
1294 struct dentry *gssd_dentry;
1295 struct dentry *clnt_dentry = NULL;
1296 struct dentry *pipe_dentry = NULL;
1297 struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1298 strlen(files[RPCAUTH_gssd].name));
1299
1300 /* We should never get this far if "gssd" doesn't exist */
1301 gssd_dentry = d_hash_and_lookup(root, &q);
1302 if (!gssd_dentry)
1303 return ERR_PTR(-ENOENT);
1304
1305 ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1306 if (ret) {
1307 pipe_dentry = ERR_PTR(ret);
1308 goto out;
1309 }
1310
1311 q.name = gssd_dummy_clnt_dir[0].name;
1312 q.len = strlen(gssd_dummy_clnt_dir[0].name);
1313 clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1314 if (!clnt_dentry) {
1315 pipe_dentry = ERR_PTR(-ENOENT);
1316 goto out;
1317 }
1318
Jeff Laytone2f0c832013-12-05 07:34:44 -05001319 ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1320 if (ret) {
Jeff Layton3396f922013-12-05 07:33:49 -05001321 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
Jeff Laytone2f0c832013-12-05 07:34:44 -05001322 pipe_dentry = ERR_PTR(ret);
1323 goto out;
1324 }
1325
1326 pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
1327 if (IS_ERR(pipe_dentry)) {
1328 __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
1329 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1330 }
Jeff Layton4b9a4452013-11-14 07:25:17 -05001331out:
1332 dput(clnt_dentry);
1333 dput(gssd_dentry);
1334 return pipe_dentry;
1335}
1336
Jeff Layton23e66ba2013-12-09 09:38:00 -05001337static void
1338rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1339{
1340 struct dentry *clnt_dir = pipe_dentry->d_parent;
1341 struct dentry *gssd_dir = clnt_dir->d_parent;
1342
Al Viro4a3877c2018-04-03 01:15:46 -04001343 dget(pipe_dentry);
David Howellsc5ef6032015-03-17 22:26:16 +00001344 __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
Jeff Layton23e66ba2013-12-09 09:38:00 -05001345 __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1346 __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1347 dput(pipe_dentry);
1348}
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350static int
1351rpc_fill_super(struct super_block *sb, void *data, int silent)
1352{
1353 struct inode *inode;
Jeff Layton4b9a4452013-11-14 07:25:17 -05001354 struct dentry *root, *gssd_dentry;
Eric W. Biedermand91ee872016-05-23 14:51:59 -05001355 struct net *net = get_net(sb->s_fs_info);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001356 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001357 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001359 sb->s_blocksize = PAGE_SIZE;
1360 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 sb->s_magic = RPCAUTH_GSSMAGIC;
1362 sb->s_op = &s_ops;
Al Virob26d4cd2013-10-25 18:47:37 -04001363 sb->s_d_op = &simple_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 sb->s_time_gran = 1;
1365
Joe Perchesd6444062018-03-23 15:54:38 -07001366 inode = rpc_get_inode(sb, S_IFDIR | 0555);
Al Viro48fde702012-01-08 22:15:13 -05001367 sb->s_root = root = d_make_root(inode);
1368 if (!root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return -ENOMEM;
Trond Myklebustac6fece2009-08-09 15:14:20 -04001370 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001371 return -ENOMEM;
Jeff Layton4b9a4452013-11-14 07:25:17 -05001372
1373 gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1374 if (IS_ERR(gssd_dentry)) {
1375 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1376 return PTR_ERR(gssd_dentry);
1377 }
1378
Vasily Averin6c67a3e4a2017-11-08 08:57:32 +03001379 dprintk("RPC: sending pipefs MOUNT notification for net %x%s\n",
1380 net->ns.inum, NET_NAME(net));
Stanislav Kinsbursky38481602013-06-24 11:52:38 +04001381 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky37629b52012-04-20 18:19:56 +04001382 sn->pipefs_sb = sb;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001383 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1384 RPC_PIPEFS_MOUNT,
1385 sb);
1386 if (err)
1387 goto err_depopulate;
Stanislav Kinsbursky38481602013-06-24 11:52:38 +04001388 mutex_unlock(&sn->pipefs_sb_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001390
1391err_depopulate:
Jeff Layton23e66ba2013-12-09 09:38:00 -05001392 rpc_gssd_dummy_depopulate(gssd_dentry);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001393 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1394 RPC_PIPEFS_UMOUNT,
1395 sb);
Stanislav Kinsbursky37629b52012-04-20 18:19:56 +04001396 sn->pipefs_sb = NULL;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001397 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
Stanislav Kinsbursky38481602013-06-24 11:52:38 +04001398 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Jeff Layton89f84242013-11-14 07:25:18 -05001402bool
1403gssd_running(struct net *net)
1404{
1405 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1406 struct rpc_pipe *pipe = sn->gssd_dummy;
1407
1408 return pipe->nreaders || pipe->nwriters;
1409}
1410EXPORT_SYMBOL_GPL(gssd_running);
1411
Al Virofc14f2f2010-07-25 01:48:30 +04001412static struct dentry *
1413rpc_mount(struct file_system_type *fs_type,
1414 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
Eric W. Biedermand91ee872016-05-23 14:51:59 -05001416 struct net *net = current->nsproxy->net_ns;
1417 return mount_ns(fs_type, flags, data, net, net->user_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Trond Myklebust09acfea2012-03-11 15:22:54 -04001420static void rpc_kill_sb(struct super_block *sb)
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001421{
1422 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001423 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001424
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001425 mutex_lock(&sn->pipefs_sb_lock);
Trond Myklebust642fe4d2012-11-08 10:01:26 -05001426 if (sn->pipefs_sb != sb) {
1427 mutex_unlock(&sn->pipefs_sb_lock);
1428 goto out;
1429 }
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001430 sn->pipefs_sb = NULL;
Vasily Averin6c67a3e4a2017-11-08 08:57:32 +03001431 dprintk("RPC: sending pipefs UMOUNT notification for net %x%s\n",
1432 net->ns.inum, NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001433 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1434 RPC_PIPEFS_UMOUNT,
1435 sb);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +04001436 mutex_unlock(&sn->pipefs_sb_lock);
Trond Myklebust642fe4d2012-11-08 10:01:26 -05001437out:
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001438 kill_litter_super(sb);
Eric W. Biedermand91ee872016-05-23 14:51:59 -05001439 put_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442static struct file_system_type rpc_pipe_fs_type = {
1443 .owner = THIS_MODULE,
1444 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001445 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001446 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08001448MODULE_ALIAS_FS("rpc_pipefs");
Eric W. Biedermanfa7614dd2013-03-12 18:27:41 -07001449MODULE_ALIAS("rpc_pipefs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001452init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
1454 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1455
Christoph Lametera35afb82007-05-16 22:10:57 -07001456 inode_init_once(&rpci->vfs_inode);
1457 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001458 rpci->pipe = NULL;
Christoph Lametera35afb82007-05-16 22:10:57 -07001459 init_waitqueue_head(&rpci->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
1462int register_rpc_pipefs(void)
1463{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001464 int err;
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001467 sizeof(struct rpc_inode),
1468 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -08001469 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
Paul Mundt20c2df82007-07-20 10:11:58 +09001470 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (!rpc_inode_cachep)
1472 return -ENOMEM;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001473 err = rpc_clients_notifier_register();
1474 if (err)
1475 goto err_notifier;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001476 err = register_filesystem(&rpc_pipe_fs_type);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001477 if (err)
1478 goto err_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001480
1481err_register:
1482 rpc_clients_notifier_unregister();
1483err_notifier:
1484 kmem_cache_destroy(rpc_inode_cachep);
1485 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
1488void unregister_rpc_pipefs(void)
1489{
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001490 rpc_clients_notifier_unregister();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001491 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 unregister_filesystem(&rpc_pipe_fs_type);
1493}