SUNRPC: Clean up rpc_unlink()
[safe/jmp/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/module.h>
12 #include <linux/slab.h>
13 #include <linux/string.h>
14 #include <linux/pagemap.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/fsnotify.h>
18 #include <linux/kernel.h>
19
20 #include <asm/ioctls.h>
21 #include <linux/fs.h>
22 #include <linux/poll.h>
23 #include <linux/wait.h>
24 #include <linux/seq_file.h>
25
26 #include <linux/sunrpc/clnt.h>
27 #include <linux/workqueue.h>
28 #include <linux/sunrpc/rpc_pipe_fs.h>
29
30 static struct vfsmount *rpc_mount __read_mostly;
31 static int rpc_mount_count;
32
33 static struct file_system_type rpc_pipe_fs_type;
34
35
36 static struct kmem_cache *rpc_inode_cachep __read_mostly;
37
38 #define RPC_UPCALL_TIMEOUT (30*HZ)
39
40 static void rpc_purge_list(struct rpc_inode *rpci, struct list_head *head,
41                 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
42 {
43         struct rpc_pipe_msg *msg;
44
45         if (list_empty(head))
46                 return;
47         do {
48                 msg = list_entry(head->next, struct rpc_pipe_msg, list);
49                 list_del(&msg->list);
50                 msg->errno = err;
51                 destroy_msg(msg);
52         } while (!list_empty(head));
53         wake_up(&rpci->waitq);
54 }
55
56 static void
57 rpc_timeout_upcall_queue(struct work_struct *work)
58 {
59         LIST_HEAD(free_list);
60         struct rpc_inode *rpci =
61                 container_of(work, struct rpc_inode, queue_timeout.work);
62         struct inode *inode = &rpci->vfs_inode;
63         void (*destroy_msg)(struct rpc_pipe_msg *);
64
65         spin_lock(&inode->i_lock);
66         if (rpci->ops == NULL) {
67                 spin_unlock(&inode->i_lock);
68                 return;
69         }
70         destroy_msg = rpci->ops->destroy_msg;
71         if (rpci->nreaders == 0) {
72                 list_splice_init(&rpci->pipe, &free_list);
73                 rpci->pipelen = 0;
74         }
75         spin_unlock(&inode->i_lock);
76         rpc_purge_list(rpci, &free_list, destroy_msg, -ETIMEDOUT);
77 }
78
79 /**
80  * rpc_queue_upcall
81  * @inode: inode of upcall pipe on which to queue given message
82  * @msg: message to queue
83  *
84  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
85  * A userspace process may then later read the upcall by performing a
86  * read on an open file for this inode.  It is up to the caller to
87  * initialize the fields of @msg (other than @msg->list) appropriately.
88  */
89 int
90 rpc_queue_upcall(struct inode *inode, struct rpc_pipe_msg *msg)
91 {
92         struct rpc_inode *rpci = RPC_I(inode);
93         int res = -EPIPE;
94
95         spin_lock(&inode->i_lock);
96         if (rpci->ops == NULL)
97                 goto out;
98         if (rpci->nreaders) {
99                 list_add_tail(&msg->list, &rpci->pipe);
100                 rpci->pipelen += msg->len;
101                 res = 0;
102         } else if (rpci->flags & RPC_PIPE_WAIT_FOR_OPEN) {
103                 if (list_empty(&rpci->pipe))
104                         queue_delayed_work(rpciod_workqueue,
105                                         &rpci->queue_timeout,
106                                         RPC_UPCALL_TIMEOUT);
107                 list_add_tail(&msg->list, &rpci->pipe);
108                 rpci->pipelen += msg->len;
109                 res = 0;
110         }
111 out:
112         spin_unlock(&inode->i_lock);
113         wake_up(&rpci->waitq);
114         return res;
115 }
116 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
117
118 static inline void
119 rpc_inode_setowner(struct inode *inode, void *private)
120 {
121         RPC_I(inode)->private = private;
122 }
123
124 static void
125 rpc_close_pipes(struct inode *inode)
126 {
127         struct rpc_inode *rpci = RPC_I(inode);
128         const struct rpc_pipe_ops *ops;
129         int need_release;
130
131         mutex_lock(&inode->i_mutex);
132         ops = rpci->ops;
133         if (ops != NULL) {
134                 LIST_HEAD(free_list);
135                 spin_lock(&inode->i_lock);
136                 need_release = rpci->nreaders != 0 || rpci->nwriters != 0;
137                 rpci->nreaders = 0;
138                 list_splice_init(&rpci->in_upcall, &free_list);
139                 list_splice_init(&rpci->pipe, &free_list);
140                 rpci->pipelen = 0;
141                 rpci->ops = NULL;
142                 spin_unlock(&inode->i_lock);
143                 rpc_purge_list(rpci, &free_list, ops->destroy_msg, -EPIPE);
144                 rpci->nwriters = 0;
145                 if (need_release && ops->release_pipe)
146                         ops->release_pipe(inode);
147                 cancel_delayed_work_sync(&rpci->queue_timeout);
148         }
149         rpc_inode_setowner(inode, NULL);
150         mutex_unlock(&inode->i_mutex);
151 }
152
153 static struct inode *
154 rpc_alloc_inode(struct super_block *sb)
155 {
156         struct rpc_inode *rpci;
157         rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
158         if (!rpci)
159                 return NULL;
160         return &rpci->vfs_inode;
161 }
162
163 static void
164 rpc_destroy_inode(struct inode *inode)
165 {
166         kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
167 }
168
169 static int
170 rpc_pipe_open(struct inode *inode, struct file *filp)
171 {
172         struct rpc_inode *rpci = RPC_I(inode);
173         int first_open;
174         int res = -ENXIO;
175
176         mutex_lock(&inode->i_mutex);
177         if (rpci->ops == NULL)
178                 goto out;
179         first_open = rpci->nreaders == 0 && rpci->nwriters == 0;
180         if (first_open && rpci->ops->open_pipe) {
181                 res = rpci->ops->open_pipe(inode);
182                 if (res)
183                         goto out;
184         }
185         if (filp->f_mode & FMODE_READ)
186                 rpci->nreaders++;
187         if (filp->f_mode & FMODE_WRITE)
188                 rpci->nwriters++;
189         res = 0;
190 out:
191         mutex_unlock(&inode->i_mutex);
192         return res;
193 }
194
195 static int
196 rpc_pipe_release(struct inode *inode, struct file *filp)
197 {
198         struct rpc_inode *rpci = RPC_I(inode);
199         struct rpc_pipe_msg *msg;
200         int last_close;
201
202         mutex_lock(&inode->i_mutex);
203         if (rpci->ops == NULL)
204                 goto out;
205         msg = (struct rpc_pipe_msg *)filp->private_data;
206         if (msg != NULL) {
207                 spin_lock(&inode->i_lock);
208                 msg->errno = -EAGAIN;
209                 list_del(&msg->list);
210                 spin_unlock(&inode->i_lock);
211                 rpci->ops->destroy_msg(msg);
212         }
213         if (filp->f_mode & FMODE_WRITE)
214                 rpci->nwriters --;
215         if (filp->f_mode & FMODE_READ) {
216                 rpci->nreaders --;
217                 if (rpci->nreaders == 0) {
218                         LIST_HEAD(free_list);
219                         spin_lock(&inode->i_lock);
220                         list_splice_init(&rpci->pipe, &free_list);
221                         rpci->pipelen = 0;
222                         spin_unlock(&inode->i_lock);
223                         rpc_purge_list(rpci, &free_list,
224                                         rpci->ops->destroy_msg, -EAGAIN);
225                 }
226         }
227         last_close = rpci->nwriters == 0 && rpci->nreaders == 0;
228         if (last_close && rpci->ops->release_pipe)
229                 rpci->ops->release_pipe(inode);
230 out:
231         mutex_unlock(&inode->i_mutex);
232         return 0;
233 }
234
235 static ssize_t
236 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
237 {
238         struct inode *inode = filp->f_path.dentry->d_inode;
239         struct rpc_inode *rpci = RPC_I(inode);
240         struct rpc_pipe_msg *msg;
241         int res = 0;
242
243         mutex_lock(&inode->i_mutex);
244         if (rpci->ops == NULL) {
245                 res = -EPIPE;
246                 goto out_unlock;
247         }
248         msg = filp->private_data;
249         if (msg == NULL) {
250                 spin_lock(&inode->i_lock);
251                 if (!list_empty(&rpci->pipe)) {
252                         msg = list_entry(rpci->pipe.next,
253                                         struct rpc_pipe_msg,
254                                         list);
255                         list_move(&msg->list, &rpci->in_upcall);
256                         rpci->pipelen -= msg->len;
257                         filp->private_data = msg;
258                         msg->copied = 0;
259                 }
260                 spin_unlock(&inode->i_lock);
261                 if (msg == NULL)
262                         goto out_unlock;
263         }
264         /* NOTE: it is up to the callback to update msg->copied */
265         res = rpci->ops->upcall(filp, msg, buf, len);
266         if (res < 0 || msg->len == msg->copied) {
267                 filp->private_data = NULL;
268                 spin_lock(&inode->i_lock);
269                 list_del(&msg->list);
270                 spin_unlock(&inode->i_lock);
271                 rpci->ops->destroy_msg(msg);
272         }
273 out_unlock:
274         mutex_unlock(&inode->i_mutex);
275         return res;
276 }
277
278 static ssize_t
279 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
280 {
281         struct inode *inode = filp->f_path.dentry->d_inode;
282         struct rpc_inode *rpci = RPC_I(inode);
283         int res;
284
285         mutex_lock(&inode->i_mutex);
286         res = -EPIPE;
287         if (rpci->ops != NULL)
288                 res = rpci->ops->downcall(filp, buf, len);
289         mutex_unlock(&inode->i_mutex);
290         return res;
291 }
292
293 static unsigned int
294 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
295 {
296         struct rpc_inode *rpci;
297         unsigned int mask = 0;
298
299         rpci = RPC_I(filp->f_path.dentry->d_inode);
300         poll_wait(filp, &rpci->waitq, wait);
301
302         mask = POLLOUT | POLLWRNORM;
303         if (rpci->ops == NULL)
304                 mask |= POLLERR | POLLHUP;
305         if (filp->private_data || !list_empty(&rpci->pipe))
306                 mask |= POLLIN | POLLRDNORM;
307         return mask;
308 }
309
310 static int
311 rpc_pipe_ioctl(struct inode *ino, struct file *filp,
312                 unsigned int cmd, unsigned long arg)
313 {
314         struct rpc_inode *rpci = RPC_I(filp->f_path.dentry->d_inode);
315         int len;
316
317         switch (cmd) {
318         case FIONREAD:
319                 if (rpci->ops == NULL)
320                         return -EPIPE;
321                 len = rpci->pipelen;
322                 if (filp->private_data) {
323                         struct rpc_pipe_msg *msg;
324                         msg = (struct rpc_pipe_msg *)filp->private_data;
325                         len += msg->len - msg->copied;
326                 }
327                 return put_user(len, (int __user *)arg);
328         default:
329                 return -EINVAL;
330         }
331 }
332
333 static const struct file_operations rpc_pipe_fops = {
334         .owner          = THIS_MODULE,
335         .llseek         = no_llseek,
336         .read           = rpc_pipe_read,
337         .write          = rpc_pipe_write,
338         .poll           = rpc_pipe_poll,
339         .ioctl          = rpc_pipe_ioctl,
340         .open           = rpc_pipe_open,
341         .release        = rpc_pipe_release,
342 };
343
344 static int
345 rpc_show_info(struct seq_file *m, void *v)
346 {
347         struct rpc_clnt *clnt = m->private;
348
349         seq_printf(m, "RPC server: %s\n", clnt->cl_server);
350         seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
351                         clnt->cl_prog, clnt->cl_vers);
352         seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
353         seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
354         seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
355         return 0;
356 }
357
358 static int
359 rpc_info_open(struct inode *inode, struct file *file)
360 {
361         struct rpc_clnt *clnt;
362         int ret = single_open(file, rpc_show_info, NULL);
363
364         if (!ret) {
365                 struct seq_file *m = file->private_data;
366                 mutex_lock(&inode->i_mutex);
367                 clnt = RPC_I(inode)->private;
368                 if (clnt) {
369                         kref_get(&clnt->cl_kref);
370                         m->private = clnt;
371                 } else {
372                         single_release(inode, file);
373                         ret = -EINVAL;
374                 }
375                 mutex_unlock(&inode->i_mutex);
376         }
377         return ret;
378 }
379
380 static int
381 rpc_info_release(struct inode *inode, struct file *file)
382 {
383         struct seq_file *m = file->private_data;
384         struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
385
386         if (clnt)
387                 rpc_release_client(clnt);
388         return single_release(inode, file);
389 }
390
391 static const struct file_operations rpc_info_operations = {
392         .owner          = THIS_MODULE,
393         .open           = rpc_info_open,
394         .read           = seq_read,
395         .llseek         = seq_lseek,
396         .release        = rpc_info_release,
397 };
398
399
400 /*
401  * We have a single directory with 1 node in it.
402  */
403 enum {
404         RPCAUTH_Root = 1,
405         RPCAUTH_lockd,
406         RPCAUTH_mount,
407         RPCAUTH_nfs,
408         RPCAUTH_portmap,
409         RPCAUTH_statd,
410         RPCAUTH_nfsd4_cb,
411         RPCAUTH_RootEOF
412 };
413
414 /*
415  * Description of fs contents.
416  */
417 struct rpc_filelist {
418         char *name;
419         const struct file_operations *i_fop;
420         umode_t mode;
421 };
422
423 static struct rpc_filelist files[] = {
424         [RPCAUTH_lockd] = {
425                 .name = "lockd",
426                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
427         },
428         [RPCAUTH_mount] = {
429                 .name = "mount",
430                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
431         },
432         [RPCAUTH_nfs] = {
433                 .name = "nfs",
434                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
435         },
436         [RPCAUTH_portmap] = {
437                 .name = "portmap",
438                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
439         },
440         [RPCAUTH_statd] = {
441                 .name = "statd",
442                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
443         },
444         [RPCAUTH_nfsd4_cb] = {
445                 .name = "nfsd4_cb",
446                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
447         },
448 };
449
450 enum {
451         RPCAUTH_info = 2,
452         RPCAUTH_EOF
453 };
454
455 static struct rpc_filelist authfiles[] = {
456         [RPCAUTH_info] = {
457                 .name = "info",
458                 .i_fop = &rpc_info_operations,
459                 .mode = S_IFREG | S_IRUSR,
460         },
461 };
462
463 struct vfsmount *rpc_get_mount(void)
464 {
465         int err;
466
467         err = simple_pin_fs(&rpc_pipe_fs_type, &rpc_mount, &rpc_mount_count);
468         if (err != 0)
469                 return ERR_PTR(err);
470         return rpc_mount;
471 }
472
473 void rpc_put_mount(void)
474 {
475         simple_release_fs(&rpc_mount, &rpc_mount_count);
476 }
477
478 static int rpc_delete_dentry(struct dentry *dentry)
479 {
480         return 1;
481 }
482
483 static const struct dentry_operations rpc_dentry_operations = {
484         .d_delete = rpc_delete_dentry,
485 };
486
487 static int __rpc_lookup_path(const char *pathname, unsigned flags,
488                              struct nameidata *nd)
489 {
490         struct vfsmount *mnt;
491
492         if (pathname[0] == '\0')
493                 return -ENOENT;
494
495         mnt = rpc_get_mount();
496         if (IS_ERR(mnt)) {
497                 printk(KERN_WARNING "%s: %s failed to mount "
498                                "pseudofilesystem \n", __FILE__, __func__);
499                 return PTR_ERR(mnt);
500         }
501
502         if (vfs_path_lookup(mnt->mnt_root, mnt, pathname, flags, nd)) {
503                 printk(KERN_WARNING "%s: %s failed to find path %s\n",
504                                 __FILE__, __func__, pathname);
505                 rpc_put_mount();
506                 return -ENOENT;
507         }
508         return 0;
509 }
510
511 static int rpc_lookup_parent(const char *pathname, struct nameidata *nd)
512 {
513         return __rpc_lookup_path(pathname, LOOKUP_PARENT, nd);
514 }
515
516 static void
517 rpc_release_path(struct nameidata *nd)
518 {
519         path_put(&nd->path);
520         rpc_put_mount();
521 }
522
523 static struct inode *
524 rpc_get_inode(struct super_block *sb, umode_t mode)
525 {
526         struct inode *inode = new_inode(sb);
527         if (!inode)
528                 return NULL;
529         inode->i_mode = mode;
530         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
531         switch(mode & S_IFMT) {
532                 case S_IFDIR:
533                         inode->i_fop = &simple_dir_operations;
534                         inode->i_op = &simple_dir_inode_operations;
535                         inc_nlink(inode);
536                 default:
537                         break;
538         }
539         return inode;
540 }
541
542 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
543                                umode_t mode,
544                                const struct file_operations *i_fop,
545                                void *private)
546 {
547         struct inode *inode;
548
549         BUG_ON(!d_unhashed(dentry));
550         inode = rpc_get_inode(dir->i_sb, mode);
551         if (!inode)
552                 goto out_err;
553         inode->i_ino = iunique(dir->i_sb, 100);
554         if (i_fop)
555                 inode->i_fop = i_fop;
556         if (private)
557                 rpc_inode_setowner(inode, private);
558         d_add(dentry, inode);
559         return 0;
560 out_err:
561         printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
562                         __FILE__, __func__, dentry->d_name.name);
563         dput(dentry);
564         return -ENOMEM;
565 }
566
567 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
568                        umode_t mode,
569                        const struct file_operations *i_fop,
570                        void *private)
571 {
572         int err;
573
574         err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
575         if (err)
576                 return err;
577         inc_nlink(dir);
578         fsnotify_mkdir(dir, dentry);
579         return 0;
580 }
581
582 static int __rpc_mkpipe(struct inode *dir, struct dentry *dentry,
583                         umode_t mode,
584                         const struct file_operations *i_fop,
585                         void *private,
586                         const struct rpc_pipe_ops *ops,
587                         int flags)
588 {
589         struct rpc_inode *rpci;
590         int err;
591
592         err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
593         if (err)
594                 return err;
595         rpci = RPC_I(dentry->d_inode);
596         rpci->nkern_readwriters = 1;
597         rpci->private = private;
598         rpci->flags = flags;
599         rpci->ops = ops;
600         fsnotify_create(dir, dentry);
601         return 0;
602 }
603
604 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
605 {
606         int ret;
607
608         dget(dentry);
609         ret = simple_unlink(dir, dentry);
610         d_delete(dentry);
611         dput(dentry);
612         return ret;
613 }
614
615 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
616 {
617         struct inode *inode = dentry->d_inode;
618         struct rpc_inode *rpci = RPC_I(inode);
619
620         rpci->nkern_readwriters--;
621         if (rpci->nkern_readwriters != 0)
622                 return 0;
623         rpc_close_pipes(inode);
624         return __rpc_unlink(dir, dentry);
625 }
626
627 /*
628  * FIXME: This probably has races.
629  */
630 static void rpc_depopulate(struct dentry *parent,
631                            unsigned long start, unsigned long eof)
632 {
633         struct inode *dir = parent->d_inode;
634         struct list_head *pos, *next;
635         struct dentry *dentry, *dvec[10];
636         int n = 0;
637
638         mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
639 repeat:
640         spin_lock(&dcache_lock);
641         list_for_each_safe(pos, next, &parent->d_subdirs) {
642                 dentry = list_entry(pos, struct dentry, d_u.d_child);
643                 if (!dentry->d_inode ||
644                                 dentry->d_inode->i_ino < start ||
645                                 dentry->d_inode->i_ino >= eof)
646                         continue;
647                 spin_lock(&dentry->d_lock);
648                 if (!d_unhashed(dentry)) {
649                         dget_locked(dentry);
650                         __d_drop(dentry);
651                         spin_unlock(&dentry->d_lock);
652                         dvec[n++] = dentry;
653                         if (n == ARRAY_SIZE(dvec))
654                                 break;
655                 } else
656                         spin_unlock(&dentry->d_lock);
657         }
658         spin_unlock(&dcache_lock);
659         if (n) {
660                 do {
661                         dentry = dvec[--n];
662                         if (S_ISREG(dentry->d_inode->i_mode))
663                                 simple_unlink(dir, dentry);
664                         else if (S_ISDIR(dentry->d_inode->i_mode))
665                                 simple_rmdir(dir, dentry);
666                         d_delete(dentry);
667                         dput(dentry);
668                 } while (n);
669                 goto repeat;
670         }
671         mutex_unlock(&dir->i_mutex);
672 }
673
674 static int
675 rpc_populate(struct dentry *parent,
676                 struct rpc_filelist *files,
677                 int start, int eof)
678 {
679         struct inode *inode, *dir = parent->d_inode;
680         void *private = RPC_I(dir)->private;
681         struct dentry *dentry;
682         umode_t mode;
683         int i;
684
685         mutex_lock(&dir->i_mutex);
686         for (i = start; i < eof; i++) {
687                 dentry = d_alloc_name(parent, files[i].name);
688                 if (!dentry)
689                         goto out_bad;
690                 dentry->d_op = &rpc_dentry_operations;
691                 mode = files[i].mode;
692                 inode = rpc_get_inode(dir->i_sb, mode);
693                 if (!inode) {
694                         dput(dentry);
695                         goto out_bad;
696                 }
697                 inode->i_ino = i;
698                 if (files[i].i_fop)
699                         inode->i_fop = files[i].i_fop;
700                 if (private)
701                         rpc_inode_setowner(inode, private);
702                 if (S_ISDIR(mode))
703                         inc_nlink(dir);
704                 d_add(dentry, inode);
705                 fsnotify_create(dir, dentry);
706         }
707         mutex_unlock(&dir->i_mutex);
708         return 0;
709 out_bad:
710         mutex_unlock(&dir->i_mutex);
711         printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
712                         __FILE__, __func__, parent->d_name.name);
713         return -ENOMEM;
714 }
715
716 static int
717 __rpc_rmdir(struct inode *dir, struct dentry *dentry)
718 {
719         int error;
720         error = simple_rmdir(dir, dentry);
721         if (!error)
722                 d_delete(dentry);
723         return error;
724 }
725
726 static struct dentry *
727 rpc_lookup_create(struct dentry *parent, const char *name, int len, int exclusive)
728 {
729         struct inode *dir = parent->d_inode;
730         struct dentry *dentry;
731
732         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
733         dentry = lookup_one_len(name, parent, len);
734         if (IS_ERR(dentry))
735                 goto out_err;
736         if (!dentry->d_inode)
737                 dentry->d_op = &rpc_dentry_operations;
738         else if (exclusive) {
739                 dput(dentry);
740                 dentry = ERR_PTR(-EEXIST);
741                 goto out_err;
742         }
743         return dentry;
744 out_err:
745         mutex_unlock(&dir->i_mutex);
746         return dentry;
747 }
748
749 static struct dentry *
750 rpc_lookup_negative(char *path, struct nameidata *nd)
751 {
752         struct dentry *dentry;
753         int error;
754
755         if ((error = rpc_lookup_parent(path, nd)) != 0)
756                 return ERR_PTR(error);
757         dentry = rpc_lookup_create(nd->path.dentry, nd->last.name, nd->last.len,
758                                    1);
759         if (IS_ERR(dentry))
760                 rpc_release_path(nd);
761         return dentry;
762 }
763
764 /**
765  * rpc_mkdir - Create a new directory in rpc_pipefs
766  * @path: path from the rpc_pipefs root to the new directory
767  * @rpc_client: rpc client to associate with this directory
768  *
769  * This creates a directory at the given @path associated with
770  * @rpc_clnt, which will contain a file named "info" with some basic
771  * information about the client, together with any "pipes" that may
772  * later be created using rpc_mkpipe().
773  */
774 struct dentry *
775 rpc_mkdir(char *path, struct rpc_clnt *rpc_client)
776 {
777         struct nameidata nd;
778         struct dentry *dentry;
779         struct inode *dir;
780         int error;
781
782         dentry = rpc_lookup_negative(path, &nd);
783         if (IS_ERR(dentry))
784                 return dentry;
785         dir = nd.path.dentry->d_inode;
786         error = __rpc_mkdir(dir, dentry, S_IRUGO | S_IXUGO, NULL, rpc_client);
787         if (error != 0)
788                 goto out_err;
789         error = rpc_populate(dentry, authfiles,
790                         RPCAUTH_info, RPCAUTH_EOF);
791         if (error)
792                 goto err_depopulate;
793         dget(dentry);
794 out:
795         mutex_unlock(&dir->i_mutex);
796         rpc_release_path(&nd);
797         return dentry;
798 err_depopulate:
799         rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
800         __rpc_rmdir(dir, dentry);
801 out_err:
802         printk(KERN_WARNING "%s: %s() failed to create directory %s (errno = %d)\n",
803                         __FILE__, __func__, path, error);
804         dentry = ERR_PTR(error);
805         goto out;
806 }
807
808 /**
809  * rpc_rmdir - Remove a directory created with rpc_mkdir()
810  * @dentry: directory to remove
811  */
812 int
813 rpc_rmdir(struct dentry *dentry)
814 {
815         struct dentry *parent;
816         struct inode *dir;
817         int error;
818
819         parent = dget_parent(dentry);
820         dir = parent->d_inode;
821         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
822         rpc_depopulate(dentry, RPCAUTH_info, RPCAUTH_EOF);
823         error = __rpc_rmdir(dir, dentry);
824         dput(dentry);
825         mutex_unlock(&dir->i_mutex);
826         dput(parent);
827         return error;
828 }
829
830 /**
831  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
832  * @parent: dentry of directory to create new "pipe" in
833  * @name: name of pipe
834  * @private: private data to associate with the pipe, for the caller's use
835  * @ops: operations defining the behavior of the pipe: upcall, downcall,
836  *      release_pipe, open_pipe, and destroy_msg.
837  * @flags: rpc_inode flags
838  *
839  * Data is made available for userspace to read by calls to
840  * rpc_queue_upcall().  The actual reads will result in calls to
841  * @ops->upcall, which will be called with the file pointer,
842  * message, and userspace buffer to copy to.
843  *
844  * Writes can come at any time, and do not necessarily have to be
845  * responses to upcalls.  They will result in calls to @msg->downcall.
846  *
847  * The @private argument passed here will be available to all these methods
848  * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
849  */
850 struct dentry *rpc_mkpipe(struct dentry *parent, const char *name,
851                           void *private, const struct rpc_pipe_ops *ops,
852                           int flags)
853 {
854         struct dentry *dentry;
855         struct inode *dir = parent->d_inode;
856         umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
857         int err;
858
859         if (ops->upcall == NULL)
860                 umode &= ~S_IRUGO;
861         if (ops->downcall == NULL)
862                 umode &= ~S_IWUGO;
863
864         dentry = rpc_lookup_create(parent, name, strlen(name), 0);
865         if (IS_ERR(dentry))
866                 return dentry;
867         dir = parent->d_inode;
868         if (dentry->d_inode) {
869                 struct rpc_inode *rpci = RPC_I(dentry->d_inode);
870                 if (rpci->private != private ||
871                                 rpci->ops != ops ||
872                                 rpci->flags != flags) {
873                         dput (dentry);
874                         err = -EBUSY;
875                         goto out_err;
876                 }
877                 rpci->nkern_readwriters++;
878                 goto out;
879         }
880
881         err = __rpc_mkpipe(dir, dentry, umode, &rpc_pipe_fops,
882                            private, ops, flags);
883         if (err)
884                 goto out_err;
885         dget(dentry);
886 out:
887         mutex_unlock(&dir->i_mutex);
888         return dentry;
889 out_err:
890         dentry = ERR_PTR(err);
891         printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
892                         __FILE__, __func__, parent->d_name.name, name,
893                         err);
894         goto out;
895 }
896 EXPORT_SYMBOL_GPL(rpc_mkpipe);
897
898 /**
899  * rpc_unlink - remove a pipe
900  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
901  *
902  * After this call, lookups will no longer find the pipe, and any
903  * attempts to read or write using preexisting opens of the pipe will
904  * return -EPIPE.
905  */
906 int
907 rpc_unlink(struct dentry *dentry)
908 {
909         struct dentry *parent;
910         struct inode *dir;
911         int error = 0;
912
913         parent = dget_parent(dentry);
914         dir = parent->d_inode;
915         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
916         error = __rpc_rmpipe(dir, dentry);
917         dput(dentry);
918         mutex_unlock(&dir->i_mutex);
919         dput(parent);
920         return error;
921 }
922 EXPORT_SYMBOL_GPL(rpc_unlink);
923
924 /*
925  * populate the filesystem
926  */
927 static struct super_operations s_ops = {
928         .alloc_inode    = rpc_alloc_inode,
929         .destroy_inode  = rpc_destroy_inode,
930         .statfs         = simple_statfs,
931 };
932
933 #define RPCAUTH_GSSMAGIC 0x67596969
934
935 static int
936 rpc_fill_super(struct super_block *sb, void *data, int silent)
937 {
938         struct inode *inode;
939         struct dentry *root;
940
941         sb->s_blocksize = PAGE_CACHE_SIZE;
942         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
943         sb->s_magic = RPCAUTH_GSSMAGIC;
944         sb->s_op = &s_ops;
945         sb->s_time_gran = 1;
946
947         inode = rpc_get_inode(sb, S_IFDIR | 0755);
948         if (!inode)
949                 return -ENOMEM;
950         root = d_alloc_root(inode);
951         if (!root) {
952                 iput(inode);
953                 return -ENOMEM;
954         }
955         if (rpc_populate(root, files, RPCAUTH_Root + 1, RPCAUTH_RootEOF))
956                 goto out;
957         sb->s_root = root;
958         return 0;
959 out:
960         d_genocide(root);
961         dput(root);
962         return -ENOMEM;
963 }
964
965 static int
966 rpc_get_sb(struct file_system_type *fs_type,
967                 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
968 {
969         return get_sb_single(fs_type, flags, data, rpc_fill_super, mnt);
970 }
971
972 static struct file_system_type rpc_pipe_fs_type = {
973         .owner          = THIS_MODULE,
974         .name           = "rpc_pipefs",
975         .get_sb         = rpc_get_sb,
976         .kill_sb        = kill_litter_super,
977 };
978
979 static void
980 init_once(void *foo)
981 {
982         struct rpc_inode *rpci = (struct rpc_inode *) foo;
983
984         inode_init_once(&rpci->vfs_inode);
985         rpci->private = NULL;
986         rpci->nreaders = 0;
987         rpci->nwriters = 0;
988         INIT_LIST_HEAD(&rpci->in_upcall);
989         INIT_LIST_HEAD(&rpci->in_downcall);
990         INIT_LIST_HEAD(&rpci->pipe);
991         rpci->pipelen = 0;
992         init_waitqueue_head(&rpci->waitq);
993         INIT_DELAYED_WORK(&rpci->queue_timeout,
994                             rpc_timeout_upcall_queue);
995         rpci->ops = NULL;
996 }
997
998 int register_rpc_pipefs(void)
999 {
1000         int err;
1001
1002         rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1003                                 sizeof(struct rpc_inode),
1004                                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1005                                                 SLAB_MEM_SPREAD),
1006                                 init_once);
1007         if (!rpc_inode_cachep)
1008                 return -ENOMEM;
1009         err = register_filesystem(&rpc_pipe_fs_type);
1010         if (err) {
1011                 kmem_cache_destroy(rpc_inode_cachep);
1012                 return err;
1013         }
1014
1015         return 0;
1016 }
1017
1018 void unregister_rpc_pipefs(void)
1019 {
1020         kmem_cache_destroy(rpc_inode_cachep);
1021         unregister_filesystem(&rpc_pipe_fs_type);
1022 }