blob: 669e12a4ed18a140a0ff94de8647aec344c39c66 [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>
17#include <linux/dnotify.h>
18#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>
29
Eric Dumazetba899662005-08-26 12:05:31 -070030static struct vfsmount *rpc_mount __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static int rpc_mount_count;
32
33static struct file_system_type rpc_pipe_fs_type;
34
35
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#define RPC_UPCALL_TIMEOUT (30*HZ)
39
Trond Myklebust9842ef32006-02-01 12:18:44 -050040static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
41 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050042{
43 struct rpc_pipe_msg *msg;
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050044
Trond Myklebust9842ef32006-02-01 12:18:44 -050045 if (list_empty(head))
46 return;
47 do {
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050048 msg = list_entry(head->next, struct rpc_pipe_msg, list);
Trond Myklebust9842ef32006-02-01 12:18:44 -050049 list_del(&msg->list);
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050050 msg->errno = err;
51 destroy_msg(msg);
Trond Myklebust9842ef32006-02-01 12:18:44 -050052 } while (!list_empty(head));
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 wake_up(&rpci->waitq);
54}
55
56static void
David Howells65f27f32006-11-22 14:55:48 +000057rpc_timeout_upcall_queue(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Trond Myklebust9842ef32006-02-01 12:18:44 -050059 LIST_HEAD(free_list);
David Howells65f27f32006-11-22 14:55:48 +000060 struct rpc_inode *rpci =
61 container_of(work, struct rpc_inode, queue_timeout.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct inode *inode = &rpci->vfs_inode;
Trond Myklebust9842ef32006-02-01 12:18:44 -050063 void (*destroy_msg)(struct rpc_pipe_msg *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Trond Myklebust9842ef32006-02-01 12:18:44 -050065 spin_lock(&inode->i_lock);
66 if (rpci->ops == NULL) {
67 spin_unlock(&inode->i_lock);
68 return;
69 }
70 destroy_msg = rpci->ops->destroy_msg;
71 if (rpci->nreaders == 0) {
72 list_splice_init(&rpci->pipe, &free_list);
73 rpci->pipelen = 0;
74 }
75 spin_unlock(&inode->i_lock);
76 rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
79int
80rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
81{
82 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -040083 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Trond Myklebust9842ef32006-02-01 12:18:44 -050085 spin_lock(&inode->i_lock);
Trond Myklebust6070fe62005-10-27 22:12:46 -040086 if (rpci->ops == NULL)
87 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (rpci->nreaders) {
89 list_add_tail(&msg->list, &rpci->pipe);
90 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -040091 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
93 if (list_empty(&rpci->pipe))
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050094 queue_delayed_work(rpciod_workqueue,
95 &rpci->queue_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 RPC_UPCALL_TIMEOUT);
97 list_add_tail(&msg->list, &rpci->pipe);
98 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -040099 res = 0;
100 }
101out:
Trond Myklebust9842ef32006-02-01 12:18:44 -0500102 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 wake_up(&rpci->waitq);
104 return res;
105}
106
Trond Myklebust6070fe62005-10-27 22:12:46 -0400107static inline void
108rpc_inode_setowner(struct inode *inode, void *private)
109{
110 RPC_I(inode)->private = private;
111}
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void
114rpc_close_pipes(struct inode *inode)
115{
116 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500117 struct rpc_pipe_ops *ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800119 mutex_lock(&inode->i_mutex);
Trond Myklebust9842ef32006-02-01 12:18:44 -0500120 ops = rpci->ops;
121 if (ops != NULL) {
122 LIST_HEAD(free_list);
123
124 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 rpci->nreaders = 0;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500126 list_splice_init(&rpci->in_upcall, &free_list);
127 list_splice_init(&rpci->pipe, &free_list);
128 rpci->pipelen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 rpci->ops = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500130 spin_unlock(&inode->i_lock);
131 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
132 rpci->nwriters = 0;
133 if (ops->release_pipe)
134 ops->release_pipe(inode);
Trond Myklebust4011cd92007-08-07 15:33:01 -0400135 cancel_delayed_work_sync(&rpci->queue_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400137 rpc_inode_setowner(inode, NULL);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800138 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct inode *
142rpc_alloc_inode(struct super_block *sb)
143{
144 struct rpc_inode *rpci;
Christoph Lametere94b1762006-12-06 20:33:17 -0800145 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!rpci)
147 return NULL;
148 return &rpci->vfs_inode;
149}
150
151static void
152rpc_destroy_inode(struct inode *inode)
153{
154 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
155}
156
157static int
158rpc_pipe_open(struct inode *inode, struct file *filp)
159{
160 struct rpc_inode *rpci = RPC_I(inode);
161 int res = -ENXIO;
162
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800163 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (rpci->ops != NULL) {
165 if (filp->f_mode & FMODE_READ)
166 rpci->nreaders ++;
167 if (filp->f_mode & FMODE_WRITE)
168 rpci->nwriters ++;
169 res = 0;
170 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800171 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return res;
173}
174
175static int
176rpc_pipe_release(struct inode *inode, struct file *filp)
177{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100178 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct rpc_pipe_msg *msg;
180
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800181 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (rpci->ops == NULL)
183 goto out;
184 msg = (struct rpc_pipe_msg *)filp->private_data;
185 if (msg != NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500186 spin_lock(&inode->i_lock);
Trond Myklebust48e49182005-12-19 17:11:22 -0500187 msg->errno = -EAGAIN;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500188 list_del(&msg->list);
189 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 rpci->ops->destroy_msg(msg);
191 }
192 if (filp->f_mode & FMODE_WRITE)
193 rpci->nwriters --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500194 if (filp->f_mode & FMODE_READ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 rpci->nreaders --;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500196 if (rpci->nreaders == 0) {
197 LIST_HEAD(free_list);
198 spin_lock(&inode->i_lock);
199 list_splice_init(&rpci->pipe, &free_list);
200 rpci->pipelen = 0;
201 spin_unlock(&inode->i_lock);
202 rpc_purge_list(rpci, &free_list,
203 rpci->ops->destroy_msg, -EAGAIN);
204 }
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (rpci->ops->release_pipe)
207 rpci->ops->release_pipe(inode);
208out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800209 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
213static ssize_t
214rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
215{
Josef Sipek303b46b2006-12-08 02:37:42 -0800216 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct rpc_inode *rpci = RPC_I(inode);
218 struct rpc_pipe_msg *msg;
219 int res = 0;
220
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800221 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (rpci->ops == NULL) {
223 res = -EPIPE;
224 goto out_unlock;
225 }
226 msg = filp->private_data;
227 if (msg == NULL) {
Trond Myklebust9842ef32006-02-01 12:18:44 -0500228 spin_lock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!list_empty(&rpci->pipe)) {
230 msg = list_entry(rpci->pipe.next,
231 struct rpc_pipe_msg,
232 list);
233 list_move(&msg->list, &rpci->in_upcall);
234 rpci->pipelen -= msg->len;
235 filp->private_data = msg;
236 msg->copied = 0;
237 }
Trond Myklebust9842ef32006-02-01 12:18:44 -0500238 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (msg == NULL)
240 goto out_unlock;
241 }
242 /* NOTE: it is up to the callback to update msg->copied */
243 res = rpci->ops->upcall(filp, msg, buf, len);
244 if (res < 0 || msg->len == msg->copied) {
245 filp->private_data = NULL;
Trond Myklebust9842ef32006-02-01 12:18:44 -0500246 spin_lock(&inode->i_lock);
247 list_del(&msg->list);
248 spin_unlock(&inode->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 rpci->ops->destroy_msg(msg);
250 }
251out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800252 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return res;
254}
255
256static ssize_t
257rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
258{
Josef Sipek303b46b2006-12-08 02:37:42 -0800259 struct inode *inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct rpc_inode *rpci = RPC_I(inode);
261 int res;
262
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800263 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 res = -EPIPE;
265 if (rpci->ops != NULL)
266 res = rpci->ops->downcall(filp, buf, len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800267 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return res;
269}
270
271static unsigned int
272rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
273{
274 struct rpc_inode *rpci;
275 unsigned int mask = 0;
276
Josef Sipek303b46b2006-12-08 02:37:42 -0800277 rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 poll_wait(filp, &rpci->waitq, wait);
279
280 mask = POLLOUT | POLLWRNORM;
281 if (rpci->ops == NULL)
282 mask |= POLLERR | POLLHUP;
283 if (!list_empty(&rpci->pipe))
284 mask |= POLLIN | POLLRDNORM;
285 return mask;
286}
287
288static int
289rpc_pipe_ioctl(struct inode *ino, struct file *filp,
290 unsigned int cmd, unsigned long arg)
291{
Josef Sipek303b46b2006-12-08 02:37:42 -0800292 struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 int len;
294
295 switch (cmd) {
296 case FIONREAD:
297 if (rpci->ops == NULL)
298 return -EPIPE;
299 len = rpci->pipelen;
300 if (filp->private_data) {
301 struct rpc_pipe_msg *msg;
302 msg = (struct rpc_pipe_msg *)filp->private_data;
303 len += msg->len - msg->copied;
304 }
305 return put_user(len, (int __user *)arg);
306 default:
307 return -EINVAL;
308 }
309}
310
Arjan van de Venda7071d2007-02-12 00:55:36 -0800311static const struct file_operations rpc_pipe_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 .owner = THIS_MODULE,
313 .llseek = no_llseek,
314 .read = rpc_pipe_read,
315 .write = rpc_pipe_write,
316 .poll = rpc_pipe_poll,
317 .ioctl = rpc_pipe_ioctl,
318 .open = rpc_pipe_open,
319 .release = rpc_pipe_release,
320};
321
322static int
323rpc_show_info(struct seq_file *m, void *v)
324{
325 struct rpc_clnt *clnt = m->private;
326
327 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
328 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
329 clnt->cl_prog, clnt->cl_vers);
Chuck Levere7f78652006-08-22 20:06:19 -0400330 seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
331 seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return 0;
333}
334
335static int
336rpc_info_open(struct inode *inode, struct file *file)
337{
338 struct rpc_clnt *clnt;
339 int ret = single_open(file, rpc_show_info, NULL);
340
341 if (!ret) {
342 struct seq_file *m = file->private_data;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800343 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 clnt = RPC_I(inode)->private;
345 if (clnt) {
Trond Myklebust34f52e32007-06-14 16:40:31 -0400346 kref_get(&clnt->cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 m->private = clnt;
348 } else {
349 single_release(inode, file);
350 ret = -EINVAL;
351 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800352 mutex_unlock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354 return ret;
355}
356
357static int
358rpc_info_release(struct inode *inode, struct file *file)
359{
360 struct seq_file *m = file->private_data;
361 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
362
363 if (clnt)
364 rpc_release_client(clnt);
365 return single_release(inode, file);
366}
367
Arjan van de Venda7071d2007-02-12 00:55:36 -0800368static const struct file_operations rpc_info_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 .owner = THIS_MODULE,
370 .open = rpc_info_open,
371 .read = seq_read,
372 .llseek = seq_lseek,
373 .release = rpc_info_release,
374};
375
376
377/*
378 * We have a single directory with 1 node in it.
379 */
380enum {
381 RPCAUTH_Root = 1,
382 RPCAUTH_lockd,
383 RPCAUTH_mount,
384 RPCAUTH_nfs,
385 RPCAUTH_portmap,
386 RPCAUTH_statd,
387 RPCAUTH_RootEOF
388};
389
390/*
391 * Description of fs contents.
392 */
393struct rpc_filelist {
394 char *name;
Arjan van de Ven99ac48f2006-03-28 01:56:41 -0800395 const struct file_operations *i_fop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 int mode;
397};
398
399static struct rpc_filelist files[] = {
400 [RPCAUTH_lockd] = {
401 .name = "lockd",
402 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
403 },
404 [RPCAUTH_mount] = {
405 .name = "mount",
406 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
407 },
408 [RPCAUTH_nfs] = {
409 .name = "nfs",
410 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
411 },
412 [RPCAUTH_portmap] = {
413 .name = "portmap",
414 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
415 },
416 [RPCAUTH_statd] = {
417 .name = "statd",
418 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
419 },
420};
421
422enum {
423 RPCAUTH_info = 2,
424 RPCAUTH_EOF
425};
426
427static struct rpc_filelist authfiles[] = {
428 [RPCAUTH_info] = {
429 .name = "info",
430 .i_fop = &rpc_info_operations,
431 .mode = S_IFREG | S_IRUSR,
432 },
433};
434
Trond Myklebust54281542006-03-20 13:44:49 -0500435struct vfsmount *rpc_get_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Trond Myklebust54281542006-03-20 13:44:49 -0500437 int err;
438
Trond Myklebust1f5ce9e2006-06-09 09:34:16 -0400439 err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
Trond Myklebust54281542006-03-20 13:44:49 -0500440 if (err != 0)
441 return ERR_PTR(err);
442 return rpc_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Trond Myklebust54281542006-03-20 13:44:49 -0500445void rpc_put_mount(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 simple_release_fs(&rpc_mount, &rpc_mount_count);
448}
449
Trond Myklebust62e17612007-06-08 14:14:46 -0400450static int rpc_delete_dentry(struct dentry *dentry)
451{
452 return 1;
453}
454
455static struct dentry_operations rpc_dentry_operations = {
456 .d_delete = rpc_delete_dentry,
457};
458
Trond Myklebustf1345852005-09-23 11:08:25 -0400459static int
460rpc_lookup_parent(char *path, struct nameidata *nd)
461{
Josef 'Jeff' Sipek4ac4efc2007-07-19 01:48:20 -0700462 struct vfsmount *mnt;
463
Trond Myklebustf1345852005-09-23 11:08:25 -0400464 if (path[0] == '\0')
465 return -ENOENT;
Josef 'Jeff' Sipek4ac4efc2007-07-19 01:48:20 -0700466
467 mnt = rpc_get_mount();
468 if (IS_ERR(mnt)) {
Trond Myklebustf1345852005-09-23 11:08:25 -0400469 printk(KERN_WARNING "%s: %s failed to mount "
470 "pseudofilesystem \n", __FILE__, __FUNCTION__);
Josef 'Jeff' Sipek4ac4efc2007-07-19 01:48:20 -0700471 return PTR_ERR(mnt);
Trond Myklebustf1345852005-09-23 11:08:25 -0400472 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400473
Josef 'Jeff' Sipek4ac4efc2007-07-19 01:48:20 -0700474 if (vfs_path_lookup(mnt->mnt_root, mnt, path, LOOKUP_PARENT, nd)) {
Trond Myklebustf1345852005-09-23 11:08:25 -0400475 printk(KERN_WARNING "%s: %s failed to find path %s\n",
476 __FILE__, __FUNCTION__, path);
477 rpc_put_mount();
478 return -ENOENT;
479 }
480 return 0;
481}
482
483static void
484rpc_release_path(struct nameidata *nd)
485{
486 path_release(nd);
487 rpc_put_mount();
488}
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static struct inode *
491rpc_get_inode(struct super_block *sb, int mode)
492{
493 struct inode *inode = new_inode(sb);
494 if (!inode)
495 return NULL;
496 inode->i_mode = mode;
497 inode->i_uid = inode->i_gid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 inode->i_blocks = 0;
499 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
500 switch(mode & S_IFMT) {
501 case S_IFDIR:
502 inode->i_fop = &simple_dir_operations;
503 inode->i_op = &simple_dir_inode_operations;
Dave Hansend8c76e62006-09-30 23:29:04 -0700504 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 default:
506 break;
507 }
508 return inode;
509}
510
511/*
512 * FIXME: This probably has races.
513 */
514static void
Trond Myklebust62e17612007-06-08 14:14:46 -0400515rpc_depopulate(struct dentry *parent, int start, int eof)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
517 struct inode *dir = parent->d_inode;
518 struct list_head *pos, *next;
519 struct dentry *dentry, *dvec[10];
520 int n = 0;
521
Arjan van de Venc6573c22006-07-03 00:25:16 -0700522 mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523repeat:
524 spin_lock(&dcache_lock);
525 list_for_each_safe(pos, next, &parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800526 dentry = list_entry(pos, struct dentry, d_u.d_child);
Trond Myklebust62e17612007-06-08 14:14:46 -0400527 if (!dentry->d_inode ||
528 dentry->d_inode->i_ino < start ||
529 dentry->d_inode->i_ino >= eof)
530 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 spin_lock(&dentry->d_lock);
532 if (!d_unhashed(dentry)) {
533 dget_locked(dentry);
534 __d_drop(dentry);
535 spin_unlock(&dentry->d_lock);
536 dvec[n++] = dentry;
537 if (n == ARRAY_SIZE(dvec))
538 break;
539 } else
540 spin_unlock(&dentry->d_lock);
541 }
542 spin_unlock(&dcache_lock);
543 if (n) {
544 do {
545 dentry = dvec[--n];
Trond Myklebust62e17612007-06-08 14:14:46 -0400546 if (S_ISREG(dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 simple_unlink(dir, dentry);
Trond Myklebust62e17612007-06-08 14:14:46 -0400548 else if (S_ISDIR(dentry->d_inode->i_mode))
549 simple_rmdir(dir, dentry);
550 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 dput(dentry);
552 } while (n);
553 goto repeat;
554 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800555 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
558static int
559rpc_populate(struct dentry *parent,
560 struct rpc_filelist *files,
561 int start, int eof)
562{
563 struct inode *inode, *dir = parent->d_inode;
564 void *private = RPC_I(dir)->private;
565 struct dentry *dentry;
566 int mode, i;
567
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800568 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 for (i = start; i < eof; i++) {
570 dentry = d_alloc_name(parent, files[i].name);
571 if (!dentry)
572 goto out_bad;
Trond Myklebust62e17612007-06-08 14:14:46 -0400573 dentry->d_op = &rpc_dentry_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mode = files[i].mode;
575 inode = rpc_get_inode(dir->i_sb, mode);
576 if (!inode) {
577 dput(dentry);
578 goto out_bad;
579 }
580 inode->i_ino = i;
581 if (files[i].i_fop)
582 inode->i_fop = files[i].i_fop;
583 if (private)
584 rpc_inode_setowner(inode, private);
585 if (S_ISDIR(mode))
Dave Hansend8c76e62006-09-30 23:29:04 -0700586 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 d_add(dentry, inode);
588 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800589 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591out_bad:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800592 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
594 __FILE__, __FUNCTION__, parent->d_name.name);
595 return -ENOMEM;
596}
597
Trond Myklebustf1345852005-09-23 11:08:25 -0400598static int
599__rpc_mkdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
601 struct inode *inode;
602
Trond Myklebust1d216322007-02-02 15:56:06 -0800603 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUGO | S_IXUGO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!inode)
Trond Myklebustf1345852005-09-23 11:08:25 -0400605 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 inode->i_ino = iunique(dir->i_sb, 100);
Christoph Hellwig278c9952005-07-24 23:53:01 +0100607 d_instantiate(dentry, inode);
Dave Hansend8c76e62006-09-30 23:29:04 -0700608 inc_nlink(dir);
Christoph Hellwig278c9952005-07-24 23:53:01 +0100609 inode_dir_notify(dir, DN_CREATE);
Trond Myklebustf1345852005-09-23 11:08:25 -0400610 return 0;
611out_err:
612 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
613 __FILE__, __FUNCTION__, dentry->d_name.name);
614 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Trond Myklebustf1345852005-09-23 11:08:25 -0400617static int
618__rpc_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Trond Myklebustf1345852005-09-23 11:08:25 -0400620 int error;
Trond Myklebust62e17612007-06-08 14:14:46 -0400621 error = simple_rmdir(dir, dentry);
622 if (!error)
623 d_delete(dentry);
624 return error;
Trond Myklebustf1345852005-09-23 11:08:25 -0400625}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100626
Trond Myklebustf1345852005-09-23 11:08:25 -0400627static struct dentry *
Trond Myklebust34f30892007-06-07 18:28:02 -0400628rpc_lookup_create(struct dentry *parent, const char *name, int len, int exclusive)
Trond Myklebustf1345852005-09-23 11:08:25 -0400629{
Trond Myklebust158998b2006-08-24 01:03:17 -0400630 struct inode *dir = parent->d_inode;
Trond Myklebustf1345852005-09-23 11:08:25 -0400631 struct dentry *dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400632
Arjan van de Venc6573c22006-07-03 00:25:16 -0700633 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust158998b2006-08-24 01:03:17 -0400634 dentry = lookup_one_len(name, parent, len);
Trond Myklebustf1345852005-09-23 11:08:25 -0400635 if (IS_ERR(dentry))
636 goto out_err;
Trond Myklebust62e17612007-06-08 14:14:46 -0400637 if (!dentry->d_inode)
638 dentry->d_op = &rpc_dentry_operations;
639 else if (exclusive) {
Trond Myklebustf1345852005-09-23 11:08:25 -0400640 dput(dentry);
641 dentry = ERR_PTR(-EEXIST);
642 goto out_err;
643 }
644 return dentry;
645out_err:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800646 mutex_unlock(&dir->i_mutex);
Trond Myklebust158998b2006-08-24 01:03:17 -0400647 return dentry;
648}
649
650static struct dentry *
651rpc_lookup_negative(char *path, struct nameidata *nd)
652{
653 struct dentry *dentry;
654 int error;
655
656 if ((error = rpc_lookup_parent(path, nd)) != 0)
657 return ERR_PTR(error);
Trond Myklebust34f30892007-06-07 18:28:02 -0400658 dentry = rpc_lookup_create(nd->dentry, nd->last.name, nd->last.len, 1);
Trond Myklebust158998b2006-08-24 01:03:17 -0400659 if (IS_ERR(dentry))
660 rpc_release_path(nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400661 return dentry;
662}
663
664
665struct dentry *
666rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
667{
668 struct nameidata nd;
669 struct dentry *dentry;
670 struct inode *dir;
671 int error;
672
673 dentry = rpc_lookup_negative(path, &nd);
674 if (IS_ERR(dentry))
675 return dentry;
676 dir = nd.dentry->d_inode;
677 if ((error = __rpc_mkdir(dir, dentry)) != 0)
678 goto err_dput;
679 RPC_I(dentry->d_inode)->private = rpc_client;
680 error = rpc_populate(dentry, authfiles,
681 RPCAUTH_info, RPCAUTH_EOF);
682 if (error)
683 goto err_depopulate;
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400684 dget(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400685out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800686 mutex_unlock(&dir->i_mutex);
Trond Myklebustf1345852005-09-23 11:08:25 -0400687 rpc_release_path(&nd);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400688 return dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400689err_depopulate:
Trond Myklebust62e17612007-06-08 14:14:46 -0400690 rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
Trond Myklebustf1345852005-09-23 11:08:25 -0400691 __rpc_rmdir(dir, dentry);
692err_dput:
693 dput(dentry);
694 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
695 __FILE__, __FUNCTION__, path, error);
696 dentry = ERR_PTR(error);
697 goto out;
698}
699
700int
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700701rpc_rmdir(struct dentry *dentry)
Trond Myklebustf1345852005-09-23 11:08:25 -0400702{
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700703 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400704 struct inode *dir;
705 int error;
706
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700707 parent = dget_parent(dentry);
708 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700709 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust62e17612007-06-08 14:14:46 -0400710 rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
Trond Myklebustf1345852005-09-23 11:08:25 -0400711 error = __rpc_rmdir(dir, dentry);
712 dput(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800713 mutex_unlock(&dir->i_mutex);
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700714 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400715 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718struct dentry *
Trond Myklebust158998b2006-08-24 01:03:17 -0400719rpc_mkpipe(struct dentry *parent, const char *name, void *private, struct rpc_pipe_ops *ops, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct dentry *dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400722 struct inode *dir, *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 struct rpc_inode *rpci;
724
Trond Myklebust34f30892007-06-07 18:28:02 -0400725 dentry = rpc_lookup_create(parent, name, strlen(name), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 if (IS_ERR(dentry))
Trond Myklebustf1345852005-09-23 11:08:25 -0400727 return dentry;
Trond Myklebust158998b2006-08-24 01:03:17 -0400728 dir = parent->d_inode;
Trond Myklebust34f30892007-06-07 18:28:02 -0400729 if (dentry->d_inode) {
730 rpci = RPC_I(dentry->d_inode);
731 if (rpci->private != private ||
732 rpci->ops != ops ||
733 rpci->flags != flags) {
734 dput (dentry);
735 dentry = ERR_PTR(-EBUSY);
736 }
Trond Myklebust03a12562007-06-08 14:14:53 -0400737 rpci->nkern_readwriters++;
Trond Myklebust34f30892007-06-07 18:28:02 -0400738 goto out;
739 }
Steve Dicksona53a3c52006-09-06 11:51:21 -0400740 inode = rpc_get_inode(dir->i_sb, S_IFIFO | S_IRUSR | S_IWUSR);
Trond Myklebustf1345852005-09-23 11:08:25 -0400741 if (!inode)
742 goto err_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 inode->i_ino = iunique(dir->i_sb, 100);
744 inode->i_fop = &rpc_pipe_fops;
Trond Myklebustf1345852005-09-23 11:08:25 -0400745 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 rpci = RPC_I(inode);
747 rpci->private = private;
748 rpci->flags = flags;
749 rpci->ops = ops;
Trond Myklebust03a12562007-06-08 14:14:53 -0400750 rpci->nkern_readwriters = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 inode_dir_notify(dir, DN_CREATE);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400752 dget(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400753out:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800754 mutex_unlock(&dir->i_mutex);
Trond Myklebust5c3e9852006-07-29 17:37:40 -0400755 return dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400756err_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 dput(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400758 dentry = ERR_PTR(-ENOMEM);
Trond Myklebust158998b2006-08-24 01:03:17 -0400759 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
760 __FILE__, __FUNCTION__, parent->d_name.name, name,
761 -ENOMEM);
Trond Myklebustf1345852005-09-23 11:08:25 -0400762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Trond Myklebustf1345852005-09-23 11:08:25 -0400765int
Trond Myklebust5d674762006-07-31 14:11:48 -0700766rpc_unlink(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Trond Myklebust5d674762006-07-31 14:11:48 -0700768 struct dentry *parent;
Trond Myklebustf1345852005-09-23 11:08:25 -0400769 struct inode *dir;
Trond Myklebust5d674762006-07-31 14:11:48 -0700770 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Trond Myklebust5d674762006-07-31 14:11:48 -0700772 parent = dget_parent(dentry);
773 dir = parent->d_inode;
Arjan van de Venc6573c22006-07-03 00:25:16 -0700774 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
Trond Myklebust03a12562007-06-08 14:14:53 -0400775 if (--RPC_I(dentry->d_inode)->nkern_readwriters == 0) {
776 rpc_close_pipes(dentry->d_inode);
777 error = simple_unlink(dir, dentry);
778 if (!error)
779 d_delete(dentry);
780 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400781 dput(dentry);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800782 mutex_unlock(&dir->i_mutex);
Trond Myklebust5d674762006-07-31 14:11:48 -0700783 dput(parent);
Trond Myklebustf1345852005-09-23 11:08:25 -0400784 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
787/*
788 * populate the filesystem
789 */
790static struct super_operations s_ops = {
791 .alloc_inode = rpc_alloc_inode,
792 .destroy_inode = rpc_destroy_inode,
793 .statfs = simple_statfs,
794};
795
796#define RPCAUTH_GSSMAGIC 0x67596969
797
798static int
799rpc_fill_super(struct super_block *sb, void *data, int silent)
800{
801 struct inode *inode;
802 struct dentry *root;
803
804 sb->s_blocksize = PAGE_CACHE_SIZE;
805 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
806 sb->s_magic = RPCAUTH_GSSMAGIC;
807 sb->s_op = &s_ops;
808 sb->s_time_gran = 1;
809
810 inode = rpc_get_inode(sb, S_IFDIR | 0755);
811 if (!inode)
812 return -ENOMEM;
813 root = d_alloc_root(inode);
814 if (!root) {
815 iput(inode);
816 return -ENOMEM;
817 }
818 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
819 goto out;
820 sb->s_root = root;
821 return 0;
822out:
823 d_genocide(root);
824 dput(root);
825 return -ENOMEM;
826}
827
David Howells454e2392006-06-23 02:02:57 -0700828static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829rpc_get_sb(struct file_system_type *fs_type,
David Howells454e2392006-06-23 02:02:57 -0700830 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
David Howells454e2392006-06-23 02:02:57 -0700832 return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835static struct file_system_type rpc_pipe_fs_type = {
836 .owner = THIS_MODULE,
837 .name = "rpc_pipefs",
838 .get_sb = rpc_get_sb,
839 .kill_sb = kill_litter_super,
840};
841
842static void
Christoph Lametere18b8902006-12-06 20:33:20 -0800843init_once(void * foo, struct kmem_cache * cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 struct rpc_inode *rpci = (struct rpc_inode *) foo;
846
Christoph Lametera35afb82007-05-16 22:10:57 -0700847 inode_init_once(&rpci->vfs_inode);
848 rpci->private = NULL;
849 rpci->nreaders = 0;
850 rpci->nwriters = 0;
851 INIT_LIST_HEAD(&rpci->in_upcall);
Trond Myklebust6e84c7b2007-06-07 15:31:36 -0400852 INIT_LIST_HEAD(&rpci->in_downcall);
Christoph Lametera35afb82007-05-16 22:10:57 -0700853 INIT_LIST_HEAD(&rpci->pipe);
854 rpci->pipelen = 0;
855 init_waitqueue_head(&rpci->waitq);
856 INIT_DELAYED_WORK(&rpci->queue_timeout,
857 rpc_timeout_upcall_queue);
858 rpci->ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859}
860
861int register_rpc_pipefs(void)
862{
Akinobu Mita5bd5f582007-05-09 02:34:51 -0700863 int err;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800866 sizeof(struct rpc_inode),
867 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
868 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900869 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!rpc_inode_cachep)
871 return -ENOMEM;
Akinobu Mita5bd5f582007-05-09 02:34:51 -0700872 err = register_filesystem(&rpc_pipe_fs_type);
873 if (err) {
874 kmem_cache_destroy(rpc_inode_cachep);
875 return err;
876 }
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 return 0;
879}
880
881void unregister_rpc_pipefs(void)
882{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700883 kmem_cache_destroy(rpc_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 unregister_filesystem(&rpc_pipe_fs_type);
885}