blob: 9a881138feadb230b101f7ee4031038a9f694fd5 [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
Trond Myklebust9842ef32006-02-01 12:18:44 -050064static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
65 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 wake_up(&rpci->waitq);
78}
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);
David Howells65f27f32006-11-22 14:55:48 +000084 struct rpc_inode *rpci =
85 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050087 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Trond Myklebust9842ef32006-02-01 12:18:44 -050089 spin_lock(&inode->i_lock);
90 if (rpci->ops == NULL) {
91 spin_unlock(&inode->i_lock);
92 return;
93 }
94 destroy_msg = rpci->ops->destroy_msg;
95 if (rpci->nreaders == 0) {
96 list_splice_init(&rpci->pipe, &free_list);
97 rpci->pipelen = 0;
98 }
99 spin_unlock(&inode->i_lock);
100 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Peng Taoc1225152011-09-22 21:50:10 -0400103ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
104 char __user *dst, size_t buflen)
105{
106 char *data = (char *)msg->data + msg->copied;
107 size_t mlen = min(msg->len - msg->copied, buflen);
108 unsigned long left;
109
110 left = copy_to_user(dst, data, mlen);
111 if (left == mlen) {
112 msg->errno = -EFAULT;
113 return -EFAULT;
114 }
115
116 mlen -= left;
117 msg->copied += mlen;
118 msg->errno = 0;
119 return mlen;
120}
121EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
122
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500123/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800124 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500125 * @inode: inode of upcall pipe on which to queue given message
126 * @msg: message to queue
127 *
128 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
129 * A userspace process may then later read the upcall by performing a
130 * read on an open file for this inode. It is up to the caller to
131 * initialize the fields of @msg (other than @msg->list) appropriately.
132 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133int
134rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
135{
136 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400137 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Trond Myklebust9842ef32006-02-01 12:18:44 -0500139 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400140 if (rpci->ops == NULL)
141 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (rpci->nreaders) {
143 list_add_tail(&msg->list, &rpci->pipe);
144 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400145 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
147 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500148 queue_delayed_work(rpciod_workqueue,
149 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 RPC_UPCALL_TIMEOUT);
151 list_add_tail(&msg->list, &rpci->pipe);
152 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400153 res = 0;
154 }
155out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500156 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 wake_up(&rpci->waitq);
158 return res;
159}
Trond Myklebust468039e2008-12-23 15:21:31 -0500160EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Trond Myklebust6070fe62005-10-27 22:12:46 -0400162static inline void
163rpc_inode_setowner(struct inode *inode, void *private)
164{
165 RPC_I(inode)->private = private;
166}
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void
169rpc_close_pipes(struct inode *inode)
170{
171 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebustb693ba42009-08-09 15:14:15 -0400172 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500173 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800175 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500176 ops = rpci->ops;
177 if (ops != NULL) {
178 LIST_HEAD(free_list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500179 spin_lock(&inode->i_lock);
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500180 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500182 list_splice_init(&rpci->in_upcall, &free_list);
183 list_splice_init(&rpci->pipe, &free_list);
184 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500186 spin_unlock(&inode->i_lock);
187 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
188 rpci->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500189 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500190 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400191 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400193 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800194 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197static struct inode *
198rpc_alloc_inode(struct super_block *sb)
199{
200 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800201 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!rpci)
203 return NULL;
204 return &rpci->vfs_inode;
205}
206
207static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100208rpc_i_callback(struct rcu_head *head)
209{
210 struct inode *inode = container_of(head, struct inode, i_rcu);
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{
223 struct rpc_inode *rpci = RPC_I(inode);
\"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);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500228 if (rpci->ops == NULL)
229 goto out;
230 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
231 if (first_open && rpci->ops->open_pipe) {
232 res = rpci->ops->open_pipe(inode);
233 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)
237 rpci->nreaders++;
238 if (filp->f_mode & FMODE_WRITE)
239 rpci->nwriters++;
240 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{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100249 struct rpc_inode *rpci = RPC_I(inode);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (rpci->ops == NULL)
255 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) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500258 spin_lock(&inode->i_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);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500261 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 rpci->ops->destroy_msg(msg);
263 }
264 if (filp->f_mode & FMODE_WRITE)
265 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500266 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500268 if (rpci->nreaders == 0) {
269 LIST_HEAD(free_list);
270 spin_lock(&inode->i_lock);
271 list_splice_init(&rpci->pipe, &free_list);
272 rpci->pipelen = 0;
273 spin_unlock(&inode->i_lock);
274 rpc_purge_list(rpci, &free_list,
275 rpci->ops->destroy_msg, -EAGAIN);
276 }
277 }
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500278 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
279 if (last_close && rpci->ops->release_pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 rpci->ops->release_pipe(inode);
281out:
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 struct rpc_inode *rpci = RPC_I(inode);
291 struct rpc_pipe_msg *msg;
292 int res = 0;
293
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800294 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (rpci->ops == NULL) {
296 res = -EPIPE;
297 goto out_unlock;
298 }
299 msg = filp->private_data;
300 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500301 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (!list_empty(&rpci->pipe)) {
303 msg = list_entry(rpci->pipe.next,
304 struct rpc_pipe_msg,
305 list);
306 list_move(&msg->list, &rpci->in_upcall);
307 rpci->pipelen -= msg->len;
308 filp->private_data = msg;
309 msg->copied = 0;
310 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500311 spin_unlock(&inode->i_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 */
316 res = rpci->ops->upcall(filp, msg, buf, len);
317 if (res < 0 || msg->len == msg->copied) {
318 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500319 spin_lock(&inode->i_lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400320 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500321 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 rpci->ops->destroy_msg(msg);
323 }
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 struct rpc_inode *rpci = RPC_I(inode);
334 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;
338 if (rpci->ops != NULL)
339 res = rpci->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{
347 struct rpc_inode *rpci;
348 unsigned int mask = 0;
349
Josef Sipek303b46b2006-12-08 02:37:42 -0800350 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 poll_wait(filp, &rpci->waitq, wait);
352
353 mask = POLLOUT | POLLWRNORM;
354 if (rpci->ops == NULL)
355 mask |= POLLERR | POLLHUP;
J. Bruce Fieldseda4f9b2007-11-06 13:05:36 -0500356 if (filp->private_data || !list_empty(&rpci->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 mask |= POLLIN | POLLRDNORM;
358 return mask;
359}
360
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200361static long
362rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200364 struct inode *inode = filp->f_path.dentry->d_inode;
365 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 int len;
367
368 switch (cmd) {
369 case FIONREAD:
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200370 spin_lock(&inode->i_lock);
371 if (rpci->ops == NULL) {
372 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 len = rpci->pipelen;
376 if (filp->private_data) {
377 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700378 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 len += msg->len - msg->copied;
380 }
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200381 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return put_user(len, (int __user *)arg);
383 default:
384 return -EINVAL;
385 }
386}
387
Arjan van de Venda7071d2007-02-12 00:55:36 -0800388static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .owner = THIS_MODULE,
390 .llseek = no_llseek,
391 .read = rpc_pipe_read,
392 .write = rpc_pipe_write,
393 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200394 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .open = rpc_pipe_open,
396 .release = rpc_pipe_release,
397};
398
399static int
400rpc_show_info(struct seq_file *m, void *v)
401{
402 struct rpc_clnt *clnt = m->private;
403
404 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
405 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
406 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400407 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
408 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400409 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
413static int
414rpc_info_open(struct inode *inode, struct file *file)
415{
Trond Myklebust006abe82010-09-12 19:55:25 -0400416 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 int ret = single_open(file, rpc_show_info, NULL);
418
419 if (!ret) {
420 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400421
422 spin_lock(&file->f_path.dentry->d_lock);
423 if (!d_unhashed(file->f_path.dentry))
424 clnt = RPC_I(inode)->private;
425 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 m->private = clnt;
428 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400429 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 single_release(inode, file);
431 ret = -EINVAL;
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434 return ret;
435}
436
437static int
438rpc_info_release(struct inode *inode, struct file *file)
439{
440 struct seq_file *m = file->private_data;
441 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
442
443 if (clnt)
444 rpc_release_client(clnt);
445 return single_release(inode, file);
446}
447
Arjan van de Venda7071d2007-02-12 00:55:36 -0800448static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 .owner = THIS_MODULE,
450 .open = rpc_info_open,
451 .read = seq_read,
452 .llseek = seq_lseek,
453 .release = rpc_info_release,
454};
455
456
457/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 * Description of fs contents.
459 */
460struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400461 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800462 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400463 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464};
465
Trond Myklebust54281542006-03-20 13:44:49 -0500466struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Trond Myklebust54281542006-03-20 13:44:49 -0500468 int err;
469
Al Virofc14f2f2010-07-25 01:48:30 +0400470 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500471 if (err != 0)
472 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400473 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400475EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Trond Myklebust54281542006-03-20 13:44:49 -0500477void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Al Virofc14f2f2010-07-25 01:48:30 +0400479 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400481EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Nick Pigginfe15ce42011-01-07 17:49:23 +1100483static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400484{
485 return 1;
486}
487
Al Viro3ba13d12009-02-20 06:02:22 +0000488static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400489 .d_delete = rpc_delete_dentry,
490};
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400493rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct inode *inode = new_inode(sb);
496 if (!inode)
497 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400498 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000501 switch (mode & S_IFMT) {
502 case S_IFDIR:
503 inode->i_fop = &simple_dir_operations;
504 inode->i_op = &simple_dir_inode_operations;
505 inc_nlink(inode);
506 default:
507 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 return inode;
510}
511
Trond Myklebust75898062009-08-09 15:14:17 -0400512static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
513 umode_t mode,
514 const struct file_operations *i_fop,
515 void *private)
516{
517 struct inode *inode;
518
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000519 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400520 inode = rpc_get_inode(dir->i_sb, mode);
521 if (!inode)
522 goto out_err;
523 inode->i_ino = iunique(dir->i_sb, 100);
524 if (i_fop)
525 inode->i_fop = i_fop;
526 if (private)
527 rpc_inode_setowner(inode, private);
528 d_add(dentry, inode);
529 return 0;
530out_err:
531 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
532 __FILE__, __func__, dentry->d_name.name);
533 dput(dentry);
534 return -ENOMEM;
535}
536
Trond Myklebustac6fece2009-08-09 15:14:20 -0400537static int __rpc_create(struct inode *dir, struct dentry *dentry,
538 umode_t mode,
539 const struct file_operations *i_fop,
540 void *private)
541{
542 int err;
543
544 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
545 if (err)
546 return err;
547 fsnotify_create(dir, dentry);
548 return 0;
549}
550
Trond Myklebust75898062009-08-09 15:14:17 -0400551static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
552 umode_t mode,
553 const struct file_operations *i_fop,
554 void *private)
555{
556 int err;
557
558 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
559 if (err)
560 return err;
561 inc_nlink(dir);
562 fsnotify_mkdir(dir, dentry);
563 return 0;
564}
565
566static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
567 umode_t mode,
568 const struct file_operations *i_fop,
569 void *private,
570 const struct rpc_pipe_ops *ops,
571 int flags)
572{
573 struct rpc_inode *rpci;
574 int err;
575
576 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
577 if (err)
578 return err;
579 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400580 rpci->private = private;
581 rpci->flags = flags;
582 rpci->ops = ops;
583 fsnotify_create(dir, dentry);
584 return 0;
585}
586
Trond Myklebustac6fece2009-08-09 15:14:20 -0400587static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
588{
589 int ret;
590
591 dget(dentry);
592 ret = simple_rmdir(dir, dentry);
593 d_delete(dentry);
594 dput(dentry);
595 return ret;
596}
597
Trond Myklebust810d90b2009-08-09 15:14:18 -0400598static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
599{
600 int ret;
601
602 dget(dentry);
603 ret = simple_unlink(dir, dentry);
604 d_delete(dentry);
605 dput(dentry);
606 return ret;
607}
608
609static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
610{
611 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400612
Trond Myklebust810d90b2009-08-09 15:14:18 -0400613 rpc_close_pipes(inode);
614 return __rpc_unlink(dir, dentry);
615}
616
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400617static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
618 struct qstr *name)
619{
620 struct dentry *dentry;
621
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300622 dentry = d_lookup(parent, name);
623 if (!dentry) {
624 dentry = d_alloc(parent, name);
625 if (!dentry)
626 return ERR_PTR(-ENOMEM);
627 }
628 if (dentry->d_inode == NULL) {
629 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe12010-03-21 12:10:34 -0400630 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300631 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400632 dput(dentry);
633 return ERR_PTR(-EEXIST);
634}
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636/*
637 * FIXME: This probably has races.
638 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400639static void __rpc_depopulate(struct dentry *parent,
640 const struct rpc_filelist *files,
641 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400644 struct dentry *dentry;
645 struct qstr name;
646 int i;
647
648 for (i = start; i < eof; i++) {
649 name.name = files[i].name;
650 name.len = strlen(files[i].name);
651 name.hash = full_name_hash(name.name, name.len);
652 dentry = d_lookup(parent, &name);
653
654 if (dentry == NULL)
655 continue;
656 if (dentry->d_inode == NULL)
657 goto next;
658 switch (dentry->d_inode->i_mode & S_IFMT) {
659 default:
660 BUG();
661 case S_IFREG:
662 __rpc_unlink(dir, dentry);
663 break;
664 case S_IFDIR:
665 __rpc_rmdir(dir, dentry);
666 }
667next:
668 dput(dentry);
669 }
670}
671
672static void rpc_depopulate(struct dentry *parent,
673 const struct rpc_filelist *files,
674 int start, int eof)
675{
676 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Arjan van de Venc6573c22006-07-03 00:25:16 -0700678 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400679 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800680 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Trond Myklebustac6fece2009-08-09 15:14:20 -0400683static int rpc_populate(struct dentry *parent,
684 const struct rpc_filelist *files,
685 int start, int eof,
686 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400688 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400690 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800692 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400694 struct qstr q;
695
696 q.name = files[i].name;
697 q.len = strlen(files[i].name);
698 q.hash = full_name_hash(q.name, q.len);
699 dentry = __rpc_lookup_create_exclusive(parent, &q);
700 err = PTR_ERR(dentry);
701 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400703 switch (files[i].mode & S_IFMT) {
704 default:
705 BUG();
706 case S_IFREG:
707 err = __rpc_create(dir, dentry,
708 files[i].mode,
709 files[i].i_fop,
710 private);
711 break;
712 case S_IFDIR:
713 err = __rpc_mkdir(dir, dentry,
714 files[i].mode,
715 NULL,
716 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400718 if (err != 0)
719 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800721 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return 0;
723out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400724 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800725 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800727 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400728 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400729}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100730
Trond Myklebuste57aed72009-08-09 15:14:26 -0400731static struct dentry *rpc_mkdir_populate(struct dentry *parent,
732 struct qstr *name, umode_t mode, void *private,
733 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400734{
Trond Myklebustf1345852005-09-23 11:08:25 -0400735 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400736 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400737 int error;
738
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400739 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
740 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400741 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400742 goto out;
743 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400744 if (error != 0)
745 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400746 if (populate != NULL) {
747 error = populate(dentry, args_populate);
748 if (error)
749 goto err_rmdir;
750 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400751out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800752 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400753 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400754err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400755 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400756out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400757 dentry = ERR_PTR(error);
758 goto out;
759}
760
Trond Myklebuste57aed72009-08-09 15:14:26 -0400761static int rpc_rmdir_depopulate(struct dentry *dentry,
762 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400763{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700764 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400765 struct inode *dir;
766 int error;
767
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700768 parent = dget_parent(dentry);
769 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700770 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400771 if (depopulate != NULL)
772 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400773 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800774 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700775 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400776 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500779/**
780 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
781 * @parent: dentry of directory to create new "pipe" in
782 * @name: name of pipe
783 * @private: private data to associate with the pipe, for the caller's use
784 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500785 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800786 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500787 *
788 * Data is made available for userspace to read by calls to
789 * rpc_queue_upcall(). The actual reads will result in calls to
790 * @ops->upcall, which will be called with the file pointer,
791 * message, and userspace buffer to copy to.
792 *
793 * Writes can come at any time, and do not necessarily have to be
794 * responses to upcalls. They will result in calls to @msg->downcall.
795 *
796 * The @private argument passed here will be available to all these methods
797 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
798 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400799struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
800 void *private, const struct rpc_pipe_ops *ops,
801 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400804 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400805 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400806 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400807 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400808
809 if (ops->upcall == NULL)
810 umode &= ~S_IRUGO;
811 if (ops->downcall == NULL)
812 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400814 q.name = name;
815 q.len = strlen(name);
816 q.hash = full_name_hash(q.name, q.len),
817
818 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300819 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400821 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400822 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400823 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400824 if (err)
825 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400826out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800827 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400828 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400829out_err:
830 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400831 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800832 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400833 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400834 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
Trond Myklebust468039e2008-12-23 15:21:31 -0500836EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500838/**
839 * rpc_unlink - remove a pipe
840 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
841 *
842 * After this call, lookups will no longer find the pipe, and any
843 * attempts to read or write using preexisting opens of the pipe will
844 * return -EPIPE.
845 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400846int
Trond Myklebust5d674762006-07-31 14:11:48 -0700847rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Trond Myklebust5d674762006-07-31 14:11:48 -0700849 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400850 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700851 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Trond Myklebust5d674762006-07-31 14:11:48 -0700853 parent = dget_parent(dentry);
854 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700855 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400856 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800857 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700858 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400859 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
Trond Myklebust468039e2008-12-23 15:21:31 -0500861EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Trond Myklebuste57aed72009-08-09 15:14:26 -0400863enum {
864 RPCAUTH_info,
865 RPCAUTH_EOF
866};
867
868static const struct rpc_filelist authfiles[] = {
869 [RPCAUTH_info] = {
870 .name = "info",
871 .i_fop = &rpc_info_operations,
872 .mode = S_IFREG | S_IRUSR,
873 },
874};
875
876static int rpc_clntdir_populate(struct dentry *dentry, void *private)
877{
878 return rpc_populate(dentry,
879 authfiles, RPCAUTH_info, RPCAUTH_EOF,
880 private);
881}
882
883static void rpc_clntdir_depopulate(struct dentry *dentry)
884{
885 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
886}
887
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400888/**
889 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400890 * @dentry: dentry from the rpc_pipefs root to the new directory
891 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400892 * @rpc_client: rpc client to associate with this directory
893 *
894 * This creates a directory at the given @path associated with
895 * @rpc_clnt, which will contain a file named "info" with some basic
896 * information about the client, together with any "pipes" that may
897 * later be created using rpc_mkpipe().
898 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400899struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400900 struct qstr *name,
901 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400902{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400903 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
904 rpc_clntdir_populate, rpc_client);
905}
906
907/**
908 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
909 * @dentry: directory to remove
910 */
911int rpc_remove_client_dir(struct dentry *dentry)
912{
913 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400914}
915
Trond Myklebust8854e822009-08-09 15:14:30 -0400916static const struct rpc_filelist cache_pipefs_files[3] = {
917 [0] = {
918 .name = "channel",
919 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400920 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400921 },
922 [1] = {
923 .name = "content",
924 .i_fop = &content_file_operations_pipefs,
925 .mode = S_IFREG|S_IRUSR,
926 },
927 [2] = {
928 .name = "flush",
929 .i_fop = &cache_flush_operations_pipefs,
930 .mode = S_IFREG|S_IRUSR|S_IWUSR,
931 },
932};
933
934static int rpc_cachedir_populate(struct dentry *dentry, void *private)
935{
936 return rpc_populate(dentry,
937 cache_pipefs_files, 0, 3,
938 private);
939}
940
941static void rpc_cachedir_depopulate(struct dentry *dentry)
942{
943 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
944}
945
946struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400947 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400948{
949 return rpc_mkdir_populate(parent, name, umode, NULL,
950 rpc_cachedir_populate, cd);
951}
952
953void rpc_remove_cache_dir(struct dentry *dentry)
954{
955 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
956}
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958/*
959 * populate the filesystem
960 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700961static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 .alloc_inode = rpc_alloc_inode,
963 .destroy_inode = rpc_destroy_inode,
964 .statfs = simple_statfs,
965};
966
967#define RPCAUTH_GSSMAGIC 0x67596969
968
Trond Myklebustbb156742009-08-09 15:14:21 -0400969/*
970 * We have a single directory with 1 node in it.
971 */
972enum {
973 RPCAUTH_lockd,
974 RPCAUTH_mount,
975 RPCAUTH_nfs,
976 RPCAUTH_portmap,
977 RPCAUTH_statd,
978 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400979 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -0400980 RPCAUTH_RootEOF
981};
982
983static const struct rpc_filelist files[] = {
984 [RPCAUTH_lockd] = {
985 .name = "lockd",
986 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
987 },
988 [RPCAUTH_mount] = {
989 .name = "mount",
990 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
991 },
992 [RPCAUTH_nfs] = {
993 .name = "nfs",
994 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
995 },
996 [RPCAUTH_portmap] = {
997 .name = "portmap",
998 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
999 },
1000 [RPCAUTH_statd] = {
1001 .name = "statd",
1002 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1003 },
1004 [RPCAUTH_nfsd4_cb] = {
1005 .name = "nfsd4_cb",
1006 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1007 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001008 [RPCAUTH_cache] = {
1009 .name = "cache",
1010 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1011 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001012};
1013
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001014/*
1015 * This call can be used only in RPC pipefs mount notification hooks.
1016 */
1017struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1018 const unsigned char *dir_name)
1019{
1020 struct qstr dir = {
1021 .name = dir_name,
1022 .len = strlen(dir_name),
1023 .hash = full_name_hash(dir_name, strlen(dir_name)),
1024 };
1025
1026 return d_lookup(sb->s_root, &dir);
1027}
1028EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1029
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001030void rpc_pipefs_init_net(struct net *net)
1031{
1032 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1033
1034 mutex_init(&sn->pipefs_sb_lock);
1035}
1036
1037/*
1038 * This call will be used for per network namespace operations calls.
1039 * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1040 * found. This lock have to be released by rpc_put_sb_net() when all operations
1041 * will be completed.
1042 */
1043struct super_block *rpc_get_sb_net(const struct net *net)
1044{
1045 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1046
1047 mutex_lock(&sn->pipefs_sb_lock);
1048 if (sn->pipefs_sb)
1049 return sn->pipefs_sb;
1050 mutex_unlock(&sn->pipefs_sb_lock);
1051 return NULL;
1052}
1053EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1054
1055void rpc_put_sb_net(const struct net *net)
1056{
1057 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1058
1059 BUG_ON(sn->pipefs_sb == NULL);
1060 mutex_unlock(&sn->pipefs_sb_lock);
1061}
1062EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static int
1065rpc_fill_super(struct super_block *sb, void *data, int silent)
1066{
1067 struct inode *inode;
1068 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001069 struct net *net = data;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001070 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 sb->s_blocksize = PAGE_CACHE_SIZE;
1074 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1075 sb->s_magic = RPCAUTH_GSSMAGIC;
1076 sb->s_op = &s_ops;
1077 sb->s_time_gran = 1;
1078
1079 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1080 if (!inode)
1081 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001082 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!root) {
1084 iput(inode);
1085 return -ENOMEM;
1086 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001087 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001088 return -ENOMEM;
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001089 dprintk("RPC: sending pipefs MOUNT notification for net %p%s\n", net,
1090 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001091 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1092 RPC_PIPEFS_MOUNT,
1093 sb);
1094 if (err)
1095 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001096 sb->s_fs_info = get_net(net);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001097 sn->pipefs_sb = sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001099
1100err_depopulate:
1101 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1102 RPC_PIPEFS_UMOUNT,
1103 sb);
1104 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1105 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Al Virofc14f2f2010-07-25 01:48:30 +04001108static struct dentry *
1109rpc_mount(struct file_system_type *fs_type,
1110 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001112 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001115void rpc_kill_sb(struct super_block *sb)
1116{
1117 struct net *net = sb->s_fs_info;
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001118 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001119
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001120 mutex_lock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky90c4e822011-12-26 15:39:30 +03001121 sn->pipefs_sb = NULL;
Stanislav Kinsburskyc21a5882011-12-26 15:39:39 +03001122 mutex_unlock(&sn->pipefs_sb_lock);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001123 put_net(net);
Stanislav Kinsburskyefc46bf2011-12-26 15:39:47 +03001124 dprintk("RPC: sending pipefs UMOUNT notification for net %p%s\n", net,
1125 NET_NAME(net));
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001126 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1127 RPC_PIPEFS_UMOUNT,
1128 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001129 kill_litter_super(sb);
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132static struct file_system_type rpc_pipe_fs_type = {
1133 .owner = THIS_MODULE,
1134 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001135 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001136 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137};
1138
1139static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001140init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1143
Christoph Lametera35afb82007-05-16 22:10:57 -07001144 inode_init_once(&rpci->vfs_inode);
1145 rpci->private = NULL;
1146 rpci->nreaders = 0;
1147 rpci->nwriters = 0;
1148 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -04001149 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -07001150 INIT_LIST_HEAD(&rpci->pipe);
1151 rpci->pipelen = 0;
1152 init_waitqueue_head(&rpci->waitq);
1153 INIT_DELAYED_WORK(&rpci->queue_timeout,
1154 rpc_timeout_upcall_queue);
1155 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158int register_rpc_pipefs(void)
1159{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001160 int err;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001163 sizeof(struct rpc_inode),
1164 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1165 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001166 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!rpc_inode_cachep)
1168 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001169 err = register_filesystem(&rpc_pipe_fs_type);
1170 if (err) {
1171 kmem_cache_destroy(rpc_inode_cachep);
1172 return err;
1173 }
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return 0;
1176}
1177
1178void unregister_rpc_pipefs(void)
1179{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001180 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unregister_filesystem(&rpc_pipe_fs_type);
1182}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001183
1184/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1185MODULE_ALIAS("rpc_pipefs");