Merge /home/trondmy/scm/kernel/git/torvalds/linux-2.6
[linux-2.6] / 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/sysfs/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 __read_mostly;
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 __read_mostly;
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         if (!rpci->nreaders && !rpci->nwriters)
181                 rpci->ops = NULL;
182 out:
183         up(&inode->i_sem);
184         return 0;
185 }
186
187 static ssize_t
188 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
189 {
190         struct inode *inode = filp->f_dentry->d_inode;
191         struct rpc_inode *rpci = RPC_I(inode);
192         struct rpc_pipe_msg *msg;
193         int res = 0;
194
195         down(&inode->i_sem);
196         if (rpci->ops == NULL) {
197                 res = -EPIPE;
198                 goto out_unlock;
199         }
200         msg = filp->private_data;
201         if (msg == NULL) {
202                 if (!list_empty(&rpci->pipe)) {
203                         msg = list_entry(rpci->pipe.next,
204                                         struct rpc_pipe_msg,
205                                         list);
206                         list_move(&msg->list, &rpci->in_upcall);
207                         rpci->pipelen -= msg->len;
208                         filp->private_data = msg;
209                         msg->copied = 0;
210                 }
211                 if (msg == NULL)
212                         goto out_unlock;
213         }
214         /* NOTE: it is up to the callback to update msg->copied */
215         res = rpci->ops->upcall(filp, msg, buf, len);
216         if (res < 0 || msg->len == msg->copied) {
217                 filp->private_data = NULL;
218                 list_del_init(&msg->list);
219                 rpci->ops->destroy_msg(msg);
220         }
221 out_unlock:
222         up(&inode->i_sem);
223         return res;
224 }
225
226 static ssize_t
227 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
228 {
229         struct inode *inode = filp->f_dentry->d_inode;
230         struct rpc_inode *rpci = RPC_I(inode);
231         int res;
232
233         down(&inode->i_sem);
234         res = -EPIPE;
235         if (rpci->ops != NULL)
236                 res = rpci->ops->downcall(filp, buf, len);
237         up(&inode->i_sem);
238         return res;
239 }
240
241 static unsigned int
242 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
243 {
244         struct rpc_inode *rpci;
245         unsigned int mask = 0;
246
247         rpci = RPC_I(filp->f_dentry->d_inode);
248         poll_wait(filp, &rpci->waitq, wait);
249
250         mask = POLLOUT | POLLWRNORM;
251         if (rpci->ops == NULL)
252                 mask |= POLLERR | POLLHUP;
253         if (!list_empty(&rpci->pipe))
254                 mask |= POLLIN | POLLRDNORM;
255         return mask;
256 }
257
258 static int
259 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
260                 unsigned int cmd, unsigned long arg)
261 {
262         struct rpc_inode *rpci = RPC_I(filp->f_dentry->d_inode);
263         int len;
264
265         switch (cmd) {
266         case FIONREAD:
267                 if (rpci->ops == NULL)
268                         return -EPIPE;
269                 len = rpci->pipelen;
270                 if (filp->private_data) {
271                         struct rpc_pipe_msg *msg;
272                         msg = (struct rpc_pipe_msg *)filp->private_data;
273                         len += msg->len - msg->copied;
274                 }
275                 return put_user(len, (int __user *)arg);
276         default:
277                 return -EINVAL;
278         }
279 }
280
281 static struct file_operations rpc_pipe_fops = {
282         .owner          = THIS_MODULE,
283         .llseek         = no_llseek,
284         .read           = rpc_pipe_read,
285         .write          = rpc_pipe_write,
286         .poll           = rpc_pipe_poll,
287         .ioctl          = rpc_pipe_ioctl,
288         .open           = rpc_pipe_open,
289         .release        = rpc_pipe_release,
290 };
291
292 static int
293 rpc_show_info(struct seq_file *m, void *v)
294 {
295         struct rpc_clnt *clnt = m->private;
296
297         seq_printf(m, "RPC server: %s\n", clnt->cl_server);
298         seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
299                         clnt->cl_prog, clnt->cl_vers);
300         seq_printf(m, "address: %u.%u.%u.%u\n",
301                         NIPQUAD(clnt->cl_xprt->addr.sin_addr.s_addr));
302         seq_printf(m, "protocol: %s\n",
303                         clnt->cl_xprt->prot == IPPROTO_UDP ? "udp" : "tcp");
304         return 0;
305 }
306
307 static int
308 rpc_info_open(struct inode *inode, struct file *file)
309 {
310         struct rpc_clnt *clnt;
311         int ret = single_open(file, rpc_show_info, NULL);
312
313         if (!ret) {
314                 struct seq_file *m = file->private_data;
315                 down(&inode->i_sem);
316                 clnt = RPC_I(inode)->private;
317                 if (clnt) {
318                         atomic_inc(&clnt->cl_users);
319                         m->private = clnt;
320                 } else {
321                         single_release(inode, file);
322                         ret = -EINVAL;
323                 }
324                 up(&inode->i_sem);
325         }
326         return ret;
327 }
328
329 static int
330 rpc_info_release(struct inode *inode, struct file *file)
331 {
332         struct seq_file *m = file->private_data;
333         struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
334
335         if (clnt)
336                 rpc_release_client(clnt);
337         return single_release(inode, file);
338 }
339
340 static struct file_operations rpc_info_operations = {
341         .owner          = THIS_MODULE,
342         .open           = rpc_info_open,
343         .read           = seq_read,
344         .llseek         = seq_lseek,
345         .release        = rpc_info_release,
346 };
347
348
349 /*
350  * We have a single directory with 1 node in it.
351  */
352 enum {
353         RPCAUTH_Root = 1,
354         RPCAUTH_lockd,
355         RPCAUTH_mount,
356         RPCAUTH_nfs,
357         RPCAUTH_portmap,
358         RPCAUTH_statd,
359         RPCAUTH_RootEOF
360 };
361
362 /*
363  * Description of fs contents.
364  */
365 struct rpc_filelist {
366         char *name;
367         struct file_operations *i_fop;
368         int mode;
369 };
370
371 static struct rpc_filelist files[] = {
372         [RPCAUTH_lockd] = {
373                 .name = "lockd",
374                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
375         },
376         [RPCAUTH_mount] = {
377                 .name = "mount",
378                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
379         },
380         [RPCAUTH_nfs] = {
381                 .name = "nfs",
382                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
383         },
384         [RPCAUTH_portmap] = {
385                 .name = "portmap",
386                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
387         },
388         [RPCAUTH_statd] = {
389                 .name = "statd",
390                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
391         },
392 };
393
394 enum {
395         RPCAUTH_info = 2,
396         RPCAUTH_EOF
397 };
398
399 static struct rpc_filelist authfiles[] = {
400         [RPCAUTH_info] = {
401                 .name = "info",
402                 .i_fop = &rpc_info_operations,
403                 .mode = S_IFREG | S_IRUSR,
404         },
405 };
406
407 static int
408 rpc_get_mount(void)
409 {
410         return simple_pin_fs("rpc_pipefs", &rpc_mount, &rpc_mount_count);
411 }
412
413 static void
414 rpc_put_mount(void)
415 {
416         simple_release_fs(&rpc_mount, &rpc_mount_count);
417 }
418
419 static int
420 rpc_lookup_parent(char *path, struct nameidata *nd)
421 {
422         if (path[0] == '\0')
423                 return -ENOENT;
424         if (rpc_get_mount()) {
425                 printk(KERN_WARNING "%s: %s failed to mount "
426                                "pseudofilesystem \n", __FILE__, __FUNCTION__);
427                 return -ENODEV;
428         }
429         nd->mnt = mntget(rpc_mount);
430         nd->dentry = dget(rpc_mount->mnt_root);
431         nd->last_type = LAST_ROOT;
432         nd->flags = LOOKUP_PARENT;
433         nd->depth = 0;
434
435         if (path_walk(path, nd)) {
436                 printk(KERN_WARNING "%s: %s failed to find path %s\n",
437                                 __FILE__, __FUNCTION__, path);
438                 rpc_put_mount();
439                 return -ENOENT;
440         }
441         return 0;
442 }
443
444 static void
445 rpc_release_path(struct nameidata *nd)
446 {
447         path_release(nd);
448         rpc_put_mount();
449 }
450
451 static struct inode *
452 rpc_get_inode(struct super_block *sb, int mode)
453 {
454         struct inode *inode = new_inode(sb);
455         if (!inode)
456                 return NULL;
457         inode->i_mode = mode;
458         inode->i_uid = inode->i_gid = 0;
459         inode->i_blksize = PAGE_CACHE_SIZE;
460         inode->i_blocks = 0;
461         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
462         switch(mode & S_IFMT) {
463                 case S_IFDIR:
464                         inode->i_fop = &simple_dir_operations;
465                         inode->i_op = &simple_dir_inode_operations;
466                         inode->i_nlink++;
467                 default:
468                         break;
469         }
470         return inode;
471 }
472
473 /*
474  * FIXME: This probably has races.
475  */
476 static void
477 rpc_depopulate(struct dentry *parent)
478 {
479         struct inode *dir = parent->d_inode;
480         struct list_head *pos, *next;
481         struct dentry *dentry, *dvec[10];
482         int n = 0;
483
484         down(&dir->i_sem);
485 repeat:
486         spin_lock(&dcache_lock);
487         list_for_each_safe(pos, next, &parent->d_subdirs) {
488                 dentry = list_entry(pos, struct dentry, d_child);
489                 spin_lock(&dentry->d_lock);
490                 if (!d_unhashed(dentry)) {
491                         dget_locked(dentry);
492                         __d_drop(dentry);
493                         spin_unlock(&dentry->d_lock);
494                         dvec[n++] = dentry;
495                         if (n == ARRAY_SIZE(dvec))
496                                 break;
497                 } else
498                         spin_unlock(&dentry->d_lock);
499         }
500         spin_unlock(&dcache_lock);
501         if (n) {
502                 do {
503                         dentry = dvec[--n];
504                         if (dentry->d_inode) {
505                                 rpc_close_pipes(dentry->d_inode);
506                                 rpc_inode_setowner(dentry->d_inode, NULL);
507                                 simple_unlink(dir, dentry);
508                         }
509                         dput(dentry);
510                 } while (n);
511                 goto repeat;
512         }
513         up(&dir->i_sem);
514 }
515
516 static int
517 rpc_populate(struct dentry *parent,
518                 struct rpc_filelist *files,
519                 int start, int eof)
520 {
521         struct inode *inode, *dir = parent->d_inode;
522         void *private = RPC_I(dir)->private;
523         struct dentry *dentry;
524         int mode, i;
525
526         down(&dir->i_sem);
527         for (i = start; i < eof; i++) {
528                 dentry = d_alloc_name(parent, files[i].name);
529                 if (!dentry)
530                         goto out_bad;
531                 mode = files[i].mode;
532                 inode = rpc_get_inode(dir->i_sb, mode);
533                 if (!inode) {
534                         dput(dentry);
535                         goto out_bad;
536                 }
537                 inode->i_ino = i;
538                 if (files[i].i_fop)
539                         inode->i_fop = files[i].i_fop;
540                 if (private)
541                         rpc_inode_setowner(inode, private);
542                 if (S_ISDIR(mode))
543                         dir->i_nlink++;
544                 d_add(dentry, inode);
545         }
546         up(&dir->i_sem);
547         return 0;
548 out_bad:
549         up(&dir->i_sem);
550         printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
551                         __FILE__, __FUNCTION__, parent->d_name.name);
552         return -ENOMEM;
553 }
554
555 static int
556 __rpc_mkdir(struct inode *dir, struct dentry *dentry)
557 {
558         struct inode *inode;
559
560         inode = rpc_get_inode(dir->i_sb, S_IFDIR | S_IRUSR | S_IXUSR);
561         if (!inode)
562                 goto out_err;
563         inode->i_ino = iunique(dir->i_sb, 100);
564         d_instantiate(dentry, inode);
565         dir->i_nlink++;
566         inode_dir_notify(dir, DN_CREATE);
567         rpc_get_mount();
568         return 0;
569 out_err:
570         printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
571                         __FILE__, __FUNCTION__, dentry->d_name.name);
572         return -ENOMEM;
573 }
574
575 static int
576 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
577 {
578         int error;
579
580         shrink_dcache_parent(dentry);
581         if (dentry->d_inode) {
582                 rpc_close_pipes(dentry->d_inode);
583                 rpc_inode_setowner(dentry->d_inode, NULL);
584         }
585         if ((error = simple_rmdir(dir, dentry)) != 0)
586                 return error;
587         if (!error) {
588                 inode_dir_notify(dir, DN_DELETE);
589                 d_drop(dentry);
590                 rpc_put_mount();
591         }
592         return 0;
593 }
594
595 static struct dentry *
596 rpc_lookup_negative(char *path, struct nameidata *nd)
597 {
598         struct dentry *dentry;
599         struct inode *dir;
600         int error;
601
602         if ((error = rpc_lookup_parent(path, nd)) != 0)
603                 return ERR_PTR(error);
604         dir = nd->dentry->d_inode;
605         down(&dir->i_sem);
606         dentry = lookup_hash(&nd->last, nd->dentry);
607         if (IS_ERR(dentry))
608                 goto out_err;
609         if (dentry->d_inode) {
610                 dput(dentry);
611                 dentry = ERR_PTR(-EEXIST);
612                 goto out_err;
613         }
614         return dentry;
615 out_err:
616         up(&dir->i_sem);
617         rpc_release_path(nd);
618         return dentry;
619 }
620
621
622 struct dentry *
623 rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
624 {
625         struct nameidata nd;
626         struct dentry *dentry;
627         struct inode *dir;
628         int error;
629
630         dentry = rpc_lookup_negative(path, &nd);
631         if (IS_ERR(dentry))
632                 return dentry;
633         dir = nd.dentry->d_inode;
634         if ((error = __rpc_mkdir(dir, dentry)) != 0)
635                 goto err_dput;
636         RPC_I(dentry->d_inode)->private = rpc_client;
637         error = rpc_populate(dentry, authfiles,
638                         RPCAUTH_info, RPCAUTH_EOF);
639         if (error)
640                 goto err_depopulate;
641 out:
642         up(&dir->i_sem);
643         rpc_release_path(&nd);
644         return dentry;
645 err_depopulate:
646         rpc_depopulate(dentry);
647         __rpc_rmdir(dir, dentry);
648 err_dput:
649         dput(dentry);
650         printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
651                         __FILE__, __FUNCTION__, path, error);
652         dentry = ERR_PTR(error);
653         goto out;
654 }
655
656 int
657 rpc_rmdir(char *path)
658 {
659         struct nameidata nd;
660         struct dentry *dentry;
661         struct inode *dir;
662         int error;
663
664         if ((error = rpc_lookup_parent(path, &nd)) != 0)
665                 return error;
666         dir = nd.dentry->d_inode;
667         down(&dir->i_sem);
668         dentry = lookup_hash(&nd.last, nd.dentry);
669         if (IS_ERR(dentry)) {
670                 error = PTR_ERR(dentry);
671                 goto out_release;
672         }
673         rpc_depopulate(dentry);
674         error = __rpc_rmdir(dir, dentry);
675         dput(dentry);
676 out_release:
677         up(&dir->i_sem);
678         rpc_release_path(&nd);
679         return error;
680 }
681
682 struct dentry *
683 rpc_mkpipe(char *path, void *private, struct rpc_pipe_ops *ops, int flags)
684 {
685         struct nameidata nd;
686         struct dentry *dentry;
687         struct inode *dir, *inode;
688         struct rpc_inode *rpci;
689
690         dentry = rpc_lookup_negative(path, &nd);
691         if (IS_ERR(dentry))
692                 return dentry;
693         dir = nd.dentry->d_inode;
694         inode = rpc_get_inode(dir->i_sb, S_IFSOCK | S_IRUSR | S_IWUSR);
695         if (!inode)
696                 goto err_dput;
697         inode->i_ino = iunique(dir->i_sb, 100);
698         inode->i_fop = &rpc_pipe_fops;
699         d_instantiate(dentry, inode);
700         rpci = RPC_I(inode);
701         rpci->private = private;
702         rpci->flags = flags;
703         rpci->ops = ops;
704         inode_dir_notify(dir, DN_CREATE);
705 out:
706         up(&dir->i_sem);
707         rpc_release_path(&nd);
708         return dentry;
709 err_dput:
710         dput(dentry);
711         dentry = ERR_PTR(-ENOMEM);
712         printk(KERN_WARNING "%s: %s() failed to create pipe %s (errno = %d)\n",
713                         __FILE__, __FUNCTION__, path, -ENOMEM);
714         goto out;
715 }
716
717 int
718 rpc_unlink(char *path)
719 {
720         struct nameidata nd;
721         struct dentry *dentry;
722         struct inode *dir;
723         int error;
724
725         if ((error = rpc_lookup_parent(path, &nd)) != 0)
726                 return error;
727         dir = nd.dentry->d_inode;
728         down(&dir->i_sem);
729         dentry = lookup_hash(&nd.last, nd.dentry);
730         if (IS_ERR(dentry)) {
731                 error = PTR_ERR(dentry);
732                 goto out_release;
733         }
734         d_drop(dentry);
735         if (dentry->d_inode) {
736                 rpc_close_pipes(dentry->d_inode);
737                 rpc_inode_setowner(dentry->d_inode, NULL);
738                 error = simple_unlink(dir, dentry);
739         }
740         dput(dentry);
741         inode_dir_notify(dir, DN_DELETE);
742 out_release:
743         up(&dir->i_sem);
744         rpc_release_path(&nd);
745         return error;
746 }
747
748 /*
749  * populate the filesystem
750  */
751 static struct super_operations s_ops = {
752         .alloc_inode    = rpc_alloc_inode,
753         .destroy_inode  = rpc_destroy_inode,
754         .statfs         = simple_statfs,
755 };
756
757 #define RPCAUTH_GSSMAGIC 0x67596969
758
759 static int
760 rpc_fill_super(struct super_block *sb, void *data, int silent)
761 {
762         struct inode *inode;
763         struct dentry *root;
764
765         sb->s_blocksize = PAGE_CACHE_SIZE;
766         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
767         sb->s_magic = RPCAUTH_GSSMAGIC;
768         sb->s_op = &s_ops;
769         sb->s_time_gran = 1;
770
771         inode = rpc_get_inode(sb, S_IFDIR | 0755);
772         if (!inode)
773                 return -ENOMEM;
774         root = d_alloc_root(inode);
775         if (!root) {
776                 iput(inode);
777                 return -ENOMEM;
778         }
779         if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
780                 goto out;
781         sb->s_root = root;
782         return 0;
783 out:
784         d_genocide(root);
785         dput(root);
786         return -ENOMEM;
787 }
788
789 static struct super_block *
790 rpc_get_sb(struct file_system_type *fs_type,
791                 int flags, const char *dev_name, void *data)
792 {
793         return get_sb_single(fs_type, flags, data, rpc_fill_super);
794 }
795
796 static struct file_system_type rpc_pipe_fs_type = {
797         .owner          = THIS_MODULE,
798         .name           = "rpc_pipefs",
799         .get_sb         = rpc_get_sb,
800         .kill_sb        = kill_litter_super,
801 };
802
803 static void
804 init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
805 {
806         struct rpc_inode *rpci = (struct rpc_inode *) foo;
807
808         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
809             SLAB_CTOR_CONSTRUCTOR) {
810                 inode_init_once(&rpci->vfs_inode);
811                 rpci->private = NULL;
812                 rpci->nreaders = 0;
813                 rpci->nwriters = 0;
814                 INIT_LIST_HEAD(&rpci->in_upcall);
815                 INIT_LIST_HEAD(&rpci->pipe);
816                 rpci->pipelen = 0;
817                 init_waitqueue_head(&rpci->waitq);
818                 INIT_WORK(&rpci->queue_timeout, rpc_timeout_upcall_queue, rpci);
819                 rpci->ops = NULL;
820         }
821 }
822
823 int register_rpc_pipefs(void)
824 {
825         rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
826                                              sizeof(struct rpc_inode),
827                                              0, SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT,
828                                              init_once, NULL);
829         if (!rpc_inode_cachep)
830                 return -ENOMEM;
831         register_filesystem(&rpc_pipe_fs_type);
832         return 0;
833 }
834
835 void unregister_rpc_pipefs(void)
836 {
837         if (kmem_cache_destroy(rpc_inode_cachep))
838                 printk(KERN_WARNING "RPC: unable to free inode cache\n");
839         unregister_filesystem(&rpc_pipe_fs_type);
840 }