blob: bfddd68b31d392ccc3917cd851667ff972a11e8a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Al Virofc14f2f2010-07-25 01:48:30 +040031static struct vfsmount *rpc_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int rpc_mount_count;
33
34static struct file_system_type rpc_pipe_fs_type;
35
36
Christoph Lametere18b8902006-12-06 20:33:20 -080037static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define RPC_UPCALL_TIMEOUT (30*HZ)
40
Trond Myklebust9842ef32006-02-01 12:18:44 -050041static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
42 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050043{
44 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050045
Trond Myklebust9842ef32006-02-01 12:18:44 -050046 if (list_empty(head))
47 return;
48 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050049 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040050 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050051 msg->errno = err;
52 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050053 } while (!list_empty(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 wake_up(&rpci->waitq);
55}
56
57static void
David Howells65f27f32006-11-22 14:55:48 +000058rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Trond Myklebust9842ef32006-02-01 12:18:44 -050060 LIST_HEAD(free_list);
David Howells65f27f32006-11-22 14:55:48 +000061 struct rpc_inode *rpci =
62 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050064 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Trond Myklebust9842ef32006-02-01 12:18:44 -050066 spin_lock(&inode->i_lock);
67 if (rpci->ops == NULL) {
68 spin_unlock(&inode->i_lock);
69 return;
70 }
71 destroy_msg = rpci->ops->destroy_msg;
72 if (rpci->nreaders == 0) {
73 list_splice_init(&rpci->pipe, &free_list);
74 rpci->pipelen = 0;
75 }
76 spin_unlock(&inode->i_lock);
77 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Peng Taoc1225152011-09-22 21:50:10 -040080ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
81 char __user *dst, size_t buflen)
82{
83 char *data = (char *)msg->data + msg->copied;
84 size_t mlen = min(msg->len - msg->copied, buflen);
85 unsigned long left;
86
87 left = copy_to_user(dst, data, mlen);
88 if (left == mlen) {
89 msg->errno = -EFAULT;
90 return -EFAULT;
91 }
92
93 mlen -= left;
94 msg->copied += mlen;
95 msg->errno = 0;
96 return mlen;
97}
98EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
99
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500100/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800101 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500102 * @inode: inode of upcall pipe on which to queue given message
103 * @msg: message to queue
104 *
105 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
106 * A userspace process may then later read the upcall by performing a
107 * read on an open file for this inode. It is up to the caller to
108 * initialize the fields of @msg (other than @msg->list) appropriately.
109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110int
111rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
112{
113 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400114 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Trond Myklebust9842ef32006-02-01 12:18:44 -0500116 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400117 if (rpci->ops == NULL)
118 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (rpci->nreaders) {
120 list_add_tail(&msg->list, &rpci->pipe);
121 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400122 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
124 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500125 queue_delayed_work(rpciod_workqueue,
126 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 RPC_UPCALL_TIMEOUT);
128 list_add_tail(&msg->list, &rpci->pipe);
129 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400130 res = 0;
131 }
132out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500133 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 wake_up(&rpci->waitq);
135 return res;
136}
Trond Myklebust468039e2008-12-23 15:21:31 -0500137EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Trond Myklebust6070fe62005-10-27 22:12:46 -0400139static inline void
140rpc_inode_setowner(struct inode *inode, void *private)
141{
142 RPC_I(inode)->private = private;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static void
146rpc_close_pipes(struct inode *inode)
147{
148 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebustb693ba42009-08-09 15:14:15 -0400149 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500150 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800152 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500153 ops = rpci->ops;
154 if (ops != NULL) {
155 LIST_HEAD(free_list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500156 spin_lock(&inode->i_lock);
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500157 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500159 list_splice_init(&rpci->in_upcall, &free_list);
160 list_splice_init(&rpci->pipe, &free_list);
161 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500163 spin_unlock(&inode->i_lock);
164 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
165 rpci->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500166 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500167 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400168 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400170 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800171 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static struct inode *
175rpc_alloc_inode(struct super_block *sb)
176{
177 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800178 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!rpci)
180 return NULL;
181 return &rpci->vfs_inode;
182}
183
184static void
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100185rpc_i_callback(struct rcu_head *head)
186{
187 struct inode *inode = container_of(head, struct inode, i_rcu);
188 INIT_LIST_HEAD(&inode->i_dentry);
189 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
190}
191
192static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193rpc_destroy_inode(struct inode *inode)
194{
Nick Pigginfa0d7e32011-01-07 17:49:49 +1100195 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198static int
199rpc_pipe_open(struct inode *inode, struct file *filp)
200{
201 struct rpc_inode *rpci = RPC_I(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500202 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int res = -ENXIO;
204
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800205 mutex_lock(&inode->i_mutex);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500206 if (rpci->ops == NULL)
207 goto out;
208 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
209 if (first_open && rpci->ops->open_pipe) {
210 res = rpci->ops->open_pipe(inode);
211 if (res)
212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500214 if (filp->f_mode & FMODE_READ)
215 rpci->nreaders++;
216 if (filp->f_mode & FMODE_WRITE)
217 rpci->nwriters++;
218 res = 0;
219out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800220 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return res;
222}
223
224static int
225rpc_pipe_release(struct inode *inode, struct file *filp)
226{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100227 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500229 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800231 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (rpci->ops == NULL)
233 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700234 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (msg != NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500236 spin_lock(&inode->i_lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500237 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400238 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500239 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 rpci->ops->destroy_msg(msg);
241 }
242 if (filp->f_mode & FMODE_WRITE)
243 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500244 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500246 if (rpci->nreaders == 0) {
247 LIST_HEAD(free_list);
248 spin_lock(&inode->i_lock);
249 list_splice_init(&rpci->pipe, &free_list);
250 rpci->pipelen = 0;
251 spin_unlock(&inode->i_lock);
252 rpc_purge_list(rpci, &free_list,
253 rpci->ops->destroy_msg, -EAGAIN);
254 }
255 }
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500256 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
257 if (last_close && rpci->ops->release_pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rpci->ops->release_pipe(inode);
259out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800260 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 return 0;
262}
263
264static ssize_t
265rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
266{
Josef Sipek303b46b2006-12-08 02:37:42 -0800267 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct rpc_inode *rpci = RPC_I(inode);
269 struct rpc_pipe_msg *msg;
270 int res = 0;
271
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800272 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (rpci->ops == NULL) {
274 res = -EPIPE;
275 goto out_unlock;
276 }
277 msg = filp->private_data;
278 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500279 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (!list_empty(&rpci->pipe)) {
281 msg = list_entry(rpci->pipe.next,
282 struct rpc_pipe_msg,
283 list);
284 list_move(&msg->list, &rpci->in_upcall);
285 rpci->pipelen -= msg->len;
286 filp->private_data = msg;
287 msg->copied = 0;
288 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500289 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (msg == NULL)
291 goto out_unlock;
292 }
293 /* NOTE: it is up to the callback to update msg->copied */
294 res = rpci->ops->upcall(filp, msg, buf, len);
295 if (res < 0 || msg->len == msg->copied) {
296 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500297 spin_lock(&inode->i_lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400298 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500299 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 rpci->ops->destroy_msg(msg);
301 }
302out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800303 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return res;
305}
306
307static ssize_t
308rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
309{
Josef Sipek303b46b2006-12-08 02:37:42 -0800310 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 struct rpc_inode *rpci = RPC_I(inode);
312 int res;
313
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800314 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 res = -EPIPE;
316 if (rpci->ops != NULL)
317 res = rpci->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800318 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return res;
320}
321
322static unsigned int
323rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
324{
325 struct rpc_inode *rpci;
326 unsigned int mask = 0;
327
Josef Sipek303b46b2006-12-08 02:37:42 -0800328 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 poll_wait(filp, &rpci->waitq, wait);
330
331 mask = POLLOUT | POLLWRNORM;
332 if (rpci->ops == NULL)
333 mask |= POLLERR | POLLHUP;
J. Bruce Fieldseda4f9b2007-11-06 13:05:36 -0500334 if (filp->private_data || !list_empty(&rpci->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 mask |= POLLIN | POLLRDNORM;
336 return mask;
337}
338
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200339static long
340rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200342 struct inode *inode = filp->f_path.dentry->d_inode;
343 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int len;
345
346 switch (cmd) {
347 case FIONREAD:
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200348 spin_lock(&inode->i_lock);
349 if (rpci->ops == NULL) {
350 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 len = rpci->pipelen;
354 if (filp->private_data) {
355 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700356 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 len += msg->len - msg->copied;
358 }
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200359 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return put_user(len, (int __user *)arg);
361 default:
362 return -EINVAL;
363 }
364}
365
Arjan van de Venda7071d2007-02-12 00:55:36 -0800366static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 .owner = THIS_MODULE,
368 .llseek = no_llseek,
369 .read = rpc_pipe_read,
370 .write = rpc_pipe_write,
371 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200372 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 .open = rpc_pipe_open,
374 .release = rpc_pipe_release,
375};
376
377static int
378rpc_show_info(struct seq_file *m, void *v)
379{
380 struct rpc_clnt *clnt = m->private;
381
382 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
383 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
384 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400385 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
386 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400387 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
389}
390
391static int
392rpc_info_open(struct inode *inode, struct file *file)
393{
Trond Myklebust006abe82010-09-12 19:55:25 -0400394 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 int ret = single_open(file, rpc_show_info, NULL);
396
397 if (!ret) {
398 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400399
400 spin_lock(&file->f_path.dentry->d_lock);
401 if (!d_unhashed(file->f_path.dentry))
402 clnt = RPC_I(inode)->private;
403 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
404 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 m->private = clnt;
406 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400407 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 single_release(inode, file);
409 ret = -EINVAL;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 return ret;
413}
414
415static int
416rpc_info_release(struct inode *inode, struct file *file)
417{
418 struct seq_file *m = file->private_data;
419 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
420
421 if (clnt)
422 rpc_release_client(clnt);
423 return single_release(inode, file);
424}
425
Arjan van de Venda7071d2007-02-12 00:55:36 -0800426static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 .owner = THIS_MODULE,
428 .open = rpc_info_open,
429 .read = seq_read,
430 .llseek = seq_lseek,
431 .release = rpc_info_release,
432};
433
434
435/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 * Description of fs contents.
437 */
438struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400439 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800440 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400441 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442};
443
Trond Myklebust54281542006-03-20 13:44:49 -0500444struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Trond Myklebust54281542006-03-20 13:44:49 -0500446 int err;
447
Al Virofc14f2f2010-07-25 01:48:30 +0400448 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500449 if (err != 0)
450 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400451 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400453EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Trond Myklebust54281542006-03-20 13:44:49 -0500455void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Al Virofc14f2f2010-07-25 01:48:30 +0400457 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400459EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Nick Pigginfe15ce42011-01-07 17:49:23 +1100461static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400462{
463 return 1;
464}
465
Al Viro3ba13d12009-02-20 06:02:22 +0000466static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400467 .d_delete = rpc_delete_dentry,
468};
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400471rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 struct inode *inode = new_inode(sb);
474 if (!inode)
475 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400476 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000479 switch (mode & S_IFMT) {
480 case S_IFDIR:
481 inode->i_fop = &simple_dir_operations;
482 inode->i_op = &simple_dir_inode_operations;
483 inc_nlink(inode);
484 default:
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 return inode;
488}
489
Trond Myklebust75898062009-08-09 15:14:17 -0400490static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
491 umode_t mode,
492 const struct file_operations *i_fop,
493 void *private)
494{
495 struct inode *inode;
496
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000497 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400498 inode = rpc_get_inode(dir->i_sb, mode);
499 if (!inode)
500 goto out_err;
501 inode->i_ino = iunique(dir->i_sb, 100);
502 if (i_fop)
503 inode->i_fop = i_fop;
504 if (private)
505 rpc_inode_setowner(inode, private);
506 d_add(dentry, inode);
507 return 0;
508out_err:
509 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
510 __FILE__, __func__, dentry->d_name.name);
511 dput(dentry);
512 return -ENOMEM;
513}
514
Trond Myklebustac6fece2009-08-09 15:14:20 -0400515static int __rpc_create(struct inode *dir, struct dentry *dentry,
516 umode_t mode,
517 const struct file_operations *i_fop,
518 void *private)
519{
520 int err;
521
522 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
523 if (err)
524 return err;
525 fsnotify_create(dir, dentry);
526 return 0;
527}
528
Trond Myklebust75898062009-08-09 15:14:17 -0400529static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
530 umode_t mode,
531 const struct file_operations *i_fop,
532 void *private)
533{
534 int err;
535
536 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
537 if (err)
538 return err;
539 inc_nlink(dir);
540 fsnotify_mkdir(dir, dentry);
541 return 0;
542}
543
544static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
545 umode_t mode,
546 const struct file_operations *i_fop,
547 void *private,
548 const struct rpc_pipe_ops *ops,
549 int flags)
550{
551 struct rpc_inode *rpci;
552 int err;
553
554 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
555 if (err)
556 return err;
557 rpci = RPC_I(dentry->d_inode);
558 rpci->nkern_readwriters = 1;
559 rpci->private = private;
560 rpci->flags = flags;
561 rpci->ops = ops;
562 fsnotify_create(dir, dentry);
563 return 0;
564}
565
Trond Myklebustac6fece2009-08-09 15:14:20 -0400566static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
567{
568 int ret;
569
570 dget(dentry);
571 ret = simple_rmdir(dir, dentry);
572 d_delete(dentry);
573 dput(dentry);
574 return ret;
575}
576
Trond Myklebust810d90b2009-08-09 15:14:18 -0400577static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
578{
579 int ret;
580
581 dget(dentry);
582 ret = simple_unlink(dir, dentry);
583 d_delete(dentry);
584 dput(dentry);
585 return ret;
586}
587
588static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
589{
590 struct inode *inode = dentry->d_inode;
591 struct rpc_inode *rpci = RPC_I(inode);
592
593 rpci->nkern_readwriters--;
594 if (rpci->nkern_readwriters != 0)
595 return 0;
596 rpc_close_pipes(inode);
597 return __rpc_unlink(dir, dentry);
598}
599
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400600static struct dentry *__rpc_lookup_create(struct dentry *parent,
601 struct qstr *name)
602{
603 struct dentry *dentry;
604
605 dentry = d_lookup(parent, name);
606 if (!dentry) {
607 dentry = d_alloc(parent, name);
608 if (!dentry) {
609 dentry = ERR_PTR(-ENOMEM);
610 goto out_err;
611 }
612 }
613 if (!dentry->d_inode)
Nick Pigginfb045ad2011-01-07 17:49:55 +1100614 d_set_d_op(dentry, &rpc_dentry_operations);
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400615out_err:
616 return dentry;
617}
618
619static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
620 struct qstr *name)
621{
622 struct dentry *dentry;
623
624 dentry = __rpc_lookup_create(parent, name);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400625 if (IS_ERR(dentry))
626 return dentry;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400627 if (dentry->d_inode == NULL)
628 return dentry;
629 dput(dentry);
630 return ERR_PTR(-EEXIST);
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/*
634 * FIXME: This probably has races.
635 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400636static void __rpc_depopulate(struct dentry *parent,
637 const struct rpc_filelist *files,
638 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400641 struct dentry *dentry;
642 struct qstr name;
643 int i;
644
645 for (i = start; i < eof; i++) {
646 name.name = files[i].name;
647 name.len = strlen(files[i].name);
648 name.hash = full_name_hash(name.name, name.len);
649 dentry = d_lookup(parent, &name);
650
651 if (dentry == NULL)
652 continue;
653 if (dentry->d_inode == NULL)
654 goto next;
655 switch (dentry->d_inode->i_mode & S_IFMT) {
656 default:
657 BUG();
658 case S_IFREG:
659 __rpc_unlink(dir, dentry);
660 break;
661 case S_IFDIR:
662 __rpc_rmdir(dir, dentry);
663 }
664next:
665 dput(dentry);
666 }
667}
668
669static void rpc_depopulate(struct dentry *parent,
670 const struct rpc_filelist *files,
671 int start, int eof)
672{
673 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Arjan van de Venc6573c22006-07-03 00:25:16 -0700675 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400676 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800677 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Trond Myklebustac6fece2009-08-09 15:14:20 -0400680static int rpc_populate(struct dentry *parent,
681 const struct rpc_filelist *files,
682 int start, int eof,
683 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400685 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400687 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800689 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400691 struct qstr q;
692
693 q.name = files[i].name;
694 q.len = strlen(files[i].name);
695 q.hash = full_name_hash(q.name, q.len);
696 dentry = __rpc_lookup_create_exclusive(parent, &q);
697 err = PTR_ERR(dentry);
698 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400700 switch (files[i].mode & S_IFMT) {
701 default:
702 BUG();
703 case S_IFREG:
704 err = __rpc_create(dir, dentry,
705 files[i].mode,
706 files[i].i_fop,
707 private);
708 break;
709 case S_IFDIR:
710 err = __rpc_mkdir(dir, dentry,
711 files[i].mode,
712 NULL,
713 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400715 if (err != 0)
716 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800718 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 return 0;
720out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400721 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800722 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800724 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400725 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400726}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100727
Trond Myklebuste57aed72009-08-09 15:14:26 -0400728static struct dentry *rpc_mkdir_populate(struct dentry *parent,
729 struct qstr *name, umode_t mode, void *private,
730 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400731{
Trond Myklebustf1345852005-09-23 11:08:25 -0400732 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400733 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400734 int error;
735
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400736 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
737 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400738 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400739 goto out;
740 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400741 if (error != 0)
742 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400743 if (populate != NULL) {
744 error = populate(dentry, args_populate);
745 if (error)
746 goto err_rmdir;
747 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400748out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800749 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400750 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400751err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400752 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400753out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400754 dentry = ERR_PTR(error);
755 goto out;
756}
757
Trond Myklebuste57aed72009-08-09 15:14:26 -0400758static int rpc_rmdir_depopulate(struct dentry *dentry,
759 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400760{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700761 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400762 struct inode *dir;
763 int error;
764
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700765 parent = dget_parent(dentry);
766 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700767 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400768 if (depopulate != NULL)
769 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400770 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800771 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700772 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400773 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500776/**
777 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
778 * @parent: dentry of directory to create new "pipe" in
779 * @name: name of pipe
780 * @private: private data to associate with the pipe, for the caller's use
781 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500782 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800783 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500784 *
785 * Data is made available for userspace to read by calls to
786 * rpc_queue_upcall(). The actual reads will result in calls to
787 * @ops->upcall, which will be called with the file pointer,
788 * message, and userspace buffer to copy to.
789 *
790 * Writes can come at any time, and do not necessarily have to be
791 * responses to upcalls. They will result in calls to @msg->downcall.
792 *
793 * The @private argument passed here will be available to all these methods
794 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
795 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400796struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
797 void *private, const struct rpc_pipe_ops *ops,
798 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400801 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400802 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400803 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400804 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400805
806 if (ops->upcall == NULL)
807 umode &= ~S_IRUGO;
808 if (ops->downcall == NULL)
809 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400811 q.name = name;
812 q.len = strlen(name);
813 q.hash = full_name_hash(q.name, q.len),
814
815 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
816 dentry = __rpc_lookup_create(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400818 goto out;
Trond Myklebust34f30892007-06-07 18:28:02 -0400819 if (dentry->d_inode) {
Trond Myklebust75898062009-08-09 15:14:17 -0400820 struct rpc_inode *rpci = RPC_I(dentry->d_inode);
Trond Myklebust34f30892007-06-07 18:28:02 -0400821 if (rpci->private != private ||
822 rpci->ops != ops ||
823 rpci->flags != flags) {
824 dput (dentry);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400825 err = -EBUSY;
826 goto out_err;
Trond Myklebust34f30892007-06-07 18:28:02 -0400827 }
Trond Myklebust03a12562007-06-08 14:14:53 -0400828 rpci->nkern_readwriters++;
Trond Myklebust34f30892007-06-07 18:28:02 -0400829 goto out;
830 }
Trond Myklebust75898062009-08-09 15:14:17 -0400831
832 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400833 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400834 if (err)
835 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400836out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800837 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400838 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400839out_err:
840 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400841 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800842 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400843 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
Trond Myklebust468039e2008-12-23 15:21:31 -0500846EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500848/**
849 * rpc_unlink - remove a pipe
850 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
851 *
852 * After this call, lookups will no longer find the pipe, and any
853 * attempts to read or write using preexisting opens of the pipe will
854 * return -EPIPE.
855 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400856int
Trond Myklebust5d674762006-07-31 14:11:48 -0700857rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Trond Myklebust5d674762006-07-31 14:11:48 -0700859 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400860 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700861 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Trond Myklebust5d674762006-07-31 14:11:48 -0700863 parent = dget_parent(dentry);
864 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700865 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400866 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800867 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700868 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400869 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Trond Myklebust468039e2008-12-23 15:21:31 -0500871EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Trond Myklebuste57aed72009-08-09 15:14:26 -0400873enum {
874 RPCAUTH_info,
875 RPCAUTH_EOF
876};
877
878static const struct rpc_filelist authfiles[] = {
879 [RPCAUTH_info] = {
880 .name = "info",
881 .i_fop = &rpc_info_operations,
882 .mode = S_IFREG | S_IRUSR,
883 },
884};
885
886static int rpc_clntdir_populate(struct dentry *dentry, void *private)
887{
888 return rpc_populate(dentry,
889 authfiles, RPCAUTH_info, RPCAUTH_EOF,
890 private);
891}
892
893static void rpc_clntdir_depopulate(struct dentry *dentry)
894{
895 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
896}
897
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400898/**
899 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400900 * @dentry: dentry from the rpc_pipefs root to the new directory
901 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400902 * @rpc_client: rpc client to associate with this directory
903 *
904 * This creates a directory at the given @path associated with
905 * @rpc_clnt, which will contain a file named "info" with some basic
906 * information about the client, together with any "pipes" that may
907 * later be created using rpc_mkpipe().
908 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400909struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400910 struct qstr *name,
911 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400912{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400913 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
914 rpc_clntdir_populate, rpc_client);
915}
916
917/**
918 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
919 * @dentry: directory to remove
920 */
921int rpc_remove_client_dir(struct dentry *dentry)
922{
923 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400924}
925
Trond Myklebust8854e822009-08-09 15:14:30 -0400926static const struct rpc_filelist cache_pipefs_files[3] = {
927 [0] = {
928 .name = "channel",
929 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400930 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400931 },
932 [1] = {
933 .name = "content",
934 .i_fop = &content_file_operations_pipefs,
935 .mode = S_IFREG|S_IRUSR,
936 },
937 [2] = {
938 .name = "flush",
939 .i_fop = &cache_flush_operations_pipefs,
940 .mode = S_IFREG|S_IRUSR|S_IWUSR,
941 },
942};
943
944static int rpc_cachedir_populate(struct dentry *dentry, void *private)
945{
946 return rpc_populate(dentry,
947 cache_pipefs_files, 0, 3,
948 private);
949}
950
951static void rpc_cachedir_depopulate(struct dentry *dentry)
952{
953 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
954}
955
956struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
957 mode_t umode, struct cache_detail *cd)
958{
959 return rpc_mkdir_populate(parent, name, umode, NULL,
960 rpc_cachedir_populate, cd);
961}
962
963void rpc_remove_cache_dir(struct dentry *dentry)
964{
965 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968/*
969 * populate the filesystem
970 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700971static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 .alloc_inode = rpc_alloc_inode,
973 .destroy_inode = rpc_destroy_inode,
974 .statfs = simple_statfs,
975};
976
977#define RPCAUTH_GSSMAGIC 0x67596969
978
Trond Myklebustbb156742009-08-09 15:14:21 -0400979/*
980 * We have a single directory with 1 node in it.
981 */
982enum {
983 RPCAUTH_lockd,
984 RPCAUTH_mount,
985 RPCAUTH_nfs,
986 RPCAUTH_portmap,
987 RPCAUTH_statd,
988 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400989 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -0400990 RPCAUTH_RootEOF
991};
992
993static const struct rpc_filelist files[] = {
994 [RPCAUTH_lockd] = {
995 .name = "lockd",
996 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
997 },
998 [RPCAUTH_mount] = {
999 .name = "mount",
1000 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1001 },
1002 [RPCAUTH_nfs] = {
1003 .name = "nfs",
1004 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1005 },
1006 [RPCAUTH_portmap] = {
1007 .name = "portmap",
1008 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1009 },
1010 [RPCAUTH_statd] = {
1011 .name = "statd",
1012 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1013 },
1014 [RPCAUTH_nfsd4_cb] = {
1015 .name = "nfsd4_cb",
1016 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1017 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -04001018 [RPCAUTH_cache] = {
1019 .name = "cache",
1020 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1021 },
Trond Myklebustbb156742009-08-09 15:14:21 -04001022};
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024static int
1025rpc_fill_super(struct super_block *sb, void *data, int silent)
1026{
1027 struct inode *inode;
1028 struct dentry *root;
1029
1030 sb->s_blocksize = PAGE_CACHE_SIZE;
1031 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1032 sb->s_magic = RPCAUTH_GSSMAGIC;
1033 sb->s_op = &s_ops;
1034 sb->s_time_gran = 1;
1035
1036 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1037 if (!inode)
1038 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001039 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 if (!root) {
1041 iput(inode);
1042 return -ENOMEM;
1043 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001044 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001045 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047}
1048
Al Virofc14f2f2010-07-25 01:48:30 +04001049static struct dentry *
1050rpc_mount(struct file_system_type *fs_type,
1051 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Al Virofc14f2f2010-07-25 01:48:30 +04001053 return mount_single(fs_type, flags, data, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
1056static struct file_system_type rpc_pipe_fs_type = {
1057 .owner = THIS_MODULE,
1058 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001059 .mount = rpc_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 .kill_sb = kill_litter_super,
1061};
1062
1063static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001064init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
1066 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1067
Christoph Lametera35afb82007-05-16 22:10:57 -07001068 inode_init_once(&rpci->vfs_inode);
1069 rpci->private = NULL;
1070 rpci->nreaders = 0;
1071 rpci->nwriters = 0;
1072 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -04001073 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -07001074 INIT_LIST_HEAD(&rpci->pipe);
1075 rpci->pipelen = 0;
1076 init_waitqueue_head(&rpci->waitq);
1077 INIT_DELAYED_WORK(&rpci->queue_timeout,
1078 rpc_timeout_upcall_queue);
1079 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082int register_rpc_pipefs(void)
1083{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001084 int err;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001087 sizeof(struct rpc_inode),
1088 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1089 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001090 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!rpc_inode_cachep)
1092 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001093 err = register_filesystem(&rpc_pipe_fs_type);
1094 if (err) {
1095 kmem_cache_destroy(rpc_inode_cachep);
1096 return err;
1097 }
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 return 0;
1100}
1101
1102void unregister_rpc_pipefs(void)
1103{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001104 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unregister_filesystem(&rpc_pipe_fs_type);
1106}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001107
1108/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1109MODULE_ALIAS("rpc_pipefs");