blob: 95ccbcf45d3eb64c6ee84c4767c85e7c5a9ee694 [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>
Frederic Weisbecker674b6042010-05-19 15:08:17 +020030#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Eric Dumazetba899662005-08-26 12:05:31 -070032static struct vfsmount *rpc_mount __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static int rpc_mount_count;
34
35static struct file_system_type rpc_pipe_fs_type;
36
37
Christoph Lametere18b8902006-12-06 20:33:20 -080038static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define RPC_UPCALL_TIMEOUT (30*HZ)
41
Trond Myklebust9842ef32006-02-01 12:18:44 -050042static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
43 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050044{
45 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050046
Trond Myklebust9842ef32006-02-01 12:18:44 -050047 if (list_empty(head))
48 return;
49 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050050 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust9842ef32006-02-01 12:18:44 -050051 list_del(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050052 msg->errno = err;
53 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050054 } while (!list_empty(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 wake_up(&rpci->waitq);
56}
57
58static void
David Howells65f27f32006-11-22 14:55:48 +000059rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Trond Myklebust9842ef32006-02-01 12:18:44 -050061 LIST_HEAD(free_list);
David Howells65f27f32006-11-22 14:55:48 +000062 struct rpc_inode *rpci =
63 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Trond Myklebust9842ef32006-02-01 12:18:44 -050067 spin_lock(&inode->i_lock);
68 if (rpci->ops == NULL) {
69 spin_unlock(&inode->i_lock);
70 return;
71 }
72 destroy_msg = rpci->ops->destroy_msg;
73 if (rpci->nreaders == 0) {
74 list_splice_init(&rpci->pipe, &free_list);
75 rpci->pipelen = 0;
76 }
77 spin_unlock(&inode->i_lock);
78 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
J. Bruce Fields93a44a72007-11-06 13:06:03 -050081/**
Ben Hutchings1a5778a2010-02-14 22:35:47 -080082 * rpc_queue_upcall - queue an upcall message to userspace
J. Bruce Fields93a44a72007-11-06 13:06:03 -050083 * @inode: inode of upcall pipe on which to queue given message
84 * @msg: message to queue
85 *
86 * Call with an @inode created by rpc_mkpipe() to queue an upcall.
87 * A userspace process may then later read the upcall by performing a
88 * read on an open file for this inode. It is up to the caller to
89 * initialize the fields of @msg (other than @msg->list) appropriately.
90 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070091int
92rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
93{
94 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -040095 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Trond Myklebust9842ef32006-02-01 12:18:44 -050097 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -040098 if (rpci->ops == NULL)
99 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (rpci->nreaders) {
101 list_add_tail(&msg->list, &rpci->pipe);
102 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400103 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
105 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -0500106 queue_delayed_work(rpciod_workqueue,
107 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 RPC_UPCALL_TIMEOUT);
109 list_add_tail(&msg->list, &rpci->pipe);
110 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400111 res = 0;
112 }
113out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500114 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 wake_up(&rpci->waitq);
116 return res;
117}
Trond Myklebust468039e2008-12-23 15:21:31 -0500118EXPORT_SYMBOL_GPL(rpc_queue_upcall);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Trond Myklebust6070fe62005-10-27 22:12:46 -0400120static inline void
121rpc_inode_setowner(struct inode *inode, void *private)
122{
123 RPC_I(inode)->private = private;
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void
127rpc_close_pipes(struct inode *inode)
128{
129 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebustb693ba42009-08-09 15:14:15 -0400130 const struct rpc_pipe_ops *ops;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500131 int need_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800133 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500134 ops = rpci->ops;
135 if (ops != NULL) {
136 LIST_HEAD(free_list);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500137 spin_lock(&inode->i_lock);
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500138 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500140 list_splice_init(&rpci->in_upcall, &free_list);
141 list_splice_init(&rpci->pipe, &free_list);
142 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500144 spin_unlock(&inode->i_lock);
145 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
146 rpci->nwriters = 0;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500147 if (need_release && ops->release_pipe)
Trond Myklebust9842ef32006-02-01 12:18:44 -0500148 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400149 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400151 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800152 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static struct inode *
156rpc_alloc_inode(struct super_block *sb)
157{
158 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800159 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (!rpci)
161 return NULL;
162 return &rpci->vfs_inode;
163}
164
165static void
166rpc_destroy_inode(struct inode *inode)
167{
168 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
169}
170
171static int
172rpc_pipe_open(struct inode *inode, struct file *filp)
173{
174 struct rpc_inode *rpci = RPC_I(inode);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500175 int first_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 int res = -ENXIO;
177
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800178 mutex_lock(&inode->i_mutex);
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500179 if (rpci->ops == NULL)
180 goto out;
181 first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
182 if (first_open && rpci->ops->open_pipe) {
183 res = rpci->ops->open_pipe(inode);
184 if (res)
185 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500187 if (filp->f_mode & FMODE_READ)
188 rpci->nreaders++;
189 if (filp->f_mode & FMODE_WRITE)
190 rpci->nwriters++;
191 res = 0;
192out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800193 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return res;
195}
196
197static int
198rpc_pipe_release(struct inode *inode, struct file *filp)
199{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100200 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct rpc_pipe_msg *msg;
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500202 int last_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800204 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (rpci->ops == NULL)
206 goto out;
207 msg = (struct rpc_pipe_msg *)filp->private_data;
208 if (msg != NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500209 spin_lock(&inode->i_lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500210 msg->errno = -EAGAIN;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500211 list_del(&msg->list);
212 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 rpci->ops->destroy_msg(msg);
214 }
215 if (filp->f_mode & FMODE_WRITE)
216 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500217 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500219 if (rpci->nreaders == 0) {
220 LIST_HEAD(free_list);
221 spin_lock(&inode->i_lock);
222 list_splice_init(&rpci->pipe, &free_list);
223 rpci->pipelen = 0;
224 spin_unlock(&inode->i_lock);
225 rpc_purge_list(rpci, &free_list,
226 rpci->ops->destroy_msg, -EAGAIN);
227 }
228 }
\"J. Bruce Fields\e7128042008-12-23 16:09:47 -0500229 last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
230 if (last_close && rpci->ops->release_pipe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 rpci->ops->release_pipe(inode);
232out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800233 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
235}
236
237static ssize_t
238rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
239{
Josef Sipek303b46b2006-12-08 02:37:42 -0800240 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 struct rpc_inode *rpci = RPC_I(inode);
242 struct rpc_pipe_msg *msg;
243 int res = 0;
244
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800245 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (rpci->ops == NULL) {
247 res = -EPIPE;
248 goto out_unlock;
249 }
250 msg = filp->private_data;
251 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500252 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!list_empty(&rpci->pipe)) {
254 msg = list_entry(rpci->pipe.next,
255 struct rpc_pipe_msg,
256 list);
257 list_move(&msg->list, &rpci->in_upcall);
258 rpci->pipelen -= msg->len;
259 filp->private_data = msg;
260 msg->copied = 0;
261 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500262 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (msg == NULL)
264 goto out_unlock;
265 }
266 /* NOTE: it is up to the callback to update msg->copied */
267 res = rpci->ops->upcall(filp, msg, buf, len);
268 if (res < 0 || msg->len == msg->copied) {
269 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500270 spin_lock(&inode->i_lock);
271 list_del(&msg->list);
272 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 rpci->ops->destroy_msg(msg);
274 }
275out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800276 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 return res;
278}
279
280static ssize_t
281rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
282{
Josef Sipek303b46b2006-12-08 02:37:42 -0800283 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct rpc_inode *rpci = RPC_I(inode);
285 int res;
286
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800287 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 res = -EPIPE;
289 if (rpci->ops != NULL)
290 res = rpci->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800291 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return res;
293}
294
295static unsigned int
296rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
297{
298 struct rpc_inode *rpci;
299 unsigned int mask = 0;
300
Josef Sipek303b46b2006-12-08 02:37:42 -0800301 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 poll_wait(filp, &rpci->waitq, wait);
303
304 mask = POLLOUT | POLLWRNORM;
305 if (rpci->ops == NULL)
306 mask |= POLLERR | POLLHUP;
J. Bruce Fieldseda4f9b2007-11-06 13:05:36 -0500307 if (filp->private_data || !list_empty(&rpci->pipe))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 mask |= POLLIN | POLLRDNORM;
309 return mask;
310}
311
312static int
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200313rpc_pipe_ioctl_unlocked(struct file *filp, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Josef Sipek303b46b2006-12-08 02:37:42 -0800315 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 int len;
317
318 switch (cmd) {
319 case FIONREAD:
320 if (rpci->ops == NULL)
321 return -EPIPE;
322 len = rpci->pipelen;
323 if (filp->private_data) {
324 struct rpc_pipe_msg *msg;
325 msg = (struct rpc_pipe_msg *)filp->private_data;
326 len += msg->len - msg->copied;
327 }
328 return put_user(len, (int __user *)arg);
329 default:
330 return -EINVAL;
331 }
332}
333
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200334static long
335rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
336{
337 long ret;
338
339 lock_kernel();
340 ret = rpc_pipe_ioctl_unlocked(filp, cmd, arg);
341 unlock_kernel();
342
343 return ret;
344}
345
Arjan van de Venda7071d2007-02-12 00:55:36 -0800346static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .owner = THIS_MODULE,
348 .llseek = no_llseek,
349 .read = rpc_pipe_read,
350 .write = rpc_pipe_write,
351 .poll = rpc_pipe_poll,
Frederic Weisbecker674b6042010-05-19 15:08:17 +0200352 .unlocked_ioctl = rpc_pipe_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 .open = rpc_pipe_open,
354 .release = rpc_pipe_release,
355};
356
357static int
358rpc_show_info(struct seq_file *m, void *v)
359{
360 struct rpc_clnt *clnt = m->private;
361
362 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
363 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
364 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400365 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
366 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
J. Bruce Fieldsbf19aac2007-09-26 14:38:09 -0400367 seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return 0;
369}
370
371static int
372rpc_info_open(struct inode *inode, struct file *file)
373{
374 struct rpc_clnt *clnt;
375 int ret = single_open(file, rpc_show_info, NULL);
376
377 if (!ret) {
378 struct seq_file *m = file->private_data;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800379 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 clnt = RPC_I(inode)->private;
381 if (clnt) {
Trond Myklebust34f52e32007-06-14 16:40:31 -0400382 kref_get(&clnt->cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 m->private = clnt;
384 } else {
385 single_release(inode, file);
386 ret = -EINVAL;
387 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800388 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390 return ret;
391}
392
393static int
394rpc_info_release(struct inode *inode, struct file *file)
395{
396 struct seq_file *m = file->private_data;
397 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
398
399 if (clnt)
400 rpc_release_client(clnt);
401 return single_release(inode, file);
402}
403
Arjan van de Venda7071d2007-02-12 00:55:36 -0800404static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 .owner = THIS_MODULE,
406 .open = rpc_info_open,
407 .read = seq_read,
408 .llseek = seq_lseek,
409 .release = rpc_info_release,
410};
411
412
413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 * Description of fs contents.
415 */
416struct rpc_filelist {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400417 const char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800418 const struct file_operations *i_fop;
Trond Myklebust7364af62009-08-09 15:14:16 -0400419 umode_t mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420};
421
Trond Myklebust54281542006-03-20 13:44:49 -0500422struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Trond Myklebust54281542006-03-20 13:44:49 -0500424 int err;
425
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400426 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500427 if (err != 0)
428 return ERR_PTR(err);
429 return rpc_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400431EXPORT_SYMBOL_GPL(rpc_get_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Trond Myklebust54281542006-03-20 13:44:49 -0500433void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 simple_release_fs(&rpc_mount, &rpc_mount_count);
436}
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400437EXPORT_SYMBOL_GPL(rpc_put_mount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Trond Myklebust62e17612007-06-08 14:14:46 -0400439static int rpc_delete_dentry(struct dentry *dentry)
440{
441 return 1;
442}
443
Al Viro3ba13d12009-02-20 06:02:22 +0000444static const struct dentry_operations rpc_dentry_operations = {
Trond Myklebust62e17612007-06-08 14:14:46 -0400445 .d_delete = rpc_delete_dentry,
446};
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static struct inode *
Trond Myklebust7364af62009-08-09 15:14:16 -0400449rpc_get_inode(struct super_block *sb, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct inode *inode = new_inode(sb);
452 if (!inode)
453 return NULL;
454 inode->i_mode = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
456 switch(mode & S_IFMT) {
457 case S_IFDIR:
458 inode->i_fop = &simple_dir_operations;
459 inode->i_op = &simple_dir_inode_operations;
Dave Hansend8c76e62006-09-30 23:29:04 -0700460 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 default:
462 break;
463 }
464 return inode;
465}
466
Trond Myklebust75898062009-08-09 15:14:17 -0400467static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
468 umode_t mode,
469 const struct file_operations *i_fop,
470 void *private)
471{
472 struct inode *inode;
473
474 BUG_ON(!d_unhashed(dentry));
475 inode = rpc_get_inode(dir->i_sb, mode);
476 if (!inode)
477 goto out_err;
478 inode->i_ino = iunique(dir->i_sb, 100);
479 if (i_fop)
480 inode->i_fop = i_fop;
481 if (private)
482 rpc_inode_setowner(inode, private);
483 d_add(dentry, inode);
484 return 0;
485out_err:
486 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
487 __FILE__, __func__, dentry->d_name.name);
488 dput(dentry);
489 return -ENOMEM;
490}
491
Trond Myklebustac6fece2009-08-09 15:14:20 -0400492static int __rpc_create(struct inode *dir, struct dentry *dentry,
493 umode_t mode,
494 const struct file_operations *i_fop,
495 void *private)
496{
497 int err;
498
499 err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
500 if (err)
501 return err;
502 fsnotify_create(dir, dentry);
503 return 0;
504}
505
Trond Myklebust75898062009-08-09 15:14:17 -0400506static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
507 umode_t mode,
508 const struct file_operations *i_fop,
509 void *private)
510{
511 int err;
512
513 err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
514 if (err)
515 return err;
516 inc_nlink(dir);
517 fsnotify_mkdir(dir, dentry);
518 return 0;
519}
520
521static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
522 umode_t mode,
523 const struct file_operations *i_fop,
524 void *private,
525 const struct rpc_pipe_ops *ops,
526 int flags)
527{
528 struct rpc_inode *rpci;
529 int err;
530
531 err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
532 if (err)
533 return err;
534 rpci = RPC_I(dentry->d_inode);
535 rpci->nkern_readwriters = 1;
536 rpci->private = private;
537 rpci->flags = flags;
538 rpci->ops = ops;
539 fsnotify_create(dir, dentry);
540 return 0;
541}
542
Trond Myklebustac6fece2009-08-09 15:14:20 -0400543static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
544{
545 int ret;
546
547 dget(dentry);
548 ret = simple_rmdir(dir, dentry);
549 d_delete(dentry);
550 dput(dentry);
551 return ret;
552}
553
Trond Myklebust810d90b2009-08-09 15:14:18 -0400554static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
555{
556 int ret;
557
558 dget(dentry);
559 ret = simple_unlink(dir, dentry);
560 d_delete(dentry);
561 dput(dentry);
562 return ret;
563}
564
565static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
566{
567 struct inode *inode = dentry->d_inode;
568 struct rpc_inode *rpci = RPC_I(inode);
569
570 rpci->nkern_readwriters--;
571 if (rpci->nkern_readwriters != 0)
572 return 0;
573 rpc_close_pipes(inode);
574 return __rpc_unlink(dir, dentry);
575}
576
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400577static struct dentry *__rpc_lookup_create(struct dentry *parent,
578 struct qstr *name)
579{
580 struct dentry *dentry;
581
582 dentry = d_lookup(parent, name);
583 if (!dentry) {
584 dentry = d_alloc(parent, name);
585 if (!dentry) {
586 dentry = ERR_PTR(-ENOMEM);
587 goto out_err;
588 }
589 }
590 if (!dentry->d_inode)
591 dentry->d_op = &rpc_dentry_operations;
592out_err:
593 return dentry;
594}
595
596static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
597 struct qstr *name)
598{
599 struct dentry *dentry;
600
601 dentry = __rpc_lookup_create(parent, name);
Dan Carpenterf1f0abe2010-03-21 12:10:34 -0400602 if (IS_ERR(dentry))
603 return dentry;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400604 if (dentry->d_inode == NULL)
605 return dentry;
606 dput(dentry);
607 return ERR_PTR(-EEXIST);
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/*
611 * FIXME: This probably has races.
612 */
Trond Myklebustac6fece2009-08-09 15:14:20 -0400613static void __rpc_depopulate(struct dentry *parent,
614 const struct rpc_filelist *files,
615 int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 struct inode *dir = parent->d_inode;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400618 struct dentry *dentry;
619 struct qstr name;
620 int i;
621
622 for (i = start; i < eof; i++) {
623 name.name = files[i].name;
624 name.len = strlen(files[i].name);
625 name.hash = full_name_hash(name.name, name.len);
626 dentry = d_lookup(parent, &name);
627
628 if (dentry == NULL)
629 continue;
630 if (dentry->d_inode == NULL)
631 goto next;
632 switch (dentry->d_inode->i_mode & S_IFMT) {
633 default:
634 BUG();
635 case S_IFREG:
636 __rpc_unlink(dir, dentry);
637 break;
638 case S_IFDIR:
639 __rpc_rmdir(dir, dentry);
640 }
641next:
642 dput(dentry);
643 }
644}
645
646static void rpc_depopulate(struct dentry *parent,
647 const struct rpc_filelist *files,
648 int start, int eof)
649{
650 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Arjan van de Venc6573c22006-07-03 00:25:16 -0700652 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400653 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800654 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Trond Myklebustac6fece2009-08-09 15:14:20 -0400657static int rpc_populate(struct dentry *parent,
658 const struct rpc_filelist *files,
659 int start, int eof,
660 void *private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Trond Myklebustac6fece2009-08-09 15:14:20 -0400662 struct inode *dir = parent->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 struct dentry *dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400664 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800666 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 for (i = start; i < eof; i++) {
Trond Myklebustac6fece2009-08-09 15:14:20 -0400668 struct qstr q;
669
670 q.name = files[i].name;
671 q.len = strlen(files[i].name);
672 q.hash = full_name_hash(q.name, q.len);
673 dentry = __rpc_lookup_create_exclusive(parent, &q);
674 err = PTR_ERR(dentry);
675 if (IS_ERR(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto out_bad;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400677 switch (files[i].mode & S_IFMT) {
678 default:
679 BUG();
680 case S_IFREG:
681 err = __rpc_create(dir, dentry,
682 files[i].mode,
683 files[i].i_fop,
684 private);
685 break;
686 case S_IFDIR:
687 err = __rpc_mkdir(dir, dentry,
688 files[i].mode,
689 NULL,
690 private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Trond Myklebustac6fece2009-08-09 15:14:20 -0400692 if (err != 0)
693 goto out_bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800695 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return 0;
697out_bad:
Trond Myklebustac6fece2009-08-09 15:14:20 -0400698 __rpc_depopulate(parent, files, start, eof);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800699 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800701 __FILE__, __func__, parent->d_name.name);
Trond Myklebustac6fece2009-08-09 15:14:20 -0400702 return err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400703}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100704
Trond Myklebuste57aed72009-08-09 15:14:26 -0400705static struct dentry *rpc_mkdir_populate(struct dentry *parent,
706 struct qstr *name, umode_t mode, void *private,
707 int (*populate)(struct dentry *, void *), void *args_populate)
Trond Myklebustf1345852005-09-23 11:08:25 -0400708{
Trond Myklebustf1345852005-09-23 11:08:25 -0400709 struct dentry *dentry;
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400710 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400711 int error;
712
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400713 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
714 dentry = __rpc_lookup_create_exclusive(parent, name);
Trond Myklebustf1345852005-09-23 11:08:25 -0400715 if (IS_ERR(dentry))
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400716 goto out;
717 error = __rpc_mkdir(dir, dentry, mode, NULL, private);
Trond Myklebust75898062009-08-09 15:14:17 -0400718 if (error != 0)
719 goto out_err;
Trond Myklebuste57aed72009-08-09 15:14:26 -0400720 if (populate != NULL) {
721 error = populate(dentry, args_populate);
722 if (error)
723 goto err_rmdir;
724 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400725out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800726 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400727 return dentry;
Trond Myklebustac6fece2009-08-09 15:14:20 -0400728err_rmdir:
Trond Myklebustf1345852005-09-23 11:08:25 -0400729 __rpc_rmdir(dir, dentry);
Trond Myklebust75898062009-08-09 15:14:17 -0400730out_err:
Trond Myklebustf1345852005-09-23 11:08:25 -0400731 dentry = ERR_PTR(error);
732 goto out;
733}
734
Trond Myklebuste57aed72009-08-09 15:14:26 -0400735static int rpc_rmdir_depopulate(struct dentry *dentry,
736 void (*depopulate)(struct dentry *))
Trond Myklebustf1345852005-09-23 11:08:25 -0400737{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700738 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400739 struct inode *dir;
740 int error;
741
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700742 parent = dget_parent(dentry);
743 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700744 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebuste57aed72009-08-09 15:14:26 -0400745 if (depopulate != NULL)
746 depopulate(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400747 error = __rpc_rmdir(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800748 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700749 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400750 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500753/**
754 * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
755 * @parent: dentry of directory to create new "pipe" in
756 * @name: name of pipe
757 * @private: private data to associate with the pipe, for the caller's use
758 * @ops: operations defining the behavior of the pipe: upcall, downcall,
\"J. Bruce Fields\c3810602008-12-23 16:08:32 -0500759 * release_pipe, open_pipe, and destroy_msg.
Randy Dunlap65b6e422008-02-13 15:03:23 -0800760 * @flags: rpc_inode flags
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500761 *
762 * Data is made available for userspace to read by calls to
763 * rpc_queue_upcall(). The actual reads will result in calls to
764 * @ops->upcall, which will be called with the file pointer,
765 * message, and userspace buffer to copy to.
766 *
767 * Writes can come at any time, and do not necessarily have to be
768 * responses to upcalls. They will result in calls to @msg->downcall.
769 *
770 * The @private argument passed here will be available to all these methods
771 * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
772 */
Trond Myklebustb693ba42009-08-09 15:14:15 -0400773struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
774 void *private, const struct rpc_pipe_ops *ops,
775 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 struct dentry *dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400778 struct inode *dir = parent->d_inode;
Trond Myklebust7364af62009-08-09 15:14:16 -0400779 umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400780 struct qstr q;
Trond Myklebust75898062009-08-09 15:14:17 -0400781 int err;
Trond Myklebust7364af62009-08-09 15:14:16 -0400782
783 if (ops->upcall == NULL)
784 umode &= ~S_IRUGO;
785 if (ops->downcall == NULL)
786 umode &= ~S_IWUGO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400788 q.name = name;
789 q.len = strlen(name);
790 q.hash = full_name_hash(q.name, q.len),
791
792 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
793 dentry = __rpc_lookup_create(parent, &q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (IS_ERR(dentry))
Trond Myklebustcfeaa4a2009-08-09 15:14:20 -0400795 goto out;
Trond Myklebust34f30892007-06-07 18:28:02 -0400796 if (dentry->d_inode) {
Trond Myklebust75898062009-08-09 15:14:17 -0400797 struct rpc_inode *rpci = RPC_I(dentry->d_inode);
Trond Myklebust34f30892007-06-07 18:28:02 -0400798 if (rpci->private != private ||
799 rpci->ops != ops ||
800 rpci->flags != flags) {
801 dput (dentry);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400802 err = -EBUSY;
803 goto out_err;
Trond Myklebust34f30892007-06-07 18:28:02 -0400804 }
Trond Myklebust03a12562007-06-08 14:14:53 -0400805 rpci->nkern_readwriters++;
Trond Myklebust34f30892007-06-07 18:28:02 -0400806 goto out;
807 }
Trond Myklebust75898062009-08-09 15:14:17 -0400808
809 err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
Trond Myklebust810d90b2009-08-09 15:14:18 -0400810 private, ops, flags);
Trond Myklebust75898062009-08-09 15:14:17 -0400811 if (err)
812 goto out_err;
Trond Myklebustf1345852005-09-23 11:08:25 -0400813out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800814 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400815 return dentry;
Trond Myklebust75898062009-08-09 15:14:17 -0400816out_err:
817 dentry = ERR_PTR(err);
Trond Myklebust158998b2006-08-24 01:03:17 -0400818 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800819 __FILE__, __func__, parent->d_name.name, name,
Trond Myklebust75898062009-08-09 15:14:17 -0400820 err);
Trond Myklebustf1345852005-09-23 11:08:25 -0400821 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
Trond Myklebust468039e2008-12-23 15:21:31 -0500823EXPORT_SYMBOL_GPL(rpc_mkpipe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
J. Bruce Fields93a44a72007-11-06 13:06:03 -0500825/**
826 * rpc_unlink - remove a pipe
827 * @dentry: dentry for the pipe, as returned from rpc_mkpipe
828 *
829 * After this call, lookups will no longer find the pipe, and any
830 * attempts to read or write using preexisting opens of the pipe will
831 * return -EPIPE.
832 */
Trond Myklebustf1345852005-09-23 11:08:25 -0400833int
Trond Myklebust5d674762006-07-31 14:11:48 -0700834rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Trond Myklebust5d674762006-07-31 14:11:48 -0700836 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400837 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700838 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Trond Myklebust5d674762006-07-31 14:11:48 -0700840 parent = dget_parent(dentry);
841 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700842 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust810d90b2009-08-09 15:14:18 -0400843 error = __rpc_rmpipe(dir, dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800844 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700845 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400846 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Trond Myklebust468039e2008-12-23 15:21:31 -0500848EXPORT_SYMBOL_GPL(rpc_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Trond Myklebuste57aed72009-08-09 15:14:26 -0400850enum {
851 RPCAUTH_info,
852 RPCAUTH_EOF
853};
854
855static const struct rpc_filelist authfiles[] = {
856 [RPCAUTH_info] = {
857 .name = "info",
858 .i_fop = &rpc_info_operations,
859 .mode = S_IFREG | S_IRUSR,
860 },
861};
862
863static int rpc_clntdir_populate(struct dentry *dentry, void *private)
864{
865 return rpc_populate(dentry,
866 authfiles, RPCAUTH_info, RPCAUTH_EOF,
867 private);
868}
869
870static void rpc_clntdir_depopulate(struct dentry *dentry)
871{
872 rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
873}
874
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400875/**
876 * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
Randy Dunlap4111d4f2009-09-23 14:36:38 -0400877 * @dentry: dentry from the rpc_pipefs root to the new directory
878 * @name: &struct qstr for the name
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400879 * @rpc_client: rpc client to associate with this directory
880 *
881 * This creates a directory at the given @path associated with
882 * @rpc_clnt, which will contain a file named "info" with some basic
883 * information about the client, together with any "pipes" that may
884 * later be created using rpc_mkpipe().
885 */
Trond Myklebust23ac6582009-08-09 15:14:25 -0400886struct dentry *rpc_create_client_dir(struct dentry *dentry,
Trond Myklebuste57aed72009-08-09 15:14:26 -0400887 struct qstr *name,
888 struct rpc_clnt *rpc_client)
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400889{
Trond Myklebuste57aed72009-08-09 15:14:26 -0400890 return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
891 rpc_clntdir_populate, rpc_client);
892}
893
894/**
895 * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
896 * @dentry: directory to remove
897 */
898int rpc_remove_client_dir(struct dentry *dentry)
899{
900 return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
Trond Myklebust7d59d1e2009-08-09 15:14:23 -0400901}
902
Trond Myklebust8854e822009-08-09 15:14:30 -0400903static const struct rpc_filelist cache_pipefs_files[3] = {
904 [0] = {
905 .name = "channel",
906 .i_fop = &cache_file_operations_pipefs,
Trond Myklebust96c61cb2009-08-19 18:12:21 -0400907 .mode = S_IFREG|S_IRUSR|S_IWUSR,
Trond Myklebust8854e822009-08-09 15:14:30 -0400908 },
909 [1] = {
910 .name = "content",
911 .i_fop = &content_file_operations_pipefs,
912 .mode = S_IFREG|S_IRUSR,
913 },
914 [2] = {
915 .name = "flush",
916 .i_fop = &cache_flush_operations_pipefs,
917 .mode = S_IFREG|S_IRUSR|S_IWUSR,
918 },
919};
920
921static int rpc_cachedir_populate(struct dentry *dentry, void *private)
922{
923 return rpc_populate(dentry,
924 cache_pipefs_files, 0, 3,
925 private);
926}
927
928static void rpc_cachedir_depopulate(struct dentry *dentry)
929{
930 rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
931}
932
933struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
934 mode_t umode, struct cache_detail *cd)
935{
936 return rpc_mkdir_populate(parent, name, umode, NULL,
937 rpc_cachedir_populate, cd);
938}
939
940void rpc_remove_cache_dir(struct dentry *dentry)
941{
942 rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945/*
946 * populate the filesystem
947 */
Alexey Dobriyanb87221d2009-09-21 17:01:09 -0700948static const struct super_operations s_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 .alloc_inode = rpc_alloc_inode,
950 .destroy_inode = rpc_destroy_inode,
951 .statfs = simple_statfs,
952};
953
954#define RPCAUTH_GSSMAGIC 0x67596969
955
Trond Myklebustbb156742009-08-09 15:14:21 -0400956/*
957 * We have a single directory with 1 node in it.
958 */
959enum {
960 RPCAUTH_lockd,
961 RPCAUTH_mount,
962 RPCAUTH_nfs,
963 RPCAUTH_portmap,
964 RPCAUTH_statd,
965 RPCAUTH_nfsd4_cb,
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400966 RPCAUTH_cache,
Trond Myklebustbb156742009-08-09 15:14:21 -0400967 RPCAUTH_RootEOF
968};
969
970static const struct rpc_filelist files[] = {
971 [RPCAUTH_lockd] = {
972 .name = "lockd",
973 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
974 },
975 [RPCAUTH_mount] = {
976 .name = "mount",
977 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
978 },
979 [RPCAUTH_nfs] = {
980 .name = "nfs",
981 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
982 },
983 [RPCAUTH_portmap] = {
984 .name = "portmap",
985 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
986 },
987 [RPCAUTH_statd] = {
988 .name = "statd",
989 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
990 },
991 [RPCAUTH_nfsd4_cb] = {
992 .name = "nfsd4_cb",
993 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
994 },
Trond Myklebuste571cbf2009-08-19 18:12:27 -0400995 [RPCAUTH_cache] = {
996 .name = "cache",
997 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
998 },
Trond Myklebustbb156742009-08-09 15:14:21 -0400999};
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001static int
1002rpc_fill_super(struct super_block *sb, void *data, int silent)
1003{
1004 struct inode *inode;
1005 struct dentry *root;
1006
1007 sb->s_blocksize = PAGE_CACHE_SIZE;
1008 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1009 sb->s_magic = RPCAUTH_GSSMAGIC;
1010 sb->s_op = &s_ops;
1011 sb->s_time_gran = 1;
1012
1013 inode = rpc_get_inode(sb, S_IFDIR | 0755);
1014 if (!inode)
1015 return -ENOMEM;
Al Virofc7bed82010-01-25 18:30:38 -05001016 sb->s_root = root = d_alloc_root(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!root) {
1018 iput(inode);
1019 return -ENOMEM;
1020 }
Trond Myklebustac6fece2009-08-09 15:14:20 -04001021 if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
Al Virofc7bed82010-01-25 18:30:38 -05001022 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
David Howells454e2392006-06-23 02:02:57 -07001026static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027rpc_get_sb(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -07001028 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
David Howells454e2392006-06-23 02:02:57 -07001030 return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
1033static struct file_system_type rpc_pipe_fs_type = {
1034 .owner = THIS_MODULE,
1035 .name = "rpc_pipefs",
1036 .get_sb = rpc_get_sb,
1037 .kill_sb = kill_litter_super,
1038};
1039
1040static void
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001041init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043 struct rpc_inode *rpci = (struct rpc_inode *) foo;
1044
Christoph Lametera35afb82007-05-16 22:10:57 -07001045 inode_init_once(&rpci->vfs_inode);
1046 rpci->private = NULL;
1047 rpci->nreaders = 0;
1048 rpci->nwriters = 0;
1049 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -04001050 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -07001051 INIT_LIST_HEAD(&rpci->pipe);
1052 rpci->pipelen = 0;
1053 init_waitqueue_head(&rpci->waitq);
1054 INIT_DELAYED_WORK(&rpci->queue_timeout,
1055 rpc_timeout_upcall_queue);
1056 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
1059int register_rpc_pipefs(void)
1060{
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001061 int err;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -08001064 sizeof(struct rpc_inode),
1065 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1066 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +09001067 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!rpc_inode_cachep)
1069 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -07001070 err = register_filesystem(&rpc_pipe_fs_type);
1071 if (err) {
1072 kmem_cache_destroy(rpc_inode_cachep);
1073 return err;
1074 }
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077}
1078
1079void unregister_rpc_pipefs(void)
1080{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001081 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unregister_filesystem(&rpc_pipe_fs_type);
1083}