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