]> asedeno.scripts.mit.edu Git - linux.git/blob - net/sunrpc/rpc_pipe.c
Merge tag 'rpmsg-v5.3' of git://github.com/andersson/remoteproc
[linux.git] / net / sunrpc / rpc_pipe.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * net/sunrpc/rpc_pipe.c
4  *
5  * Userland/kernel interface for rpcauth_gss.
6  * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
7  * and fs/sysfs/inode.c
8  *
9  * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
10  *
11  */
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/string.h>
15 #include <linux/pagemap.h>
16 #include <linux/mount.h>
17 #include <linux/namei.h>
18 #include <linux/fsnotify.h>
19 #include <linux/kernel.h>
20 #include <linux/rcupdate.h>
21 #include <linux/utsname.h>
22
23 #include <asm/ioctls.h>
24 #include <linux/poll.h>
25 #include <linux/wait.h>
26 #include <linux/seq_file.h>
27
28 #include <linux/sunrpc/clnt.h>
29 #include <linux/workqueue.h>
30 #include <linux/sunrpc/rpc_pipe_fs.h>
31 #include <linux/sunrpc/cache.h>
32 #include <linux/nsproxy.h>
33 #include <linux/notifier.h>
34
35 #include "netns.h"
36 #include "sunrpc.h"
37
38 #define RPCDBG_FACILITY RPCDBG_DEBUG
39
40 #define NET_NAME(net)   ((net == &init_net) ? " (init_net)" : "")
41
42 static struct file_system_type rpc_pipe_fs_type;
43 static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
44
45 static struct kmem_cache *rpc_inode_cachep __read_mostly;
46
47 #define RPC_UPCALL_TIMEOUT (30*HZ)
48
49 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
50
51 int rpc_pipefs_notifier_register(struct notifier_block *nb)
52 {
53         return blocking_notifier_chain_cond_register(&rpc_pipefs_notifier_list, nb);
54 }
55 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
56
57 void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
58 {
59         blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
60 }
61 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
62
63 static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
64                 void (*destroy_msg)(struct rpc_pipe_msg *), int err)
65 {
66         struct rpc_pipe_msg *msg;
67
68         if (list_empty(head))
69                 return;
70         do {
71                 msg = list_entry(head->next, struct rpc_pipe_msg, list);
72                 list_del_init(&msg->list);
73                 msg->errno = err;
74                 destroy_msg(msg);
75         } while (!list_empty(head));
76
77         if (waitq)
78                 wake_up(waitq);
79 }
80
81 static void
82 rpc_timeout_upcall_queue(struct work_struct *work)
83 {
84         LIST_HEAD(free_list);
85         struct rpc_pipe *pipe =
86                 container_of(work, struct rpc_pipe, queue_timeout.work);
87         void (*destroy_msg)(struct rpc_pipe_msg *);
88         struct dentry *dentry;
89
90         spin_lock(&pipe->lock);
91         destroy_msg = pipe->ops->destroy_msg;
92         if (pipe->nreaders == 0) {
93                 list_splice_init(&pipe->pipe, &free_list);
94                 pipe->pipelen = 0;
95         }
96         dentry = dget(pipe->dentry);
97         spin_unlock(&pipe->lock);
98         rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
99                         &free_list, destroy_msg, -ETIMEDOUT);
100         dput(dentry);
101 }
102
103 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
104                                 char __user *dst, size_t buflen)
105 {
106         char *data = (char *)msg->data + msg->copied;
107         size_t mlen = min(msg->len - msg->copied, buflen);
108         unsigned long left;
109
110         left = copy_to_user(dst, data, mlen);
111         if (left == mlen) {
112                 msg->errno = -EFAULT;
113                 return -EFAULT;
114         }
115
116         mlen -= left;
117         msg->copied += mlen;
118         msg->errno = 0;
119         return mlen;
120 }
121 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
122
123 /**
124  * rpc_queue_upcall - queue an upcall message to userspace
125  * @pipe: upcall pipe on which to queue given message
126  * @msg: message to queue
127  *
128  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
129  * A userspace process may then later read the upcall by performing a
130  * read on an open file for this inode.  It is up to the caller to
131  * initialize the fields of @msg (other than @msg->list) appropriately.
132  */
133 int
134 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
135 {
136         int res = -EPIPE;
137         struct dentry *dentry;
138
139         spin_lock(&pipe->lock);
140         if (pipe->nreaders) {
141                 list_add_tail(&msg->list, &pipe->pipe);
142                 pipe->pipelen += msg->len;
143                 res = 0;
144         } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
145                 if (list_empty(&pipe->pipe))
146                         queue_delayed_work(rpciod_workqueue,
147                                         &pipe->queue_timeout,
148                                         RPC_UPCALL_TIMEOUT);
149                 list_add_tail(&msg->list, &pipe->pipe);
150                 pipe->pipelen += msg->len;
151                 res = 0;
152         }
153         dentry = dget(pipe->dentry);
154         spin_unlock(&pipe->lock);
155         if (dentry) {
156                 wake_up(&RPC_I(d_inode(dentry))->waitq);
157                 dput(dentry);
158         }
159         return res;
160 }
161 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
162
163 static inline void
164 rpc_inode_setowner(struct inode *inode, void *private)
165 {
166         RPC_I(inode)->private = private;
167 }
168
169 static void
170 rpc_close_pipes(struct inode *inode)
171 {
172         struct rpc_pipe *pipe = RPC_I(inode)->pipe;
173         int need_release;
174         LIST_HEAD(free_list);
175
176         inode_lock(inode);
177         spin_lock(&pipe->lock);
178         need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
179         pipe->nreaders = 0;
180         list_splice_init(&pipe->in_upcall, &free_list);
181         list_splice_init(&pipe->pipe, &free_list);
182         pipe->pipelen = 0;
183         pipe->dentry = NULL;
184         spin_unlock(&pipe->lock);
185         rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
186         pipe->nwriters = 0;
187         if (need_release && pipe->ops->release_pipe)
188                 pipe->ops->release_pipe(inode);
189         cancel_delayed_work_sync(&pipe->queue_timeout);
190         rpc_inode_setowner(inode, NULL);
191         RPC_I(inode)->pipe = NULL;
192         inode_unlock(inode);
193 }
194
195 static struct inode *
196 rpc_alloc_inode(struct super_block *sb)
197 {
198         struct rpc_inode *rpci;
199         rpci = kmem_cache_alloc(rpc_inode_cachep, GFP_KERNEL);
200         if (!rpci)
201                 return NULL;
202         return &rpci->vfs_inode;
203 }
204
205 static void
206 rpc_free_inode(struct inode *inode)
207 {
208         kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
209 }
210
211 static int
212 rpc_pipe_open(struct inode *inode, struct file *filp)
213 {
214         struct rpc_pipe *pipe;
215         int first_open;
216         int res = -ENXIO;
217
218         inode_lock(inode);
219         pipe = RPC_I(inode)->pipe;
220         if (pipe == NULL)
221                 goto out;
222         first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
223         if (first_open && pipe->ops->open_pipe) {
224                 res = pipe->ops->open_pipe(inode);
225                 if (res)
226                         goto out;
227         }
228         if (filp->f_mode & FMODE_READ)
229                 pipe->nreaders++;
230         if (filp->f_mode & FMODE_WRITE)
231                 pipe->nwriters++;
232         res = 0;
233 out:
234         inode_unlock(inode);
235         return res;
236 }
237
238 static int
239 rpc_pipe_release(struct inode *inode, struct file *filp)
240 {
241         struct rpc_pipe *pipe;
242         struct rpc_pipe_msg *msg;
243         int last_close;
244
245         inode_lock(inode);
246         pipe = RPC_I(inode)->pipe;
247         if (pipe == NULL)
248                 goto out;
249         msg = filp->private_data;
250         if (msg != NULL) {
251                 spin_lock(&pipe->lock);
252                 msg->errno = -EAGAIN;
253                 list_del_init(&msg->list);
254                 spin_unlock(&pipe->lock);
255                 pipe->ops->destroy_msg(msg);
256         }
257         if (filp->f_mode & FMODE_WRITE)
258                 pipe->nwriters --;
259         if (filp->f_mode & FMODE_READ) {
260                 pipe->nreaders --;
261                 if (pipe->nreaders == 0) {
262                         LIST_HEAD(free_list);
263                         spin_lock(&pipe->lock);
264                         list_splice_init(&pipe->pipe, &free_list);
265                         pipe->pipelen = 0;
266                         spin_unlock(&pipe->lock);
267                         rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
268                                         pipe->ops->destroy_msg, -EAGAIN);
269                 }
270         }
271         last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
272         if (last_close && pipe->ops->release_pipe)
273                 pipe->ops->release_pipe(inode);
274 out:
275         inode_unlock(inode);
276         return 0;
277 }
278
279 static ssize_t
280 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
281 {
282         struct inode *inode = file_inode(filp);
283         struct rpc_pipe *pipe;
284         struct rpc_pipe_msg *msg;
285         int res = 0;
286
287         inode_lock(inode);
288         pipe = RPC_I(inode)->pipe;
289         if (pipe == NULL) {
290                 res = -EPIPE;
291                 goto out_unlock;
292         }
293         msg = filp->private_data;
294         if (msg == NULL) {
295                 spin_lock(&pipe->lock);
296                 if (!list_empty(&pipe->pipe)) {
297                         msg = list_entry(pipe->pipe.next,
298                                         struct rpc_pipe_msg,
299                                         list);
300                         list_move(&msg->list, &pipe->in_upcall);
301                         pipe->pipelen -= msg->len;
302                         filp->private_data = msg;
303                         msg->copied = 0;
304                 }
305                 spin_unlock(&pipe->lock);
306                 if (msg == NULL)
307                         goto out_unlock;
308         }
309         /* NOTE: it is up to the callback to update msg->copied */
310         res = pipe->ops->upcall(filp, msg, buf, len);
311         if (res < 0 || msg->len == msg->copied) {
312                 filp->private_data = NULL;
313                 spin_lock(&pipe->lock);
314                 list_del_init(&msg->list);
315                 spin_unlock(&pipe->lock);
316                 pipe->ops->destroy_msg(msg);
317         }
318 out_unlock:
319         inode_unlock(inode);
320         return res;
321 }
322
323 static ssize_t
324 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
325 {
326         struct inode *inode = file_inode(filp);
327         int res;
328
329         inode_lock(inode);
330         res = -EPIPE;
331         if (RPC_I(inode)->pipe != NULL)
332                 res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
333         inode_unlock(inode);
334         return res;
335 }
336
337 static __poll_t
338 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
339 {
340         struct inode *inode = file_inode(filp);
341         struct rpc_inode *rpci = RPC_I(inode);
342         __poll_t mask = EPOLLOUT | EPOLLWRNORM;
343
344         poll_wait(filp, &rpci->waitq, wait);
345
346         inode_lock(inode);
347         if (rpci->pipe == NULL)
348                 mask |= EPOLLERR | EPOLLHUP;
349         else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
350                 mask |= EPOLLIN | EPOLLRDNORM;
351         inode_unlock(inode);
352         return mask;
353 }
354
355 static long
356 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
357 {
358         struct inode *inode = file_inode(filp);
359         struct rpc_pipe *pipe;
360         int len;
361
362         switch (cmd) {
363         case FIONREAD:
364                 inode_lock(inode);
365                 pipe = RPC_I(inode)->pipe;
366                 if (pipe == NULL) {
367                         inode_unlock(inode);
368                         return -EPIPE;
369                 }
370                 spin_lock(&pipe->lock);
371                 len = pipe->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(&pipe->lock);
378                 inode_unlock(inode);
379                 return put_user(len, (int __user *)arg);
380         default:
381                 return -EINVAL;
382         }
383 }
384
385 static const struct file_operations rpc_pipe_fops = {
386         .owner          = THIS_MODULE,
387         .llseek         = no_llseek,
388         .read           = rpc_pipe_read,
389         .write          = rpc_pipe_write,
390         .poll           = rpc_pipe_poll,
391         .unlocked_ioctl = rpc_pipe_ioctl,
392         .open           = rpc_pipe_open,
393         .release        = rpc_pipe_release,
394 };
395
396 static int
397 rpc_show_info(struct seq_file *m, void *v)
398 {
399         struct rpc_clnt *clnt = m->private;
400
401         rcu_read_lock();
402         seq_printf(m, "RPC server: %s\n",
403                         rcu_dereference(clnt->cl_xprt)->servername);
404         seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
405                         clnt->cl_prog, clnt->cl_vers);
406         seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
407         seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
408         seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
409         rcu_read_unlock();
410         return 0;
411 }
412
413 static int
414 rpc_info_open(struct inode *inode, struct file *file)
415 {
416         struct rpc_clnt *clnt = NULL;
417         int ret = single_open(file, rpc_show_info, NULL);
418
419         if (!ret) {
420                 struct seq_file *m = file->private_data;
421
422                 spin_lock(&file->f_path.dentry->d_lock);
423                 if (!d_unhashed(file->f_path.dentry))
424                         clnt = RPC_I(inode)->private;
425                 if (clnt != NULL && atomic_inc_not_zero(&clnt->cl_count)) {
426                         spin_unlock(&file->f_path.dentry->d_lock);
427                         m->private = clnt;
428                 } else {
429                         spin_unlock(&file->f_path.dentry->d_lock);
430                         single_release(inode, file);
431                         ret = -EINVAL;
432                 }
433         }
434         return ret;
435 }
436
437 static int
438 rpc_info_release(struct inode *inode, struct file *file)
439 {
440         struct seq_file *m = file->private_data;
441         struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
442
443         if (clnt)
444                 rpc_release_client(clnt);
445         return single_release(inode, file);
446 }
447
448 static const struct file_operations rpc_info_operations = {
449         .owner          = THIS_MODULE,
450         .open           = rpc_info_open,
451         .read           = seq_read,
452         .llseek         = seq_lseek,
453         .release        = rpc_info_release,
454 };
455
456
457 /*
458  * Description of fs contents.
459  */
460 struct rpc_filelist {
461         const char *name;
462         const struct file_operations *i_fop;
463         umode_t mode;
464 };
465
466 static struct inode *
467 rpc_get_inode(struct super_block *sb, umode_t mode)
468 {
469         struct inode *inode = new_inode(sb);
470         if (!inode)
471                 return NULL;
472         inode->i_ino = get_next_ino();
473         inode->i_mode = mode;
474         inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
475         switch (mode & S_IFMT) {
476         case S_IFDIR:
477                 inode->i_fop = &simple_dir_operations;
478                 inode->i_op = &simple_dir_inode_operations;
479                 inc_nlink(inode);
480         default:
481                 break;
482         }
483         return inode;
484 }
485
486 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
487                                umode_t mode,
488                                const struct file_operations *i_fop,
489                                void *private)
490 {
491         struct inode *inode;
492
493         d_drop(dentry);
494         inode = rpc_get_inode(dir->i_sb, mode);
495         if (!inode)
496                 goto out_err;
497         inode->i_ino = iunique(dir->i_sb, 100);
498         if (i_fop)
499                 inode->i_fop = i_fop;
500         if (private)
501                 rpc_inode_setowner(inode, private);
502         d_add(dentry, inode);
503         return 0;
504 out_err:
505         printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
506                         __FILE__, __func__, dentry);
507         dput(dentry);
508         return -ENOMEM;
509 }
510
511 static int __rpc_create(struct inode *dir, struct dentry *dentry,
512                         umode_t mode,
513                         const struct file_operations *i_fop,
514                         void *private)
515 {
516         int err;
517
518         err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
519         if (err)
520                 return err;
521         fsnotify_create(dir, dentry);
522         return 0;
523 }
524
525 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
526                        umode_t mode,
527                        const struct file_operations *i_fop,
528                        void *private)
529 {
530         int err;
531
532         err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
533         if (err)
534                 return err;
535         inc_nlink(dir);
536         fsnotify_mkdir(dir, dentry);
537         return 0;
538 }
539
540 static void
541 init_pipe(struct rpc_pipe *pipe)
542 {
543         pipe->nreaders = 0;
544         pipe->nwriters = 0;
545         INIT_LIST_HEAD(&pipe->in_upcall);
546         INIT_LIST_HEAD(&pipe->in_downcall);
547         INIT_LIST_HEAD(&pipe->pipe);
548         pipe->pipelen = 0;
549         INIT_DELAYED_WORK(&pipe->queue_timeout,
550                             rpc_timeout_upcall_queue);
551         pipe->ops = NULL;
552         spin_lock_init(&pipe->lock);
553         pipe->dentry = NULL;
554 }
555
556 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
557 {
558         kfree(pipe);
559 }
560 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
561
562 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
563 {
564         struct rpc_pipe *pipe;
565
566         pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
567         if (!pipe)
568                 return ERR_PTR(-ENOMEM);
569         init_pipe(pipe);
570         pipe->ops = ops;
571         pipe->flags = flags;
572         return pipe;
573 }
574 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
575
576 static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
577                                umode_t mode,
578                                const struct file_operations *i_fop,
579                                void *private,
580                                struct rpc_pipe *pipe)
581 {
582         struct rpc_inode *rpci;
583         int err;
584
585         err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
586         if (err)
587                 return err;
588         rpci = RPC_I(d_inode(dentry));
589         rpci->private = private;
590         rpci->pipe = pipe;
591         fsnotify_create(dir, dentry);
592         return 0;
593 }
594
595 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
596 {
597         int ret;
598
599         dget(dentry);
600         ret = simple_rmdir(dir, dentry);
601         if (!ret)
602                 fsnotify_rmdir(dir, dentry);
603         d_delete(dentry);
604         dput(dentry);
605         return ret;
606 }
607
608 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
609 {
610         int ret;
611
612         dget(dentry);
613         ret = simple_unlink(dir, dentry);
614         if (!ret)
615                 fsnotify_unlink(dir, dentry);
616         d_delete(dentry);
617         dput(dentry);
618         return ret;
619 }
620
621 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
622 {
623         struct inode *inode = d_inode(dentry);
624
625         rpc_close_pipes(inode);
626         return __rpc_unlink(dir, dentry);
627 }
628
629 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
630                                           const char *name)
631 {
632         struct qstr q = QSTR_INIT(name, strlen(name));
633         struct dentry *dentry = d_hash_and_lookup(parent, &q);
634         if (!dentry) {
635                 dentry = d_alloc(parent, &q);
636                 if (!dentry)
637                         return ERR_PTR(-ENOMEM);
638         }
639         if (d_really_is_negative(dentry))
640                 return dentry;
641         dput(dentry);
642         return ERR_PTR(-EEXIST);
643 }
644
645 /*
646  * FIXME: This probably has races.
647  */
648 static void __rpc_depopulate(struct dentry *parent,
649                              const struct rpc_filelist *files,
650                              int start, int eof)
651 {
652         struct inode *dir = d_inode(parent);
653         struct dentry *dentry;
654         struct qstr name;
655         int i;
656
657         for (i = start; i < eof; i++) {
658                 name.name = files[i].name;
659                 name.len = strlen(files[i].name);
660                 dentry = d_hash_and_lookup(parent, &name);
661
662                 if (dentry == NULL)
663                         continue;
664                 if (d_really_is_negative(dentry))
665                         goto next;
666                 switch (d_inode(dentry)->i_mode & S_IFMT) {
667                         default:
668                                 BUG();
669                         case S_IFREG:
670                                 __rpc_unlink(dir, dentry);
671                                 break;
672                         case S_IFDIR:
673                                 __rpc_rmdir(dir, dentry);
674                 }
675 next:
676                 dput(dentry);
677         }
678 }
679
680 static void rpc_depopulate(struct dentry *parent,
681                            const struct rpc_filelist *files,
682                            int start, int eof)
683 {
684         struct inode *dir = d_inode(parent);
685
686         inode_lock_nested(dir, I_MUTEX_CHILD);
687         __rpc_depopulate(parent, files, start, eof);
688         inode_unlock(dir);
689 }
690
691 static int rpc_populate(struct dentry *parent,
692                         const struct rpc_filelist *files,
693                         int start, int eof,
694                         void *private)
695 {
696         struct inode *dir = d_inode(parent);
697         struct dentry *dentry;
698         int i, err;
699
700         inode_lock(dir);
701         for (i = start; i < eof; i++) {
702                 dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
703                 err = PTR_ERR(dentry);
704                 if (IS_ERR(dentry))
705                         goto out_bad;
706                 switch (files[i].mode & S_IFMT) {
707                         default:
708                                 BUG();
709                         case S_IFREG:
710                                 err = __rpc_create(dir, dentry,
711                                                 files[i].mode,
712                                                 files[i].i_fop,
713                                                 private);
714                                 break;
715                         case S_IFDIR:
716                                 err = __rpc_mkdir(dir, dentry,
717                                                 files[i].mode,
718                                                 NULL,
719                                                 private);
720                 }
721                 if (err != 0)
722                         goto out_bad;
723         }
724         inode_unlock(dir);
725         return 0;
726 out_bad:
727         __rpc_depopulate(parent, files, start, eof);
728         inode_unlock(dir);
729         printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
730                         __FILE__, __func__, parent);
731         return err;
732 }
733
734 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
735                 const char *name, umode_t mode, void *private,
736                 int (*populate)(struct dentry *, void *), void *args_populate)
737 {
738         struct dentry *dentry;
739         struct inode *dir = d_inode(parent);
740         int error;
741
742         inode_lock_nested(dir, I_MUTEX_PARENT);
743         dentry = __rpc_lookup_create_exclusive(parent, name);
744         if (IS_ERR(dentry))
745                 goto out;
746         error = __rpc_mkdir(dir, dentry, mode, NULL, private);
747         if (error != 0)
748                 goto out_err;
749         if (populate != NULL) {
750                 error = populate(dentry, args_populate);
751                 if (error)
752                         goto err_rmdir;
753         }
754 out:
755         inode_unlock(dir);
756         return dentry;
757 err_rmdir:
758         __rpc_rmdir(dir, dentry);
759 out_err:
760         dentry = ERR_PTR(error);
761         goto out;
762 }
763
764 static int rpc_rmdir_depopulate(struct dentry *dentry,
765                 void (*depopulate)(struct dentry *))
766 {
767         struct dentry *parent;
768         struct inode *dir;
769         int error;
770
771         parent = dget_parent(dentry);
772         dir = d_inode(parent);
773         inode_lock_nested(dir, I_MUTEX_PARENT);
774         if (depopulate != NULL)
775                 depopulate(dentry);
776         error = __rpc_rmdir(dir, dentry);
777         inode_unlock(dir);
778         dput(parent);
779         return error;
780 }
781
782 /**
783  * rpc_mkpipe - make an rpc_pipefs file for kernel<->userspace communication
784  * @parent: dentry of directory to create new "pipe" in
785  * @name: name of pipe
786  * @private: private data to associate with the pipe, for the caller's use
787  * @pipe: &rpc_pipe containing input parameters
788  *
789  * Data is made available for userspace to read by calls to
790  * rpc_queue_upcall().  The actual reads will result in calls to
791  * @ops->upcall, which will be called with the file pointer,
792  * message, and userspace buffer to copy to.
793  *
794  * Writes can come at any time, and do not necessarily have to be
795  * responses to upcalls.  They will result in calls to @msg->downcall.
796  *
797  * The @private argument passed here will be available to all these methods
798  * from the file pointer, via RPC_I(file_inode(file))->private.
799  */
800 struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
801                                  void *private, struct rpc_pipe *pipe)
802 {
803         struct dentry *dentry;
804         struct inode *dir = d_inode(parent);
805         umode_t umode = S_IFIFO | 0600;
806         int err;
807
808         if (pipe->ops->upcall == NULL)
809                 umode &= ~0444;
810         if (pipe->ops->downcall == NULL)
811                 umode &= ~0222;
812
813         inode_lock_nested(dir, I_MUTEX_PARENT);
814         dentry = __rpc_lookup_create_exclusive(parent, name);
815         if (IS_ERR(dentry))
816                 goto out;
817         err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
818                                   private, pipe);
819         if (err)
820                 goto out_err;
821 out:
822         inode_unlock(dir);
823         return dentry;
824 out_err:
825         dentry = ERR_PTR(err);
826         printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
827                         __FILE__, __func__, parent, name,
828                         err);
829         goto out;
830 }
831 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
832
833 /**
834  * rpc_unlink - remove a pipe
835  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
836  *
837  * After this call, lookups will no longer find the pipe, and any
838  * attempts to read or write using preexisting opens of the pipe will
839  * return -EPIPE.
840  */
841 int
842 rpc_unlink(struct dentry *dentry)
843 {
844         struct dentry *parent;
845         struct inode *dir;
846         int error = 0;
847
848         parent = dget_parent(dentry);
849         dir = d_inode(parent);
850         inode_lock_nested(dir, I_MUTEX_PARENT);
851         error = __rpc_rmpipe(dir, dentry);
852         inode_unlock(dir);
853         dput(parent);
854         return error;
855 }
856 EXPORT_SYMBOL_GPL(rpc_unlink);
857
858 /**
859  * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
860  * @pdh: pointer to struct rpc_pipe_dir_head
861  */
862 void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
863 {
864         INIT_LIST_HEAD(&pdh->pdh_entries);
865         pdh->pdh_dentry = NULL;
866 }
867 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
868
869 /**
870  * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
871  * @pdo: pointer to struct rpc_pipe_dir_object
872  * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
873  * @pdo_data: pointer to caller-defined data
874  */
875 void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
876                 const struct rpc_pipe_dir_object_ops *pdo_ops,
877                 void *pdo_data)
878 {
879         INIT_LIST_HEAD(&pdo->pdo_head);
880         pdo->pdo_ops = pdo_ops;
881         pdo->pdo_data = pdo_data;
882 }
883 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
884
885 static int
886 rpc_add_pipe_dir_object_locked(struct net *net,
887                 struct rpc_pipe_dir_head *pdh,
888                 struct rpc_pipe_dir_object *pdo)
889 {
890         int ret = 0;
891
892         if (pdh->pdh_dentry)
893                 ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
894         if (ret == 0)
895                 list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
896         return ret;
897 }
898
899 static void
900 rpc_remove_pipe_dir_object_locked(struct net *net,
901                 struct rpc_pipe_dir_head *pdh,
902                 struct rpc_pipe_dir_object *pdo)
903 {
904         if (pdh->pdh_dentry)
905                 pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
906         list_del_init(&pdo->pdo_head);
907 }
908
909 /**
910  * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
911  * @net: pointer to struct net
912  * @pdh: pointer to struct rpc_pipe_dir_head
913  * @pdo: pointer to struct rpc_pipe_dir_object
914  *
915  */
916 int
917 rpc_add_pipe_dir_object(struct net *net,
918                 struct rpc_pipe_dir_head *pdh,
919                 struct rpc_pipe_dir_object *pdo)
920 {
921         int ret = 0;
922
923         if (list_empty(&pdo->pdo_head)) {
924                 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
925
926                 mutex_lock(&sn->pipefs_sb_lock);
927                 ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
928                 mutex_unlock(&sn->pipefs_sb_lock);
929         }
930         return ret;
931 }
932 EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
933
934 /**
935  * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
936  * @net: pointer to struct net
937  * @pdh: pointer to struct rpc_pipe_dir_head
938  * @pdo: pointer to struct rpc_pipe_dir_object
939  *
940  */
941 void
942 rpc_remove_pipe_dir_object(struct net *net,
943                 struct rpc_pipe_dir_head *pdh,
944                 struct rpc_pipe_dir_object *pdo)
945 {
946         if (!list_empty(&pdo->pdo_head)) {
947                 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
948
949                 mutex_lock(&sn->pipefs_sb_lock);
950                 rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
951                 mutex_unlock(&sn->pipefs_sb_lock);
952         }
953 }
954 EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
955
956 /**
957  * rpc_find_or_alloc_pipe_dir_object
958  * @net: pointer to struct net
959  * @pdh: pointer to struct rpc_pipe_dir_head
960  * @match: match struct rpc_pipe_dir_object to data
961  * @alloc: allocate a new struct rpc_pipe_dir_object
962  * @data: user defined data for match() and alloc()
963  *
964  */
965 struct rpc_pipe_dir_object *
966 rpc_find_or_alloc_pipe_dir_object(struct net *net,
967                 struct rpc_pipe_dir_head *pdh,
968                 int (*match)(struct rpc_pipe_dir_object *, void *),
969                 struct rpc_pipe_dir_object *(*alloc)(void *),
970                 void *data)
971 {
972         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
973         struct rpc_pipe_dir_object *pdo;
974
975         mutex_lock(&sn->pipefs_sb_lock);
976         list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
977                 if (!match(pdo, data))
978                         continue;
979                 goto out;
980         }
981         pdo = alloc(data);
982         if (!pdo)
983                 goto out;
984         rpc_add_pipe_dir_object_locked(net, pdh, pdo);
985 out:
986         mutex_unlock(&sn->pipefs_sb_lock);
987         return pdo;
988 }
989 EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
990
991 static void
992 rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
993 {
994         struct rpc_pipe_dir_object *pdo;
995         struct dentry *dir = pdh->pdh_dentry;
996
997         list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
998                 pdo->pdo_ops->create(dir, pdo);
999 }
1000
1001 static void
1002 rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
1003 {
1004         struct rpc_pipe_dir_object *pdo;
1005         struct dentry *dir = pdh->pdh_dentry;
1006
1007         list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1008                 pdo->pdo_ops->destroy(dir, pdo);
1009 }
1010
1011 enum {
1012         RPCAUTH_info,
1013         RPCAUTH_EOF
1014 };
1015
1016 static const struct rpc_filelist authfiles[] = {
1017         [RPCAUTH_info] = {
1018                 .name = "info",
1019                 .i_fop = &rpc_info_operations,
1020                 .mode = S_IFREG | 0400,
1021         },
1022 };
1023
1024 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1025 {
1026         return rpc_populate(dentry,
1027                             authfiles, RPCAUTH_info, RPCAUTH_EOF,
1028                             private);
1029 }
1030
1031 static void rpc_clntdir_depopulate(struct dentry *dentry)
1032 {
1033         rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1034 }
1035
1036 /**
1037  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
1038  * @dentry: the parent of new directory
1039  * @name: the name of new directory
1040  * @rpc_client: rpc client to associate with this directory
1041  *
1042  * This creates a directory at the given @path associated with
1043  * @rpc_clnt, which will contain a file named "info" with some basic
1044  * information about the client, together with any "pipes" that may
1045  * later be created using rpc_mkpipe().
1046  */
1047 struct dentry *rpc_create_client_dir(struct dentry *dentry,
1048                                    const char *name,
1049                                    struct rpc_clnt *rpc_client)
1050 {
1051         struct dentry *ret;
1052
1053         ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1054                                  rpc_clntdir_populate, rpc_client);
1055         if (!IS_ERR(ret)) {
1056                 rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1057                 rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1058         }
1059         return ret;
1060 }
1061
1062 /**
1063  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
1064  * @rpc_client: rpc_client for the pipe
1065  */
1066 int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
1067 {
1068         struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1069
1070         if (dentry == NULL)
1071                 return 0;
1072         rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1073         rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
1074         return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
1075 }
1076
1077 static const struct rpc_filelist cache_pipefs_files[3] = {
1078         [0] = {
1079                 .name = "channel",
1080                 .i_fop = &cache_file_operations_pipefs,
1081                 .mode = S_IFREG | 0600,
1082         },
1083         [1] = {
1084                 .name = "content",
1085                 .i_fop = &content_file_operations_pipefs,
1086                 .mode = S_IFREG | 0400,
1087         },
1088         [2] = {
1089                 .name = "flush",
1090                 .i_fop = &cache_flush_operations_pipefs,
1091                 .mode = S_IFREG | 0600,
1092         },
1093 };
1094
1095 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1096 {
1097         return rpc_populate(dentry,
1098                             cache_pipefs_files, 0, 3,
1099                             private);
1100 }
1101
1102 static void rpc_cachedir_depopulate(struct dentry *dentry)
1103 {
1104         rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1105 }
1106
1107 struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
1108                                     umode_t umode, struct cache_detail *cd)
1109 {
1110         return rpc_mkdir_populate(parent, name, umode, NULL,
1111                         rpc_cachedir_populate, cd);
1112 }
1113
1114 void rpc_remove_cache_dir(struct dentry *dentry)
1115 {
1116         rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1117 }
1118
1119 /*
1120  * populate the filesystem
1121  */
1122 static const struct super_operations s_ops = {
1123         .alloc_inode    = rpc_alloc_inode,
1124         .free_inode     = rpc_free_inode,
1125         .statfs         = simple_statfs,
1126 };
1127
1128 #define RPCAUTH_GSSMAGIC 0x67596969
1129
1130 /*
1131  * We have a single directory with 1 node in it.
1132  */
1133 enum {
1134         RPCAUTH_lockd,
1135         RPCAUTH_mount,
1136         RPCAUTH_nfs,
1137         RPCAUTH_portmap,
1138         RPCAUTH_statd,
1139         RPCAUTH_nfsd4_cb,
1140         RPCAUTH_cache,
1141         RPCAUTH_nfsd,
1142         RPCAUTH_gssd,
1143         RPCAUTH_RootEOF
1144 };
1145
1146 static const struct rpc_filelist files[] = {
1147         [RPCAUTH_lockd] = {
1148                 .name = "lockd",
1149                 .mode = S_IFDIR | 0555,
1150         },
1151         [RPCAUTH_mount] = {
1152                 .name = "mount",
1153                 .mode = S_IFDIR | 0555,
1154         },
1155         [RPCAUTH_nfs] = {
1156                 .name = "nfs",
1157                 .mode = S_IFDIR | 0555,
1158         },
1159         [RPCAUTH_portmap] = {
1160                 .name = "portmap",
1161                 .mode = S_IFDIR | 0555,
1162         },
1163         [RPCAUTH_statd] = {
1164                 .name = "statd",
1165                 .mode = S_IFDIR | 0555,
1166         },
1167         [RPCAUTH_nfsd4_cb] = {
1168                 .name = "nfsd4_cb",
1169                 .mode = S_IFDIR | 0555,
1170         },
1171         [RPCAUTH_cache] = {
1172                 .name = "cache",
1173                 .mode = S_IFDIR | 0555,
1174         },
1175         [RPCAUTH_nfsd] = {
1176                 .name = "nfsd",
1177                 .mode = S_IFDIR | 0555,
1178         },
1179         [RPCAUTH_gssd] = {
1180                 .name = "gssd",
1181                 .mode = S_IFDIR | 0555,
1182         },
1183 };
1184
1185 /*
1186  * This call can be used only in RPC pipefs mount notification hooks.
1187  */
1188 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1189                                const unsigned char *dir_name)
1190 {
1191         struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
1192         return d_hash_and_lookup(sb->s_root, &dir);
1193 }
1194 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1195
1196 int rpc_pipefs_init_net(struct net *net)
1197 {
1198         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1199
1200         sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1201         if (IS_ERR(sn->gssd_dummy))
1202                 return PTR_ERR(sn->gssd_dummy);
1203
1204         mutex_init(&sn->pipefs_sb_lock);
1205         sn->pipe_version = -1;
1206         return 0;
1207 }
1208
1209 void rpc_pipefs_exit_net(struct net *net)
1210 {
1211         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1212
1213         rpc_destroy_pipe_data(sn->gssd_dummy);
1214 }
1215
1216 /*
1217  * This call will be used for per network namespace operations calls.
1218  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1219  * found. This lock have to be released by rpc_put_sb_net() when all operations
1220  * will be completed.
1221  */
1222 struct super_block *rpc_get_sb_net(const struct net *net)
1223 {
1224         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1225
1226         mutex_lock(&sn->pipefs_sb_lock);
1227         if (sn->pipefs_sb)
1228                 return sn->pipefs_sb;
1229         mutex_unlock(&sn->pipefs_sb_lock);
1230         return NULL;
1231 }
1232 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1233
1234 void rpc_put_sb_net(const struct net *net)
1235 {
1236         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1237
1238         WARN_ON(sn->pipefs_sb == NULL);
1239         mutex_unlock(&sn->pipefs_sb_lock);
1240 }
1241 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1242
1243 static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1244         [0] = {
1245                 .name = "clntXX",
1246                 .mode = S_IFDIR | 0555,
1247         },
1248 };
1249
1250 static ssize_t
1251 dummy_downcall(struct file *filp, const char __user *src, size_t len)
1252 {
1253         return -EINVAL;
1254 }
1255
1256 static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1257         .upcall         = rpc_pipe_generic_upcall,
1258         .downcall       = dummy_downcall,
1259 };
1260
1261 /*
1262  * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1263  * that it will ever use this info to handle an upcall, but rpc.gssd expects
1264  * that this file will be there and have a certain format.
1265  */
1266 static int
1267 rpc_dummy_info_show(struct seq_file *m, void *v)
1268 {
1269         seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1270         seq_printf(m, "service: foo (1) version 0\n");
1271         seq_printf(m, "address: 127.0.0.1\n");
1272         seq_printf(m, "protocol: tcp\n");
1273         seq_printf(m, "port: 0\n");
1274         return 0;
1275 }
1276 DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
1277
1278 static const struct rpc_filelist gssd_dummy_info_file[] = {
1279         [0] = {
1280                 .name = "info",
1281                 .i_fop = &rpc_dummy_info_fops,
1282                 .mode = S_IFREG | 0400,
1283         },
1284 };
1285
1286 /**
1287  * rpc_gssd_dummy_populate - create a dummy gssd pipe
1288  * @root:       root of the rpc_pipefs filesystem
1289  * @pipe_data:  pipe data created when netns is initialized
1290  *
1291  * Create a dummy set of directories and a pipe that gssd can hold open to
1292  * indicate that it is up and running.
1293  */
1294 static struct dentry *
1295 rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1296 {
1297         int ret = 0;
1298         struct dentry *gssd_dentry;
1299         struct dentry *clnt_dentry = NULL;
1300         struct dentry *pipe_dentry = NULL;
1301         struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1302                                   strlen(files[RPCAUTH_gssd].name));
1303
1304         /* We should never get this far if "gssd" doesn't exist */
1305         gssd_dentry = d_hash_and_lookup(root, &q);
1306         if (!gssd_dentry)
1307                 return ERR_PTR(-ENOENT);
1308
1309         ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1310         if (ret) {
1311                 pipe_dentry = ERR_PTR(ret);
1312                 goto out;
1313         }
1314
1315         q.name = gssd_dummy_clnt_dir[0].name;
1316         q.len = strlen(gssd_dummy_clnt_dir[0].name);
1317         clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1318         if (!clnt_dentry) {
1319                 pipe_dentry = ERR_PTR(-ENOENT);
1320                 goto out;
1321         }
1322
1323         ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1324         if (ret) {
1325                 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1326                 pipe_dentry = ERR_PTR(ret);
1327                 goto out;
1328         }
1329
1330         pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
1331         if (IS_ERR(pipe_dentry)) {
1332                 __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
1333                 __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1334         }
1335 out:
1336         dput(clnt_dentry);
1337         dput(gssd_dentry);
1338         return pipe_dentry;
1339 }
1340
1341 static void
1342 rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1343 {
1344         struct dentry *clnt_dir = pipe_dentry->d_parent;
1345         struct dentry *gssd_dir = clnt_dir->d_parent;
1346
1347         dget(pipe_dentry);
1348         __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
1349         __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1350         __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1351         dput(pipe_dentry);
1352 }
1353
1354 static int
1355 rpc_fill_super(struct super_block *sb, void *data, int silent)
1356 {
1357         struct inode *inode;
1358         struct dentry *root, *gssd_dentry;
1359         struct net *net = get_net(sb->s_fs_info);
1360         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1361         int err;
1362
1363         sb->s_blocksize = PAGE_SIZE;
1364         sb->s_blocksize_bits = PAGE_SHIFT;
1365         sb->s_magic = RPCAUTH_GSSMAGIC;
1366         sb->s_op = &s_ops;
1367         sb->s_d_op = &simple_dentry_operations;
1368         sb->s_time_gran = 1;
1369
1370         inode = rpc_get_inode(sb, S_IFDIR | 0555);
1371         sb->s_root = root = d_make_root(inode);
1372         if (!root)
1373                 return -ENOMEM;
1374         if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1375                 return -ENOMEM;
1376
1377         gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1378         if (IS_ERR(gssd_dentry)) {
1379                 __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1380                 return PTR_ERR(gssd_dentry);
1381         }
1382
1383         dprintk("RPC:       sending pipefs MOUNT notification for net %x%s\n",
1384                 net->ns.inum, NET_NAME(net));
1385         mutex_lock(&sn->pipefs_sb_lock);
1386         sn->pipefs_sb = sb;
1387         err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1388                                            RPC_PIPEFS_MOUNT,
1389                                            sb);
1390         if (err)
1391                 goto err_depopulate;
1392         mutex_unlock(&sn->pipefs_sb_lock);
1393         return 0;
1394
1395 err_depopulate:
1396         rpc_gssd_dummy_depopulate(gssd_dentry);
1397         blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1398                                            RPC_PIPEFS_UMOUNT,
1399                                            sb);
1400         sn->pipefs_sb = NULL;
1401         __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1402         mutex_unlock(&sn->pipefs_sb_lock);
1403         return err;
1404 }
1405
1406 bool
1407 gssd_running(struct net *net)
1408 {
1409         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1410         struct rpc_pipe *pipe = sn->gssd_dummy;
1411
1412         return pipe->nreaders || pipe->nwriters;
1413 }
1414 EXPORT_SYMBOL_GPL(gssd_running);
1415
1416 static struct dentry *
1417 rpc_mount(struct file_system_type *fs_type,
1418                 int flags, const char *dev_name, void *data)
1419 {
1420         struct net *net = current->nsproxy->net_ns;
1421         return mount_ns(fs_type, flags, data, net, net->user_ns, rpc_fill_super);
1422 }
1423
1424 static void rpc_kill_sb(struct super_block *sb)
1425 {
1426         struct net *net = sb->s_fs_info;
1427         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1428
1429         mutex_lock(&sn->pipefs_sb_lock);
1430         if (sn->pipefs_sb != sb) {
1431                 mutex_unlock(&sn->pipefs_sb_lock);
1432                 goto out;
1433         }
1434         sn->pipefs_sb = NULL;
1435         dprintk("RPC:       sending pipefs UMOUNT notification for net %x%s\n",
1436                 net->ns.inum, NET_NAME(net));
1437         blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1438                                            RPC_PIPEFS_UMOUNT,
1439                                            sb);
1440         mutex_unlock(&sn->pipefs_sb_lock);
1441 out:
1442         kill_litter_super(sb);
1443         put_net(net);
1444 }
1445
1446 static struct file_system_type rpc_pipe_fs_type = {
1447         .owner          = THIS_MODULE,
1448         .name           = "rpc_pipefs",
1449         .mount          = rpc_mount,
1450         .kill_sb        = rpc_kill_sb,
1451 };
1452 MODULE_ALIAS_FS("rpc_pipefs");
1453 MODULE_ALIAS("rpc_pipefs");
1454
1455 static void
1456 init_once(void *foo)
1457 {
1458         struct rpc_inode *rpci = (struct rpc_inode *) foo;
1459
1460         inode_init_once(&rpci->vfs_inode);
1461         rpci->private = NULL;
1462         rpci->pipe = NULL;
1463         init_waitqueue_head(&rpci->waitq);
1464 }
1465
1466 int register_rpc_pipefs(void)
1467 {
1468         int err;
1469
1470         rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1471                                 sizeof(struct rpc_inode),
1472                                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1473                                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1474                                 init_once);
1475         if (!rpc_inode_cachep)
1476                 return -ENOMEM;
1477         err = rpc_clients_notifier_register();
1478         if (err)
1479                 goto err_notifier;
1480         err = register_filesystem(&rpc_pipe_fs_type);
1481         if (err)
1482                 goto err_register;
1483         return 0;
1484
1485 err_register:
1486         rpc_clients_notifier_unregister();
1487 err_notifier:
1488         kmem_cache_destroy(rpc_inode_cachep);
1489         return err;
1490 }
1491
1492 void unregister_rpc_pipefs(void)
1493 {
1494         rpc_clients_notifier_unregister();
1495         kmem_cache_destroy(rpc_inode_cachep);
1496         unregister_filesystem(&rpc_pipe_fs_type);
1497 }