linux/fs/nfs/super.c
<<
>>
Prefs
   1/*
   2 *  linux/fs/nfs/super.c
   3 *
   4 *  Copyright (C) 1992  Rick Sladkey
   5 *
   6 *  nfs superblock handling functions
   7 *
   8 *  Modularised by Alan Cox <alan@lxorguk.ukuu.org.uk>, while hacking some
   9 *  experimental NFS changes. Modularisation taken straight from SYS5 fs.
  10 *
  11 *  Change to nfs_read_super() to permit NFS mounts to multi-homed hosts.
  12 *  J.S.Peatfield@damtp.cam.ac.uk
  13 *
  14 *  Split from inode.c by David Howells <dhowells@redhat.com>
  15 *
  16 * - superblocks are indexed on server only - all inodes, dentries, etc. associated with a
  17 *   particular server are held in the same superblock
  18 * - NFS superblocks can have several effective roots to the dentry tree
  19 * - directory type roots are spliced into the tree when a path from one root reaches the root
  20 *   of another (see nfs_lookup())
  21 */
  22
  23#include <linux/module.h>
  24#include <linux/init.h>
  25
  26#include <linux/time.h>
  27#include <linux/kernel.h>
  28#include <linux/mm.h>
  29#include <linux/string.h>
  30#include <linux/stat.h>
  31#include <linux/errno.h>
  32#include <linux/unistd.h>
  33#include <linux/sunrpc/clnt.h>
  34#include <linux/sunrpc/stats.h>
  35#include <linux/sunrpc/metrics.h>
  36#include <linux/sunrpc/xprtsock.h>
  37#include <linux/sunrpc/xprtrdma.h>
  38#include <linux/nfs_fs.h>
  39#include <linux/nfs_mount.h>
  40#include <linux/nfs4_mount.h>
  41#include <linux/lockd/bind.h>
  42#include <linux/smp_lock.h>
  43#include <linux/seq_file.h>
  44#include <linux/mount.h>
  45#include <linux/mnt_namespace.h>
  46#include <linux/namei.h>
  47#include <linux/nfs_idmap.h>
  48#include <linux/vfs.h>
  49#include <linux/inet.h>
  50#include <linux/in6.h>
  51#include <linux/slab.h>
  52#include <net/ipv6.h>
  53#include <linux/netdevice.h>
  54#include <linux/nfs_xdr.h>
  55#include <linux/magic.h>
  56#include <linux/parser.h>
  57
  58#include <asm/system.h>
  59#include <asm/uaccess.h>
  60
  61#include "nfs4_fs.h"
  62#include "callback.h"
  63#include "delegation.h"
  64#include "iostat.h"
  65#include "internal.h"
  66#include "fscache.h"
  67
  68#define NFSDBG_FACILITY         NFSDBG_VFS
  69
  70enum {
  71        /* Mount options that take no arguments */
  72        Opt_soft, Opt_hard,
  73        Opt_posix, Opt_noposix,
  74        Opt_cto, Opt_nocto,
  75        Opt_ac, Opt_noac,
  76        Opt_lock, Opt_nolock,
  77        Opt_v2, Opt_v3, Opt_v4,
  78        Opt_udp, Opt_tcp, Opt_rdma,
  79        Opt_acl, Opt_noacl,
  80        Opt_rdirplus, Opt_nordirplus,
  81        Opt_sharecache, Opt_nosharecache,
  82        Opt_resvport, Opt_noresvport,
  83        Opt_fscache, Opt_nofscache,
  84
  85        /* Mount options that take integer arguments */
  86        Opt_port,
  87        Opt_rsize, Opt_wsize, Opt_bsize,
  88        Opt_timeo, Opt_retrans,
  89        Opt_acregmin, Opt_acregmax,
  90        Opt_acdirmin, Opt_acdirmax,
  91        Opt_actimeo,
  92        Opt_namelen,
  93        Opt_mountport,
  94        Opt_mountvers,
  95        Opt_nfsvers,
  96        Opt_minorversion,
  97
  98        /* Mount options that take string arguments */
  99        Opt_sec, Opt_proto, Opt_mountproto, Opt_mounthost,
 100        Opt_addr, Opt_mountaddr, Opt_clientaddr,
 101        Opt_lookupcache,
 102        Opt_fscache_uniq,
 103
 104        /* Special mount options */
 105        Opt_userspace, Opt_deprecated, Opt_sloppy,
 106
 107        Opt_err
 108};
 109
 110static const match_table_t nfs_mount_option_tokens = {
 111        { Opt_userspace, "bg" },
 112        { Opt_userspace, "fg" },
 113        { Opt_userspace, "retry=%s" },
 114
 115        { Opt_sloppy, "sloppy" },
 116
 117        { Opt_soft, "soft" },
 118        { Opt_hard, "hard" },
 119        { Opt_deprecated, "intr" },
 120        { Opt_deprecated, "nointr" },
 121        { Opt_posix, "posix" },
 122        { Opt_noposix, "noposix" },
 123        { Opt_cto, "cto" },
 124        { Opt_nocto, "nocto" },
 125        { Opt_ac, "ac" },
 126        { Opt_noac, "noac" },
 127        { Opt_lock, "lock" },
 128        { Opt_nolock, "nolock" },
 129        { Opt_v2, "v2" },
 130        { Opt_v3, "v3" },
 131        { Opt_v4, "v4" },
 132        { Opt_udp, "udp" },
 133        { Opt_tcp, "tcp" },
 134        { Opt_rdma, "rdma" },
 135        { Opt_acl, "acl" },
 136        { Opt_noacl, "noacl" },
 137        { Opt_rdirplus, "rdirplus" },
 138        { Opt_nordirplus, "nordirplus" },
 139        { Opt_sharecache, "sharecache" },
 140        { Opt_nosharecache, "nosharecache" },
 141        { Opt_resvport, "resvport" },
 142        { Opt_noresvport, "noresvport" },
 143        { Opt_fscache, "fsc" },
 144        { Opt_nofscache, "nofsc" },
 145
 146        { Opt_port, "port=%s" },
 147        { Opt_rsize, "rsize=%s" },
 148        { Opt_wsize, "wsize=%s" },
 149        { Opt_bsize, "bsize=%s" },
 150        { Opt_timeo, "timeo=%s" },
 151        { Opt_retrans, "retrans=%s" },
 152        { Opt_acregmin, "acregmin=%s" },
 153        { Opt_acregmax, "acregmax=%s" },
 154        { Opt_acdirmin, "acdirmin=%s" },
 155        { Opt_acdirmax, "acdirmax=%s" },
 156        { Opt_actimeo, "actimeo=%s" },
 157        { Opt_namelen, "namlen=%s" },
 158        { Opt_mountport, "mountport=%s" },
 159        { Opt_mountvers, "mountvers=%s" },
 160        { Opt_nfsvers, "nfsvers=%s" },
 161        { Opt_nfsvers, "vers=%s" },
 162        { Opt_minorversion, "minorversion=%s" },
 163
 164        { Opt_sec, "sec=%s" },
 165        { Opt_proto, "proto=%s" },
 166        { Opt_mountproto, "mountproto=%s" },
 167        { Opt_addr, "addr=%s" },
 168        { Opt_clientaddr, "clientaddr=%s" },
 169        { Opt_mounthost, "mounthost=%s" },
 170        { Opt_mountaddr, "mountaddr=%s" },
 171
 172        { Opt_lookupcache, "lookupcache=%s" },
 173        { Opt_fscache_uniq, "fsc=%s" },
 174
 175        { Opt_err, NULL }
 176};
 177
 178enum {
 179        Opt_xprt_udp, Opt_xprt_udp6, Opt_xprt_tcp, Opt_xprt_tcp6, Opt_xprt_rdma,
 180
 181        Opt_xprt_err
 182};
 183
 184static const match_table_t nfs_xprt_protocol_tokens = {
 185        { Opt_xprt_udp, "udp" },
 186        { Opt_xprt_udp6, "udp6" },
 187        { Opt_xprt_tcp, "tcp" },
 188        { Opt_xprt_tcp6, "tcp6" },
 189        { Opt_xprt_rdma, "rdma" },
 190
 191        { Opt_xprt_err, NULL }
 192};
 193
 194enum {
 195        Opt_sec_none, Opt_sec_sys,
 196        Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
 197        Opt_sec_lkey, Opt_sec_lkeyi, Opt_sec_lkeyp,
 198        Opt_sec_spkm, Opt_sec_spkmi, Opt_sec_spkmp,
 199
 200        Opt_sec_err
 201};
 202
 203static const match_table_t nfs_secflavor_tokens = {
 204        { Opt_sec_none, "none" },
 205        { Opt_sec_none, "null" },
 206        { Opt_sec_sys, "sys" },
 207
 208        { Opt_sec_krb5, "krb5" },
 209        { Opt_sec_krb5i, "krb5i" },
 210        { Opt_sec_krb5p, "krb5p" },
 211
 212        { Opt_sec_lkey, "lkey" },
 213        { Opt_sec_lkeyi, "lkeyi" },
 214        { Opt_sec_lkeyp, "lkeyp" },
 215
 216        { Opt_sec_spkm, "spkm3" },
 217        { Opt_sec_spkmi, "spkm3i" },
 218        { Opt_sec_spkmp, "spkm3p" },
 219
 220        { Opt_sec_err, NULL }
 221};
 222
 223enum {
 224        Opt_lookupcache_all, Opt_lookupcache_positive,
 225        Opt_lookupcache_none,
 226
 227        Opt_lookupcache_err
 228};
 229
 230static match_table_t nfs_lookupcache_tokens = {
 231        { Opt_lookupcache_all, "all" },
 232        { Opt_lookupcache_positive, "pos" },
 233        { Opt_lookupcache_positive, "positive" },
 234        { Opt_lookupcache_none, "none" },
 235
 236        { Opt_lookupcache_err, NULL }
 237};
 238
 239
 240static void nfs_umount_begin(struct super_block *);
 241static int  nfs_statfs(struct dentry *, struct kstatfs *);
 242static int  nfs_show_options(struct seq_file *, struct vfsmount *);
 243static int  nfs_show_stats(struct seq_file *, struct vfsmount *);
 244static int nfs_get_sb(struct file_system_type *, int, const char *, void *, struct vfsmount *);
 245static int nfs_xdev_get_sb(struct file_system_type *fs_type,
 246                int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 247static void nfs_put_super(struct super_block *);
 248static void nfs_kill_super(struct super_block *);
 249static int nfs_remount(struct super_block *sb, int *flags, char *raw_data);
 250
 251static struct file_system_type nfs_fs_type = {
 252        .owner          = THIS_MODULE,
 253        .name           = "nfs",
 254        .get_sb         = nfs_get_sb,
 255        .kill_sb        = nfs_kill_super,
 256        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 257};
 258
 259struct file_system_type nfs_xdev_fs_type = {
 260        .owner          = THIS_MODULE,
 261        .name           = "nfs",
 262        .get_sb         = nfs_xdev_get_sb,
 263        .kill_sb        = nfs_kill_super,
 264        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 265};
 266
 267static const struct super_operations nfs_sops = {
 268        .alloc_inode    = nfs_alloc_inode,
 269        .destroy_inode  = nfs_destroy_inode,
 270        .write_inode    = nfs_write_inode,
 271        .put_super      = nfs_put_super,
 272        .statfs         = nfs_statfs,
 273        .clear_inode    = nfs_clear_inode,
 274        .umount_begin   = nfs_umount_begin,
 275        .show_options   = nfs_show_options,
 276        .show_stats     = nfs_show_stats,
 277        .remount_fs     = nfs_remount,
 278};
 279
 280#ifdef CONFIG_NFS_V4
 281static int nfs4_validate_text_mount_data(void *options,
 282        struct nfs_parsed_mount_data *args, const char *dev_name);
 283static int nfs4_try_mount(int flags, const char *dev_name,
 284        struct nfs_parsed_mount_data *data, struct vfsmount *mnt);
 285static int nfs4_get_sb(struct file_system_type *fs_type,
 286        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 287static int nfs4_remote_get_sb(struct file_system_type *fs_type,
 288        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 289static int nfs4_xdev_get_sb(struct file_system_type *fs_type,
 290        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 291static int nfs4_referral_get_sb(struct file_system_type *fs_type,
 292        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 293static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
 294        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt);
 295static void nfs4_kill_super(struct super_block *sb);
 296
 297static struct file_system_type nfs4_fs_type = {
 298        .owner          = THIS_MODULE,
 299        .name           = "nfs4",
 300        .get_sb         = nfs4_get_sb,
 301        .kill_sb        = nfs4_kill_super,
 302        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 303};
 304
 305static struct file_system_type nfs4_remote_fs_type = {
 306        .owner          = THIS_MODULE,
 307        .name           = "nfs4",
 308        .get_sb         = nfs4_remote_get_sb,
 309        .kill_sb        = nfs4_kill_super,
 310        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 311};
 312
 313struct file_system_type nfs4_xdev_fs_type = {
 314        .owner          = THIS_MODULE,
 315        .name           = "nfs4",
 316        .get_sb         = nfs4_xdev_get_sb,
 317        .kill_sb        = nfs4_kill_super,
 318        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 319};
 320
 321static struct file_system_type nfs4_remote_referral_fs_type = {
 322        .owner          = THIS_MODULE,
 323        .name           = "nfs4",
 324        .get_sb         = nfs4_remote_referral_get_sb,
 325        .kill_sb        = nfs4_kill_super,
 326        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 327};
 328
 329struct file_system_type nfs4_referral_fs_type = {
 330        .owner          = THIS_MODULE,
 331        .name           = "nfs4",
 332        .get_sb         = nfs4_referral_get_sb,
 333        .kill_sb        = nfs4_kill_super,
 334        .fs_flags       = FS_RENAME_DOES_D_MOVE|FS_REVAL_DOT|FS_BINARY_MOUNTDATA,
 335};
 336
 337static const struct super_operations nfs4_sops = {
 338        .alloc_inode    = nfs_alloc_inode,
 339        .destroy_inode  = nfs_destroy_inode,
 340        .write_inode    = nfs_write_inode,
 341        .put_super      = nfs_put_super,
 342        .statfs         = nfs_statfs,
 343        .clear_inode    = nfs4_clear_inode,
 344        .umount_begin   = nfs_umount_begin,
 345        .show_options   = nfs_show_options,
 346        .show_stats     = nfs_show_stats,
 347        .remount_fs     = nfs_remount,
 348};
 349#endif
 350
 351static struct shrinker acl_shrinker = {
 352        .shrink         = nfs_access_cache_shrinker,
 353        .seeks          = DEFAULT_SEEKS,
 354};
 355
 356/*
 357 * Register the NFS filesystems
 358 */
 359int __init register_nfs_fs(void)
 360{
 361        int ret;
 362
 363        ret = register_filesystem(&nfs_fs_type);
 364        if (ret < 0)
 365                goto error_0;
 366
 367        ret = nfs_register_sysctl();
 368        if (ret < 0)
 369                goto error_1;
 370#ifdef CONFIG_NFS_V4
 371        ret = register_filesystem(&nfs4_fs_type);
 372        if (ret < 0)
 373                goto error_2;
 374#endif
 375        register_shrinker(&acl_shrinker);
 376        return 0;
 377
 378#ifdef CONFIG_NFS_V4
 379error_2:
 380        nfs_unregister_sysctl();
 381#endif
 382error_1:
 383        unregister_filesystem(&nfs_fs_type);
 384error_0:
 385        return ret;
 386}
 387
 388/*
 389 * Unregister the NFS filesystems
 390 */
 391void __exit unregister_nfs_fs(void)
 392{
 393        unregister_shrinker(&acl_shrinker);
 394#ifdef CONFIG_NFS_V4
 395        unregister_filesystem(&nfs4_fs_type);
 396#endif
 397        nfs_unregister_sysctl();
 398        unregister_filesystem(&nfs_fs_type);
 399}
 400
 401void nfs_sb_active(struct super_block *sb)
 402{
 403        struct nfs_server *server = NFS_SB(sb);
 404
 405        if (atomic_inc_return(&server->active) == 1)
 406                atomic_inc(&sb->s_active);
 407}
 408
 409void nfs_sb_deactive(struct super_block *sb)
 410{
 411        struct nfs_server *server = NFS_SB(sb);
 412
 413        if (atomic_dec_and_test(&server->active))
 414                deactivate_super(sb);
 415}
 416
 417/*
 418 * Deliver file system statistics to userspace
 419 */
 420static int nfs_statfs(struct dentry *dentry, struct kstatfs *buf)
 421{
 422        struct nfs_server *server = NFS_SB(dentry->d_sb);
 423        unsigned char blockbits;
 424        unsigned long blockres;
 425        struct nfs_fh *fh = NFS_FH(dentry->d_inode);
 426        struct nfs_fsstat res;
 427        int error = -ENOMEM;
 428
 429        res.fattr = nfs_alloc_fattr();
 430        if (res.fattr == NULL)
 431                goto out_err;
 432
 433        error = server->nfs_client->rpc_ops->statfs(server, fh, &res);
 434        if (unlikely(error == -ESTALE)) {
 435                struct dentry *pd_dentry;
 436
 437                pd_dentry = dget_parent(dentry);
 438                if (pd_dentry != NULL) {
 439                        nfs_zap_caches(pd_dentry->d_inode);
 440                        dput(pd_dentry);
 441                }
 442        }
 443        nfs_free_fattr(res.fattr);
 444        if (error < 0)
 445                goto out_err;
 446
 447        buf->f_type = NFS_SUPER_MAGIC;
 448
 449        /*
 450         * Current versions of glibc do not correctly handle the
 451         * case where f_frsize != f_bsize.  Eventually we want to
 452         * report the value of wtmult in this field.
 453         */
 454        buf->f_frsize = dentry->d_sb->s_blocksize;
 455
 456        /*
 457         * On most *nix systems, f_blocks, f_bfree, and f_bavail
 458         * are reported in units of f_frsize.  Linux hasn't had
 459         * an f_frsize field in its statfs struct until recently,
 460         * thus historically Linux's sys_statfs reports these
 461         * fields in units of f_bsize.
 462         */
 463        buf->f_bsize = dentry->d_sb->s_blocksize;
 464        blockbits = dentry->d_sb->s_blocksize_bits;
 465        blockres = (1 << blockbits) - 1;
 466        buf->f_blocks = (res.tbytes + blockres) >> blockbits;
 467        buf->f_bfree = (res.fbytes + blockres) >> blockbits;
 468        buf->f_bavail = (res.abytes + blockres) >> blockbits;
 469
 470        buf->f_files = res.tfiles;
 471        buf->f_ffree = res.afiles;
 472
 473        buf->f_namelen = server->namelen;
 474
 475        return 0;
 476
 477 out_err:
 478        dprintk("%s: statfs error = %d\n", __func__, -error);
 479        return error;
 480}
 481
 482/*
 483 * Map the security flavour number to a name
 484 */
 485static const char *nfs_pseudoflavour_to_name(rpc_authflavor_t flavour)
 486{
 487        static const struct {
 488                rpc_authflavor_t flavour;
 489                const char *str;
 490        } sec_flavours[] = {
 491                { RPC_AUTH_NULL, "null" },
 492                { RPC_AUTH_UNIX, "sys" },
 493                { RPC_AUTH_GSS_KRB5, "krb5" },
 494                { RPC_AUTH_GSS_KRB5I, "krb5i" },
 495                { RPC_AUTH_GSS_KRB5P, "krb5p" },
 496                { RPC_AUTH_GSS_LKEY, "lkey" },
 497                { RPC_AUTH_GSS_LKEYI, "lkeyi" },
 498                { RPC_AUTH_GSS_LKEYP, "lkeyp" },
 499                { RPC_AUTH_GSS_SPKM, "spkm" },
 500                { RPC_AUTH_GSS_SPKMI, "spkmi" },
 501                { RPC_AUTH_GSS_SPKMP, "spkmp" },
 502                { UINT_MAX, "unknown" }
 503        };
 504        int i;
 505
 506        for (i = 0; sec_flavours[i].flavour != UINT_MAX; i++) {
 507                if (sec_flavours[i].flavour == flavour)
 508                        break;
 509        }
 510        return sec_flavours[i].str;
 511}
 512
 513static void nfs_show_mountd_netid(struct seq_file *m, struct nfs_server *nfss,
 514                                  int showdefaults)
 515{
 516        struct sockaddr *sap = (struct sockaddr *) &nfss->mountd_address;
 517
 518        seq_printf(m, ",mountproto=");
 519        switch (sap->sa_family) {
 520        case AF_INET:
 521                switch (nfss->mountd_protocol) {
 522                case IPPROTO_UDP:
 523                        seq_printf(m, RPCBIND_NETID_UDP);
 524                        break;
 525                case IPPROTO_TCP:
 526                        seq_printf(m, RPCBIND_NETID_TCP);
 527                        break;
 528                default:
 529                        if (showdefaults)
 530                                seq_printf(m, "auto");
 531                }
 532                break;
 533        case AF_INET6:
 534                switch (nfss->mountd_protocol) {
 535                case IPPROTO_UDP:
 536                        seq_printf(m, RPCBIND_NETID_UDP6);
 537                        break;
 538                case IPPROTO_TCP:
 539                        seq_printf(m, RPCBIND_NETID_TCP6);
 540                        break;
 541                default:
 542                        if (showdefaults)
 543                                seq_printf(m, "auto");
 544                }
 545                break;
 546        default:
 547                if (showdefaults)
 548                        seq_printf(m, "auto");
 549        }
 550}
 551
 552static void nfs_show_mountd_options(struct seq_file *m, struct nfs_server *nfss,
 553                                    int showdefaults)
 554{
 555        struct sockaddr *sap = (struct sockaddr *)&nfss->mountd_address;
 556
 557        switch (sap->sa_family) {
 558        case AF_INET: {
 559                struct sockaddr_in *sin = (struct sockaddr_in *)sap;
 560                seq_printf(m, ",mountaddr=%pI4", &sin->sin_addr.s_addr);
 561                break;
 562        }
 563        case AF_INET6: {
 564                struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
 565                seq_printf(m, ",mountaddr=%pI6c", &sin6->sin6_addr);
 566                break;
 567        }
 568        default:
 569                if (showdefaults)
 570                        seq_printf(m, ",mountaddr=unspecified");
 571        }
 572
 573        if (nfss->mountd_version || showdefaults)
 574                seq_printf(m, ",mountvers=%u", nfss->mountd_version);
 575        if (nfss->mountd_port || showdefaults)
 576                seq_printf(m, ",mountport=%u", nfss->mountd_port);
 577
 578        nfs_show_mountd_netid(m, nfss, showdefaults);
 579}
 580
 581#ifdef CONFIG_NFS_V4
 582static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
 583                                    int showdefaults)
 584{
 585        struct nfs_client *clp = nfss->nfs_client;
 586
 587        seq_printf(m, ",clientaddr=%s", clp->cl_ipaddr);
 588        seq_printf(m, ",minorversion=%u", clp->cl_minorversion);
 589}
 590#else
 591static void nfs_show_nfsv4_options(struct seq_file *m, struct nfs_server *nfss,
 592                                    int showdefaults)
 593{
 594}
 595#endif
 596
 597/*
 598 * Describe the mount options in force on this server representation
 599 */
 600static void nfs_show_mount_options(struct seq_file *m, struct nfs_server *nfss,
 601                                   int showdefaults)
 602{
 603        static const struct proc_nfs_info {
 604                int flag;
 605                const char *str;
 606                const char *nostr;
 607        } nfs_info[] = {
 608                { NFS_MOUNT_SOFT, ",soft", ",hard" },
 609                { NFS_MOUNT_POSIX, ",posix", "" },
 610                { NFS_MOUNT_NOCTO, ",nocto", "" },
 611                { NFS_MOUNT_NOAC, ",noac", "" },
 612                { NFS_MOUNT_NONLM, ",nolock", "" },
 613                { NFS_MOUNT_NOACL, ",noacl", "" },
 614                { NFS_MOUNT_NORDIRPLUS, ",nordirplus", "" },
 615                { NFS_MOUNT_UNSHARED, ",nosharecache", "" },
 616                { NFS_MOUNT_NORESVPORT, ",noresvport", "" },
 617                { 0, NULL, NULL }
 618        };
 619        const struct proc_nfs_info *nfs_infop;
 620        struct nfs_client *clp = nfss->nfs_client;
 621        u32 version = clp->rpc_ops->version;
 622
 623        seq_printf(m, ",vers=%u", version);
 624        seq_printf(m, ",rsize=%u", nfss->rsize);
 625        seq_printf(m, ",wsize=%u", nfss->wsize);
 626        if (nfss->bsize != 0)
 627                seq_printf(m, ",bsize=%u", nfss->bsize);
 628        seq_printf(m, ",namlen=%u", nfss->namelen);
 629        if (nfss->acregmin != NFS_DEF_ACREGMIN*HZ || showdefaults)
 630                seq_printf(m, ",acregmin=%u", nfss->acregmin/HZ);
 631        if (nfss->acregmax != NFS_DEF_ACREGMAX*HZ || showdefaults)
 632                seq_printf(m, ",acregmax=%u", nfss->acregmax/HZ);
 633        if (nfss->acdirmin != NFS_DEF_ACDIRMIN*HZ || showdefaults)
 634                seq_printf(m, ",acdirmin=%u", nfss->acdirmin/HZ);
 635        if (nfss->acdirmax != NFS_DEF_ACDIRMAX*HZ || showdefaults)
 636                seq_printf(m, ",acdirmax=%u", nfss->acdirmax/HZ);
 637        for (nfs_infop = nfs_info; nfs_infop->flag; nfs_infop++) {
 638                if (nfss->flags & nfs_infop->flag)
 639                        seq_puts(m, nfs_infop->str);
 640                else
 641                        seq_puts(m, nfs_infop->nostr);
 642        }
 643        seq_printf(m, ",proto=%s",
 644                   rpc_peeraddr2str(nfss->client, RPC_DISPLAY_NETID));
 645        if (version == 4) {
 646                if (nfss->port != NFS_PORT)
 647                        seq_printf(m, ",port=%u", nfss->port);
 648        } else
 649                if (nfss->port)
 650                        seq_printf(m, ",port=%u", nfss->port);
 651
 652        seq_printf(m, ",timeo=%lu", 10U * nfss->client->cl_timeout->to_initval / HZ);
 653        seq_printf(m, ",retrans=%u", nfss->client->cl_timeout->to_retries);
 654        seq_printf(m, ",sec=%s", nfs_pseudoflavour_to_name(nfss->client->cl_auth->au_flavor));
 655
 656        if (version != 4)
 657                nfs_show_mountd_options(m, nfss, showdefaults);
 658        else
 659                nfs_show_nfsv4_options(m, nfss, showdefaults);
 660
 661        if (nfss->options & NFS_OPTION_FSCACHE)
 662                seq_printf(m, ",fsc");
 663
 664        if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) {
 665                if (nfss->flags & NFS_MOUNT_LOOKUP_CACHE_NONE)
 666                        seq_printf(m, ",lookupcache=none");
 667                else
 668                        seq_printf(m, ",lookupcache=pos");
 669        }
 670}
 671
 672/*
 673 * Describe the mount options on this VFS mountpoint
 674 */
 675static int nfs_show_options(struct seq_file *m, struct vfsmount *mnt)
 676{
 677        struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
 678
 679        nfs_show_mount_options(m, nfss, 0);
 680
 681        seq_printf(m, ",addr=%s",
 682                        rpc_peeraddr2str(nfss->nfs_client->cl_rpcclient,
 683                                                        RPC_DISPLAY_ADDR));
 684
 685        return 0;
 686}
 687
 688/*
 689 * Present statistical information for this VFS mountpoint
 690 */
 691static int nfs_show_stats(struct seq_file *m, struct vfsmount *mnt)
 692{
 693        int i, cpu;
 694        struct nfs_server *nfss = NFS_SB(mnt->mnt_sb);
 695        struct rpc_auth *auth = nfss->client->cl_auth;
 696        struct nfs_iostats totals = { };
 697
 698        seq_printf(m, "statvers=%s", NFS_IOSTAT_VERS);
 699
 700        /*
 701         * Display all mount option settings
 702         */
 703        seq_printf(m, "\n\topts:\t");
 704        seq_puts(m, mnt->mnt_sb->s_flags & MS_RDONLY ? "ro" : "rw");
 705        seq_puts(m, mnt->mnt_sb->s_flags & MS_SYNCHRONOUS ? ",sync" : "");
 706        seq_puts(m, mnt->mnt_sb->s_flags & MS_NOATIME ? ",noatime" : "");
 707        seq_puts(m, mnt->mnt_sb->s_flags & MS_NODIRATIME ? ",nodiratime" : "");
 708        nfs_show_mount_options(m, nfss, 1);
 709
 710        seq_printf(m, "\n\tage:\t%lu", (jiffies - nfss->mount_time) / HZ);
 711
 712        seq_printf(m, "\n\tcaps:\t");
 713        seq_printf(m, "caps=0x%x", nfss->caps);
 714        seq_printf(m, ",wtmult=%u", nfss->wtmult);
 715        seq_printf(m, ",dtsize=%u", nfss->dtsize);
 716        seq_printf(m, ",bsize=%u", nfss->bsize);
 717        seq_printf(m, ",namlen=%u", nfss->namelen);
 718
 719#ifdef CONFIG_NFS_V4
 720        if (nfss->nfs_client->rpc_ops->version == 4) {
 721                seq_printf(m, "\n\tnfsv4:\t");
 722                seq_printf(m, "bm0=0x%x", nfss->attr_bitmask[0]);
 723                seq_printf(m, ",bm1=0x%x", nfss->attr_bitmask[1]);
 724                seq_printf(m, ",acl=0x%x", nfss->acl_bitmask);
 725        }
 726#endif
 727
 728        /*
 729         * Display security flavor in effect for this mount
 730         */
 731        seq_printf(m, "\n\tsec:\tflavor=%u", auth->au_ops->au_flavor);
 732        if (auth->au_flavor)
 733                seq_printf(m, ",pseudoflavor=%u", auth->au_flavor);
 734
 735        /*
 736         * Display superblock I/O counters
 737         */
 738        for_each_possible_cpu(cpu) {
 739                struct nfs_iostats *stats;
 740
 741                preempt_disable();
 742                stats = per_cpu_ptr(nfss->io_stats, cpu);
 743
 744                for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
 745                        totals.events[i] += stats->events[i];
 746                for (i = 0; i < __NFSIOS_BYTESMAX; i++)
 747                        totals.bytes[i] += stats->bytes[i];
 748#ifdef CONFIG_NFS_FSCACHE
 749                for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
 750                        totals.fscache[i] += stats->fscache[i];
 751#endif
 752
 753                preempt_enable();
 754        }
 755
 756        seq_printf(m, "\n\tevents:\t");
 757        for (i = 0; i < __NFSIOS_COUNTSMAX; i++)
 758                seq_printf(m, "%lu ", totals.events[i]);
 759        seq_printf(m, "\n\tbytes:\t");
 760        for (i = 0; i < __NFSIOS_BYTESMAX; i++)
 761                seq_printf(m, "%Lu ", totals.bytes[i]);
 762#ifdef CONFIG_NFS_FSCACHE
 763        if (nfss->options & NFS_OPTION_FSCACHE) {
 764                seq_printf(m, "\n\tfsc:\t");
 765                for (i = 0; i < __NFSIOS_FSCACHEMAX; i++)
 766                        seq_printf(m, "%Lu ", totals.bytes[i]);
 767        }
 768#endif
 769        seq_printf(m, "\n");
 770
 771        rpc_print_iostats(m, nfss->client);
 772
 773        return 0;
 774}
 775
 776/*
 777 * Begin unmount by attempting to remove all automounted mountpoints we added
 778 * in response to xdev traversals and referrals
 779 */
 780static void nfs_umount_begin(struct super_block *sb)
 781{
 782        struct nfs_server *server;
 783        struct rpc_clnt *rpc;
 784
 785        server = NFS_SB(sb);
 786        /* -EIO all pending I/O */
 787        rpc = server->client_acl;
 788        if (!IS_ERR(rpc))
 789                rpc_killall_tasks(rpc);
 790        rpc = server->client;
 791        if (!IS_ERR(rpc))
 792                rpc_killall_tasks(rpc);
 793}
 794
 795static struct nfs_parsed_mount_data *nfs_alloc_parsed_mount_data(unsigned int version)
 796{
 797        struct nfs_parsed_mount_data *data;
 798
 799        data = kzalloc(sizeof(*data), GFP_KERNEL);
 800        if (data) {
 801                data->acregmin          = NFS_DEF_ACREGMIN;
 802                data->acregmax          = NFS_DEF_ACREGMAX;
 803                data->acdirmin          = NFS_DEF_ACDIRMIN;
 804                data->acdirmax          = NFS_DEF_ACDIRMAX;
 805                data->mount_server.port = NFS_UNSPEC_PORT;
 806                data->nfs_server.port   = NFS_UNSPEC_PORT;
 807                data->nfs_server.protocol = XPRT_TRANSPORT_TCP;
 808                data->auth_flavors[0]   = RPC_AUTH_UNIX;
 809                data->auth_flavor_len   = 1;
 810                data->version           = version;
 811                data->minorversion      = 0;
 812        }
 813        return data;
 814}
 815
 816/*
 817 * Sanity-check a server address provided by the mount command.
 818 *
 819 * Address family must be initialized, and address must not be
 820 * the ANY address for that family.
 821 */
 822static int nfs_verify_server_address(struct sockaddr *addr)
 823{
 824        switch (addr->sa_family) {
 825        case AF_INET: {
 826                struct sockaddr_in *sa = (struct sockaddr_in *)addr;
 827                return sa->sin_addr.s_addr != htonl(INADDR_ANY);
 828        }
 829        case AF_INET6: {
 830                struct in6_addr *sa = &((struct sockaddr_in6 *)addr)->sin6_addr;
 831                return !ipv6_addr_any(sa);
 832        }
 833        }
 834
 835        dfprintk(MOUNT, "NFS: Invalid IP address specified\n");
 836        return 0;
 837}
 838
 839/*
 840 * Select between a default port value and a user-specified port value.
 841 * If a zero value is set, then autobind will be used.
 842 */
 843static void nfs_set_port(struct sockaddr *sap, int *port,
 844                                 const unsigned short default_port)
 845{
 846        if (*port == NFS_UNSPEC_PORT)
 847                *port = default_port;
 848
 849        rpc_set_port(sap, *port);
 850}
 851
 852/*
 853 * Sanity check the NFS transport protocol.
 854 *
 855 */
 856static void nfs_validate_transport_protocol(struct nfs_parsed_mount_data *mnt)
 857{
 858        switch (mnt->nfs_server.protocol) {
 859        case XPRT_TRANSPORT_UDP:
 860        case XPRT_TRANSPORT_TCP:
 861        case XPRT_TRANSPORT_RDMA:
 862                break;
 863        default:
 864                mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
 865        }
 866}
 867
 868/*
 869 * For text based NFSv2/v3 mounts, the mount protocol transport default
 870 * settings should depend upon the specified NFS transport.
 871 */
 872static void nfs_set_mount_transport_protocol(struct nfs_parsed_mount_data *mnt)
 873{
 874        nfs_validate_transport_protocol(mnt);
 875
 876        if (mnt->mount_server.protocol == XPRT_TRANSPORT_UDP ||
 877            mnt->mount_server.protocol == XPRT_TRANSPORT_TCP)
 878                        return;
 879        switch (mnt->nfs_server.protocol) {
 880        case XPRT_TRANSPORT_UDP:
 881                mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
 882                break;
 883        case XPRT_TRANSPORT_TCP:
 884        case XPRT_TRANSPORT_RDMA:
 885                mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
 886        }
 887}
 888
 889/*
 890 * Parse the value of the 'sec=' option.
 891 */
 892static int nfs_parse_security_flavors(char *value,
 893                                      struct nfs_parsed_mount_data *mnt)
 894{
 895        substring_t args[MAX_OPT_ARGS];
 896
 897        dfprintk(MOUNT, "NFS: parsing sec=%s option\n", value);
 898
 899        switch (match_token(value, nfs_secflavor_tokens, args)) {
 900        case Opt_sec_none:
 901                mnt->auth_flavors[0] = RPC_AUTH_NULL;
 902                break;
 903        case Opt_sec_sys:
 904                mnt->auth_flavors[0] = RPC_AUTH_UNIX;
 905                break;
 906        case Opt_sec_krb5:
 907                mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5;
 908                break;
 909        case Opt_sec_krb5i:
 910                mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5I;
 911                break;
 912        case Opt_sec_krb5p:
 913                mnt->auth_flavors[0] = RPC_AUTH_GSS_KRB5P;
 914                break;
 915        case Opt_sec_lkey:
 916                mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEY;
 917                break;
 918        case Opt_sec_lkeyi:
 919                mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYI;
 920                break;
 921        case Opt_sec_lkeyp:
 922                mnt->auth_flavors[0] = RPC_AUTH_GSS_LKEYP;
 923                break;
 924        case Opt_sec_spkm:
 925                mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKM;
 926                break;
 927        case Opt_sec_spkmi:
 928                mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMI;
 929                break;
 930        case Opt_sec_spkmp:
 931                mnt->auth_flavors[0] = RPC_AUTH_GSS_SPKMP;
 932                break;
 933        default:
 934                return 0;
 935        }
 936
 937        mnt->auth_flavor_len = 1;
 938        return 1;
 939}
 940
 941/*
 942 * Error-check and convert a string of mount options from user space into
 943 * a data structure.  The whole mount string is processed; bad options are
 944 * skipped as they are encountered.  If there were no errors, return 1;
 945 * otherwise return 0 (zero).
 946 */
 947static int nfs_parse_mount_options(char *raw,
 948                                   struct nfs_parsed_mount_data *mnt)
 949{
 950        char *p, *string, *secdata;
 951        int rc, sloppy = 0, invalid_option = 0;
 952        unsigned short protofamily = AF_UNSPEC;
 953        unsigned short mountfamily = AF_UNSPEC;
 954
 955        if (!raw) {
 956                dfprintk(MOUNT, "NFS: mount options string was NULL.\n");
 957                return 1;
 958        }
 959        dfprintk(MOUNT, "NFS: nfs mount opts='%s'\n", raw);
 960
 961        secdata = alloc_secdata();
 962        if (!secdata)
 963                goto out_nomem;
 964
 965        rc = security_sb_copy_data(raw, secdata);
 966        if (rc)
 967                goto out_security_failure;
 968
 969        rc = security_sb_parse_opts_str(secdata, &mnt->lsm_opts);
 970        if (rc)
 971                goto out_security_failure;
 972
 973        free_secdata(secdata);
 974
 975        while ((p = strsep(&raw, ",")) != NULL) {
 976                substring_t args[MAX_OPT_ARGS];
 977                unsigned long option;
 978                int token;
 979
 980                if (!*p)
 981                        continue;
 982
 983                dfprintk(MOUNT, "NFS:   parsing nfs mount option '%s'\n", p);
 984
 985                token = match_token(p, nfs_mount_option_tokens, args);
 986                switch (token) {
 987
 988                /*
 989                 * boolean options:  foo/nofoo
 990                 */
 991                case Opt_soft:
 992                        mnt->flags |= NFS_MOUNT_SOFT;
 993                        break;
 994                case Opt_hard:
 995                        mnt->flags &= ~NFS_MOUNT_SOFT;
 996                        break;
 997                case Opt_posix:
 998                        mnt->flags |= NFS_MOUNT_POSIX;
 999                        break;
1000                case Opt_noposix:
1001                        mnt->flags &= ~NFS_MOUNT_POSIX;
1002                        break;
1003                case Opt_cto:
1004                        mnt->flags &= ~NFS_MOUNT_NOCTO;
1005                        break;
1006                case Opt_nocto:
1007                        mnt->flags |= NFS_MOUNT_NOCTO;
1008                        break;
1009                case Opt_ac:
1010                        mnt->flags &= ~NFS_MOUNT_NOAC;
1011                        break;
1012                case Opt_noac:
1013                        mnt->flags |= NFS_MOUNT_NOAC;
1014                        break;
1015                case Opt_lock:
1016                        mnt->flags &= ~NFS_MOUNT_NONLM;
1017                        break;
1018                case Opt_nolock:
1019                        mnt->flags |= NFS_MOUNT_NONLM;
1020                        break;
1021                case Opt_v2:
1022                        mnt->flags &= ~NFS_MOUNT_VER3;
1023                        mnt->version = 2;
1024                        break;
1025                case Opt_v3:
1026                        mnt->flags |= NFS_MOUNT_VER3;
1027                        mnt->version = 3;
1028                        break;
1029#ifdef CONFIG_NFS_V4
1030                case Opt_v4:
1031                        mnt->flags &= ~NFS_MOUNT_VER3;
1032                        mnt->version = 4;
1033                        break;
1034#endif
1035                case Opt_udp:
1036                        mnt->flags &= ~NFS_MOUNT_TCP;
1037                        mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1038                        break;
1039                case Opt_tcp:
1040                        mnt->flags |= NFS_MOUNT_TCP;
1041                        mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1042                        break;
1043                case Opt_rdma:
1044                        mnt->flags |= NFS_MOUNT_TCP; /* for side protocols */
1045                        mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1046                        xprt_load_transport(p);
1047                        break;
1048                case Opt_acl:
1049                        mnt->flags &= ~NFS_MOUNT_NOACL;
1050                        break;
1051                case Opt_noacl:
1052                        mnt->flags |= NFS_MOUNT_NOACL;
1053                        break;
1054                case Opt_rdirplus:
1055                        mnt->flags &= ~NFS_MOUNT_NORDIRPLUS;
1056                        break;
1057                case Opt_nordirplus:
1058                        mnt->flags |= NFS_MOUNT_NORDIRPLUS;
1059                        break;
1060                case Opt_sharecache:
1061                        mnt->flags &= ~NFS_MOUNT_UNSHARED;
1062                        break;
1063                case Opt_nosharecache:
1064                        mnt->flags |= NFS_MOUNT_UNSHARED;
1065                        break;
1066                case Opt_resvport:
1067                        mnt->flags &= ~NFS_MOUNT_NORESVPORT;
1068                        break;
1069                case Opt_noresvport:
1070                        mnt->flags |= NFS_MOUNT_NORESVPORT;
1071                        break;
1072                case Opt_fscache:
1073                        mnt->options |= NFS_OPTION_FSCACHE;
1074                        kfree(mnt->fscache_uniq);
1075                        mnt->fscache_uniq = NULL;
1076                        break;
1077                case Opt_nofscache:
1078                        mnt->options &= ~NFS_OPTION_FSCACHE;
1079                        kfree(mnt->fscache_uniq);
1080                        mnt->fscache_uniq = NULL;
1081                        break;
1082
1083                /*
1084                 * options that take numeric values
1085                 */
1086                case Opt_port:
1087                        string = match_strdup(args);
1088                        if (string == NULL)
1089                                goto out_nomem;
1090                        rc = strict_strtoul(string, 10, &option);
1091                        kfree(string);
1092                        if (rc != 0 || option > USHRT_MAX)
1093                                goto out_invalid_value;
1094                        mnt->nfs_server.port = option;
1095                        break;
1096                case Opt_rsize:
1097                        string = match_strdup(args);
1098                        if (string == NULL)
1099                                goto out_nomem;
1100                        rc = strict_strtoul(string, 10, &option);
1101                        kfree(string);
1102                        if (rc != 0)
1103                                goto out_invalid_value;
1104                        mnt->rsize = option;
1105                        break;
1106                case Opt_wsize:
1107                        string = match_strdup(args);
1108                        if (string == NULL)
1109                                goto out_nomem;
1110                        rc = strict_strtoul(string, 10, &option);
1111                        kfree(string);
1112                        if (rc != 0)
1113                                goto out_invalid_value;
1114                        mnt->wsize = option;
1115                        break;
1116                case Opt_bsize:
1117                        string = match_strdup(args);
1118                        if (string == NULL)
1119                                goto out_nomem;
1120                        rc = strict_strtoul(string, 10, &option);
1121                        kfree(string);
1122                        if (rc != 0)
1123                                goto out_invalid_value;
1124                        mnt->bsize = option;
1125                        break;
1126                case Opt_timeo:
1127                        string = match_strdup(args);
1128                        if (string == NULL)
1129                                goto out_nomem;
1130                        rc = strict_strtoul(string, 10, &option);
1131                        kfree(string);
1132                        if (rc != 0 || option == 0)
1133                                goto out_invalid_value;
1134                        mnt->timeo = option;
1135                        break;
1136                case Opt_retrans:
1137                        string = match_strdup(args);
1138                        if (string == NULL)
1139                                goto out_nomem;
1140                        rc = strict_strtoul(string, 10, &option);
1141                        kfree(string);
1142                        if (rc != 0 || option == 0)
1143                                goto out_invalid_value;
1144                        mnt->retrans = option;
1145                        break;
1146                case Opt_acregmin:
1147                        string = match_strdup(args);
1148                        if (string == NULL)
1149                                goto out_nomem;
1150                        rc = strict_strtoul(string, 10, &option);
1151                        kfree(string);
1152                        if (rc != 0)
1153                                goto out_invalid_value;
1154                        mnt->acregmin = option;
1155                        break;
1156                case Opt_acregmax:
1157                        string = match_strdup(args);
1158                        if (string == NULL)
1159                                goto out_nomem;
1160                        rc = strict_strtoul(string, 10, &option);
1161                        kfree(string);
1162                        if (rc != 0)
1163                                goto out_invalid_value;
1164                        mnt->acregmax = option;
1165                        break;
1166                case Opt_acdirmin:
1167                        string = match_strdup(args);
1168                        if (string == NULL)
1169                                goto out_nomem;
1170                        rc = strict_strtoul(string, 10, &option);
1171                        kfree(string);
1172                        if (rc != 0)
1173                                goto out_invalid_value;
1174                        mnt->acdirmin = option;
1175                        break;
1176                case Opt_acdirmax:
1177                        string = match_strdup(args);
1178                        if (string == NULL)
1179                                goto out_nomem;
1180                        rc = strict_strtoul(string, 10, &option);
1181                        kfree(string);
1182                        if (rc != 0)
1183                                goto out_invalid_value;
1184                        mnt->acdirmax = option;
1185                        break;
1186                case Opt_actimeo:
1187                        string = match_strdup(args);
1188                        if (string == NULL)
1189                                goto out_nomem;
1190                        rc = strict_strtoul(string, 10, &option);
1191                        kfree(string);
1192                        if (rc != 0)
1193                                goto out_invalid_value;
1194                        mnt->acregmin = mnt->acregmax =
1195                        mnt->acdirmin = mnt->acdirmax = option;
1196                        break;
1197                case Opt_namelen:
1198                        string = match_strdup(args);
1199                        if (string == NULL)
1200                                goto out_nomem;
1201                        rc = strict_strtoul(string, 10, &option);
1202                        kfree(string);
1203                        if (rc != 0)
1204                                goto out_invalid_value;
1205                        mnt->namlen = option;
1206                        break;
1207                case Opt_mountport:
1208                        string = match_strdup(args);
1209                        if (string == NULL)
1210                                goto out_nomem;
1211                        rc = strict_strtoul(string, 10, &option);
1212                        kfree(string);
1213                        if (rc != 0 || option > USHRT_MAX)
1214                                goto out_invalid_value;
1215                        mnt->mount_server.port = option;
1216                        break;
1217                case Opt_mountvers:
1218                        string = match_strdup(args);
1219                        if (string == NULL)
1220                                goto out_nomem;
1221                        rc = strict_strtoul(string, 10, &option);
1222                        kfree(string);
1223                        if (rc != 0 ||
1224                            option < NFS_MNT_VERSION ||
1225                            option > NFS_MNT3_VERSION)
1226                                goto out_invalid_value;
1227                        mnt->mount_server.version = option;
1228                        break;
1229                case Opt_nfsvers:
1230                        string = match_strdup(args);
1231                        if (string == NULL)
1232                                goto out_nomem;
1233                        rc = strict_strtoul(string, 10, &option);
1234                        kfree(string);
1235                        if (rc != 0)
1236                                goto out_invalid_value;
1237                        switch (option) {
1238                        case NFS2_VERSION:
1239                                mnt->flags &= ~NFS_MOUNT_VER3;
1240                                mnt->version = 2;
1241                                break;
1242                        case NFS3_VERSION:
1243                                mnt->flags |= NFS_MOUNT_VER3;
1244                                mnt->version = 3;
1245                                break;
1246#ifdef CONFIG_NFS_V4
1247                        case NFS4_VERSION:
1248                                mnt->flags &= ~NFS_MOUNT_VER3;
1249                                mnt->version = 4;
1250                                break;
1251#endif
1252                        default:
1253                                goto out_invalid_value;
1254                        }
1255                        break;
1256                case Opt_minorversion:
1257                        string = match_strdup(args);
1258                        if (string == NULL)
1259                                goto out_nomem;
1260                        rc = strict_strtoul(string, 10, &option);
1261                        kfree(string);
1262                        if (rc != 0)
1263                                goto out_invalid_value;
1264                        if (option > NFS4_MAX_MINOR_VERSION)
1265                                goto out_invalid_value;
1266                        mnt->minorversion = option;
1267                        break;
1268
1269                /*
1270                 * options that take text values
1271                 */
1272                case Opt_sec:
1273                        string = match_strdup(args);
1274                        if (string == NULL)
1275                                goto out_nomem;
1276                        rc = nfs_parse_security_flavors(string, mnt);
1277                        kfree(string);
1278                        if (!rc) {
1279                                dfprintk(MOUNT, "NFS:   unrecognized "
1280                                                "security flavor\n");
1281                                return 0;
1282                        }
1283                        break;
1284                case Opt_proto:
1285                        string = match_strdup(args);
1286                        if (string == NULL)
1287                                goto out_nomem;
1288                        token = match_token(string,
1289                                            nfs_xprt_protocol_tokens, args);
1290
1291                        protofamily = AF_INET;
1292                        switch (token) {
1293                        case Opt_xprt_udp6:
1294                                protofamily = AF_INET6;
1295                        case Opt_xprt_udp:
1296                                mnt->flags &= ~NFS_MOUNT_TCP;
1297                                mnt->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1298                                kfree(string);
1299                                break;
1300                        case Opt_xprt_tcp6:
1301                                protofamily = AF_INET6;
1302                        case Opt_xprt_tcp:
1303                                mnt->flags |= NFS_MOUNT_TCP;
1304                                mnt->nfs_server.protocol = XPRT_TRANSPORT_TCP;
1305                                kfree(string);
1306                                break;
1307                        case Opt_xprt_rdma:
1308                                /* vector side protocols to TCP */
1309                                mnt->flags |= NFS_MOUNT_TCP;
1310                                mnt->nfs_server.protocol = XPRT_TRANSPORT_RDMA;
1311                                xprt_load_transport(string);
1312                                kfree(string);
1313                                break;
1314                        default:
1315                                dfprintk(MOUNT, "NFS:   unrecognized "
1316                                                "transport protocol\n");
1317                                kfree(string);
1318                                return 0;
1319                        }
1320                        break;
1321                case Opt_mountproto:
1322                        string = match_strdup(args);
1323                        if (string == NULL)
1324                                goto out_nomem;
1325                        token = match_token(string,
1326                                            nfs_xprt_protocol_tokens, args);
1327                        kfree(string);
1328
1329                        mountfamily = AF_INET;
1330                        switch (token) {
1331                        case Opt_xprt_udp6:
1332                                mountfamily = AF_INET6;
1333                        case Opt_xprt_udp:
1334                                mnt->mount_server.protocol = XPRT_TRANSPORT_UDP;
1335                                break;
1336                        case Opt_xprt_tcp6:
1337                                mountfamily = AF_INET6;
1338                        case Opt_xprt_tcp:
1339                                mnt->mount_server.protocol = XPRT_TRANSPORT_TCP;
1340                                break;
1341                        case Opt_xprt_rdma: /* not used for side protocols */
1342                        default:
1343                                dfprintk(MOUNT, "NFS:   unrecognized "
1344                                                "transport protocol\n");
1345                                return 0;
1346                        }
1347                        break;
1348                case Opt_addr:
1349                        string = match_strdup(args);
1350                        if (string == NULL)
1351                                goto out_nomem;
1352                        mnt->nfs_server.addrlen =
1353                                rpc_pton(string, strlen(string),
1354                                        (struct sockaddr *)
1355                                        &mnt->nfs_server.address,
1356                                        sizeof(mnt->nfs_server.address));
1357                        kfree(string);
1358                        if (mnt->nfs_server.addrlen == 0)
1359                                goto out_invalid_address;
1360                        break;
1361                case Opt_clientaddr:
1362                        string = match_strdup(args);
1363                        if (string == NULL)
1364                                goto out_nomem;
1365                        kfree(mnt->client_address);
1366                        mnt->client_address = string;
1367                        break;
1368                case Opt_mounthost:
1369                        string = match_strdup(args);
1370                        if (string == NULL)
1371                                goto out_nomem;
1372                        kfree(mnt->mount_server.hostname);
1373                        mnt->mount_server.hostname = string;
1374                        break;
1375                case Opt_mountaddr:
1376                        string = match_strdup(args);
1377                        if (string == NULL)
1378                                goto out_nomem;
1379                        mnt->mount_server.addrlen =
1380                                rpc_pton(string, strlen(string),
1381                                        (struct sockaddr *)
1382                                        &mnt->mount_server.address,
1383                                        sizeof(mnt->mount_server.address));
1384                        kfree(string);
1385                        if (mnt->mount_server.addrlen == 0)
1386                                goto out_invalid_address;
1387                        break;
1388                case Opt_lookupcache:
1389                        string = match_strdup(args);
1390                        if (string == NULL)
1391                                goto out_nomem;
1392                        token = match_token(string,
1393                                        nfs_lookupcache_tokens, args);
1394                        kfree(string);
1395                        switch (token) {
1396                                case Opt_lookupcache_all:
1397                                        mnt->flags &= ~(NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE);
1398                                        break;
1399                                case Opt_lookupcache_positive:
1400                                        mnt->flags &= ~NFS_MOUNT_LOOKUP_CACHE_NONE;
1401                                        mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG;
1402                                        break;
1403                                case Opt_lookupcache_none:
1404                                        mnt->flags |= NFS_MOUNT_LOOKUP_CACHE_NONEG|NFS_MOUNT_LOOKUP_CACHE_NONE;
1405                                        break;
1406                                default:
1407                                        dfprintk(MOUNT, "NFS:   invalid "
1408                                                        "lookupcache argument\n");
1409                                        return 0;
1410                        };
1411                        break;
1412                case Opt_fscache_uniq:
1413                        string = match_strdup(args);
1414                        if (string == NULL)
1415                                goto out_nomem;
1416                        kfree(mnt->fscache_uniq);
1417                        mnt->fscache_uniq = string;
1418                        mnt->options |= NFS_OPTION_FSCACHE;
1419                        break;
1420
1421                /*
1422                 * Special options
1423                 */
1424                case Opt_sloppy:
1425                        sloppy = 1;
1426                        dfprintk(MOUNT, "NFS:   relaxing parsing rules\n");
1427                        break;
1428                case Opt_userspace:
1429                case Opt_deprecated:
1430                        dfprintk(MOUNT, "NFS:   ignoring mount option "
1431                                        "'%s'\n", p);
1432                        break;
1433
1434                default:
1435                        invalid_option = 1;
1436                        dfprintk(MOUNT, "NFS:   unrecognized mount option "
1437                                        "'%s'\n", p);
1438                }
1439        }
1440
1441        if (!sloppy && invalid_option)
1442                return 0;
1443
1444        /*
1445         * verify that any proto=/mountproto= options match the address
1446         * familiies in the addr=/mountaddr= options.
1447         */
1448        if (protofamily != AF_UNSPEC &&
1449            protofamily != mnt->nfs_server.address.ss_family)
1450                goto out_proto_mismatch;
1451
1452        if (mountfamily != AF_UNSPEC) {
1453                if (mnt->mount_server.addrlen) {
1454                        if (mountfamily != mnt->mount_server.address.ss_family)
1455                                goto out_mountproto_mismatch;
1456                } else {
1457                        if (mountfamily != mnt->nfs_server.address.ss_family)
1458                                goto out_mountproto_mismatch;
1459                }
1460        }
1461
1462        return 1;
1463
1464out_mountproto_mismatch:
1465        printk(KERN_INFO "NFS: mount server address does not match mountproto= "
1466                         "option\n");
1467        return 0;
1468out_proto_mismatch:
1469        printk(KERN_INFO "NFS: server address does not match proto= option\n");
1470        return 0;
1471out_invalid_address:
1472        printk(KERN_INFO "NFS: bad IP address specified: %s\n", p);
1473        return 0;
1474out_invalid_value:
1475        printk(KERN_INFO "NFS: bad mount option value specified: %s\n", p);
1476        return 0;
1477out_nomem:
1478        printk(KERN_INFO "NFS: not enough memory to parse option\n");
1479        return 0;
1480out_security_failure:
1481        free_secdata(secdata);
1482        printk(KERN_INFO "NFS: security options invalid: %d\n", rc);
1483        return 0;
1484}
1485
1486/*
1487 * Match the requested auth flavors with the list returned by
1488 * the server.  Returns zero and sets the mount's authentication
1489 * flavor on success; returns -EACCES if server does not support
1490 * the requested flavor.
1491 */
1492static int nfs_walk_authlist(struct nfs_parsed_mount_data *args,
1493                             struct nfs_mount_request *request)
1494{
1495        unsigned int i, j, server_authlist_len = *(request->auth_flav_len);
1496
1497        /*
1498         * Certain releases of Linux's mountd return an empty
1499         * flavor list.  To prevent behavioral regression with
1500         * these servers (ie. rejecting mounts that used to
1501         * succeed), revert to pre-2.6.32 behavior (no checking)
1502         * if the returned flavor list is empty.
1503         */
1504        if (server_authlist_len == 0)
1505                return 0;
1506
1507        /*
1508         * We avoid sophisticated negotiating here, as there are
1509         * plenty of cases where we can get it wrong, providing
1510         * either too little or too much security.
1511         *
1512         * RFC 2623, section 2.7 suggests we SHOULD prefer the
1513         * flavor listed first.  However, some servers list
1514         * AUTH_NULL first.  Our caller plants AUTH_SYS, the
1515         * preferred default, in args->auth_flavors[0] if user
1516         * didn't specify sec= mount option.
1517         */
1518        for (i = 0; i < args->auth_flavor_len; i++)
1519                for (j = 0; j < server_authlist_len; j++)
1520                        if (args->auth_flavors[i] == request->auth_flavs[j]) {
1521                                dfprintk(MOUNT, "NFS: using auth flavor %d\n",
1522                                        request->auth_flavs[j]);
1523                                args->auth_flavors[0] = request->auth_flavs[j];
1524                                return 0;
1525                        }
1526
1527        dfprintk(MOUNT, "NFS: server does not support requested auth flavor\n");
1528        nfs_umount(request);
1529        return -EACCES;
1530}
1531
1532/*
1533 * Use the remote server's MOUNT service to request the NFS file handle
1534 * corresponding to the provided path.
1535 */
1536static int nfs_try_mount(struct nfs_parsed_mount_data *args,
1537                         struct nfs_fh *root_fh)
1538{
1539        rpc_authflavor_t server_authlist[NFS_MAX_SECFLAVORS];
1540        unsigned int server_authlist_len = ARRAY_SIZE(server_authlist);
1541        struct nfs_mount_request request = {
1542                .sap            = (struct sockaddr *)
1543                                                &args->mount_server.address,
1544                .dirpath        = args->nfs_server.export_path,
1545                .protocol       = args->mount_server.protocol,
1546                .fh             = root_fh,
1547                .noresvport     = args->flags & NFS_MOUNT_NORESVPORT,
1548                .auth_flav_len  = &server_authlist_len,
1549                .auth_flavs     = server_authlist,
1550        };
1551        int status;
1552
1553        if (args->mount_server.version == 0) {
1554                switch (args->version) {
1555                        default:
1556                                args->mount_server.version = NFS_MNT3_VERSION;
1557                                break;
1558                        case 2:
1559                                args->mount_server.version = NFS_MNT_VERSION;
1560                }
1561        }
1562        request.version = args->mount_server.version;
1563
1564        if (args->mount_server.hostname)
1565                request.hostname = args->mount_server.hostname;
1566        else
1567                request.hostname = args->nfs_server.hostname;
1568
1569        /*
1570         * Construct the mount server's address.
1571         */
1572        if (args->mount_server.address.ss_family == AF_UNSPEC) {
1573                memcpy(request.sap, &args->nfs_server.address,
1574                       args->nfs_server.addrlen);
1575                args->mount_server.addrlen = args->nfs_server.addrlen;
1576        }
1577        request.salen = args->mount_server.addrlen;
1578        nfs_set_port(request.sap, &args->mount_server.port, 0);
1579
1580        /*
1581         * Now ask the mount server to map our export path
1582         * to a file handle.
1583         */
1584        status = nfs_mount(&request);
1585        if (status != 0) {
1586                dfprintk(MOUNT, "NFS: unable to mount server %s, error %d\n",
1587                                request.hostname, status);
1588                return status;
1589        }
1590
1591        /*
1592         * MNTv1 (NFSv2) does not support auth flavor negotiation.
1593         */
1594        if (args->mount_server.version != NFS_MNT3_VERSION)
1595                return 0;
1596        return nfs_walk_authlist(args, &request);
1597}
1598
1599static int nfs_parse_simple_hostname(const char *dev_name,
1600                                     char **hostname, size_t maxnamlen,
1601                                     char **export_path, size_t maxpathlen)
1602{
1603        size_t len;
1604        char *colon, *comma;
1605
1606        colon = strchr(dev_name, ':');
1607        if (colon == NULL)
1608                goto out_bad_devname;
1609
1610        len = colon - dev_name;
1611        if (len > maxnamlen)
1612                goto out_hostname;
1613
1614        /* N.B. caller will free nfs_server.hostname in all cases */
1615        *hostname = kstrndup(dev_name, len, GFP_KERNEL);
1616        if (!*hostname)
1617                goto out_nomem;
1618
1619        /* kill possible hostname list: not supported */
1620        comma = strchr(*hostname, ',');
1621        if (comma != NULL) {
1622                if (comma == *hostname)
1623                        goto out_bad_devname;
1624                *comma = '\0';
1625        }
1626
1627        colon++;
1628        len = strlen(colon);
1629        if (len > maxpathlen)
1630                goto out_path;
1631        *export_path = kstrndup(colon, len, GFP_KERNEL);
1632        if (!*export_path)
1633                goto out_nomem;
1634
1635        dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", *export_path);
1636        return 0;
1637
1638out_bad_devname:
1639        dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1640        return -EINVAL;
1641
1642out_nomem:
1643        dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1644        return -ENOMEM;
1645
1646out_hostname:
1647        dfprintk(MOUNT, "NFS: server hostname too long\n");
1648        return -ENAMETOOLONG;
1649
1650out_path:
1651        dfprintk(MOUNT, "NFS: export pathname too long\n");
1652        return -ENAMETOOLONG;
1653}
1654
1655/*
1656 * Hostname has square brackets around it because it contains one or
1657 * more colons.  We look for the first closing square bracket, and a
1658 * colon must follow it.
1659 */
1660static int nfs_parse_protected_hostname(const char *dev_name,
1661                                        char **hostname, size_t maxnamlen,
1662                                        char **export_path, size_t maxpathlen)
1663{
1664        size_t len;
1665        char *start, *end;
1666
1667        start = (char *)(dev_name + 1);
1668
1669        end = strchr(start, ']');
1670        if (end == NULL)
1671                goto out_bad_devname;
1672        if (*(end + 1) != ':')
1673                goto out_bad_devname;
1674
1675        len = end - start;
1676        if (len > maxnamlen)
1677                goto out_hostname;
1678
1679        /* N.B. caller will free nfs_server.hostname in all cases */
1680        *hostname = kstrndup(start, len, GFP_KERNEL);
1681        if (*hostname == NULL)
1682                goto out_nomem;
1683
1684        end += 2;
1685        len = strlen(end);
1686        if (len > maxpathlen)
1687                goto out_path;
1688        *export_path = kstrndup(end, len, GFP_KERNEL);
1689        if (!*export_path)
1690                goto out_nomem;
1691
1692        return 0;
1693
1694out_bad_devname:
1695        dfprintk(MOUNT, "NFS: device name not in host:path format\n");
1696        return -EINVAL;
1697
1698out_nomem:
1699        dfprintk(MOUNT, "NFS: not enough memory to parse device name\n");
1700        return -ENOMEM;
1701
1702out_hostname:
1703        dfprintk(MOUNT, "NFS: server hostname too long\n");
1704        return -ENAMETOOLONG;
1705
1706out_path:
1707        dfprintk(MOUNT, "NFS: export pathname too long\n");
1708        return -ENAMETOOLONG;
1709}
1710
1711/*
1712 * Split "dev_name" into "hostname:export_path".
1713 *
1714 * The leftmost colon demarks the split between the server's hostname
1715 * and the export path.  If the hostname starts with a left square
1716 * bracket, then it may contain colons.
1717 *
1718 * Note: caller frees hostname and export path, even on error.
1719 */
1720static int nfs_parse_devname(const char *dev_name,
1721                             char **hostname, size_t maxnamlen,
1722                             char **export_path, size_t maxpathlen)
1723{
1724        if (*dev_name == '[')
1725                return nfs_parse_protected_hostname(dev_name,
1726                                                    hostname, maxnamlen,
1727                                                    export_path, maxpathlen);
1728
1729        return nfs_parse_simple_hostname(dev_name,
1730                                         hostname, maxnamlen,
1731                                         export_path, maxpathlen);
1732}
1733
1734/*
1735 * Validate the NFS2/NFS3 mount data
1736 * - fills in the mount root filehandle
1737 *
1738 * For option strings, user space handles the following behaviors:
1739 *
1740 * + DNS: mapping server host name to IP address ("addr=" option)
1741 *
1742 * + failure mode: how to behave if a mount request can't be handled
1743 *   immediately ("fg/bg" option)
1744 *
1745 * + retry: how often to retry a mount request ("retry=" option)
1746 *
1747 * + breaking back: trying proto=udp after proto=tcp, v2 after v3,
1748 *   mountproto=tcp after mountproto=udp, and so on
1749 */
1750static int nfs_validate_mount_data(void *options,
1751                                   struct nfs_parsed_mount_data *args,
1752                                   struct nfs_fh *mntfh,
1753                                   const char *dev_name)
1754{
1755        struct nfs_mount_data *data = (struct nfs_mount_data *)options;
1756        struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
1757
1758        if (data == NULL)
1759                goto out_no_data;
1760
1761        switch (data->version) {
1762        case 1:
1763                data->namlen = 0;
1764        case 2:
1765                data->bsize = 0;
1766        case 3:
1767                if (data->flags & NFS_MOUNT_VER3)
1768                        goto out_no_v3;
1769                data->root.size = NFS2_FHSIZE;
1770                memcpy(data->root.data, data->old_root.data, NFS2_FHSIZE);
1771        case 4:
1772                if (data->flags & NFS_MOUNT_SECFLAVOUR)
1773                        goto out_no_sec;
1774        case 5:
1775                memset(data->context, 0, sizeof(data->context));
1776        case 6:
1777                if (data->flags & NFS_MOUNT_VER3) {
1778                        if (data->root.size > NFS3_FHSIZE || data->root.size == 0)
1779                                goto out_invalid_fh;
1780                        mntfh->size = data->root.size;
1781                        args->version = 3;
1782                } else {
1783                        mntfh->size = NFS2_FHSIZE;
1784                        args->version = 2;
1785                }
1786
1787
1788                memcpy(mntfh->data, data->root.data, mntfh->size);
1789                if (mntfh->size < sizeof(mntfh->data))
1790                        memset(mntfh->data + mntfh->size, 0,
1791                               sizeof(mntfh->data) - mntfh->size);
1792
1793                /*
1794                 * Translate to nfs_parsed_mount_data, which nfs_fill_super
1795                 * can deal with.
1796                 */
1797                args->flags             = data->flags & NFS_MOUNT_FLAGMASK;
1798                args->rsize             = data->rsize;
1799                args->wsize             = data->wsize;
1800                args->timeo             = data->timeo;
1801                args->retrans           = data->retrans;
1802                args->acregmin          = data->acregmin;
1803                args->acregmax          = data->acregmax;
1804                args->acdirmin          = data->acdirmin;
1805                args->acdirmax          = data->acdirmax;
1806
1807                memcpy(sap, &data->addr, sizeof(data->addr));
1808                args->nfs_server.addrlen = sizeof(data->addr);
1809                if (!nfs_verify_server_address(sap))
1810                        goto out_no_address;
1811
1812                if (!(data->flags & NFS_MOUNT_TCP))
1813                        args->nfs_server.protocol = XPRT_TRANSPORT_UDP;
1814                /* N.B. caller will free nfs_server.hostname in all cases */
1815                args->nfs_server.hostname = kstrdup(data->hostname, GFP_KERNEL);
1816                args->namlen            = data->namlen;
1817                args->bsize             = data->bsize;
1818
1819                if (data->flags & NFS_MOUNT_SECFLAVOUR)
1820                        args->auth_flavors[0] = data->pseudoflavor;
1821                if (!args->nfs_server.hostname)
1822                        goto out_nomem;
1823
1824                /*
1825                 * The legacy version 6 binary mount data from userspace has a
1826                 * field used only to transport selinux information into the
1827                 * the kernel.  To continue to support that functionality we
1828                 * have a touch of selinux knowledge here in the NFS code. The
1829                 * userspace code converted context=blah to just blah so we are
1830                 * converting back to the full string selinux understands.
1831                 */
1832                if (data->context[0]){
1833#ifdef CONFIG_SECURITY_SELINUX
1834                        int rc;
1835                        char *opts_str = kmalloc(sizeof(data->context) + 8, GFP_KERNEL);
1836                        if (!opts_str)
1837                                return -ENOMEM;
1838                        strcpy(opts_str, "context=");
1839                        data->context[NFS_MAX_CONTEXT_LEN] = '\0';
1840                        strcat(opts_str, &data->context[0]);
1841                        rc = security_sb_parse_opts_str(opts_str, &args->lsm_opts);
1842                        kfree(opts_str);
1843                        if (rc)
1844                                return rc;
1845#else
1846                        return -EINVAL;
1847#endif
1848                }
1849
1850                break;
1851        default: {
1852                int status;
1853
1854                if (nfs_parse_mount_options((char *)options, args) == 0)
1855                        return -EINVAL;
1856
1857                if (!nfs_verify_server_address(sap))
1858                        goto out_no_address;
1859
1860                if (args->version == 4)
1861#ifdef CONFIG_NFS_V4
1862                        return nfs4_validate_text_mount_data(options,
1863                                                             args, dev_name);
1864#else
1865                        goto out_v4_not_compiled;
1866#endif
1867
1868                nfs_set_port(sap, &args->nfs_server.port, 0);
1869
1870                nfs_set_mount_transport_protocol(args);
1871
1872                status = nfs_parse_devname(dev_name,
1873                                           &args->nfs_server.hostname,
1874                                           PAGE_SIZE,
1875                                           &args->nfs_server.export_path,
1876                                           NFS_MAXPATHLEN);
1877                if (!status)
1878                        status = nfs_try_mount(args, mntfh);
1879
1880                kfree(args->nfs_server.export_path);
1881                args->nfs_server.export_path = NULL;
1882
1883                if (status)
1884                        return status;
1885
1886                break;
1887                }
1888        }
1889
1890#ifndef CONFIG_NFS_V3
1891        if (args->version == 3)
1892                goto out_v3_not_compiled;
1893#endif /* !CONFIG_NFS_V3 */
1894
1895        return 0;
1896
1897out_no_data:
1898        dfprintk(MOUNT, "NFS: mount program didn't pass any mount data\n");
1899        return -EINVAL;
1900
1901out_no_v3:
1902        dfprintk(MOUNT, "NFS: nfs_mount_data version %d does not support v3\n",
1903                 data->version);
1904        return -EINVAL;
1905
1906out_no_sec:
1907        dfprintk(MOUNT, "NFS: nfs_mount_data version supports only AUTH_SYS\n");
1908        return -EINVAL;
1909
1910#ifndef CONFIG_NFS_V3
1911out_v3_not_compiled:
1912        dfprintk(MOUNT, "NFS: NFSv3 is not compiled into kernel\n");
1913        return -EPROTONOSUPPORT;
1914#endif /* !CONFIG_NFS_V3 */
1915
1916#ifndef CONFIG_NFS_V4
1917out_v4_not_compiled:
1918        dfprintk(MOUNT, "NFS: NFSv4 is not compiled into kernel\n");
1919        return -EPROTONOSUPPORT;
1920#endif /* !CONFIG_NFS_V4 */
1921
1922out_nomem:
1923        dfprintk(MOUNT, "NFS: not enough memory to handle mount options\n");
1924        return -ENOMEM;
1925
1926out_no_address:
1927        dfprintk(MOUNT, "NFS: mount program didn't pass remote address\n");
1928        return -EINVAL;
1929
1930out_invalid_fh:
1931        dfprintk(MOUNT, "NFS: invalid root filehandle\n");
1932        return -EINVAL;
1933}
1934
1935static int
1936nfs_compare_remount_data(struct nfs_server *nfss,
1937                         struct nfs_parsed_mount_data *data)
1938{
1939        if (data->flags != nfss->flags ||
1940            data->rsize != nfss->rsize ||
1941            data->wsize != nfss->wsize ||
1942            data->retrans != nfss->client->cl_timeout->to_retries ||
1943            data->auth_flavors[0] != nfss->client->cl_auth->au_flavor ||
1944            data->acregmin != nfss->acregmin / HZ ||
1945            data->acregmax != nfss->acregmax / HZ ||
1946            data->acdirmin != nfss->acdirmin / HZ ||
1947            data->acdirmax != nfss->acdirmax / HZ ||
1948            data->timeo != (10U * nfss->client->cl_timeout->to_initval / HZ) ||
1949            data->nfs_server.port != nfss->port ||
1950            data->nfs_server.addrlen != nfss->nfs_client->cl_addrlen ||
1951            !rpc_cmp_addr((struct sockaddr *)&data->nfs_server.address,
1952                          (struct sockaddr *)&nfss->nfs_client->cl_addr))
1953                return -EINVAL;
1954
1955        return 0;
1956}
1957
1958static int
1959nfs_remount(struct super_block *sb, int *flags, char *raw_data)
1960{
1961        int error;
1962        struct nfs_server *nfss = sb->s_fs_info;
1963        struct nfs_parsed_mount_data *data;
1964        struct nfs_mount_data *options = (struct nfs_mount_data *)raw_data;
1965        struct nfs4_mount_data *options4 = (struct nfs4_mount_data *)raw_data;
1966        u32 nfsvers = nfss->nfs_client->rpc_ops->version;
1967
1968        /*
1969         * Userspace mount programs that send binary options generally send
1970         * them populated with default values. We have no way to know which
1971         * ones were explicitly specified. Fall back to legacy behavior and
1972         * just return success.
1973         */
1974        if ((nfsvers == 4 && (!options4 || options4->version == 1)) ||
1975            (nfsvers <= 3 && (!options || (options->version >= 1 &&
1976                                           options->version <= 6))))
1977                return 0;
1978
1979        data = kzalloc(sizeof(*data), GFP_KERNEL);
1980        if (data == NULL)
1981                return -ENOMEM;
1982
1983        /* fill out struct with values from existing mount */
1984        data->flags = nfss->flags;
1985        data->rsize = nfss->rsize;
1986        data->wsize = nfss->wsize;
1987        data->retrans = nfss->client->cl_timeout->to_retries;
1988        data->auth_flavors[0] = nfss->client->cl_auth->au_flavor;
1989        data->acregmin = nfss->acregmin / HZ;
1990        data->acregmax = nfss->acregmax / HZ;
1991        data->acdirmin = nfss->acdirmin / HZ;
1992        data->acdirmax = nfss->acdirmax / HZ;
1993        data->timeo = 10U * nfss->client->cl_timeout->to_initval / HZ;
1994        data->nfs_server.port = nfss->port;
1995        data->nfs_server.addrlen = nfss->nfs_client->cl_addrlen;
1996        memcpy(&data->nfs_server.address, &nfss->nfs_client->cl_addr,
1997                data->nfs_server.addrlen);
1998
1999        /* overwrite those values with any that were specified */
2000        error = nfs_parse_mount_options((char *)options, data);
2001        if (error < 0)
2002                goto out;
2003
2004        /* compare new mount options with old ones */
2005        error = nfs_compare_remount_data(nfss, data);
2006out:
2007        kfree(data);
2008        return error;
2009}
2010
2011/*
2012 * Initialise the common bits of the superblock
2013 */
2014static inline void nfs_initialise_sb(struct super_block *sb)
2015{
2016        struct nfs_server *server = NFS_SB(sb);
2017
2018        sb->s_magic = NFS_SUPER_MAGIC;
2019
2020        /* We probably want something more informative here */
2021        snprintf(sb->s_id, sizeof(sb->s_id),
2022                 "%x:%x", MAJOR(sb->s_dev), MINOR(sb->s_dev));
2023
2024        if (sb->s_blocksize == 0)
2025                sb->s_blocksize = nfs_block_bits(server->wsize,
2026                                                 &sb->s_blocksize_bits);
2027
2028        if (server->flags & NFS_MOUNT_NOAC)
2029                sb->s_flags |= MS_SYNCHRONOUS;
2030
2031        sb->s_bdi = &server->backing_dev_info;
2032
2033        nfs_super_set_maxbytes(sb, server->maxfilesize);
2034}
2035
2036/*
2037 * Finish setting up an NFS2/3 superblock
2038 */
2039static void nfs_fill_super(struct super_block *sb,
2040                           struct nfs_parsed_mount_data *data)
2041{
2042        struct nfs_server *server = NFS_SB(sb);
2043
2044        sb->s_blocksize_bits = 0;
2045        sb->s_blocksize = 0;
2046        if (data->bsize)
2047                sb->s_blocksize = nfs_block_size(data->bsize, &sb->s_blocksize_bits);
2048
2049        if (server->nfs_client->rpc_ops->version == 3) {
2050                /* The VFS shouldn't apply the umask to mode bits. We will do
2051                 * so ourselves when necessary.
2052                 */
2053                sb->s_flags |= MS_POSIXACL;
2054                sb->s_time_gran = 1;
2055        }
2056
2057        sb->s_op = &nfs_sops;
2058        nfs_initialise_sb(sb);
2059}
2060
2061/*
2062 * Finish setting up a cloned NFS2/3 superblock
2063 */
2064static void nfs_clone_super(struct super_block *sb,
2065                            const struct super_block *old_sb)
2066{
2067        struct nfs_server *server = NFS_SB(sb);
2068
2069        sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2070        sb->s_blocksize = old_sb->s_blocksize;
2071        sb->s_maxbytes = old_sb->s_maxbytes;
2072
2073        if (server->nfs_client->rpc_ops->version == 3) {
2074                /* The VFS shouldn't apply the umask to mode bits. We will do
2075                 * so ourselves when necessary.
2076                 */
2077                sb->s_flags |= MS_POSIXACL;
2078                sb->s_time_gran = 1;
2079        }
2080
2081        sb->s_op = old_sb->s_op;
2082        nfs_initialise_sb(sb);
2083}
2084
2085static int nfs_compare_mount_options(const struct super_block *s, const struct nfs_server *b, int flags)
2086{
2087        const struct nfs_server *a = s->s_fs_info;
2088        const struct rpc_clnt *clnt_a = a->client;
2089        const struct rpc_clnt *clnt_b = b->client;
2090
2091        if ((s->s_flags & NFS_MS_MASK) != (flags & NFS_MS_MASK))
2092                goto Ebusy;
2093        if (a->nfs_client != b->nfs_client)
2094                goto Ebusy;
2095        if (a->flags != b->flags)
2096                goto Ebusy;
2097        if (a->wsize != b->wsize)
2098                goto Ebusy;
2099        if (a->rsize != b->rsize)
2100                goto Ebusy;
2101        if (a->acregmin != b->acregmin)
2102                goto Ebusy;
2103        if (a->acregmax != b->acregmax)
2104                goto Ebusy;
2105        if (a->acdirmin != b->acdirmin)
2106                goto Ebusy;
2107        if (a->acdirmax != b->acdirmax)
2108                goto Ebusy;
2109        if (clnt_a->cl_auth->au_flavor != clnt_b->cl_auth->au_flavor)
2110                goto Ebusy;
2111        return 1;
2112Ebusy:
2113        return 0;
2114}
2115
2116struct nfs_sb_mountdata {
2117        struct nfs_server *server;
2118        int mntflags;
2119};
2120
2121static int nfs_set_super(struct super_block *s, void *data)
2122{
2123        struct nfs_sb_mountdata *sb_mntdata = data;
2124        struct nfs_server *server = sb_mntdata->server;
2125        int ret;
2126
2127        s->s_flags = sb_mntdata->mntflags;
2128        s->s_fs_info = server;
2129        ret = set_anon_super(s, server);
2130        if (ret == 0)
2131                server->s_dev = s->s_dev;
2132        return ret;
2133}
2134
2135static int nfs_compare_super_address(struct nfs_server *server1,
2136                                     struct nfs_server *server2)
2137{
2138        struct sockaddr *sap1, *sap2;
2139
2140        sap1 = (struct sockaddr *)&server1->nfs_client->cl_addr;
2141        sap2 = (struct sockaddr *)&server2->nfs_client->cl_addr;
2142
2143        if (sap1->sa_family != sap2->sa_family)
2144                return 0;
2145
2146        switch (sap1->sa_family) {
2147        case AF_INET: {
2148                struct sockaddr_in *sin1 = (struct sockaddr_in *)sap1;
2149                struct sockaddr_in *sin2 = (struct sockaddr_in *)sap2;
2150                if (sin1->sin_addr.s_addr != sin2->sin_addr.s_addr)
2151                        return 0;
2152                if (sin1->sin_port != sin2->sin_port)
2153                        return 0;
2154                break;
2155        }
2156        case AF_INET6: {
2157                struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)sap1;
2158                struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)sap2;
2159                if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
2160                        return 0;
2161                if (sin1->sin6_port != sin2->sin6_port)
2162                        return 0;
2163                break;
2164        }
2165        default:
2166                return 0;
2167        }
2168
2169        return 1;
2170}
2171
2172static int nfs_compare_super(struct super_block *sb, void *data)
2173{
2174        struct nfs_sb_mountdata *sb_mntdata = data;
2175        struct nfs_server *server = sb_mntdata->server, *old = NFS_SB(sb);
2176        int mntflags = sb_mntdata->mntflags;
2177
2178        if (!nfs_compare_super_address(old, server))
2179                return 0;
2180        /* Note: NFS_MOUNT_UNSHARED == NFS4_MOUNT_UNSHARED */
2181        if (old->flags & NFS_MOUNT_UNSHARED)
2182                return 0;
2183        if (memcmp(&old->fsid, &server->fsid, sizeof(old->fsid)) != 0)
2184                return 0;
2185        return nfs_compare_mount_options(sb, server, mntflags);
2186}
2187
2188static int nfs_bdi_register(struct nfs_server *server)
2189{
2190        return bdi_register_dev(&server->backing_dev_info, server->s_dev);
2191}
2192
2193static int nfs_get_sb(struct file_system_type *fs_type,
2194        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2195{
2196        struct nfs_server *server = NULL;
2197        struct super_block *s;
2198        struct nfs_parsed_mount_data *data;
2199        struct nfs_fh *mntfh;
2200        struct dentry *mntroot;
2201        int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2202        struct nfs_sb_mountdata sb_mntdata = {
2203                .mntflags = flags,
2204        };
2205        int error = -ENOMEM;
2206
2207        data = nfs_alloc_parsed_mount_data(3);
2208        mntfh = nfs_alloc_fhandle();
2209        if (data == NULL || mntfh == NULL)
2210                goto out_free_fh;
2211
2212        security_init_mnt_opts(&data->lsm_opts);
2213
2214        /* Validate the mount data */
2215        error = nfs_validate_mount_data(raw_data, data, mntfh, dev_name);
2216        if (error < 0)
2217                goto out;
2218
2219#ifdef CONFIG_NFS_V4
2220        if (data->version == 4) {
2221                error = nfs4_try_mount(flags, dev_name, data, mnt);
2222                kfree(data->client_address);
2223                kfree(data->nfs_server.export_path);
2224                goto out;
2225        }
2226#endif  /* CONFIG_NFS_V4 */
2227
2228        /* Get a volume representation */
2229        server = nfs_create_server(data, mntfh);
2230        if (IS_ERR(server)) {
2231                error = PTR_ERR(server);
2232                goto out;
2233        }
2234        sb_mntdata.server = server;
2235
2236        if (server->flags & NFS_MOUNT_UNSHARED)
2237                compare_super = NULL;
2238
2239        /* Get a superblock - note that we may end up sharing one that already exists */
2240        s = sget(fs_type, compare_super, nfs_set_super, &sb_mntdata);
2241        if (IS_ERR(s)) {
2242                error = PTR_ERR(s);
2243                goto out_err_nosb;
2244        }
2245
2246        if (s->s_fs_info != server) {
2247                nfs_free_server(server);
2248                server = NULL;
2249        } else {
2250                error = nfs_bdi_register(server);
2251                if (error)
2252                        goto error_splat_bdi;
2253        }
2254
2255        if (!s->s_root) {
2256                /* initial superblock/root creation */
2257                nfs_fill_super(s, data);
2258                nfs_fscache_get_super_cookie(
2259                        s, data ? data->fscache_uniq : NULL, NULL);
2260        }
2261
2262        mntroot = nfs_get_root(s, mntfh);
2263        if (IS_ERR(mntroot)) {
2264                error = PTR_ERR(mntroot);
2265                goto error_splat_super;
2266        }
2267
2268        error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2269        if (error)
2270                goto error_splat_root;
2271
2272        s->s_flags |= MS_ACTIVE;
2273        mnt->mnt_sb = s;
2274        mnt->mnt_root = mntroot;
2275        error = 0;
2276
2277out:
2278        kfree(data->nfs_server.hostname);
2279        kfree(data->mount_server.hostname);
2280        kfree(data->fscache_uniq);
2281        security_free_mnt_opts(&data->lsm_opts);
2282out_free_fh:
2283        nfs_free_fhandle(mntfh);
2284        kfree(data);
2285        return error;
2286
2287out_err_nosb:
2288        nfs_free_server(server);
2289        goto out;
2290
2291error_splat_root:
2292        dput(mntroot);
2293error_splat_super:
2294        if (server && !s->s_root)
2295                bdi_unregister(&server->backing_dev_info);
2296error_splat_bdi:
2297        deactivate_locked_super(s);
2298        goto out;
2299}
2300
2301/*
2302 * Ensure that we unregister the bdi before kill_anon_super
2303 * releases the device name
2304 */
2305static void nfs_put_super(struct super_block *s)
2306{
2307        struct nfs_server *server = NFS_SB(s);
2308
2309        bdi_unregister(&server->backing_dev_info);
2310}
2311
2312/*
2313 * Destroy an NFS2/3 superblock
2314 */
2315static void nfs_kill_super(struct super_block *s)
2316{
2317        struct nfs_server *server = NFS_SB(s);
2318
2319        kill_anon_super(s);
2320        nfs_fscache_release_super_cookie(s);
2321        nfs_free_server(server);
2322}
2323
2324/*
2325 * Clone an NFS2/3 server record on xdev traversal (FSID-change)
2326 */
2327static int nfs_xdev_get_sb(struct file_system_type *fs_type, int flags,
2328                           const char *dev_name, void *raw_data,
2329                           struct vfsmount *mnt)
2330{
2331        struct nfs_clone_mount *data = raw_data;
2332        struct super_block *s;
2333        struct nfs_server *server;
2334        struct dentry *mntroot;
2335        int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2336        struct nfs_sb_mountdata sb_mntdata = {
2337                .mntflags = flags,
2338        };
2339        int error;
2340
2341        dprintk("--> nfs_xdev_get_sb()\n");
2342
2343        /* create a new volume representation */
2344        server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2345        if (IS_ERR(server)) {
2346                error = PTR_ERR(server);
2347                goto out_err_noserver;
2348        }
2349        sb_mntdata.server = server;
2350
2351        if (server->flags & NFS_MOUNT_UNSHARED)
2352                compare_super = NULL;
2353
2354        /* Get a superblock - note that we may end up sharing one that already exists */
2355        s = sget(&nfs_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2356        if (IS_ERR(s)) {
2357                error = PTR_ERR(s);
2358                goto out_err_nosb;
2359        }
2360
2361        if (s->s_fs_info != server) {
2362                nfs_free_server(server);
2363                server = NULL;
2364        } else {
2365                error = nfs_bdi_register(server);
2366                if (error)
2367                        goto error_splat_bdi;
2368        }
2369
2370        if (!s->s_root) {
2371                /* initial superblock/root creation */
2372                nfs_clone_super(s, data->sb);
2373                nfs_fscache_get_super_cookie(s, NULL, data);
2374        }
2375
2376        mntroot = nfs_get_root(s, data->fh);
2377        if (IS_ERR(mntroot)) {
2378                error = PTR_ERR(mntroot);
2379                goto error_splat_super;
2380        }
2381        if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2382                dput(mntroot);
2383                error = -ESTALE;
2384                goto error_splat_super;
2385        }
2386
2387        s->s_flags |= MS_ACTIVE;
2388        mnt->mnt_sb = s;
2389        mnt->mnt_root = mntroot;
2390
2391        /* clone any lsm security options from the parent to the new sb */
2392        security_sb_clone_mnt_opts(data->sb, s);
2393
2394        dprintk("<-- nfs_xdev_get_sb() = 0\n");
2395        return 0;
2396
2397out_err_nosb:
2398        nfs_free_server(server);
2399out_err_noserver:
2400        dprintk("<-- nfs_xdev_get_sb() = %d [error]\n", error);
2401        return error;
2402
2403error_splat_super:
2404        if (server && !s->s_root)
2405                bdi_unregister(&server->backing_dev_info);
2406error_splat_bdi:
2407        deactivate_locked_super(s);
2408        dprintk("<-- nfs_xdev_get_sb() = %d [splat]\n", error);
2409        return error;
2410}
2411
2412#ifdef CONFIG_NFS_V4
2413
2414/*
2415 * Finish setting up a cloned NFS4 superblock
2416 */
2417static void nfs4_clone_super(struct super_block *sb,
2418                            const struct super_block *old_sb)
2419{
2420        sb->s_blocksize_bits = old_sb->s_blocksize_bits;
2421        sb->s_blocksize = old_sb->s_blocksize;
2422        sb->s_maxbytes = old_sb->s_maxbytes;
2423        sb->s_time_gran = 1;
2424        sb->s_op = old_sb->s_op;
2425        nfs_initialise_sb(sb);
2426}
2427
2428/*
2429 * Set up an NFS4 superblock
2430 */
2431static void nfs4_fill_super(struct super_block *sb)
2432{
2433        sb->s_time_gran = 1;
2434        sb->s_op = &nfs4_sops;
2435        nfs_initialise_sb(sb);
2436}
2437
2438static void nfs4_validate_mount_flags(struct nfs_parsed_mount_data *args)
2439{
2440        args->flags &= ~(NFS_MOUNT_NONLM|NFS_MOUNT_NOACL|NFS_MOUNT_VER3);
2441}
2442
2443static int nfs4_validate_text_mount_data(void *options,
2444                                         struct nfs_parsed_mount_data *args,
2445                                         const char *dev_name)
2446{
2447        struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2448
2449        nfs_set_port(sap, &args->nfs_server.port, NFS_PORT);
2450
2451        nfs_validate_transport_protocol(args);
2452
2453        nfs4_validate_mount_flags(args);
2454
2455        if (args->version != 4) {
2456                dfprintk(MOUNT,
2457                         "NFS4: Illegal mount version\n");
2458                return -EINVAL;
2459        }
2460
2461        if (args->auth_flavor_len > 1) {
2462                dfprintk(MOUNT,
2463                         "NFS4: Too many RPC auth flavours specified\n");
2464                return -EINVAL;
2465        }
2466
2467        if (args->client_address == NULL) {
2468                dfprintk(MOUNT,
2469                         "NFS4: mount program didn't pass callback address\n");
2470                return -EINVAL;
2471        }
2472
2473        return nfs_parse_devname(dev_name,
2474                                   &args->nfs_server.hostname,
2475                                   NFS4_MAXNAMLEN,
2476                                   &args->nfs_server.export_path,
2477                                   NFS4_MAXPATHLEN);
2478}
2479
2480/*
2481 * Validate NFSv4 mount options
2482 */
2483static int nfs4_validate_mount_data(void *options,
2484                                    struct nfs_parsed_mount_data *args,
2485                                    const char *dev_name)
2486{
2487        struct sockaddr *sap = (struct sockaddr *)&args->nfs_server.address;
2488        struct nfs4_mount_data *data = (struct nfs4_mount_data *)options;
2489        char *c;
2490
2491        if (data == NULL)
2492                goto out_no_data;
2493
2494        switch (data->version) {
2495        case 1:
2496                if (data->host_addrlen > sizeof(args->nfs_server.address))
2497                        goto out_no_address;
2498                if (data->host_addrlen == 0)
2499                        goto out_no_address;
2500                args->nfs_server.addrlen = data->host_addrlen;
2501                if (copy_from_user(sap, data->host_addr, data->host_addrlen))
2502                        return -EFAULT;
2503                if (!nfs_verify_server_address(sap))
2504                        goto out_no_address;
2505
2506                if (data->auth_flavourlen) {
2507                        if (data->auth_flavourlen > 1)
2508                                goto out_inval_auth;
2509                        if (copy_from_user(&args->auth_flavors[0],
2510                                           data->auth_flavours,
2511                                           sizeof(args->auth_flavors[0])))
2512                                return -EFAULT;
2513                }
2514
2515                c = strndup_user(data->hostname.data, NFS4_MAXNAMLEN);
2516                if (IS_ERR(c))
2517                        return PTR_ERR(c);
2518                args->nfs_server.hostname = c;
2519
2520                c = strndup_user(data->mnt_path.data, NFS4_MAXPATHLEN);
2521                if (IS_ERR(c))
2522                        return PTR_ERR(c);
2523                args->nfs_server.export_path = c;
2524                dfprintk(MOUNT, "NFS: MNTPATH: '%s'\n", c);
2525
2526                c = strndup_user(data->client_addr.data, 16);
2527                if (IS_ERR(c))
2528                        return PTR_ERR(c);
2529                args->client_address = c;
2530
2531                /*
2532                 * Translate to nfs_parsed_mount_data, which nfs4_fill_super
2533                 * can deal with.
2534                 */
2535
2536                args->flags     = data->flags & NFS4_MOUNT_FLAGMASK;
2537                args->rsize     = data->rsize;
2538                args->wsize     = data->wsize;
2539                args->timeo     = data->timeo;
2540                args->retrans   = data->retrans;
2541                args->acregmin  = data->acregmin;
2542                args->acregmax  = data->acregmax;
2543                args->acdirmin  = data->acdirmin;
2544                args->acdirmax  = data->acdirmax;
2545                args->nfs_server.protocol = data->proto;
2546                nfs_validate_transport_protocol(args);
2547
2548                break;
2549        default:
2550                if (nfs_parse_mount_options((char *)options, args) == 0)
2551                        return -EINVAL;
2552
2553                if (!nfs_verify_server_address(sap))
2554                        return -EINVAL;
2555
2556                return nfs4_validate_text_mount_data(options, args, dev_name);
2557        }
2558
2559        return 0;
2560
2561out_no_data:
2562        dfprintk(MOUNT, "NFS4: mount program didn't pass any mount data\n");
2563        return -EINVAL;
2564
2565out_inval_auth:
2566        dfprintk(MOUNT, "NFS4: Invalid number of RPC auth flavours %d\n",
2567                 data->auth_flavourlen);
2568        return -EINVAL;
2569
2570out_no_address:
2571        dfprintk(MOUNT, "NFS4: mount program didn't pass remote address\n");
2572        return -EINVAL;
2573}
2574
2575/*
2576 * Get the superblock for the NFS4 root partition
2577 */
2578static int nfs4_remote_get_sb(struct file_system_type *fs_type,
2579        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2580{
2581        struct nfs_parsed_mount_data *data = raw_data;
2582        struct super_block *s;
2583        struct nfs_server *server;
2584        struct nfs_fh *mntfh;
2585        struct dentry *mntroot;
2586        int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2587        struct nfs_sb_mountdata sb_mntdata = {
2588                .mntflags = flags,
2589        };
2590        int error = -ENOMEM;
2591
2592        mntfh = nfs_alloc_fhandle();
2593        if (data == NULL || mntfh == NULL)
2594                goto out_free_fh;
2595
2596        security_init_mnt_opts(&data->lsm_opts);
2597
2598        /* Get a volume representation */
2599        server = nfs4_create_server(data, mntfh);
2600        if (IS_ERR(server)) {
2601                error = PTR_ERR(server);
2602                goto out;
2603        }
2604        sb_mntdata.server = server;
2605
2606        if (server->flags & NFS4_MOUNT_UNSHARED)
2607                compare_super = NULL;
2608
2609        /* Get a superblock - note that we may end up sharing one that already exists */
2610        s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2611        if (IS_ERR(s)) {
2612                error = PTR_ERR(s);
2613                goto out_free;
2614        }
2615
2616        if (s->s_fs_info != server) {
2617                nfs_free_server(server);
2618                server = NULL;
2619        } else {
2620                error = nfs_bdi_register(server);
2621                if (error)
2622                        goto error_splat_bdi;
2623        }
2624
2625        if (!s->s_root) {
2626                /* initial superblock/root creation */
2627                nfs4_fill_super(s);
2628                nfs_fscache_get_super_cookie(
2629                        s, data ? data->fscache_uniq : NULL, NULL);
2630        }
2631
2632        mntroot = nfs4_get_root(s, mntfh);
2633        if (IS_ERR(mntroot)) {
2634                error = PTR_ERR(mntroot);
2635                goto error_splat_super;
2636        }
2637
2638        error = security_sb_set_mnt_opts(s, &data->lsm_opts);
2639        if (error)
2640                goto error_splat_root;
2641
2642        s->s_flags |= MS_ACTIVE;
2643        mnt->mnt_sb = s;
2644        mnt->mnt_root = mntroot;
2645        error = 0;
2646
2647out:
2648        security_free_mnt_opts(&data->lsm_opts);
2649out_free_fh:
2650        nfs_free_fhandle(mntfh);
2651        return error;
2652
2653out_free:
2654        nfs_free_server(server);
2655        goto out;
2656
2657error_splat_root:
2658        dput(mntroot);
2659error_splat_super:
2660        if (server && !s->s_root)
2661                bdi_unregister(&server->backing_dev_info);
2662error_splat_bdi:
2663        deactivate_locked_super(s);
2664        goto out;
2665}
2666
2667static struct vfsmount *nfs_do_root_mount(struct file_system_type *fs_type,
2668                int flags, void *data, const char *hostname)
2669{
2670        struct vfsmount *root_mnt;
2671        char *root_devname;
2672        size_t len;
2673
2674        len = strlen(hostname) + 3;
2675        root_devname = kmalloc(len, GFP_KERNEL);
2676        if (root_devname == NULL)
2677                return ERR_PTR(-ENOMEM);
2678        snprintf(root_devname, len, "%s:/", hostname);
2679        root_mnt = vfs_kern_mount(fs_type, flags, root_devname, data);
2680        kfree(root_devname);
2681        return root_mnt;
2682}
2683
2684static void nfs_fix_devname(const struct path *path, struct vfsmount *mnt)
2685{
2686        char *page = (char *) __get_free_page(GFP_KERNEL);
2687        char *devname, *tmp;
2688
2689        if (page == NULL)
2690                return;
2691        devname = nfs_path(path->mnt->mnt_devname,
2692                        path->mnt->mnt_root, path->dentry,
2693                        page, PAGE_SIZE);
2694        if (IS_ERR(devname))
2695                goto out_freepage;
2696        tmp = kstrdup(devname, GFP_KERNEL);
2697        if (tmp == NULL)
2698                goto out_freepage;
2699        kfree(mnt->mnt_devname);
2700        mnt->mnt_devname = tmp;
2701out_freepage:
2702        free_page((unsigned long)page);
2703}
2704
2705struct nfs_referral_count {
2706        struct list_head list;
2707        const struct task_struct *task;
2708        unsigned int referral_count;
2709};
2710
2711static LIST_HEAD(nfs_referral_count_list);
2712static DEFINE_SPINLOCK(nfs_referral_count_list_lock);
2713
2714static struct nfs_referral_count *nfs_find_referral_count(void)
2715{
2716        struct nfs_referral_count *p;
2717
2718        list_for_each_entry(p, &nfs_referral_count_list, list) {
2719                if (p->task == current)
2720                        return p;
2721        }
2722        return NULL;
2723}
2724
2725#define NFS_MAX_NESTED_REFERRALS 2
2726
2727static int nfs_referral_loop_protect(void)
2728{
2729        struct nfs_referral_count *p, *new;
2730        int ret = -ENOMEM;
2731
2732        new = kmalloc(sizeof(*new), GFP_KERNEL);
2733        if (!new)
2734                goto out;
2735        new->task = current;
2736        new->referral_count = 1;
2737
2738        ret = 0;
2739        spin_lock(&nfs_referral_count_list_lock);
2740        p = nfs_find_referral_count();
2741        if (p != NULL) {
2742                if (p->referral_count >= NFS_MAX_NESTED_REFERRALS)
2743                        ret = -ELOOP;
2744                else
2745                        p->referral_count++;
2746        } else {
2747                list_add(&new->list, &nfs_referral_count_list);
2748                new = NULL;
2749        }
2750        spin_unlock(&nfs_referral_count_list_lock);
2751        kfree(new);
2752out:
2753        return ret;
2754}
2755
2756static void nfs_referral_loop_unprotect(void)
2757{
2758        struct nfs_referral_count *p;
2759
2760        spin_lock(&nfs_referral_count_list_lock);
2761        p = nfs_find_referral_count();
2762        p->referral_count--;
2763        if (p->referral_count == 0)
2764                list_del(&p->list);
2765        else
2766                p = NULL;
2767        spin_unlock(&nfs_referral_count_list_lock);
2768        kfree(p);
2769}
2770
2771static int nfs_follow_remote_path(struct vfsmount *root_mnt,
2772                const char *export_path, struct vfsmount *mnt_target)
2773{
2774        struct nameidata *nd = NULL;
2775        struct mnt_namespace *ns_private;
2776        struct super_block *s;
2777        int ret;
2778
2779        nd = kmalloc(sizeof(*nd), GFP_KERNEL);
2780        if (nd == NULL)
2781                return -ENOMEM;
2782
2783        ns_private = create_mnt_ns(root_mnt);
2784        ret = PTR_ERR(ns_private);
2785        if (IS_ERR(ns_private))
2786                goto out_mntput;
2787
2788        ret = nfs_referral_loop_protect();
2789        if (ret != 0)
2790                goto out_put_mnt_ns;
2791
2792        ret = vfs_path_lookup(root_mnt->mnt_root, root_mnt,
2793                        export_path, LOOKUP_FOLLOW, nd);
2794
2795        nfs_referral_loop_unprotect();
2796        put_mnt_ns(ns_private);
2797
2798        if (ret != 0)
2799                goto out_err;
2800
2801        s = nd->path.mnt->mnt_sb;
2802        atomic_inc(&s->s_active);
2803        mnt_target->mnt_sb = s;
2804        mnt_target->mnt_root = dget(nd->path.dentry);
2805
2806        /* Correct the device pathname */
2807        nfs_fix_devname(&nd->path, mnt_target);
2808
2809        path_put(&nd->path);
2810        kfree(nd);
2811        down_write(&s->s_umount);
2812        return 0;
2813out_put_mnt_ns:
2814        put_mnt_ns(ns_private);
2815out_mntput:
2816        mntput(root_mnt);
2817out_err:
2818        kfree(nd);
2819        return ret;
2820}
2821
2822static int nfs4_try_mount(int flags, const char *dev_name,
2823                         struct nfs_parsed_mount_data *data,
2824                         struct vfsmount *mnt)
2825{
2826        char *export_path;
2827        struct vfsmount *root_mnt;
2828        int error;
2829
2830        dfprintk(MOUNT, "--> nfs4_try_mount()\n");
2831
2832        export_path = data->nfs_server.export_path;
2833        data->nfs_server.export_path = "/";
2834        root_mnt = nfs_do_root_mount(&nfs4_remote_fs_type, flags, data,
2835                        data->nfs_server.hostname);
2836        data->nfs_server.export_path = export_path;
2837
2838        error = PTR_ERR(root_mnt);
2839        if (IS_ERR(root_mnt))
2840                goto out;
2841
2842        error = nfs_follow_remote_path(root_mnt, export_path, mnt);
2843
2844out:
2845        dfprintk(MOUNT, "<-- nfs4_try_mount() = %d%s\n", error,
2846                        error != 0 ? " [error]" : "");
2847        return error;
2848}
2849
2850/*
2851 * Get the superblock for an NFS4 mountpoint
2852 */
2853static int nfs4_get_sb(struct file_system_type *fs_type,
2854        int flags, const char *dev_name, void *raw_data, struct vfsmount *mnt)
2855{
2856        struct nfs_parsed_mount_data *data;
2857        int error = -ENOMEM;
2858
2859        data = nfs_alloc_parsed_mount_data(4);
2860        if (data == NULL)
2861                goto out_free_data;
2862
2863        /* Validate the mount data */
2864        error = nfs4_validate_mount_data(raw_data, data, dev_name);
2865        if (error < 0)
2866                goto out;
2867
2868        error = nfs4_try_mount(flags, dev_name, data, mnt);
2869
2870out:
2871        kfree(data->client_address);
2872        kfree(data->nfs_server.export_path);
2873        kfree(data->nfs_server.hostname);
2874        kfree(data->fscache_uniq);
2875out_free_data:
2876        kfree(data);
2877        dprintk("<-- nfs4_get_sb() = %d%s\n", error,
2878                        error != 0 ? " [error]" : "");
2879        return error;
2880}
2881
2882static void nfs4_kill_super(struct super_block *sb)
2883{
2884        struct nfs_server *server = NFS_SB(sb);
2885
2886        dprintk("--> %s\n", __func__);
2887        nfs_super_return_all_delegations(sb);
2888        kill_anon_super(sb);
2889        nfs_fscache_release_super_cookie(sb);
2890        nfs_free_server(server);
2891        dprintk("<-- %s\n", __func__);
2892}
2893
2894/*
2895 * Clone an NFS4 server record on xdev traversal (FSID-change)
2896 */
2897static int nfs4_xdev_get_sb(struct file_system_type *fs_type, int flags,
2898                            const char *dev_name, void *raw_data,
2899                            struct vfsmount *mnt)
2900{
2901        struct nfs_clone_mount *data = raw_data;
2902        struct super_block *s;
2903        struct nfs_server *server;
2904        struct dentry *mntroot;
2905        int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2906        struct nfs_sb_mountdata sb_mntdata = {
2907                .mntflags = flags,
2908        };
2909        int error;
2910
2911        dprintk("--> nfs4_xdev_get_sb()\n");
2912
2913        /* create a new volume representation */
2914        server = nfs_clone_server(NFS_SB(data->sb), data->fh, data->fattr);
2915        if (IS_ERR(server)) {
2916                error = PTR_ERR(server);
2917                goto out_err_noserver;
2918        }
2919        sb_mntdata.server = server;
2920
2921        if (server->flags & NFS4_MOUNT_UNSHARED)
2922                compare_super = NULL;
2923
2924        /* Get a superblock - note that we may end up sharing one that already exists */
2925        s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
2926        if (IS_ERR(s)) {
2927                error = PTR_ERR(s);
2928                goto out_err_nosb;
2929        }
2930
2931        if (s->s_fs_info != server) {
2932                nfs_free_server(server);
2933                server = NULL;
2934        } else {
2935                error = nfs_bdi_register(server);
2936                if (error)
2937                        goto error_splat_bdi;
2938        }
2939
2940        if (!s->s_root) {
2941                /* initial superblock/root creation */
2942                nfs4_clone_super(s, data->sb);
2943                nfs_fscache_get_super_cookie(s, NULL, data);
2944        }
2945
2946        mntroot = nfs4_get_root(s, data->fh);
2947        if (IS_ERR(mntroot)) {
2948                error = PTR_ERR(mntroot);
2949                goto error_splat_super;
2950        }
2951        if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
2952                dput(mntroot);
2953                error = -ESTALE;
2954                goto error_splat_super;
2955        }
2956
2957        s->s_flags |= MS_ACTIVE;
2958        mnt->mnt_sb = s;
2959        mnt->mnt_root = mntroot;
2960
2961        security_sb_clone_mnt_opts(data->sb, s);
2962
2963        dprintk("<-- nfs4_xdev_get_sb() = 0\n");
2964        return 0;
2965
2966out_err_nosb:
2967        nfs_free_server(server);
2968out_err_noserver:
2969        dprintk("<-- nfs4_xdev_get_sb() = %d [error]\n", error);
2970        return error;
2971
2972error_splat_super:
2973        if (server && !s->s_root)
2974                bdi_unregister(&server->backing_dev_info);
2975error_splat_bdi:
2976        deactivate_locked_super(s);
2977        dprintk("<-- nfs4_xdev_get_sb() = %d [splat]\n", error);
2978        return error;
2979}
2980
2981static int nfs4_remote_referral_get_sb(struct file_system_type *fs_type,
2982                int flags, const char *dev_name, void *raw_data,
2983                struct vfsmount *mnt)
2984{
2985        struct nfs_clone_mount *data = raw_data;
2986        struct super_block *s;
2987        struct nfs_server *server;
2988        struct dentry *mntroot;
2989        struct nfs_fh *mntfh;
2990        int (*compare_super)(struct super_block *, void *) = nfs_compare_super;
2991        struct nfs_sb_mountdata sb_mntdata = {
2992                .mntflags = flags,
2993        };
2994        int error = -ENOMEM;
2995
2996        dprintk("--> nfs4_referral_get_sb()\n");
2997
2998        mntfh = nfs_alloc_fhandle();
2999        if (mntfh == NULL)
3000                goto out_err_nofh;
3001
3002        /* create a new volume representation */
3003        server = nfs4_create_referral_server(data, mntfh);
3004        if (IS_ERR(server)) {
3005                error = PTR_ERR(server);
3006                goto out_err_noserver;
3007        }
3008        sb_mntdata.server = server;
3009
3010        if (server->flags & NFS4_MOUNT_UNSHARED)
3011                compare_super = NULL;
3012
3013        /* Get a superblock - note that we may end up sharing one that already exists */
3014        s = sget(&nfs4_fs_type, compare_super, nfs_set_super, &sb_mntdata);
3015        if (IS_ERR(s)) {
3016                error = PTR_ERR(s);
3017                goto out_err_nosb;
3018        }
3019
3020        if (s->s_fs_info != server) {
3021                nfs_free_server(server);
3022                server = NULL;
3023        } else {
3024                error = nfs_bdi_register(server);
3025                if (error)
3026                        goto error_splat_bdi;
3027        }
3028
3029        if (!s->s_root) {
3030                /* initial superblock/root creation */
3031                nfs4_fill_super(s);
3032                nfs_fscache_get_super_cookie(s, NULL, data);
3033        }
3034
3035        mntroot = nfs4_get_root(s, mntfh);
3036        if (IS_ERR(mntroot)) {
3037                error = PTR_ERR(mntroot);
3038                goto error_splat_super;
3039        }
3040        if (mntroot->d_inode->i_op != NFS_SB(s)->nfs_client->rpc_ops->dir_inode_ops) {
3041                dput(mntroot);
3042                error = -ESTALE;
3043                goto error_splat_super;
3044        }
3045
3046        s->s_flags |= MS_ACTIVE;
3047        mnt->mnt_sb = s;
3048        mnt->mnt_root = mntroot;
3049
3050        security_sb_clone_mnt_opts(data->sb, s);
3051
3052        nfs_free_fhandle(mntfh);
3053        dprintk("<-- nfs4_referral_get_sb() = 0\n");
3054        return 0;
3055
3056out_err_nosb:
3057        nfs_free_server(server);
3058out_err_noserver:
3059        nfs_free_fhandle(mntfh);
3060out_err_nofh:
3061        dprintk("<-- nfs4_referral_get_sb() = %d [error]\n", error);
3062        return error;
3063
3064error_splat_super:
3065        if (server && !s->s_root)
3066                bdi_unregister(&server->backing_dev_info);
3067error_splat_bdi:
3068        deactivate_locked_super(s);
3069        nfs_free_fhandle(mntfh);
3070        dprintk("<-- nfs4_referral_get_sb() = %d [splat]\n", error);
3071        return error;
3072}
3073
3074/*
3075 * Create an NFS4 server record on referral traversal
3076 */
3077static int nfs4_referral_get_sb(struct file_system_type *fs_type,
3078                int flags, const char *dev_name, void *raw_data,
3079                struct vfsmount *mnt)
3080{
3081        struct nfs_clone_mount *data = raw_data;
3082        char *export_path;
3083        struct vfsmount *root_mnt;
3084        int error;
3085
3086        dprintk("--> nfs4_referral_get_sb()\n");
3087
3088        export_path = data->mnt_path;
3089        data->mnt_path = "/";
3090
3091        root_mnt = nfs_do_root_mount(&nfs4_remote_referral_fs_type,
3092                        flags, data, data->hostname);
3093        data->mnt_path = export_path;
3094
3095        error = PTR_ERR(root_mnt);
3096        if (IS_ERR(root_mnt))
3097                goto out;
3098
3099        error = nfs_follow_remote_path(root_mnt, export_path, mnt);
3100out:
3101        dprintk("<-- nfs4_referral_get_sb() = %d%s\n", error,
3102                        error != 0 ? " [error]" : "");
3103        return error;
3104}
3105
3106#endif /* CONFIG_NFS_V4 */
3107
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.