blob: 6f295e6c12a0f98a8d98f06965ef15bc45c655d3 [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
Al Virofc14f2f2010-07-25 01:48:30 +040036static struct vfsmount *rpc_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static int rpc_mount_count;
38
39static struct file_system_type rpc_pipe_fs_type;
40
41
Christoph Lametere18b8902006-12-06 20:33:20 -080042static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#define RPC_UPCALL_TIMEOUT (30*HZ)
45
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +030046static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
47
48int rpc_pipefs_notifier_register(struct notifier_block *nb)
49{
50 return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
51}
52EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
53
54void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
55{
56 blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
57}
58EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
59
Trond Myklebust9842ef32006-02-01 12:18:44 -050060static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
61 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050062{
63 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050064
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 if (list_empty(head))
66 return;
67 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050068 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040069 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050070 msg->errno = err;
71 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050072 } while (!list_empty(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 wake_up(&rpci->waitq);
74}
75
76static void
David Howells65f27f32006-11-22 14:55:48 +000077rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Trond Myklebust9842ef32006-02-01 12:18:44 -050079 LIST_HEAD(free_list);
David Howells65f27f32006-11-22 14:55:48 +000080 struct rpc_inode *rpci =
81 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050083 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Trond Myklebust9842ef32006-02-01 12:18:44 -050085 spin_lock(&inode->i_lock);
86 if (rpci->ops == NULL) {
87 spin_unlock(&inode->i_lock);
88 return;
89 }
90 destroy_msg = rpci->ops->destroy_msg;
91 if (rpci->nreaders == 0) {
92 list_splice_init(&rpci->pipe, &free_list);
93 rpci->pipelen = 0;
94 }
95 spin_unlock(&inode->i_lock);
96 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
Peng Taoc1225152011-09-22 21:50:10 -040099ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
100 char __user *dst, size_t buflen)
101{
102 char *data = (char *)msg->data + msg->copied;
103 size_t mlen = min(msg->len - msg->copied, buflen);
104 unsigned long left;
105
106 left = copy_to_user(dst, data, mlen);
107 if (left == mlen) {
108 msg->errno = -EFAULT;
109 return -EFAULT;
110 }
111
112 mlen -= left;
113 msg->copied += mlen;
114 msg->errno = 0;
115 return mlen;
116}
117EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
118
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500119/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800120 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500121 * @inode: inode of upcall pipe on which to queue given message
122 * @msg: message to queue
123 *
124 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
125 * A userspace process may then later read the upcall by performing a
126 * read on an open file for this inode. It is up to the caller to
127 * initialize the fields of @msg (other than @msg->list) appropriately.
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129int
130rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
131{
132 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400133 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Trond Myklebust9842ef32006-02-01 12:18:44 -0500135 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400136 if (rpci->ops == NULL)
137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 if (rpci->nreaders) {
139 list_add_tail(&msg->list, &rpci->pipe);
140 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400141 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
143 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500144 queue_delayed_work(rpciod_workqueue,
145 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 RPC_UPCALL_TIMEOUT);
147 list_add_tail(&msg->list, &rpci->pipe);
148 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400149 res = 0;
150 }
151out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500152 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 wake_up(&rpci->waitq);
154 return res;
155}
Trond Myklebust468039e2008-12-23 15:21:31 -0500156EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Trond Myklebust6070fe62005-10-27 22:12:46 -0400158static inline void
159rpc_inode_setowner(struct inode *inode, void *private)
160{
161 RPC_I(inode)->private = private;
162}
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void
165rpc_close_pipes(struct inode *inode)
166{
167 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebustb693ba42009-08-09 15:14:15 -0400168 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500169 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800171 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500172 ops = rpci->ops;
173 if (ops != NULL) {
174 LIST_HEAD(free_list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500175 spin_lock(&inode->i_lock);
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500176 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500178 list_splice_init(&rpci->in_upcall, &free_list);
179 list_splice_init(&rpci->pipe, &free_list);
180 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500182 spin_unlock(&inode->i_lock);
183 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
184 rpci->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500185 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500186 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400187 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400189 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800190 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static struct inode *
194rpc_alloc_inode(struct super_block *sb)
195{
196 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800197 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (!rpci)
199 return NULL;
200 return &rpci->vfs_inode;
201}
202
203static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100204rpc_i_callback(struct rcu_head *head)
205{
206 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100207 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
208}
209
210static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211rpc_destroy_inode(struct inode *inode)
212{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100213 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static int
217rpc_pipe_open(struct inode *inode, struct file *filp)
218{
219 struct rpc_inode *rpci = RPC_I(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500220 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 int res = -ENXIO;
222
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800223 mutex_lock(&inode->i_mutex);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500224 if (rpci->ops == NULL)
225 goto out;
226 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
227 if (first_open && rpci->ops->open_pipe) {
228 res = rpci->ops->open_pipe(inode);
229 if (res)
230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500232 if (filp->f_mode & FMODE_READ)
233 rpci->nreaders++;
234 if (filp->f_mode & FMODE_WRITE)
235 rpci->nwriters++;
236 res = 0;
237out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800238 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return res;
240}
241
242static int
243rpc_pipe_release(struct inode *inode, struct file *filp)
244{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100245 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500247 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800249 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (rpci->ops == NULL)
251 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700252 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (msg != NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500254 spin_lock(&inode->i_lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500255 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400256 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500257 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rpci->ops->destroy_msg(msg);
259 }
260 if (filp->f_mode & FMODE_WRITE)
261 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500262 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500264 if (rpci->nreaders == 0) {
265 LIST_HEAD(free_list);
266 spin_lock(&inode->i_lock);
267 list_splice_init(&rpci->pipe, &free_list);
268 rpci->pipelen = 0;
269 spin_unlock(&inode->i_lock);
270 rpc_purge_list(rpci, &free_list,
271 rpci->ops->destroy_msg, -EAGAIN);
272 }
273 }
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500274 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
275 if (last_close && rpci->ops->release_pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 rpci->ops->release_pipe(inode);
277out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800278 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return 0;
280}
281
282static ssize_t
283rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
284{
Josef Sipek303b46b2006-12-08 02:37:42 -0800285 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct rpc_inode *rpci = RPC_I(inode);
287 struct rpc_pipe_msg *msg;
288 int res = 0;
289
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800290 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (rpci->ops == NULL) {
292 res = -EPIPE;
293 goto out_unlock;
294 }
295 msg = filp->private_data;
296 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500297 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!list_empty(&rpci->pipe)) {
299 msg = list_entry(rpci->pipe.next,
300 struct rpc_pipe_msg,
301 list);
302 list_move(&msg->list, &rpci->in_upcall);
303 rpci->pipelen -= msg->len;
304 filp->private_data = msg;
305 msg->copied = 0;
306 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500307 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (msg == NULL)
309 goto out_unlock;
310 }
311 /* NOTE: it is up to the callback to update msg->copied */
312 res = rpci->ops->upcall(filp, msg, buf, len);
313 if (res < 0 || msg->len == msg->copied) {
314 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500315 spin_lock(&inode->i_lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400316 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500317 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 rpci->ops->destroy_msg(msg);
319 }
320out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800321 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return res;
323}
324
325static ssize_t
326rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
327{
Josef Sipek303b46b2006-12-08 02:37:42 -0800328 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct rpc_inode *rpci = RPC_I(inode);
330 int res;
331
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800332 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 res = -EPIPE;
334 if (rpci->ops != NULL)
335 res = rpci->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800336 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return res;
338}
339
340static unsigned int
341rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
342{
343 struct rpc_inode *rpci;
344 unsigned int mask = 0;
345
Josef Sipek303b46b2006-12-08 02:37:42 -0800346 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 poll_wait(filp, &rpci->waitq, wait);
348
349 mask = POLLOUT | POLLWRNORM;
350 if (rpci->ops == NULL)
351 mask |= POLLERR | POLLHUP;
J. Bruce Fieldseda4f9b2007-11-06 13:05:36 -0500352 if (filp->private_data || !list_empty(&rpci->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 mask |= POLLIN | POLLRDNORM;
354 return mask;
355}
356
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200357static long
358rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200360 struct inode *inode = filp->f_path.dentry->d_inode;
361 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 int len;
363
364 switch (cmd) {
365 case FIONREAD:
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200366 spin_lock(&inode->i_lock);
367 if (rpci->ops == NULL) {
368 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 len = rpci->pipelen;
372 if (filp->private_data) {
373 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700374 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 len += msg->len - msg->copied;
376 }
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200377 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return put_user(len, (int __user *)arg);
379 default:
380 return -EINVAL;
381 }
382}
383
Arjan van de Venda7071d2007-02-12 00:55:36 -0800384static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 .owner = THIS_MODULE,
386 .llseek = no_llseek,
387 .read = rpc_pipe_read,
388 .write = rpc_pipe_write,
389 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200390 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 .open = rpc_pipe_open,
392 .release = rpc_pipe_release,
393};
394
395static int
396rpc_show_info(struct seq_file *m, void *v)
397{
398 struct rpc_clnt *clnt = m->private;
399
400 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
401 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
402 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400403 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
404 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400405 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
407}
408
409static int
410rpc_info_open(struct inode *inode, struct file *file)
411{
Trond Myklebust006abe82010-09-12 19:55:25 -0400412 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 int ret = single_open(file, rpc_show_info, NULL);
414
415 if (!ret) {
416 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400417
418 spin_lock(&file->f_path.dentry->d_lock);
419 if (!d_unhashed(file->f_path.dentry))
420 clnt = RPC_I(inode)->private;
421 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
422 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 m->private = clnt;
424 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400425 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 single_release(inode, file);
427 ret = -EINVAL;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430 return ret;
431}
432
433static int
434rpc_info_release(struct inode *inode, struct file *file)
435{
436 struct seq_file *m = file->private_data;
437 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
438
439 if (clnt)
440 rpc_release_client(clnt);
441 return single_release(inode, file);
442}
443
Arjan van de Venda7071d2007-02-12 00:55:36 -0800444static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .owner = THIS_MODULE,
446 .open = rpc_info_open,
447 .read = seq_read,
448 .llseek = seq_lseek,
449 .release = rpc_info_release,
450};
451
452
453/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * Description of fs contents.
455 */
456struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400457 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800458 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400459 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460};
461
Trond Myklebust54281542006-03-20 13:44:49 -0500462struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Trond Myklebust54281542006-03-20 13:44:49 -0500464 int err;
465
Al Virofc14f2f2010-07-25 01:48:30 +0400466 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500467 if (err != 0)
468 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400469 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400471EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Trond Myklebust54281542006-03-20 13:44:49 -0500473void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Al Virofc14f2f2010-07-25 01:48:30 +0400475 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400477EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Nick Pigginfe15ce42011-01-07 17:49:23 +1100479static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400480{
481 return 1;
482}
483
Al Viro3ba13d12009-02-20 06:02:22 +0000484static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400485 .d_delete = rpc_delete_dentry,
486};
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400489rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 struct inode *inode = new_inode(sb);
492 if (!inode)
493 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400494 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000497 switch (mode & S_IFMT) {
498 case S_IFDIR:
499 inode->i_fop = &simple_dir_operations;
500 inode->i_op = &simple_dir_inode_operations;
501 inc_nlink(inode);
502 default:
503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return inode;
506}
507
Trond Myklebust75898062009-08-09 15:14:17 -0400508static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
509 umode_t mode,
510 const struct file_operations *i_fop,
511 void *private)
512{
513 struct inode *inode;
514
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000515 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400516 inode = rpc_get_inode(dir->i_sb, mode);
517 if (!inode)
518 goto out_err;
519 inode->i_ino = iunique(dir->i_sb, 100);
520 if (i_fop)
521 inode->i_fop = i_fop;
522 if (private)
523 rpc_inode_setowner(inode, private);
524 d_add(dentry, inode);
525 return 0;
526out_err:
527 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
528 __FILE__, __func__, dentry->d_name.name);
529 dput(dentry);
530 return -ENOMEM;
531}
532
Trond Myklebustac6fece2009-08-09 15:14:20 -0400533static int __rpc_create(struct inode *dir, struct dentry *dentry,
534 umode_t mode,
535 const struct file_operations *i_fop,
536 void *private)
537{
538 int err;
539
540 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
541 if (err)
542 return err;
543 fsnotify_create(dir, dentry);
544 return 0;
545}
546
Trond Myklebust75898062009-08-09 15:14:17 -0400547static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
548 umode_t mode,
549 const struct file_operations *i_fop,
550 void *private)
551{
552 int err;
553
554 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
555 if (err)
556 return err;
557 inc_nlink(dir);
558 fsnotify_mkdir(dir, dentry);
559 return 0;
560}
561
562static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
563 umode_t mode,
564 const struct file_operations *i_fop,
565 void *private,
566 const struct rpc_pipe_ops *ops,
567 int flags)
568{
569 struct rpc_inode *rpci;
570 int err;
571
572 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
573 if (err)
574 return err;
575 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400576 rpci->private = private;
577 rpci->flags = flags;
578 rpci->ops = ops;
579 fsnotify_create(dir, dentry);
580 return 0;
581}
582
Trond Myklebustac6fece2009-08-09 15:14:20 -0400583static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
584{
585 int ret;
586
587 dget(dentry);
588 ret = simple_rmdir(dir, dentry);
589 d_delete(dentry);
590 dput(dentry);
591 return ret;
592}
593
Trond Myklebust810d90b2009-08-09 15:14:18 -0400594static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
595{
596 int ret;
597
598 dget(dentry);
599 ret = simple_unlink(dir, dentry);
600 d_delete(dentry);
601 dput(dentry);
602 return ret;
603}
604
605static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
606{
607 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400608
Trond Myklebust810d90b2009-08-09 15:14:18 -0400609 rpc_close_pipes(inode);
610 return __rpc_unlink(dir, dentry);
611}
612
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400613static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
614 struct qstr *name)
615{
616 struct dentry *dentry;
617
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300618 dentry = d_lookup(parent, name);
619 if (!dentry) {
620 dentry = d_alloc(parent, name);
621 if (!dentry)
622 return ERR_PTR(-ENOMEM);
623 }
624 if (dentry->d_inode == NULL) {
625 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400626 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300627 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400628 dput(dentry);
629 return ERR_PTR(-EEXIST);
630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * FIXME: This probably has races.
634 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400635static void __rpc_depopulate(struct dentry *parent,
636 const struct rpc_filelist *files,
637 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400640 struct dentry *dentry;
641 struct qstr name;
642 int i;
643
644 for (i = start; i < eof; i++) {
645 name.name = files[i].name;
646 name.len = strlen(files[i].name);
647 name.hash = full_name_hash(name.name, name.len);
648 dentry = d_lookup(parent, &name);
649
650 if (dentry == NULL)
651 continue;
652 if (dentry->d_inode == NULL)
653 goto next;
654 switch (dentry->d_inode->i_mode & S_IFMT) {
655 default:
656 BUG();
657 case S_IFREG:
658 __rpc_unlink(dir, dentry);
659 break;
660 case S_IFDIR:
661 __rpc_rmdir(dir, dentry);
662 }
663next:
664 dput(dentry);
665 }
666}
667
668static void rpc_depopulate(struct dentry *parent,
669 const struct rpc_filelist *files,
670 int start, int eof)
671{
672 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Arjan van de Venc6573c22006-07-03 00:25:16 -0700674 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400675 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800676 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
Trond Myklebustac6fece2009-08-09 15:14:20 -0400679static int rpc_populate(struct dentry *parent,
680 const struct rpc_filelist *files,
681 int start, int eof,
682 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400684 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400686 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800688 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400690 struct qstr q;
691
692 q.name = files[i].name;
693 q.len = strlen(files[i].name);
694 q.hash = full_name_hash(q.name, q.len);
695 dentry = __rpc_lookup_create_exclusive(parent, &q);
696 err = PTR_ERR(dentry);
697 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400699 switch (files[i].mode & S_IFMT) {
700 default:
701 BUG();
702 case S_IFREG:
703 err = __rpc_create(dir, dentry,
704 files[i].mode,
705 files[i].i_fop,
706 private);
707 break;
708 case S_IFDIR:
709 err = __rpc_mkdir(dir, dentry,
710 files[i].mode,
711 NULL,
712 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400714 if (err != 0)
715 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800717 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
719out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400720 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800721 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800723 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400724 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400725}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100726
Trond Myklebuste57aed72009-08-09 15:14:26 -0400727static struct dentry *rpc_mkdir_populate(struct dentry *parent,
728 struct qstr *name, umode_t mode, void *private,
729 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400730{
Trond Myklebustf1345852005-09-23 11:08:25 -0400731 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400732 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400733 int error;
734
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400735 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
736 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400737 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400738 goto out;
739 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400740 if (error != 0)
741 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400742 if (populate != NULL) {
743 error = populate(dentry, args_populate);
744 if (error)
745 goto err_rmdir;
746 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400747out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800748 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400749 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400750err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400751 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400752out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400753 dentry = ERR_PTR(error);
754 goto out;
755}
756
Trond Myklebuste57aed72009-08-09 15:14:26 -0400757static int rpc_rmdir_depopulate(struct dentry *dentry,
758 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400759{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700760 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400761 struct inode *dir;
762 int error;
763
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700764 parent = dget_parent(dentry);
765 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700766 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400767 if (depopulate != NULL)
768 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400769 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800770 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700771 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400772 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500775/**
776 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
777 * @parent: dentry of directory to create new "pipe" in
778 * @name: name of pipe
779 * @private: private data to associate with the pipe, for the caller's use
780 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500781 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800782 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500783 *
784 * Data is made available for userspace to read by calls to
785 * rpc_queue_upcall(). The actual reads will result in calls to
786 * @ops->upcall, which will be called with the file pointer,
787 * message, and userspace buffer to copy to.
788 *
789 * Writes can come at any time, and do not necessarily have to be
790 * responses to upcalls. They will result in calls to @msg->downcall.
791 *
792 * The @private argument passed here will be available to all these methods
793 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
794 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400795struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
796 void *private, const struct rpc_pipe_ops *ops,
797 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400800 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400801 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400802 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400803 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400804
805 if (ops->upcall == NULL)
806 umode &= ~S_IRUGO;
807 if (ops->downcall == NULL)
808 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400810 q.name = name;
811 q.len = strlen(name);
812 q.hash = full_name_hash(q.name, q.len),
813
814 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300815 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400817 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400818 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400819 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400820 if (err)
821 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400822out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800823 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400824 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400825out_err:
826 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400827 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800828 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400829 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
Trond Myklebust468039e2008-12-23 15:21:31 -0500832EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500834/**
835 * rpc_unlink - remove a pipe
836 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
837 *
838 * After this call, lookups will no longer find the pipe, and any
839 * attempts to read or write using preexisting opens of the pipe will
840 * return -EPIPE.
841 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400842int
Trond Myklebust5d674762006-07-31 14:11:48 -0700843rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Trond Myklebust5d674762006-07-31 14:11:48 -0700845 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400846 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700847 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Trond Myklebust5d674762006-07-31 14:11:48 -0700849 parent = dget_parent(dentry);
850 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700851 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400852 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800853 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700854 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400855 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
Trond Myklebust468039e2008-12-23 15:21:31 -0500857EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Trond Myklebuste57aed72009-08-09 15:14:26 -0400859enum {
860 RPCAUTH_info,
861 RPCAUTH_EOF
862};
863
864static const struct rpc_filelist authfiles[] = {
865 [RPCAUTH_info] = {
866 .name = "info",
867 .i_fop = &rpc_info_operations,
868 .mode = S_IFREG | S_IRUSR,
869 },
870};
871
872static int rpc_clntdir_populate(struct dentry *dentry, void *private)
873{
874 return rpc_populate(dentry,
875 authfiles, RPCAUTH_info, RPCAUTH_EOF,
876 private);
877}
878
879static void rpc_clntdir_depopulate(struct dentry *dentry)
880{
881 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
882}
883
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400884/**
885 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400886 * @dentry: dentry from the rpc_pipefs root to the new directory
887 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400888 * @rpc_client: rpc client to associate with this directory
889 *
890 * This creates a directory at the given @path associated with
891 * @rpc_clnt, which will contain a file named "info" with some basic
892 * information about the client, together with any "pipes" that may
893 * later be created using rpc_mkpipe().
894 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400895struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400896 struct qstr *name,
897 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400898{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400899 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
900 rpc_clntdir_populate, rpc_client);
901}
902
903/**
904 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
905 * @dentry: directory to remove
906 */
907int rpc_remove_client_dir(struct dentry *dentry)
908{
909 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400910}
911
Trond Myklebust8854e822009-08-09 15:14:30 -0400912static const struct rpc_filelist cache_pipefs_files[3] = {
913 [0] = {
914 .name = "channel",
915 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400916 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400917 },
918 [1] = {
919 .name = "content",
920 .i_fop = &content_file_operations_pipefs,
921 .mode = S_IFREG|S_IRUSR,
922 },
923 [2] = {
924 .name = "flush",
925 .i_fop = &cache_flush_operations_pipefs,
926 .mode = S_IFREG|S_IRUSR|S_IWUSR,
927 },
928};
929
930static int rpc_cachedir_populate(struct dentry *dentry, void *private)
931{
932 return rpc_populate(dentry,
933 cache_pipefs_files, 0, 3,
934 private);
935}
936
937static void rpc_cachedir_depopulate(struct dentry *dentry)
938{
939 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
940}
941
942struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400943 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400944{
945 return rpc_mkdir_populate(parent, name, umode, NULL,
946 rpc_cachedir_populate, cd);
947}
948
949void rpc_remove_cache_dir(struct dentry *dentry)
950{
951 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954/*
955 * populate the filesystem
956 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700957static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .alloc_inode = rpc_alloc_inode,
959 .destroy_inode = rpc_destroy_inode,
960 .statfs = simple_statfs,
961};
962
963#define RPCAUTH_GSSMAGIC 0x67596969
964
Trond Myklebustbb156742009-08-09 15:14:21 -0400965/*
966 * We have a single directory with 1 node in it.
967 */
968enum {
969 RPCAUTH_lockd,
970 RPCAUTH_mount,
971 RPCAUTH_nfs,
972 RPCAUTH_portmap,
973 RPCAUTH_statd,
974 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400975 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -0400976 RPCAUTH_RootEOF
977};
978
979static const struct rpc_filelist files[] = {
980 [RPCAUTH_lockd] = {
981 .name = "lockd",
982 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
983 },
984 [RPCAUTH_mount] = {
985 .name = "mount",
986 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
987 },
988 [RPCAUTH_nfs] = {
989 .name = "nfs",
990 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
991 },
992 [RPCAUTH_portmap] = {
993 .name = "portmap",
994 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
995 },
996 [RPCAUTH_statd] = {
997 .name = "statd",
998 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
999 },
1000 [RPCAUTH_nfsd4_cb] = {
1001 .name = "nfsd4_cb",
1002 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1003 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001004 [RPCAUTH_cache] = {
1005 .name = "cache",
1006 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1007 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001008};
1009
Stanislav Kinsbursky432eb1a2011-12-26 15:39:22 +03001010/*
1011 * This call can be used only in RPC pipefs mount notification hooks.
1012 */
1013struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1014 const unsigned char *dir_name)
1015{
1016 struct qstr dir = {
1017 .name = dir_name,
1018 .len = strlen(dir_name),
1019 .hash = full_name_hash(dir_name, strlen(dir_name)),
1020 };
1021
1022 return d_lookup(sb->s_root, &dir);
1023}
1024EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026static int
1027rpc_fill_super(struct super_block *sb, void *data, int silent)
1028{
1029 struct inode *inode;
1030 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001031 struct net *net = data;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001032 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 sb->s_blocksize = PAGE_CACHE_SIZE;
1035 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1036 sb->s_magic = RPCAUTH_GSSMAGIC;
1037 sb->s_op = &s_ops;
1038 sb->s_time_gran = 1;
1039
1040 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1041 if (!inode)
1042 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001043 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 if (!root) {
1045 iput(inode);
1046 return -ENOMEM;
1047 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001048 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001049 return -ENOMEM;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001050 err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1051 RPC_PIPEFS_MOUNT,
1052 sb);
1053 if (err)
1054 goto err_depopulate;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001055 sb->s_fs_info = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 return 0;
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001057
1058err_depopulate:
1059 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1060 RPC_PIPEFS_UMOUNT,
1061 sb);
1062 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1063 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Al Virofc14f2f2010-07-25 01:48:30 +04001066static struct dentry *
1067rpc_mount(struct file_system_type *fs_type,
1068 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001070 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
1072
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001073void rpc_kill_sb(struct super_block *sb)
1074{
1075 struct net *net = sb->s_fs_info;
1076
1077 put_net(net);
Stanislav Kinsbursky2d001312011-12-26 15:39:13 +03001078 blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1079 RPC_PIPEFS_UMOUNT,
1080 sb);
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001081 kill_litter_super(sb);
1082}
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084static struct file_system_type rpc_pipe_fs_type = {
1085 .owner = THIS_MODULE,
1086 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001087 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001088 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
1090
1091static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001092init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1095
Christoph Lametera35afb82007-05-16 22:10:57 -07001096 inode_init_once(&rpci->vfs_inode);
1097 rpci->private = NULL;
1098 rpci->nreaders = 0;
1099 rpci->nwriters = 0;
1100 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -04001101 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -07001102 INIT_LIST_HEAD(&rpci->pipe);
1103 rpci->pipelen = 0;
1104 init_waitqueue_head(&rpci->waitq);
1105 INIT_DELAYED_WORK(&rpci->queue_timeout,
1106 rpc_timeout_upcall_queue);
1107 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
1110int register_rpc_pipefs(void)
1111{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001112 int err;
1113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001115 sizeof(struct rpc_inode),
1116 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1117 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001118 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (!rpc_inode_cachep)
1120 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001121 err = register_filesystem(&rpc_pipe_fs_type);
1122 if (err) {
1123 kmem_cache_destroy(rpc_inode_cachep);
1124 return err;
1125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return 0;
1128}
1129
1130void unregister_rpc_pipefs(void)
1131{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001132 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 unregister_filesystem(&rpc_pipe_fs_type);
1134}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001135
1136/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1137MODULE_ALIAS("rpc_pipefs");