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