blob: f628b0f48a87c04f6cf384356148e6d9f5029c35 [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>
31
32#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Al Virofc14f2f2010-07-25 01:48:30 +040034static struct vfsmount *rpc_mnt __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static int rpc_mount_count;
36
37static struct file_system_type rpc_pipe_fs_type;
38
39
Christoph Lametere18b8902006-12-06 20:33:20 -080040static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#define RPC_UPCALL_TIMEOUT (30*HZ)
43
Trond Myklebust9842ef32006-02-01 12:18:44 -050044static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
45 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050046{
47 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050048
Trond Myklebust9842ef32006-02-01 12:18:44 -050049 if (list_empty(head))
50 return;
51 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050052 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust5a676572010-09-12 19:55:25 -040053 list_del_init(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050054 msg->errno = err;
55 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050056 } while (!list_empty(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 wake_up(&rpci->waitq);
58}
59
60static void
David Howells65f27f32006-11-22 14:55:48 +000061rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Trond Myklebust9842ef32006-02-01 12:18:44 -050063 LIST_HEAD(free_list);
David Howells65f27f32006-11-22 14:55:48 +000064 struct rpc_inode *rpci =
65 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050067 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Trond Myklebust9842ef32006-02-01 12:18:44 -050069 spin_lock(&inode->i_lock);
70 if (rpci->ops == NULL) {
71 spin_unlock(&inode->i_lock);
72 return;
73 }
74 destroy_msg = rpci->ops->destroy_msg;
75 if (rpci->nreaders == 0) {
76 list_splice_init(&rpci->pipe, &free_list);
77 rpci->pipelen = 0;
78 }
79 spin_unlock(&inode->i_lock);
80 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Peng Taoc1225152011-09-22 21:50:10 -040083ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
84 char __user *dst, size_t buflen)
85{
86 char *data = (char *)msg->data + msg->copied;
87 size_t mlen = min(msg->len - msg->copied, buflen);
88 unsigned long left;
89
90 left = copy_to_user(dst, data, mlen);
91 if (left == mlen) {
92 msg->errno = -EFAULT;
93 return -EFAULT;
94 }
95
96 mlen -= left;
97 msg->copied += mlen;
98 msg->errno = 0;
99 return mlen;
100}
101EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
102
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500103/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -0800104 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500105 * @inode: inode of upcall pipe on which to queue given message
106 * @msg: message to queue
107 *
108 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
109 * A userspace process may then later read the upcall by performing a
110 * read on an open file for this inode. It is up to the caller to
111 * initialize the fields of @msg (other than @msg->list) appropriately.
112 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113int
114rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
115{
116 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400117 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Trond Myklebust9842ef32006-02-01 12:18:44 -0500119 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400120 if (rpci->ops == NULL)
121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (rpci->nreaders) {
123 list_add_tail(&msg->list, &rpci->pipe);
124 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400125 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
127 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500128 queue_delayed_work(rpciod_workqueue,
129 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 RPC_UPCALL_TIMEOUT);
131 list_add_tail(&msg->list, &rpci->pipe);
132 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400133 res = 0;
134 }
135out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500136 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 wake_up(&rpci->waitq);
138 return res;
139}
Trond Myklebust468039e2008-12-23 15:21:31 -0500140EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Trond Myklebust6070fe62005-10-27 22:12:46 -0400142static inline void
143rpc_inode_setowner(struct inode *inode, void *private)
144{
145 RPC_I(inode)->private = private;
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static void
149rpc_close_pipes(struct inode *inode)
150{
151 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebustb693ba42009-08-09 15:14:15 -0400152 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500153 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800155 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500156 ops = rpci->ops;
157 if (ops != NULL) {
158 LIST_HEAD(free_list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500159 spin_lock(&inode->i_lock);
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500160 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500162 list_splice_init(&rpci->in_upcall, &free_list);
163 list_splice_init(&rpci->pipe, &free_list);
164 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500166 spin_unlock(&inode->i_lock);
167 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
168 rpci->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500169 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500170 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400171 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400173 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800174 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static struct inode *
178rpc_alloc_inode(struct super_block *sb)
179{
180 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800181 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (!rpci)
183 return NULL;
184 return &rpci->vfs_inode;
185}
186
187static void
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100188rpc_i_callback(struct rcu_head *head)
189{
190 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100191 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
192}
193
194static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195rpc_destroy_inode(struct inode *inode)
196{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100197 call_rcu(&inode->i_rcu, rpc_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200static int
201rpc_pipe_open(struct inode *inode, struct file *filp)
202{
203 struct rpc_inode *rpci = RPC_I(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500204 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int res = -ENXIO;
206
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800207 mutex_lock(&inode->i_mutex);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500208 if (rpci->ops == NULL)
209 goto out;
210 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
211 if (first_open && rpci->ops->open_pipe) {
212 res = rpci->ops->open_pipe(inode);
213 if (res)
214 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500216 if (filp->f_mode & FMODE_READ)
217 rpci->nreaders++;
218 if (filp->f_mode & FMODE_WRITE)
219 rpci->nwriters++;
220 res = 0;
221out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800222 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return res;
224}
225
226static int
227rpc_pipe_release(struct inode *inode, struct file *filp)
228{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100229 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500231 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800233 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (rpci->ops == NULL)
235 goto out;
Joe Perches655b5bb2010-09-04 18:52:53 -0700236 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 if (msg != NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500238 spin_lock(&inode->i_lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500239 msg->errno = -EAGAIN;
Trond Myklebust5a676572010-09-12 19:55:25 -0400240 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500241 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 rpci->ops->destroy_msg(msg);
243 }
244 if (filp->f_mode & FMODE_WRITE)
245 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500246 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500248 if (rpci->nreaders == 0) {
249 LIST_HEAD(free_list);
250 spin_lock(&inode->i_lock);
251 list_splice_init(&rpci->pipe, &free_list);
252 rpci->pipelen = 0;
253 spin_unlock(&inode->i_lock);
254 rpc_purge_list(rpci, &free_list,
255 rpci->ops->destroy_msg, -EAGAIN);
256 }
257 }
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500258 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
259 if (last_close && rpci->ops->release_pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 rpci->ops->release_pipe(inode);
261out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800262 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
264}
265
266static ssize_t
267rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
268{
Josef Sipek303b46b2006-12-08 02:37:42 -0800269 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 struct rpc_inode *rpci = RPC_I(inode);
271 struct rpc_pipe_msg *msg;
272 int res = 0;
273
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800274 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (rpci->ops == NULL) {
276 res = -EPIPE;
277 goto out_unlock;
278 }
279 msg = filp->private_data;
280 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500281 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (!list_empty(&rpci->pipe)) {
283 msg = list_entry(rpci->pipe.next,
284 struct rpc_pipe_msg,
285 list);
286 list_move(&msg->list, &rpci->in_upcall);
287 rpci->pipelen -= msg->len;
288 filp->private_data = msg;
289 msg->copied = 0;
290 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500291 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (msg == NULL)
293 goto out_unlock;
294 }
295 /* NOTE: it is up to the callback to update msg->copied */
296 res = rpci->ops->upcall(filp, msg, buf, len);
297 if (res < 0 || msg->len == msg->copied) {
298 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500299 spin_lock(&inode->i_lock);
Trond Myklebust5a676572010-09-12 19:55:25 -0400300 list_del_init(&msg->list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500301 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 rpci->ops->destroy_msg(msg);
303 }
304out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800305 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return res;
307}
308
309static ssize_t
310rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
311{
Josef Sipek303b46b2006-12-08 02:37:42 -0800312 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct rpc_inode *rpci = RPC_I(inode);
314 int res;
315
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800316 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 res = -EPIPE;
318 if (rpci->ops != NULL)
319 res = rpci->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800320 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return res;
322}
323
324static unsigned int
325rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
326{
327 struct rpc_inode *rpci;
328 unsigned int mask = 0;
329
Josef Sipek303b46b2006-12-08 02:37:42 -0800330 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 poll_wait(filp, &rpci->waitq, wait);
332
333 mask = POLLOUT | POLLWRNORM;
334 if (rpci->ops == NULL)
335 mask |= POLLERR | POLLHUP;
J. Bruce Fieldseda4f9b2007-11-06 13:05:36 -0500336 if (filp->private_data || !list_empty(&rpci->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 mask |= POLLIN | POLLRDNORM;
338 return mask;
339}
340
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200341static long
342rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200344 struct inode *inode = filp->f_path.dentry->d_inode;
345 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 int len;
347
348 switch (cmd) {
349 case FIONREAD:
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200350 spin_lock(&inode->i_lock);
351 if (rpci->ops == NULL) {
352 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return -EPIPE;
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 len = rpci->pipelen;
356 if (filp->private_data) {
357 struct rpc_pipe_msg *msg;
Joe Perches655b5bb2010-09-04 18:52:53 -0700358 msg = filp->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 len += msg->len - msg->copied;
360 }
Arnd Bergmanna6f8dbc2010-10-04 21:18:23 +0200361 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return put_user(len, (int __user *)arg);
363 default:
364 return -EINVAL;
365 }
366}
367
Arjan van de Venda7071d2007-02-12 00:55:36 -0800368static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 .owner = THIS_MODULE,
370 .llseek = no_llseek,
371 .read = rpc_pipe_read,
372 .write = rpc_pipe_write,
373 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200374 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 .open = rpc_pipe_open,
376 .release = rpc_pipe_release,
377};
378
379static int
380rpc_show_info(struct seq_file *m, void *v)
381{
382 struct rpc_clnt *clnt = m->private;
383
384 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
385 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
386 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400387 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
388 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400389 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391}
392
393static int
394rpc_info_open(struct inode *inode, struct file *file)
395{
Trond Myklebust006abe82010-09-12 19:55:25 -0400396 struct rpc_clnt *clnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 int ret = single_open(file, rpc_show_info, NULL);
398
399 if (!ret) {
400 struct seq_file *m = file->private_data;
Trond Myklebust006abe82010-09-12 19:55:25 -0400401
402 spin_lock(&file->f_path.dentry->d_lock);
403 if (!d_unhashed(file->f_path.dentry))
404 clnt = RPC_I(inode)->private;
405 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
406 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 m->private = clnt;
408 } else {
Trond Myklebust006abe82010-09-12 19:55:25 -0400409 spin_unlock(&file->f_path.dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 single_release(inode, file);
411 ret = -EINVAL;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 return ret;
415}
416
417static int
418rpc_info_release(struct inode *inode, struct file *file)
419{
420 struct seq_file *m = file->private_data;
421 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
422
423 if (clnt)
424 rpc_release_client(clnt);
425 return single_release(inode, file);
426}
427
Arjan van de Venda7071d2007-02-12 00:55:36 -0800428static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 .owner = THIS_MODULE,
430 .open = rpc_info_open,
431 .read = seq_read,
432 .llseek = seq_lseek,
433 .release = rpc_info_release,
434};
435
436
437/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * Description of fs contents.
439 */
440struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400441 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800442 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400443 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444};
445
Trond Myklebust54281542006-03-20 13:44:49 -0500446struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Trond Myklebust54281542006-03-20 13:44:49 -0500448 int err;
449
Al Virofc14f2f2010-07-25 01:48:30 +0400450 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mnt, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500451 if (err != 0)
452 return ERR_PTR(err);
Al Virofc14f2f2010-07-25 01:48:30 +0400453 return rpc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400455EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Trond Myklebust54281542006-03-20 13:44:49 -0500457void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Al Virofc14f2f2010-07-25 01:48:30 +0400459 simple_release_fs(&rpc_mnt, &rpc_mount_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400461EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Nick Pigginfe15ce42011-01-07 17:49:23 +1100463static int rpc_delete_dentry(const struct dentry *dentry)
Trond Myklebust62e17612007-06-08 14:14:46 -0400464{
465 return 1;
466}
467
Al Viro3ba13d12009-02-20 06:02:22 +0000468static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400469 .d_delete = rpc_delete_dentry,
470};
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400473rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct inode *inode = new_inode(sb);
476 if (!inode)
477 return NULL;
Christoph Hellwig85fe4022010-10-23 11:19:54 -0400478 inode->i_ino = get_next_ino();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Joe Perches89f0e4f2011-07-01 09:43:12 +0000481 switch (mode & S_IFMT) {
482 case S_IFDIR:
483 inode->i_fop = &simple_dir_operations;
484 inode->i_op = &simple_dir_inode_operations;
485 inc_nlink(inode);
486 default:
487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 return inode;
490}
491
Trond Myklebust75898062009-08-09 15:14:17 -0400492static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
493 umode_t mode,
494 const struct file_operations *i_fop,
495 void *private)
496{
497 struct inode *inode;
498
Trond Myklebustbeb0f0a2010-12-20 21:19:26 +0000499 d_drop(dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400500 inode = rpc_get_inode(dir->i_sb, mode);
501 if (!inode)
502 goto out_err;
503 inode->i_ino = iunique(dir->i_sb, 100);
504 if (i_fop)
505 inode->i_fop = i_fop;
506 if (private)
507 rpc_inode_setowner(inode, private);
508 d_add(dentry, inode);
509 return 0;
510out_err:
511 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
512 __FILE__, __func__, dentry->d_name.name);
513 dput(dentry);
514 return -ENOMEM;
515}
516
Trond Myklebustac6fece2009-08-09 15:14:20 -0400517static int __rpc_create(struct inode *dir, struct dentry *dentry,
518 umode_t mode,
519 const struct file_operations *i_fop,
520 void *private)
521{
522 int err;
523
524 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
525 if (err)
526 return err;
527 fsnotify_create(dir, dentry);
528 return 0;
529}
530
Trond Myklebust75898062009-08-09 15:14:17 -0400531static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
532 umode_t mode,
533 const struct file_operations *i_fop,
534 void *private)
535{
536 int err;
537
538 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
539 if (err)
540 return err;
541 inc_nlink(dir);
542 fsnotify_mkdir(dir, dentry);
543 return 0;
544}
545
546static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
547 umode_t mode,
548 const struct file_operations *i_fop,
549 void *private,
550 const struct rpc_pipe_ops *ops,
551 int flags)
552{
553 struct rpc_inode *rpci;
554 int err;
555
556 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
557 if (err)
558 return err;
559 rpci = RPC_I(dentry->d_inode);
Trond Myklebust75898062009-08-09 15:14:17 -0400560 rpci->private = private;
561 rpci->flags = flags;
562 rpci->ops = ops;
563 fsnotify_create(dir, dentry);
564 return 0;
565}
566
Trond Myklebustac6fece2009-08-09 15:14:20 -0400567static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
568{
569 int ret;
570
571 dget(dentry);
572 ret = simple_rmdir(dir, dentry);
573 d_delete(dentry);
574 dput(dentry);
575 return ret;
576}
577
Trond Myklebust810d90b2009-08-09 15:14:18 -0400578static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
579{
580 int ret;
581
582 dget(dentry);
583 ret = simple_unlink(dir, dentry);
584 d_delete(dentry);
585 dput(dentry);
586 return ret;
587}
588
589static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
590{
591 struct inode *inode = dentry->d_inode;
Trond Myklebust810d90b2009-08-09 15:14:18 -0400592
Trond Myklebust810d90b2009-08-09 15:14:18 -0400593 rpc_close_pipes(inode);
594 return __rpc_unlink(dir, dentry);
595}
596
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400597static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
598 struct qstr *name)
599{
600 struct dentry *dentry;
601
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300602 dentry = d_lookup(parent, name);
603 if (!dentry) {
604 dentry = d_alloc(parent, name);
605 if (!dentry)
606 return ERR_PTR(-ENOMEM);
607 }
608 if (dentry->d_inode == NULL) {
609 d_set_d_op(dentry, &rpc_dentry_operations);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400610 return dentry;
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300611 }
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400612 dput(dentry);
613 return ERR_PTR(-EEXIST);
614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
617 * FIXME: This probably has races.
618 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400619static void __rpc_depopulate(struct dentry *parent,
620 const struct rpc_filelist *files,
621 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400624 struct dentry *dentry;
625 struct qstr name;
626 int i;
627
628 for (i = start; i < eof; i++) {
629 name.name = files[i].name;
630 name.len = strlen(files[i].name);
631 name.hash = full_name_hash(name.name, name.len);
632 dentry = d_lookup(parent, &name);
633
634 if (dentry == NULL)
635 continue;
636 if (dentry->d_inode == NULL)
637 goto next;
638 switch (dentry->d_inode->i_mode & S_IFMT) {
639 default:
640 BUG();
641 case S_IFREG:
642 __rpc_unlink(dir, dentry);
643 break;
644 case S_IFDIR:
645 __rpc_rmdir(dir, dentry);
646 }
647next:
648 dput(dentry);
649 }
650}
651
652static void rpc_depopulate(struct dentry *parent,
653 const struct rpc_filelist *files,
654 int start, int eof)
655{
656 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Arjan van de Venc6573c22006-07-03 00:25:16 -0700658 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400659 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800660 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Trond Myklebustac6fece2009-08-09 15:14:20 -0400663static int rpc_populate(struct dentry *parent,
664 const struct rpc_filelist *files,
665 int start, int eof,
666 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400668 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400670 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800672 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400674 struct qstr q;
675
676 q.name = files[i].name;
677 q.len = strlen(files[i].name);
678 q.hash = full_name_hash(q.name, q.len);
679 dentry = __rpc_lookup_create_exclusive(parent, &q);
680 err = PTR_ERR(dentry);
681 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400683 switch (files[i].mode & S_IFMT) {
684 default:
685 BUG();
686 case S_IFREG:
687 err = __rpc_create(dir, dentry,
688 files[i].mode,
689 files[i].i_fop,
690 private);
691 break;
692 case S_IFDIR:
693 err = __rpc_mkdir(dir, dentry,
694 files[i].mode,
695 NULL,
696 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400698 if (err != 0)
699 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800701 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400704 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800705 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800707 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400708 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400709}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100710
Trond Myklebuste57aed72009-08-09 15:14:26 -0400711static struct dentry *rpc_mkdir_populate(struct dentry *parent,
712 struct qstr *name, umode_t mode, void *private,
713 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400714{
Trond Myklebustf1345852005-09-23 11:08:25 -0400715 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400716 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400717 int error;
718
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400719 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
720 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400721 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400722 goto out;
723 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400724 if (error != 0)
725 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400726 if (populate != NULL) {
727 error = populate(dentry, args_populate);
728 if (error)
729 goto err_rmdir;
730 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400731out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800732 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400733 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400734err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400735 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400736out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400737 dentry = ERR_PTR(error);
738 goto out;
739}
740
Trond Myklebuste57aed72009-08-09 15:14:26 -0400741static int rpc_rmdir_depopulate(struct dentry *dentry,
742 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400743{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700744 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400745 struct inode *dir;
746 int error;
747
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700748 parent = dget_parent(dentry);
749 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700750 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400751 if (depopulate != NULL)
752 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400753 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800754 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700755 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400756 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500759/**
760 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
761 * @parent: dentry of directory to create new "pipe" in
762 * @name: name of pipe
763 * @private: private data to associate with the pipe, for the caller's use
764 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500765 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800766 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500767 *
768 * Data is made available for userspace to read by calls to
769 * rpc_queue_upcall(). The actual reads will result in calls to
770 * @ops->upcall, which will be called with the file pointer,
771 * message, and userspace buffer to copy to.
772 *
773 * Writes can come at any time, and do not necessarily have to be
774 * responses to upcalls. They will result in calls to @msg->downcall.
775 *
776 * The @private argument passed here will be available to all these methods
777 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
778 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400779struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
780 void *private, const struct rpc_pipe_ops *ops,
781 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400784 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400785 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400786 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400787 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400788
789 if (ops->upcall == NULL)
790 umode &= ~S_IRUGO;
791 if (ops->downcall == NULL)
792 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400794 q.name = name;
795 q.len = strlen(name);
796 q.hash = full_name_hash(q.name, q.len),
797
798 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Stanislav Kinsbursky5bff0382011-11-08 15:09:19 +0300799 dentry = __rpc_lookup_create_exclusive(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400801 goto out;
Trond Myklebust75898062009-08-09 15:14:17 -0400802 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400803 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400804 if (err)
805 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400806out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800807 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400808 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400809out_err:
810 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400811 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800812 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400813 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
Trond Myklebust468039e2008-12-23 15:21:31 -0500816EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500818/**
819 * rpc_unlink - remove a pipe
820 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
821 *
822 * After this call, lookups will no longer find the pipe, and any
823 * attempts to read or write using preexisting opens of the pipe will
824 * return -EPIPE.
825 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400826int
Trond Myklebust5d674762006-07-31 14:11:48 -0700827rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Trond Myklebust5d674762006-07-31 14:11:48 -0700829 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400830 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700831 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Trond Myklebust5d674762006-07-31 14:11:48 -0700833 parent = dget_parent(dentry);
834 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700835 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400836 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800837 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700838 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400839 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Trond Myklebust468039e2008-12-23 15:21:31 -0500841EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Trond Myklebuste57aed72009-08-09 15:14:26 -0400843enum {
844 RPCAUTH_info,
845 RPCAUTH_EOF
846};
847
848static const struct rpc_filelist authfiles[] = {
849 [RPCAUTH_info] = {
850 .name = "info",
851 .i_fop = &rpc_info_operations,
852 .mode = S_IFREG | S_IRUSR,
853 },
854};
855
856static int rpc_clntdir_populate(struct dentry *dentry, void *private)
857{
858 return rpc_populate(dentry,
859 authfiles, RPCAUTH_info, RPCAUTH_EOF,
860 private);
861}
862
863static void rpc_clntdir_depopulate(struct dentry *dentry)
864{
865 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
866}
867
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400868/**
869 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400870 * @dentry: dentry from the rpc_pipefs root to the new directory
871 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400872 * @rpc_client: rpc client to associate with this directory
873 *
874 * This creates a directory at the given @path associated with
875 * @rpc_clnt, which will contain a file named "info" with some basic
876 * information about the client, together with any "pipes" that may
877 * later be created using rpc_mkpipe().
878 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400879struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400880 struct qstr *name,
881 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400882{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400883 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
884 rpc_clntdir_populate, rpc_client);
885}
886
887/**
888 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
889 * @dentry: directory to remove
890 */
891int rpc_remove_client_dir(struct dentry *dentry)
892{
893 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400894}
895
Trond Myklebust8854e822009-08-09 15:14:30 -0400896static const struct rpc_filelist cache_pipefs_files[3] = {
897 [0] = {
898 .name = "channel",
899 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400900 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400901 },
902 [1] = {
903 .name = "content",
904 .i_fop = &content_file_operations_pipefs,
905 .mode = S_IFREG|S_IRUSR,
906 },
907 [2] = {
908 .name = "flush",
909 .i_fop = &cache_flush_operations_pipefs,
910 .mode = S_IFREG|S_IRUSR|S_IWUSR,
911 },
912};
913
914static int rpc_cachedir_populate(struct dentry *dentry, void *private)
915{
916 return rpc_populate(dentry,
917 cache_pipefs_files, 0, 3,
918 private);
919}
920
921static void rpc_cachedir_depopulate(struct dentry *dentry)
922{
923 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
924}
925
926struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
Al Viro64f14262011-07-25 00:35:13 -0400927 umode_t umode, struct cache_detail *cd)
Trond Myklebust8854e822009-08-09 15:14:30 -0400928{
929 return rpc_mkdir_populate(parent, name, umode, NULL,
930 rpc_cachedir_populate, cd);
931}
932
933void rpc_remove_cache_dir(struct dentry *dentry)
934{
935 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * populate the filesystem
940 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700941static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 .alloc_inode = rpc_alloc_inode,
943 .destroy_inode = rpc_destroy_inode,
944 .statfs = simple_statfs,
945};
946
947#define RPCAUTH_GSSMAGIC 0x67596969
948
Trond Myklebustbb156742009-08-09 15:14:21 -0400949/*
950 * We have a single directory with 1 node in it.
951 */
952enum {
953 RPCAUTH_lockd,
954 RPCAUTH_mount,
955 RPCAUTH_nfs,
956 RPCAUTH_portmap,
957 RPCAUTH_statd,
958 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400959 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -0400960 RPCAUTH_RootEOF
961};
962
963static const struct rpc_filelist files[] = {
964 [RPCAUTH_lockd] = {
965 .name = "lockd",
966 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
967 },
968 [RPCAUTH_mount] = {
969 .name = "mount",
970 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
971 },
972 [RPCAUTH_nfs] = {
973 .name = "nfs",
974 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
975 },
976 [RPCAUTH_portmap] = {
977 .name = "portmap",
978 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
979 },
980 [RPCAUTH_statd] = {
981 .name = "statd",
982 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
983 },
984 [RPCAUTH_nfsd4_cb] = {
985 .name = "nfsd4_cb",
986 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
987 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400988 [RPCAUTH_cache] = {
989 .name = "cache",
990 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
991 },
Trond Myklebustbb156742009-08-09 15:14:21 -0400992};
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994static int
995rpc_fill_super(struct super_block *sb, void *data, int silent)
996{
997 struct inode *inode;
998 struct dentry *root;
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +0300999 struct net *net = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 sb->s_blocksize = PAGE_CACHE_SIZE;
1002 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1003 sb->s_magic = RPCAUTH_GSSMAGIC;
1004 sb->s_op = &s_ops;
1005 sb->s_time_gran = 1;
1006
1007 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1008 if (!inode)
1009 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001010 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (!root) {
1012 iput(inode);
1013 return -ENOMEM;
1014 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001015 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001016 return -ENOMEM;
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001017 sb->s_fs_info = get_net(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019}
1020
Al Virofc14f2f2010-07-25 01:48:30 +04001021static struct dentry *
1022rpc_mount(struct file_system_type *fs_type,
1023 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
Stanislav Kinsbursky38b0da72011-12-26 15:38:56 +03001025 return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001028void rpc_kill_sb(struct super_block *sb)
1029{
1030 struct net *net = sb->s_fs_info;
1031
1032 put_net(net);
1033 kill_litter_super(sb);
1034}
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static struct file_system_type rpc_pipe_fs_type = {
1037 .owner = THIS_MODULE,
1038 .name = "rpc_pipefs",
Al Virofc14f2f2010-07-25 01:48:30 +04001039 .mount = rpc_mount,
Stanislav Kinsbursky021c68d2011-12-26 15:39:04 +03001040 .kill_sb = rpc_kill_sb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041};
1042
1043static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001044init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1047
Christoph Lametera35afb82007-05-16 22:10:57 -07001048 inode_init_once(&rpci->vfs_inode);
1049 rpci->private = NULL;
1050 rpci->nreaders = 0;
1051 rpci->nwriters = 0;
1052 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -04001053 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -07001054 INIT_LIST_HEAD(&rpci->pipe);
1055 rpci->pipelen = 0;
1056 init_waitqueue_head(&rpci->waitq);
1057 INIT_DELAYED_WORK(&rpci->queue_timeout,
1058 rpc_timeout_upcall_queue);
1059 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062int register_rpc_pipefs(void)
1063{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001064 int err;
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001067 sizeof(struct rpc_inode),
1068 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1069 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001070 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (!rpc_inode_cachep)
1072 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001073 err = register_filesystem(&rpc_pipe_fs_type);
1074 if (err) {
1075 kmem_cache_destroy(rpc_inode_cachep);
1076 return err;
1077 }
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return 0;
1080}
1081
1082void unregister_rpc_pipefs(void)
1083{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001084 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 unregister_filesystem(&rpc_pipe_fs_type);
1086}
Michal Schmidtdcbf8c32011-07-02 00:47:12 +02001087
1088/* Make 'mount -t rpc_pipefs ...' autoload this module. */
1089MODULE_ALIAS("rpc_pipefs");