Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * net/sunrpc/rpc_pipe.c
0004  *
0005  * Userland/kernel interface for rpcauth_gss.
0006  * Code shamelessly plagiarized from fs/nfsd/nfsctl.c
0007  * and fs/sysfs/inode.c
0008  *
0009  * Copyright (c) 2002, Trond Myklebust <trond.myklebust@fys.uio.no>
0010  *
0011  */
0012 #include <linux/module.h>
0013 #include <linux/slab.h>
0014 #include <linux/string.h>
0015 #include <linux/pagemap.h>
0016 #include <linux/mount.h>
0017 #include <linux/fs_context.h>
0018 #include <linux/namei.h>
0019 #include <linux/fsnotify.h>
0020 #include <linux/kernel.h>
0021 #include <linux/rcupdate.h>
0022 #include <linux/utsname.h>
0023 
0024 #include <asm/ioctls.h>
0025 #include <linux/poll.h>
0026 #include <linux/wait.h>
0027 #include <linux/seq_file.h>
0028 
0029 #include <linux/sunrpc/clnt.h>
0030 #include <linux/workqueue.h>
0031 #include <linux/sunrpc/rpc_pipe_fs.h>
0032 #include <linux/sunrpc/cache.h>
0033 #include <linux/nsproxy.h>
0034 #include <linux/notifier.h>
0035 
0036 #include "netns.h"
0037 #include "sunrpc.h"
0038 
0039 #define RPCDBG_FACILITY RPCDBG_DEBUG
0040 
0041 #define NET_NAME(net)   ((net == &init_net) ? " (init_net)" : "")
0042 
0043 static struct file_system_type rpc_pipe_fs_type;
0044 static const struct rpc_pipe_ops gssd_dummy_pipe_ops;
0045 
0046 static struct kmem_cache *rpc_inode_cachep __read_mostly;
0047 
0048 #define RPC_UPCALL_TIMEOUT (30*HZ)
0049 
0050 static BLOCKING_NOTIFIER_HEAD(rpc_pipefs_notifier_list);
0051 
0052 int rpc_pipefs_notifier_register(struct notifier_block *nb)
0053 {
0054     return blocking_notifier_chain_register(&rpc_pipefs_notifier_list, nb);
0055 }
0056 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_register);
0057 
0058 void rpc_pipefs_notifier_unregister(struct notifier_block *nb)
0059 {
0060     blocking_notifier_chain_unregister(&rpc_pipefs_notifier_list, nb);
0061 }
0062 EXPORT_SYMBOL_GPL(rpc_pipefs_notifier_unregister);
0063 
0064 static void rpc_purge_list(wait_queue_head_t *waitq, struct list_head *head,
0065         void (*destroy_msg)(struct rpc_pipe_msg *), int err)
0066 {
0067     struct rpc_pipe_msg *msg;
0068 
0069     if (list_empty(head))
0070         return;
0071     do {
0072         msg = list_entry(head->next, struct rpc_pipe_msg, list);
0073         list_del_init(&msg->list);
0074         msg->errno = err;
0075         destroy_msg(msg);
0076     } while (!list_empty(head));
0077 
0078     if (waitq)
0079         wake_up(waitq);
0080 }
0081 
0082 static void
0083 rpc_timeout_upcall_queue(struct work_struct *work)
0084 {
0085     LIST_HEAD(free_list);
0086     struct rpc_pipe *pipe =
0087         container_of(work, struct rpc_pipe, queue_timeout.work);
0088     void (*destroy_msg)(struct rpc_pipe_msg *);
0089     struct dentry *dentry;
0090 
0091     spin_lock(&pipe->lock);
0092     destroy_msg = pipe->ops->destroy_msg;
0093     if (pipe->nreaders == 0) {
0094         list_splice_init(&pipe->pipe, &free_list);
0095         pipe->pipelen = 0;
0096     }
0097     dentry = dget(pipe->dentry);
0098     spin_unlock(&pipe->lock);
0099     rpc_purge_list(dentry ? &RPC_I(d_inode(dentry))->waitq : NULL,
0100             &free_list, destroy_msg, -ETIMEDOUT);
0101     dput(dentry);
0102 }
0103 
0104 ssize_t rpc_pipe_generic_upcall(struct file *filp, struct rpc_pipe_msg *msg,
0105                 char __user *dst, size_t buflen)
0106 {
0107     char *data = (char *)msg->data + msg->copied;
0108     size_t mlen = min(msg->len - msg->copied, buflen);
0109     unsigned long left;
0110 
0111     left = copy_to_user(dst, data, mlen);
0112     if (left == mlen) {
0113         msg->errno = -EFAULT;
0114         return -EFAULT;
0115     }
0116 
0117     mlen -= left;
0118     msg->copied += mlen;
0119     msg->errno = 0;
0120     return mlen;
0121 }
0122 EXPORT_SYMBOL_GPL(rpc_pipe_generic_upcall);
0123 
0124 /**
0125  * rpc_queue_upcall - queue an upcall message to userspace
0126  * @pipe: upcall pipe on which to queue given message
0127  * @msg: message to queue
0128  *
0129  * Call with an @inode created by rpc_mkpipe() to queue an upcall.
0130  * A userspace process may then later read the upcall by performing a
0131  * read on an open file for this inode.  It is up to the caller to
0132  * initialize the fields of @msg (other than @msg->list) appropriately.
0133  */
0134 int
0135 rpc_queue_upcall(struct rpc_pipe *pipe, struct rpc_pipe_msg *msg)
0136 {
0137     int res = -EPIPE;
0138     struct dentry *dentry;
0139 
0140     spin_lock(&pipe->lock);
0141     if (pipe->nreaders) {
0142         list_add_tail(&msg->list, &pipe->pipe);
0143         pipe->pipelen += msg->len;
0144         res = 0;
0145     } else if (pipe->flags & RPC_PIPE_WAIT_FOR_OPEN) {
0146         if (list_empty(&pipe->pipe))
0147             queue_delayed_work(rpciod_workqueue,
0148                     &pipe->queue_timeout,
0149                     RPC_UPCALL_TIMEOUT);
0150         list_add_tail(&msg->list, &pipe->pipe);
0151         pipe->pipelen += msg->len;
0152         res = 0;
0153     }
0154     dentry = dget(pipe->dentry);
0155     spin_unlock(&pipe->lock);
0156     if (dentry) {
0157         wake_up(&RPC_I(d_inode(dentry))->waitq);
0158         dput(dentry);
0159     }
0160     return res;
0161 }
0162 EXPORT_SYMBOL_GPL(rpc_queue_upcall);
0163 
0164 static inline void
0165 rpc_inode_setowner(struct inode *inode, void *private)
0166 {
0167     RPC_I(inode)->private = private;
0168 }
0169 
0170 static void
0171 rpc_close_pipes(struct inode *inode)
0172 {
0173     struct rpc_pipe *pipe = RPC_I(inode)->pipe;
0174     int need_release;
0175     LIST_HEAD(free_list);
0176 
0177     inode_lock(inode);
0178     spin_lock(&pipe->lock);
0179     need_release = pipe->nreaders != 0 || pipe->nwriters != 0;
0180     pipe->nreaders = 0;
0181     list_splice_init(&pipe->in_upcall, &free_list);
0182     list_splice_init(&pipe->pipe, &free_list);
0183     pipe->pipelen = 0;
0184     pipe->dentry = NULL;
0185     spin_unlock(&pipe->lock);
0186     rpc_purge_list(&RPC_I(inode)->waitq, &free_list, pipe->ops->destroy_msg, -EPIPE);
0187     pipe->nwriters = 0;
0188     if (need_release && pipe->ops->release_pipe)
0189         pipe->ops->release_pipe(inode);
0190     cancel_delayed_work_sync(&pipe->queue_timeout);
0191     rpc_inode_setowner(inode, NULL);
0192     RPC_I(inode)->pipe = NULL;
0193     inode_unlock(inode);
0194 }
0195 
0196 static struct inode *
0197 rpc_alloc_inode(struct super_block *sb)
0198 {
0199     struct rpc_inode *rpci;
0200     rpci = alloc_inode_sb(sb, rpc_inode_cachep, GFP_KERNEL);
0201     if (!rpci)
0202         return NULL;
0203     return &rpci->vfs_inode;
0204 }
0205 
0206 static void
0207 rpc_free_inode(struct inode *inode)
0208 {
0209     kmem_cache_free(rpc_inode_cachep, RPC_I(inode));
0210 }
0211 
0212 static int
0213 rpc_pipe_open(struct inode *inode, struct file *filp)
0214 {
0215     struct rpc_pipe *pipe;
0216     int first_open;
0217     int res = -ENXIO;
0218 
0219     inode_lock(inode);
0220     pipe = RPC_I(inode)->pipe;
0221     if (pipe == NULL)
0222         goto out;
0223     first_open = pipe->nreaders == 0 && pipe->nwriters == 0;
0224     if (first_open && pipe->ops->open_pipe) {
0225         res = pipe->ops->open_pipe(inode);
0226         if (res)
0227             goto out;
0228     }
0229     if (filp->f_mode & FMODE_READ)
0230         pipe->nreaders++;
0231     if (filp->f_mode & FMODE_WRITE)
0232         pipe->nwriters++;
0233     res = 0;
0234 out:
0235     inode_unlock(inode);
0236     return res;
0237 }
0238 
0239 static int
0240 rpc_pipe_release(struct inode *inode, struct file *filp)
0241 {
0242     struct rpc_pipe *pipe;
0243     struct rpc_pipe_msg *msg;
0244     int last_close;
0245 
0246     inode_lock(inode);
0247     pipe = RPC_I(inode)->pipe;
0248     if (pipe == NULL)
0249         goto out;
0250     msg = filp->private_data;
0251     if (msg != NULL) {
0252         spin_lock(&pipe->lock);
0253         msg->errno = -EAGAIN;
0254         list_del_init(&msg->list);
0255         spin_unlock(&pipe->lock);
0256         pipe->ops->destroy_msg(msg);
0257     }
0258     if (filp->f_mode & FMODE_WRITE)
0259         pipe->nwriters --;
0260     if (filp->f_mode & FMODE_READ) {
0261         pipe->nreaders --;
0262         if (pipe->nreaders == 0) {
0263             LIST_HEAD(free_list);
0264             spin_lock(&pipe->lock);
0265             list_splice_init(&pipe->pipe, &free_list);
0266             pipe->pipelen = 0;
0267             spin_unlock(&pipe->lock);
0268             rpc_purge_list(&RPC_I(inode)->waitq, &free_list,
0269                     pipe->ops->destroy_msg, -EAGAIN);
0270         }
0271     }
0272     last_close = pipe->nwriters == 0 && pipe->nreaders == 0;
0273     if (last_close && pipe->ops->release_pipe)
0274         pipe->ops->release_pipe(inode);
0275 out:
0276     inode_unlock(inode);
0277     return 0;
0278 }
0279 
0280 static ssize_t
0281 rpc_pipe_read(struct file *filp, char __user *buf, size_t len, loff_t *offset)
0282 {
0283     struct inode *inode = file_inode(filp);
0284     struct rpc_pipe *pipe;
0285     struct rpc_pipe_msg *msg;
0286     int res = 0;
0287 
0288     inode_lock(inode);
0289     pipe = RPC_I(inode)->pipe;
0290     if (pipe == NULL) {
0291         res = -EPIPE;
0292         goto out_unlock;
0293     }
0294     msg = filp->private_data;
0295     if (msg == NULL) {
0296         spin_lock(&pipe->lock);
0297         if (!list_empty(&pipe->pipe)) {
0298             msg = list_entry(pipe->pipe.next,
0299                     struct rpc_pipe_msg,
0300                     list);
0301             list_move(&msg->list, &pipe->in_upcall);
0302             pipe->pipelen -= msg->len;
0303             filp->private_data = msg;
0304             msg->copied = 0;
0305         }
0306         spin_unlock(&pipe->lock);
0307         if (msg == NULL)
0308             goto out_unlock;
0309     }
0310     /* NOTE: it is up to the callback to update msg->copied */
0311     res = pipe->ops->upcall(filp, msg, buf, len);
0312     if (res < 0 || msg->len == msg->copied) {
0313         filp->private_data = NULL;
0314         spin_lock(&pipe->lock);
0315         list_del_init(&msg->list);
0316         spin_unlock(&pipe->lock);
0317         pipe->ops->destroy_msg(msg);
0318     }
0319 out_unlock:
0320     inode_unlock(inode);
0321     return res;
0322 }
0323 
0324 static ssize_t
0325 rpc_pipe_write(struct file *filp, const char __user *buf, size_t len, loff_t *offset)
0326 {
0327     struct inode *inode = file_inode(filp);
0328     int res;
0329 
0330     inode_lock(inode);
0331     res = -EPIPE;
0332     if (RPC_I(inode)->pipe != NULL)
0333         res = RPC_I(inode)->pipe->ops->downcall(filp, buf, len);
0334     inode_unlock(inode);
0335     return res;
0336 }
0337 
0338 static __poll_t
0339 rpc_pipe_poll(struct file *filp, struct poll_table_struct *wait)
0340 {
0341     struct inode *inode = file_inode(filp);
0342     struct rpc_inode *rpci = RPC_I(inode);
0343     __poll_t mask = EPOLLOUT | EPOLLWRNORM;
0344 
0345     poll_wait(filp, &rpci->waitq, wait);
0346 
0347     inode_lock(inode);
0348     if (rpci->pipe == NULL)
0349         mask |= EPOLLERR | EPOLLHUP;
0350     else if (filp->private_data || !list_empty(&rpci->pipe->pipe))
0351         mask |= EPOLLIN | EPOLLRDNORM;
0352     inode_unlock(inode);
0353     return mask;
0354 }
0355 
0356 static long
0357 rpc_pipe_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0358 {
0359     struct inode *inode = file_inode(filp);
0360     struct rpc_pipe *pipe;
0361     int len;
0362 
0363     switch (cmd) {
0364     case FIONREAD:
0365         inode_lock(inode);
0366         pipe = RPC_I(inode)->pipe;
0367         if (pipe == NULL) {
0368             inode_unlock(inode);
0369             return -EPIPE;
0370         }
0371         spin_lock(&pipe->lock);
0372         len = pipe->pipelen;
0373         if (filp->private_data) {
0374             struct rpc_pipe_msg *msg;
0375             msg = filp->private_data;
0376             len += msg->len - msg->copied;
0377         }
0378         spin_unlock(&pipe->lock);
0379         inode_unlock(inode);
0380         return put_user(len, (int __user *)arg);
0381     default:
0382         return -EINVAL;
0383     }
0384 }
0385 
0386 static const struct file_operations rpc_pipe_fops = {
0387     .owner      = THIS_MODULE,
0388     .llseek     = no_llseek,
0389     .read       = rpc_pipe_read,
0390     .write      = rpc_pipe_write,
0391     .poll       = rpc_pipe_poll,
0392     .unlocked_ioctl = rpc_pipe_ioctl,
0393     .open       = rpc_pipe_open,
0394     .release    = rpc_pipe_release,
0395 };
0396 
0397 static int
0398 rpc_show_info(struct seq_file *m, void *v)
0399 {
0400     struct rpc_clnt *clnt = m->private;
0401 
0402     rcu_read_lock();
0403     seq_printf(m, "RPC server: %s\n",
0404             rcu_dereference(clnt->cl_xprt)->servername);
0405     seq_printf(m, "service: %s (%d) version %d\n", clnt->cl_program->name,
0406             clnt->cl_prog, clnt->cl_vers);
0407     seq_printf(m, "address: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
0408     seq_printf(m, "protocol: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PROTO));
0409     seq_printf(m, "port: %s\n", rpc_peeraddr2str(clnt, RPC_DISPLAY_PORT));
0410     rcu_read_unlock();
0411     return 0;
0412 }
0413 
0414 static int
0415 rpc_info_open(struct inode *inode, struct file *file)
0416 {
0417     struct rpc_clnt *clnt = NULL;
0418     int ret = single_open(file, rpc_show_info, NULL);
0419 
0420     if (!ret) {
0421         struct seq_file *m = file->private_data;
0422 
0423         spin_lock(&file->f_path.dentry->d_lock);
0424         if (!d_unhashed(file->f_path.dentry))
0425             clnt = RPC_I(inode)->private;
0426         if (clnt != NULL && refcount_inc_not_zero(&clnt->cl_count)) {
0427             spin_unlock(&file->f_path.dentry->d_lock);
0428             m->private = clnt;
0429         } else {
0430             spin_unlock(&file->f_path.dentry->d_lock);
0431             single_release(inode, file);
0432             ret = -EINVAL;
0433         }
0434     }
0435     return ret;
0436 }
0437 
0438 static int
0439 rpc_info_release(struct inode *inode, struct file *file)
0440 {
0441     struct seq_file *m = file->private_data;
0442     struct rpc_clnt *clnt = (struct rpc_clnt *)m->private;
0443 
0444     if (clnt)
0445         rpc_release_client(clnt);
0446     return single_release(inode, file);
0447 }
0448 
0449 static const struct file_operations rpc_info_operations = {
0450     .owner      = THIS_MODULE,
0451     .open       = rpc_info_open,
0452     .read       = seq_read,
0453     .llseek     = seq_lseek,
0454     .release    = rpc_info_release,
0455 };
0456 
0457 
0458 /*
0459  * Description of fs contents.
0460  */
0461 struct rpc_filelist {
0462     const char *name;
0463     const struct file_operations *i_fop;
0464     umode_t mode;
0465 };
0466 
0467 static struct inode *
0468 rpc_get_inode(struct super_block *sb, umode_t mode)
0469 {
0470     struct inode *inode = new_inode(sb);
0471     if (!inode)
0472         return NULL;
0473     inode->i_ino = get_next_ino();
0474     inode->i_mode = mode;
0475     inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
0476     switch (mode & S_IFMT) {
0477     case S_IFDIR:
0478         inode->i_fop = &simple_dir_operations;
0479         inode->i_op = &simple_dir_inode_operations;
0480         inc_nlink(inode);
0481         break;
0482     default:
0483         break;
0484     }
0485     return inode;
0486 }
0487 
0488 static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
0489                    umode_t mode,
0490                    const struct file_operations *i_fop,
0491                    void *private)
0492 {
0493     struct inode *inode;
0494 
0495     d_drop(dentry);
0496     inode = rpc_get_inode(dir->i_sb, mode);
0497     if (!inode)
0498         goto out_err;
0499     inode->i_ino = iunique(dir->i_sb, 100);
0500     if (i_fop)
0501         inode->i_fop = i_fop;
0502     if (private)
0503         rpc_inode_setowner(inode, private);
0504     d_add(dentry, inode);
0505     return 0;
0506 out_err:
0507     printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
0508             __FILE__, __func__, dentry);
0509     dput(dentry);
0510     return -ENOMEM;
0511 }
0512 
0513 static int __rpc_create(struct inode *dir, struct dentry *dentry,
0514             umode_t mode,
0515             const struct file_operations *i_fop,
0516             void *private)
0517 {
0518     int err;
0519 
0520     err = __rpc_create_common(dir, dentry, S_IFREG | mode, i_fop, private);
0521     if (err)
0522         return err;
0523     fsnotify_create(dir, dentry);
0524     return 0;
0525 }
0526 
0527 static int __rpc_mkdir(struct inode *dir, struct dentry *dentry,
0528                umode_t mode,
0529                const struct file_operations *i_fop,
0530                void *private)
0531 {
0532     int err;
0533 
0534     err = __rpc_create_common(dir, dentry, S_IFDIR | mode, i_fop, private);
0535     if (err)
0536         return err;
0537     inc_nlink(dir);
0538     fsnotify_mkdir(dir, dentry);
0539     return 0;
0540 }
0541 
0542 static void
0543 init_pipe(struct rpc_pipe *pipe)
0544 {
0545     pipe->nreaders = 0;
0546     pipe->nwriters = 0;
0547     INIT_LIST_HEAD(&pipe->in_upcall);
0548     INIT_LIST_HEAD(&pipe->in_downcall);
0549     INIT_LIST_HEAD(&pipe->pipe);
0550     pipe->pipelen = 0;
0551     INIT_DELAYED_WORK(&pipe->queue_timeout,
0552                 rpc_timeout_upcall_queue);
0553     pipe->ops = NULL;
0554     spin_lock_init(&pipe->lock);
0555     pipe->dentry = NULL;
0556 }
0557 
0558 void rpc_destroy_pipe_data(struct rpc_pipe *pipe)
0559 {
0560     kfree(pipe);
0561 }
0562 EXPORT_SYMBOL_GPL(rpc_destroy_pipe_data);
0563 
0564 struct rpc_pipe *rpc_mkpipe_data(const struct rpc_pipe_ops *ops, int flags)
0565 {
0566     struct rpc_pipe *pipe;
0567 
0568     pipe = kzalloc(sizeof(struct rpc_pipe), GFP_KERNEL);
0569     if (!pipe)
0570         return ERR_PTR(-ENOMEM);
0571     init_pipe(pipe);
0572     pipe->ops = ops;
0573     pipe->flags = flags;
0574     return pipe;
0575 }
0576 EXPORT_SYMBOL_GPL(rpc_mkpipe_data);
0577 
0578 static int __rpc_mkpipe_dentry(struct inode *dir, struct dentry *dentry,
0579                    umode_t mode,
0580                    const struct file_operations *i_fop,
0581                    void *private,
0582                    struct rpc_pipe *pipe)
0583 {
0584     struct rpc_inode *rpci;
0585     int err;
0586 
0587     err = __rpc_create_common(dir, dentry, S_IFIFO | mode, i_fop, private);
0588     if (err)
0589         return err;
0590     rpci = RPC_I(d_inode(dentry));
0591     rpci->private = private;
0592     rpci->pipe = pipe;
0593     fsnotify_create(dir, dentry);
0594     return 0;
0595 }
0596 
0597 static int __rpc_rmdir(struct inode *dir, struct dentry *dentry)
0598 {
0599     int ret;
0600 
0601     dget(dentry);
0602     ret = simple_rmdir(dir, dentry);
0603     d_drop(dentry);
0604     if (!ret)
0605         fsnotify_rmdir(dir, dentry);
0606     dput(dentry);
0607     return ret;
0608 }
0609 
0610 static int __rpc_unlink(struct inode *dir, struct dentry *dentry)
0611 {
0612     int ret;
0613 
0614     dget(dentry);
0615     ret = simple_unlink(dir, dentry);
0616     d_drop(dentry);
0617     if (!ret)
0618         fsnotify_unlink(dir, dentry);
0619     dput(dentry);
0620     return ret;
0621 }
0622 
0623 static int __rpc_rmpipe(struct inode *dir, struct dentry *dentry)
0624 {
0625     struct inode *inode = d_inode(dentry);
0626 
0627     rpc_close_pipes(inode);
0628     return __rpc_unlink(dir, dentry);
0629 }
0630 
0631 static struct dentry *__rpc_lookup_create_exclusive(struct dentry *parent,
0632                       const char *name)
0633 {
0634     struct qstr q = QSTR_INIT(name, strlen(name));
0635     struct dentry *dentry = d_hash_and_lookup(parent, &q);
0636     if (!dentry) {
0637         dentry = d_alloc(parent, &q);
0638         if (!dentry)
0639             return ERR_PTR(-ENOMEM);
0640     }
0641     if (d_really_is_negative(dentry))
0642         return dentry;
0643     dput(dentry);
0644     return ERR_PTR(-EEXIST);
0645 }
0646 
0647 /*
0648  * FIXME: This probably has races.
0649  */
0650 static void __rpc_depopulate(struct dentry *parent,
0651                  const struct rpc_filelist *files,
0652                  int start, int eof)
0653 {
0654     struct inode *dir = d_inode(parent);
0655     struct dentry *dentry;
0656     struct qstr name;
0657     int i;
0658 
0659     for (i = start; i < eof; i++) {
0660         name.name = files[i].name;
0661         name.len = strlen(files[i].name);
0662         dentry = d_hash_and_lookup(parent, &name);
0663 
0664         if (dentry == NULL)
0665             continue;
0666         if (d_really_is_negative(dentry))
0667             goto next;
0668         switch (d_inode(dentry)->i_mode & S_IFMT) {
0669             default:
0670                 BUG();
0671             case S_IFREG:
0672                 __rpc_unlink(dir, dentry);
0673                 break;
0674             case S_IFDIR:
0675                 __rpc_rmdir(dir, dentry);
0676         }
0677 next:
0678         dput(dentry);
0679     }
0680 }
0681 
0682 static void rpc_depopulate(struct dentry *parent,
0683                const struct rpc_filelist *files,
0684                int start, int eof)
0685 {
0686     struct inode *dir = d_inode(parent);
0687 
0688     inode_lock_nested(dir, I_MUTEX_CHILD);
0689     __rpc_depopulate(parent, files, start, eof);
0690     inode_unlock(dir);
0691 }
0692 
0693 static int rpc_populate(struct dentry *parent,
0694             const struct rpc_filelist *files,
0695             int start, int eof,
0696             void *private)
0697 {
0698     struct inode *dir = d_inode(parent);
0699     struct dentry *dentry;
0700     int i, err;
0701 
0702     inode_lock(dir);
0703     for (i = start; i < eof; i++) {
0704         dentry = __rpc_lookup_create_exclusive(parent, files[i].name);
0705         err = PTR_ERR(dentry);
0706         if (IS_ERR(dentry))
0707             goto out_bad;
0708         switch (files[i].mode & S_IFMT) {
0709             default:
0710                 BUG();
0711             case S_IFREG:
0712                 err = __rpc_create(dir, dentry,
0713                         files[i].mode,
0714                         files[i].i_fop,
0715                         private);
0716                 break;
0717             case S_IFDIR:
0718                 err = __rpc_mkdir(dir, dentry,
0719                         files[i].mode,
0720                         NULL,
0721                         private);
0722         }
0723         if (err != 0)
0724             goto out_bad;
0725     }
0726     inode_unlock(dir);
0727     return 0;
0728 out_bad:
0729     __rpc_depopulate(parent, files, start, eof);
0730     inode_unlock(dir);
0731     printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
0732             __FILE__, __func__, parent);
0733     return err;
0734 }
0735 
0736 static struct dentry *rpc_mkdir_populate(struct dentry *parent,
0737         const char *name, umode_t mode, void *private,
0738         int (*populate)(struct dentry *, void *), void *args_populate)
0739 {
0740     struct dentry *dentry;
0741     struct inode *dir = d_inode(parent);
0742     int error;
0743 
0744     inode_lock_nested(dir, I_MUTEX_PARENT);
0745     dentry = __rpc_lookup_create_exclusive(parent, name);
0746     if (IS_ERR(dentry))
0747         goto out;
0748     error = __rpc_mkdir(dir, dentry, mode, NULL, private);
0749     if (error != 0)
0750         goto out_err;
0751     if (populate != NULL) {
0752         error = populate(dentry, args_populate);
0753         if (error)
0754             goto err_rmdir;
0755     }
0756 out:
0757     inode_unlock(dir);
0758     return dentry;
0759 err_rmdir:
0760     __rpc_rmdir(dir, dentry);
0761 out_err:
0762     dentry = ERR_PTR(error);
0763     goto out;
0764 }
0765 
0766 static int rpc_rmdir_depopulate(struct dentry *dentry,
0767         void (*depopulate)(struct dentry *))
0768 {
0769     struct dentry *parent;
0770     struct inode *dir;
0771     int error;
0772 
0773     parent = dget_parent(dentry);
0774     dir = d_inode(parent);
0775     inode_lock_nested(dir, I_MUTEX_PARENT);
0776     if (depopulate != NULL)
0777         depopulate(dentry);
0778     error = __rpc_rmdir(dir, dentry);
0779     inode_unlock(dir);
0780     dput(parent);
0781     return error;
0782 }
0783 
0784 /**
0785  * rpc_mkpipe_dentry - make an rpc_pipefs file for kernel<->userspace
0786  *             communication
0787  * @parent: dentry of directory to create new "pipe" in
0788  * @name: name of pipe
0789  * @private: private data to associate with the pipe, for the caller's use
0790  * @pipe: &rpc_pipe containing input parameters
0791  *
0792  * Data is made available for userspace to read by calls to
0793  * rpc_queue_upcall().  The actual reads will result in calls to
0794  * @ops->upcall, which will be called with the file pointer,
0795  * message, and userspace buffer to copy to.
0796  *
0797  * Writes can come at any time, and do not necessarily have to be
0798  * responses to upcalls.  They will result in calls to @msg->downcall.
0799  *
0800  * The @private argument passed here will be available to all these methods
0801  * from the file pointer, via RPC_I(file_inode(file))->private.
0802  */
0803 struct dentry *rpc_mkpipe_dentry(struct dentry *parent, const char *name,
0804                  void *private, struct rpc_pipe *pipe)
0805 {
0806     struct dentry *dentry;
0807     struct inode *dir = d_inode(parent);
0808     umode_t umode = S_IFIFO | 0600;
0809     int err;
0810 
0811     if (pipe->ops->upcall == NULL)
0812         umode &= ~0444;
0813     if (pipe->ops->downcall == NULL)
0814         umode &= ~0222;
0815 
0816     inode_lock_nested(dir, I_MUTEX_PARENT);
0817     dentry = __rpc_lookup_create_exclusive(parent, name);
0818     if (IS_ERR(dentry))
0819         goto out;
0820     err = __rpc_mkpipe_dentry(dir, dentry, umode, &rpc_pipe_fops,
0821                   private, pipe);
0822     if (err)
0823         goto out_err;
0824 out:
0825     inode_unlock(dir);
0826     return dentry;
0827 out_err:
0828     dentry = ERR_PTR(err);
0829     printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
0830             __FILE__, __func__, parent, name,
0831             err);
0832     goto out;
0833 }
0834 EXPORT_SYMBOL_GPL(rpc_mkpipe_dentry);
0835 
0836 /**
0837  * rpc_unlink - remove a pipe
0838  * @dentry: dentry for the pipe, as returned from rpc_mkpipe
0839  *
0840  * After this call, lookups will no longer find the pipe, and any
0841  * attempts to read or write using preexisting opens of the pipe will
0842  * return -EPIPE.
0843  */
0844 int
0845 rpc_unlink(struct dentry *dentry)
0846 {
0847     struct dentry *parent;
0848     struct inode *dir;
0849     int error = 0;
0850 
0851     parent = dget_parent(dentry);
0852     dir = d_inode(parent);
0853     inode_lock_nested(dir, I_MUTEX_PARENT);
0854     error = __rpc_rmpipe(dir, dentry);
0855     inode_unlock(dir);
0856     dput(parent);
0857     return error;
0858 }
0859 EXPORT_SYMBOL_GPL(rpc_unlink);
0860 
0861 /**
0862  * rpc_init_pipe_dir_head - initialise a struct rpc_pipe_dir_head
0863  * @pdh: pointer to struct rpc_pipe_dir_head
0864  */
0865 void rpc_init_pipe_dir_head(struct rpc_pipe_dir_head *pdh)
0866 {
0867     INIT_LIST_HEAD(&pdh->pdh_entries);
0868     pdh->pdh_dentry = NULL;
0869 }
0870 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_head);
0871 
0872 /**
0873  * rpc_init_pipe_dir_object - initialise a struct rpc_pipe_dir_object
0874  * @pdo: pointer to struct rpc_pipe_dir_object
0875  * @pdo_ops: pointer to const struct rpc_pipe_dir_object_ops
0876  * @pdo_data: pointer to caller-defined data
0877  */
0878 void rpc_init_pipe_dir_object(struct rpc_pipe_dir_object *pdo,
0879         const struct rpc_pipe_dir_object_ops *pdo_ops,
0880         void *pdo_data)
0881 {
0882     INIT_LIST_HEAD(&pdo->pdo_head);
0883     pdo->pdo_ops = pdo_ops;
0884     pdo->pdo_data = pdo_data;
0885 }
0886 EXPORT_SYMBOL_GPL(rpc_init_pipe_dir_object);
0887 
0888 static int
0889 rpc_add_pipe_dir_object_locked(struct net *net,
0890         struct rpc_pipe_dir_head *pdh,
0891         struct rpc_pipe_dir_object *pdo)
0892 {
0893     int ret = 0;
0894 
0895     if (pdh->pdh_dentry)
0896         ret = pdo->pdo_ops->create(pdh->pdh_dentry, pdo);
0897     if (ret == 0)
0898         list_add_tail(&pdo->pdo_head, &pdh->pdh_entries);
0899     return ret;
0900 }
0901 
0902 static void
0903 rpc_remove_pipe_dir_object_locked(struct net *net,
0904         struct rpc_pipe_dir_head *pdh,
0905         struct rpc_pipe_dir_object *pdo)
0906 {
0907     if (pdh->pdh_dentry)
0908         pdo->pdo_ops->destroy(pdh->pdh_dentry, pdo);
0909     list_del_init(&pdo->pdo_head);
0910 }
0911 
0912 /**
0913  * rpc_add_pipe_dir_object - associate a rpc_pipe_dir_object to a directory
0914  * @net: pointer to struct net
0915  * @pdh: pointer to struct rpc_pipe_dir_head
0916  * @pdo: pointer to struct rpc_pipe_dir_object
0917  *
0918  */
0919 int
0920 rpc_add_pipe_dir_object(struct net *net,
0921         struct rpc_pipe_dir_head *pdh,
0922         struct rpc_pipe_dir_object *pdo)
0923 {
0924     int ret = 0;
0925 
0926     if (list_empty(&pdo->pdo_head)) {
0927         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
0928 
0929         mutex_lock(&sn->pipefs_sb_lock);
0930         ret = rpc_add_pipe_dir_object_locked(net, pdh, pdo);
0931         mutex_unlock(&sn->pipefs_sb_lock);
0932     }
0933     return ret;
0934 }
0935 EXPORT_SYMBOL_GPL(rpc_add_pipe_dir_object);
0936 
0937 /**
0938  * rpc_remove_pipe_dir_object - remove a rpc_pipe_dir_object from a directory
0939  * @net: pointer to struct net
0940  * @pdh: pointer to struct rpc_pipe_dir_head
0941  * @pdo: pointer to struct rpc_pipe_dir_object
0942  *
0943  */
0944 void
0945 rpc_remove_pipe_dir_object(struct net *net,
0946         struct rpc_pipe_dir_head *pdh,
0947         struct rpc_pipe_dir_object *pdo)
0948 {
0949     if (!list_empty(&pdo->pdo_head)) {
0950         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
0951 
0952         mutex_lock(&sn->pipefs_sb_lock);
0953         rpc_remove_pipe_dir_object_locked(net, pdh, pdo);
0954         mutex_unlock(&sn->pipefs_sb_lock);
0955     }
0956 }
0957 EXPORT_SYMBOL_GPL(rpc_remove_pipe_dir_object);
0958 
0959 /**
0960  * rpc_find_or_alloc_pipe_dir_object
0961  * @net: pointer to struct net
0962  * @pdh: pointer to struct rpc_pipe_dir_head
0963  * @match: match struct rpc_pipe_dir_object to data
0964  * @alloc: allocate a new struct rpc_pipe_dir_object
0965  * @data: user defined data for match() and alloc()
0966  *
0967  */
0968 struct rpc_pipe_dir_object *
0969 rpc_find_or_alloc_pipe_dir_object(struct net *net,
0970         struct rpc_pipe_dir_head *pdh,
0971         int (*match)(struct rpc_pipe_dir_object *, void *),
0972         struct rpc_pipe_dir_object *(*alloc)(void *),
0973         void *data)
0974 {
0975     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
0976     struct rpc_pipe_dir_object *pdo;
0977 
0978     mutex_lock(&sn->pipefs_sb_lock);
0979     list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head) {
0980         if (!match(pdo, data))
0981             continue;
0982         goto out;
0983     }
0984     pdo = alloc(data);
0985     if (!pdo)
0986         goto out;
0987     rpc_add_pipe_dir_object_locked(net, pdh, pdo);
0988 out:
0989     mutex_unlock(&sn->pipefs_sb_lock);
0990     return pdo;
0991 }
0992 EXPORT_SYMBOL_GPL(rpc_find_or_alloc_pipe_dir_object);
0993 
0994 static void
0995 rpc_create_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
0996 {
0997     struct rpc_pipe_dir_object *pdo;
0998     struct dentry *dir = pdh->pdh_dentry;
0999 
1000     list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1001         pdo->pdo_ops->create(dir, pdo);
1002 }
1003 
1004 static void
1005 rpc_destroy_pipe_dir_objects(struct rpc_pipe_dir_head *pdh)
1006 {
1007     struct rpc_pipe_dir_object *pdo;
1008     struct dentry *dir = pdh->pdh_dentry;
1009 
1010     list_for_each_entry(pdo, &pdh->pdh_entries, pdo_head)
1011         pdo->pdo_ops->destroy(dir, pdo);
1012 }
1013 
1014 enum {
1015     RPCAUTH_info,
1016     RPCAUTH_EOF
1017 };
1018 
1019 static const struct rpc_filelist authfiles[] = {
1020     [RPCAUTH_info] = {
1021         .name = "info",
1022         .i_fop = &rpc_info_operations,
1023         .mode = S_IFREG | 0400,
1024     },
1025 };
1026 
1027 static int rpc_clntdir_populate(struct dentry *dentry, void *private)
1028 {
1029     return rpc_populate(dentry,
1030                 authfiles, RPCAUTH_info, RPCAUTH_EOF,
1031                 private);
1032 }
1033 
1034 static void rpc_clntdir_depopulate(struct dentry *dentry)
1035 {
1036     rpc_depopulate(dentry, authfiles, RPCAUTH_info, RPCAUTH_EOF);
1037 }
1038 
1039 /**
1040  * rpc_create_client_dir - Create a new rpc_client directory in rpc_pipefs
1041  * @dentry: the parent of new directory
1042  * @name: the name of new directory
1043  * @rpc_client: rpc client to associate with this directory
1044  *
1045  * This creates a directory at the given @path associated with
1046  * @rpc_clnt, which will contain a file named "info" with some basic
1047  * information about the client, together with any "pipes" that may
1048  * later be created using rpc_mkpipe().
1049  */
1050 struct dentry *rpc_create_client_dir(struct dentry *dentry,
1051                    const char *name,
1052                    struct rpc_clnt *rpc_client)
1053 {
1054     struct dentry *ret;
1055 
1056     ret = rpc_mkdir_populate(dentry, name, 0555, NULL,
1057                  rpc_clntdir_populate, rpc_client);
1058     if (!IS_ERR(ret)) {
1059         rpc_client->cl_pipedir_objects.pdh_dentry = ret;
1060         rpc_create_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1061     }
1062     return ret;
1063 }
1064 
1065 /**
1066  * rpc_remove_client_dir - Remove a directory created with rpc_create_client_dir()
1067  * @rpc_client: rpc_client for the pipe
1068  */
1069 int rpc_remove_client_dir(struct rpc_clnt *rpc_client)
1070 {
1071     struct dentry *dentry = rpc_client->cl_pipedir_objects.pdh_dentry;
1072 
1073     if (dentry == NULL)
1074         return 0;
1075     rpc_destroy_pipe_dir_objects(&rpc_client->cl_pipedir_objects);
1076     rpc_client->cl_pipedir_objects.pdh_dentry = NULL;
1077     return rpc_rmdir_depopulate(dentry, rpc_clntdir_depopulate);
1078 }
1079 
1080 static const struct rpc_filelist cache_pipefs_files[3] = {
1081     [0] = {
1082         .name = "channel",
1083         .i_fop = &cache_file_operations_pipefs,
1084         .mode = S_IFREG | 0600,
1085     },
1086     [1] = {
1087         .name = "content",
1088         .i_fop = &content_file_operations_pipefs,
1089         .mode = S_IFREG | 0400,
1090     },
1091     [2] = {
1092         .name = "flush",
1093         .i_fop = &cache_flush_operations_pipefs,
1094         .mode = S_IFREG | 0600,
1095     },
1096 };
1097 
1098 static int rpc_cachedir_populate(struct dentry *dentry, void *private)
1099 {
1100     return rpc_populate(dentry,
1101                 cache_pipefs_files, 0, 3,
1102                 private);
1103 }
1104 
1105 static void rpc_cachedir_depopulate(struct dentry *dentry)
1106 {
1107     rpc_depopulate(dentry, cache_pipefs_files, 0, 3);
1108 }
1109 
1110 struct dentry *rpc_create_cache_dir(struct dentry *parent, const char *name,
1111                     umode_t umode, struct cache_detail *cd)
1112 {
1113     return rpc_mkdir_populate(parent, name, umode, NULL,
1114             rpc_cachedir_populate, cd);
1115 }
1116 
1117 void rpc_remove_cache_dir(struct dentry *dentry)
1118 {
1119     rpc_rmdir_depopulate(dentry, rpc_cachedir_depopulate);
1120 }
1121 
1122 /*
1123  * populate the filesystem
1124  */
1125 static const struct super_operations s_ops = {
1126     .alloc_inode    = rpc_alloc_inode,
1127     .free_inode = rpc_free_inode,
1128     .statfs     = simple_statfs,
1129 };
1130 
1131 #define RPCAUTH_GSSMAGIC 0x67596969
1132 
1133 /*
1134  * We have a single directory with 1 node in it.
1135  */
1136 enum {
1137     RPCAUTH_lockd,
1138     RPCAUTH_mount,
1139     RPCAUTH_nfs,
1140     RPCAUTH_portmap,
1141     RPCAUTH_statd,
1142     RPCAUTH_nfsd4_cb,
1143     RPCAUTH_cache,
1144     RPCAUTH_nfsd,
1145     RPCAUTH_gssd,
1146     RPCAUTH_RootEOF
1147 };
1148 
1149 static const struct rpc_filelist files[] = {
1150     [RPCAUTH_lockd] = {
1151         .name = "lockd",
1152         .mode = S_IFDIR | 0555,
1153     },
1154     [RPCAUTH_mount] = {
1155         .name = "mount",
1156         .mode = S_IFDIR | 0555,
1157     },
1158     [RPCAUTH_nfs] = {
1159         .name = "nfs",
1160         .mode = S_IFDIR | 0555,
1161     },
1162     [RPCAUTH_portmap] = {
1163         .name = "portmap",
1164         .mode = S_IFDIR | 0555,
1165     },
1166     [RPCAUTH_statd] = {
1167         .name = "statd",
1168         .mode = S_IFDIR | 0555,
1169     },
1170     [RPCAUTH_nfsd4_cb] = {
1171         .name = "nfsd4_cb",
1172         .mode = S_IFDIR | 0555,
1173     },
1174     [RPCAUTH_cache] = {
1175         .name = "cache",
1176         .mode = S_IFDIR | 0555,
1177     },
1178     [RPCAUTH_nfsd] = {
1179         .name = "nfsd",
1180         .mode = S_IFDIR | 0555,
1181     },
1182     [RPCAUTH_gssd] = {
1183         .name = "gssd",
1184         .mode = S_IFDIR | 0555,
1185     },
1186 };
1187 
1188 /*
1189  * This call can be used only in RPC pipefs mount notification hooks.
1190  */
1191 struct dentry *rpc_d_lookup_sb(const struct super_block *sb,
1192                    const unsigned char *dir_name)
1193 {
1194     struct qstr dir = QSTR_INIT(dir_name, strlen(dir_name));
1195     return d_hash_and_lookup(sb->s_root, &dir);
1196 }
1197 EXPORT_SYMBOL_GPL(rpc_d_lookup_sb);
1198 
1199 int rpc_pipefs_init_net(struct net *net)
1200 {
1201     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1202 
1203     sn->gssd_dummy = rpc_mkpipe_data(&gssd_dummy_pipe_ops, 0);
1204     if (IS_ERR(sn->gssd_dummy))
1205         return PTR_ERR(sn->gssd_dummy);
1206 
1207     mutex_init(&sn->pipefs_sb_lock);
1208     sn->pipe_version = -1;
1209     return 0;
1210 }
1211 
1212 void rpc_pipefs_exit_net(struct net *net)
1213 {
1214     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1215 
1216     rpc_destroy_pipe_data(sn->gssd_dummy);
1217 }
1218 
1219 /*
1220  * This call will be used for per network namespace operations calls.
1221  * Note: Function will be returned with pipefs_sb_lock taken if superblock was
1222  * found. This lock have to be released by rpc_put_sb_net() when all operations
1223  * will be completed.
1224  */
1225 struct super_block *rpc_get_sb_net(const struct net *net)
1226 {
1227     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1228 
1229     mutex_lock(&sn->pipefs_sb_lock);
1230     if (sn->pipefs_sb)
1231         return sn->pipefs_sb;
1232     mutex_unlock(&sn->pipefs_sb_lock);
1233     return NULL;
1234 }
1235 EXPORT_SYMBOL_GPL(rpc_get_sb_net);
1236 
1237 void rpc_put_sb_net(const struct net *net)
1238 {
1239     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1240 
1241     WARN_ON(sn->pipefs_sb == NULL);
1242     mutex_unlock(&sn->pipefs_sb_lock);
1243 }
1244 EXPORT_SYMBOL_GPL(rpc_put_sb_net);
1245 
1246 static const struct rpc_filelist gssd_dummy_clnt_dir[] = {
1247     [0] = {
1248         .name = "clntXX",
1249         .mode = S_IFDIR | 0555,
1250     },
1251 };
1252 
1253 static ssize_t
1254 dummy_downcall(struct file *filp, const char __user *src, size_t len)
1255 {
1256     return -EINVAL;
1257 }
1258 
1259 static const struct rpc_pipe_ops gssd_dummy_pipe_ops = {
1260     .upcall     = rpc_pipe_generic_upcall,
1261     .downcall   = dummy_downcall,
1262 };
1263 
1264 /*
1265  * Here we present a bogus "info" file to keep rpc.gssd happy. We don't expect
1266  * that it will ever use this info to handle an upcall, but rpc.gssd expects
1267  * that this file will be there and have a certain format.
1268  */
1269 static int
1270 rpc_dummy_info_show(struct seq_file *m, void *v)
1271 {
1272     seq_printf(m, "RPC server: %s\n", utsname()->nodename);
1273     seq_printf(m, "service: foo (1) version 0\n");
1274     seq_printf(m, "address: 127.0.0.1\n");
1275     seq_printf(m, "protocol: tcp\n");
1276     seq_printf(m, "port: 0\n");
1277     return 0;
1278 }
1279 DEFINE_SHOW_ATTRIBUTE(rpc_dummy_info);
1280 
1281 static const struct rpc_filelist gssd_dummy_info_file[] = {
1282     [0] = {
1283         .name = "info",
1284         .i_fop = &rpc_dummy_info_fops,
1285         .mode = S_IFREG | 0400,
1286     },
1287 };
1288 
1289 /**
1290  * rpc_gssd_dummy_populate - create a dummy gssd pipe
1291  * @root:   root of the rpc_pipefs filesystem
1292  * @pipe_data:  pipe data created when netns is initialized
1293  *
1294  * Create a dummy set of directories and a pipe that gssd can hold open to
1295  * indicate that it is up and running.
1296  */
1297 static struct dentry *
1298 rpc_gssd_dummy_populate(struct dentry *root, struct rpc_pipe *pipe_data)
1299 {
1300     int ret = 0;
1301     struct dentry *gssd_dentry;
1302     struct dentry *clnt_dentry = NULL;
1303     struct dentry *pipe_dentry = NULL;
1304     struct qstr q = QSTR_INIT(files[RPCAUTH_gssd].name,
1305                   strlen(files[RPCAUTH_gssd].name));
1306 
1307     /* We should never get this far if "gssd" doesn't exist */
1308     gssd_dentry = d_hash_and_lookup(root, &q);
1309     if (!gssd_dentry)
1310         return ERR_PTR(-ENOENT);
1311 
1312     ret = rpc_populate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1, NULL);
1313     if (ret) {
1314         pipe_dentry = ERR_PTR(ret);
1315         goto out;
1316     }
1317 
1318     q.name = gssd_dummy_clnt_dir[0].name;
1319     q.len = strlen(gssd_dummy_clnt_dir[0].name);
1320     clnt_dentry = d_hash_and_lookup(gssd_dentry, &q);
1321     if (!clnt_dentry) {
1322         __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1323         pipe_dentry = ERR_PTR(-ENOENT);
1324         goto out;
1325     }
1326 
1327     ret = rpc_populate(clnt_dentry, gssd_dummy_info_file, 0, 1, NULL);
1328     if (ret) {
1329         __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1330         pipe_dentry = ERR_PTR(ret);
1331         goto out;
1332     }
1333 
1334     pipe_dentry = rpc_mkpipe_dentry(clnt_dentry, "gssd", NULL, pipe_data);
1335     if (IS_ERR(pipe_dentry)) {
1336         __rpc_depopulate(clnt_dentry, gssd_dummy_info_file, 0, 1);
1337         __rpc_depopulate(gssd_dentry, gssd_dummy_clnt_dir, 0, 1);
1338     }
1339 out:
1340     dput(clnt_dentry);
1341     dput(gssd_dentry);
1342     return pipe_dentry;
1343 }
1344 
1345 static void
1346 rpc_gssd_dummy_depopulate(struct dentry *pipe_dentry)
1347 {
1348     struct dentry *clnt_dir = pipe_dentry->d_parent;
1349     struct dentry *gssd_dir = clnt_dir->d_parent;
1350 
1351     dget(pipe_dentry);
1352     __rpc_rmpipe(d_inode(clnt_dir), pipe_dentry);
1353     __rpc_depopulate(clnt_dir, gssd_dummy_info_file, 0, 1);
1354     __rpc_depopulate(gssd_dir, gssd_dummy_clnt_dir, 0, 1);
1355     dput(pipe_dentry);
1356 }
1357 
1358 static int
1359 rpc_fill_super(struct super_block *sb, struct fs_context *fc)
1360 {
1361     struct inode *inode;
1362     struct dentry *root, *gssd_dentry;
1363     struct net *net = sb->s_fs_info;
1364     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1365     int err;
1366 
1367     sb->s_blocksize = PAGE_SIZE;
1368     sb->s_blocksize_bits = PAGE_SHIFT;
1369     sb->s_magic = RPCAUTH_GSSMAGIC;
1370     sb->s_op = &s_ops;
1371     sb->s_d_op = &simple_dentry_operations;
1372     sb->s_time_gran = 1;
1373 
1374     inode = rpc_get_inode(sb, S_IFDIR | 0555);
1375     sb->s_root = root = d_make_root(inode);
1376     if (!root)
1377         return -ENOMEM;
1378     if (rpc_populate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF, NULL))
1379         return -ENOMEM;
1380 
1381     gssd_dentry = rpc_gssd_dummy_populate(root, sn->gssd_dummy);
1382     if (IS_ERR(gssd_dentry)) {
1383         __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1384         return PTR_ERR(gssd_dentry);
1385     }
1386 
1387     dprintk("RPC:       sending pipefs MOUNT notification for net %x%s\n",
1388         net->ns.inum, NET_NAME(net));
1389     mutex_lock(&sn->pipefs_sb_lock);
1390     sn->pipefs_sb = sb;
1391     err = blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1392                        RPC_PIPEFS_MOUNT,
1393                        sb);
1394     if (err)
1395         goto err_depopulate;
1396     mutex_unlock(&sn->pipefs_sb_lock);
1397     return 0;
1398 
1399 err_depopulate:
1400     rpc_gssd_dummy_depopulate(gssd_dentry);
1401     blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1402                        RPC_PIPEFS_UMOUNT,
1403                        sb);
1404     sn->pipefs_sb = NULL;
1405     __rpc_depopulate(root, files, RPCAUTH_lockd, RPCAUTH_RootEOF);
1406     mutex_unlock(&sn->pipefs_sb_lock);
1407     return err;
1408 }
1409 
1410 bool
1411 gssd_running(struct net *net)
1412 {
1413     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1414     struct rpc_pipe *pipe = sn->gssd_dummy;
1415 
1416     return pipe->nreaders || pipe->nwriters;
1417 }
1418 EXPORT_SYMBOL_GPL(gssd_running);
1419 
1420 static int rpc_fs_get_tree(struct fs_context *fc)
1421 {
1422     return get_tree_keyed(fc, rpc_fill_super, get_net(fc->net_ns));
1423 }
1424 
1425 static void rpc_fs_free_fc(struct fs_context *fc)
1426 {
1427     if (fc->s_fs_info)
1428         put_net(fc->s_fs_info);
1429 }
1430 
1431 static const struct fs_context_operations rpc_fs_context_ops = {
1432     .free       = rpc_fs_free_fc,
1433     .get_tree   = rpc_fs_get_tree,
1434 };
1435 
1436 static int rpc_init_fs_context(struct fs_context *fc)
1437 {
1438     put_user_ns(fc->user_ns);
1439     fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1440     fc->ops = &rpc_fs_context_ops;
1441     return 0;
1442 }
1443 
1444 static void rpc_kill_sb(struct super_block *sb)
1445 {
1446     struct net *net = sb->s_fs_info;
1447     struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
1448 
1449     mutex_lock(&sn->pipefs_sb_lock);
1450     if (sn->pipefs_sb != sb) {
1451         mutex_unlock(&sn->pipefs_sb_lock);
1452         goto out;
1453     }
1454     sn->pipefs_sb = NULL;
1455     dprintk("RPC:       sending pipefs UMOUNT notification for net %x%s\n",
1456         net->ns.inum, NET_NAME(net));
1457     blocking_notifier_call_chain(&rpc_pipefs_notifier_list,
1458                        RPC_PIPEFS_UMOUNT,
1459                        sb);
1460     mutex_unlock(&sn->pipefs_sb_lock);
1461 out:
1462     kill_litter_super(sb);
1463     put_net(net);
1464 }
1465 
1466 static struct file_system_type rpc_pipe_fs_type = {
1467     .owner      = THIS_MODULE,
1468     .name       = "rpc_pipefs",
1469     .init_fs_context = rpc_init_fs_context,
1470     .kill_sb    = rpc_kill_sb,
1471 };
1472 MODULE_ALIAS_FS("rpc_pipefs");
1473 MODULE_ALIAS("rpc_pipefs");
1474 
1475 static void
1476 init_once(void *foo)
1477 {
1478     struct rpc_inode *rpci = (struct rpc_inode *) foo;
1479 
1480     inode_init_once(&rpci->vfs_inode);
1481     rpci->private = NULL;
1482     rpci->pipe = NULL;
1483     init_waitqueue_head(&rpci->waitq);
1484 }
1485 
1486 int register_rpc_pipefs(void)
1487 {
1488     int err;
1489 
1490     rpc_inode_cachep = kmem_cache_create("rpc_inode_cache",
1491                 sizeof(struct rpc_inode),
1492                 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
1493                         SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1494                 init_once);
1495     if (!rpc_inode_cachep)
1496         return -ENOMEM;
1497     err = rpc_clients_notifier_register();
1498     if (err)
1499         goto err_notifier;
1500     err = register_filesystem(&rpc_pipe_fs_type);
1501     if (err)
1502         goto err_register;
1503     return 0;
1504 
1505 err_register:
1506     rpc_clients_notifier_unregister();
1507 err_notifier:
1508     kmem_cache_destroy(rpc_inode_cachep);
1509     return err;
1510 }
1511 
1512 void unregister_rpc_pipefs(void)
1513 {
1514     rpc_clients_notifier_unregister();
1515     unregister_filesystem(&rpc_pipe_fs_type);
1516     kmem_cache_destroy(rpc_inode_cachep);
1517 }