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