SUNRPC: move waitq from RPC pipe to RPC inode
[linux-3.10.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/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/poll.h>
22 #include <linux/wait.h>
23 #include <linux/seq_file.h>
24
25 #include <linux/sunrpc/clnt.h>
26 #include <linux/workqueue.h>
27 #include <linux/sunrpc/rpc_pipe_fs.h>
28 #include <linux/sunrpc/cache.h>
29 #include <linux/nsproxy.h>
30 #include <linux/notifier.h>
31
32 #include "netns.h"
33 #include "sunrpc.h"
34
35 #define RPCDBG_FACILITY RPCDBG_DEBUG
36
37 #define NET_NAME(net)   ((net == &init_net) ? " (init_net)" : "")
38
39 static struct file_system_type rpc_pipe_fs_type;
40
41
42 static struct kmem_cache *rpc_inode_cachep __read_mostly;
43
44 #define RPC_UPCALL_TIMEOUT (30*HZ)
45
46 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
47
48 int rpc_pipefs_notifier_register(struct notifier_block *nb)
49 {
50         return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
51 }
52 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
53
54 void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
55 {
56         blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
57 }
58 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
59
60 static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
61                 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
62 {
63         struct rpc_pipe_msg *msg;
64
65         if (list_empty(head))
66                 return;
67         do {
68                 msg = list_entry(head->next, struct rpc_pipe_msg, list);
69                 list_del_init(&msg->list);
70                 msg->errno = err;
71                 destroy_msg(msg);
72         } while (!list_empty(head));
73         wake_up(waitq);
74 }
75
76 static void
77 rpc_timeout_upcall_queue(struct work_struct *work)
78 {
79         LIST_HEAD(free_list);
80         struct rpc_pipe *pipe =
81                 container_of(work, struct rpc_pipe, queue_timeout.work);
82         void (*destroy_msg)(struct rpc_pipe_msg *);
83         struct dentry *dentry;
84
85         spin_lock(&pipe->lock);
86         destroy_msg = pipe->ops->destroy_msg;
87         if (pipe->nreaders == 0) {
88                 list_splice_init(&pipe->pipe, &free_list);
89                 pipe->pipelen = 0;
90         }
91         dentry = dget(pipe->dentry);
92         spin_unlock(&pipe->lock);
93         if (dentry) {
94                 rpc_purge_list(&RPC_I(dentry->d_inode)->waitq,
95                                &free_list, destroy_msg, -ETIMEDOUT);
96                 dput(dentry);
97         }
98 }
99
100 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
101                                 char __user *dst, size_t buflen)
102 {
103         char *data = (char *)msg->data + msg->copied;
104         size_t mlen = min(msg->len - msg->copied, buflen);
105         unsigned long left;
106
107         left = copy_to_user(dst, data, mlen);
108         if (left == mlen) {
109                 msg->errno = -EFAULT;
110                 return -EFAULT;
111         }
112
113         mlen -= left;
114         msg->copied += mlen;
115         msg->errno = 0;
116         return mlen;
117 }
118 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
119
120 /**
121  * rpc_queue_upcall - queue an upcall message to userspace
122  * @inode: inode of upcall pipe on which to queue given message
123  * @msg: message to queue
124  *
125  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
126  * A userspace process may then later read the upcall by performing a
127  * read on an open file for this inode.  It is up to the caller to
128  * initialize the fields of @msg (other than @msg->list) appropriately.
129  */
130 int
131 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
132 {
133         int res = -EPIPE;
134         struct dentry *dentry;
135
136         spin_lock(&pipe->lock);
137         if (pipe->nreaders) {
138                 list_add_tail(&msg->list, &pipe->pipe);
139                 pipe->pipelen += msg->len;
140                 res = 0;
141         } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
142                 if (list_empty(&pipe->pipe))
143                         queue_delayed_work(rpciod_workqueue,
144                                         &pipe->queue_timeout,
145                                         RPC_UPCALL_TIMEOUT);
146                 list_add_tail(&msg->list, &pipe->pipe);
147                 pipe->pipelen += msg->len;
148                 res = 0;
149         }
150         dentry = dget(pipe->dentry);
151         spin_unlock(&pipe->lock);
152         if (dentry) {
153                 wake_up(&RPC_I(dentry->d_inode)->waitq);
154                 dput(dentry);
155         }
156         return res;
157 }
158 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
159
160 static inline void
161 rpc_inode_setowner(struct inode *inode, void *private)
162 {
163         RPC_I(inode)->private = private;
164 }
165
166 static void
167 rpc_close_pipes(struct inode *inode)
168 {
169         struct rpc_pipe *pipe = RPC_I(inode)->pipe;
170         int need_release;
171         LIST_HEAD(free_list);
172
173         mutex_lock(&inode->i_mutex);
174         spin_lock(&pipe->lock);
175         need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
176         pipe->nreaders = 0;
177         list_splice_init(&pipe->in_upcall, &free_list);
178         list_splice_init(&pipe->pipe, &free_list);
179         pipe->pipelen = 0;
180         pipe->dentry = NULL;
181         spin_unlock(&pipe->lock);
182         rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
183         pipe->nwriters = 0;
184         if (need_release && pipe->ops->release_pipe)
185                 pipe->ops->release_pipe(inode);
186         cancel_delayed_work_sync(&pipe->queue_timeout);
187         rpc_inode_setowner(inode, NULL);
188         RPC_I(inode)->pipe = NULL;
189         mutex_unlock(&inode->i_mutex);
190 }
191
192 static struct inode *
193 rpc_alloc_inode(struct super_block *sb)
194 {
195         struct rpc_inode *rpci;
196         rpci = (struct rpc_inode *)kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
197         if (!rpci)
198                 return NULL;
199         return &rpci->vfs_inode;
200 }
201
202 static void
203 rpc_i_callback(struct rcu_head *head)
204 {
205         struct inode *inode = container_of(head, struct inode, i_rcu);
206         kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
207 }
208
209 static void
210 rpc_destroy_inode(struct inode *inode)
211 {
212         call_rcu(&inode->i_rcu, rpc_i_callback);
213 }
214
215 static int
216 rpc_pipe_open(struct inode *inode, struct file *filp)
217 {
218         struct rpc_pipe *pipe;
219         int first_open;
220         int res = -ENXIO;
221
222         mutex_lock(&inode->i_mutex);
223         pipe = RPC_I(inode)->pipe;
224         if (pipe == NULL)
225                 goto out;
226         first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
227         if (first_open && pipe->ops->open_pipe) {
228                 res = pipe->ops->open_pipe(inode);
229                 if (res)
230                         goto out;
231         }
232         if (filp->f_mode & FMODE_READ)
233                 pipe->nreaders++;
234         if (filp->f_mode & FMODE_WRITE)
235                 pipe->nwriters++;
236         res = 0;
237 out:
238         mutex_unlock(&inode->i_mutex);
239         return res;
240 }
241
242 static int
243 rpc_pipe_release(struct inode *inode, struct file *filp)
244 {
245         struct rpc_pipe *pipe;
246         struct rpc_pipe_msg *msg;
247         int last_close;
248
249         mutex_lock(&inode->i_mutex);
250         pipe = RPC_I(inode)->pipe;
251         if (pipe == NULL)
252                 goto out;
253         msg = filp->private_data;
254         if (msg != NULL) {
255                 spin_lock(&pipe->lock);
256                 msg->errno = -EAGAIN;
257                 list_del_init(&msg->list);
258                 spin_unlock(&pipe->lock);
259                 pipe->ops->destroy_msg(msg);
260         }
261         if (filp->f_mode & FMODE_WRITE)
262                 pipe->nwriters --;
263         if (filp->f_mode & FMODE_READ) {
264                 pipe->nreaders --;
265                 if (pipe->nreaders == 0) {
266                         LIST_HEAD(free_list);
267                         spin_lock(&pipe->lock);
268                         list_splice_init(&pipe->pipe, &free_list);
269                         pipe->pipelen = 0;
270                         spin_unlock(&pipe->lock);
271                         rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
272                                         pipe->ops->destroy_msg, -EAGAIN);
273                 }
274         }
275         last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
276         if (last_close && pipe->ops->release_pipe)
277                 pipe->ops->release_pipe(inode);
278 out:
279         mutex_unlock(&inode->i_mutex);
280         return 0;
281 }
282
283 static ssize_t
284 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
285 {
286         struct inode *inode = filp->f_path.dentry->d_inode;
287         struct rpc_pipe *pipe;
288         struct rpc_pipe_msg *msg;
289         int res = 0;
290
291         mutex_lock(&inode->i_mutex);
292         pipe = RPC_I(inode)->pipe;
293         if (pipe == NULL) {
294                 res = -EPIPE;
295                 goto out_unlock;
296         }
297         msg = filp->private_data;
298         if (msg == NULL) {
299                 spin_lock(&pipe->lock);
300                 if (!list_empty(&pipe->pipe)) {
301                         msg = list_entry(pipe->pipe.next,
302                                         struct rpc_pipe_msg,
303                                         list);
304                         list_move(&msg->list, &pipe->in_upcall);
305                         pipe->pipelen -= msg->len;
306                         filp->private_data = msg;
307                         msg->copied = 0;
308                 }
309                 spin_unlock(&pipe->lock);
310                 if (msg == NULL)
311                         goto out_unlock;
312         }
313         /* NOTE: it is up to the callback to update msg->copied */
314         res = pipe->ops->upcall(filp, msg, buf, len);
315         if (res < 0 || msg->len == msg->copied) {
316                 filp->private_data = NULL;
317                 spin_lock(&pipe->lock);
318                 list_del_init(&msg->list);
319                 spin_unlock(&pipe->lock);
320                 pipe->ops->destroy_msg(msg);
321         }
322 out_unlock:
323         mutex_unlock(&inode->i_mutex);
324         return res;
325 }
326
327 static ssize_t
328 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
329 {
330         struct inode *inode = filp->f_path.dentry->d_inode;
331         int res;
332
333         mutex_lock(&inode->i_mutex);
334         res = -EPIPE;
335         if (RPC_I(inode)->pipe != NULL)
336                 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
337         mutex_unlock(&inode->i_mutex);
338         return res;
339 }
340
341 static unsigned int
342 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
343 {
344         struct inode *inode = filp->f_path.dentry->d_inode;
345         struct rpc_inode *rpci = RPC_I(inode);
346         unsigned int mask = POLLOUT | POLLWRNORM;
347
348         poll_wait(filp, &rpci->waitq, wait);
349
350         mutex_lock(&inode->i_mutex);
351         if (rpci->pipe == NULL)
352                 mask |= POLLERR | POLLHUP;
353         else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
354                 mask |= POLLIN | POLLRDNORM;
355         mutex_unlock(&inode->i_mutex);
356         return mask;
357 }
358
359 static long
360 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
361 {
362         struct inode *inode = filp->f_path.dentry->d_inode;
363         struct rpc_pipe *pipe;
364         int len;
365
366         switch (cmd) {
367         case FIONREAD:
368                 mutex_lock(&inode->i_mutex);
369                 pipe = RPC_I(inode)->pipe;
370                 if (pipe == NULL) {
371                         mutex_unlock(&inode->i_mutex);
372                         return -EPIPE;
373                 }
374                 spin_lock(&pipe->lock);
375                 len = pipe->pipelen;
376                 if (filp->private_data) {
377                         struct rpc_pipe_msg *msg;
378                         msg = filp->private_data;
379                         len += msg->len - msg->copied;
380                 }
381                 spin_unlock(&pipe->lock);
382                 mutex_unlock(&inode->i_mutex);
383                 return put_user(len, (int __user *)arg);
384         default:
385                 return -EINVAL;
386         }
387 }
388
389 static const struct file_operations rpc_pipe_fops = {
390         .owner          = THIS_MODULE,
391         .llseek         = no_llseek,
392         .read           = rpc_pipe_read,
393         .write          = rpc_pipe_write,
394         .poll           = rpc_pipe_poll,
395         .unlocked_ioctl = rpc_pipe_ioctl,
396         .open           = rpc_pipe_open,
397         .release        = rpc_pipe_release,
398 };
399
400 static int
401 rpc_show_info(struct seq_file *m, void *v)
402 {
403         struct rpc_clnt *clnt = m->private;
404
405         seq_printf(m, "RPC server: %s\n", clnt->cl_server);
406         seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_protname,
407                         clnt->cl_prog, clnt->cl_vers);
408         seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
409         seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
410         seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
411         return 0;
412 }
413
414 static int
415 rpc_info_open(struct inode *inode, struct file *file)
416 {
417         struct rpc_clnt *clnt = NULL;
418         int ret = single_open(file, rpc_show_info, NULL);
419
420         if (!ret) {
421                 struct seq_file *m = file->private_data;
422
423                 spin_lock(&file->f_path.dentry->d_lock);
424                 if (!d_unhashed(file->f_path.dentry))
425                         clnt = RPC_I(inode)->private;
426                 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
427                         spin_unlock(&file->f_path.dentry->d_lock);
428                         m->private = clnt;
429                 } else {
430                         spin_unlock(&file->f_path.dentry->d_lock);
431                         single_release(inode, file);
432                         ret = -EINVAL;
433                 }
434         }
435         return ret;
436 }
437
438 static int
439 rpc_info_release(struct inode *inode, struct file *file)
440 {
441         struct seq_file *m = file->private_data;
442         struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
443
444         if (clnt)
445                 rpc_release_client(clnt);
446         return single_release(inode, file);
447 }
448
449 static const struct file_operations rpc_info_operations = {
450         .owner          = THIS_MODULE,
451         .open           = rpc_info_open,
452         .read           = seq_read,
453         .llseek         = seq_lseek,
454         .release        = rpc_info_release,
455 };
456
457
458 /*
459  * Description of fs contents.
460  */
461 struct rpc_filelist {
462         const char *name;
463         const struct file_operations *i_fop;
464         umode_t mode;
465 };
466
467 static int rpc_delete_dentry(const struct dentry *dentry)
468 {
469         return 1;
470 }
471
472 static const struct dentry_operations rpc_dentry_operations = {
473         .d_delete = rpc_delete_dentry,
474 };
475
476 static struct inode *
477 rpc_get_inode(struct super_block *sb, umode_t mode)
478 {
479         struct inode *inode = new_inode(sb);
480         if (!inode)
481                 return NULL;
482         inode->i_ino = get_next_ino();
483         inode->i_mode = mode;
484         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
485         switch (mode & S_IFMT) {
486         case S_IFDIR:
487                 inode->i_fop = &simple_dir_operations;
488                 inode->i_op = &simple_dir_inode_operations;
489                 inc_nlink(inode);
490         default:
491                 break;
492         }
493         return inode;
494 }
495
496 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
497                                umode_t mode,
498                                const struct file_operations *i_fop,
499                                void *private)
500 {
501         struct inode *inode;
502
503         d_drop(dentry);
504         inode = rpc_get_inode(dir->i_sb, mode);
505         if (!inode)
506                 goto out_err;
507         inode->i_ino = iunique(dir->i_sb, 100);
508         if (i_fop)
509                 inode->i_fop = i_fop;
510         if (private)
511                 rpc_inode_setowner(inode, private);
512         d_add(dentry, inode);
513         return 0;
514 out_err:
515         printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n",
516                         __FILE__, __func__, dentry->d_name.name);
517         dput(dentry);
518         return -ENOMEM;
519 }
520
521 static int __rpc_create(struct inode *dir, struct dentry *dentry,
522                         umode_t mode,
523                         const struct file_operations *i_fop,
524                         void *private)
525 {
526         int err;
527
528         err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
529         if (err)
530                 return err;
531         fsnotify_create(dir, dentry);
532         return 0;
533 }
534
535 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
536                        umode_t mode,
537                        const struct file_operations *i_fop,
538                        void *private)
539 {
540         int err;
541
542         err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
543         if (err)
544                 return err;
545         inc_nlink(dir);
546         fsnotify_mkdir(dir, dentry);
547         return 0;
548 }
549
550 static void
551 init_pipe(struct rpc_pipe *pipe)
552 {
553         pipe->nreaders = 0;
554         pipe->nwriters = 0;
555         INIT_LIST_HEAD(&pipe->in_upcall);
556         INIT_LIST_HEAD(&pipe->in_downcall);
557         INIT_LIST_HEAD(&pipe->pipe);
558         pipe->pipelen = 0;
559         INIT_DELAYED_WORK(&pipe->queue_timeout,
560                             rpc_timeout_upcall_queue);
561         pipe->ops = NULL;
562         spin_lock_init(&pipe->lock);
563         pipe->dentry = NULL;
564 }
565
566 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
567 {
568         kfree(pipe);
569 }
570 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
571
572 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
573 {
574         struct rpc_pipe *pipe;
575
576         pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
577         if (!pipe)
578                 return ERR_PTR(-ENOMEM);
579         init_pipe(pipe);
580         pipe->ops = ops;
581         pipe->flags = flags;
582         return pipe;
583 }
584 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
585
586 static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
587                                umode_t mode,
588                                const struct file_operations *i_fop,
589                                void *private,
590                                struct rpc_pipe *pipe)
591 {
592         struct rpc_inode *rpci;
593         int err;
594
595         err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
596         if (err)
597                 return err;
598         rpci = RPC_I(dentry->d_inode);
599         rpci->private = private;
600         rpci->pipe = pipe;
601         fsnotify_create(dir, dentry);
602         return 0;
603 }
604
605 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
606 {
607         int ret;
608
609         dget(dentry);
610         ret = simple_rmdir(dir, dentry);
611         d_delete(dentry);
612         dput(dentry);
613         return ret;
614 }
615
616 int rpc_rmdir(struct dentry *dentry)
617 {
618         struct dentry *parent;
619         struct inode *dir;
620         int error;
621
622         parent = dget_parent(dentry);
623         dir = parent->d_inode;
624         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
625         error = __rpc_rmdir(dir, dentry);
626         mutex_unlock(&dir->i_mutex);
627         dput(parent);
628         return error;
629 }
630 EXPORT_SYMBOL_GPL(rpc_rmdir);
631
632 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
633 {
634         int ret;
635
636         dget(dentry);
637         ret = simple_unlink(dir, dentry);
638         d_delete(dentry);
639         dput(dentry);
640         return ret;
641 }
642
643 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
644 {
645         struct inode *inode = dentry->d_inode;
646
647         rpc_close_pipes(inode);
648         return __rpc_unlink(dir, dentry);
649 }
650
651 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
652                                           struct qstr *name)
653 {
654         struct dentry *dentry;
655
656         dentry = d_lookup(parent, name);
657         if (!dentry) {
658                 dentry = d_alloc(parent, name);
659                 if (!dentry)
660                         return ERR_PTR(-ENOMEM);
661         }
662         if (dentry->d_inode == NULL) {
663                 d_set_d_op(dentry, &rpc_dentry_operations);
664                 return dentry;
665         }
666         dput(dentry);
667         return ERR_PTR(-EEXIST);
668 }
669
670 /*
671  * FIXME: This probably has races.
672  */
673 static void __rpc_depopulate(struct dentry *parent,
674                              const struct rpc_filelist *files,
675                              int start, int eof)
676 {
677         struct inode *dir = parent->d_inode;
678         struct dentry *dentry;
679         struct qstr name;
680         int i;
681
682         for (i = start; i < eof; i++) {
683                 name.name = files[i].name;
684                 name.len = strlen(files[i].name);
685                 name.hash = full_name_hash(name.name, name.len);
686                 dentry = d_lookup(parent, &name);
687
688                 if (dentry == NULL)
689                         continue;
690                 if (dentry->d_inode == NULL)
691                         goto next;
692                 switch (dentry->d_inode->i_mode & S_IFMT) {
693                         default:
694                                 BUG();
695                         case S_IFREG:
696                                 __rpc_unlink(dir, dentry);
697                                 break;
698                         case S_IFDIR:
699                                 __rpc_rmdir(dir, dentry);
700                 }
701 next:
702                 dput(dentry);
703         }
704 }
705
706 static void rpc_depopulate(struct dentry *parent,
707                            const struct rpc_filelist *files,
708                            int start, int eof)
709 {
710         struct inode *dir = parent->d_inode;
711
712         mutex_lock_nested(&dir->i_mutex, I_MUTEX_CHILD);
713         __rpc_depopulate(parent, files, start, eof);
714         mutex_unlock(&dir->i_mutex);
715 }
716
717 static int rpc_populate(struct dentry *parent,
718                         const struct rpc_filelist *files,
719                         int start, int eof,
720                         void *private)
721 {
722         struct inode *dir = parent->d_inode;
723         struct dentry *dentry;
724         int i, err;
725
726         mutex_lock(&dir->i_mutex);
727         for (i = start; i < eof; i++) {
728                 struct qstr q;
729
730                 q.name = files[i].name;
731                 q.len = strlen(files[i].name);
732                 q.hash = full_name_hash(q.name, q.len);
733                 dentry = __rpc_lookup_create_exclusive(parent, &q);
734                 err = PTR_ERR(dentry);
735                 if (IS_ERR(dentry))
736                         goto out_bad;
737                 switch (files[i].mode & S_IFMT) {
738                         default:
739                                 BUG();
740                         case S_IFREG:
741                                 err = __rpc_create(dir, dentry,
742                                                 files[i].mode,
743                                                 files[i].i_fop,
744                                                 private);
745                                 break;
746                         case S_IFDIR:
747                                 err = __rpc_mkdir(dir, dentry,
748                                                 files[i].mode,
749                                                 NULL,
750                                                 private);
751                 }
752                 if (err != 0)
753                         goto out_bad;
754         }
755         mutex_unlock(&dir->i_mutex);
756         return 0;
757 out_bad:
758         __rpc_depopulate(parent, files, start, eof);
759         mutex_unlock(&dir->i_mutex);
760         printk(KERN_WARNING "%s: %s failed to populate directory %s\n",
761                         __FILE__, __func__, parent->d_name.name);
762         return err;
763 }
764
765 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
766                 struct qstr *name, umode_t mode, void *private,
767                 int (*populate)(struct dentry *, void *), void *args_populate)
768 {
769         struct dentry *dentry;
770         struct inode *dir = parent->d_inode;
771         int error;
772
773         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
774         dentry = __rpc_lookup_create_exclusive(parent, name);
775         if (IS_ERR(dentry))
776                 goto out;
777         error = __rpc_mkdir(dir, dentry, mode, NULL, private);
778         if (error != 0)
779                 goto out_err;
780         if (populate != NULL) {
781                 error = populate(dentry, args_populate);
782                 if (error)
783                         goto err_rmdir;
784         }
785 out:
786         mutex_unlock(&dir->i_mutex);
787         return dentry;
788 err_rmdir:
789         __rpc_rmdir(dir, dentry);
790 out_err:
791         dentry = ERR_PTR(error);
792         goto out;
793 }
794
795 static int rpc_rmdir_depopulate(struct dentry *dentry,
796                 void (*depopulate)(struct dentry *))
797 {
798         struct dentry *parent;
799         struct inode *dir;
800         int error;
801
802         parent = dget_parent(dentry);
803         dir = parent->d_inode;
804         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
805         if (depopulate != NULL)
806                 depopulate(dentry);
807         error = __rpc_rmdir(dir, dentry);
808         mutex_unlock(&dir->i_mutex);
809         dput(parent);
810         return error;
811 }
812
813 /**
814  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
815  * @parent: dentry of directory to create new "pipe" in
816  * @name: name of pipe
817  * @private: private data to associate with the pipe, for the caller's use
818  * @ops: operations defining the behavior of the pipe: upcall, downcall,
819  *      release_pipe, open_pipe, and destroy_msg.
820  * @flags: rpc_pipe flags
821  *
822  * Data is made available for userspace to read by calls to
823  * rpc_queue_upcall().  The actual reads will result in calls to
824  * @ops->upcall, which will be called with the file pointer,
825  * message, and userspace buffer to copy to.
826  *
827  * Writes can come at any time, and do not necessarily have to be
828  * responses to upcalls.  They will result in calls to @msg->downcall.
829  *
830  * The @private argument passed here will be available to all these methods
831  * from the file pointer, via RPC_I(file->f_dentry->d_inode)->private.
832  */
833 struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
834                                  void *private, struct rpc_pipe *pipe)
835 {
836         struct dentry *dentry;
837         struct inode *dir = parent->d_inode;
838         umode_t umode = S_IFIFO | S_IRUSR | S_IWUSR;
839         struct qstr q;
840         int err;
841
842         if (pipe->ops->upcall == NULL)
843                 umode &= ~S_IRUGO;
844         if (pipe->ops->downcall == NULL)
845                 umode &= ~S_IWUGO;
846
847         q.name = name;
848         q.len = strlen(name);
849         q.hash = full_name_hash(q.name, q.len),
850
851         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
852         dentry = __rpc_lookup_create_exclusive(parent, &q);
853         if (IS_ERR(dentry))
854                 goto out;
855         err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
856                                   private, pipe);
857         if (err)
858                 goto out_err;
859 out:
860         mutex_unlock(&dir->i_mutex);
861         return dentry;
862 out_err:
863         dentry = ERR_PTR(err);
864         printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n",
865                         __FILE__, __func__, parent->d_name.name, name,
866                         err);
867         goto out;
868 }
869 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
870
871 /**
872  * rpc_unlink - remove a pipe
873  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
874  *
875  * After this call, lookups will no longer find the pipe, and any
876  * attempts to read or write using preexisting opens of the pipe will
877  * return -EPIPE.
878  */
879 int
880 rpc_unlink(struct dentry *dentry)
881 {
882         struct dentry *parent;
883         struct inode *dir;
884         int error = 0;
885
886         parent = dget_parent(dentry);
887         dir = parent->d_inode;
888         mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
889         error = __rpc_rmpipe(dir, dentry);
890         mutex_unlock(&dir->i_mutex);
891         dput(parent);
892         return error;
893 }
894 EXPORT_SYMBOL_GPL(rpc_unlink);
895
896 enum {
897         RPCAUTH_info,
898         RPCAUTH_EOF
899 };
900
901 static const struct rpc_filelist authfiles[] = {
902         [RPCAUTH_info] = {
903                 .name = "info",
904                 .i_fop = &rpc_info_operations,
905                 .mode = S_IFREG | S_IRUSR,
906         },
907 };
908
909 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
910 {
911         return rpc_populate(dentry,
912                             authfiles, RPCAUTH_info, RPCAUTH_EOF,
913                             private);
914 }
915
916 static void rpc_clntdir_depopulate(struct dentry *dentry)
917 {
918         rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
919 }
920
921 /**
922  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
923  * @dentry: dentry from the rpc_pipefs root to the new directory
924  * @name: &struct qstr for the name
925  * @rpc_client: rpc client to associate with this directory
926  *
927  * This creates a directory at the given @path associated with
928  * @rpc_clnt, which will contain a file named "info" with some basic
929  * information about the client, together with any "pipes" that may
930  * later be created using rpc_mkpipe().
931  */
932 struct dentry *rpc_create_client_dir(struct dentry *dentry,
933                                    struct qstr *name,
934                                    struct rpc_clnt *rpc_client)
935 {
936         return rpc_mkdir_populate(dentry, name, S_IRUGO | S_IXUGO, NULL,
937                         rpc_clntdir_populate, rpc_client);
938 }
939
940 /**
941  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
942  * @clnt: rpc client
943  */
944 int rpc_remove_client_dir(struct dentry *dentry)
945 {
946         return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
947 }
948
949 static const struct rpc_filelist cache_pipefs_files[3] = {
950         [0] = {
951                 .name = "channel",
952                 .i_fop = &cache_file_operations_pipefs,
953                 .mode = S_IFREG|S_IRUSR|S_IWUSR,
954         },
955         [1] = {
956                 .name = "content",
957                 .i_fop = &content_file_operations_pipefs,
958                 .mode = S_IFREG|S_IRUSR,
959         },
960         [2] = {
961                 .name = "flush",
962                 .i_fop = &cache_flush_operations_pipefs,
963                 .mode = S_IFREG|S_IRUSR|S_IWUSR,
964         },
965 };
966
967 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
968 {
969         return rpc_populate(dentry,
970                             cache_pipefs_files, 0, 3,
971                             private);
972 }
973
974 static void rpc_cachedir_depopulate(struct dentry *dentry)
975 {
976         rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
977 }
978
979 struct dentry *rpc_create_cache_dir(struct dentry *parent, struct qstr *name,
980                                     umode_t umode, struct cache_detail *cd)
981 {
982         return rpc_mkdir_populate(parent, name, umode, NULL,
983                         rpc_cachedir_populate, cd);
984 }
985
986 void rpc_remove_cache_dir(struct dentry *dentry)
987 {
988         rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
989 }
990
991 /*
992  * populate the filesystem
993  */
994 static const struct super_operations s_ops = {
995         .alloc_inode    = rpc_alloc_inode,
996         .destroy_inode  = rpc_destroy_inode,
997         .statfs         = simple_statfs,
998 };
999
1000 #define RPCAUTH_GSSMAGIC 0x67596969
1001
1002 /*
1003  * We have a single directory with 1 node in it.
1004  */
1005 enum {
1006         RPCAUTH_lockd,
1007         RPCAUTH_mount,
1008         RPCAUTH_nfs,
1009         RPCAUTH_portmap,
1010         RPCAUTH_statd,
1011         RPCAUTH_nfsd4_cb,
1012         RPCAUTH_cache,
1013         RPCAUTH_RootEOF
1014 };
1015
1016 static const struct rpc_filelist files[] = {
1017         [RPCAUTH_lockd] = {
1018                 .name = "lockd",
1019                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1020         },
1021         [RPCAUTH_mount] = {
1022                 .name = "mount",
1023                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1024         },
1025         [RPCAUTH_nfs] = {
1026                 .name = "nfs",
1027                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1028         },
1029         [RPCAUTH_portmap] = {
1030                 .name = "portmap",
1031                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1032         },
1033         [RPCAUTH_statd] = {
1034                 .name = "statd",
1035                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1036         },
1037         [RPCAUTH_nfsd4_cb] = {
1038                 .name = "nfsd4_cb",
1039                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1040         },
1041         [RPCAUTH_cache] = {
1042                 .name = "cache",
1043                 .mode = S_IFDIR | S_IRUGO | S_IXUGO,
1044         },
1045 };
1046
1047 /*
1048  * This call can be used only in RPC pipefs mount notification hooks.
1049  */
1050 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1051                                const unsigned char *dir_name)
1052 {
1053         struct qstr dir = {
1054                 .name = dir_name,
1055                 .len = strlen(dir_name),
1056                 .hash = full_name_hash(dir_name, strlen(dir_name)),
1057         };
1058
1059         return d_lookup(sb->s_root, &dir);
1060 }
1061 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1062
1063 void rpc_pipefs_init_net(struct net *net)
1064 {
1065         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1066
1067         mutex_init(&sn->pipefs_sb_lock);
1068 }
1069
1070 /*
1071  * This call will be used for per network namespace operations calls.
1072  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1073  * found. This lock have to be released by rpc_put_sb_net() when all operations
1074  * will be completed.
1075  */
1076 struct super_block *rpc_get_sb_net(const struct net *net)
1077 {
1078         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1079
1080         mutex_lock(&sn->pipefs_sb_lock);
1081         if (sn->pipefs_sb)
1082                 return sn->pipefs_sb;
1083         mutex_unlock(&sn->pipefs_sb_lock);
1084         return NULL;
1085 }
1086 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1087
1088 void rpc_put_sb_net(const struct net *net)
1089 {
1090         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1091
1092         BUG_ON(sn->pipefs_sb == NULL);
1093         mutex_unlock(&sn->pipefs_sb_lock);
1094 }
1095 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1096
1097 static int
1098 rpc_fill_super(struct super_block *sb, void *data, int silent)
1099 {
1100         struct inode *inode;
1101         struct dentry *root;
1102         struct net *net = data;
1103         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1104         int err;
1105
1106         sb->s_blocksize = PAGE_CACHE_SIZE;
1107         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1108         sb->s_magic = RPCAUTH_GSSMAGIC;
1109         sb->s_op = &s_ops;
1110         sb->s_time_gran = 1;
1111
1112         inode = rpc_get_inode(sb, S_IFDIR | 0755);
1113         if (!inode)
1114                 return -ENOMEM;
1115         sb->s_root = root = d_alloc_root(inode);
1116         if (!root) {
1117                 iput(inode);
1118                 return -ENOMEM;
1119         }
1120         if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1121                 return -ENOMEM;
1122         dprintk("RPC:   sending pipefs MOUNT notification for net %p%s\n", net,
1123                                                                 NET_NAME(net));
1124         err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1125                                            RPC_PIPEFS_MOUNT,
1126                                            sb);
1127         if (err)
1128                 goto err_depopulate;
1129         sb->s_fs_info = get_net(net);
1130         sn->pipefs_sb = sb;
1131         return 0;
1132
1133 err_depopulate:
1134         blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1135                                            RPC_PIPEFS_UMOUNT,
1136                                            sb);
1137         __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1138         return err;
1139 }
1140
1141 static struct dentry *
1142 rpc_mount(struct file_system_type *fs_type,
1143                 int flags, const char *dev_name, void *data)
1144 {
1145         return mount_ns(fs_type, flags, current->nsproxy->net_ns, rpc_fill_super);
1146 }
1147
1148 void rpc_kill_sb(struct super_block *sb)
1149 {
1150         struct net *net = sb->s_fs_info;
1151         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1152
1153         mutex_lock(&sn->pipefs_sb_lock);
1154         sn->pipefs_sb = NULL;
1155         mutex_unlock(&sn->pipefs_sb_lock);
1156         put_net(net);
1157         dprintk("RPC:   sending pipefs UMOUNT notification for net %p%s\n", net,
1158                                                                 NET_NAME(net));
1159         blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1160                                            RPC_PIPEFS_UMOUNT,
1161                                            sb);
1162         kill_litter_super(sb);
1163 }
1164
1165 static struct file_system_type rpc_pipe_fs_type = {
1166         .owner          = THIS_MODULE,
1167         .name           = "rpc_pipefs",
1168         .mount          = rpc_mount,
1169         .kill_sb        = rpc_kill_sb,
1170 };
1171
1172 static void
1173 init_once(void *foo)
1174 {
1175         struct rpc_inode *rpci = (struct rpc_inode *) foo;
1176
1177         inode_init_once(&rpci->vfs_inode);
1178         rpci->private = NULL;
1179         rpci->pipe = NULL;
1180         init_waitqueue_head(&rpci->waitq);
1181 }
1182
1183 int register_rpc_pipefs(void)
1184 {
1185         int err;
1186
1187         rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1188                                 sizeof(struct rpc_inode),
1189                                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1190                                                 SLAB_MEM_SPREAD),
1191                                 init_once);
1192         if (!rpc_inode_cachep)
1193                 return -ENOMEM;
1194         err = rpc_clients_notifier_register();
1195         if (err)
1196                 goto err_notifier;
1197         err = register_filesystem(&rpc_pipe_fs_type);
1198         if (err)
1199                 goto err_register;
1200         return 0;
1201
1202 err_register:
1203         rpc_clients_notifier_unregister();
1204 err_notifier:
1205         kmem_cache_destroy(rpc_inode_cachep);
1206         return err;
1207 }
1208
1209 void unregister_rpc_pipefs(void)
1210 {
1211         rpc_clients_notifier_unregister();
1212         kmem_cache_destroy(rpc_inode_cachep);
1213         unregister_filesystem(&rpc_pipe_fs_type);
1214 }
1215
1216 /* Make 'mount -t rpc_pipefs ...' autoload this module. */
1217 MODULE_ALIAS("rpc_pipefs");