blob: 299b1a3c3e49b84062d5d8348865ec28ed1d4159 [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
133rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
134{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300135 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400136 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300138 spin_lock(&pipe->lock);
139 if (pipe->ops == NULL)
Trond Myklebust6070fe62005-10-27 22:12:46 -0400140 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300141 if (pipe->nreaders) {
142 list_add_tail(&msg->list, &pipe->pipe);
143 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400144 res = 0;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300145 } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
146 if (list_empty(&pipe->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500147 queue_delayed_work(rpciod_workqueue,
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300148 &pipe->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 RPC_UPCALL_TIMEOUT);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300150 list_add_tail(&msg->list, &pipe->pipe);
151 pipe->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400152 res = 0;
153 }
154out:
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300155 spin_unlock(&pipe->lock);
156 wake_up(&pipe->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 return res;
158}
Trond Myklebust468039e2008-12-23 15:21:31 -0500159EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Trond Myklebust6070fe62005-10-27 22:12:46 -0400161static inline void
162rpc_inode_setowner(struct inode *inode, void *private)
163{
164 RPC_I(inode)->private = private;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void
168rpc_close_pipes(struct inode *inode)
169{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300170 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Trond Myklebustb693ba42009-08-09 15:14:15 -0400171 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500172 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800174 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300175 ops = pipe->ops;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500176 if (ops != NULL) {
177 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300178 spin_lock(&pipe->lock);
179 need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
180 pipe->nreaders = 0;
181 list_splice_init(&pipe->in_upcall, &free_list);
182 list_splice_init(&pipe->pipe, &free_list);
183 pipe->pipelen = 0;
184 pipe->ops = NULL;
185 spin_unlock(&pipe->lock);
186 rpc_purge_list(pipe, &free_list, ops->destroy_msg, -EPIPE);
187 pipe->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500188 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500189 ops->release_pipe(inode);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300190 cancel_delayed_work_sync(&pipe->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400192 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800193 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static struct inode *
197rpc_alloc_inode(struct super_block *sb)
198{
199 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800200 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!rpci)
202 return NULL;
203 return &rpci->vfs_inode;
204}
205
206static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100207rpc_i_callback(struct rcu_head *head)
208{
209 struct inode *inode = container_of(head, struct inode, i_rcu);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300210 kfree(RPC_I(inode)->pipe);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100211 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
212}
213
214static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215rpc_destroy_inode(struct inode *inode)
216{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100217 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
220static int
221rpc_pipe_open(struct inode *inode, struct file *filp)
222{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300223 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500224 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int res = -ENXIO;
226
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800227 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300228 if (pipe->ops == NULL)
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500229 goto out;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300230 first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
231 if (first_open && pipe->ops->open_pipe) {
232 res = pipe->ops->open_pipe(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500233 if (res)
234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500236 if (filp->f_mode & FMODE_READ)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300237 pipe->nreaders++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500238 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300239 pipe->nwriters++;
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500240 res = 0;
241out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800242 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return res;
244}
245
246static int
247rpc_pipe_release(struct inode *inode, struct file *filp)
248{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300249 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500251 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800253 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300254 if (pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700256 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (msg != NULL) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300258 spin_lock(&pipe->lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500259 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400260 list_del_init(&msg->list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300261 spin_unlock(&pipe->lock);
262 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 if (filp->f_mode & FMODE_WRITE)
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300265 pipe->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500266 if (filp->f_mode & FMODE_READ) {
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300267 pipe->nreaders --;
268 if (pipe->nreaders == 0) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500269 LIST_HEAD(free_list);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300270 spin_lock(&pipe->lock);
271 list_splice_init(&pipe->pipe, &free_list);
272 pipe->pipelen = 0;
273 spin_unlock(&pipe->lock);
274 rpc_purge_list(pipe, &free_list,
275 pipe->ops->destroy_msg, -EAGAIN);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500276 }
277 }
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300278 last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
279 if (last_close && pipe->ops->release_pipe)
280 pipe->ops->release_pipe(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800282 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return 0;
284}
285
286static ssize_t
287rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
288{
Josef Sipek303b46b2006-12-08 02:37:42 -0800289 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300290 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 struct rpc_pipe_msg *msg;
292 int res = 0;
293
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800294 mutex_lock(&inode->i_mutex);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300295 if (pipe->ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 res = -EPIPE;
297 goto out_unlock;
298 }
299 msg = filp->private_data;
300 if (msg == NULL) {
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300301 spin_lock(&pipe->lock);
302 if (!list_empty(&pipe->pipe)) {
303 msg = list_entry(pipe->pipe.next,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 struct rpc_pipe_msg,
305 list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300306 list_move(&msg->list, &pipe->in_upcall);
307 pipe->pipelen -= msg->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 filp->private_data = msg;
309 msg->copied = 0;
310 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300311 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (msg == NULL)
313 goto out_unlock;
314 }
315 /* NOTE: it is up to the callback to update msg->copied */
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300316 res = pipe->ops->upcall(filp, msg, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (res < 0 || msg->len == msg->copied) {
318 filp->private_data = NULL;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300319 spin_lock(&pipe->lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400320 list_del_init(&msg->list);
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300321 spin_unlock(&pipe->lock);
322 pipe->ops->destroy_msg(msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800325 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return res;
327}
328
329static ssize_t
330rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
331{
Josef Sipek303b46b2006-12-08 02:37:42 -0800332 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300333 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 int res;
335
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800336 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 res = -EPIPE;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300338 if (pipe->ops != NULL)
339 res = pipe->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800340 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return res;
342}
343
344static unsigned int
345rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
346{
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300347 struct rpc_pipe *pipe = RPC_I(filp->f_path.dentry->d_inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned int mask = 0;
349
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300350 poll_wait(filp, &pipe->waitq, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 mask = POLLOUT | POLLWRNORM;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300353 if (pipe->ops == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 mask |= POLLERR | POLLHUP;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300355 if (filp->private_data || !list_empty(&pipe->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 mask |= POLLIN | POLLRDNORM;
357 return mask;
358}
359
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200360static long
361rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200363 struct inode *inode = filp->f_path.dentry->d_inode;
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300364 struct rpc_pipe *pipe = RPC_I(inode)->pipe;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int len;
366
367 switch (cmd) {
368 case FIONREAD:
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300369 spin_lock(&pipe->lock);
370 if (pipe->ops == NULL) {
371 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200373 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300374 len = pipe->pipelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (filp->private_data) {
376 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700377 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 len += msg->len - msg->copied;
379 }
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300380 spin_unlock(&pipe->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return put_user(len, (int __user *)arg);
382 default:
383 return -EINVAL;
384 }
385}
386
Arjan van de Venda7071d2007-02-12 00:55:36 -0800387static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .owner = THIS_MODULE,
389 .llseek = no_llseek,
390 .read = rpc_pipe_read,
391 .write = rpc_pipe_write,
392 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200393 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .open = rpc_pipe_open,
395 .release = rpc_pipe_release,
396};
397
398static int
399rpc_show_info(struct seq_file *m, void *v)
400{
401 struct rpc_clnt *clnt = m->private;
402
403 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
404 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
405 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400406 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
407 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400408 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410}
411
412static int
413rpc_info_open(struct inode *inode, struct file *file)
414{
Trond Myklebust006abe82010-09-12 19:55:25 -0400415 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 int ret = single_open(file, rpc_show_info, NULL);
417
418 if (!ret) {
419 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400420
421 spin_lock(&file->f_path.dentry->d_lock);
422 if (!d_unhashed(file->f_path.dentry))
423 clnt = RPC_I(inode)->private;
424 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
425 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 m->private = clnt;
427 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400428 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 single_release(inode, file);
430 ret = -EINVAL;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433 return ret;
434}
435
436static int
437rpc_info_release(struct inode *inode, struct file *file)
438{
439 struct seq_file *m = file->private_data;
440 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
441
442 if (clnt)
443 rpc_release_client(clnt);
444 return single_release(inode, file);
445}
446
Arjan van de Venda7071d2007-02-12 00:55:36 -0800447static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 .owner = THIS_MODULE,
449 .open = rpc_info_open,
450 .read = seq_read,
451 .llseek = seq_lseek,
452 .release = rpc_info_release,
453};
454
455
456/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 * Description of fs contents.
458 */
459struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400460 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800461 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400462 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463};
464
Trond Myklebust54281542006-03-20 13:44:49 -0500465struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Trond Myklebust54281542006-03-20 13:44:49 -0500467 int err;
468
Al Virofc14f2f2010-07-25 01:48:30 +0400469 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500470 if (err != 0)
471 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400472 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400474EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Trond Myklebust54281542006-03-20 13:44:49 -0500476void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Al Virofc14f2f2010-07-25 01:48:30 +0400478 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400480EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Nick Pigginfe15ce42011-01-07 17:49:23 +1100482static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400483{
484 return 1;
485}
486
Al Viro3ba13d12009-02-20 06:02:22 +0000487static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400488 .d_delete = rpc_delete_dentry,
489};
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400492rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct inode *inode = new_inode(sb);
495 if (!inode)
496 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400497 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000500 switch (mode & S_IFMT) {
501 case S_IFDIR:
502 inode->i_fop = &simple_dir_operations;
503 inode->i_op = &simple_dir_inode_operations;
504 inc_nlink(inode);
505 default:
506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 return inode;
509}
510
Trond Myklebust75898062009-08-09 15:14:17 -0400511static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
512 umode_t mode,
513 const struct file_operations *i_fop,
514 void *private)
515{
516 struct inode *inode;
517
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000518 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400519 inode = rpc_get_inode(dir->i_sb, mode);
520 if (!inode)
521 goto out_err;
522 inode->i_ino = iunique(dir->i_sb, 100);
523 if (i_fop)
524 inode->i_fop = i_fop;
525 if (private)
526 rpc_inode_setowner(inode, private);
527 d_add(dentry, inode);
528 return 0;
529out_err:
530 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
531 __FILE__, __func__, dentry->d_name.name);
532 dput(dentry);
533 return -ENOMEM;
534}
535
Trond Myklebustac6fece2009-08-09 15:14:20 -0400536static int __rpc_create(struct inode *dir, struct dentry *dentry,
537 umode_t mode,
538 const struct file_operations *i_fop,
539 void *private)
540{
541 int err;
542
543 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
544 if (err)
545 return err;
546 fsnotify_create(dir, dentry);
547 return 0;
548}
549
Trond Myklebust75898062009-08-09 15:14:17 -0400550static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
551 umode_t mode,
552 const struct file_operations *i_fop,
553 void *private)
554{
555 int err;
556
557 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
558 if (err)
559 return err;
560 inc_nlink(dir);
561 fsnotify_mkdir(dir, dentry);
562 return 0;
563}
564
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300565static void
566init_pipe(struct rpc_pipe *pipe)
567{
568 pipe->nreaders = 0;
569 pipe->nwriters = 0;
570 INIT_LIST_HEAD(&pipe->in_upcall);
571 INIT_LIST_HEAD(&pipe->in_downcall);
572 INIT_LIST_HEAD(&pipe->pipe);
573 pipe->pipelen = 0;
574 init_waitqueue_head(&pipe->waitq);
575 INIT_DELAYED_WORK(&pipe->queue_timeout,
576 rpc_timeout_upcall_queue);
577 pipe->ops = NULL;
578 spin_lock_init(&pipe->lock);
579
580}
581
Trond Myklebust75898062009-08-09 15:14:17 -0400582static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
583 umode_t mode,
584 const struct file_operations *i_fop,
585 void *private,
586 const struct rpc_pipe_ops *ops,
587 int flags)
588{
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300589 struct rpc_pipe *pipe;
Trond Myklebust75898062009-08-09 15:14:17 -0400590 struct rpc_inode *rpci;
591 int err;
592
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300593 pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
594 if (!pipe)
595 return -ENOMEM;
596 init_pipe(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400597 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300598 if (err) {
599 kfree(pipe);
Trond Myklebust75898062009-08-09 15:14:17 -0400600 return err;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300601 }
Trond Myklebust75898062009-08-09 15:14:17 -0400602 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400603 rpci->private = private;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +0300604 rpci->pipe = pipe;
605 rpci->pipe->flags = flags;
606 rpci->pipe->ops = ops;
Trond Myklebust75898062009-08-09 15:14:17 -0400607 fsnotify_create(dir, dentry);
608 return 0;
609}
610
Trond Myklebustac6fece2009-08-09 15:14:20 -0400611static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
612{
613 int ret;
614
615 dget(dentry);
616 ret = simple_rmdir(dir, dentry);
617 d_delete(dentry);
618 dput(dentry);
619 return ret;
620}
621
Trond Myklebust810d90b2009-08-09 15:14:18 -0400622static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
623{
624 int ret;
625
626 dget(dentry);
627 ret = simple_unlink(dir, dentry);
628 d_delete(dentry);
629 dput(dentry);
630 return ret;
631}
632
633static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
634{
635 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400636
Trond Myklebust810d90b2009-08-09 15:14:18 -0400637 rpc_close_pipes(inode);
638 return __rpc_unlink(dir, dentry);
639}
640
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400641static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
642 struct qstr *name)
643{
644 struct dentry *dentry;
645
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300646 dentry = d_lookup(parent, name);
647 if (!dentry) {
648 dentry = d_alloc(parent, name);
649 if (!dentry)
650 return ERR_PTR(-ENOMEM);
651 }
652 if (dentry->d_inode == NULL) {
653 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400654 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300655 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400656 dput(dentry);
657 return ERR_PTR(-EEXIST);
658}
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660/*
661 * FIXME: This probably has races.
662 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400663static void __rpc_depopulate(struct dentry *parent,
664 const struct rpc_filelist *files,
665 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400668 struct dentry *dentry;
669 struct qstr name;
670 int i;
671
672 for (i = start; i < eof; i++) {
673 name.name = files[i].name;
674 name.len = strlen(files[i].name);
675 name.hash = full_name_hash(name.name, name.len);
676 dentry = d_lookup(parent, &name);
677
678 if (dentry == NULL)
679 continue;
680 if (dentry->d_inode == NULL)
681 goto next;
682 switch (dentry->d_inode->i_mode & S_IFMT) {
683 default:
684 BUG();
685 case S_IFREG:
686 __rpc_unlink(dir, dentry);
687 break;
688 case S_IFDIR:
689 __rpc_rmdir(dir, dentry);
690 }
691next:
692 dput(dentry);
693 }
694}
695
696static void rpc_depopulate(struct dentry *parent,
697 const struct rpc_filelist *files,
698 int start, int eof)
699{
700 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Arjan van de Venc6573c22006-07-03 00:25:16 -0700702 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400703 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800704 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Trond Myklebustac6fece2009-08-09 15:14:20 -0400707static int rpc_populate(struct dentry *parent,
708 const struct rpc_filelist *files,
709 int start, int eof,
710 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400712 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400714 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800716 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400718 struct qstr q;
719
720 q.name = files[i].name;
721 q.len = strlen(files[i].name);
722 q.hash = full_name_hash(q.name, q.len);
723 dentry = __rpc_lookup_create_exclusive(parent, &q);
724 err = PTR_ERR(dentry);
725 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400727 switch (files[i].mode & S_IFMT) {
728 default:
729 BUG();
730 case S_IFREG:
731 err = __rpc_create(dir, dentry,
732 files[i].mode,
733 files[i].i_fop,
734 private);
735 break;
736 case S_IFDIR:
737 err = __rpc_mkdir(dir, dentry,
738 files[i].mode,
739 NULL,
740 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400742 if (err != 0)
743 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800745 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400748 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800749 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800751 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400752 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400753}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100754
Trond Myklebuste57aed72009-08-09 15:14:26 -0400755static struct dentry *rpc_mkdir_populate(struct dentry *parent,
756 struct qstr *name, umode_t mode, void *private,
757 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400758{
Trond Myklebustf1345852005-09-23 11:08:25 -0400759 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400760 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400761 int error;
762
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400763 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
764 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400765 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400766 goto out;
767 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400768 if (error != 0)
769 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400770 if (populate != NULL) {
771 error = populate(dentry, args_populate);
772 if (error)
773 goto err_rmdir;
774 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400775out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800776 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400777 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400778err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400779 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400780out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400781 dentry = ERR_PTR(error);
782 goto out;
783}
784
Trond Myklebuste57aed72009-08-09 15:14:26 -0400785static int rpc_rmdir_depopulate(struct dentry *dentry,
786 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400787{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700788 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400789 struct inode *dir;
790 int error;
791
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700792 parent = dget_parent(dentry);
793 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700794 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400795 if (depopulate != NULL)
796 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400797 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800798 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700799 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400800 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500803/**
804 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
805 * @parent: dentry of directory to create new "pipe" in
806 * @name: name of pipe
807 * @private: private data to associate with the pipe, for the caller's use
808 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500809 * release_pipe, open_pipe, and destroy_msg.
Stanislav Kinsburskyd0fe13b2011-12-26 15:43:41 +0300810 * @flags: rpc_pipe flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500811 *
812 * Data is made available for userspace to read by calls to
813 * rpc_queue_upcall(). The actual reads will result in calls to
814 * @ops->upcall, which will be called with the file pointer,
815 * message, and userspace buffer to copy to.
816 *
817 * Writes can come at any time, and do not necessarily have to be
818 * responses to upcalls. They will result in calls to @msg->downcall.
819 *
820 * The @private argument passed here will be available to all these methods
821 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
822 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400823struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
824 void *private, const struct rpc_pipe_ops *ops,
825 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400828 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400829 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400830 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400831 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400832
833 if (ops->upcall == NULL)
834 umode &= ~S_IRUGO;
835 if (ops->downcall == NULL)
836 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400838 q.name = name;
839 q.len = strlen(name);
840 q.hash = full_name_hash(q.name, q.len),
841
842 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300843 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400845 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400846 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400847 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400848 if (err)
849 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400850out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800851 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400852 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400853out_err:
854 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400855 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800856 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400857 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400858 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
Trond Myklebust468039e2008-12-23 15:21:31 -0500860EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500862/**
863 * rpc_unlink - remove a pipe
864 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
865 *
866 * After this call, lookups will no longer find the pipe, and any
867 * attempts to read or write using preexisting opens of the pipe will
868 * return -EPIPE.
869 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400870int
Trond Myklebust5d674762006-07-31 14:11:48 -0700871rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Trond Myklebust5d674762006-07-31 14:11:48 -0700873 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400874 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700875 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Trond Myklebust5d674762006-07-31 14:11:48 -0700877 parent = dget_parent(dentry);
878 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700879 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400880 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800881 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700882 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400883 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884}
Trond Myklebust468039e2008-12-23 15:21:31 -0500885EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Trond Myklebuste57aed72009-08-09 15:14:26 -0400887enum {
888 RPCAUTH_info,
889 RPCAUTH_EOF
890};
891
892static const struct rpc_filelist authfiles[] = {
893 [RPCAUTH_info] = {
894 .name = "info",
895 .i_fop = &rpc_info_operations,
896 .mode = S_IFREG | S_IRUSR,
897 },
898};
899
900static int rpc_clntdir_populate(struct dentry *dentry, void *private)
901{
902 return rpc_populate(dentry,
903 authfiles, RPCAUTH_info, RPCAUTH_EOF,
904 private);
905}
906
907static void rpc_clntdir_depopulate(struct dentry *dentry)
908{
909 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
910}
911
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400912/**
913 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400914 * @dentry: dentry from the rpc_pipefs root to the new directory
915 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400916 * @rpc_client: rpc client to associate with this directory
917 *
918 * This creates a directory at the given @path associated with
919 * @rpc_clnt, which will contain a file named "info" with some basic
920 * information about the client, together with any "pipes" that may
921 * later be created using rpc_mkpipe().
922 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400923struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400924 struct qstr *name,
925 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400926{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400927 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
928 rpc_clntdir_populate, rpc_client);
929}
930
931/**
932 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
933 * @dentry: directory to remove
934 */
935int rpc_remove_client_dir(struct dentry *dentry)
936{
937 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400938}
939
Trond Myklebust8854e822009-08-09 15:14:30 -0400940static const struct rpc_filelist cache_pipefs_files[3] = {
941 [0] = {
942 .name = "channel",
943 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400944 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400945 },
946 [1] = {
947 .name = "content",
948 .i_fop = &content_file_operations_pipefs,
949 .mode = S_IFREG|S_IRUSR,
950 },
951 [2] = {
952 .name = "flush",
953 .i_fop = &cache_flush_operations_pipefs,
954 .mode = S_IFREG|S_IRUSR|S_IWUSR,
955 },
956};
957
958static int rpc_cachedir_populate(struct dentry *dentry, void *private)
959{
960 return rpc_populate(dentry,
961 cache_pipefs_files, 0, 3,
962 private);
963}
964
965static void rpc_cachedir_depopulate(struct dentry *dentry)
966{
967 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
968}
969
970struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400971 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400972{
973 return rpc_mkdir_populate(parent, name, umode, NULL,
974 rpc_cachedir_populate, cd);
975}
976
977void rpc_remove_cache_dir(struct dentry *dentry)
978{
979 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * populate the filesystem
984 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700985static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 .alloc_inode = rpc_alloc_inode,
987 .destroy_inode = rpc_destroy_inode,
988 .statfs = simple_statfs,
989};
990
991#define RPCAUTH_GSSMAGIC 0x67596969
992
Trond Myklebustbb156742009-08-09 15:14:21 -0400993/*
994 * We have a single directory with 1 node in it.
995 */
996enum {
997 RPCAUTH_lockd,
998 RPCAUTH_mount,
999 RPCAUTH_nfs,
1000 RPCAUTH_portmap,
1001 RPCAUTH_statd,
1002 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001003 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -04001004 RPCAUTH_RootEOF
1005};
1006
1007static const struct rpc_filelist files[] = {
1008 [RPCAUTH_lockd] = {
1009 .name = "lockd",
1010 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1011 },
1012 [RPCAUTH_mount] = {
1013 .name = "mount",
1014 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1015 },
1016 [RPCAUTH_nfs] = {
1017 .name = "nfs",
1018 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1019 },
1020 [RPCAUTH_portmap] = {
1021 .name = "portmap",
1022 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1023 },
1024 [RPCAUTH_statd] = {
1025 .name = "statd",
1026 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1027 },
1028 [RPCAUTH_nfsd4_cb] = {
1029 .name = "nfsd4_cb",
1030 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1031 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001032 [RPCAUTH_cache] = {
1033 .name = "cache",
1034 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1035 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001036};
1037
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001038/*
1039 * This call can be used only in RPC pipefs mount notification hooks.
1040 */
1041struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1042 const unsigned char *dir_name)
1043{
1044 struct qstr dir = {
1045 .name = dir_name,
1046 .len = strlen(dir_name),
1047 .hash = full_name_hash(dir_name, strlen(dir_name)),
1048 };
1049
1050 return d_lookup(sb->s_root, &dir);
1051}
1052EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1053
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001054void rpc_pipefs_init_net(struct net *net)
1055{
1056 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1057
1058 mutex_init(&sn->pipefs_sb_lock);
1059}
1060
1061/*
1062 * This call will be used for per network namespace operations calls.
1063 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1064 * found. This lock have to be released by rpc_put_sb_net() when all operations
1065 * will be completed.
1066 */
1067struct super_block *rpc_get_sb_net(const struct net *net)
1068{
1069 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1070
1071 mutex_lock(&sn->pipefs_sb_lock);
1072 if (sn->pipefs_sb)
1073 return sn->pipefs_sb;
1074 mutex_unlock(&sn->pipefs_sb_lock);
1075 return NULL;
1076}
1077EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1078
1079void rpc_put_sb_net(const struct net *net)
1080{
1081 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1082
1083 BUG_ON(sn->pipefs_sb == NULL);
1084 mutex_unlock(&sn->pipefs_sb_lock);
1085}
1086EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088static int
1089rpc_fill_super(struct super_block *sb, void *data, int silent)
1090{
1091 struct inode *inode;
1092 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001093 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001094 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001095 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 sb->s_blocksize = PAGE_CACHE_SIZE;
1098 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1099 sb->s_magic = RPCAUTH_GSSMAGIC;
1100 sb->s_op = &s_ops;
1101 sb->s_time_gran = 1;
1102
1103 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1104 if (!inode)
1105 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001106 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (!root) {
1108 iput(inode);
1109 return -ENOMEM;
1110 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001111 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001112 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001113 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1114 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001115 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1116 RPC_PIPEFS_MOUNT,
1117 sb);
1118 if (err)
1119 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001120 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001121 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001123
1124err_depopulate:
1125 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1126 RPC_PIPEFS_UMOUNT,
1127 sb);
1128 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1129 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Al Virofc14f2f2010-07-25 01:48:30 +04001132static struct dentry *
1133rpc_mount(struct file_system_type *fs_type,
1134 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001136 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001139void rpc_kill_sb(struct super_block *sb)
1140{
1141 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001142 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001143
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001144 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001145 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001146 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001147 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001148 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1149 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001150 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1151 RPC_PIPEFS_UMOUNT,
1152 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001153 kill_litter_super(sb);
1154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156static struct file_system_type rpc_pipe_fs_type = {
1157 .owner = THIS_MODULE,
1158 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001159 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001160 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161};
1162
1163static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001164init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
1166 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1167
Christoph Lametera35afb82007-05-16 22:10:57 -07001168 inode_init_once(&rpci->vfs_inode);
1169 rpci->private = NULL;
Stanislav Kinsburskyba9e0972011-12-26 15:43:32 +03001170 rpci->pipe = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173int register_rpc_pipefs(void)
1174{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001175 int err;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001178 sizeof(struct rpc_inode),
1179 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1180 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001181 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (!rpc_inode_cachep)
1183 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001184 err = register_filesystem(&rpc_pipe_fs_type);
1185 if (err) {
1186 kmem_cache_destroy(rpc_inode_cachep);
1187 return err;
1188 }
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return 0;
1191}
1192
1193void unregister_rpc_pipefs(void)
1194{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001195 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 unregister_filesystem(&rpc_pipe_fs_type);
1197}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001198
1199/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1200MODULE_ALIAS("rpc_pipefs");