VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / sunrpc / rpc_pipe.c
1 /*
2  * net/sunrpc/rpc_pipe.c
3  *
4  * Userland/kernel interface for rpcauth_gss.
5  * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
6  * and fs/driverfs/inode.c
7  *
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
31 static struct vfsmount *rpc_mount;
32 static int rpc_mount_count;
33
34 static struct file_system_type rpc_pipe_fs_type;
35
36
37 static kmem_cache_t *rpc_inode_cachep;
38
39 #define RPC_UPCALL_TIMEOUT (30*HZ)
40
41 static void
42 __rpc_purge_upcall(struct inode *inode, int err)
43 {
44         struct rpc_inode *rpci = RPC_I(inode);
45         struct rpc_pipe_msg *msg;
46
47         while (!list_empty(&rpci->pipe)) {
48                 msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
49                 list_del_init(&msg->list);
50                 msg->errno = err;
51                 rpci->ops->destroy_msg(msg);
52         }
53         while (!list_empty(&rpci->in_upcall)) {
54                 msg = list_entry(rpci->pipe.next, struct rpc_pipe_msg, list);
55                 list_del_init(&msg->list);
56                 msg->errno = err;
57                 rpci->ops->destroy_msg(msg);
58         }
59         rpci->pipelen = 0;
60         wake_up(&rpci->waitq);
61 }
62
63 static void
64 rpc_timeout_upcall_queue(void *data)
65 {
66         struct rpc_inode *rpci = (struct rpc_inode *)data;
67         struct inode *inode = &rpci->vfs_inode;
68
69         down(&inode->i_sem);
70         if (rpci->nreaders == 0 && !list_empty(&rpci->pipe))
71                 __rpc_purge_upcall(inode, -ETIMEDOUT);
72         up(&inode->i_sem);
73 }
74
75 int
76 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
77 {
78         struct rpc_inode *rpci = RPC_I(inode);
79         int res = 0;
80
81         down(&inode->i_sem);
82         if (rpci->nreaders) {
83                 list_add_tail(&msg->list, &rpci->pipe);
84                 rpci->pipelen += msg->len;
85         } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
86                 if (list_empty(&rpci->pipe))
87                         schedule_delayed_work(&rpci->queue_timeout,
88                                         RPC_UPCALL_TIMEOUT);
89                 list_add_tail(&msg->list, &rpci->pipe);
90                 rpci->pipelen += msg->len;
91         } else
92                 res = -EPIPE;
93         up(&inode->i_sem);
94         wake_up(&rpci->waitq);
95         return res;
96 }
97
98 static void
99 rpc_close_pipes(struct inode *inode)
100 {
101         struct rpc_inode *rpci = RPC_I(inode);
102
103         cancel_delayed_work(&rpci->queue_timeout);
104         flush_scheduled_work();
105         down(&inode->i_sem);
106         if (rpci->ops != NULL) {
107                 rpci->nreaders = 0;
108                 __rpc_purge_upcall(inode, -EPIPE);
109                 rpci->nwriters = 0;
110                 if (rpci->ops->release_pipe)
111                         rpci->ops->release_pipe(inode);
112                 rpci->ops = NULL;
113         }
114         up(&inode->i_sem);
115 }
116
117 static inline void
118 rpc_inode_setowner(struct inode *inode, void *private)
119 {
120         RPC_I(inode)->private = private;
121 }
122
123 static struct inode *
124 rpc_alloc_inode(struct super_block *sb)
125 {
126         struct rpc_inode *rpci;
127         rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, SLAB_KERNEL);
128         if (!rpci)
129                 return NULL;
130         return &rpci->vfs_inode;
131 }
132
133 static void
134 rpc_destroy_inode(struct inode *inode)
135 {
136         kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
137 }
138
139 static int
140 rpc_pipe_open(struct inode *inode, struct file *filp)
141 {
142         struct rpc_inode *rpci = RPC_I(inode);
143         int res = -ENXIO;
144
145         down(&inode->i_sem);
146         if (rpci->ops != NULL) {
147                 if (filp->f_mode & FMODE_READ)
148                         rpci->nreaders ++;
149                 if (filp->f_mode & FMODE_WRITE)
150                         rpci->nwriters ++;
151                 res = 0;
152         }
153         up(&inode->i_sem);
154         return res;
155 }
156
157 static int
158 rpc_pipe_release(struct inode *inode, struct file *filp)
159 {
160         struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
161         struct rpc_pipe_msg *msg;
162
163         down(&inode->i_sem);
164         if (rpci->ops == NULL)
165                 goto out;
166         msg = (struct rpc_pipe_msg *)filp->private_data;
167         if (msg != NULL) {
168                 msg->errno = -EPIPE;
169                 list_del_init(&msg->list);
170                 rpci->ops->destroy_msg(msg);
171         }
172         if (filp->f_mode & FMODE_WRITE)
173                 rpci->nwriters --;
174         if (filp->f_mode & FMODE_READ)
175                 rpci->nreaders --;
176         if (!rpci->nreaders)
177                 __rpc_purge_upcall(inode, -EPIPE);
178         if (rpci->ops->release_pipe)
179                 rpci->ops->release_pipe(inode);
180 out:
181         up(&inode->i_sem);
182         return 0;
183 }
184
185 static ssize_t
186 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
187 {
188         struct inode *inode = filp->f_dentry->d_inode;
189         struct rpc_inode *rpci = RPC_I(inode);
190         struct rpc_pipe_msg *msg;
191         int res = 0;
192
193         down(&inode->i_sem);
194         if (rpci->ops == NULL) {
195                 res = -EPIPE;
196                 goto out_unlock;
197         }
198         msg = filp->private_data;
199         if (msg == NULL) {
200                 if (!list_empty(&rpci->pipe)) {
201                         msg = list_entry(rpci->pipe.next,
202                                         struct rpc_pipe_msg,
203                                         list);
204                         list_move(&msg->list, &rpci->in_upcall);
205                         rpci->pipelen -= msg->len;
206                         filp->private_data = msg;
207                         msg->copied = 0;
208                 }
209                 if (msg == NULL)
210                         goto out_unlock;
211         }
212         /* NOTE: it is up to the callback to update msg->copied */
213         res = rpci->ops->upcall(filp, msg, buf, len);
214         if (res < 0 || msg->len == msg->copied) {
215                 filp->private_data = NULL;
216                 list_del_init(&msg->list);
217                 rpci->ops->destroy_msg(msg);
218         }
219 out_unlock:
220         up(&inode->i_sem);
221         return res;
222 }
223
224 static ssize_t
225 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
226 {
227         struct inode *inode = filp->f_dentry->d_inode;
228         struct rpc_inode *rpci = RPC_I(inode);
229         int res;
230
231         down(&inode->i_sem);
232         res = -EPIPE;
233         if (rpci->ops != NULL)
234                 res = rpci->ops->downcall(filp, buf, len);
235         up(&inode->i_sem);
236         return res;
237 }
238
239 static unsigned int
240 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
241 {
242         struct rpc_inode *rpci;
243         unsigned int mask = 0;
244
245         rpci = RPC_I(filp->f_dentry->d_inode);
246         poll_wait(filp, &rpci->waitq, wait);
247
248         mask = POLLOUT | POLLWRNORM;
249         if (rpci->ops == NULL)
250                 mask |= POLLERR | POLLHUP;
251         if (!list_empty(&rpci->pipe))
252                 mask |= POLLIN | POLLRDNORM;
253         return mask;
254 }
255
256 static int
257 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
258                 unsigned int cmd, unsigned long arg)
259 {
260         struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
261         int len;
262
263         switch (cmd) {
264         case FIONREAD:
265                 if (rpci->ops == NULL)
266                         return -EPIPE;
267                 len = rpci->pipelen;
268                 if (filp->private_data) {
269                         struct rpc_pipe_msg *msg;
270                         msg = (struct rpc_pipe_msg *)filp->private_data;
271                         len += msg->len - msg->copied;
272                 }
273                 return put_user(len, (int __user *)arg);
274         default:
275                 return -EINVAL;
276         }
277 }
278
279 struct inode_operations rpc_pipe_iops = {
280         .lookup         = simple_lookup,
281 };
282
283
284 struct file_operations rpc_pipe_fops = {
285         .owner          = THIS_MODULE,
286         .llseek         = no_llseek,
287         .read           = rpc_pipe_read,
288         .write          = rpc_pipe_write,
289         .poll           = rpc_pipe_poll,
290         .ioctl          = rpc_pipe_ioctl,
291         .open           = rpc_pipe_open,
292         .release        = rpc_pipe_release,
293 };
294
295 static int
296 rpc_show_info(struct seq_file *m, void *v)
297 {
298         struct rpc_clnt *clnt = m->private;
299
300         seq_printf(m, "RPC server: %s\n", clnt->cl_server);
301         seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
302                         clnt->cl_prog, clnt->cl_vers);
303         seq_printf(m, "address: %u.%u.%u.%u\n",
304                         NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
305         seq_printf(m, "protocol: %s\n",
306                         clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
307         return 0;
308 }
309
310 static int
311 rpc_info_open(struct inode *inode, struct file *file)
312 {
313         struct rpc_clnt *clnt;
314         int ret = single_open(file, rpc_show_info, NULL);
315
316         if (!ret) {
317                 struct seq_file *m = file->private_data;
318                 down(&inode->i_sem);
319                 clnt = RPC_I(inode)->private;
320                 if (clnt) {
321                         atomic_inc(&clnt->cl_users);
322                         m->private = clnt;
323                 } else {
324                         single_release(inode, file);
325                         ret = -EINVAL;
326                 }
327                 up(&inode->i_sem);
328         }
329         return ret;
330 }
331
332 static int
333 rpc_info_release(struct inode *inode, struct file *file)
334 {
335         struct seq_file *m = file->private_data;
336         struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
337
338         if (clnt)
339                 rpc_release_client(clnt);
340         return single_release(inode, file);
341 }
342
343 static struct file_operations rpc_info_operations = {
344         .owner          = THIS_MODULE,
345         .open           = rpc_info_open,
346         .read           = seq_read,
347         .llseek         = seq_lseek,
348         .release        = rpc_info_release,
349 };
350
351
352 /*
353  * We have a single directory with 1 node in it.
354  */
355 enum {
356         RPCAUTH_Root = 1,
357         RPCAUTH_lockd,
358         RPCAUTH_mount,
359         RPCAUTH_nfs,
360         RPCAUTH_portmap,
361         RPCAUTH_statd,
362         RPCAUTH_RootEOF
363 };
364
365 /*
366  * Description of fs contents.
367  */
368 struct rpc_filelist {
369         char *name;
370         struct file_operations *i_fop;
371         int mode;
372 };
373
374 static struct rpc_filelist files[] = {
375         [RPCAUTH_lockd] = {
376                 .name = "lockd",
377                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
378         },
379         [RPCAUTH_mount] = {
380                 .name = "mount",
381                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
382         },
383         [RPCAUTH_nfs] = {
384                 .name = "nfs",
385                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
386         },
387         [RPCAUTH_portmap] = {
388                 .name = "portmap",
389                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
390         },
391         [RPCAUTH_statd] = {
392                 .name = "statd",
393                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
394         },
395 };
396
397 enum {
398         RPCAUTH_info = 2,
399         RPCAUTH_EOF
400 };
401
402 static struct rpc_filelist authfiles[] = {
403         [RPCAUTH_info] = {
404                 .name = "info",
405                 .i_fop = &rpc_info_operations,
406                 .mode = S_IFREG | S_IRUSR,
407         },
408 };
409
410 static int
411 rpc_get_mount(void)
412 {
413         return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
414 }
415
416 static void
417 rpc_put_mount(void)
418 {
419         simple_release_fs(&rpc_mount, &rpc_mount_count);
420 }
421
422 static int
423 rpc_lookup_parent(char *path, struct nameidata *nd)
424 {
425         if (path[0] == '\0')
426                 return -ENOENT;
427         if (rpc_get_mount()) {
428                 printk(KERN_WARNING "%s: %s failed to mount "
429                                "pseudofilesystem \n", __FILE__, __FUNCTION__);
430                 return -ENODEV;
431         }
432         nd->mnt = mntget(rpc_mount);
433         nd->dentry = dget(rpc_mount->mnt_root);
434         nd->last_type = LAST_ROOT;
435         nd->flags = LOOKUP_PARENT;
436         nd->depth = 0;
437
438         if (path_walk(path, nd)) {
439                 printk(KERN_WARNING "%s: %s failed to find path %s\n",
440                                 __FILE__, __FUNCTION__, path);
441                 rpc_put_mount();
442                 return -ENOENT;
443         }
444         return 0;
445 }
446
447 static void
448 rpc_release_path(struct nameidata *nd)
449 {
450         path_release(nd);
451         rpc_put_mount();
452 }
453
454 static struct inode *
455 rpc_get_inode(struct super_block *sb, int mode)
456 {
457         struct inode *inode = new_inode(sb);
458         if (!inode)
459                 return NULL;
460         inode->i_mode = mode;
461         inode->i_uid = inode->i_gid = 0;
462         inode->i_blksize = PAGE_CACHE_SIZE;
463         inode->i_blocks = 0;
464         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
465         switch(mode & S_IFMT) {
466                 case S_IFDIR:
467                         inode->i_fop = &simple_dir_operations;
468                         inode->i_op = &simple_dir_inode_operations;
469                         inode->i_nlink++;
470                 default:
471                         break;
472         }
473         return inode;
474 }
475
476 /*
477  * FIXME: This probably has races.
478  */
479 static void
480 rpc_depopulate(struct dentry *parent)
481 {
482         struct inode *dir = parent->d_inode;
483         struct list_head *pos, *next;
484         struct dentry *dentry, *dvec[10];
485         int n = 0;
486
487         down(&dir->i_sem);
488 repeat:
489         spin_lock(&dcache_lock);
490         list_for_each_safe(pos, next, &parent->d_subdirs) {
491                 dentry = list_entry(pos, struct dentry, d_child);
492                 spin_lock(&dentry->d_lock);
493                 if (!d_unhashed(dentry)) {
494                         dget_locked(dentry);
495                         __d_drop(dentry);
496                         spin_unlock(&dentry->d_lock);
497                         dvec[n++] = dentry;
498                         if (n == ARRAY_SIZE(dvec))
499                                 break;
500                 } else
501                         spin_unlock(&dentry->d_lock);
502         }
503         spin_unlock(&dcache_lock);
504         if (n) {
505                 do {
506                         dentry = dvec[--n];
507                         if (dentry->d_inode) {
508                                 rpc_close_pipes(dentry->d_inode);
509                                 rpc_inode_setowner(dentry->d_inode, NULL);
510                                 simple_unlink(dir, dentry);
511                         }
512                         dput(dentry);
513                 } while (n);
514                 goto repeat;
515         }
516         up(&dir->i_sem);
517 }
518
519 static int
520 rpc_populate(struct dentry *parent,
521                 struct rpc_filelist *files,
522                 int start, int eof)
523 {
524         struct inode *inode, *dir = parent->d_inode;
525         void *private = RPC_I(dir)->private;
526         struct qstr name;
527         struct dentry *dentry;
528         int mode, i;
529
530         down(&dir->i_sem);
531         for (i = start; i < eof; i++) {
532                 name.name = files[i].name;
533                 name.len = strlen(name.name);
534                 name.hash = full_name_hash(name.name, name.len);
535                 dentry = d_alloc(parent, &name);
536                 if (!dentry)
537                         goto out_bad;
538                 mode = files[i].mode;
539                 inode = rpc_get_inode(dir->i_sb, mode);
540                 if (!inode) {
541                         dput(dentry);
542                         goto out_bad;
543                 }
544                 inode->i_ino = i;
545                 if (files[i].i_fop)
546                         inode->i_fop = files[i].i_fop;
547                 if (private)
548                         rpc_inode_setowner(inode, private);
549                 if (S_ISDIR(mode))
550                         dir->i_nlink++;
551                 d_add(dentry, inode);
552         }
553         up(&dir->i_sem);
554         return 0;
555 out_bad:
556         up(&dir->i_sem);
557         printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
558                         __FILE__, __FUNCTION__, parent->d_name.name);
559         return -ENOMEM;
560 }
561
562 static int
563 __rpc_mkdir(struct inode *dir, struct dentry *dentry)
564 {
565         struct inode *inode;
566
567         inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
568         if (!inode)
569                 goto out_err;
570         inode->i_ino = iunique(dir->i_sb, 100);
571         d_instantiate(dentry, inode);
572         dir->i_nlink++;
573         inode_dir_notify(dir, DN_CREATE);
574         rpc_get_mount();
575         return 0;
576 out_err:
577         printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
578                         __FILE__, __FUNCTION__, dentry->d_name.name);
579         return -ENOMEM;
580 }
581
582 static int
583 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
584 {
585         int error;
586
587         shrink_dcache_parent(dentry);
588         if (dentry->d_inode) {
589                 rpc_close_pipes(dentry->d_inode);
590                 rpc_inode_setowner(dentry->d_inode, NULL);
591         }
592         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 }
601
602 struct dentry *
603 rpc_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);
613         dentry = lookup_hash(&nd->last, nd->dentry);
614         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;
622 out_err:
623         up(&dir->i_sem);
624         rpc_release_path(nd);
625         return dentry;
626 }
627
628
629 struct dentry *
630 rpc_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;
648 out:
649         up(&dir->i_sem);
650         rpc_release_path(&nd);
651         return dentry;
652 err_depopulate:
653         rpc_depopulate(dentry);
654         __rpc_rmdir(dir, dentry);
655 err_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
663 int
664 rpc_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);
675         dentry = lookup_hash(&nd.last, nd.dentry);
676         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);
683 out_release:
684         up(&dir->i_sem);
685         rpc_release_path(&nd);
686         return error;
687 }
688
689 struct dentry *
690 rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
691 {
692         struct nameidata nd;
693         struct dentry *dentry;
694         struct inode *dir, *inode;
695         struct rpc_inode *rpci;
696
697         dentry = rpc_lookup_negative(path, &nd);
698         if (IS_ERR(dentry))
699                 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;
704         inode->i_ino = iunique(dir->i_sb, 100);
705         inode->i_fop = &rpc_pipe_fops;
706         d_instantiate(dentry, inode);
707         rpci = RPC_I(inode);
708         rpci->private = private;
709         rpci->flags = flags;
710         rpci->ops = ops;
711         inode_dir_notify(dir, DN_CREATE);
712 out:
713         up(&dir->i_sem);
714         rpc_release_path(&nd);
715         return dentry;
716 err_dput:
717         dput(dentry);
718         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;
722 }
723
724 int
725 rpc_unlink(char *path)
726 {
727         struct nameidata nd;
728         struct dentry *dentry;
729         struct inode *dir;
730         int error;
731
732         if ((error = rpc_lookup_parent(path, &nd)) != 0)
733                 return error;
734         dir = nd.dentry->d_inode;
735         down(&dir->i_sem);
736         dentry = lookup_hash(&nd.last, nd.dentry);
737         if (IS_ERR(dentry)) {
738                 error = PTR_ERR(dentry);
739                 goto out_release;
740         }
741         d_drop(dentry);
742         if (dentry->d_inode) {
743                 rpc_close_pipes(dentry->d_inode);
744                 rpc_inode_setowner(dentry->d_inode, NULL);
745                 error = simple_unlink(dir, dentry);
746         }
747         dput(dentry);
748         inode_dir_notify(dir, DN_DELETE);
749 out_release:
750         up(&dir->i_sem);
751         rpc_release_path(&nd);
752         return error;
753 }
754
755 /*
756  * populate the filesystem
757  */
758 static struct super_operations s_ops = {
759         .alloc_inode    = rpc_alloc_inode,
760         .destroy_inode  = rpc_destroy_inode,
761         .statfs         = simple_statfs,
762 };
763
764 #define RPCAUTH_GSSMAGIC 0x67596969
765
766 static int
767 rpc_fill_super(struct super_block *sb, void *data, int silent)
768 {
769         struct inode *inode;
770         struct dentry *root;
771
772         sb->s_blocksize = PAGE_CACHE_SIZE;
773         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
774         sb->s_magic = RPCAUTH_GSSMAGIC;
775         sb->s_op = &s_ops;
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;
789 out:
790         d_genocide(root);
791         dput(root);
792         return -ENOMEM;
793 }
794
795 static struct super_block *
796 rpc_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
802 static 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
809 static void
810 init_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
829 int 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
841 void 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 }