blob: b67b2aecc4ff6fb29be24388cadda9a31d615ec2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * net/sunrpc/rpc_pipe.c
3 *
4 * Userland/kernel interface for rpcauth_gss.
5 * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
Rolf Eike Beerd51fe1b2005-09-02 08:59:25 +02006 * and fs/sysfs/inode.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
9 *
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/string.h>
14#include <linux/pagemap.h>
15#include <linux/mount.h>
16#include <linux/namei.h>
Trond Myklebust50e437d2007-06-07 22:44:34 -040017#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19
20#include <asm/ioctls.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/poll.h>
22#include <linux/wait.h>
23#include <linux/seq_file.h>
24
25#include <linux/sunrpc/clnt.h>
26#include <linux/workqueue.h>
27#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040028#include <linux/sunrpc/cache.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030029#include <linux/nsproxy.h>
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030030#include <linux/notifier.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030031
32#include "netns.h"
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030033#include "sunrpc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +030035#define RPCDBG_FACILITY RPCDBG_DEBUG
36
37#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static struct file_system_type rpc_pipe_fs_type;
40
41
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define RPC_UPCALL_TIMEOUT (30*HZ)
45
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030046static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
47
48int rpc_pipefs_notifier_register(struct notifier_block *nb)
49{
50 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
51}
52EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
53
54void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
55{
56 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
57}
58EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
59
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030060static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
Trond Myklebust9842ef32006-02-01 12:18:44 -050061 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050062{
63 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050064
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 if (list_empty(head))
66 return;
67 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050068 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040069 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050070 msg->errno = err;
71 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050072 } while (!list_empty(head));
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030073 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static void
David Howells65f27f32006-11-22 14:55:48 +000077rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Trond Myklebust9842ef32006-02-01 12:18:44 -050079 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030080 struct rpc_pipe *pipe =
81 container_of(work, struct rpc_pipe, queue_timeout.work);
Trond Myklebust9842ef32006-02-01 12:18:44 -050082 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030084 spin_lock(&pipe->lock);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030085 destroy_msg = pipe->ops->destroy_msg;
86 if (pipe->nreaders == 0) {
87 list_splice_init(&pipe->pipe, &free_list);
88 pipe->pipelen = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -050089 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030090 spin_unlock(&pipe->lock);
91 rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
Peng Taoc1225152011-09-22 21:50:10 -040094ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
95 char __user *dst, size_t buflen)
96{
97 char *data = (char *)msg->data + msg->copied;
98 size_t mlen = min(msg->len - msg->copied, buflen);
99 unsigned long left;
100
101 left = copy_to_user(dst, data, mlen);
102 if (left == mlen) {
103 msg->errno = -EFAULT;
104 return -EFAULT;
105 }
106
107 mlen -= left;
108 msg->copied += mlen;
109 msg->errno = 0;
110 return mlen;
111}
112EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
113
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500114/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800115 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500116 * @inode: inode of upcall pipe on which to queue given message
117 * @msg: message to queue
118 *
119 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
120 * A userspace process may then later read the upcall by performing a
121 * read on an open file for this inode. It is up to the caller to
122 * initialize the fields of @msg (other than @msg->list) appropriately.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124int
Stanislav Kinsburskyd706ed12011-12-26 15:43:49 +0300125rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Trond Myklebust6070fe62005-10-27 22:12:46 -0400127 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300129 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300130 if (pipe->nreaders) {
131 list_add_tail(&msg->list, &pipe->pipe);
132 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400133 res = 0;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300134 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
135 if (list_empty(&pipe->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500136 queue_delayed_work(rpciod_workqueue,
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300137 &pipe->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 RPC_UPCALL_TIMEOUT);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300139 list_add_tail(&msg->list, &pipe->pipe);
140 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400141 res = 0;
142 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300143 spin_unlock(&pipe->lock);
144 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return res;
146}
Trond Myklebust468039e2008-12-23 15:21:31 -0500147EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Trond Myklebust6070fe62005-10-27 22:12:46 -0400149static inline void
150rpc_inode_setowner(struct inode *inode, void *private)
151{
152 RPC_I(inode)->private = private;
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void
156rpc_close_pipes(struct inode *inode)
157{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300158 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500159 int need_release;
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400160 LIST_HEAD(free_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800162 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400163 spin_lock(&pipe->lock);
164 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
165 pipe->nreaders = 0;
166 list_splice_init(&pipe->in_upcall, &free_list);
167 list_splice_init(&pipe->pipe, &free_list);
168 pipe->pipelen = 0;
169 pipe->dentry = NULL;
170 spin_unlock(&pipe->lock);
171 rpc_purge_list(pipe, &free_list, pipe->ops->destroy_msg, -EPIPE);
172 pipe->nwriters = 0;
173 if (need_release && pipe->ops->release_pipe)
174 pipe->ops->release_pipe(inode);
175 cancel_delayed_work_sync(&pipe->queue_timeout);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400176 rpc_inode_setowner(inode, NULL);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400177 RPC_I(inode)->pipe = NULL;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800178 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static struct inode *
182rpc_alloc_inode(struct super_block *sb)
183{
184 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800185 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!rpci)
187 return NULL;
188 return &rpci->vfs_inode;
189}
190
191static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100192rpc_i_callback(struct rcu_head *head)
193{
194 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100195 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
196}
197
198static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199rpc_destroy_inode(struct inode *inode)
200{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100201 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204static int
205rpc_pipe_open(struct inode *inode, struct file *filp)
206{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400207 struct rpc_pipe *pipe;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500208 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 int res = -ENXIO;
210
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800211 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400212 pipe = RPC_I(inode)->pipe;
213 if (pipe == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500214 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300215 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
216 if (first_open && pipe->ops->open_pipe) {
217 res = pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500218 if (res)
219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500221 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300222 pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500223 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300224 pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500225 res = 0;
226out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800227 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return res;
229}
230
231static int
232rpc_pipe_release(struct inode *inode, struct file *filp)
233{
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400234 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500236 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800238 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400239 pipe = RPC_I(inode)->pipe;
240 if (pipe == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700242 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300244 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500245 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400246 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300247 spin_unlock(&pipe->lock);
248 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300251 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500252 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300253 pipe->nreaders --;
254 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500255 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300256 spin_lock(&pipe->lock);
257 list_splice_init(&pipe->pipe, &free_list);
258 pipe->pipelen = 0;
259 spin_unlock(&pipe->lock);
260 rpc_purge_list(pipe, &free_list,
261 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500262 }
263 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300264 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
265 if (last_close && pipe->ops->release_pipe)
266 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800268 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270}
271
272static ssize_t
273rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
274{
Josef Sipek303b46b2006-12-08 02:37:42 -0800275 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400276 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct rpc_pipe_msg *msg;
278 int res = 0;
279
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800280 mutex_lock(&inode->i_mutex);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400281 pipe = RPC_I(inode)->pipe;
282 if (pipe == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 res = -EPIPE;
284 goto out_unlock;
285 }
286 msg = filp->private_data;
287 if (msg == NULL) {
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300288 spin_lock(&pipe->lock);
289 if (!list_empty(&pipe->pipe)) {
290 msg = list_entry(pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct rpc_pipe_msg,
292 list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300293 list_move(&msg->list, &pipe->in_upcall);
294 pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 filp->private_data = msg;
296 msg->copied = 0;
297 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300298 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (msg == NULL)
300 goto out_unlock;
301 }
302 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300303 res = pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 if (res < 0 || msg->len == msg->copied) {
305 filp->private_data = NULL;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300306 spin_lock(&pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400307 list_del_init(&msg->list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300308 spin_unlock(&pipe->lock);
309 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800312 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return res;
314}
315
316static ssize_t
317rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
318{
Josef Sipek303b46b2006-12-08 02:37:42 -0800319 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int res;
321
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800322 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 res = -EPIPE;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400324 if (RPC_I(inode)->pipe != NULL)
325 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800326 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return res;
328}
329
330static unsigned int
331rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
332{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300333 struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 unsigned int mask = 0;
335
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300336 poll_wait(filp, &pipe->waitq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 mask = POLLOUT | POLLWRNORM;
Stanislav Kinsburskyad6b1342012-01-10 16:12:38 +0400339 if (pipe->dentry == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 mask |= POLLERR | POLLHUP;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300341 if (filp->private_data || !list_empty(&pipe->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 mask |= POLLIN | POLLRDNORM;
343 return mask;
344}
345
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200346static long
347rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200349 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400350 struct rpc_pipe *pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 int len;
352
353 switch (cmd) {
354 case FIONREAD:
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400355 mutex_lock(&inode->i_mutex);
356 pipe = RPC_I(inode)->pipe;
357 if (pipe == NULL) {
358 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200360 }
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400361 spin_lock(&pipe->lock);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300362 len = pipe->pipelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (filp->private_data) {
364 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700365 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 len += msg->len - msg->copied;
367 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300368 spin_unlock(&pipe->lock);
Stanislav Kinsbursky2c9030e2012-02-27 22:05:45 +0400369 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return put_user(len, (int __user *)arg);
371 default:
372 return -EINVAL;
373 }
374}
375
Arjan van de Venda7071d2007-02-12 00:55:36 -0800376static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .read = rpc_pipe_read,
380 .write = rpc_pipe_write,
381 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200382 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 .open = rpc_pipe_open,
384 .release = rpc_pipe_release,
385};
386
387static int
388rpc_show_info(struct seq_file *m, void *v)
389{
390 struct rpc_clnt *clnt = m->private;
391
392 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
393 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
394 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400395 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
396 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400397 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 0;
399}
400
401static int
402rpc_info_open(struct inode *inode, struct file *file)
403{
Trond Myklebust006abe82010-09-12 19:55:25 -0400404 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 int ret = single_open(file, rpc_show_info, NULL);
406
407 if (!ret) {
408 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400409
410 spin_lock(&file->f_path.dentry->d_lock);
411 if (!d_unhashed(file->f_path.dentry))
412 clnt = RPC_I(inode)->private;
413 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
414 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 m->private = clnt;
416 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400417 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 single_release(inode, file);
419 ret = -EINVAL;
420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422 return ret;
423}
424
425static int
426rpc_info_release(struct inode *inode, struct file *file)
427{
428 struct seq_file *m = file->private_data;
429 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
430
431 if (clnt)
432 rpc_release_client(clnt);
433 return single_release(inode, file);
434}
435
Arjan van de Venda7071d2007-02-12 00:55:36 -0800436static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .owner = THIS_MODULE,
438 .open = rpc_info_open,
439 .read = seq_read,
440 .llseek = seq_lseek,
441 .release = rpc_info_release,
442};
443
444
445/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * Description of fs contents.
447 */
448struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400449 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800450 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400451 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452};
453
Nick Pigginfe15ce42011-01-07 17:49:23 +1100454static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400455{
456 return 1;
457}
458
Al Viro3ba13d12009-02-20 06:02:22 +0000459static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400460 .d_delete = rpc_delete_dentry,
461};
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400464rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct inode *inode = new_inode(sb);
467 if (!inode)
468 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400469 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000472 switch (mode & S_IFMT) {
473 case S_IFDIR:
474 inode->i_fop = &simple_dir_operations;
475 inode->i_op = &simple_dir_inode_operations;
476 inc_nlink(inode);
477 default:
478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
480 return inode;
481}
482
Trond Myklebust75898062009-08-09 15:14:17 -0400483static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
484 umode_t mode,
485 const struct file_operations *i_fop,
486 void *private)
487{
488 struct inode *inode;
489
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000490 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400491 inode = rpc_get_inode(dir->i_sb, mode);
492 if (!inode)
493 goto out_err;
494 inode->i_ino = iunique(dir->i_sb, 100);
495 if (i_fop)
496 inode->i_fop = i_fop;
497 if (private)
498 rpc_inode_setowner(inode, private);
499 d_add(dentry, inode);
500 return 0;
501out_err:
502 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
503 __FILE__, __func__, dentry->d_name.name);
504 dput(dentry);
505 return -ENOMEM;
506}
507
Trond Myklebustac6fece2009-08-09 15:14:20 -0400508static int __rpc_create(struct inode *dir, struct dentry *dentry,
509 umode_t mode,
510 const struct file_operations *i_fop,
511 void *private)
512{
513 int err;
514
515 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
516 if (err)
517 return err;
518 fsnotify_create(dir, dentry);
519 return 0;
520}
521
Trond Myklebust75898062009-08-09 15:14:17 -0400522static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
523 umode_t mode,
524 const struct file_operations *i_fop,
525 void *private)
526{
527 int err;
528
529 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
530 if (err)
531 return err;
532 inc_nlink(dir);
533 fsnotify_mkdir(dir, dentry);
534 return 0;
535}
536
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300537static void
538init_pipe(struct rpc_pipe *pipe)
539{
540 pipe->nreaders = 0;
541 pipe->nwriters = 0;
542 INIT_LIST_HEAD(&pipe->in_upcall);
543 INIT_LIST_HEAD(&pipe->in_downcall);
544 INIT_LIST_HEAD(&pipe->pipe);
545 pipe->pipelen = 0;
546 init_waitqueue_head(&pipe->waitq);
547 INIT_DELAYED_WORK(&pipe->queue_timeout,
548 rpc_timeout_upcall_queue);
549 pipe->ops = NULL;
550 spin_lock_init(&pipe->lock);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300551 pipe->dentry = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300552}
553
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300554void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
555{
556 kfree(pipe);
557}
558EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
559
560struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
Trond Myklebust75898062009-08-09 15:14:17 -0400561{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300562 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400563
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300564 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
565 if (!pipe)
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300566 return ERR_PTR(-ENOMEM);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300567 init_pipe(pipe);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300568 pipe->ops = ops;
569 pipe->flags = flags;
570 return pipe;
571}
572EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
573
574static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
575 umode_t mode,
576 const struct file_operations *i_fop,
577 void *private,
578 struct rpc_pipe *pipe)
579{
580 struct rpc_inode *rpci;
581 int err;
582
Trond Myklebust75898062009-08-09 15:14:17 -0400583 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300584 if (err)
Trond Myklebust75898062009-08-09 15:14:17 -0400585 return err;
586 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400587 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300588 rpci->pipe = pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400589 fsnotify_create(dir, dentry);
590 return 0;
591}
592
Trond Myklebustac6fece2009-08-09 15:14:20 -0400593static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
594{
595 int ret;
596
597 dget(dentry);
598 ret = simple_rmdir(dir, dentry);
599 d_delete(dentry);
600 dput(dentry);
601 return ret;
602}
603
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400604int rpc_rmdir(struct dentry *dentry)
605{
606 struct dentry *parent;
607 struct inode *dir;
608 int error;
609
610 parent = dget_parent(dentry);
611 dir = parent->d_inode;
612 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
613 error = __rpc_rmdir(dir, dentry);
614 mutex_unlock(&dir->i_mutex);
615 dput(parent);
616 return error;
617}
618EXPORT_SYMBOL_GPL(rpc_rmdir);
619
Trond Myklebust810d90b2009-08-09 15:14:18 -0400620static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
621{
622 int ret;
623
624 dget(dentry);
625 ret = simple_unlink(dir, dentry);
626 d_delete(dentry);
627 dput(dentry);
628 return ret;
629}
630
631static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
632{
633 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400634
Trond Myklebust810d90b2009-08-09 15:14:18 -0400635 rpc_close_pipes(inode);
636 return __rpc_unlink(dir, dentry);
637}
638
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400639static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
640 struct qstr *name)
641{
642 struct dentry *dentry;
643
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300644 dentry = d_lookup(parent, name);
645 if (!dentry) {
646 dentry = d_alloc(parent, name);
647 if (!dentry)
648 return ERR_PTR(-ENOMEM);
649 }
650 if (dentry->d_inode == NULL) {
651 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400652 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300653 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400654 dput(dentry);
655 return ERR_PTR(-EEXIST);
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/*
659 * FIXME: This probably has races.
660 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400661static void __rpc_depopulate(struct dentry *parent,
662 const struct rpc_filelist *files,
663 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400666 struct dentry *dentry;
667 struct qstr name;
668 int i;
669
670 for (i = start; i < eof; i++) {
671 name.name = files[i].name;
672 name.len = strlen(files[i].name);
673 name.hash = full_name_hash(name.name, name.len);
674 dentry = d_lookup(parent, &name);
675
676 if (dentry == NULL)
677 continue;
678 if (dentry->d_inode == NULL)
679 goto next;
680 switch (dentry->d_inode->i_mode & S_IFMT) {
681 default:
682 BUG();
683 case S_IFREG:
684 __rpc_unlink(dir, dentry);
685 break;
686 case S_IFDIR:
687 __rpc_rmdir(dir, dentry);
688 }
689next:
690 dput(dentry);
691 }
692}
693
694static void rpc_depopulate(struct dentry *parent,
695 const struct rpc_filelist *files,
696 int start, int eof)
697{
698 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Arjan van de Venc6573c22006-07-03 00:25:16 -0700700 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400701 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800702 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Trond Myklebustac6fece2009-08-09 15:14:20 -0400705static int rpc_populate(struct dentry *parent,
706 const struct rpc_filelist *files,
707 int start, int eof,
708 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400710 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400712 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800714 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400716 struct qstr q;
717
718 q.name = files[i].name;
719 q.len = strlen(files[i].name);
720 q.hash = full_name_hash(q.name, q.len);
721 dentry = __rpc_lookup_create_exclusive(parent, &q);
722 err = PTR_ERR(dentry);
723 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400725 switch (files[i].mode & S_IFMT) {
726 default:
727 BUG();
728 case S_IFREG:
729 err = __rpc_create(dir, dentry,
730 files[i].mode,
731 files[i].i_fop,
732 private);
733 break;
734 case S_IFDIR:
735 err = __rpc_mkdir(dir, dentry,
736 files[i].mode,
737 NULL,
738 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400740 if (err != 0)
741 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800743 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400746 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800747 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800749 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400750 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400751}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100752
Trond Myklebuste57aed72009-08-09 15:14:26 -0400753static struct dentry *rpc_mkdir_populate(struct dentry *parent,
754 struct qstr *name, umode_t mode, void *private,
755 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400756{
Trond Myklebustf1345852005-09-23 11:08:25 -0400757 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400758 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400759 int error;
760
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400761 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
762 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400763 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400764 goto out;
765 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400766 if (error != 0)
767 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400768 if (populate != NULL) {
769 error = populate(dentry, args_populate);
770 if (error)
771 goto err_rmdir;
772 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400773out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800774 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400775 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400776err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400777 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400778out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400779 dentry = ERR_PTR(error);
780 goto out;
781}
782
Trond Myklebuste57aed72009-08-09 15:14:26 -0400783static int rpc_rmdir_depopulate(struct dentry *dentry,
784 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400785{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700786 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400787 struct inode *dir;
788 int error;
789
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700790 parent = dget_parent(dentry);
791 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700792 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400793 if (depopulate != NULL)
794 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400795 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800796 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700797 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400798 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500801/**
802 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
803 * @parent: dentry of directory to create new "pipe" in
804 * @name: name of pipe
805 * @private: private data to associate with the pipe, for the caller's use
806 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500807 * release_pipe, open_pipe, and destroy_msg.
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300808 * @flags: rpc_pipe flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500809 *
810 * Data is made available for userspace to read by calls to
811 * rpc_queue_upcall(). The actual reads will result in calls to
812 * @ops->upcall, which will be called with the file pointer,
813 * message, and userspace buffer to copy to.
814 *
815 * Writes can come at any time, and do not necessarily have to be
816 * responses to upcalls. They will result in calls to @msg->downcall.
817 *
818 * The @private argument passed here will be available to all these methods
819 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
820 */
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300821struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
822 void *private, struct rpc_pipe *pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400825 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400826 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400827 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400828 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400829
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300830 if (pipe->ops->upcall == NULL)
Trond Myklebust7364af62009-08-09 15:14:16 -0400831 umode &= ~S_IRUGO;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300832 if (pipe->ops->downcall == NULL)
Trond Myklebust7364af62009-08-09 15:14:16 -0400833 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400835 q.name = name;
836 q.len = strlen(name);
837 q.hash = full_name_hash(q.name, q.len),
838
839 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300840 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400842 goto out;
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300843 err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
844 private, pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400845 if (err)
846 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400847out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800848 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400849 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400850out_err:
851 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400852 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800853 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400854 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400855 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Stanislav Kinsburskyc239d832011-12-26 15:44:06 +0300857EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500859/**
860 * rpc_unlink - remove a pipe
861 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
862 *
863 * After this call, lookups will no longer find the pipe, and any
864 * attempts to read or write using preexisting opens of the pipe will
865 * return -EPIPE.
866 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400867int
Trond Myklebust5d674762006-07-31 14:11:48 -0700868rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
Trond Myklebust5d674762006-07-31 14:11:48 -0700870 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400871 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700872 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Trond Myklebust5d674762006-07-31 14:11:48 -0700874 parent = dget_parent(dentry);
875 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700876 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400877 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800878 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700879 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400880 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
Trond Myklebust468039e2008-12-23 15:21:31 -0500882EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Trond Myklebuste57aed72009-08-09 15:14:26 -0400884enum {
885 RPCAUTH_info,
886 RPCAUTH_EOF
887};
888
889static const struct rpc_filelist authfiles[] = {
890 [RPCAUTH_info] = {
891 .name = "info",
892 .i_fop = &rpc_info_operations,
893 .mode = S_IFREG | S_IRUSR,
894 },
895};
896
897static int rpc_clntdir_populate(struct dentry *dentry, void *private)
898{
899 return rpc_populate(dentry,
900 authfiles, RPCAUTH_info, RPCAUTH_EOF,
901 private);
902}
903
904static void rpc_clntdir_depopulate(struct dentry *dentry)
905{
906 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
907}
908
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400909/**
910 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400911 * @dentry: dentry from the rpc_pipefs root to the new directory
912 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400913 * @rpc_client: rpc client to associate with this directory
914 *
915 * This creates a directory at the given @path associated with
916 * @rpc_clnt, which will contain a file named "info" with some basic
917 * information about the client, together with any "pipes" that may
918 * later be created using rpc_mkpipe().
919 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400920struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400921 struct qstr *name,
922 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400923{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400924 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
925 rpc_clntdir_populate, rpc_client);
926}
927
928/**
929 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400930 * @clnt: rpc client
Trond Myklebuste57aed72009-08-09 15:14:26 -0400931 */
932int rpc_remove_client_dir(struct dentry *dentry)
933{
934 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400935}
936
Trond Myklebust8854e822009-08-09 15:14:30 -0400937static const struct rpc_filelist cache_pipefs_files[3] = {
938 [0] = {
939 .name = "channel",
940 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400941 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400942 },
943 [1] = {
944 .name = "content",
945 .i_fop = &content_file_operations_pipefs,
946 .mode = S_IFREG|S_IRUSR,
947 },
948 [2] = {
949 .name = "flush",
950 .i_fop = &cache_flush_operations_pipefs,
951 .mode = S_IFREG|S_IRUSR|S_IWUSR,
952 },
953};
954
955static int rpc_cachedir_populate(struct dentry *dentry, void *private)
956{
957 return rpc_populate(dentry,
958 cache_pipefs_files, 0, 3,
959 private);
960}
961
962static void rpc_cachedir_depopulate(struct dentry *dentry)
963{
964 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
965}
966
967struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400968 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400969{
970 return rpc_mkdir_populate(parent, name, umode, NULL,
971 rpc_cachedir_populate, cd);
972}
973
974void rpc_remove_cache_dir(struct dentry *dentry)
975{
976 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
977}
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * populate the filesystem
981 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700982static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 .alloc_inode = rpc_alloc_inode,
984 .destroy_inode = rpc_destroy_inode,
985 .statfs = simple_statfs,
986};
987
988#define RPCAUTH_GSSMAGIC 0x67596969
989
Trond Myklebustbb156742009-08-09 15:14:21 -0400990/*
991 * We have a single directory with 1 node in it.
992 */
993enum {
994 RPCAUTH_lockd,
995 RPCAUTH_mount,
996 RPCAUTH_nfs,
997 RPCAUTH_portmap,
998 RPCAUTH_statd,
999 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001000 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -04001001 RPCAUTH_RootEOF
1002};
1003
1004static const struct rpc_filelist files[] = {
1005 [RPCAUTH_lockd] = {
1006 .name = "lockd",
1007 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1008 },
1009 [RPCAUTH_mount] = {
1010 .name = "mount",
1011 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1012 },
1013 [RPCAUTH_nfs] = {
1014 .name = "nfs",
1015 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1016 },
1017 [RPCAUTH_portmap] = {
1018 .name = "portmap",
1019 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1020 },
1021 [RPCAUTH_statd] = {
1022 .name = "statd",
1023 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024 },
1025 [RPCAUTH_nfsd4_cb] = {
1026 .name = "nfsd4_cb",
1027 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1028 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001029 [RPCAUTH_cache] = {
1030 .name = "cache",
1031 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1032 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001033};
1034
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001035/*
1036 * This call can be used only in RPC pipefs mount notification hooks.
1037 */
1038struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1039 const unsigned char *dir_name)
1040{
1041 struct qstr dir = {
1042 .name = dir_name,
1043 .len = strlen(dir_name),
1044 .hash = full_name_hash(dir_name, strlen(dir_name)),
1045 };
1046
1047 return d_lookup(sb->s_root, &dir);
1048}
1049EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1050
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001051void rpc_pipefs_init_net(struct net *net)
1052{
1053 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1054
1055 mutex_init(&sn->pipefs_sb_lock);
1056}
1057
1058/*
1059 * This call will be used for per network namespace operations calls.
1060 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1061 * found. This lock have to be released by rpc_put_sb_net() when all operations
1062 * will be completed.
1063 */
1064struct super_block *rpc_get_sb_net(const struct net *net)
1065{
1066 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1067
1068 mutex_lock(&sn->pipefs_sb_lock);
1069 if (sn->pipefs_sb)
1070 return sn->pipefs_sb;
1071 mutex_unlock(&sn->pipefs_sb_lock);
1072 return NULL;
1073}
1074EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1075
1076void rpc_put_sb_net(const struct net *net)
1077{
1078 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1079
1080 BUG_ON(sn->pipefs_sb == NULL);
1081 mutex_unlock(&sn->pipefs_sb_lock);
1082}
1083EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085static int
1086rpc_fill_super(struct super_block *sb, void *data, int silent)
1087{
1088 struct inode *inode;
1089 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001090 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001091 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001092 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 sb->s_blocksize = PAGE_CACHE_SIZE;
1095 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1096 sb->s_magic = RPCAUTH_GSSMAGIC;
1097 sb->s_op = &s_ops;
1098 sb->s_time_gran = 1;
1099
1100 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1101 if (!inode)
1102 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001103 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!root) {
1105 iput(inode);
1106 return -ENOMEM;
1107 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001108 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001109 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001110 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1111 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001112 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1113 RPC_PIPEFS_MOUNT,
1114 sb);
1115 if (err)
1116 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001117 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001118 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001120
1121err_depopulate:
1122 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1123 RPC_PIPEFS_UMOUNT,
1124 sb);
1125 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1126 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127}
1128
Al Virofc14f2f2010-07-25 01:48:30 +04001129static struct dentry *
1130rpc_mount(struct file_system_type *fs_type,
1131 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001133 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001136void rpc_kill_sb(struct super_block *sb)
1137{
1138 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001139 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001140
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001141 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001142 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001143 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001144 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001145 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1146 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001147 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1148 RPC_PIPEFS_UMOUNT,
1149 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001150 kill_litter_super(sb);
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153static struct file_system_type rpc_pipe_fs_type = {
1154 .owner = THIS_MODULE,
1155 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001156 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001157 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158};
1159
1160static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001161init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1164
Christoph Lametera35afb82007-05-16 22:10:57 -07001165 inode_init_once(&rpci->vfs_inode);
1166 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001167 rpci->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170int register_rpc_pipefs(void)
1171{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001172 int err;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001175 sizeof(struct rpc_inode),
1176 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1177 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001178 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 if (!rpc_inode_cachep)
1180 return -ENOMEM;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001181 err = rpc_clients_notifier_register();
1182 if (err)
1183 goto err_notifier;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001184 err = register_filesystem(&rpc_pipe_fs_type);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001185 if (err)
1186 goto err_register;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001188
1189err_register:
1190 rpc_clients_notifier_unregister();
1191err_notifier:
1192 kmem_cache_destroy(rpc_inode_cachep);
1193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
1196void unregister_rpc_pipefs(void)
1197{
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +04001198 rpc_clients_notifier_unregister();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001199 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 unregister_filesystem(&rpc_pipe_fs_type);
1201}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001202
1203/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1204MODULE_ALIAS("rpc_pipefs");