blob: 4093da79d5123c5e643456f54524759d22fd0496 [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>
21#include <linux/fs.h>
22#include <linux/poll.h>
23#include <linux/wait.h>
24#include <linux/seq_file.h>
25
26#include <linux/sunrpc/clnt.h>
27#include <linux/workqueue.h>
28#include <linux/sunrpc/rpc_pipe_fs.h>
Trond Myklebust8854e822009-08-09 15:14:30 -040029#include <linux/sunrpc/cache.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030030#include <linux/nsproxy.h>
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030031#include <linux/notifier.h>
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +030032
33#include "netns.h"
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030034#include "sunrpc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +030036#define RPCDBG_FACILITY RPCDBG_DEBUG
37
38#define NET_NAME(net) ((net == &init_net) ? " (init_net)" : "")
39
Al Virofc14f2f2010-07-25 01:48:30 +040040static struct vfsmount *rpc_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int rpc_mount_count;
42
43static struct file_system_type rpc_pipe_fs_type;
44
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define RPC_UPCALL_TIMEOUT (30*HZ)
49
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030050static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
51
52int rpc_pipefs_notifier_register(struct notifier_block *nb)
53{
54 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
55}
56EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
57
58void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
59{
60 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
61}
62EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
63
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030064static void rpc_purge_list(struct rpc_pipe *pipe, struct list_head *head,
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050066{
67 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050068
Trond Myklebust9842ef32006-02-01 12:18:44 -050069 if (list_empty(head))
70 return;
71 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050072 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040073 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050074 msg->errno = err;
75 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050076 } while (!list_empty(head));
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030077 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void
David Howells65f27f32006-11-22 14:55:48 +000081rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Trond Myklebust9842ef32006-02-01 12:18:44 -050083 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030084 struct rpc_pipe *pipe =
85 container_of(work, struct rpc_pipe, queue_timeout.work);
Trond Myklebust9842ef32006-02-01 12:18:44 -050086 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030088 spin_lock(&pipe->lock);
89 if (pipe->ops == NULL) {
90 spin_unlock(&pipe->lock);
Trond Myklebust9842ef32006-02-01 12:18:44 -050091 return;
92 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030093 destroy_msg = pipe->ops->destroy_msg;
94 if (pipe->nreaders == 0) {
95 list_splice_init(&pipe->pipe, &free_list);
96 pipe->pipelen = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -050097 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +030098 spin_unlock(&pipe->lock);
99 rpc_purge_list(pipe, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
Peng Taoc1225152011-09-22 21:50:10 -0400102ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
103 char __user *dst, size_t buflen)
104{
105 char *data = (char *)msg->data + msg->copied;
106 size_t mlen = min(msg->len - msg->copied, buflen);
107 unsigned long left;
108
109 left = copy_to_user(dst, data, mlen);
110 if (left == mlen) {
111 msg->errno = -EFAULT;
112 return -EFAULT;
113 }
114
115 mlen -= left;
116 msg->copied += mlen;
117 msg->errno = 0;
118 return mlen;
119}
120EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
121
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500122/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800123 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500124 * @inode: inode of upcall pipe on which to queue given message
125 * @msg: message to queue
126 *
127 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
128 * A userspace process may then later read the upcall by performing a
129 * read on an open file for this inode. It is up to the caller to
130 * initialize the fields of @msg (other than @msg->list) appropriately.
131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132int
Stanislav Kinsburskyd706ed12011-12-26 15:43:49 +0300133rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Trond Myklebust6070fe62005-10-27 22:12:46 -0400135 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300137 spin_lock(&pipe->lock);
138 if (pipe->ops == NULL)
Trond Myklebust6070fe62005-10-27 22:12:46 -0400139 goto out;
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 }
153out:
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300154 spin_unlock(&pipe->lock);
155 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return res;
157}
Trond Myklebust468039e2008-12-23 15:21:31 -0500158EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Trond Myklebust6070fe62005-10-27 22:12:46 -0400160static inline void
161rpc_inode_setowner(struct inode *inode, void *private)
162{
163 RPC_I(inode)->private = private;
164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void
167rpc_close_pipes(struct inode *inode)
168{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300169 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Trond Myklebustb693ba42009-08-09 15:14:15 -0400170 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500171 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800173 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300174 ops = pipe->ops;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500175 if (ops != NULL) {
176 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300177 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->ops = NULL;
184 spin_unlock(&pipe->lock);
185 rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
186 pipe->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500187 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500188 ops->release_pipe(inode);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300189 cancel_delayed_work_sync(&pipe->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400191 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800192 mutex_unlock(&inode->i_mutex);
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;
Christoph Lametere94b1762006-12-06 20:33:17 -0800199 rpci = (struct rpc_inode *)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
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100206rpc_i_callback(struct rcu_head *head)
207{
208 struct inode *inode = container_of(head, struct inode, i_rcu);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300209 kfree(RPC_I(inode)->pipe);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100210 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
211}
212
213static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214rpc_destroy_inode(struct inode *inode)
215{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100216 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
219static int
220rpc_pipe_open(struct inode *inode, struct file *filp)
221{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300222 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500223 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 int res = -ENXIO;
225
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800226 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300227 if (pipe->ops == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500228 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300229 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
230 if (first_open && pipe->ops->open_pipe) {
231 res = pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500232 if (res)
233 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500235 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300236 pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500237 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300238 pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500239 res = 0;
240out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800241 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return res;
243}
244
245static int
246rpc_pipe_release(struct inode *inode, struct file *filp)
247{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300248 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500250 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800252 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300253 if (pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700255 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300257 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500258 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400259 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300260 spin_unlock(&pipe->lock);
261 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300264 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500265 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300266 pipe->nreaders --;
267 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500268 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300269 spin_lock(&pipe->lock);
270 list_splice_init(&pipe->pipe, &free_list);
271 pipe->pipelen = 0;
272 spin_unlock(&pipe->lock);
273 rpc_purge_list(pipe, &free_list,
274 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500275 }
276 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300277 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
278 if (last_close && pipe->ops->release_pipe)
279 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800281 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return 0;
283}
284
285static ssize_t
286rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
287{
Josef Sipek303b46b2006-12-08 02:37:42 -0800288 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300289 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct rpc_pipe_msg *msg;
291 int res = 0;
292
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800293 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300294 if (pipe->ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 res = -EPIPE;
296 goto out_unlock;
297 }
298 msg = filp->private_data;
299 if (msg == NULL) {
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300300 spin_lock(&pipe->lock);
301 if (!list_empty(&pipe->pipe)) {
302 msg = list_entry(pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 struct rpc_pipe_msg,
304 list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300305 list_move(&msg->list, &pipe->in_upcall);
306 pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 filp->private_data = msg;
308 msg->copied = 0;
309 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300310 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (msg == NULL)
312 goto out_unlock;
313 }
314 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300315 res = pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (res < 0 || msg->len == msg->copied) {
317 filp->private_data = NULL;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300318 spin_lock(&pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400319 list_del_init(&msg->list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300320 spin_unlock(&pipe->lock);
321 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
323out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800324 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 return res;
326}
327
328static ssize_t
329rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
330{
Josef Sipek303b46b2006-12-08 02:37:42 -0800331 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300332 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 int res;
334
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800335 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 res = -EPIPE;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300337 if (pipe->ops != NULL)
338 res = pipe->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800339 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return res;
341}
342
343static unsigned int
344rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
345{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300346 struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 unsigned int mask = 0;
348
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300349 poll_wait(filp, &pipe->waitq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 mask = POLLOUT | POLLWRNORM;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300352 if (pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 mask |= POLLERR | POLLHUP;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300354 if (filp->private_data || !list_empty(&pipe->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mask |= POLLIN | POLLRDNORM;
356 return mask;
357}
358
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200359static long
360rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200362 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300363 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 int len;
365
366 switch (cmd) {
367 case FIONREAD:
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300368 spin_lock(&pipe->lock);
369 if (pipe->ops == NULL) {
370 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200372 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300373 len = pipe->pipelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (filp->private_data) {
375 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700376 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 len += msg->len - msg->copied;
378 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300379 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return put_user(len, (int __user *)arg);
381 default:
382 return -EINVAL;
383 }
384}
385
Arjan van de Venda7071d2007-02-12 00:55:36 -0800386static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 .owner = THIS_MODULE,
388 .llseek = no_llseek,
389 .read = rpc_pipe_read,
390 .write = rpc_pipe_write,
391 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200392 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .open = rpc_pipe_open,
394 .release = rpc_pipe_release,
395};
396
397static int
398rpc_show_info(struct seq_file *m, void *v)
399{
400 struct rpc_clnt *clnt = m->private;
401
402 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
403 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
404 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400405 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
406 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400407 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
409}
410
411static int
412rpc_info_open(struct inode *inode, struct file *file)
413{
Trond Myklebust006abe82010-09-12 19:55:25 -0400414 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 int ret = single_open(file, rpc_show_info, NULL);
416
417 if (!ret) {
418 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400419
420 spin_lock(&file->f_path.dentry->d_lock);
421 if (!d_unhashed(file->f_path.dentry))
422 clnt = RPC_I(inode)->private;
423 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
424 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 m->private = clnt;
426 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400427 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 single_release(inode, file);
429 ret = -EINVAL;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 return ret;
433}
434
435static int
436rpc_info_release(struct inode *inode, struct file *file)
437{
438 struct seq_file *m = file->private_data;
439 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
440
441 if (clnt)
442 rpc_release_client(clnt);
443 return single_release(inode, file);
444}
445
Arjan van de Venda7071d2007-02-12 00:55:36 -0800446static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 .owner = THIS_MODULE,
448 .open = rpc_info_open,
449 .read = seq_read,
450 .llseek = seq_lseek,
451 .release = rpc_info_release,
452};
453
454
455/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * Description of fs contents.
457 */
458struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400459 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800460 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400461 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Trond Myklebust54281542006-03-20 13:44:49 -0500464struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Trond Myklebust54281542006-03-20 13:44:49 -0500466 int err;
467
Al Virofc14f2f2010-07-25 01:48:30 +0400468 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500469 if (err != 0)
470 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400471 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400473EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Trond Myklebust54281542006-03-20 13:44:49 -0500475void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Al Virofc14f2f2010-07-25 01:48:30 +0400477 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400479EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Nick Pigginfe15ce42011-01-07 17:49:23 +1100481static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400482{
483 return 1;
484}
485
Al Viro3ba13d12009-02-20 06:02:22 +0000486static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400487 .d_delete = rpc_delete_dentry,
488};
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400491rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct inode *inode = new_inode(sb);
494 if (!inode)
495 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400496 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000499 switch (mode & S_IFMT) {
500 case S_IFDIR:
501 inode->i_fop = &simple_dir_operations;
502 inode->i_op = &simple_dir_inode_operations;
503 inc_nlink(inode);
504 default:
505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 return inode;
508}
509
Trond Myklebust75898062009-08-09 15:14:17 -0400510static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
511 umode_t mode,
512 const struct file_operations *i_fop,
513 void *private)
514{
515 struct inode *inode;
516
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000517 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400518 inode = rpc_get_inode(dir->i_sb, mode);
519 if (!inode)
520 goto out_err;
521 inode->i_ino = iunique(dir->i_sb, 100);
522 if (i_fop)
523 inode->i_fop = i_fop;
524 if (private)
525 rpc_inode_setowner(inode, private);
526 d_add(dentry, inode);
527 return 0;
528out_err:
529 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
530 __FILE__, __func__, dentry->d_name.name);
531 dput(dentry);
532 return -ENOMEM;
533}
534
Trond Myklebustac6fece2009-08-09 15:14:20 -0400535static int __rpc_create(struct inode *dir, struct dentry *dentry,
536 umode_t mode,
537 const struct file_operations *i_fop,
538 void *private)
539{
540 int err;
541
542 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
543 if (err)
544 return err;
545 fsnotify_create(dir, dentry);
546 return 0;
547}
548
Trond Myklebust75898062009-08-09 15:14:17 -0400549static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
550 umode_t mode,
551 const struct file_operations *i_fop,
552 void *private)
553{
554 int err;
555
556 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
557 if (err)
558 return err;
559 inc_nlink(dir);
560 fsnotify_mkdir(dir, dentry);
561 return 0;
562}
563
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300564static void
565init_pipe(struct rpc_pipe *pipe)
566{
567 pipe->nreaders = 0;
568 pipe->nwriters = 0;
569 INIT_LIST_HEAD(&pipe->in_upcall);
570 INIT_LIST_HEAD(&pipe->in_downcall);
571 INIT_LIST_HEAD(&pipe->pipe);
572 pipe->pipelen = 0;
573 init_waitqueue_head(&pipe->waitq);
574 INIT_DELAYED_WORK(&pipe->queue_timeout,
575 rpc_timeout_upcall_queue);
576 pipe->ops = NULL;
577 spin_lock_init(&pipe->lock);
578
579}
580
Trond Myklebust75898062009-08-09 15:14:17 -0400581static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
582 umode_t mode,
583 const struct file_operations *i_fop,
584 void *private,
585 const struct rpc_pipe_ops *ops,
586 int flags)
587{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300588 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400589 struct rpc_inode *rpci;
590 int err;
591
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300592 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
593 if (!pipe)
594 return -ENOMEM;
595 init_pipe(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400596 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300597 if (err) {
598 kfree(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400599 return err;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300600 }
Trond Myklebust75898062009-08-09 15:14:17 -0400601 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400602 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300603 rpci->pipe = pipe;
604 rpci->pipe->flags = flags;
605 rpci->pipe->ops = ops;
Trond Myklebust75898062009-08-09 15:14:17 -0400606 fsnotify_create(dir, dentry);
607 return 0;
608}
609
Trond Myklebustac6fece2009-08-09 15:14:20 -0400610static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
611{
612 int ret;
613
614 dget(dentry);
615 ret = simple_rmdir(dir, dentry);
616 d_delete(dentry);
617 dput(dentry);
618 return ret;
619}
620
Trond Myklebust810d90b2009-08-09 15:14:18 -0400621static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
622{
623 int ret;
624
625 dget(dentry);
626 ret = simple_unlink(dir, dentry);
627 d_delete(dentry);
628 dput(dentry);
629 return ret;
630}
631
632static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
633{
634 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400635
Trond Myklebust810d90b2009-08-09 15:14:18 -0400636 rpc_close_pipes(inode);
637 return __rpc_unlink(dir, dentry);
638}
639
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400640static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
641 struct qstr *name)
642{
643 struct dentry *dentry;
644
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300645 dentry = d_lookup(parent, name);
646 if (!dentry) {
647 dentry = d_alloc(parent, name);
648 if (!dentry)
649 return ERR_PTR(-ENOMEM);
650 }
651 if (dentry->d_inode == NULL) {
652 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe12010-03-21 12:10:34 -0400653 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300654 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400655 dput(dentry);
656 return ERR_PTR(-EEXIST);
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659/*
660 * FIXME: This probably has races.
661 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400662static void __rpc_depopulate(struct dentry *parent,
663 const struct rpc_filelist *files,
664 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400667 struct dentry *dentry;
668 struct qstr name;
669 int i;
670
671 for (i = start; i < eof; i++) {
672 name.name = files[i].name;
673 name.len = strlen(files[i].name);
674 name.hash = full_name_hash(name.name, name.len);
675 dentry = d_lookup(parent, &name);
676
677 if (dentry == NULL)
678 continue;
679 if (dentry->d_inode == NULL)
680 goto next;
681 switch (dentry->d_inode->i_mode & S_IFMT) {
682 default:
683 BUG();
684 case S_IFREG:
685 __rpc_unlink(dir, dentry);
686 break;
687 case S_IFDIR:
688 __rpc_rmdir(dir, dentry);
689 }
690next:
691 dput(dentry);
692 }
693}
694
695static void rpc_depopulate(struct dentry *parent,
696 const struct rpc_filelist *files,
697 int start, int eof)
698{
699 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Arjan van de Venc6573c22006-07-03 00:25:16 -0700701 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400702 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800703 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
Trond Myklebustac6fece2009-08-09 15:14:20 -0400706static int rpc_populate(struct dentry *parent,
707 const struct rpc_filelist *files,
708 int start, int eof,
709 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400711 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400713 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800715 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400717 struct qstr q;
718
719 q.name = files[i].name;
720 q.len = strlen(files[i].name);
721 q.hash = full_name_hash(q.name, q.len);
722 dentry = __rpc_lookup_create_exclusive(parent, &q);
723 err = PTR_ERR(dentry);
724 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400726 switch (files[i].mode & S_IFMT) {
727 default:
728 BUG();
729 case S_IFREG:
730 err = __rpc_create(dir, dentry,
731 files[i].mode,
732 files[i].i_fop,
733 private);
734 break;
735 case S_IFDIR:
736 err = __rpc_mkdir(dir, dentry,
737 files[i].mode,
738 NULL,
739 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400741 if (err != 0)
742 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800744 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return 0;
746out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400747 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800748 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800750 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400751 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400752}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100753
Trond Myklebuste57aed72009-08-09 15:14:26 -0400754static struct dentry *rpc_mkdir_populate(struct dentry *parent,
755 struct qstr *name, umode_t mode, void *private,
756 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400757{
Trond Myklebustf1345852005-09-23 11:08:25 -0400758 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400759 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400760 int error;
761
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400762 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
763 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400764 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400765 goto out;
766 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400767 if (error != 0)
768 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400769 if (populate != NULL) {
770 error = populate(dentry, args_populate);
771 if (error)
772 goto err_rmdir;
773 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400774out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800775 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400776 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400777err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400778 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400779out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400780 dentry = ERR_PTR(error);
781 goto out;
782}
783
Trond Myklebuste57aed72009-08-09 15:14:26 -0400784static int rpc_rmdir_depopulate(struct dentry *dentry,
785 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400786{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700787 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400788 struct inode *dir;
789 int error;
790
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700791 parent = dget_parent(dentry);
792 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700793 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400794 if (depopulate != NULL)
795 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400796 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800797 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700798 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400799 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500802/**
803 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
804 * @parent: dentry of directory to create new "pipe" in
805 * @name: name of pipe
806 * @private: private data to associate with the pipe, for the caller's use
807 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500808 * release_pipe, open_pipe, and destroy_msg.
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300809 * @flags: rpc_pipe flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500810 *
811 * Data is made available for userspace to read by calls to
812 * rpc_queue_upcall(). The actual reads will result in calls to
813 * @ops->upcall, which will be called with the file pointer,
814 * message, and userspace buffer to copy to.
815 *
816 * Writes can come at any time, and do not necessarily have to be
817 * responses to upcalls. They will result in calls to @msg->downcall.
818 *
819 * The @private argument passed here will be available to all these methods
820 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
821 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400822struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
823 void *private, const struct rpc_pipe_ops *ops,
824 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400827 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400828 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400829 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400830 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400831
832 if (ops->upcall == NULL)
833 umode &= ~S_IRUGO;
834 if (ops->downcall == NULL)
835 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400837 q.name = name;
838 q.len = strlen(name);
839 q.hash = full_name_hash(q.name, q.len),
840
841 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300842 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400844 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400845 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400846 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400847 if (err)
848 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400849out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800850 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400851 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400852out_err:
853 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400854 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800855 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400856 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Trond Myklebust468039e2008-12-23 15:21:31 -0500859EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500861/**
862 * rpc_unlink - remove a pipe
863 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
864 *
865 * After this call, lookups will no longer find the pipe, and any
866 * attempts to read or write using preexisting opens of the pipe will
867 * return -EPIPE.
868 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400869int
Trond Myklebust5d674762006-07-31 14:11:48 -0700870rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Trond Myklebust5d674762006-07-31 14:11:48 -0700872 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400873 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700874 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Trond Myklebust5d674762006-07-31 14:11:48 -0700876 parent = dget_parent(dentry);
877 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700878 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400879 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800880 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700881 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400882 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
Trond Myklebust468039e2008-12-23 15:21:31 -0500884EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Trond Myklebuste57aed72009-08-09 15:14:26 -0400886enum {
887 RPCAUTH_info,
888 RPCAUTH_EOF
889};
890
891static const struct rpc_filelist authfiles[] = {
892 [RPCAUTH_info] = {
893 .name = "info",
894 .i_fop = &rpc_info_operations,
895 .mode = S_IFREG | S_IRUSR,
896 },
897};
898
899static int rpc_clntdir_populate(struct dentry *dentry, void *private)
900{
901 return rpc_populate(dentry,
902 authfiles, RPCAUTH_info, RPCAUTH_EOF,
903 private);
904}
905
906static void rpc_clntdir_depopulate(struct dentry *dentry)
907{
908 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
909}
910
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400911/**
912 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400913 * @dentry: dentry from the rpc_pipefs root to the new directory
914 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400915 * @rpc_client: rpc client to associate with this directory
916 *
917 * This creates a directory at the given @path associated with
918 * @rpc_clnt, which will contain a file named "info" with some basic
919 * information about the client, together with any "pipes" that may
920 * later be created using rpc_mkpipe().
921 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400922struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400923 struct qstr *name,
924 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400925{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400926 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
927 rpc_clntdir_populate, rpc_client);
928}
929
930/**
931 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
932 * @dentry: directory to remove
933 */
934int rpc_remove_client_dir(struct dentry *dentry)
935{
936 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400937}
938
Trond Myklebust8854e822009-08-09 15:14:30 -0400939static const struct rpc_filelist cache_pipefs_files[3] = {
940 [0] = {
941 .name = "channel",
942 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400943 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400944 },
945 [1] = {
946 .name = "content",
947 .i_fop = &content_file_operations_pipefs,
948 .mode = S_IFREG|S_IRUSR,
949 },
950 [2] = {
951 .name = "flush",
952 .i_fop = &cache_flush_operations_pipefs,
953 .mode = S_IFREG|S_IRUSR|S_IWUSR,
954 },
955};
956
957static int rpc_cachedir_populate(struct dentry *dentry, void *private)
958{
959 return rpc_populate(dentry,
960 cache_pipefs_files, 0, 3,
961 private);
962}
963
964static void rpc_cachedir_depopulate(struct dentry *dentry)
965{
966 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
967}
968
969struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400970 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400971{
972 return rpc_mkdir_populate(parent, name, umode, NULL,
973 rpc_cachedir_populate, cd);
974}
975
976void rpc_remove_cache_dir(struct dentry *dentry)
977{
978 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/*
982 * populate the filesystem
983 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700984static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 .alloc_inode = rpc_alloc_inode,
986 .destroy_inode = rpc_destroy_inode,
987 .statfs = simple_statfs,
988};
989
990#define RPCAUTH_GSSMAGIC 0x67596969
991
Trond Myklebustbb156742009-08-09 15:14:21 -0400992/*
993 * We have a single directory with 1 node in it.
994 */
995enum {
996 RPCAUTH_lockd,
997 RPCAUTH_mount,
998 RPCAUTH_nfs,
999 RPCAUTH_portmap,
1000 RPCAUTH_statd,
1001 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001002 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -04001003 RPCAUTH_RootEOF
1004};
1005
1006static const struct rpc_filelist files[] = {
1007 [RPCAUTH_lockd] = {
1008 .name = "lockd",
1009 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1010 },
1011 [RPCAUTH_mount] = {
1012 .name = "mount",
1013 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1014 },
1015 [RPCAUTH_nfs] = {
1016 .name = "nfs",
1017 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1018 },
1019 [RPCAUTH_portmap] = {
1020 .name = "portmap",
1021 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1022 },
1023 [RPCAUTH_statd] = {
1024 .name = "statd",
1025 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1026 },
1027 [RPCAUTH_nfsd4_cb] = {
1028 .name = "nfsd4_cb",
1029 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1030 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001031 [RPCAUTH_cache] = {
1032 .name = "cache",
1033 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1034 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001035};
1036
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001037/*
1038 * This call can be used only in RPC pipefs mount notification hooks.
1039 */
1040struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1041 const unsigned char *dir_name)
1042{
1043 struct qstr dir = {
1044 .name = dir_name,
1045 .len = strlen(dir_name),
1046 .hash = full_name_hash(dir_name, strlen(dir_name)),
1047 };
1048
1049 return d_lookup(sb->s_root, &dir);
1050}
1051EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1052
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001053void rpc_pipefs_init_net(struct net *net)
1054{
1055 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1056
1057 mutex_init(&sn->pipefs_sb_lock);
1058}
1059
1060/*
1061 * This call will be used for per network namespace operations calls.
1062 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1063 * found. This lock have to be released by rpc_put_sb_net() when all operations
1064 * will be completed.
1065 */
1066struct super_block *rpc_get_sb_net(const struct net *net)
1067{
1068 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1069
1070 mutex_lock(&sn->pipefs_sb_lock);
1071 if (sn->pipefs_sb)
1072 return sn->pipefs_sb;
1073 mutex_unlock(&sn->pipefs_sb_lock);
1074 return NULL;
1075}
1076EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1077
1078void rpc_put_sb_net(const struct net *net)
1079{
1080 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1081
1082 BUG_ON(sn->pipefs_sb == NULL);
1083 mutex_unlock(&sn->pipefs_sb_lock);
1084}
1085EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087static int
1088rpc_fill_super(struct super_block *sb, void *data, int silent)
1089{
1090 struct inode *inode;
1091 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001092 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001093 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001094 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 sb->s_blocksize = PAGE_CACHE_SIZE;
1097 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1098 sb->s_magic = RPCAUTH_GSSMAGIC;
1099 sb->s_op = &s_ops;
1100 sb->s_time_gran = 1;
1101
1102 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1103 if (!inode)
1104 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001105 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (!root) {
1107 iput(inode);
1108 return -ENOMEM;
1109 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001110 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001111 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001112 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1113 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001114 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1115 RPC_PIPEFS_MOUNT,
1116 sb);
1117 if (err)
1118 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001119 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001120 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001122
1123err_depopulate:
1124 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1125 RPC_PIPEFS_UMOUNT,
1126 sb);
1127 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1128 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Al Virofc14f2f2010-07-25 01:48:30 +04001131static struct dentry *
1132rpc_mount(struct file_system_type *fs_type,
1133 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001135 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001138void rpc_kill_sb(struct super_block *sb)
1139{
1140 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001141 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001142
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001143 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001144 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001145 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001146 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001147 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1148 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001149 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1150 RPC_PIPEFS_UMOUNT,
1151 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001152 kill_litter_super(sb);
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155static struct file_system_type rpc_pipe_fs_type = {
1156 .owner = THIS_MODULE,
1157 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001158 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001159 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160};
1161
1162static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001163init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1166
Christoph Lametera35afb82007-05-16 22:10:57 -07001167 inode_init_once(&rpci->vfs_inode);
1168 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001169 rpci->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172int register_rpc_pipefs(void)
1173{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001174 int err;
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001177 sizeof(struct rpc_inode),
1178 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1179 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001180 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 if (!rpc_inode_cachep)
1182 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001183 err = register_filesystem(&rpc_pipe_fs_type);
1184 if (err) {
1185 kmem_cache_destroy(rpc_inode_cachep);
1186 return err;
1187 }
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return 0;
1190}
1191
1192void unregister_rpc_pipefs(void)
1193{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001194 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unregister_filesystem(&rpc_pipe_fs_type);
1196}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001197
1198/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1199MODULE_ALIAS("rpc_pipefs");