linux/fs/nfs/nfs4client.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
   3 * Written by David Howells (dhowells@redhat.com)
   4 */
   5#include <linux/module.h>
   6#include <linux/nfs_fs.h>
   7#include <linux/nfs_idmap.h>
   8#include <linux/nfs_mount.h>
   9#include <linux/sunrpc/auth.h>
  10#include <linux/sunrpc/xprt.h>
  11#include <linux/sunrpc/bc_xprt.h>
  12#include "internal.h"
  13#include "callback.h"
  14#include "delegation.h"
  15#include "pnfs.h"
  16#include "netns.h"
  17
  18#define NFSDBG_FACILITY         NFSDBG_CLIENT
  19
  20/*
  21 * Get a unique NFSv4.0 callback identifier which will be used
  22 * by the V4.0 callback service to lookup the nfs_client struct
  23 */
  24static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
  25{
  26        int ret = 0;
  27        struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  28
  29        if (clp->rpc_ops->version != 4 || minorversion != 0)
  30                return ret;
  31retry:
  32        if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
  33                return -ENOMEM;
  34        spin_lock(&nn->nfs_client_lock);
  35        ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
  36        spin_unlock(&nn->nfs_client_lock);
  37        if (ret == -EAGAIN)
  38                goto retry;
  39        return ret;
  40}
  41
  42#ifdef CONFIG_NFS_V4_1
  43static void nfs4_shutdown_session(struct nfs_client *clp)
  44{
  45        if (nfs4_has_session(clp)) {
  46                nfs4_destroy_session(clp->cl_session);
  47                nfs4_destroy_clientid(clp);
  48        }
  49
  50}
  51#else /* CONFIG_NFS_V4_1 */
  52static void nfs4_shutdown_session(struct nfs_client *clp)
  53{
  54}
  55#endif /* CONFIG_NFS_V4_1 */
  56
  57struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
  58{
  59        int err;
  60        struct nfs_client *clp = nfs_alloc_client(cl_init);
  61        if (IS_ERR(clp))
  62                return clp;
  63
  64        err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
  65        if (err)
  66                goto error;
  67
  68        spin_lock_init(&clp->cl_lock);
  69        INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
  70        rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
  71        clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
  72        clp->cl_minorversion = cl_init->minorversion;
  73        clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
  74        return clp;
  75
  76error:
  77        nfs_free_client(clp);
  78        return ERR_PTR(err);
  79}
  80
  81/*
  82 * Destroy the NFS4 callback service
  83 */
  84static void nfs4_destroy_callback(struct nfs_client *clp)
  85{
  86        if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  87                nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
  88}
  89
  90static void nfs4_shutdown_client(struct nfs_client *clp)
  91{
  92        if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
  93                nfs4_kill_renewd(clp);
  94        nfs4_shutdown_session(clp);
  95        nfs4_destroy_callback(clp);
  96        if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
  97                nfs_idmap_delete(clp);
  98
  99        rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
 100        kfree(clp->cl_serverowner);
 101        kfree(clp->cl_serverscope);
 102        kfree(clp->cl_implid);
 103}
 104
 105void nfs4_free_client(struct nfs_client *clp)
 106{
 107        nfs4_shutdown_client(clp);
 108        nfs_free_client(clp);
 109}
 110
 111/*
 112 * Initialize the NFS4 callback service
 113 */
 114static int nfs4_init_callback(struct nfs_client *clp)
 115{
 116        int error;
 117
 118        if (clp->rpc_ops->version == 4) {
 119                struct rpc_xprt *xprt;
 120
 121                xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
 122
 123                if (nfs4_has_session(clp)) {
 124                        error = xprt_setup_backchannel(xprt,
 125                                                NFS41_BC_MIN_CALLBACKS);
 126                        if (error < 0)
 127                                return error;
 128                }
 129
 130                error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
 131                if (error < 0) {
 132                        dprintk("%s: failed to start callback. Error = %d\n",
 133                                __func__, error);
 134                        return error;
 135                }
 136                __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
 137        }
 138        return 0;
 139}
 140
 141/*
 142 * Initialize the minor version specific parts of an NFS4 client record
 143 */
 144static int nfs4_init_client_minor_version(struct nfs_client *clp)
 145{
 146#if defined(CONFIG_NFS_V4_1)
 147        if (clp->cl_mvops->minor_version) {
 148                struct nfs4_session *session = NULL;
 149                /*
 150                 * Create the session and mark it expired.
 151                 * When a SEQUENCE operation encounters the expired session
 152                 * it will do session recovery to initialize it.
 153                 */
 154                session = nfs4_alloc_session(clp);
 155                if (!session)
 156                        return -ENOMEM;
 157
 158                clp->cl_session = session;
 159                /*
 160                 * The create session reply races with the server back
 161                 * channel probe. Mark the client NFS_CS_SESSION_INITING
 162                 * so that the client back channel can find the
 163                 * nfs_client struct
 164                 */
 165                nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
 166        }
 167#endif /* CONFIG_NFS_V4_1 */
 168
 169        return nfs4_init_callback(clp);
 170}
 171
 172/**
 173 * nfs4_init_client - Initialise an NFS4 client record
 174 *
 175 * @clp: nfs_client to initialise
 176 * @timeparms: timeout parameters for underlying RPC transport
 177 * @ip_addr: callback IP address in presentation format
 178 * @authflavor: authentication flavor for underlying RPC transport
 179 *
 180 * Returns pointer to an NFS client, or an ERR_PTR value.
 181 */
 182struct nfs_client *nfs4_init_client(struct nfs_client *clp,
 183                                    const struct rpc_timeout *timeparms,
 184                                    const char *ip_addr,
 185                                    rpc_authflavor_t authflavour)
 186{
 187        char buf[INET6_ADDRSTRLEN + 1];
 188        struct nfs_client *old;
 189        int error;
 190
 191        if (clp->cl_cons_state == NFS_CS_READY) {
 192                /* the client is initialised already */
 193                dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
 194                return clp;
 195        }
 196
 197        /* Check NFS protocol revision and initialize RPC op vector */
 198        clp->rpc_ops = &nfs_v4_clientops;
 199
 200        __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
 201        error = nfs_create_rpc_client(clp, timeparms, authflavour);
 202        if (error < 0)
 203                goto error;
 204
 205        /* If no clientaddr= option was specified, find a usable cb address */
 206        if (ip_addr == NULL) {
 207                struct sockaddr_storage cb_addr;
 208                struct sockaddr *sap = (struct sockaddr *)&cb_addr;
 209
 210                error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
 211                if (error < 0)
 212                        goto error;
 213                error = rpc_ntop(sap, buf, sizeof(buf));
 214                if (error < 0)
 215                        goto error;
 216                ip_addr = (const char *)buf;
 217        }
 218        strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
 219
 220        error = nfs_idmap_new(clp);
 221        if (error < 0) {
 222                dprintk("%s: failed to create idmapper. Error = %d\n",
 223                        __func__, error);
 224                goto error;
 225        }
 226        __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
 227
 228        error = nfs4_init_client_minor_version(clp);
 229        if (error < 0)
 230                goto error;
 231
 232        if (!nfs4_has_session(clp))
 233                nfs_mark_client_ready(clp, NFS_CS_READY);
 234
 235        error = nfs4_discover_server_trunking(clp, &old);
 236        if (error < 0)
 237                goto error;
 238        nfs_put_client(clp);
 239        if (clp != old) {
 240                clp->cl_preserve_clid = true;
 241                clp = old;
 242        }
 243
 244        return clp;
 245
 246error:
 247        nfs_mark_client_ready(clp, error);
 248        nfs_put_client(clp);
 249        dprintk("<-- nfs4_init_client() = xerror %d\n", error);
 250        return ERR_PTR(error);
 251}
 252
 253/*
 254 * SETCLIENTID just did a callback update with the callback ident in
 255 * "drop," but server trunking discovery claims "drop" and "keep" are
 256 * actually the same server.  Swap the callback IDs so that "keep"
 257 * will continue to use the callback ident the server now knows about,
 258 * and so that "keep"'s original callback ident is destroyed when
 259 * "drop" is freed.
 260 */
 261static void nfs4_swap_callback_idents(struct nfs_client *keep,
 262                                      struct nfs_client *drop)
 263{
 264        struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
 265        unsigned int save = keep->cl_cb_ident;
 266
 267        if (keep->cl_cb_ident == drop->cl_cb_ident)
 268                return;
 269
 270        dprintk("%s: keeping callback ident %u and dropping ident %u\n",
 271                __func__, keep->cl_cb_ident, drop->cl_cb_ident);
 272
 273        spin_lock(&nn->nfs_client_lock);
 274
 275        idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
 276        keep->cl_cb_ident = drop->cl_cb_ident;
 277
 278        idr_replace(&nn->cb_ident_idr, drop, save);
 279        drop->cl_cb_ident = save;
 280
 281        spin_unlock(&nn->nfs_client_lock);
 282}
 283
 284/**
 285 * nfs40_walk_client_list - Find server that recognizes a client ID
 286 *
 287 * @new: nfs_client with client ID to test
 288 * @result: OUT: found nfs_client, or new
 289 * @cred: credential to use for trunking test
 290 *
 291 * Returns zero, a negative errno, or a negative NFS4ERR status.
 292 * If zero is returned, an nfs_client pointer is planted in "result."
 293 *
 294 * NB: nfs40_walk_client_list() relies on the new nfs_client being
 295 *     the last nfs_client on the list.
 296 */
 297int nfs40_walk_client_list(struct nfs_client *new,
 298                           struct nfs_client **result,
 299                           struct rpc_cred *cred)
 300{
 301        struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
 302        struct nfs_client *pos, *n, *prev = NULL;
 303        struct nfs4_setclientid_res clid = {
 304                .clientid       = new->cl_clientid,
 305                .confirm        = new->cl_confirm,
 306        };
 307        int status = -NFS4ERR_STALE_CLIENTID;
 308
 309        spin_lock(&nn->nfs_client_lock);
 310        list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) {
 311                /* If "pos" isn't marked ready, we can't trust the
 312                 * remaining fields in "pos" */
 313                if (pos->cl_cons_state < NFS_CS_READY)
 314                        continue;
 315
 316                if (pos->rpc_ops != new->rpc_ops)
 317                        continue;
 318
 319                if (pos->cl_proto != new->cl_proto)
 320                        continue;
 321
 322                if (pos->cl_minorversion != new->cl_minorversion)
 323                        continue;
 324
 325                if (pos->cl_clientid != new->cl_clientid)
 326                        continue;
 327
 328                atomic_inc(&pos->cl_count);
 329                spin_unlock(&nn->nfs_client_lock);
 330
 331                if (prev)
 332                        nfs_put_client(prev);
 333                prev = pos;
 334
 335                status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
 336                switch (status) {
 337                case -NFS4ERR_STALE_CLIENTID:
 338                        break;
 339                case 0:
 340                        nfs4_swap_callback_idents(pos, new);
 341
 342                        prev = NULL;
 343                        *result = pos;
 344                        dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
 345                                __func__, pos, atomic_read(&pos->cl_count));
 346                default:
 347                        goto out;
 348                }
 349
 350                spin_lock(&nn->nfs_client_lock);
 351        }
 352        spin_unlock(&nn->nfs_client_lock);
 353
 354        /* No match found. The server lost our clientid */
 355out:
 356        if (prev)
 357                nfs_put_client(prev);
 358        dprintk("NFS: <-- %s status = %d\n", __func__, status);
 359        return status;
 360}
 361
 362#ifdef CONFIG_NFS_V4_1
 363/*
 364 * Returns true if the client IDs match
 365 */
 366static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
 367{
 368        if (a->cl_clientid != b->cl_clientid) {
 369                dprintk("NFS: --> %s client ID %llx does not match %llx\n",
 370                        __func__, a->cl_clientid, b->cl_clientid);
 371                return false;
 372        }
 373        dprintk("NFS: --> %s client ID %llx matches %llx\n",
 374                __func__, a->cl_clientid, b->cl_clientid);
 375        return true;
 376}
 377
 378/*
 379 * Returns true if the server owners match
 380 */
 381static bool
 382nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
 383{
 384        struct nfs41_server_owner *o1 = a->cl_serverowner;
 385        struct nfs41_server_owner *o2 = b->cl_serverowner;
 386
 387        if (o1->minor_id != o2->minor_id) {
 388                dprintk("NFS: --> %s server owner minor IDs do not match\n",
 389                        __func__);
 390                return false;
 391        }
 392
 393        if (o1->major_id_sz != o2->major_id_sz)
 394                goto out_major_mismatch;
 395        if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
 396                goto out_major_mismatch;
 397
 398        dprintk("NFS: --> %s server owners match\n", __func__);
 399        return true;
 400
 401out_major_mismatch:
 402        dprintk("NFS: --> %s server owner major IDs do not match\n",
 403                __func__);
 404        return false;
 405}
 406
 407/**
 408 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
 409 *
 410 * @new: nfs_client with client ID to test
 411 * @result: OUT: found nfs_client, or new
 412 * @cred: credential to use for trunking test
 413 *
 414 * Returns zero, a negative errno, or a negative NFS4ERR status.
 415 * If zero is returned, an nfs_client pointer is planted in "result."
 416 *
 417 * NB: nfs41_walk_client_list() relies on the new nfs_client being
 418 *     the last nfs_client on the list.
 419 */
 420int nfs41_walk_client_list(struct nfs_client *new,
 421                           struct nfs_client **result,
 422                           struct rpc_cred *cred)
 423{
 424        struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
 425        struct nfs_client *pos, *n, *prev = NULL;
 426        int status = -NFS4ERR_STALE_CLIENTID;
 427
 428        spin_lock(&nn->nfs_client_lock);
 429        list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) {
 430                /* If "pos" isn't marked ready, we can't trust the
 431                 * remaining fields in "pos", especially the client
 432                 * ID and serverowner fields.  Wait for CREATE_SESSION
 433                 * to finish. */
 434                if (pos->cl_cons_state < NFS_CS_READY) {
 435                        atomic_inc(&pos->cl_count);
 436                        spin_unlock(&nn->nfs_client_lock);
 437
 438                        if (prev)
 439                                nfs_put_client(prev);
 440                        prev = pos;
 441
 442                        nfs4_schedule_lease_recovery(pos);
 443                        status = nfs_wait_client_init_complete(pos);
 444                        if (status < 0) {
 445                                nfs_put_client(pos);
 446                                spin_lock(&nn->nfs_client_lock);
 447                                continue;
 448                        }
 449                        status = pos->cl_cons_state;
 450                        spin_lock(&nn->nfs_client_lock);
 451                        if (status < 0)
 452                                continue;
 453                }
 454
 455                if (pos->rpc_ops != new->rpc_ops)
 456                        continue;
 457
 458                if (pos->cl_proto != new->cl_proto)
 459                        continue;
 460
 461                if (pos->cl_minorversion != new->cl_minorversion)
 462                        continue;
 463
 464                if (!nfs4_match_clientids(pos, new))
 465                        continue;
 466
 467                if (!nfs4_match_serverowners(pos, new))
 468                        continue;
 469
 470                atomic_inc(&pos->cl_count);
 471                spin_unlock(&nn->nfs_client_lock);
 472                dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
 473                        __func__, pos, atomic_read(&pos->cl_count));
 474
 475                *result = pos;
 476                return 0;
 477        }
 478
 479        /* No matching nfs_client found. */
 480        spin_unlock(&nn->nfs_client_lock);
 481        dprintk("NFS: <-- %s status = %d\n", __func__, status);
 482        return status;
 483}
 484#endif  /* CONFIG_NFS_V4_1 */
 485
 486static void nfs4_destroy_server(struct nfs_server *server)
 487{
 488        nfs_server_return_all_delegations(server);
 489        unset_pnfs_layoutdriver(server);
 490        nfs4_purge_state_owners(server);
 491}
 492
 493/*
 494 * NFSv4.0 callback thread helper
 495 *
 496 * Find a client by callback identifier
 497 */
 498struct nfs_client *
 499nfs4_find_client_ident(struct net *net, int cb_ident)
 500{
 501        struct nfs_client *clp;
 502        struct nfs_net *nn = net_generic(net, nfs_net_id);
 503
 504        spin_lock(&nn->nfs_client_lock);
 505        clp = idr_find(&nn->cb_ident_idr, cb_ident);
 506        if (clp)
 507                atomic_inc(&clp->cl_count);
 508        spin_unlock(&nn->nfs_client_lock);
 509        return clp;
 510}
 511
 512#if defined(CONFIG_NFS_V4_1)
 513/* Common match routine for v4.0 and v4.1 callback services */
 514static bool nfs4_cb_match_client(const struct sockaddr *addr,
 515                struct nfs_client *clp, u32 minorversion)
 516{
 517        struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
 518
 519        /* Don't match clients that failed to initialise */
 520        if (!(clp->cl_cons_state == NFS_CS_READY ||
 521            clp->cl_cons_state == NFS_CS_SESSION_INITING))
 522                return false;
 523
 524        smp_rmb();
 525
 526        /* Match the version and minorversion */
 527        if (clp->rpc_ops->version != 4 ||
 528            clp->cl_minorversion != minorversion)
 529                return false;
 530
 531        /* Match only the IP address, not the port number */
 532        if (!nfs_sockaddr_match_ipaddr(addr, clap))
 533                return false;
 534
 535        return true;
 536}
 537
 538/*
 539 * NFSv4.1 callback thread helper
 540 * For CB_COMPOUND calls, find a client by IP address, protocol version,
 541 * minorversion, and sessionID
 542 *
 543 * Returns NULL if no such client
 544 */
 545struct nfs_client *
 546nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
 547                           struct nfs4_sessionid *sid)
 548{
 549        struct nfs_client *clp;
 550        struct nfs_net *nn = net_generic(net, nfs_net_id);
 551
 552        spin_lock(&nn->nfs_client_lock);
 553        list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
 554                if (nfs4_cb_match_client(addr, clp, 1) == false)
 555                        continue;
 556
 557                if (!nfs4_has_session(clp))
 558                        continue;
 559
 560                /* Match sessionid*/
 561                if (memcmp(clp->cl_session->sess_id.data,
 562                    sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
 563                        continue;
 564
 565                atomic_inc(&clp->cl_count);
 566                spin_unlock(&nn->nfs_client_lock);
 567                return clp;
 568        }
 569        spin_unlock(&nn->nfs_client_lock);
 570        return NULL;
 571}
 572
 573#else /* CONFIG_NFS_V4_1 */
 574
 575struct nfs_client *
 576nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
 577                           struct nfs4_sessionid *sid)
 578{
 579        return NULL;
 580}
 581#endif /* CONFIG_NFS_V4_1 */
 582
 583/*
 584 * Set up an NFS4 client
 585 */
 586static int nfs4_set_client(struct nfs_server *server,
 587                const char *hostname,
 588                const struct sockaddr *addr,
 589                const size_t addrlen,
 590                const char *ip_addr,
 591                rpc_authflavor_t authflavour,
 592                int proto, const struct rpc_timeout *timeparms,
 593                u32 minorversion, struct net *net)
 594{
 595        struct nfs_client_initdata cl_init = {
 596                .hostname = hostname,
 597                .addr = addr,
 598                .addrlen = addrlen,
 599                .nfs_mod = &nfs_v4,
 600                .proto = proto,
 601                .minorversion = minorversion,
 602                .net = net,
 603        };
 604        struct nfs_client *clp;
 605        int error;
 606
 607        dprintk("--> nfs4_set_client()\n");
 608
 609        if (server->flags & NFS_MOUNT_NORESVPORT)
 610                set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
 611
 612        /* Allocate or find a client reference we can use */
 613        clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
 614        if (IS_ERR(clp)) {
 615                error = PTR_ERR(clp);
 616                goto error;
 617        }
 618
 619        /*
 620         * Query for the lease time on clientid setup or renewal
 621         *
 622         * Note that this will be set on nfs_clients that were created
 623         * only for the DS role and did not set this bit, but now will
 624         * serve a dual role.
 625         */
 626        set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
 627
 628        server->nfs_client = clp;
 629        dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
 630        return 0;
 631error:
 632        dprintk("<-- nfs4_set_client() = xerror %d\n", error);
 633        return error;
 634}
 635
 636/*
 637 * Set up a pNFS Data Server client.
 638 *
 639 * Return any existing nfs_client that matches server address,port,version
 640 * and minorversion.
 641 *
 642 * For a new nfs_client, use a soft mount (default), a low retrans and a
 643 * low timeout interval so that if a connection is lost, we retry through
 644 * the MDS.
 645 */
 646struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
 647                const struct sockaddr *ds_addr, int ds_addrlen,
 648                int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
 649{
 650        struct nfs_client_initdata cl_init = {
 651                .addr = ds_addr,
 652                .addrlen = ds_addrlen,
 653                .nfs_mod = &nfs_v4,
 654                .proto = ds_proto,
 655                .minorversion = mds_clp->cl_minorversion,
 656                .net = mds_clp->cl_net,
 657        };
 658        struct rpc_timeout ds_timeout;
 659        struct nfs_client *clp;
 660
 661        /*
 662         * Set an authflavor equual to the MDS value. Use the MDS nfs_client
 663         * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
 664         * (section 13.1 RFC 5661).
 665         */
 666        nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
 667        clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
 668                             mds_clp->cl_rpcclient->cl_auth->au_flavor);
 669
 670        dprintk("<-- %s %p\n", __func__, clp);
 671        return clp;
 672}
 673EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
 674
 675/*
 676 * Session has been established, and the client marked ready.
 677 * Set the mount rsize and wsize with negotiated fore channel
 678 * attributes which will be bound checked in nfs_server_set_fsinfo.
 679 */
 680static void nfs4_session_set_rwsize(struct nfs_server *server)
 681{
 682#ifdef CONFIG_NFS_V4_1
 683        struct nfs4_session *sess;
 684        u32 server_resp_sz;
 685        u32 server_rqst_sz;
 686
 687        if (!nfs4_has_session(server->nfs_client))
 688                return;
 689        sess = server->nfs_client->cl_session;
 690        server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
 691        server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
 692
 693        if (server->rsize > server_resp_sz)
 694                server->rsize = server_resp_sz;
 695        if (server->wsize > server_rqst_sz)
 696                server->wsize = server_rqst_sz;
 697#endif /* CONFIG_NFS_V4_1 */
 698}
 699
 700static int nfs4_server_common_setup(struct nfs_server *server,
 701                struct nfs_fh *mntfh)
 702{
 703        struct nfs_fattr *fattr;
 704        int error;
 705
 706        BUG_ON(!server->nfs_client);
 707        BUG_ON(!server->nfs_client->rpc_ops);
 708        BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
 709
 710        /* data servers support only a subset of NFSv4.1 */
 711        if (is_ds_only_client(server->nfs_client))
 712                return -EPROTONOSUPPORT;
 713
 714        fattr = nfs_alloc_fattr();
 715        if (fattr == NULL)
 716                return -ENOMEM;
 717
 718        /* We must ensure the session is initialised first */
 719        error = nfs4_init_session(server);
 720        if (error < 0)
 721                goto out;
 722
 723        /* Probe the root fh to retrieve its FSID and filehandle */
 724        error = nfs4_get_rootfh(server, mntfh);
 725        if (error < 0)
 726                goto out;
 727
 728        dprintk("Server FSID: %llx:%llx\n",
 729                        (unsigned long long) server->fsid.major,
 730                        (unsigned long long) server->fsid.minor);
 731        dprintk("Mount FH: %d\n", mntfh->size);
 732
 733        nfs4_session_set_rwsize(server);
 734
 735        error = nfs_probe_fsinfo(server, mntfh, fattr);
 736        if (error < 0)
 737                goto out;
 738
 739        if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
 740                server->namelen = NFS4_MAXNAMLEN;
 741
 742        nfs_server_insert_lists(server);
 743        server->mount_time = jiffies;
 744        server->destroy = nfs4_destroy_server;
 745out:
 746        nfs_free_fattr(fattr);
 747        return error;
 748}
 749
 750/*
 751 * Create a version 4 volume record
 752 */
 753static int nfs4_init_server(struct nfs_server *server,
 754                const struct nfs_parsed_mount_data *data)
 755{
 756        struct rpc_timeout timeparms;
 757        int error;
 758
 759        dprintk("--> nfs4_init_server()\n");
 760
 761        nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
 762                        data->timeo, data->retrans);
 763
 764        /* Initialise the client representation from the mount data */
 765        server->flags = data->flags;
 766        server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
 767        if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
 768                        server->caps |= NFS_CAP_READDIRPLUS;
 769        server->options = data->options;
 770
 771        /* Get a client record */
 772        error = nfs4_set_client(server,
 773                        data->nfs_server.hostname,
 774                        (const struct sockaddr *)&data->nfs_server.address,
 775                        data->nfs_server.addrlen,
 776                        data->client_address,
 777                        data->auth_flavors[0],
 778                        data->nfs_server.protocol,
 779                        &timeparms,
 780                        data->minorversion,
 781                        data->net);
 782        if (error < 0)
 783                goto error;
 784
 785        /*
 786         * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
 787         * authentication.
 788         */
 789        if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
 790                server->caps |= NFS_CAP_UIDGID_NOMAP;
 791
 792        if (data->rsize)
 793                server->rsize = nfs_block_size(data->rsize, NULL);
 794        if (data->wsize)
 795                server->wsize = nfs_block_size(data->wsize, NULL);
 796
 797        server->acregmin = data->acregmin * HZ;
 798        server->acregmax = data->acregmax * HZ;
 799        server->acdirmin = data->acdirmin * HZ;
 800        server->acdirmax = data->acdirmax * HZ;
 801
 802        server->port = data->nfs_server.port;
 803
 804        error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
 805
 806error:
 807        /* Done */
 808        dprintk("<-- nfs4_init_server() = %d\n", error);
 809        return error;
 810}
 811
 812/*
 813 * Create a version 4 volume record
 814 * - keyed on server and FSID
 815 */
 816/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
 817                                      struct nfs_fh *mntfh)*/
 818struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
 819                                      struct nfs_subversion *nfs_mod)
 820{
 821        struct nfs_server *server;
 822        int error;
 823
 824        dprintk("--> nfs4_create_server()\n");
 825
 826        server = nfs_alloc_server();
 827        if (!server)
 828                return ERR_PTR(-ENOMEM);
 829
 830        /* set up the general RPC client */
 831        error = nfs4_init_server(server, mount_info->parsed);
 832        if (error < 0)
 833                goto error;
 834
 835        error = nfs4_server_common_setup(server, mount_info->mntfh);
 836        if (error < 0)
 837                goto error;
 838
 839        dprintk("<-- nfs4_create_server() = %p\n", server);
 840        return server;
 841
 842error:
 843        nfs_free_server(server);
 844        dprintk("<-- nfs4_create_server() = error %d\n", error);
 845        return ERR_PTR(error);
 846}
 847
 848/*
 849 * Create an NFS4 referral server record
 850 */
 851struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
 852                                               struct nfs_fh *mntfh)
 853{
 854        struct nfs_client *parent_client;
 855        struct nfs_server *server, *parent_server;
 856        int error;
 857
 858        dprintk("--> nfs4_create_referral_server()\n");
 859
 860        server = nfs_alloc_server();
 861        if (!server)
 862                return ERR_PTR(-ENOMEM);
 863
 864        parent_server = NFS_SB(data->sb);
 865        parent_client = parent_server->nfs_client;
 866
 867        /* Initialise the client representation from the parent server */
 868        nfs_server_copy_userdata(server, parent_server);
 869        server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
 870
 871        /* Get a client representation.
 872         * Note: NFSv4 always uses TCP, */
 873        error = nfs4_set_client(server, data->hostname,
 874                                data->addr,
 875                                data->addrlen,
 876                                parent_client->cl_ipaddr,
 877                                data->authflavor,
 878                                rpc_protocol(parent_server->client),
 879                                parent_server->client->cl_timeout,
 880                                parent_client->cl_mvops->minor_version,
 881                                parent_client->cl_net);
 882        if (error < 0)
 883                goto error;
 884
 885        error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
 886        if (error < 0)
 887                goto error;
 888
 889        error = nfs4_server_common_setup(server, mntfh);
 890        if (error < 0)
 891                goto error;
 892
 893        dprintk("<-- nfs_create_referral_server() = %p\n", server);
 894        return server;
 895
 896error:
 897        nfs_free_server(server);
 898        dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
 899        return ERR_PTR(error);
 900}
 901
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.