linux/fs/nfs/nfs4state.c
<<
>>
Prefs
   1/*
   2 *  fs/nfs/nfs4state.c
   3 *
   4 *  Client-side XDR for NFSv4.
   5 *
   6 *  Copyright (c) 2002 The Regents of the University of Michigan.
   7 *  All rights reserved.
   8 *
   9 *  Kendrick Smith <kmsmith@umich.edu>
  10 *
  11 *  Redistribution and use in source and binary forms, with or without
  12 *  modification, are permitted provided that the following conditions
  13 *  are met:
  14 *
  15 *  1. Redistributions of source code must retain the above copyright
  16 *     notice, this list of conditions and the following disclaimer.
  17 *  2. Redistributions in binary form must reproduce the above copyright
  18 *     notice, this list of conditions and the following disclaimer in the
  19 *     documentation and/or other materials provided with the distribution.
  20 *  3. Neither the name of the University nor the names of its
  21 *     contributors may be used to endorse or promote products derived
  22 *     from this software without specific prior written permission.
  23 *
  24 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  25 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27 *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  31 *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35 *
  36 * Implementation of the NFSv4 state model.  For the time being,
  37 * this is minimal, but will be made much more complex in a
  38 * subsequent patch.
  39 */
  40
  41#include <linux/kernel.h>
  42#include <linux/slab.h>
  43#include <linux/fs.h>
  44#include <linux/nfs_fs.h>
  45#include <linux/nfs_idmap.h>
  46#include <linux/kthread.h>
  47#include <linux/module.h>
  48#include <linux/random.h>
  49#include <linux/ratelimit.h>
  50#include <linux/workqueue.h>
  51#include <linux/bitops.h>
  52#include <linux/jiffies.h>
  53
  54#include <linux/sunrpc/clnt.h>
  55
  56#include "nfs4_fs.h"
  57#include "callback.h"
  58#include "delegation.h"
  59#include "internal.h"
  60#include "pnfs.h"
  61#include "netns.h"
  62
  63#define NFSDBG_FACILITY         NFSDBG_STATE
  64
  65#define OPENOWNER_POOL_SIZE     8
  66
  67const nfs4_stateid zero_stateid;
  68static DEFINE_MUTEX(nfs_clid_init_mutex);
  69static LIST_HEAD(nfs4_clientid_list);
  70
  71int nfs4_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
  72{
  73        struct nfs4_setclientid_res clid = {
  74                .clientid = clp->cl_clientid,
  75                .confirm = clp->cl_confirm,
  76        };
  77        unsigned short port;
  78        int status;
  79        struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  80
  81        if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
  82                goto do_confirm;
  83        port = nn->nfs_callback_tcpport;
  84        if (clp->cl_addr.ss_family == AF_INET6)
  85                port = nn->nfs_callback_tcpport6;
  86
  87        status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
  88        if (status != 0)
  89                goto out;
  90        clp->cl_clientid = clid.clientid;
  91        clp->cl_confirm = clid.confirm;
  92        set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  93do_confirm:
  94        status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
  95        if (status != 0)
  96                goto out;
  97        clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
  98        nfs4_schedule_state_renewal(clp);
  99out:
 100        return status;
 101}
 102
 103/**
 104 * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
 105 *
 106 * @clp: nfs_client under test
 107 * @result: OUT: found nfs_client, or clp
 108 * @cred: credential to use for trunking test
 109 *
 110 * Returns zero, a negative errno, or a negative NFS4ERR status.
 111 * If zero is returned, an nfs_client pointer is planted in
 112 * "result".
 113 *
 114 * Note: The returned client may not yet be marked ready.
 115 */
 116int nfs40_discover_server_trunking(struct nfs_client *clp,
 117                                   struct nfs_client **result,
 118                                   struct rpc_cred *cred)
 119{
 120        struct nfs4_setclientid_res clid = {
 121                .clientid = clp->cl_clientid,
 122                .confirm = clp->cl_confirm,
 123        };
 124        struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
 125        unsigned short port;
 126        int status;
 127
 128        port = nn->nfs_callback_tcpport;
 129        if (clp->cl_addr.ss_family == AF_INET6)
 130                port = nn->nfs_callback_tcpport6;
 131
 132        status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
 133        if (status != 0)
 134                goto out;
 135        clp->cl_clientid = clid.clientid;
 136        clp->cl_confirm = clid.confirm;
 137
 138        status = nfs40_walk_client_list(clp, result, cred);
 139        if (status == 0) {
 140                /* Sustain the lease, even if it's empty.  If the clientid4
 141                 * goes stale it's of no use for trunking discovery. */
 142                nfs4_schedule_state_renewal(*result);
 143        }
 144out:
 145        return status;
 146}
 147
 148struct rpc_cred *nfs4_get_machine_cred_locked(struct nfs_client *clp)
 149{
 150        struct rpc_cred *cred = NULL;
 151
 152        if (clp->cl_machine_cred != NULL)
 153                cred = get_rpccred(clp->cl_machine_cred);
 154        return cred;
 155}
 156
 157static void nfs4_clear_machine_cred(struct nfs_client *clp)
 158{
 159        struct rpc_cred *cred;
 160
 161        spin_lock(&clp->cl_lock);
 162        cred = clp->cl_machine_cred;
 163        clp->cl_machine_cred = NULL;
 164        spin_unlock(&clp->cl_lock);
 165        if (cred != NULL)
 166                put_rpccred(cred);
 167}
 168
 169static struct rpc_cred *
 170nfs4_get_renew_cred_server_locked(struct nfs_server *server)
 171{
 172        struct rpc_cred *cred = NULL;
 173        struct nfs4_state_owner *sp;
 174        struct rb_node *pos;
 175
 176        for (pos = rb_first(&server->state_owners);
 177             pos != NULL;
 178             pos = rb_next(pos)) {
 179                sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
 180                if (list_empty(&sp->so_states))
 181                        continue;
 182                cred = get_rpccred(sp->so_cred);
 183                break;
 184        }
 185        return cred;
 186}
 187
 188/**
 189 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
 190 * @clp: client state handle
 191 *
 192 * Returns an rpc_cred with reference count bumped, or NULL.
 193 * Caller must hold clp->cl_lock.
 194 */
 195struct rpc_cred *nfs4_get_renew_cred_locked(struct nfs_client *clp)
 196{
 197        struct rpc_cred *cred = NULL;
 198        struct nfs_server *server;
 199
 200        /* Use machine credentials if available */
 201        cred = nfs4_get_machine_cred_locked(clp);
 202        if (cred != NULL)
 203                goto out;
 204
 205        rcu_read_lock();
 206        list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
 207                cred = nfs4_get_renew_cred_server_locked(server);
 208                if (cred != NULL)
 209                        break;
 210        }
 211        rcu_read_unlock();
 212
 213out:
 214        return cred;
 215}
 216
 217#if defined(CONFIG_NFS_V4_1)
 218
 219static int nfs41_setup_state_renewal(struct nfs_client *clp)
 220{
 221        int status;
 222        struct nfs_fsinfo fsinfo;
 223
 224        if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
 225                nfs4_schedule_state_renewal(clp);
 226                return 0;
 227        }
 228
 229        status = nfs4_proc_get_lease_time(clp, &fsinfo);
 230        if (status == 0) {
 231                /* Update lease time and schedule renewal */
 232                spin_lock(&clp->cl_lock);
 233                clp->cl_lease_time = fsinfo.lease_time * HZ;
 234                clp->cl_last_renewal = jiffies;
 235                spin_unlock(&clp->cl_lock);
 236
 237                nfs4_schedule_state_renewal(clp);
 238        }
 239
 240        return status;
 241}
 242
 243/*
 244 * Back channel returns NFS4ERR_DELAY for new requests when
 245 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
 246 * is ended.
 247 */
 248static void nfs4_end_drain_session(struct nfs_client *clp)
 249{
 250        struct nfs4_session *ses = clp->cl_session;
 251        struct nfs4_slot_table *tbl;
 252        int max_slots;
 253
 254        if (ses == NULL)
 255                return;
 256        tbl = &ses->fc_slot_table;
 257        if (test_and_clear_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
 258                spin_lock(&tbl->slot_tbl_lock);
 259                max_slots = tbl->max_slots;
 260                while (max_slots--) {
 261                        if (rpc_wake_up_first(&tbl->slot_tbl_waitq,
 262                                                nfs4_set_task_privileged,
 263                                                NULL) == NULL)
 264                                break;
 265                }
 266                spin_unlock(&tbl->slot_tbl_lock);
 267        }
 268}
 269
 270static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table *tbl)
 271{
 272        spin_lock(&tbl->slot_tbl_lock);
 273        if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
 274                INIT_COMPLETION(tbl->complete);
 275                spin_unlock(&tbl->slot_tbl_lock);
 276                return wait_for_completion_interruptible(&tbl->complete);
 277        }
 278        spin_unlock(&tbl->slot_tbl_lock);
 279        return 0;
 280}
 281
 282static int nfs4_begin_drain_session(struct nfs_client *clp)
 283{
 284        struct nfs4_session *ses = clp->cl_session;
 285        int ret = 0;
 286
 287        set_bit(NFS4_SESSION_DRAINING, &ses->session_state);
 288        /* back channel */
 289        ret = nfs4_wait_on_slot_tbl(&ses->bc_slot_table);
 290        if (ret)
 291                return ret;
 292        /* fore channel */
 293        return nfs4_wait_on_slot_tbl(&ses->fc_slot_table);
 294}
 295
 296static void nfs41_finish_session_reset(struct nfs_client *clp)
 297{
 298        clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 299        clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
 300        /* create_session negotiated new slot table */
 301        clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
 302        clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
 303        nfs41_setup_state_renewal(clp);
 304}
 305
 306int nfs41_init_clientid(struct nfs_client *clp, struct rpc_cred *cred)
 307{
 308        int status;
 309
 310        if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
 311                goto do_confirm;
 312        nfs4_begin_drain_session(clp);
 313        status = nfs4_proc_exchange_id(clp, cred);
 314        if (status != 0)
 315                goto out;
 316        set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 317do_confirm:
 318        status = nfs4_proc_create_session(clp, cred);
 319        if (status != 0)
 320                goto out;
 321        nfs41_finish_session_reset(clp);
 322        nfs_mark_client_ready(clp, NFS_CS_READY);
 323out:
 324        return status;
 325}
 326
 327/**
 328 * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
 329 *
 330 * @clp: nfs_client under test
 331 * @result: OUT: found nfs_client, or clp
 332 * @cred: credential to use for trunking test
 333 *
 334 * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
 335 * If NFS4_OK is returned, an nfs_client pointer is planted in
 336 * "result".
 337 *
 338 * Note: The returned client may not yet be marked ready.
 339 */
 340int nfs41_discover_server_trunking(struct nfs_client *clp,
 341                                   struct nfs_client **result,
 342                                   struct rpc_cred *cred)
 343{
 344        int status;
 345
 346        status = nfs4_proc_exchange_id(clp, cred);
 347        if (status != NFS4_OK)
 348                return status;
 349        set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 350
 351        return nfs41_walk_client_list(clp, result, cred);
 352}
 353
 354struct rpc_cred *nfs4_get_exchange_id_cred(struct nfs_client *clp)
 355{
 356        struct rpc_cred *cred;
 357
 358        spin_lock(&clp->cl_lock);
 359        cred = nfs4_get_machine_cred_locked(clp);
 360        spin_unlock(&clp->cl_lock);
 361        return cred;
 362}
 363
 364#endif /* CONFIG_NFS_V4_1 */
 365
 366static struct rpc_cred *
 367nfs4_get_setclientid_cred_server(struct nfs_server *server)
 368{
 369        struct nfs_client *clp = server->nfs_client;
 370        struct rpc_cred *cred = NULL;
 371        struct nfs4_state_owner *sp;
 372        struct rb_node *pos;
 373
 374        spin_lock(&clp->cl_lock);
 375        pos = rb_first(&server->state_owners);
 376        if (pos != NULL) {
 377                sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
 378                cred = get_rpccred(sp->so_cred);
 379        }
 380        spin_unlock(&clp->cl_lock);
 381        return cred;
 382}
 383
 384/**
 385 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
 386 * @clp: client state handle
 387 *
 388 * Returns an rpc_cred with reference count bumped, or NULL.
 389 */
 390struct rpc_cred *nfs4_get_setclientid_cred(struct nfs_client *clp)
 391{
 392        struct nfs_server *server;
 393        struct rpc_cred *cred;
 394
 395        spin_lock(&clp->cl_lock);
 396        cred = nfs4_get_machine_cred_locked(clp);
 397        spin_unlock(&clp->cl_lock);
 398        if (cred != NULL)
 399                goto out;
 400
 401        rcu_read_lock();
 402        list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
 403                cred = nfs4_get_setclientid_cred_server(server);
 404                if (cred != NULL)
 405                        break;
 406        }
 407        rcu_read_unlock();
 408
 409out:
 410        return cred;
 411}
 412
 413static struct nfs4_state_owner *
 414nfs4_find_state_owner_locked(struct nfs_server *server, struct rpc_cred *cred)
 415{
 416        struct rb_node **p = &server->state_owners.rb_node,
 417                       *parent = NULL;
 418        struct nfs4_state_owner *sp;
 419
 420        while (*p != NULL) {
 421                parent = *p;
 422                sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
 423
 424                if (cred < sp->so_cred)
 425                        p = &parent->rb_left;
 426                else if (cred > sp->so_cred)
 427                        p = &parent->rb_right;
 428                else {
 429                        if (!list_empty(&sp->so_lru))
 430                                list_del_init(&sp->so_lru);
 431                        atomic_inc(&sp->so_count);
 432                        return sp;
 433                }
 434        }
 435        return NULL;
 436}
 437
 438static struct nfs4_state_owner *
 439nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
 440{
 441        struct nfs_server *server = new->so_server;
 442        struct rb_node **p = &server->state_owners.rb_node,
 443                       *parent = NULL;
 444        struct nfs4_state_owner *sp;
 445        int err;
 446
 447        while (*p != NULL) {
 448                parent = *p;
 449                sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
 450
 451                if (new->so_cred < sp->so_cred)
 452                        p = &parent->rb_left;
 453                else if (new->so_cred > sp->so_cred)
 454                        p = &parent->rb_right;
 455                else {
 456                        if (!list_empty(&sp->so_lru))
 457                                list_del_init(&sp->so_lru);
 458                        atomic_inc(&sp->so_count);
 459                        return sp;
 460                }
 461        }
 462        err = ida_get_new(&server->openowner_id, &new->so_seqid.owner_id);
 463        if (err)
 464                return ERR_PTR(err);
 465        rb_link_node(&new->so_server_node, parent, p);
 466        rb_insert_color(&new->so_server_node, &server->state_owners);
 467        return new;
 468}
 469
 470static void
 471nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
 472{
 473        struct nfs_server *server = sp->so_server;
 474
 475        if (!RB_EMPTY_NODE(&sp->so_server_node))
 476                rb_erase(&sp->so_server_node, &server->state_owners);
 477        ida_remove(&server->openowner_id, sp->so_seqid.owner_id);
 478}
 479
 480static void
 481nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
 482{
 483        sc->create_time = ktime_get();
 484        sc->flags = 0;
 485        sc->counter = 0;
 486        spin_lock_init(&sc->lock);
 487        INIT_LIST_HEAD(&sc->list);
 488        rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
 489}
 490
 491static void
 492nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
 493{
 494        rpc_destroy_wait_queue(&sc->wait);
 495}
 496
 497/*
 498 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
 499 * create a new state_owner.
 500 *
 501 */
 502static struct nfs4_state_owner *
 503nfs4_alloc_state_owner(struct nfs_server *server,
 504                struct rpc_cred *cred,
 505                gfp_t gfp_flags)
 506{
 507        struct nfs4_state_owner *sp;
 508
 509        sp = kzalloc(sizeof(*sp), gfp_flags);
 510        if (!sp)
 511                return NULL;
 512        sp->so_server = server;
 513        sp->so_cred = get_rpccred(cred);
 514        spin_lock_init(&sp->so_lock);
 515        INIT_LIST_HEAD(&sp->so_states);
 516        nfs4_init_seqid_counter(&sp->so_seqid);
 517        atomic_set(&sp->so_count, 1);
 518        INIT_LIST_HEAD(&sp->so_lru);
 519        return sp;
 520}
 521
 522static void
 523nfs4_drop_state_owner(struct nfs4_state_owner *sp)
 524{
 525        struct rb_node *rb_node = &sp->so_server_node;
 526
 527        if (!RB_EMPTY_NODE(rb_node)) {
 528                struct nfs_server *server = sp->so_server;
 529                struct nfs_client *clp = server->nfs_client;
 530
 531                spin_lock(&clp->cl_lock);
 532                if (!RB_EMPTY_NODE(rb_node)) {
 533                        rb_erase(rb_node, &server->state_owners);
 534                        RB_CLEAR_NODE(rb_node);
 535                }
 536                spin_unlock(&clp->cl_lock);
 537        }
 538}
 539
 540static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
 541{
 542        nfs4_destroy_seqid_counter(&sp->so_seqid);
 543        put_rpccred(sp->so_cred);
 544        kfree(sp);
 545}
 546
 547static void nfs4_gc_state_owners(struct nfs_server *server)
 548{
 549        struct nfs_client *clp = server->nfs_client;
 550        struct nfs4_state_owner *sp, *tmp;
 551        unsigned long time_min, time_max;
 552        LIST_HEAD(doomed);
 553
 554        spin_lock(&clp->cl_lock);
 555        time_max = jiffies;
 556        time_min = (long)time_max - (long)clp->cl_lease_time;
 557        list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
 558                /* NB: LRU is sorted so that oldest is at the head */
 559                if (time_in_range(sp->so_expires, time_min, time_max))
 560                        break;
 561                list_move(&sp->so_lru, &doomed);
 562                nfs4_remove_state_owner_locked(sp);
 563        }
 564        spin_unlock(&clp->cl_lock);
 565
 566        list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
 567                list_del(&sp->so_lru);
 568                nfs4_free_state_owner(sp);
 569        }
 570}
 571
 572/**
 573 * nfs4_get_state_owner - Look up a state owner given a credential
 574 * @server: nfs_server to search
 575 * @cred: RPC credential to match
 576 *
 577 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
 578 */
 579struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
 580                                              struct rpc_cred *cred,
 581                                              gfp_t gfp_flags)
 582{
 583        struct nfs_client *clp = server->nfs_client;
 584        struct nfs4_state_owner *sp, *new;
 585
 586        spin_lock(&clp->cl_lock);
 587        sp = nfs4_find_state_owner_locked(server, cred);
 588        spin_unlock(&clp->cl_lock);
 589        if (sp != NULL)
 590                goto out;
 591        new = nfs4_alloc_state_owner(server, cred, gfp_flags);
 592        if (new == NULL)
 593                goto out;
 594        do {
 595                if (ida_pre_get(&server->openowner_id, gfp_flags) == 0)
 596                        break;
 597                spin_lock(&clp->cl_lock);
 598                sp = nfs4_insert_state_owner_locked(new);
 599                spin_unlock(&clp->cl_lock);
 600        } while (sp == ERR_PTR(-EAGAIN));
 601        if (sp != new)
 602                nfs4_free_state_owner(new);
 603out:
 604        nfs4_gc_state_owners(server);
 605        return sp;
 606}
 607
 608/**
 609 * nfs4_put_state_owner - Release a nfs4_state_owner
 610 * @sp: state owner data to release
 611 *
 612 * Note that we keep released state owners on an LRU
 613 * list.
 614 * This caches valid state owners so that they can be
 615 * reused, to avoid the OPEN_CONFIRM on minor version 0.
 616 * It also pins the uniquifier of dropped state owners for
 617 * a while, to ensure that those state owner names are
 618 * never reused.
 619 */
 620void nfs4_put_state_owner(struct nfs4_state_owner *sp)
 621{
 622        struct nfs_server *server = sp->so_server;
 623        struct nfs_client *clp = server->nfs_client;
 624
 625        if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
 626                return;
 627
 628        sp->so_expires = jiffies;
 629        list_add_tail(&sp->so_lru, &server->state_owners_lru);
 630        spin_unlock(&clp->cl_lock);
 631}
 632
 633/**
 634 * nfs4_purge_state_owners - Release all cached state owners
 635 * @server: nfs_server with cached state owners to release
 636 *
 637 * Called at umount time.  Remaining state owners will be on
 638 * the LRU with ref count of zero.
 639 */
 640void nfs4_purge_state_owners(struct nfs_server *server)
 641{
 642        struct nfs_client *clp = server->nfs_client;
 643        struct nfs4_state_owner *sp, *tmp;
 644        LIST_HEAD(doomed);
 645
 646        spin_lock(&clp->cl_lock);
 647        list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
 648                list_move(&sp->so_lru, &doomed);
 649                nfs4_remove_state_owner_locked(sp);
 650        }
 651        spin_unlock(&clp->cl_lock);
 652
 653        list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
 654                list_del(&sp->so_lru);
 655                nfs4_free_state_owner(sp);
 656        }
 657}
 658
 659static struct nfs4_state *
 660nfs4_alloc_open_state(void)
 661{
 662        struct nfs4_state *state;
 663
 664        state = kzalloc(sizeof(*state), GFP_NOFS);
 665        if (!state)
 666                return NULL;
 667        atomic_set(&state->count, 1);
 668        INIT_LIST_HEAD(&state->lock_states);
 669        spin_lock_init(&state->state_lock);
 670        seqlock_init(&state->seqlock);
 671        return state;
 672}
 673
 674void
 675nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
 676{
 677        if (state->state == fmode)
 678                return;
 679        /* NB! List reordering - see the reclaim code for why.  */
 680        if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
 681                if (fmode & FMODE_WRITE)
 682                        list_move(&state->open_states, &state->owner->so_states);
 683                else
 684                        list_move_tail(&state->open_states, &state->owner->so_states);
 685        }
 686        state->state = fmode;
 687}
 688
 689static struct nfs4_state *
 690__nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
 691{
 692        struct nfs_inode *nfsi = NFS_I(inode);
 693        struct nfs4_state *state;
 694
 695        list_for_each_entry(state, &nfsi->open_states, inode_states) {
 696                if (state->owner != owner)
 697                        continue;
 698                if (atomic_inc_not_zero(&state->count))
 699                        return state;
 700        }
 701        return NULL;
 702}
 703
 704static void
 705nfs4_free_open_state(struct nfs4_state *state)
 706{
 707        kfree(state);
 708}
 709
 710struct nfs4_state *
 711nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
 712{
 713        struct nfs4_state *state, *new;
 714        struct nfs_inode *nfsi = NFS_I(inode);
 715
 716        spin_lock(&inode->i_lock);
 717        state = __nfs4_find_state_byowner(inode, owner);
 718        spin_unlock(&inode->i_lock);
 719        if (state)
 720                goto out;
 721        new = nfs4_alloc_open_state();
 722        spin_lock(&owner->so_lock);
 723        spin_lock(&inode->i_lock);
 724        state = __nfs4_find_state_byowner(inode, owner);
 725        if (state == NULL && new != NULL) {
 726                state = new;
 727                state->owner = owner;
 728                atomic_inc(&owner->so_count);
 729                list_add(&state->inode_states, &nfsi->open_states);
 730                ihold(inode);
 731                state->inode = inode;
 732                spin_unlock(&inode->i_lock);
 733                /* Note: The reclaim code dictates that we add stateless
 734                 * and read-only stateids to the end of the list */
 735                list_add_tail(&state->open_states, &owner->so_states);
 736                spin_unlock(&owner->so_lock);
 737        } else {
 738                spin_unlock(&inode->i_lock);
 739                spin_unlock(&owner->so_lock);
 740                if (new)
 741                        nfs4_free_open_state(new);
 742        }
 743out:
 744        return state;
 745}
 746
 747void nfs4_put_open_state(struct nfs4_state *state)
 748{
 749        struct inode *inode = state->inode;
 750        struct nfs4_state_owner *owner = state->owner;
 751
 752        if (!atomic_dec_and_lock(&state->count, &owner->so_lock))
 753                return;
 754        spin_lock(&inode->i_lock);
 755        list_del(&state->inode_states);
 756        list_del(&state->open_states);
 757        spin_unlock(&inode->i_lock);
 758        spin_unlock(&owner->so_lock);
 759        iput(inode);
 760        nfs4_free_open_state(state);
 761        nfs4_put_state_owner(owner);
 762}
 763
 764/*
 765 * Close the current file.
 766 */
 767static void __nfs4_close(struct nfs4_state *state,
 768                fmode_t fmode, gfp_t gfp_mask, int wait)
 769{
 770        struct nfs4_state_owner *owner = state->owner;
 771        int call_close = 0;
 772        fmode_t newstate;
 773
 774        atomic_inc(&owner->so_count);
 775        /* Protect against nfs4_find_state() */
 776        spin_lock(&owner->so_lock);
 777        switch (fmode & (FMODE_READ | FMODE_WRITE)) {
 778                case FMODE_READ:
 779                        state->n_rdonly--;
 780                        break;
 781                case FMODE_WRITE:
 782                        state->n_wronly--;
 783                        break;
 784                case FMODE_READ|FMODE_WRITE:
 785                        state->n_rdwr--;
 786        }
 787        newstate = FMODE_READ|FMODE_WRITE;
 788        if (state->n_rdwr == 0) {
 789                if (state->n_rdonly == 0) {
 790                        newstate &= ~FMODE_READ;
 791                        call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
 792                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
 793                }
 794                if (state->n_wronly == 0) {
 795                        newstate &= ~FMODE_WRITE;
 796                        call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
 797                        call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
 798                }
 799                if (newstate == 0)
 800                        clear_bit(NFS_DELEGATED_STATE, &state->flags);
 801        }
 802        nfs4_state_set_mode_locked(state, newstate);
 803        spin_unlock(&owner->so_lock);
 804
 805        if (!call_close) {
 806                nfs4_put_open_state(state);
 807                nfs4_put_state_owner(owner);
 808        } else
 809                nfs4_do_close(state, gfp_mask, wait);
 810}
 811
 812void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
 813{
 814        __nfs4_close(state, fmode, GFP_NOFS, 0);
 815}
 816
 817void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
 818{
 819        __nfs4_close(state, fmode, GFP_KERNEL, 1);
 820}
 821
 822/*
 823 * Search the state->lock_states for an existing lock_owner
 824 * that is compatible with current->files
 825 */
 826static struct nfs4_lock_state *
 827__nfs4_find_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
 828{
 829        struct nfs4_lock_state *pos;
 830        list_for_each_entry(pos, &state->lock_states, ls_locks) {
 831                if (type != NFS4_ANY_LOCK_TYPE && pos->ls_owner.lo_type != type)
 832                        continue;
 833                switch (pos->ls_owner.lo_type) {
 834                case NFS4_POSIX_LOCK_TYPE:
 835                        if (pos->ls_owner.lo_u.posix_owner != fl_owner)
 836                                continue;
 837                        break;
 838                case NFS4_FLOCK_LOCK_TYPE:
 839                        if (pos->ls_owner.lo_u.flock_owner != fl_pid)
 840                                continue;
 841                }
 842                atomic_inc(&pos->ls_count);
 843                return pos;
 844        }
 845        return NULL;
 846}
 847
 848/*
 849 * Return a compatible lock_state. If no initialized lock_state structure
 850 * exists, return an uninitialized one.
 851 *
 852 */
 853static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner, pid_t fl_pid, unsigned int type)
 854{
 855        struct nfs4_lock_state *lsp;
 856        struct nfs_server *server = state->owner->so_server;
 857
 858        lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
 859        if (lsp == NULL)
 860                return NULL;
 861        nfs4_init_seqid_counter(&lsp->ls_seqid);
 862        atomic_set(&lsp->ls_count, 1);
 863        lsp->ls_state = state;
 864        lsp->ls_owner.lo_type = type;
 865        switch (lsp->ls_owner.lo_type) {
 866        case NFS4_FLOCK_LOCK_TYPE:
 867                lsp->ls_owner.lo_u.flock_owner = fl_pid;
 868                break;
 869        case NFS4_POSIX_LOCK_TYPE:
 870                lsp->ls_owner.lo_u.posix_owner = fl_owner;
 871                break;
 872        default:
 873                goto out_free;
 874        }
 875        lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
 876        if (lsp->ls_seqid.owner_id < 0)
 877                goto out_free;
 878        INIT_LIST_HEAD(&lsp->ls_locks);
 879        return lsp;
 880out_free:
 881        kfree(lsp);
 882        return NULL;
 883}
 884
 885void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
 886{
 887        ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
 888        nfs4_destroy_seqid_counter(&lsp->ls_seqid);
 889        kfree(lsp);
 890}
 891
 892/*
 893 * Return a compatible lock_state. If no initialized lock_state structure
 894 * exists, return an uninitialized one.
 895 *
 896 */
 897static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner, pid_t pid, unsigned int type)
 898{
 899        struct nfs4_lock_state *lsp, *new = NULL;
 900        
 901        for(;;) {
 902                spin_lock(&state->state_lock);
 903                lsp = __nfs4_find_lock_state(state, owner, pid, type);
 904                if (lsp != NULL)
 905                        break;
 906                if (new != NULL) {
 907                        list_add(&new->ls_locks, &state->lock_states);
 908                        set_bit(LK_STATE_IN_USE, &state->flags);
 909                        lsp = new;
 910                        new = NULL;
 911                        break;
 912                }
 913                spin_unlock(&state->state_lock);
 914                new = nfs4_alloc_lock_state(state, owner, pid, type);
 915                if (new == NULL)
 916                        return NULL;
 917        }
 918        spin_unlock(&state->state_lock);
 919        if (new != NULL)
 920                nfs4_free_lock_state(state->owner->so_server, new);
 921        return lsp;
 922}
 923
 924/*
 925 * Release reference to lock_state, and free it if we see that
 926 * it is no longer in use
 927 */
 928void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
 929{
 930        struct nfs4_state *state;
 931
 932        if (lsp == NULL)
 933                return;
 934        state = lsp->ls_state;
 935        if (!atomic_dec_and_lock(&lsp->ls_count, &state->state_lock))
 936                return;
 937        list_del(&lsp->ls_locks);
 938        if (list_empty(&state->lock_states))
 939                clear_bit(LK_STATE_IN_USE, &state->flags);
 940        spin_unlock(&state->state_lock);
 941        if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
 942                if (nfs4_release_lockowner(lsp) == 0)
 943                        return;
 944        }
 945        nfs4_free_lock_state(lsp->ls_state->owner->so_server, lsp);
 946}
 947
 948static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
 949{
 950        struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
 951
 952        dst->fl_u.nfs4_fl.owner = lsp;
 953        atomic_inc(&lsp->ls_count);
 954}
 955
 956static void nfs4_fl_release_lock(struct file_lock *fl)
 957{
 958        nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
 959}
 960
 961static const struct file_lock_operations nfs4_fl_lock_ops = {
 962        .fl_copy_lock = nfs4_fl_copy_lock,
 963        .fl_release_private = nfs4_fl_release_lock,
 964};
 965
 966int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
 967{
 968        struct nfs4_lock_state *lsp;
 969
 970        if (fl->fl_ops != NULL)
 971                return 0;
 972        if (fl->fl_flags & FL_POSIX)
 973                lsp = nfs4_get_lock_state(state, fl->fl_owner, 0, NFS4_POSIX_LOCK_TYPE);
 974        else if (fl->fl_flags & FL_FLOCK)
 975                lsp = nfs4_get_lock_state(state, NULL, fl->fl_pid,
 976                                NFS4_FLOCK_LOCK_TYPE);
 977        else
 978                return -EINVAL;
 979        if (lsp == NULL)
 980                return -ENOMEM;
 981        fl->fl_u.nfs4_fl.owner = lsp;
 982        fl->fl_ops = &nfs4_fl_lock_ops;
 983        return 0;
 984}
 985
 986static bool nfs4_copy_lock_stateid(nfs4_stateid *dst, struct nfs4_state *state,
 987                const struct nfs_lockowner *lockowner)
 988{
 989        struct nfs4_lock_state *lsp;
 990        fl_owner_t fl_owner;
 991        pid_t fl_pid;
 992        bool ret = false;
 993
 994
 995        if (lockowner == NULL)
 996                goto out;
 997
 998        if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
 999                goto out;
1000
1001        fl_owner = lockowner->l_owner;
1002        fl_pid = lockowner->l_pid;
1003        spin_lock(&state->state_lock);
1004        lsp = __nfs4_find_lock_state(state, fl_owner, fl_pid, NFS4_ANY_LOCK_TYPE);
1005        if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1006                nfs4_stateid_copy(dst, &lsp->ls_stateid);
1007                ret = true;
1008        }
1009        spin_unlock(&state->state_lock);
1010        nfs4_put_lock_state(lsp);
1011out:
1012        return ret;
1013}
1014
1015static void nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1016{
1017        int seq;
1018
1019        do {
1020                seq = read_seqbegin(&state->seqlock);
1021                nfs4_stateid_copy(dst, &state->stateid);
1022        } while (read_seqretry(&state->seqlock, seq));
1023}
1024
1025/*
1026 * Byte-range lock aware utility to initialize the stateid of read/write
1027 * requests.
1028 */
1029void nfs4_select_rw_stateid(nfs4_stateid *dst, struct nfs4_state *state,
1030                fmode_t fmode, const struct nfs_lockowner *lockowner)
1031{
1032        if (nfs4_copy_delegation_stateid(dst, state->inode, fmode))
1033                return;
1034        if (nfs4_copy_lock_stateid(dst, state, lockowner))
1035                return;
1036        nfs4_copy_open_stateid(dst, state);
1037}
1038
1039struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1040{
1041        struct nfs_seqid *new;
1042
1043        new = kmalloc(sizeof(*new), gfp_mask);
1044        if (new != NULL) {
1045                new->sequence = counter;
1046                INIT_LIST_HEAD(&new->list);
1047                new->task = NULL;
1048        }
1049        return new;
1050}
1051
1052void nfs_release_seqid(struct nfs_seqid *seqid)
1053{
1054        struct nfs_seqid_counter *sequence;
1055
1056        if (list_empty(&seqid->list))
1057                return;
1058        sequence = seqid->sequence;
1059        spin_lock(&sequence->lock);
1060        list_del_init(&seqid->list);
1061        if (!list_empty(&sequence->list)) {
1062                struct nfs_seqid *next;
1063
1064                next = list_first_entry(&sequence->list,
1065                                struct nfs_seqid, list);
1066                rpc_wake_up_queued_task(&sequence->wait, next->task);
1067        }
1068        spin_unlock(&sequence->lock);
1069}
1070
1071void nfs_free_seqid(struct nfs_seqid *seqid)
1072{
1073        nfs_release_seqid(seqid);
1074        kfree(seqid);
1075}
1076
1077/*
1078 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1079 * failed with a seqid incrementing error -
1080 * see comments nfs_fs.h:seqid_mutating_error()
1081 */
1082static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1083{
1084        BUG_ON(list_first_entry(&seqid->sequence->list, struct nfs_seqid, list) != seqid);
1085        switch (status) {
1086                case 0:
1087                        break;
1088                case -NFS4ERR_BAD_SEQID:
1089                        if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1090                                return;
1091                        pr_warn_ratelimited("NFS: v4 server returned a bad"
1092                                        " sequence-id error on an"
1093                                        " unconfirmed sequence %p!\n",
1094                                        seqid->sequence);
1095                case -NFS4ERR_STALE_CLIENTID:
1096                case -NFS4ERR_STALE_STATEID:
1097                case -NFS4ERR_BAD_STATEID:
1098                case -NFS4ERR_BADXDR:
1099                case -NFS4ERR_RESOURCE:
1100                case -NFS4ERR_NOFILEHANDLE:
1101                        /* Non-seqid mutating errors */
1102                        return;
1103        };
1104        /*
1105         * Note: no locking needed as we are guaranteed to be first
1106         * on the sequence list
1107         */
1108        seqid->sequence->counter++;
1109}
1110
1111void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1112{
1113        struct nfs4_state_owner *sp = container_of(seqid->sequence,
1114                                        struct nfs4_state_owner, so_seqid);
1115        struct nfs_server *server = sp->so_server;
1116
1117        if (status == -NFS4ERR_BAD_SEQID)
1118                nfs4_drop_state_owner(sp);
1119        if (!nfs4_has_session(server->nfs_client))
1120                nfs_increment_seqid(status, seqid);
1121}
1122
1123/*
1124 * Increment the seqid if the LOCK/LOCKU succeeded, or
1125 * failed with a seqid incrementing error -
1126 * see comments nfs_fs.h:seqid_mutating_error()
1127 */
1128void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1129{
1130        nfs_increment_seqid(status, seqid);
1131}
1132
1133int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1134{
1135        struct nfs_seqid_counter *sequence = seqid->sequence;
1136        int status = 0;
1137
1138        spin_lock(&sequence->lock);
1139        seqid->task = task;
1140        if (list_empty(&seqid->list))
1141                list_add_tail(&seqid->list, &sequence->list);
1142        if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1143                goto unlock;
1144        rpc_sleep_on(&sequence->wait, task, NULL);
1145        status = -EAGAIN;
1146unlock:
1147        spin_unlock(&sequence->lock);
1148        return status;
1149}
1150
1151static int nfs4_run_state_manager(void *);
1152
1153static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1154{
1155        smp_mb__before_clear_bit();
1156        clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1157        smp_mb__after_clear_bit();
1158        wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1159        rpc_wake_up(&clp->cl_rpcwaitq);
1160}
1161
1162/*
1163 * Schedule the nfs_client asynchronous state management routine
1164 */
1165void nfs4_schedule_state_manager(struct nfs_client *clp)
1166{
1167        struct task_struct *task;
1168        char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1169
1170        if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1171                return;
1172        __module_get(THIS_MODULE);
1173        atomic_inc(&clp->cl_count);
1174
1175        /* The rcu_read_lock() is not strictly necessary, as the state
1176         * manager is the only thread that ever changes the rpc_xprt
1177         * after it's initialized.  At this point, we're single threaded. */
1178        rcu_read_lock();
1179        snprintf(buf, sizeof(buf), "%s-manager",
1180                        rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1181        rcu_read_unlock();
1182        task = kthread_run(nfs4_run_state_manager, clp, buf);
1183        if (IS_ERR(task)) {
1184                printk(KERN_ERR "%s: kthread_run: %ld\n",
1185                        __func__, PTR_ERR(task));
1186                nfs4_clear_state_manager_bit(clp);
1187                nfs_put_client(clp);
1188                module_put(THIS_MODULE);
1189        }
1190}
1191
1192/*
1193 * Schedule a lease recovery attempt
1194 */
1195void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1196{
1197        if (!clp)
1198                return;
1199        if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1200                set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1201        dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1202                        clp->cl_hostname);
1203        nfs4_schedule_state_manager(clp);
1204}
1205EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1206
1207/*
1208 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1209 * @clp: client to process
1210 *
1211 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1212 * resend of the SETCLIENTID and hence re-establish the
1213 * callback channel. Then return all existing delegations.
1214 */
1215static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1216{
1217        set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1218        nfs_expire_all_delegations(clp);
1219        dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1220                        clp->cl_hostname);
1221}
1222
1223void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1224{
1225        nfs40_handle_cb_pathdown(clp);
1226        nfs4_schedule_state_manager(clp);
1227}
1228
1229static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1230{
1231
1232        set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1233        /* Don't recover state that expired before the reboot */
1234        if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1235                clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1236                return 0;
1237        }
1238        set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1239        set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1240        return 1;
1241}
1242
1243static int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1244{
1245        set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1246        clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1247        set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1248        set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1249        return 1;
1250}
1251
1252void nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1253{
1254        struct nfs_client *clp = server->nfs_client;
1255
1256        nfs4_state_mark_reclaim_nograce(clp, state);
1257        dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1258                        clp->cl_hostname);
1259        nfs4_schedule_state_manager(clp);
1260}
1261EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1262
1263void nfs_inode_find_state_and_recover(struct inode *inode,
1264                const nfs4_stateid *stateid)
1265{
1266        struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1267        struct nfs_inode *nfsi = NFS_I(inode);
1268        struct nfs_open_context *ctx;
1269        struct nfs4_state *state;
1270        bool found = false;
1271
1272        spin_lock(&inode->i_lock);
1273        list_for_each_entry(ctx, &nfsi->open_files, list) {
1274                state = ctx->state;
1275                if (state == NULL)
1276                        continue;
1277                if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
1278                        continue;
1279                if (!nfs4_stateid_match(&state->stateid, stateid))
1280                        continue;
1281                nfs4_state_mark_reclaim_nograce(clp, state);
1282                found = true;
1283        }
1284        spin_unlock(&inode->i_lock);
1285        if (found)
1286                nfs4_schedule_state_manager(clp);
1287}
1288
1289
1290static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1291{
1292        struct inode *inode = state->inode;
1293        struct nfs_inode *nfsi = NFS_I(inode);
1294        struct file_lock *fl;
1295        int status = 0;
1296
1297        if (inode->i_flock == NULL)
1298                return 0;
1299
1300        /* Guard against delegation returns and new lock/unlock calls */
1301        down_write(&nfsi->rwsem);
1302        /* Protect inode->i_flock using the BKL */
1303        lock_flocks();
1304        for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
1305                if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
1306                        continue;
1307                if (nfs_file_open_context(fl->fl_file)->state != state)
1308                        continue;
1309                unlock_flocks();
1310                status = ops->recover_lock(state, fl);
1311                switch (status) {
1312                        case 0:
1313                                break;
1314                        case -ESTALE:
1315                        case -NFS4ERR_ADMIN_REVOKED:
1316                        case -NFS4ERR_STALE_STATEID:
1317                        case -NFS4ERR_BAD_STATEID:
1318                        case -NFS4ERR_EXPIRED:
1319                        case -NFS4ERR_NO_GRACE:
1320                        case -NFS4ERR_STALE_CLIENTID:
1321                        case -NFS4ERR_BADSESSION:
1322                        case -NFS4ERR_BADSLOT:
1323                        case -NFS4ERR_BAD_HIGH_SLOT:
1324                        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1325                                goto out;
1326                        default:
1327                                printk(KERN_ERR "NFS: %s: unhandled error %d. "
1328                                        "Zeroing state\n", __func__, status);
1329                        case -ENOMEM:
1330                        case -NFS4ERR_DENIED:
1331                        case -NFS4ERR_RECLAIM_BAD:
1332                        case -NFS4ERR_RECLAIM_CONFLICT:
1333                                /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1334                                status = 0;
1335                }
1336                lock_flocks();
1337        }
1338        unlock_flocks();
1339out:
1340        up_write(&nfsi->rwsem);
1341        return status;
1342}
1343
1344static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1345{
1346        struct nfs4_state *state;
1347        struct nfs4_lock_state *lock;
1348        int status = 0;
1349
1350        /* Note: we rely on the sp->so_states list being ordered 
1351         * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1352         * states first.
1353         * This is needed to ensure that the server won't give us any
1354         * read delegations that we have to return if, say, we are
1355         * recovering after a network partition or a reboot from a
1356         * server that doesn't support a grace period.
1357         */
1358restart:
1359        spin_lock(&sp->so_lock);
1360        list_for_each_entry(state, &sp->so_states, open_states) {
1361                if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1362                        continue;
1363                if (state->state == 0)
1364                        continue;
1365                atomic_inc(&state->count);
1366                spin_unlock(&sp->so_lock);
1367                status = ops->recover_open(sp, state);
1368                if (status >= 0) {
1369                        status = nfs4_reclaim_locks(state, ops);
1370                        if (status >= 0) {
1371                                spin_lock(&state->state_lock);
1372                                list_for_each_entry(lock, &state->lock_states, ls_locks) {
1373                                        if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1374                                                pr_warn_ratelimited("NFS: "
1375                                                        "%s: Lock reclaim "
1376                                                        "failed!\n", __func__);
1377                                }
1378                                spin_unlock(&state->state_lock);
1379                                nfs4_put_open_state(state);
1380                                goto restart;
1381                        }
1382                }
1383                switch (status) {
1384                        default:
1385                                printk(KERN_ERR "NFS: %s: unhandled error %d. "
1386                                        "Zeroing state\n", __func__, status);
1387                        case -ENOENT:
1388                        case -ENOMEM:
1389                        case -ESTALE:
1390                                /*
1391                                 * Open state on this file cannot be recovered
1392                                 * All we can do is revert to using the zero stateid.
1393                                 */
1394                                memset(&state->stateid, 0,
1395                                        sizeof(state->stateid));
1396                                /* Mark the file as being 'closed' */
1397                                state->state = 0;
1398                                break;
1399                        case -EKEYEXPIRED:
1400                                /*
1401                                 * User RPCSEC_GSS context has expired.
1402                                 * We cannot recover this stateid now, so
1403                                 * skip it and allow recovery thread to
1404                                 * proceed.
1405                                 */
1406                                break;
1407                        case -NFS4ERR_ADMIN_REVOKED:
1408                        case -NFS4ERR_STALE_STATEID:
1409                        case -NFS4ERR_BAD_STATEID:
1410                        case -NFS4ERR_RECLAIM_BAD:
1411                        case -NFS4ERR_RECLAIM_CONFLICT:
1412                                nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1413                                break;
1414                        case -NFS4ERR_EXPIRED:
1415                        case -NFS4ERR_NO_GRACE:
1416                                nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1417                        case -NFS4ERR_STALE_CLIENTID:
1418                        case -NFS4ERR_BADSESSION:
1419                        case -NFS4ERR_BADSLOT:
1420                        case -NFS4ERR_BAD_HIGH_SLOT:
1421                        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1422                                goto out_err;
1423                }
1424                nfs4_put_open_state(state);
1425                goto restart;
1426        }
1427        spin_unlock(&sp->so_lock);
1428        return 0;
1429out_err:
1430        nfs4_put_open_state(state);
1431        return status;
1432}
1433
1434static void nfs4_clear_open_state(struct nfs4_state *state)
1435{
1436        struct nfs4_lock_state *lock;
1437
1438        clear_bit(NFS_DELEGATED_STATE, &state->flags);
1439        clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1440        clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1441        clear_bit(NFS_O_RDWR_STATE, &state->flags);
1442        spin_lock(&state->state_lock);
1443        list_for_each_entry(lock, &state->lock_states, ls_locks) {
1444                lock->ls_seqid.flags = 0;
1445                clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1446        }
1447        spin_unlock(&state->state_lock);
1448}
1449
1450static void nfs4_reset_seqids(struct nfs_server *server,
1451        int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1452{
1453        struct nfs_client *clp = server->nfs_client;
1454        struct nfs4_state_owner *sp;
1455        struct rb_node *pos;
1456        struct nfs4_state *state;
1457
1458        spin_lock(&clp->cl_lock);
1459        for (pos = rb_first(&server->state_owners);
1460             pos != NULL;
1461             pos = rb_next(pos)) {
1462                sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1463                sp->so_seqid.flags = 0;
1464                spin_lock(&sp->so_lock);
1465                list_for_each_entry(state, &sp->so_states, open_states) {
1466                        if (mark_reclaim(clp, state))
1467                                nfs4_clear_open_state(state);
1468                }
1469                spin_unlock(&sp->so_lock);
1470        }
1471        spin_unlock(&clp->cl_lock);
1472}
1473
1474static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1475        int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1476{
1477        struct nfs_server *server;
1478
1479        rcu_read_lock();
1480        list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1481                nfs4_reset_seqids(server, mark_reclaim);
1482        rcu_read_unlock();
1483}
1484
1485static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1486{
1487        /* Mark all delegations for reclaim */
1488        nfs_delegation_mark_reclaim(clp);
1489        nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1490}
1491
1492static void nfs4_reclaim_complete(struct nfs_client *clp,
1493                                 const struct nfs4_state_recovery_ops *ops)
1494{
1495        /* Notify the server we're done reclaiming our state */
1496        if (ops->reclaim_complete)
1497                (void)ops->reclaim_complete(clp);
1498}
1499
1500static void nfs4_clear_reclaim_server(struct nfs_server *server)
1501{
1502        struct nfs_client *clp = server->nfs_client;
1503        struct nfs4_state_owner *sp;
1504        struct rb_node *pos;
1505        struct nfs4_state *state;
1506
1507        spin_lock(&clp->cl_lock);
1508        for (pos = rb_first(&server->state_owners);
1509             pos != NULL;
1510             pos = rb_next(pos)) {
1511                sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1512                spin_lock(&sp->so_lock);
1513                list_for_each_entry(state, &sp->so_states, open_states) {
1514                        if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1515                                                &state->flags))
1516                                continue;
1517                        nfs4_state_mark_reclaim_nograce(clp, state);
1518                }
1519                spin_unlock(&sp->so_lock);
1520        }
1521        spin_unlock(&clp->cl_lock);
1522}
1523
1524static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1525{
1526        struct nfs_server *server;
1527
1528        if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1529                return 0;
1530
1531        rcu_read_lock();
1532        list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1533                nfs4_clear_reclaim_server(server);
1534        rcu_read_unlock();
1535
1536        nfs_delegation_reap_unclaimed(clp);
1537        return 1;
1538}
1539
1540static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1541{
1542        if (!nfs4_state_clear_reclaim_reboot(clp))
1543                return;
1544        nfs4_reclaim_complete(clp, clp->cl_mvops->reboot_recovery_ops);
1545}
1546
1547static void nfs_delegation_clear_all(struct nfs_client *clp)
1548{
1549        nfs_delegation_mark_reclaim(clp);
1550        nfs_delegation_reap_unclaimed(clp);
1551}
1552
1553static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1554{
1555        nfs_delegation_clear_all(clp);
1556        nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1557}
1558
1559static void nfs4_warn_keyexpired(const char *s)
1560{
1561        printk_ratelimited(KERN_WARNING "Error: state manager"
1562                        " encountered RPCSEC_GSS session"
1563                        " expired against NFSv4 server %s.\n",
1564                        s);
1565}
1566
1567static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1568{
1569        switch (error) {
1570                case 0:
1571                        break;
1572                case -NFS4ERR_CB_PATH_DOWN:
1573                        nfs40_handle_cb_pathdown(clp);
1574                        break;
1575                case -NFS4ERR_NO_GRACE:
1576                        nfs4_state_end_reclaim_reboot(clp);
1577                        break;
1578                case -NFS4ERR_STALE_CLIENTID:
1579                case -NFS4ERR_LEASE_MOVED:
1580                        set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1581                        nfs4_state_clear_reclaim_reboot(clp);
1582                        nfs4_state_start_reclaim_reboot(clp);
1583                        break;
1584                case -NFS4ERR_EXPIRED:
1585                        set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1586                        nfs4_state_start_reclaim_nograce(clp);
1587                        break;
1588                case -NFS4ERR_BADSESSION:
1589                case -NFS4ERR_BADSLOT:
1590                case -NFS4ERR_BAD_HIGH_SLOT:
1591                case -NFS4ERR_DEADSESSION:
1592                case -NFS4ERR_SEQ_FALSE_RETRY:
1593                case -NFS4ERR_SEQ_MISORDERED:
1594                        set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1595                        /* Zero session reset errors */
1596                        break;
1597                case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1598                        set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1599                        break;
1600                case -EKEYEXPIRED:
1601                        /* Nothing we can do */
1602                        nfs4_warn_keyexpired(clp->cl_hostname);
1603                        break;
1604                default:
1605                        dprintk("%s: failed to handle error %d for server %s\n",
1606                                        __func__, error, clp->cl_hostname);
1607                        return error;
1608        }
1609        dprintk("%s: handled error %d for server %s\n", __func__, error,
1610                        clp->cl_hostname);
1611        return 0;
1612}
1613
1614static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1615{
1616        struct nfs4_state_owner *sp;
1617        struct nfs_server *server;
1618        struct rb_node *pos;
1619        int status = 0;
1620
1621restart:
1622        rcu_read_lock();
1623        list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1624                nfs4_purge_state_owners(server);
1625                spin_lock(&clp->cl_lock);
1626                for (pos = rb_first(&server->state_owners);
1627                     pos != NULL;
1628                     pos = rb_next(pos)) {
1629                        sp = rb_entry(pos,
1630                                struct nfs4_state_owner, so_server_node);
1631                        if (!test_and_clear_bit(ops->owner_flag_bit,
1632                                                        &sp->so_flags))
1633                                continue;
1634                        atomic_inc(&sp->so_count);
1635                        spin_unlock(&clp->cl_lock);
1636                        rcu_read_unlock();
1637
1638                        status = nfs4_reclaim_open_state(sp, ops);
1639                        if (status < 0) {
1640                                set_bit(ops->owner_flag_bit, &sp->so_flags);
1641                                nfs4_put_state_owner(sp);
1642                                return nfs4_recovery_handle_error(clp, status);
1643                        }
1644
1645                        nfs4_put_state_owner(sp);
1646                        goto restart;
1647                }
1648                spin_unlock(&clp->cl_lock);
1649        }
1650        rcu_read_unlock();
1651        return status;
1652}
1653
1654static int nfs4_check_lease(struct nfs_client *clp)
1655{
1656        struct rpc_cred *cred;
1657        const struct nfs4_state_maintenance_ops *ops =
1658                clp->cl_mvops->state_renewal_ops;
1659        int status;
1660
1661        /* Is the client already known to have an expired lease? */
1662        if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1663                return 0;
1664        spin_lock(&clp->cl_lock);
1665        cred = ops->get_state_renewal_cred_locked(clp);
1666        spin_unlock(&clp->cl_lock);
1667        if (cred == NULL) {
1668                cred = nfs4_get_setclientid_cred(clp);
1669                status = -ENOKEY;
1670                if (cred == NULL)
1671                        goto out;
1672        }
1673        status = ops->renew_lease(clp, cred);
1674        put_rpccred(cred);
1675out:
1676        return nfs4_recovery_handle_error(clp, status);
1677}
1678
1679/* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1680 * and for recoverable errors on EXCHANGE_ID for v4.1
1681 */
1682static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1683{
1684        switch (status) {
1685        case -NFS4ERR_SEQ_MISORDERED:
1686                if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1687                        return -ESERVERFAULT;
1688                /* Lease confirmation error: retry after purging the lease */
1689                ssleep(1);
1690                clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1691                break;
1692        case -NFS4ERR_STALE_CLIENTID:
1693                clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1694                nfs4_state_clear_reclaim_reboot(clp);
1695                nfs4_state_start_reclaim_reboot(clp);
1696                break;
1697        case -NFS4ERR_CLID_INUSE:
1698                pr_err("NFS: Server %s reports our clientid is in use\n",
1699                        clp->cl_hostname);
1700                nfs_mark_client_ready(clp, -EPERM);
1701                clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1702                return -EPERM;
1703        case -EACCES:
1704                if (clp->cl_machine_cred == NULL)
1705                        return -EACCES;
1706                /* Handle case where the user hasn't set up machine creds */
1707                nfs4_clear_machine_cred(clp);
1708        case -NFS4ERR_DELAY:
1709        case -ETIMEDOUT:
1710        case -EAGAIN:
1711                ssleep(1);
1712                break;
1713
1714        case -NFS4ERR_MINOR_VERS_MISMATCH:
1715                if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1716                        nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1717                dprintk("%s: exit with error %d for server %s\n",
1718                                __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1719                return -EPROTONOSUPPORT;
1720        case -EKEYEXPIRED:
1721                nfs4_warn_keyexpired(clp->cl_hostname);
1722        case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1723                                 * in nfs4_exchange_id */
1724        default:
1725                dprintk("%s: exit with error %d for server %s\n", __func__,
1726                                status, clp->cl_hostname);
1727                return status;
1728        }
1729        set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1730        dprintk("%s: handled error %d for server %s\n", __func__, status,
1731                        clp->cl_hostname);
1732        return 0;
1733}
1734
1735static int nfs4_establish_lease(struct nfs_client *clp)
1736{
1737        struct rpc_cred *cred;
1738        const struct nfs4_state_recovery_ops *ops =
1739                clp->cl_mvops->reboot_recovery_ops;
1740        int status;
1741
1742        cred = ops->get_clid_cred(clp);
1743        if (cred == NULL)
1744                return -ENOENT;
1745        status = ops->establish_clid(clp, cred);
1746        put_rpccred(cred);
1747        if (status != 0)
1748                return status;
1749        pnfs_destroy_all_layouts(clp);
1750        return 0;
1751}
1752
1753/*
1754 * Returns zero or a negative errno.  NFS4ERR values are converted
1755 * to local errno values.
1756 */
1757static int nfs4_reclaim_lease(struct nfs_client *clp)
1758{
1759        int status;
1760
1761        status = nfs4_establish_lease(clp);
1762        if (status < 0)
1763                return nfs4_handle_reclaim_lease_error(clp, status);
1764        if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
1765                nfs4_state_start_reclaim_nograce(clp);
1766        if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
1767                set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1768        clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1769        clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1770        return 0;
1771}
1772
1773static int nfs4_purge_lease(struct nfs_client *clp)
1774{
1775        int status;
1776
1777        status = nfs4_establish_lease(clp);
1778        if (status < 0)
1779                return nfs4_handle_reclaim_lease_error(clp, status);
1780        clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1781        set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1782        nfs4_state_start_reclaim_nograce(clp);
1783        return 0;
1784}
1785
1786/**
1787 * nfs4_discover_server_trunking - Detect server IP address trunking
1788 *
1789 * @clp: nfs_client under test
1790 * @result: OUT: found nfs_client, or clp
1791 *
1792 * Returns zero or a negative errno.  If zero is returned,
1793 * an nfs_client pointer is planted in "result".
1794 *
1795 * Note: since we are invoked in process context, and
1796 * not from inside the state manager, we cannot use
1797 * nfs4_handle_reclaim_lease_error().
1798 */
1799int nfs4_discover_server_trunking(struct nfs_client *clp,
1800                                  struct nfs_client **result)
1801{
1802        const struct nfs4_state_recovery_ops *ops =
1803                                clp->cl_mvops->reboot_recovery_ops;
1804        rpc_authflavor_t *flavors, flav, save;
1805        struct rpc_clnt *clnt;
1806        struct rpc_cred *cred;
1807        int i, len, status;
1808
1809        dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
1810
1811        len = NFS_MAX_SECFLAVORS;
1812        flavors = kcalloc(len, sizeof(*flavors), GFP_KERNEL);
1813        if (flavors == NULL) {
1814                status = -ENOMEM;
1815                goto out;
1816        }
1817        len = rpcauth_list_flavors(flavors, len);
1818        if (len < 0) {
1819                status = len;
1820                goto out_free;
1821        }
1822        clnt = clp->cl_rpcclient;
1823        save = clnt->cl_auth->au_flavor;
1824        i = 0;
1825
1826        mutex_lock(&nfs_clid_init_mutex);
1827        status  = -ENOENT;
1828again:
1829        cred = ops->get_clid_cred(clp);
1830        if (cred == NULL)
1831                goto out_unlock;
1832
1833        status = ops->detect_trunking(clp, result, cred);
1834        put_rpccred(cred);
1835        switch (status) {
1836        case 0:
1837                break;
1838
1839        case -EACCES:
1840                if (clp->cl_machine_cred == NULL)
1841                        break;
1842                /* Handle case where the user hasn't set up machine creds */
1843                nfs4_clear_machine_cred(clp);
1844        case -NFS4ERR_DELAY:
1845        case -ETIMEDOUT:
1846        case -EAGAIN:
1847                ssleep(1);
1848        case -NFS4ERR_STALE_CLIENTID:
1849                dprintk("NFS: %s after status %d, retrying\n",
1850                        __func__, status);
1851                goto again;
1852
1853        case -NFS4ERR_CLID_INUSE:
1854        case -NFS4ERR_WRONGSEC:
1855                status = -EPERM;
1856                if (i >= len)
1857                        break;
1858
1859                flav = flavors[i++];
1860                if (flav == save)
1861                        flav = flavors[i++];
1862                clnt = rpc_clone_client_set_auth(clnt, flav);
1863                if (IS_ERR(clnt)) {
1864                        status = PTR_ERR(clnt);
1865                        break;
1866                }
1867                clp->cl_rpcclient = clnt;
1868                goto again;
1869
1870        case -NFS4ERR_MINOR_VERS_MISMATCH:
1871                status = -EPROTONOSUPPORT;
1872                break;
1873
1874        case -EKEYEXPIRED:
1875                nfs4_warn_keyexpired(clp->cl_hostname);
1876        case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1877                                 * in nfs4_exchange_id */
1878                status = -EKEYEXPIRED;
1879        }
1880
1881out_unlock:
1882        mutex_unlock(&nfs_clid_init_mutex);
1883out_free:
1884        kfree(flavors);
1885out:
1886        dprintk("NFS: %s: status = %d\n", __func__, status);
1887        return status;
1888}
1889
1890#ifdef CONFIG_NFS_V4_1
1891void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
1892{
1893        struct nfs_client *clp = session->clp;
1894
1895        switch (err) {
1896        default:
1897                set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1898                break;
1899        case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1900                set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1901        }
1902        nfs4_schedule_lease_recovery(clp);
1903}
1904EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
1905
1906void nfs41_handle_recall_slot(struct nfs_client *clp)
1907{
1908        set_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state);
1909        dprintk("%s: scheduling slot recall for server %s\n", __func__,
1910                        clp->cl_hostname);
1911        nfs4_schedule_state_manager(clp);
1912}
1913
1914static void nfs4_reset_all_state(struct nfs_client *clp)
1915{
1916        if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1917                set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
1918                clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1919                nfs4_state_start_reclaim_nograce(clp);
1920                dprintk("%s: scheduling reset of all state for server %s!\n",
1921                                __func__, clp->cl_hostname);
1922                nfs4_schedule_state_manager(clp);
1923        }
1924}
1925
1926static void nfs41_handle_server_reboot(struct nfs_client *clp)
1927{
1928        if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
1929                nfs4_state_start_reclaim_reboot(clp);
1930                dprintk("%s: server %s rebooted!\n", __func__,
1931                                clp->cl_hostname);
1932                nfs4_schedule_state_manager(clp);
1933        }
1934}
1935
1936static void nfs41_handle_state_revoked(struct nfs_client *clp)
1937{
1938        nfs4_reset_all_state(clp);
1939        dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
1940}
1941
1942static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
1943{
1944        /* This will need to handle layouts too */
1945        nfs_expire_all_delegations(clp);
1946        dprintk("%s: Recallable state revoked on server %s!\n", __func__,
1947                        clp->cl_hostname);
1948}
1949
1950static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
1951{
1952        nfs_expire_all_delegations(clp);
1953        if (test_and_set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) == 0)
1954                nfs4_schedule_state_manager(clp);
1955        dprintk("%s: server %s declared a backchannel fault\n", __func__,
1956                        clp->cl_hostname);
1957}
1958
1959static void nfs41_handle_cb_path_down(struct nfs_client *clp)
1960{
1961        if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
1962                &clp->cl_state) == 0)
1963                nfs4_schedule_state_manager(clp);
1964}
1965
1966void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags)
1967{
1968        if (!flags)
1969                return;
1970
1971        dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1972                __func__, clp->cl_hostname, clp->cl_clientid, flags);
1973
1974        if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
1975                nfs41_handle_server_reboot(clp);
1976        if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED |
1977                            SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
1978                            SEQ4_STATUS_ADMIN_STATE_REVOKED |
1979                            SEQ4_STATUS_LEASE_MOVED))
1980                nfs41_handle_state_revoked(clp);
1981        if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
1982                nfs41_handle_recallable_state_revoked(clp);
1983        if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
1984                nfs41_handle_backchannel_fault(clp);
1985        else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
1986                                SEQ4_STATUS_CB_PATH_DOWN_SESSION))
1987                nfs41_handle_cb_path_down(clp);
1988}
1989
1990static int nfs4_reset_session(struct nfs_client *clp)
1991{
1992        struct rpc_cred *cred;
1993        int status;
1994
1995        if (!nfs4_has_session(clp))
1996                return 0;
1997        nfs4_begin_drain_session(clp);
1998        cred = nfs4_get_exchange_id_cred(clp);
1999        status = nfs4_proc_destroy_session(clp->cl_session, cred);
2000        switch (status) {
2001        case 0:
2002        case -NFS4ERR_BADSESSION:
2003        case -NFS4ERR_DEADSESSION:
2004                break;
2005        case -NFS4ERR_BACK_CHAN_BUSY:
2006        case -NFS4ERR_DELAY:
2007                set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2008                status = 0;
2009                ssleep(1);
2010                goto out;
2011        default:
2012                status = nfs4_recovery_handle_error(clp, status);
2013                goto out;
2014        }
2015
2016        memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2017        status = nfs4_proc_create_session(clp, cred);
2018        if (status) {
2019                dprintk("%s: session reset failed with status %d for server %s!\n",
2020                        __func__, status, clp->cl_hostname);
2021                status = nfs4_handle_reclaim_lease_error(clp, status);
2022                goto out;
2023        }
2024        nfs41_finish_session_reset(clp);
2025        dprintk("%s: session reset was successful for server %s!\n",
2026                        __func__, clp->cl_hostname);
2027out:
2028        if (cred)
2029                put_rpccred(cred);
2030        return status;
2031}
2032
2033static int nfs4_recall_slot(struct nfs_client *clp)
2034{
2035        struct nfs4_slot_table *fc_tbl;
2036        struct nfs4_slot *new, *old;
2037        int i;
2038
2039        if (!nfs4_has_session(clp))
2040                return 0;
2041        nfs4_begin_drain_session(clp);
2042        fc_tbl = &clp->cl_session->fc_slot_table;
2043        new = kmalloc(fc_tbl->target_max_slots * sizeof(struct nfs4_slot),
2044                      GFP_NOFS);
2045        if (!new)
2046                return -ENOMEM;
2047
2048        spin_lock(&fc_tbl->slot_tbl_lock);
2049        for (i = 0; i < fc_tbl->target_max_slots; i++)
2050                new[i].seq_nr = fc_tbl->slots[i].seq_nr;
2051        old = fc_tbl->slots;
2052        fc_tbl->slots = new;
2053        fc_tbl->max_slots = fc_tbl->target_max_slots;
2054        fc_tbl->target_max_slots = 0;
2055        clp->cl_session->fc_attrs.max_reqs = fc_tbl->max_slots;
2056        spin_unlock(&fc_tbl->slot_tbl_lock);
2057
2058        kfree(old);
2059        return 0;
2060}
2061
2062static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2063{
2064        struct rpc_cred *cred;
2065        int ret;
2066
2067        if (!nfs4_has_session(clp))
2068                return 0;
2069        nfs4_begin_drain_session(clp);
2070        cred = nfs4_get_exchange_id_cred(clp);
2071        ret = nfs4_proc_bind_conn_to_session(clp, cred);
2072        if (cred)
2073                put_rpccred(cred);
2074        clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2075        switch (ret) {
2076        case 0:
2077                dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2078                        __func__, clp->cl_hostname);
2079                break;
2080        case -NFS4ERR_DELAY:
2081                ssleep(1);
2082                set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2083                break;
2084        default:
2085                return nfs4_recovery_handle_error(clp, ret);
2086        }
2087        return 0;
2088}
2089#else /* CONFIG_NFS_V4_1 */
2090static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2091static int nfs4_end_drain_session(struct nfs_client *clp) { return 0; }
2092static int nfs4_recall_slot(struct nfs_client *clp) { return 0; }
2093
2094static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2095{
2096        return 0;
2097}
2098#endif /* CONFIG_NFS_V4_1 */
2099
2100static void nfs4_state_manager(struct nfs_client *clp)
2101{
2102        int status = 0;
2103        const char *section = "", *section_sep = "";
2104
2105        /* Ensure exclusive access to NFSv4 state */
2106        do {
2107                if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2108                        section = "purge state";
2109                        status = nfs4_purge_lease(clp);
2110                        if (status < 0)
2111                                goto out_error;
2112                        continue;
2113                }
2114
2115                if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2116                        section = "lease expired";
2117                        /* We're going to have to re-establish a clientid */
2118                        status = nfs4_reclaim_lease(clp);
2119                        if (status < 0)
2120                                goto out_error;
2121                        continue;
2122                }
2123
2124                if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2125                        section = "check lease";
2126                        status = nfs4_check_lease(clp);
2127                        if (status < 0)
2128                                goto out_error;
2129                        if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2130                                continue;
2131                }
2132
2133                /* Initialize or reset the session */
2134                if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2135                        section = "reset session";
2136                        status = nfs4_reset_session(clp);
2137                        if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2138                                continue;
2139                        if (status < 0)
2140                                goto out_error;
2141                }
2142
2143                /* Send BIND_CONN_TO_SESSION */
2144                if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2145                                &clp->cl_state)) {
2146                        section = "bind conn to session";
2147                        status = nfs4_bind_conn_to_session(clp);
2148                        if (status < 0)
2149                                goto out_error;
2150                        continue;
2151                }
2152
2153                /* Recall session slots */
2154                if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT, &clp->cl_state)) {
2155                        section = "recall slot";
2156                        status = nfs4_recall_slot(clp);
2157                        if (status < 0)
2158                                goto out_error;
2159                        continue;
2160                }
2161
2162                /* First recover reboot state... */
2163                if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2164                        section = "reclaim reboot";
2165                        status = nfs4_do_reclaim(clp,
2166                                clp->cl_mvops->reboot_recovery_ops);
2167                        if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2168                            test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
2169                                continue;
2170                        nfs4_state_end_reclaim_reboot(clp);
2171                        if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2172                                continue;
2173                        if (status < 0)
2174                                goto out_error;
2175                }
2176
2177                /* Now recover expired state... */
2178                if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2179                        section = "reclaim nograce";
2180                        status = nfs4_do_reclaim(clp,
2181                                clp->cl_mvops->nograce_recovery_ops);
2182                        if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) ||
2183                            test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state) ||
2184                            test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
2185                                continue;
2186                        if (status < 0)
2187                                goto out_error;
2188                }
2189
2190                nfs4_end_drain_session(clp);
2191                if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2192                        nfs_client_return_marked_delegations(clp);
2193                        continue;
2194                }
2195
2196                nfs4_clear_state_manager_bit(clp);
2197                /* Did we race with an attempt to give us more work? */
2198                if (clp->cl_state == 0)
2199                        break;
2200                if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2201                        break;
2202        } while (atomic_read(&clp->cl_count) > 1);
2203        return;
2204out_error:
2205        if (strlen(section))
2206                section_sep = ": ";
2207        pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2208                        " with error %d\n", section_sep, section,
2209                        clp->cl_hostname, -status);
2210        ssleep(1);
2211        nfs4_end_drain_session(clp);
2212        nfs4_clear_state_manager_bit(clp);
2213}
2214
2215static int nfs4_run_state_manager(void *ptr)
2216{
2217        struct nfs_client *clp = ptr;
2218
2219        allow_signal(SIGKILL);
2220        nfs4_state_manager(clp);
2221        nfs_put_client(clp);
2222        module_put_and_exit(0);
2223        return 0;
2224}
2225
2226/*
2227 * Local variables:
2228 *  c-basic-offset: 8
2229 * End:
2230 */
2231
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.