blob: e3b242daf53c64506f9ba77937a94bb544bcefe6 [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);
62 __rpc_purge_list(rpci, &rpci->in_upcall, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 rpci->pipelen = 0;
64 wake_up(&rpci->waitq);
65}
66
67static void
68rpc_timeout_upcall_queue(void *data)
69{
70 struct rpc_inode *rpci = (struct rpc_inode *)data;
71 struct inode *inode = &rpci->vfs_inode;
72
73 down(&inode->i_sem);
74 if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
75 __rpc_purge_upcall(inode, -ETIMEDOUT);
76 up(&inode->i_sem);
77}
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
85 down(&inode->i_sem);
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))
94 schedule_delayed_work(&rpci->queue_timeout,
95 RPC_UPCALL_TIMEOUT);
96 list_add_tail(&msg->list, &rpci->pipe);
97 rpci->pipelen += msg->len;
Trond Myklebust6070fe62005-10-27 22:12:46 -040098 res = 0;
99 }
100out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 up(&inode->i_sem);
102 wake_up(&rpci->waitq);
103 return res;
104}
105
Trond Myklebust6070fe62005-10-27 22:12:46 -0400106static inline void
107rpc_inode_setowner(struct inode *inode, void *private)
108{
109 RPC_I(inode)->private = private;
110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112static void
113rpc_close_pipes(struct inode *inode)
114{
115 struct rpc_inode *rpci = RPC_I(inode);
116
117 cancel_delayed_work(&rpci->queue_timeout);
118 flush_scheduled_work();
119 down(&inode->i_sem);
120 if (rpci->ops != NULL) {
121 rpci->nreaders = 0;
122 __rpc_purge_upcall(inode, -EPIPE);
123 rpci->nwriters = 0;
124 if (rpci->ops->release_pipe)
125 rpci->ops->release_pipe(inode);
126 rpci->ops = NULL;
127 }
Trond Myklebust6070fe62005-10-27 22:12:46 -0400128 rpc_inode_setowner(inode, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 up(&inode->i_sem);
130}
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct inode *
133rpc_alloc_inode(struct super_block *sb)
134{
135 struct rpc_inode *rpci;
136 rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
137 if (!rpci)
138 return NULL;
139 return &rpci->vfs_inode;
140}
141
142static void
143rpc_destroy_inode(struct inode *inode)
144{
145 kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
146}
147
148static int
149rpc_pipe_open(struct inode *inode, struct file *filp)
150{
151 struct rpc_inode *rpci = RPC_I(inode);
152 int res = -ENXIO;
153
154 down(&inode->i_sem);
155 if (rpci->ops != NULL) {
156 if (filp->f_mode & FMODE_READ)
157 rpci->nreaders ++;
158 if (filp->f_mode & FMODE_WRITE)
159 rpci->nwriters ++;
160 res = 0;
161 }
162 up(&inode->i_sem);
163 return res;
164}
165
166static int
167rpc_pipe_release(struct inode *inode, struct file *filp)
168{
169 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
170 struct rpc_pipe_msg *msg;
171
172 down(&inode->i_sem);
173 if (rpci->ops == NULL)
174 goto out;
175 msg = (struct rpc_pipe_msg *)filp->private_data;
176 if (msg != NULL) {
177 msg->errno = -EPIPE;
178 list_del_init(&msg->list);
179 rpci->ops->destroy_msg(msg);
180 }
181 if (filp->f_mode & FMODE_WRITE)
182 rpci->nwriters --;
183 if (filp->f_mode & FMODE_READ)
184 rpci->nreaders --;
185 if (!rpci->nreaders)
186 __rpc_purge_upcall(inode, -EPIPE);
187 if (rpci->ops->release_pipe)
188 rpci->ops->release_pipe(inode);
189out:
190 up(&inode->i_sem);
191 return 0;
192}
193
194static ssize_t
195rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
196{
197 struct inode *inode = filp->f_dentry->d_inode;
198 struct rpc_inode *rpci = RPC_I(inode);
199 struct rpc_pipe_msg *msg;
200 int res = 0;
201
202 down(&inode->i_sem);
203 if (rpci->ops == NULL) {
204 res = -EPIPE;
205 goto out_unlock;
206 }
207 msg = filp->private_data;
208 if (msg == NULL) {
209 if (!list_empty(&rpci->pipe)) {
210 msg = list_entry(rpci->pipe.next,
211 struct rpc_pipe_msg,
212 list);
213 list_move(&msg->list, &rpci->in_upcall);
214 rpci->pipelen -= msg->len;
215 filp->private_data = msg;
216 msg->copied = 0;
217 }
218 if (msg == NULL)
219 goto out_unlock;
220 }
221 /* NOTE: it is up to the callback to update msg->copied */
222 res = rpci->ops->upcall(filp, msg, buf, len);
223 if (res < 0 || msg->len == msg->copied) {
224 filp->private_data = NULL;
225 list_del_init(&msg->list);
226 rpci->ops->destroy_msg(msg);
227 }
228out_unlock:
229 up(&inode->i_sem);
230 return res;
231}
232
233static ssize_t
234rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
235{
236 struct inode *inode = filp->f_dentry->d_inode;
237 struct rpc_inode *rpci = RPC_I(inode);
238 int res;
239
240 down(&inode->i_sem);
241 res = -EPIPE;
242 if (rpci->ops != NULL)
243 res = rpci->ops->downcall(filp, buf, len);
244 up(&inode->i_sem);
245 return res;
246}
247
248static unsigned int
249rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
250{
251 struct rpc_inode *rpci;
252 unsigned int mask = 0;
253
254 rpci = RPC_I(filp->f_dentry->d_inode);
255 poll_wait(filp, &rpci->waitq, wait);
256
257 mask = POLLOUT | POLLWRNORM;
258 if (rpci->ops == NULL)
259 mask |= POLLERR | POLLHUP;
260 if (!list_empty(&rpci->pipe))
261 mask |= POLLIN | POLLRDNORM;
262 return mask;
263}
264
265static int
266rpc_pipe_ioctl(struct inode *ino, struct file *filp,
267 unsigned int cmd, unsigned long arg)
268{
269 struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
270 int len;
271
272 switch (cmd) {
273 case FIONREAD:
274 if (rpci->ops == NULL)
275 return -EPIPE;
276 len = rpci->pipelen;
277 if (filp->private_data) {
278 struct rpc_pipe_msg *msg;
279 msg = (struct rpc_pipe_msg *)filp->private_data;
280 len += msg->len - msg->copied;
281 }
282 return put_user(len, (int __user *)arg);
283 default:
284 return -EINVAL;
285 }
286}
287
288static struct file_operations rpc_pipe_fops = {
289 .owner = THIS_MODULE,
290 .llseek = no_llseek,
291 .read = rpc_pipe_read,
292 .write = rpc_pipe_write,
293 .poll = rpc_pipe_poll,
294 .ioctl = rpc_pipe_ioctl,
295 .open = rpc_pipe_open,
296 .release = rpc_pipe_release,
297};
298
299static int
300rpc_show_info(struct seq_file *m, void *v)
301{
302 struct rpc_clnt *clnt = m->private;
303
304 seq_printf(m, "RPC server: %s\n", clnt->cl_server);
305 seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
306 clnt->cl_prog, clnt->cl_vers);
307 seq_printf(m, "address: %u.%u.%u.%u\n",
308 NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
309 seq_printf(m, "protocol: %s\n",
310 clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
311 return 0;
312}
313
314static int
315rpc_info_open(struct inode *inode, struct file *file)
316{
317 struct rpc_clnt *clnt;
318 int ret = single_open(file, rpc_show_info, NULL);
319
320 if (!ret) {
321 struct seq_file *m = file->private_data;
322 down(&inode->i_sem);
323 clnt = RPC_I(inode)->private;
324 if (clnt) {
325 atomic_inc(&clnt->cl_users);
326 m->private = clnt;
327 } else {
328 single_release(inode, file);
329 ret = -EINVAL;
330 }
331 up(&inode->i_sem);
332 }
333 return ret;
334}
335
336static int
337rpc_info_release(struct inode *inode, struct file *file)
338{
339 struct seq_file *m = file->private_data;
340 struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
341
342 if (clnt)
343 rpc_release_client(clnt);
344 return single_release(inode, file);
345}
346
347static struct file_operations rpc_info_operations = {
348 .owner = THIS_MODULE,
349 .open = rpc_info_open,
350 .read = seq_read,
351 .llseek = seq_lseek,
352 .release = rpc_info_release,
353};
354
355
356/*
357 * We have a single directory with 1 node in it.
358 */
359enum {
360 RPCAUTH_Root = 1,
361 RPCAUTH_lockd,
362 RPCAUTH_mount,
363 RPCAUTH_nfs,
364 RPCAUTH_portmap,
365 RPCAUTH_statd,
366 RPCAUTH_RootEOF
367};
368
369/*
370 * Description of fs contents.
371 */
372struct rpc_filelist {
373 char *name;
374 struct file_operations *i_fop;
375 int mode;
376};
377
378static struct rpc_filelist files[] = {
379 [RPCAUTH_lockd] = {
380 .name = "lockd",
381 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
382 },
383 [RPCAUTH_mount] = {
384 .name = "mount",
385 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
386 },
387 [RPCAUTH_nfs] = {
388 .name = "nfs",
389 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
390 },
391 [RPCAUTH_portmap] = {
392 .name = "portmap",
393 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
394 },
395 [RPCAUTH_statd] = {
396 .name = "statd",
397 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
398 },
399};
400
401enum {
402 RPCAUTH_info = 2,
403 RPCAUTH_EOF
404};
405
406static struct rpc_filelist authfiles[] = {
407 [RPCAUTH_info] = {
408 .name = "info",
409 .i_fop = &rpc_info_operations,
410 .mode = S_IFREG | S_IRUSR,
411 },
412};
413
414static int
415rpc_get_mount(void)
416{
417 return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
418}
419
420static void
421rpc_put_mount(void)
422{
423 simple_release_fs(&rpc_mount, &rpc_mount_count);
424}
425
Trond Myklebustf1345852005-09-23 11:08:25 -0400426static int
427rpc_lookup_parent(char *path, struct nameidata *nd)
428{
429 if (path[0] == '\0')
430 return -ENOENT;
431 if (rpc_get_mount()) {
432 printk(KERN_WARNING "%s: %s failed to mount "
433 "pseudofilesystem \n", __FILE__, __FUNCTION__);
434 return -ENODEV;
435 }
436 nd->mnt = mntget(rpc_mount);
437 nd->dentry = dget(rpc_mount->mnt_root);
438 nd->last_type = LAST_ROOT;
439 nd->flags = LOOKUP_PARENT;
440 nd->depth = 0;
441
442 if (path_walk(path, nd)) {
443 printk(KERN_WARNING "%s: %s failed to find path %s\n",
444 __FILE__, __FUNCTION__, path);
445 rpc_put_mount();
446 return -ENOENT;
447 }
448 return 0;
449}
450
451static void
452rpc_release_path(struct nameidata *nd)
453{
454 path_release(nd);
455 rpc_put_mount();
456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static struct inode *
459rpc_get_inode(struct super_block *sb, int mode)
460{
461 struct inode *inode = new_inode(sb);
462 if (!inode)
463 return NULL;
464 inode->i_mode = mode;
465 inode->i_uid = inode->i_gid = 0;
466 inode->i_blksize = PAGE_CACHE_SIZE;
467 inode->i_blocks = 0;
468 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
469 switch(mode & S_IFMT) {
470 case S_IFDIR:
471 inode->i_fop = &simple_dir_operations;
472 inode->i_op = &simple_dir_inode_operations;
473 inode->i_nlink++;
474 default:
475 break;
476 }
477 return inode;
478}
479
480/*
481 * FIXME: This probably has races.
482 */
483static void
484rpc_depopulate(struct dentry *parent)
485{
486 struct inode *dir = parent->d_inode;
487 struct list_head *pos, *next;
488 struct dentry *dentry, *dvec[10];
489 int n = 0;
490
491 down(&dir->i_sem);
492repeat:
493 spin_lock(&dcache_lock);
494 list_for_each_safe(pos, next, &parent->d_subdirs) {
495 dentry = list_entry(pos, struct dentry, d_child);
496 spin_lock(&dentry->d_lock);
497 if (!d_unhashed(dentry)) {
498 dget_locked(dentry);
499 __d_drop(dentry);
500 spin_unlock(&dentry->d_lock);
501 dvec[n++] = dentry;
502 if (n == ARRAY_SIZE(dvec))
503 break;
504 } else
505 spin_unlock(&dentry->d_lock);
506 }
507 spin_unlock(&dcache_lock);
508 if (n) {
509 do {
510 dentry = dvec[--n];
511 if (dentry->d_inode) {
512 rpc_close_pipes(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 simple_unlink(dir, dentry);
514 }
515 dput(dentry);
516 } while (n);
517 goto repeat;
518 }
519 up(&dir->i_sem);
520}
521
522static int
523rpc_populate(struct dentry *parent,
524 struct rpc_filelist *files,
525 int start, int eof)
526{
527 struct inode *inode, *dir = parent->d_inode;
528 void *private = RPC_I(dir)->private;
529 struct dentry *dentry;
530 int mode, i;
531
532 down(&dir->i_sem);
533 for (i = start; i < eof; i++) {
534 dentry = d_alloc_name(parent, files[i].name);
535 if (!dentry)
536 goto out_bad;
537 mode = files[i].mode;
538 inode = rpc_get_inode(dir->i_sb, mode);
539 if (!inode) {
540 dput(dentry);
541 goto out_bad;
542 }
543 inode->i_ino = i;
544 if (files[i].i_fop)
545 inode->i_fop = files[i].i_fop;
546 if (private)
547 rpc_inode_setowner(inode, private);
548 if (S_ISDIR(mode))
549 dir->i_nlink++;
550 d_add(dentry, inode);
551 }
552 up(&dir->i_sem);
553 return 0;
554out_bad:
555 up(&dir->i_sem);
556 printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
557 __FILE__, __FUNCTION__, parent->d_name.name);
558 return -ENOMEM;
559}
560
Trond Myklebustf1345852005-09-23 11:08:25 -0400561static int
562__rpc_mkdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 struct inode *inode;
565
566 inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
567 if (!inode)
Trond Myklebustf1345852005-09-23 11:08:25 -0400568 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 inode->i_ino = iunique(dir->i_sb, 100);
Christoph Hellwig278c9952005-07-24 23:53:01 +0100570 d_instantiate(dentry, inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400571 dir->i_nlink++;
Christoph Hellwig278c9952005-07-24 23:53:01 +0100572 inode_dir_notify(dir, DN_CREATE);
Trond Myklebustf1345852005-09-23 11:08:25 -0400573 rpc_get_mount();
574 return 0;
575out_err:
576 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
577 __FILE__, __FUNCTION__, dentry->d_name.name);
578 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Trond Myklebustf1345852005-09-23 11:08:25 -0400581static int
582__rpc_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Trond Myklebustf1345852005-09-23 11:08:25 -0400584 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Trond Myklebustf1345852005-09-23 11:08:25 -0400586 shrink_dcache_parent(dentry);
Trond Myklebust6070fe62005-10-27 22:12:46 -0400587 if (dentry->d_inode)
Christoph Hellwig278c9952005-07-24 23:53:01 +0100588 rpc_close_pipes(dentry->d_inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400589 if ((error = simple_rmdir(dir, dentry)) != 0)
590 return error;
591 if (!error) {
592 inode_dir_notify(dir, DN_DELETE);
593 d_drop(dentry);
594 rpc_put_mount();
595 }
596 return 0;
597}
Christoph Hellwig278c9952005-07-24 23:53:01 +0100598
Trond Myklebustf1345852005-09-23 11:08:25 -0400599static struct dentry *
600rpc_lookup_negative(char *path, struct nameidata *nd)
601{
602 struct dentry *dentry;
603 struct inode *dir;
604 int error;
605
606 if ((error = rpc_lookup_parent(path, nd)) != 0)
607 return ERR_PTR(error);
608 dir = nd->dentry->d_inode;
609 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800610 dentry = lookup_hash(nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400611 if (IS_ERR(dentry))
612 goto out_err;
613 if (dentry->d_inode) {
614 dput(dentry);
615 dentry = ERR_PTR(-EEXIST);
616 goto out_err;
617 }
618 return dentry;
619out_err:
620 up(&dir->i_sem);
621 rpc_release_path(nd);
622 return dentry;
623}
624
625
626struct dentry *
627rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
628{
629 struct nameidata nd;
630 struct dentry *dentry;
631 struct inode *dir;
632 int error;
633
634 dentry = rpc_lookup_negative(path, &nd);
635 if (IS_ERR(dentry))
636 return dentry;
637 dir = nd.dentry->d_inode;
638 if ((error = __rpc_mkdir(dir, dentry)) != 0)
639 goto err_dput;
640 RPC_I(dentry->d_inode)->private = rpc_client;
641 error = rpc_populate(dentry, authfiles,
642 RPCAUTH_info, RPCAUTH_EOF);
643 if (error)
644 goto err_depopulate;
645out:
646 up(&dir->i_sem);
647 rpc_release_path(&nd);
648 return dentry;
649err_depopulate:
650 rpc_depopulate(dentry);
651 __rpc_rmdir(dir, dentry);
652err_dput:
653 dput(dentry);
654 printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
655 __FILE__, __FUNCTION__, path, error);
656 dentry = ERR_PTR(error);
657 goto out;
658}
659
660int
661rpc_rmdir(char *path)
662{
663 struct nameidata nd;
664 struct dentry *dentry;
665 struct inode *dir;
666 int error;
667
668 if ((error = rpc_lookup_parent(path, &nd)) != 0)
669 return error;
670 dir = nd.dentry->d_inode;
671 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800672 dentry = lookup_hash(&nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400673 if (IS_ERR(dentry)) {
674 error = PTR_ERR(dentry);
675 goto out_release;
676 }
677 rpc_depopulate(dentry);
678 error = __rpc_rmdir(dir, dentry);
679 dput(dentry);
680out_release:
681 up(&dir->i_sem);
682 rpc_release_path(&nd);
683 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686struct dentry *
Trond Myklebustf1345852005-09-23 11:08:25 -0400687rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Trond Myklebustf1345852005-09-23 11:08:25 -0400689 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct dentry *dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400691 struct inode *dir, *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct rpc_inode *rpci;
693
Trond Myklebustf1345852005-09-23 11:08:25 -0400694 dentry = rpc_lookup_negative(path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (IS_ERR(dentry))
Trond Myklebustf1345852005-09-23 11:08:25 -0400696 return dentry;
697 dir = nd.dentry->d_inode;
698 inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
699 if (!inode)
700 goto err_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 inode->i_ino = iunique(dir->i_sb, 100);
702 inode->i_fop = &rpc_pipe_fops;
Trond Myklebustf1345852005-09-23 11:08:25 -0400703 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 rpci = RPC_I(inode);
705 rpci->private = private;
706 rpci->flags = flags;
707 rpci->ops = ops;
708 inode_dir_notify(dir, DN_CREATE);
Trond Myklebustf1345852005-09-23 11:08:25 -0400709out:
710 up(&dir->i_sem);
711 rpc_release_path(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return dentry;
Trond Myklebustf1345852005-09-23 11:08:25 -0400713err_dput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 dput(dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400715 dentry = ERR_PTR(-ENOMEM);
716 printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
717 __FILE__, __FUNCTION__, path, -ENOMEM);
718 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Trond Myklebustf1345852005-09-23 11:08:25 -0400721int
722rpc_unlink(char *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Trond Myklebustf1345852005-09-23 11:08:25 -0400724 struct nameidata nd;
725 struct dentry *dentry;
726 struct inode *dir;
727 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Trond Myklebustf1345852005-09-23 11:08:25 -0400729 if ((error = rpc_lookup_parent(path, &nd)) != 0)
730 return error;
731 dir = nd.dentry->d_inode;
732 down(&dir->i_sem);
Christoph Hellwig49705b72005-11-08 21:35:06 -0800733 dentry = lookup_hash(&nd);
Trond Myklebustf1345852005-09-23 11:08:25 -0400734 if (IS_ERR(dentry)) {
735 error = PTR_ERR(dentry);
736 goto out_release;
737 }
738 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (dentry->d_inode) {
740 rpc_close_pipes(dentry->d_inode);
Trond Myklebustf1345852005-09-23 11:08:25 -0400741 error = simple_unlink(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400743 dput(dentry);
744 inode_dir_notify(dir, DN_DELETE);
745out_release:
746 up(&dir->i_sem);
747 rpc_release_path(&nd);
748 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
751/*
752 * populate the filesystem
753 */
754static struct super_operations s_ops = {
755 .alloc_inode = rpc_alloc_inode,
756 .destroy_inode = rpc_destroy_inode,
757 .statfs = simple_statfs,
758};
759
760#define RPCAUTH_GSSMAGIC 0x67596969
761
762static int
763rpc_fill_super(struct super_block *sb, void *data, int silent)
764{
765 struct inode *inode;
766 struct dentry *root;
767
768 sb->s_blocksize = PAGE_CACHE_SIZE;
769 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
770 sb->s_magic = RPCAUTH_GSSMAGIC;
771 sb->s_op = &s_ops;
772 sb->s_time_gran = 1;
773
774 inode = rpc_get_inode(sb, S_IFDIR | 0755);
775 if (!inode)
776 return -ENOMEM;
777 root = d_alloc_root(inode);
778 if (!root) {
779 iput(inode);
780 return -ENOMEM;
781 }
782 if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
783 goto out;
784 sb->s_root = root;
785 return 0;
786out:
787 d_genocide(root);
788 dput(root);
789 return -ENOMEM;
790}
791
792static struct super_block *
793rpc_get_sb(struct file_system_type *fs_type,
794 int flags, const char *dev_name, void *data)
795{
796 return get_sb_single(fs_type, flags, data, rpc_fill_super);
797}
798
799static struct file_system_type rpc_pipe_fs_type = {
800 .owner = THIS_MODULE,
801 .name = "rpc_pipefs",
802 .get_sb = rpc_get_sb,
803 .kill_sb = kill_litter_super,
804};
805
806static void
807init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
808{
809 struct rpc_inode *rpci = (struct rpc_inode *) foo;
810
811 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
812 SLAB_CTOR_CONSTRUCTOR) {
813 inode_init_once(&rpci->vfs_inode);
814 rpci->private = NULL;
815 rpci->nreaders = 0;
816 rpci->nwriters = 0;
817 INIT_LIST_HEAD(&rpci->in_upcall);
818 INIT_LIST_HEAD(&rpci->pipe);
819 rpci->pipelen = 0;
820 init_waitqueue_head(&rpci->waitq);
821 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
822 rpci->ops = NULL;
823 }
824}
825
826int register_rpc_pipefs(void)
827{
828 rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
829 sizeof(struct rpc_inode),
830 0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
831 init_once, NULL);
832 if (!rpc_inode_cachep)
833 return -ENOMEM;
834 register_filesystem(&rpc_pipe_fs_type);
835 return 0;
836}
837
838void unregister_rpc_pipefs(void)
839{
840 if (kmem_cache_destroy(rpc_inode_cachep))
841 printk(KERN_WARNING "RPC: unable to free inode cache\n");
842 unregister_filesystem(&rpc_pipe_fs_type);
843}