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