blob: e14c1cae74600d5035b571a50414a65f7429ac8a [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 */
11#include <linux/config.h>
12#include <linux/module.h>
13#include <linux/slab.h>
14#include <linux/string.h>
15#include <linux/pagemap.h>
16#include <linux/mount.h>
17#include <linux/namei.h>
18#include <linux/dnotify.h>
19#include <linux/kernel.h>
20
21#include <asm/ioctls.h>
22#include <linux/fs.h>
23#include <linux/poll.h>
24#include <linux/wait.h>
25#include <linux/seq_file.h>
26
27#include <linux/sunrpc/clnt.h>
28#include <linux/workqueue.h>
29#include <linux/sunrpc/rpc_pipe_fs.h>
30
Eric Dumazetba899662005-08-26 12:05:31 -070031static struct vfsmount *rpc_mount __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int rpc_mount_count;
33
34static struct file_system_type rpc_pipe_fs_type;
35
36
Eric Dumazetba899662005-08-26 12:05:31 -070037static kmem_cache_t *rpc_inode_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define RPC_UPCALL_TIMEOUT (30*HZ)
40
41static void
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050042__rpc_purge_list(struct rpc_inode *rpci, struct list_head *head, int err)
43{
44 struct rpc_pipe_msg *msg;
45 void (*destroy_msg)(struct rpc_pipe_msg *);
46
47 destroy_msg = rpci->ops->destroy_msg;
48 while (!list_empty(head)) {
49 msg = list_entry(head->next, struct rpc_pipe_msg, list);
50 list_del_init(&msg->list);
51 msg->errno = err;
52 destroy_msg(msg);
53 }
54}
55
56static void
Linus Torvalds1da177e2005-04-16 15:20:36 -070057__rpc_purge_upcall(struct inode *inode, int err)
58{
59 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Trond Myklebustb3eb67a2005-11-25 17:10:11 -050061 __rpc_purge_list(rpci, &rpci->pipe, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 rpci->pipelen = 0;
63 wake_up(&rpci->waitq);
64}
65
66static void
67rpc_timeout_upcall_queue(void *data)
68{
69 struct rpc_inode *rpci = (struct rpc_inode *)data;
70 struct inode *inode = &rpci->vfs_inode;
71
72 down(&inode->i_sem);
Trond Myklebust969b7f22006-01-03 09:55:36 +010073 if (rpci->ops == NULL)
74 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
76 __rpc_purge_upcall(inode, -ETIMEDOUT);
Trond Myklebust969b7f22006-01-03 09:55:36 +010077out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 up(&inode->i_sem);
79}
80
81int
82rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
83{
84 struct rpc_inode *rpci = RPC_I(inode);
Trond Myklebust6070fe62005-10-27 22:12:46 -040085 int res = -EPIPE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 down(&inode->i_sem);
Trond Myklebust6070fe62005-10-27 22:12:46 -040088 if (rpci->ops == NULL)
89 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (rpci->nreaders) {
91 list_add_tail(&msg->list, &rpci->pipe);
92 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -040093 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
95 if (list_empty(&rpci->pipe))
96 schedule_delayed_work(&rpci->queue_timeout,
97 RPC_UPCALL_TIMEOUT);
98 list_add_tail(&msg->list, &rpci->pipe);
99 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -0400100 res = 0;
101 }
102out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 up(&inode->i_sem);
104 wake_up(&rpci->waitq);
105 return res;
106}
107
Trond Myklebust6070fe62005-10-27 22:12:46 -0400108static inline void
109rpc_inode_setowner(struct inode *inode, void *private)
110{
111 RPC_I(inode)->private = private;
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static void
115rpc_close_pipes(struct inode *inode)
116{
117 struct rpc_inode *rpci = RPC_I(inode);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 down(&inode->i_sem);
120 if (rpci->ops != NULL) {
121 rpci->nreaders = 0;
Trond Myklebustbb184f32005-12-03 15:20:10 -0500122 __rpc_purge_list(rpci, &rpci->in_upcall, -EPIPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 __rpc_purge_upcall(inode, -EPIPE);
124 rpci->nwriters = 0;
125 if (rpci->ops->release_pipe)
126 rpci->ops->release_pipe(inode);
127 rpci->ops = NULL;
128 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400129 rpc_inode_setowner(inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 up(&inode->i_sem);
Trond Myklebust969b7f22006-01-03 09:55:36 +0100131 cancel_delayed_work(&rpci->queue_timeout);
132 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static struct inode *
136rpc_alloc_inode(struct super_block *sb)
137{
138 struct rpc_inode *rpci;
139 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
140 if (!rpci)
141 return NULL;
142 return &rpci->vfs_inode;
143}
144
145static void
146rpc_destroy_inode(struct inode *inode)
147{
148 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
149}
150
151static int
152rpc_pipe_open(struct inode *inode, struct file *filp)
153{
154 struct rpc_inode *rpci = RPC_I(inode);
155 int res = -ENXIO;
156
157 down(&inode->i_sem);
158 if (rpci->ops != NULL) {
159 if (filp->f_mode & FMODE_READ)
160 rpci->nreaders ++;
161 if (filp->f_mode & FMODE_WRITE)
162 rpci->nwriters ++;
163 res = 0;
164 }
165 up(&inode->i_sem);
166 return res;
167}
168
169static int
170rpc_pipe_release(struct inode *inode, struct file *filp)
171{
Trond Myklebust969b7f22006-01-03 09:55:36 +0100172 struct rpc_inode *rpci = RPC_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 struct rpc_pipe_msg *msg;
174
175 down(&inode->i_sem);
176 if (rpci->ops == NULL)
177 goto out;
178 msg = (struct rpc_pipe_msg *)filp->private_data;
179 if (msg != NULL) {
Trond Myklebust48e49182005-12-19 17:11:22 -0500180 msg->errno = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 list_del_init(&msg->list);
182 rpci->ops->destroy_msg(msg);
183 }
184 if (filp->f_mode & FMODE_WRITE)
185 rpci->nwriters --;
186 if (filp->f_mode & FMODE_READ)
187 rpci->nreaders --;
188 if (!rpci->nreaders)
Trond Myklebust48e49182005-12-19 17:11:22 -0500189 __rpc_purge_upcall(inode, -EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (rpci->ops->release_pipe)
191 rpci->ops->release_pipe(inode);
192out:
193 up(&inode->i_sem);
194 return 0;
195}
196
197static ssize_t
198rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
199{
200 struct inode *inode = filp->f_dentry->d_inode;
201 struct rpc_inode *rpci = RPC_I(inode);
202 struct rpc_pipe_msg *msg;
203 int res = 0;
204
205 down(&inode->i_sem);
206 if (rpci->ops == NULL) {
207 res = -EPIPE;
208 goto out_unlock;
209 }
210 msg = filp->private_data;
211 if (msg == NULL) {
212 if (!list_empty(&rpci->pipe)) {
213 msg = list_entry(rpci->pipe.next,
214 struct rpc_pipe_msg,
215 list);
216 list_move(&msg->list, &rpci->in_upcall);
217 rpci->pipelen -= msg->len;
218 filp->private_data = msg;
219 msg->copied = 0;
220 }
221 if (msg == NULL)
222 goto out_unlock;
223 }
224 /* NOTE: it is up to the callback to update msg->copied */
225 res = rpci->ops->upcall(filp, msg, buf, len);
226 if (res < 0 || msg->len == msg->copied) {
227 filp->private_data = NULL;
228 list_del_init(&msg->list);
229 rpci->ops->destroy_msg(msg);
230 }
231out_unlock:
232 up(&inode->i_sem);
233 return res;
234}
235
236static ssize_t
237rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
238{
239 struct inode *inode = filp->f_dentry->d_inode;
240 struct rpc_inode *rpci = RPC_I(inode);
241 int res;
242
243 down(&inode->i_sem);
244 res = -EPIPE;
245 if (rpci->ops != NULL)
246 res = rpci->ops->downcall(filp, buf, len);
247 up(&inode->i_sem);
248 return res;
249}
250
251static unsigned int
252rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
253{
254 struct rpc_inode *rpci;
255 unsigned int mask = 0;
256
257 rpci = RPC_I(filp->f_dentry->d_inode);
258 poll_wait(filp, &rpci->waitq, wait);
259
260 mask = POLLOUT | POLLWRNORM;
261 if (rpci->ops == NULL)
262 mask |= POLLERR | POLLHUP;
263 if (!list_empty(&rpci->pipe))
264 mask |= POLLIN | POLLRDNORM;
265 return mask;
266}
267
268static int
269rpc_pipe_ioctl(struct inode *ino, struct file *filp,
270 unsigned int cmd, unsigned long arg)
271{
272 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
273 int len;
274
275 switch (cmd) {
276 case FIONREAD:
277 if (rpci->ops == NULL)
278 return -EPIPE;
279 len = rpci->pipelen;
280 if (filp->private_data) {
281 struct rpc_pipe_msg *msg;
282 msg = (struct rpc_pipe_msg *)filp->private_data;
283 len += msg->len - msg->copied;
284 }
285 return put_user(len, (int __user *)arg);
286 default:
287 return -EINVAL;
288 }
289}
290
291static struct file_operations rpc_pipe_fops = {
292 .owner = THIS_MODULE,
293 .llseek = no_llseek,
294 .read = rpc_pipe_read,
295 .write = rpc_pipe_write,
296 .poll = rpc_pipe_poll,
297 .ioctl = rpc_pipe_ioctl,
298 .open = rpc_pipe_open,
299 .release = rpc_pipe_release,
300};
301
302static int
303rpc_show_info(struct seq_file *m, void *v)
304{
305 struct rpc_clnt *clnt = m->private;
306
307 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
308 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
309 clnt->cl_prog, clnt->cl_vers);
310 seq_printf(m, "address: %u.%u.%u.%u\n",
311 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
312 seq_printf(m, "protocol: %s\n",
313 clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
314 return 0;
315}
316
317static int
318rpc_info_open(struct inode *inode, struct file *file)
319{
320 struct rpc_clnt *clnt;
321 int ret = single_open(file, rpc_show_info, NULL);
322
323 if (!ret) {
324 struct seq_file *m = file->private_data;
325 down(&inode->i_sem);
326 clnt = RPC_I(inode)->private;
327 if (clnt) {
328 atomic_inc(&clnt->cl_users);
329 m->private = clnt;
330 } else {
331 single_release(inode, file);
332 ret = -EINVAL;
333 }
334 up(&inode->i_sem);
335 }
336 return ret;
337}
338
339static int
340rpc_info_release(struct inode *inode, struct file *file)
341{
342 struct seq_file *m = file->private_data;
343 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
344
345 if (clnt)
346 rpc_release_client(clnt);
347 return single_release(inode, file);
348}
349
350static struct file_operations rpc_info_operations = {
351 .owner = THIS_MODULE,
352 .open = rpc_info_open,
353 .read = seq_read,
354 .llseek = seq_lseek,
355 .release = rpc_info_release,
356};
357
358
359/*
360 * We have a single directory with 1 node in it.
361 */
362enum {
363 RPCAUTH_Root = 1,
364 RPCAUTH_lockd,
365 RPCAUTH_mount,
366 RPCAUTH_nfs,
367 RPCAUTH_portmap,
368 RPCAUTH_statd,
369 RPCAUTH_RootEOF
370};
371
372/*
373 * Description of fs contents.
374 */
375struct rpc_filelist {
376 char *name;
377 struct file_operations *i_fop;
378 int mode;
379};
380
381static struct rpc_filelist files[] = {
382 [RPCAUTH_lockd] = {
383 .name = "lockd",
384 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
385 },
386 [RPCAUTH_mount] = {
387 .name = "mount",
388 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
389 },
390 [RPCAUTH_nfs] = {
391 .name = "nfs",
392 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
393 },
394 [RPCAUTH_portmap] = {
395 .name = "portmap",
396 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
397 },
398 [RPCAUTH_statd] = {
399 .name = "statd",
400 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
401 },
402};
403
404enum {
405 RPCAUTH_info = 2,
406 RPCAUTH_EOF
407};
408
409static struct rpc_filelist authfiles[] = {
410 [RPCAUTH_info] = {
411 .name = "info",
412 .i_fop = &rpc_info_operations,
413 .mode = S_IFREG | S_IRUSR,
414 },
415};
416
417static int
418rpc_get_mount(void)
419{
420 return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
421}
422
423static void
424rpc_put_mount(void)
425{
426 simple_release_fs(&rpc_mount, &rpc_mount_count);
427}
428
Trond Myklebustf1345852005-09-23 11:08:25 -0400429static int
430rpc_lookup_parent(char *path, struct nameidata *nd)
431{
432 if (path[0] == '\0')
433 return -ENOENT;
434 if (rpc_get_mount()) {
435 printk(KERN_WARNING "%s: %s failed to mount "
436 "pseudofilesystem \n", __FILE__, __FUNCTION__);
437 return -ENODEV;
438 }
439 nd->mnt = mntget(rpc_mount);
440 nd->dentry = dget(rpc_mount->mnt_root);
441 nd->last_type = LAST_ROOT;
442 nd->flags = LOOKUP_PARENT;
443 nd->depth = 0;
444
445 if (path_walk(path, nd)) {
446 printk(KERN_WARNING "%s: %s failed to find path %s\n",
447 __FILE__, __FUNCTION__, path);
448 rpc_put_mount();
449 return -ENOENT;
450 }
451 return 0;
452}
453
454static void
455rpc_release_path(struct nameidata *nd)
456{
457 path_release(nd);
458 rpc_put_mount();
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct inode *
462rpc_get_inode(struct super_block *sb, int mode)
463{
464 struct inode *inode = new_inode(sb);
465 if (!inode)
466 return NULL;
467 inode->i_mode = mode;
468 inode->i_uid = inode->i_gid = 0;
469 inode->i_blksize = PAGE_CACHE_SIZE;
470 inode->i_blocks = 0;
471 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
472 switch(mode & S_IFMT) {
473 case S_IFDIR:
474 inode->i_fop = &simple_dir_operations;
475 inode->i_op = &simple_dir_inode_operations;
476 inode->i_nlink++;
477 default:
478 break;
479 }
480 return inode;
481}
482
483/*
484 * FIXME: This probably has races.
485 */
486static void
487rpc_depopulate(struct dentry *parent)
488{
489 struct inode *dir = parent->d_inode;
490 struct list_head *pos, *next;
491 struct dentry *dentry, *dvec[10];
492 int n = 0;
493
494 down(&dir->i_sem);
495repeat:
496 spin_lock(&dcache_lock);
497 list_for_each_safe(pos, next, &parent->d_subdirs) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800498 dentry = list_entry(pos, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 spin_lock(&dentry->d_lock);
500 if (!d_unhashed(dentry)) {
501 dget_locked(dentry);
502 __d_drop(dentry);
503 spin_unlock(&dentry->d_lock);
504 dvec[n++] = dentry;
505 if (n == ARRAY_SIZE(dvec))
506 break;
507 } else
508 spin_unlock(&dentry->d_lock);
509 }
510 spin_unlock(&dcache_lock);
511 if (n) {
512 do {
513 dentry = dvec[--n];
514 if (dentry->d_inode) {
515 rpc_close_pipes(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 simple_unlink(dir, dentry);
517 }
518 dput(dentry);
519 } while (n);
520 goto repeat;
521 }
522 up(&dir->i_sem);
523}
524
525static int
526rpc_populate(struct dentry *parent,
527 struct rpc_filelist *files,
528 int start, int eof)
529{
530 struct inode *inode, *dir = parent->d_inode;
531 void *private = RPC_I(dir)->private;
532 struct dentry *dentry;
533 int mode, i;
534
535 down(&dir->i_sem);
536 for (i = start; i < eof; i++) {
537 dentry = d_alloc_name(parent, files[i].name);
538 if (!dentry)
539 goto out_bad;
540 mode = files[i].mode;
541 inode = rpc_get_inode(dir->i_sb, mode);
542 if (!inode) {
543 dput(dentry);
544 goto out_bad;
545 }
546 inode->i_ino = i;
547 if (files[i].i_fop)
548 inode->i_fop = files[i].i_fop;
549 if (private)
550 rpc_inode_setowner(inode, private);
551 if (S_ISDIR(mode))
552 dir->i_nlink++;
553 d_add(dentry, inode);
554 }
555 up(&dir->i_sem);
556 return 0;
557out_bad:
558 up(&dir->i_sem);
559 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
560 __FILE__, __FUNCTION__, parent->d_name.name);
561 return -ENOMEM;
562}
563
Trond Myklebustf1345852005-09-23 11:08:25 -0400564static int
565__rpc_mkdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct inode *inode;
568
569 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
570 if (!inode)
Trond Myklebustf1345852005-09-23 11:08:25 -0400571 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 inode->i_ino = iunique(dir->i_sb, 100);
Christoph Hellwig278c9952005-07-24 23:53:01 +0100573 d_instantiate(dentry, inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400574 dir->i_nlink++;
Christoph Hellwig278c9952005-07-24 23:53:01 +0100575 inode_dir_notify(dir, DN_CREATE);
Trond Myklebustf1345852005-09-23 11:08:25 -0400576 rpc_get_mount();
577 return 0;
578out_err:
579 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
580 __FILE__, __FUNCTION__, dentry->d_name.name);
581 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
Trond Myklebustf1345852005-09-23 11:08:25 -0400584static int
585__rpc_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Trond Myklebustf1345852005-09-23 11:08:25 -0400587 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Trond Myklebustf1345852005-09-23 11:08:25 -0400589 shrink_dcache_parent(dentry);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400590 if (dentry->d_inode)
Christoph Hellwig278c9952005-07-24 23:53:01 +0100591 rpc_close_pipes(dentry->d_inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400592 if ((error = simple_rmdir(dir, dentry)) != 0)
593 return error;
594 if (!error) {
595 inode_dir_notify(dir, DN_DELETE);
596 d_drop(dentry);
597 rpc_put_mount();
598 }
599 return 0;
600}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100601
Trond Myklebustf1345852005-09-23 11:08:25 -0400602static struct dentry *
603rpc_lookup_negative(char *path, struct nameidata *nd)
604{
605 struct dentry *dentry;
606 struct inode *dir;
607 int error;
608
609 if ((error = rpc_lookup_parent(path, nd)) != 0)
610 return ERR_PTR(error);
611 dir = nd->dentry->d_inode;
612 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800613 dentry = lookup_hash(nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400614 if (IS_ERR(dentry))
615 goto out_err;
616 if (dentry->d_inode) {
617 dput(dentry);
618 dentry = ERR_PTR(-EEXIST);
619 goto out_err;
620 }
621 return dentry;
622out_err:
623 up(&dir->i_sem);
624 rpc_release_path(nd);
625 return dentry;
626}
627
628
629struct dentry *
630rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
631{
632 struct nameidata nd;
633 struct dentry *dentry;
634 struct inode *dir;
635 int error;
636
637 dentry = rpc_lookup_negative(path, &nd);
638 if (IS_ERR(dentry))
639 return dentry;
640 dir = nd.dentry->d_inode;
641 if ((error = __rpc_mkdir(dir, dentry)) != 0)
642 goto err_dput;
643 RPC_I(dentry->d_inode)->private = rpc_client;
644 error = rpc_populate(dentry, authfiles,
645 RPCAUTH_info, RPCAUTH_EOF);
646 if (error)
647 goto err_depopulate;
648out:
649 up(&dir->i_sem);
650 rpc_release_path(&nd);
651 return dentry;
652err_depopulate:
653 rpc_depopulate(dentry);
654 __rpc_rmdir(dir, dentry);
655err_dput:
656 dput(dentry);
657 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
658 __FILE__, __FUNCTION__, path, error);
659 dentry = ERR_PTR(error);
660 goto out;
661}
662
663int
664rpc_rmdir(char *path)
665{
666 struct nameidata nd;
667 struct dentry *dentry;
668 struct inode *dir;
669 int error;
670
671 if ((error = rpc_lookup_parent(path, &nd)) != 0)
672 return error;
673 dir = nd.dentry->d_inode;
674 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800675 dentry = lookup_hash(&nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400676 if (IS_ERR(dentry)) {
677 error = PTR_ERR(dentry);
678 goto out_release;
679 }
680 rpc_depopulate(dentry);
681 error = __rpc_rmdir(dir, dentry);
682 dput(dentry);
683out_release:
684 up(&dir->i_sem);
685 rpc_release_path(&nd);
686 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689struct dentry *
Trond Myklebustf1345852005-09-23 11:08:25 -0400690rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Trond Myklebustf1345852005-09-23 11:08:25 -0400692 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct dentry *dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400694 struct inode *dir, *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct rpc_inode *rpci;
696
Trond Myklebustf1345852005-09-23 11:08:25 -0400697 dentry = rpc_lookup_negative(path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (IS_ERR(dentry))
Trond Myklebustf1345852005-09-23 11:08:25 -0400699 return dentry;
700 dir = nd.dentry->d_inode;
701 inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
702 if (!inode)
703 goto err_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 inode->i_ino = iunique(dir->i_sb, 100);
705 inode->i_fop = &rpc_pipe_fops;
Trond Myklebustf1345852005-09-23 11:08:25 -0400706 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 rpci = RPC_I(inode);
708 rpci->private = private;
709 rpci->flags = flags;
710 rpci->ops = ops;
711 inode_dir_notify(dir, DN_CREATE);
Trond Myklebustf1345852005-09-23 11:08:25 -0400712out:
713 up(&dir->i_sem);
714 rpc_release_path(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400716err_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 dput(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400718 dentry = ERR_PTR(-ENOMEM);
719 printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
720 __FILE__, __FUNCTION__, path, -ENOMEM);
721 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Trond Myklebustf1345852005-09-23 11:08:25 -0400724int
725rpc_unlink(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Trond Myklebustf1345852005-09-23 11:08:25 -0400727 struct nameidata nd;
728 struct dentry *dentry;
729 struct inode *dir;
730 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Trond Myklebustf1345852005-09-23 11:08:25 -0400732 if ((error = rpc_lookup_parent(path, &nd)) != 0)
733 return error;
734 dir = nd.dentry->d_inode;
735 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800736 dentry = lookup_hash(&nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400737 if (IS_ERR(dentry)) {
738 error = PTR_ERR(dentry);
739 goto out_release;
740 }
741 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (dentry->d_inode) {
743 rpc_close_pipes(dentry->d_inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400744 error = simple_unlink(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400746 dput(dentry);
747 inode_dir_notify(dir, DN_DELETE);
748out_release:
749 up(&dir->i_sem);
750 rpc_release_path(&nd);
751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/*
755 * populate the filesystem
756 */
757static struct super_operations s_ops = {
758 .alloc_inode = rpc_alloc_inode,
759 .destroy_inode = rpc_destroy_inode,
760 .statfs = simple_statfs,
761};
762
763#define RPCAUTH_GSSMAGIC 0x67596969
764
765static int
766rpc_fill_super(struct super_block *sb, void *data, int silent)
767{
768 struct inode *inode;
769 struct dentry *root;
770
771 sb->s_blocksize = PAGE_CACHE_SIZE;
772 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
773 sb->s_magic = RPCAUTH_GSSMAGIC;
774 sb->s_op = &s_ops;
775 sb->s_time_gran = 1;
776
777 inode = rpc_get_inode(sb, S_IFDIR | 0755);
778 if (!inode)
779 return -ENOMEM;
780 root = d_alloc_root(inode);
781 if (!root) {
782 iput(inode);
783 return -ENOMEM;
784 }
785 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
786 goto out;
787 sb->s_root = root;
788 return 0;
789out:
790 d_genocide(root);
791 dput(root);
792 return -ENOMEM;
793}
794
795static struct super_block *
796rpc_get_sb(struct file_system_type *fs_type,
797 int flags, const char *dev_name, void *data)
798{
799 return get_sb_single(fs_type, flags, data, rpc_fill_super);
800}
801
802static struct file_system_type rpc_pipe_fs_type = {
803 .owner = THIS_MODULE,
804 .name = "rpc_pipefs",
805 .get_sb = rpc_get_sb,
806 .kill_sb = kill_litter_super,
807};
808
809static void
810init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
811{
812 struct rpc_inode *rpci = (struct rpc_inode *) foo;
813
814 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
815 SLAB_CTOR_CONSTRUCTOR) {
816 inode_init_once(&rpci->vfs_inode);
817 rpci->private = NULL;
818 rpci->nreaders = 0;
819 rpci->nwriters = 0;
820 INIT_LIST_HEAD(&rpci->in_upcall);
821 INIT_LIST_HEAD(&rpci->pipe);
822 rpci->pipelen = 0;
823 init_waitqueue_head(&rpci->waitq);
824 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
825 rpci->ops = NULL;
826 }
827}
828
829int register_rpc_pipefs(void)
830{
831 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
832 sizeof(struct rpc_inode),
833 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
834 init_once, NULL);
835 if (!rpc_inode_cachep)
836 return -ENOMEM;
837 register_filesystem(&rpc_pipe_fs_type);
838 return 0;
839}
840
841void unregister_rpc_pipefs(void)
842{
843 if (kmem_cache_destroy(rpc_inode_cachep))
844 printk(KERN_WARNING "RPC: unable to free inode cache\n");
845 unregister_filesystem(&rpc_pipe_fs_type);
846}