root/fs/nfs/nfs4state.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. nfs4_setup_state_renewal
  2. nfs4_init_clientid
  3. nfs40_discover_server_trunking
  4. nfs4_get_machine_cred
  5. nfs4_root_machine_cred
  6. nfs4_get_renew_cred_server_locked
  7. nfs4_get_renew_cred
  8. nfs4_end_drain_slot_table
  9. nfs4_end_drain_session
  10. nfs4_drain_slot_tbl
  11. nfs4_begin_drain_session
  12. nfs41_finish_session_reset
  13. nfs41_init_clientid
  14. nfs41_discover_server_trunking
  15. nfs4_get_clid_cred
  16. nfs4_find_state_owner_locked
  17. nfs4_insert_state_owner_locked
  18. nfs4_remove_state_owner_locked
  19. nfs4_init_seqid_counter
  20. nfs4_destroy_seqid_counter
  21. nfs4_alloc_state_owner
  22. nfs4_reset_state_owner
  23. nfs4_free_state_owner
  24. nfs4_gc_state_owners
  25. nfs4_get_state_owner
  26. nfs4_put_state_owner
  27. nfs4_purge_state_owners
  28. nfs4_free_state_owners
  29. nfs4_alloc_open_state
  30. nfs4_state_set_mode_locked
  31. __nfs4_find_state_byowner
  32. nfs4_free_open_state
  33. nfs4_get_open_state
  34. nfs4_put_open_state
  35. __nfs4_close
  36. nfs4_close_state
  37. nfs4_close_sync
  38. __nfs4_find_lock_state
  39. nfs4_alloc_lock_state
  40. nfs4_free_lock_state
  41. nfs4_get_lock_state
  42. nfs4_put_lock_state
  43. nfs4_fl_copy_lock
  44. nfs4_fl_release_lock
  45. nfs4_set_lock_state
  46. nfs4_copy_lock_stateid
  47. nfs4_copy_open_stateid
  48. nfs4_select_rw_stateid
  49. nfs_alloc_seqid
  50. nfs_release_seqid
  51. nfs_free_seqid
  52. nfs_increment_seqid
  53. nfs_increment_open_seqid
  54. nfs_increment_lock_seqid
  55. nfs_wait_on_sequence
  56. nfs4_clear_state_manager_bit
  57. nfs4_schedule_state_manager
  58. nfs4_schedule_lease_recovery
  59. nfs4_schedule_migration_recovery
  60. nfs4_schedule_lease_moved_recovery
  61. nfs4_wait_clnt_recover
  62. nfs4_client_recover_expired_lease
  63. nfs40_handle_cb_pathdown
  64. nfs4_schedule_path_down_recovery
  65. nfs4_state_mark_reclaim_reboot
  66. nfs4_state_mark_reclaim_nograce
  67. nfs4_schedule_stateid_recovery
  68. nfs_state_find_lock_state_by_stateid
  69. nfs_state_lock_state_matches_stateid
  70. nfs_inode_find_state_and_recover
  71. nfs4_state_mark_open_context_bad
  72. nfs4_state_mark_recovery_failed
  73. nfs4_reclaim_locks
  74. nfs42_complete_copies
  75. nfs42_complete_copies
  76. __nfs4_reclaim_open_state
  77. nfs4_reclaim_open_state
  78. nfs4_clear_open_state
  79. nfs4_reset_seqids
  80. nfs4_state_mark_reclaim_helper
  81. nfs4_state_start_reclaim_reboot
  82. nfs4_reclaim_complete
  83. nfs4_clear_reclaim_server
  84. nfs4_state_clear_reclaim_reboot
  85. nfs4_state_end_reclaim_reboot
  86. nfs4_state_start_reclaim_nograce
  87. nfs4_recovery_handle_error
  88. nfs4_do_reclaim
  89. nfs4_check_lease
  90. nfs4_handle_reclaim_lease_error
  91. nfs4_establish_lease
  92. nfs4_reclaim_lease
  93. nfs4_purge_lease
  94. nfs4_try_migration
  95. nfs4_handle_migration
  96. nfs4_handle_lease_moved
  97. nfs4_discover_server_trunking
  98. nfs4_schedule_session_recovery
  99. nfs41_notify_server
  100. nfs4_reset_all_state
  101. nfs41_handle_server_reboot
  102. nfs41_handle_all_state_revoked
  103. nfs41_handle_some_state_revoked
  104. nfs41_handle_recallable_state_revoked
  105. nfs41_handle_backchannel_fault
  106. nfs41_handle_cb_path_down
  107. nfs41_handle_sequence_flag_errors
  108. nfs4_reset_session
  109. nfs4_bind_conn_to_session
  110. nfs4_reset_session
  111. nfs4_bind_conn_to_session
  112. nfs4_state_manager
  113. nfs4_run_state_manager

   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/kthread.h>
  46 #include <linux/module.h>
  47 #include <linux/random.h>
  48 #include <linux/ratelimit.h>
  49 #include <linux/workqueue.h>
  50 #include <linux/bitops.h>
  51 #include <linux/jiffies.h>
  52 
  53 #include <linux/sunrpc/clnt.h>
  54 
  55 #include "nfs4_fs.h"
  56 #include "callback.h"
  57 #include "delegation.h"
  58 #include "internal.h"
  59 #include "nfs4idmap.h"
  60 #include "nfs4session.h"
  61 #include "pnfs.h"
  62 #include "netns.h"
  63 
  64 #define NFSDBG_FACILITY         NFSDBG_STATE
  65 
  66 #define OPENOWNER_POOL_SIZE     8
  67 
  68 const nfs4_stateid zero_stateid = {
  69         { .data = { 0 } },
  70         .type = NFS4_SPECIAL_STATEID_TYPE,
  71 };
  72 const nfs4_stateid invalid_stateid = {
  73         {
  74                 /* Funky initialiser keeps older gcc versions happy */
  75                 .data = { 0xff, 0xff, 0xff, 0xff, 0 },
  76         },
  77         .type = NFS4_INVALID_STATEID_TYPE,
  78 };
  79 
  80 const nfs4_stateid current_stateid = {
  81         {
  82                 /* Funky initialiser keeps older gcc versions happy */
  83                 .data = { 0x0, 0x0, 0x0, 0x1, 0 },
  84         },
  85         .type = NFS4_SPECIAL_STATEID_TYPE,
  86 };
  87 
  88 static DEFINE_MUTEX(nfs_clid_init_mutex);
  89 
  90 static int nfs4_setup_state_renewal(struct nfs_client *clp)
  91 {
  92         int status;
  93         struct nfs_fsinfo fsinfo;
  94 
  95         if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
  96                 nfs4_schedule_state_renewal(clp);
  97                 return 0;
  98         }
  99 
 100         status = nfs4_proc_get_lease_time(clp, &fsinfo);
 101         if (status == 0) {
 102                 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ);
 103                 nfs4_schedule_state_renewal(clp);
 104         }
 105 
 106         return status;
 107 }
 108 
 109 int nfs4_init_clientid(struct nfs_client *clp, const struct cred *cred)
 110 {
 111         struct nfs4_setclientid_res clid = {
 112                 .clientid = clp->cl_clientid,
 113                 .confirm = clp->cl_confirm,
 114         };
 115         unsigned short port;
 116         int status;
 117         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
 118 
 119         if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
 120                 goto do_confirm;
 121         port = nn->nfs_callback_tcpport;
 122         if (clp->cl_addr.ss_family == AF_INET6)
 123                 port = nn->nfs_callback_tcpport6;
 124 
 125         status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
 126         if (status != 0)
 127                 goto out;
 128         clp->cl_clientid = clid.clientid;
 129         clp->cl_confirm = clid.confirm;
 130         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 131 do_confirm:
 132         status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
 133         if (status != 0)
 134                 goto out;
 135         clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 136         nfs4_setup_state_renewal(clp);
 137 out:
 138         return status;
 139 }
 140 
 141 /**
 142  * nfs40_discover_server_trunking - Detect server IP address trunking (mv0)
 143  *
 144  * @clp: nfs_client under test
 145  * @result: OUT: found nfs_client, or clp
 146  * @cred: credential to use for trunking test
 147  *
 148  * Returns zero, a negative errno, or a negative NFS4ERR status.
 149  * If zero is returned, an nfs_client pointer is planted in
 150  * "result".
 151  *
 152  * Note: The returned client may not yet be marked ready.
 153  */
 154 int nfs40_discover_server_trunking(struct nfs_client *clp,
 155                                    struct nfs_client **result,
 156                                    const struct cred *cred)
 157 {
 158         struct nfs4_setclientid_res clid = {
 159                 .clientid = clp->cl_clientid,
 160                 .confirm = clp->cl_confirm,
 161         };
 162         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
 163         unsigned short port;
 164         int status;
 165 
 166         port = nn->nfs_callback_tcpport;
 167         if (clp->cl_addr.ss_family == AF_INET6)
 168                 port = nn->nfs_callback_tcpport6;
 169 
 170         status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
 171         if (status != 0)
 172                 goto out;
 173         clp->cl_clientid = clid.clientid;
 174         clp->cl_confirm = clid.confirm;
 175 
 176         status = nfs40_walk_client_list(clp, result, cred);
 177         if (status == 0) {
 178                 /* Sustain the lease, even if it's empty.  If the clientid4
 179                  * goes stale it's of no use for trunking discovery. */
 180                 nfs4_schedule_state_renewal(*result);
 181 
 182                 /* If the client state need to recover, do it. */
 183                 if (clp->cl_state)
 184                         nfs4_schedule_state_manager(clp);
 185         }
 186 out:
 187         return status;
 188 }
 189 
 190 const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
 191 {
 192         return get_cred(rpc_machine_cred());
 193 }
 194 
 195 static void nfs4_root_machine_cred(struct nfs_client *clp)
 196 {
 197 
 198         /* Force root creds instead of machine */
 199         clp->cl_principal = NULL;
 200         clp->cl_rpcclient->cl_principal = NULL;
 201 }
 202 
 203 static const struct cred *
 204 nfs4_get_renew_cred_server_locked(struct nfs_server *server)
 205 {
 206         const struct cred *cred = NULL;
 207         struct nfs4_state_owner *sp;
 208         struct rb_node *pos;
 209 
 210         for (pos = rb_first(&server->state_owners);
 211              pos != NULL;
 212              pos = rb_next(pos)) {
 213                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
 214                 if (list_empty(&sp->so_states))
 215                         continue;
 216                 cred = get_cred(sp->so_cred);
 217                 break;
 218         }
 219         return cred;
 220 }
 221 
 222 /**
 223  * nfs4_get_renew_cred - Acquire credential for a renew operation
 224  * @clp: client state handle
 225  *
 226  * Returns an rpc_cred with reference count bumped, or NULL.
 227  * Caller must hold clp->cl_lock.
 228  */
 229 const struct cred *nfs4_get_renew_cred(struct nfs_client *clp)
 230 {
 231         const struct cred *cred = NULL;
 232         struct nfs_server *server;
 233 
 234         /* Use machine credentials if available */
 235         cred = nfs4_get_machine_cred(clp);
 236         if (cred != NULL)
 237                 goto out;
 238 
 239         spin_lock(&clp->cl_lock);
 240         rcu_read_lock();
 241         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
 242                 cred = nfs4_get_renew_cred_server_locked(server);
 243                 if (cred != NULL)
 244                         break;
 245         }
 246         rcu_read_unlock();
 247         spin_unlock(&clp->cl_lock);
 248 
 249 out:
 250         return cred;
 251 }
 252 
 253 static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
 254 {
 255         if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
 256                 spin_lock(&tbl->slot_tbl_lock);
 257                 nfs41_wake_slot_table(tbl);
 258                 spin_unlock(&tbl->slot_tbl_lock);
 259         }
 260 }
 261 
 262 static void nfs4_end_drain_session(struct nfs_client *clp)
 263 {
 264         struct nfs4_session *ses = clp->cl_session;
 265 
 266         if (clp->cl_slot_tbl) {
 267                 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
 268                 return;
 269         }
 270 
 271         if (ses != NULL) {
 272                 nfs4_end_drain_slot_table(&ses->bc_slot_table);
 273                 nfs4_end_drain_slot_table(&ses->fc_slot_table);
 274         }
 275 }
 276 
 277 static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
 278 {
 279         set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
 280         spin_lock(&tbl->slot_tbl_lock);
 281         if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
 282                 reinit_completion(&tbl->complete);
 283                 spin_unlock(&tbl->slot_tbl_lock);
 284                 return wait_for_completion_interruptible(&tbl->complete);
 285         }
 286         spin_unlock(&tbl->slot_tbl_lock);
 287         return 0;
 288 }
 289 
 290 static int nfs4_begin_drain_session(struct nfs_client *clp)
 291 {
 292         struct nfs4_session *ses = clp->cl_session;
 293         int ret;
 294 
 295         if (clp->cl_slot_tbl)
 296                 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
 297 
 298         /* back channel */
 299         ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
 300         if (ret)
 301                 return ret;
 302         /* fore channel */
 303         return nfs4_drain_slot_tbl(&ses->fc_slot_table);
 304 }
 305 
 306 #if defined(CONFIG_NFS_V4_1)
 307 
 308 static void nfs41_finish_session_reset(struct nfs_client *clp)
 309 {
 310         clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 311         clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
 312         /* create_session negotiated new slot table */
 313         clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
 314         nfs4_setup_state_renewal(clp);
 315 }
 316 
 317 int nfs41_init_clientid(struct nfs_client *clp, const struct cred *cred)
 318 {
 319         int status;
 320 
 321         if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
 322                 goto do_confirm;
 323         status = nfs4_proc_exchange_id(clp, cred);
 324         if (status != 0)
 325                 goto out;
 326         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 327 do_confirm:
 328         status = nfs4_proc_create_session(clp, cred);
 329         if (status != 0)
 330                 goto out;
 331         nfs41_finish_session_reset(clp);
 332         nfs_mark_client_ready(clp, NFS_CS_READY);
 333 out:
 334         return status;
 335 }
 336 
 337 /**
 338  * nfs41_discover_server_trunking - Detect server IP address trunking (mv1)
 339  *
 340  * @clp: nfs_client under test
 341  * @result: OUT: found nfs_client, or clp
 342  * @cred: credential to use for trunking test
 343  *
 344  * Returns NFS4_OK, a negative errno, or a negative NFS4ERR status.
 345  * If NFS4_OK is returned, an nfs_client pointer is planted in
 346  * "result".
 347  *
 348  * Note: The returned client may not yet be marked ready.
 349  */
 350 int nfs41_discover_server_trunking(struct nfs_client *clp,
 351                                    struct nfs_client **result,
 352                                    const struct cred *cred)
 353 {
 354         int status;
 355 
 356         status = nfs4_proc_exchange_id(clp, cred);
 357         if (status != NFS4_OK)
 358                 return status;
 359 
 360         status = nfs41_walk_client_list(clp, result, cred);
 361         if (status < 0)
 362                 return status;
 363         if (clp != *result)
 364                 return 0;
 365 
 366         /*
 367          * Purge state if the client id was established in a prior
 368          * instance and the client id could not have arrived on the
 369          * server via Transparent State Migration.
 370          */
 371         if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
 372                 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
 373                         set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
 374                 else
 375                         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
 376         }
 377         nfs4_schedule_state_manager(clp);
 378         status = nfs_wait_client_init_complete(clp);
 379         if (status < 0)
 380                 nfs_put_client(clp);
 381         return status;
 382 }
 383 
 384 #endif /* CONFIG_NFS_V4_1 */
 385 
 386 /**
 387  * nfs4_get_clid_cred - Acquire credential for a setclientid operation
 388  * @clp: client state handle
 389  *
 390  * Returns a cred with reference count bumped, or NULL.
 391  */
 392 const struct cred *nfs4_get_clid_cred(struct nfs_client *clp)
 393 {
 394         const struct cred *cred;
 395 
 396         cred = nfs4_get_machine_cred(clp);
 397         return cred;
 398 }
 399 
 400 static struct nfs4_state_owner *
 401 nfs4_find_state_owner_locked(struct nfs_server *server, const struct cred *cred)
 402 {
 403         struct rb_node **p = &server->state_owners.rb_node,
 404                        *parent = NULL;
 405         struct nfs4_state_owner *sp;
 406         int cmp;
 407 
 408         while (*p != NULL) {
 409                 parent = *p;
 410                 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
 411                 cmp = cred_fscmp(cred, sp->so_cred);
 412 
 413                 if (cmp < 0)
 414                         p = &parent->rb_left;
 415                 else if (cmp > 0)
 416                         p = &parent->rb_right;
 417                 else {
 418                         if (!list_empty(&sp->so_lru))
 419                                 list_del_init(&sp->so_lru);
 420                         atomic_inc(&sp->so_count);
 421                         return sp;
 422                 }
 423         }
 424         return NULL;
 425 }
 426 
 427 static struct nfs4_state_owner *
 428 nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
 429 {
 430         struct nfs_server *server = new->so_server;
 431         struct rb_node **p = &server->state_owners.rb_node,
 432                        *parent = NULL;
 433         struct nfs4_state_owner *sp;
 434         int cmp;
 435 
 436         while (*p != NULL) {
 437                 parent = *p;
 438                 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
 439                 cmp = cred_fscmp(new->so_cred, sp->so_cred);
 440 
 441                 if (cmp < 0)
 442                         p = &parent->rb_left;
 443                 else if (cmp > 0)
 444                         p = &parent->rb_right;
 445                 else {
 446                         if (!list_empty(&sp->so_lru))
 447                                 list_del_init(&sp->so_lru);
 448                         atomic_inc(&sp->so_count);
 449                         return sp;
 450                 }
 451         }
 452         rb_link_node(&new->so_server_node, parent, p);
 453         rb_insert_color(&new->so_server_node, &server->state_owners);
 454         return new;
 455 }
 456 
 457 static void
 458 nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
 459 {
 460         struct nfs_server *server = sp->so_server;
 461 
 462         if (!RB_EMPTY_NODE(&sp->so_server_node))
 463                 rb_erase(&sp->so_server_node, &server->state_owners);
 464 }
 465 
 466 static void
 467 nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
 468 {
 469         sc->create_time = ktime_get();
 470         sc->flags = 0;
 471         sc->counter = 0;
 472         spin_lock_init(&sc->lock);
 473         INIT_LIST_HEAD(&sc->list);
 474         rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
 475 }
 476 
 477 static void
 478 nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
 479 {
 480         rpc_destroy_wait_queue(&sc->wait);
 481 }
 482 
 483 /*
 484  * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
 485  * create a new state_owner.
 486  *
 487  */
 488 static struct nfs4_state_owner *
 489 nfs4_alloc_state_owner(struct nfs_server *server,
 490                 const struct cred *cred,
 491                 gfp_t gfp_flags)
 492 {
 493         struct nfs4_state_owner *sp;
 494 
 495         sp = kzalloc(sizeof(*sp), gfp_flags);
 496         if (!sp)
 497                 return NULL;
 498         sp->so_seqid.owner_id = ida_simple_get(&server->openowner_id, 0, 0,
 499                                                 gfp_flags);
 500         if (sp->so_seqid.owner_id < 0) {
 501                 kfree(sp);
 502                 return NULL;
 503         }
 504         sp->so_server = server;
 505         sp->so_cred = get_cred(cred);
 506         spin_lock_init(&sp->so_lock);
 507         INIT_LIST_HEAD(&sp->so_states);
 508         nfs4_init_seqid_counter(&sp->so_seqid);
 509         atomic_set(&sp->so_count, 1);
 510         INIT_LIST_HEAD(&sp->so_lru);
 511         seqcount_init(&sp->so_reclaim_seqcount);
 512         mutex_init(&sp->so_delegreturn_mutex);
 513         return sp;
 514 }
 515 
 516 static void
 517 nfs4_reset_state_owner(struct nfs4_state_owner *sp)
 518 {
 519         /* This state_owner is no longer usable, but must
 520          * remain in place so that state recovery can find it
 521          * and the opens associated with it.
 522          * It may also be used for new 'open' request to
 523          * return a delegation to the server.
 524          * So update the 'create_time' so that it looks like
 525          * a new state_owner.  This will cause the server to
 526          * request an OPEN_CONFIRM to start a new sequence.
 527          */
 528         sp->so_seqid.create_time = ktime_get();
 529 }
 530 
 531 static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
 532 {
 533         nfs4_destroy_seqid_counter(&sp->so_seqid);
 534         put_cred(sp->so_cred);
 535         ida_simple_remove(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
 536         kfree(sp);
 537 }
 538 
 539 static void nfs4_gc_state_owners(struct nfs_server *server)
 540 {
 541         struct nfs_client *clp = server->nfs_client;
 542         struct nfs4_state_owner *sp, *tmp;
 543         unsigned long time_min, time_max;
 544         LIST_HEAD(doomed);
 545 
 546         spin_lock(&clp->cl_lock);
 547         time_max = jiffies;
 548         time_min = (long)time_max - (long)clp->cl_lease_time;
 549         list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
 550                 /* NB: LRU is sorted so that oldest is at the head */
 551                 if (time_in_range(sp->so_expires, time_min, time_max))
 552                         break;
 553                 list_move(&sp->so_lru, &doomed);
 554                 nfs4_remove_state_owner_locked(sp);
 555         }
 556         spin_unlock(&clp->cl_lock);
 557 
 558         list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
 559                 list_del(&sp->so_lru);
 560                 nfs4_free_state_owner(sp);
 561         }
 562 }
 563 
 564 /**
 565  * nfs4_get_state_owner - Look up a state owner given a credential
 566  * @server: nfs_server to search
 567  * @cred: RPC credential to match
 568  * @gfp_flags: allocation mode
 569  *
 570  * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
 571  */
 572 struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
 573                                               const struct cred *cred,
 574                                               gfp_t gfp_flags)
 575 {
 576         struct nfs_client *clp = server->nfs_client;
 577         struct nfs4_state_owner *sp, *new;
 578 
 579         spin_lock(&clp->cl_lock);
 580         sp = nfs4_find_state_owner_locked(server, cred);
 581         spin_unlock(&clp->cl_lock);
 582         if (sp != NULL)
 583                 goto out;
 584         new = nfs4_alloc_state_owner(server, cred, gfp_flags);
 585         if (new == NULL)
 586                 goto out;
 587         spin_lock(&clp->cl_lock);
 588         sp = nfs4_insert_state_owner_locked(new);
 589         spin_unlock(&clp->cl_lock);
 590         if (sp != new)
 591                 nfs4_free_state_owner(new);
 592 out:
 593         nfs4_gc_state_owners(server);
 594         return sp;
 595 }
 596 
 597 /**
 598  * nfs4_put_state_owner - Release a nfs4_state_owner
 599  * @sp: state owner data to release
 600  *
 601  * Note that we keep released state owners on an LRU
 602  * list.
 603  * This caches valid state owners so that they can be
 604  * reused, to avoid the OPEN_CONFIRM on minor version 0.
 605  * It also pins the uniquifier of dropped state owners for
 606  * a while, to ensure that those state owner names are
 607  * never reused.
 608  */
 609 void nfs4_put_state_owner(struct nfs4_state_owner *sp)
 610 {
 611         struct nfs_server *server = sp->so_server;
 612         struct nfs_client *clp = server->nfs_client;
 613 
 614         if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
 615                 return;
 616 
 617         sp->so_expires = jiffies;
 618         list_add_tail(&sp->so_lru, &server->state_owners_lru);
 619         spin_unlock(&clp->cl_lock);
 620 }
 621 
 622 /**
 623  * nfs4_purge_state_owners - Release all cached state owners
 624  * @server: nfs_server with cached state owners to release
 625  * @head: resulting list of state owners
 626  *
 627  * Called at umount time.  Remaining state owners will be on
 628  * the LRU with ref count of zero.
 629  * Note that the state owners are not freed, but are added
 630  * to the list @head, which can later be used as an argument
 631  * to nfs4_free_state_owners.
 632  */
 633 void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
 634 {
 635         struct nfs_client *clp = server->nfs_client;
 636         struct nfs4_state_owner *sp, *tmp;
 637 
 638         spin_lock(&clp->cl_lock);
 639         list_for_each_entry_safe(sp, tmp, &server->state_owners_lru, so_lru) {
 640                 list_move(&sp->so_lru, head);
 641                 nfs4_remove_state_owner_locked(sp);
 642         }
 643         spin_unlock(&clp->cl_lock);
 644 }
 645 
 646 /**
 647  * nfs4_purge_state_owners - Release all cached state owners
 648  * @head: resulting list of state owners
 649  *
 650  * Frees a list of state owners that was generated by
 651  * nfs4_purge_state_owners
 652  */
 653 void nfs4_free_state_owners(struct list_head *head)
 654 {
 655         struct nfs4_state_owner *sp, *tmp;
 656 
 657         list_for_each_entry_safe(sp, tmp, head, so_lru) {
 658                 list_del(&sp->so_lru);
 659                 nfs4_free_state_owner(sp);
 660         }
 661 }
 662 
 663 static struct nfs4_state *
 664 nfs4_alloc_open_state(void)
 665 {
 666         struct nfs4_state *state;
 667 
 668         state = kzalloc(sizeof(*state), GFP_NOFS);
 669         if (!state)
 670                 return NULL;
 671         refcount_set(&state->count, 1);
 672         INIT_LIST_HEAD(&state->lock_states);
 673         spin_lock_init(&state->state_lock);
 674         seqlock_init(&state->seqlock);
 675         init_waitqueue_head(&state->waitq);
 676         return state;
 677 }
 678 
 679 void
 680 nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
 681 {
 682         if (state->state == fmode)
 683                 return;
 684         /* NB! List reordering - see the reclaim code for why.  */
 685         if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
 686                 if (fmode & FMODE_WRITE)
 687                         list_move(&state->open_states, &state->owner->so_states);
 688                 else
 689                         list_move_tail(&state->open_states, &state->owner->so_states);
 690         }
 691         state->state = fmode;
 692 }
 693 
 694 static struct nfs4_state *
 695 __nfs4_find_state_byowner(struct inode *inode, struct nfs4_state_owner *owner)
 696 {
 697         struct nfs_inode *nfsi = NFS_I(inode);
 698         struct nfs4_state *state;
 699 
 700         list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
 701                 if (state->owner != owner)
 702                         continue;
 703                 if (!nfs4_valid_open_stateid(state))
 704                         continue;
 705                 if (refcount_inc_not_zero(&state->count))
 706                         return state;
 707         }
 708         return NULL;
 709 }
 710 
 711 static void
 712 nfs4_free_open_state(struct nfs4_state *state)
 713 {
 714         kfree_rcu(state, rcu_head);
 715 }
 716 
 717 struct nfs4_state *
 718 nfs4_get_open_state(struct inode *inode, struct nfs4_state_owner *owner)
 719 {
 720         struct nfs4_state *state, *new;
 721         struct nfs_inode *nfsi = NFS_I(inode);
 722 
 723         rcu_read_lock();
 724         state = __nfs4_find_state_byowner(inode, owner);
 725         rcu_read_unlock();
 726         if (state)
 727                 goto out;
 728         new = nfs4_alloc_open_state();
 729         spin_lock(&owner->so_lock);
 730         spin_lock(&inode->i_lock);
 731         state = __nfs4_find_state_byowner(inode, owner);
 732         if (state == NULL && new != NULL) {
 733                 state = new;
 734                 state->owner = owner;
 735                 atomic_inc(&owner->so_count);
 736                 ihold(inode);
 737                 state->inode = inode;
 738                 list_add_rcu(&state->inode_states, &nfsi->open_states);
 739                 spin_unlock(&inode->i_lock);
 740                 /* Note: The reclaim code dictates that we add stateless
 741                  * and read-only stateids to the end of the list */
 742                 list_add_tail(&state->open_states, &owner->so_states);
 743                 spin_unlock(&owner->so_lock);
 744         } else {
 745                 spin_unlock(&inode->i_lock);
 746                 spin_unlock(&owner->so_lock);
 747                 if (new)
 748                         nfs4_free_open_state(new);
 749         }
 750 out:
 751         return state;
 752 }
 753 
 754 void nfs4_put_open_state(struct nfs4_state *state)
 755 {
 756         struct inode *inode = state->inode;
 757         struct nfs4_state_owner *owner = state->owner;
 758 
 759         if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
 760                 return;
 761         spin_lock(&inode->i_lock);
 762         list_del_rcu(&state->inode_states);
 763         list_del(&state->open_states);
 764         spin_unlock(&inode->i_lock);
 765         spin_unlock(&owner->so_lock);
 766         iput(inode);
 767         nfs4_free_open_state(state);
 768         nfs4_put_state_owner(owner);
 769 }
 770 
 771 /*
 772  * Close the current file.
 773  */
 774 static void __nfs4_close(struct nfs4_state *state,
 775                 fmode_t fmode, gfp_t gfp_mask, int wait)
 776 {
 777         struct nfs4_state_owner *owner = state->owner;
 778         int call_close = 0;
 779         fmode_t newstate;
 780 
 781         atomic_inc(&owner->so_count);
 782         /* Protect against nfs4_find_state() */
 783         spin_lock(&owner->so_lock);
 784         switch (fmode & (FMODE_READ | FMODE_WRITE)) {
 785                 case FMODE_READ:
 786                         state->n_rdonly--;
 787                         break;
 788                 case FMODE_WRITE:
 789                         state->n_wronly--;
 790                         break;
 791                 case FMODE_READ|FMODE_WRITE:
 792                         state->n_rdwr--;
 793         }
 794         newstate = FMODE_READ|FMODE_WRITE;
 795         if (state->n_rdwr == 0) {
 796                 if (state->n_rdonly == 0) {
 797                         newstate &= ~FMODE_READ;
 798                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
 799                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
 800                 }
 801                 if (state->n_wronly == 0) {
 802                         newstate &= ~FMODE_WRITE;
 803                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
 804                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
 805                 }
 806                 if (newstate == 0)
 807                         clear_bit(NFS_DELEGATED_STATE, &state->flags);
 808         }
 809         nfs4_state_set_mode_locked(state, newstate);
 810         spin_unlock(&owner->so_lock);
 811 
 812         if (!call_close) {
 813                 nfs4_put_open_state(state);
 814                 nfs4_put_state_owner(owner);
 815         } else
 816                 nfs4_do_close(state, gfp_mask, wait);
 817 }
 818 
 819 void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
 820 {
 821         __nfs4_close(state, fmode, GFP_NOFS, 0);
 822 }
 823 
 824 void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
 825 {
 826         __nfs4_close(state, fmode, GFP_KERNEL, 1);
 827 }
 828 
 829 /*
 830  * Search the state->lock_states for an existing lock_owner
 831  * that is compatible with either of the given owners.
 832  * If the second is non-zero, then the first refers to a Posix-lock
 833  * owner (current->files) and the second refers to a flock/OFD
 834  * owner (struct file*).  In that case, prefer a match for the first
 835  * owner.
 836  * If both sorts of locks are held on the one file we cannot know
 837  * which stateid was intended to be used, so a "correct" choice cannot
 838  * be made.  Failing that, a "consistent" choice is preferable.  The
 839  * consistent choice we make is to prefer the first owner, that of a
 840  * Posix lock.
 841  */
 842 static struct nfs4_lock_state *
 843 __nfs4_find_lock_state(struct nfs4_state *state,
 844                        fl_owner_t fl_owner, fl_owner_t fl_owner2)
 845 {
 846         struct nfs4_lock_state *pos, *ret = NULL;
 847         list_for_each_entry(pos, &state->lock_states, ls_locks) {
 848                 if (pos->ls_owner == fl_owner) {
 849                         ret = pos;
 850                         break;
 851                 }
 852                 if (pos->ls_owner == fl_owner2)
 853                         ret = pos;
 854         }
 855         if (ret)
 856                 refcount_inc(&ret->ls_count);
 857         return ret;
 858 }
 859 
 860 /*
 861  * Return a compatible lock_state. If no initialized lock_state structure
 862  * exists, return an uninitialized one.
 863  *
 864  */
 865 static struct nfs4_lock_state *nfs4_alloc_lock_state(struct nfs4_state *state, fl_owner_t fl_owner)
 866 {
 867         struct nfs4_lock_state *lsp;
 868         struct nfs_server *server = state->owner->so_server;
 869 
 870         lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
 871         if (lsp == NULL)
 872                 return NULL;
 873         nfs4_init_seqid_counter(&lsp->ls_seqid);
 874         refcount_set(&lsp->ls_count, 1);
 875         lsp->ls_state = state;
 876         lsp->ls_owner = fl_owner;
 877         lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
 878         if (lsp->ls_seqid.owner_id < 0)
 879                 goto out_free;
 880         INIT_LIST_HEAD(&lsp->ls_locks);
 881         return lsp;
 882 out_free:
 883         kfree(lsp);
 884         return NULL;
 885 }
 886 
 887 void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
 888 {
 889         ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
 890         nfs4_destroy_seqid_counter(&lsp->ls_seqid);
 891         kfree(lsp);
 892 }
 893 
 894 /*
 895  * Return a compatible lock_state. If no initialized lock_state structure
 896  * exists, return an uninitialized one.
 897  *
 898  */
 899 static struct nfs4_lock_state *nfs4_get_lock_state(struct nfs4_state *state, fl_owner_t owner)
 900 {
 901         struct nfs4_lock_state *lsp, *new = NULL;
 902         
 903         for(;;) {
 904                 spin_lock(&state->state_lock);
 905                 lsp = __nfs4_find_lock_state(state, owner, NULL);
 906                 if (lsp != NULL)
 907                         break;
 908                 if (new != NULL) {
 909                         list_add(&new->ls_locks, &state->lock_states);
 910                         set_bit(LK_STATE_IN_USE, &state->flags);
 911                         lsp = new;
 912                         new = NULL;
 913                         break;
 914                 }
 915                 spin_unlock(&state->state_lock);
 916                 new = nfs4_alloc_lock_state(state, owner);
 917                 if (new == NULL)
 918                         return NULL;
 919         }
 920         spin_unlock(&state->state_lock);
 921         if (new != NULL)
 922                 nfs4_free_lock_state(state->owner->so_server, new);
 923         return lsp;
 924 }
 925 
 926 /*
 927  * Release reference to lock_state, and free it if we see that
 928  * it is no longer in use
 929  */
 930 void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
 931 {
 932         struct nfs_server *server;
 933         struct nfs4_state *state;
 934 
 935         if (lsp == NULL)
 936                 return;
 937         state = lsp->ls_state;
 938         if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
 939                 return;
 940         list_del(&lsp->ls_locks);
 941         if (list_empty(&state->lock_states))
 942                 clear_bit(LK_STATE_IN_USE, &state->flags);
 943         spin_unlock(&state->state_lock);
 944         server = state->owner->so_server;
 945         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
 946                 struct nfs_client *clp = server->nfs_client;
 947 
 948                 clp->cl_mvops->free_lock_state(server, lsp);
 949         } else
 950                 nfs4_free_lock_state(server, lsp);
 951 }
 952 
 953 static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
 954 {
 955         struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
 956 
 957         dst->fl_u.nfs4_fl.owner = lsp;
 958         refcount_inc(&lsp->ls_count);
 959 }
 960 
 961 static void nfs4_fl_release_lock(struct file_lock *fl)
 962 {
 963         nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
 964 }
 965 
 966 static const struct file_lock_operations nfs4_fl_lock_ops = {
 967         .fl_copy_lock = nfs4_fl_copy_lock,
 968         .fl_release_private = nfs4_fl_release_lock,
 969 };
 970 
 971 int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
 972 {
 973         struct nfs4_lock_state *lsp;
 974 
 975         if (fl->fl_ops != NULL)
 976                 return 0;
 977         lsp = nfs4_get_lock_state(state, fl->fl_owner);
 978         if (lsp == NULL)
 979                 return -ENOMEM;
 980         fl->fl_u.nfs4_fl.owner = lsp;
 981         fl->fl_ops = &nfs4_fl_lock_ops;
 982         return 0;
 983 }
 984 
 985 static int nfs4_copy_lock_stateid(nfs4_stateid *dst,
 986                 struct nfs4_state *state,
 987                 const struct nfs_lock_context *l_ctx)
 988 {
 989         struct nfs4_lock_state *lsp;
 990         fl_owner_t fl_owner, fl_flock_owner;
 991         int ret = -ENOENT;
 992 
 993         if (l_ctx == NULL)
 994                 goto out;
 995 
 996         if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
 997                 goto out;
 998 
 999         fl_owner = l_ctx->lockowner;
1000         fl_flock_owner = l_ctx->open_context->flock_owner;
1001 
1002         spin_lock(&state->state_lock);
1003         lsp = __nfs4_find_lock_state(state, fl_owner, fl_flock_owner);
1004         if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
1005                 ret = -EIO;
1006         else if (lsp != NULL && test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0) {
1007                 nfs4_stateid_copy(dst, &lsp->ls_stateid);
1008                 ret = 0;
1009         }
1010         spin_unlock(&state->state_lock);
1011         nfs4_put_lock_state(lsp);
1012 out:
1013         return ret;
1014 }
1015 
1016 bool nfs4_copy_open_stateid(nfs4_stateid *dst, struct nfs4_state *state)
1017 {
1018         bool ret;
1019         const nfs4_stateid *src;
1020         int seq;
1021 
1022         do {
1023                 ret = false;
1024                 src = &zero_stateid;
1025                 seq = read_seqbegin(&state->seqlock);
1026                 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
1027                         src = &state->open_stateid;
1028                         ret = true;
1029                 }
1030                 nfs4_stateid_copy(dst, src);
1031         } while (read_seqretry(&state->seqlock, seq));
1032         return ret;
1033 }
1034 
1035 /*
1036  * Byte-range lock aware utility to initialize the stateid of read/write
1037  * requests.
1038  */
1039 int nfs4_select_rw_stateid(struct nfs4_state *state,
1040                 fmode_t fmode, const struct nfs_lock_context *l_ctx,
1041                 nfs4_stateid *dst, const struct cred **cred)
1042 {
1043         int ret;
1044 
1045         if (!nfs4_valid_open_stateid(state))
1046                 return -EIO;
1047         if (cred != NULL)
1048                 *cred = NULL;
1049         ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
1050         if (ret == -EIO)
1051                 /* A lost lock - don't even consider delegations */
1052                 goto out;
1053         /* returns true if delegation stateid found and copied */
1054         if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
1055                 ret = 0;
1056                 goto out;
1057         }
1058         if (ret != -ENOENT)
1059                 /* nfs4_copy_delegation_stateid() didn't over-write
1060                  * dst, so it still has the lock stateid which we now
1061                  * choose to use.
1062                  */
1063                 goto out;
1064         ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
1065 out:
1066         if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
1067                 dst->seqid = 0;
1068         return ret;
1069 }
1070 
1071 struct nfs_seqid *nfs_alloc_seqid(struct nfs_seqid_counter *counter, gfp_t gfp_mask)
1072 {
1073         struct nfs_seqid *new;
1074 
1075         new = kmalloc(sizeof(*new), gfp_mask);
1076         if (new == NULL)
1077                 return ERR_PTR(-ENOMEM);
1078         new->sequence = counter;
1079         INIT_LIST_HEAD(&new->list);
1080         new->task = NULL;
1081         return new;
1082 }
1083 
1084 void nfs_release_seqid(struct nfs_seqid *seqid)
1085 {
1086         struct nfs_seqid_counter *sequence;
1087 
1088         if (seqid == NULL || list_empty(&seqid->list))
1089                 return;
1090         sequence = seqid->sequence;
1091         spin_lock(&sequence->lock);
1092         list_del_init(&seqid->list);
1093         if (!list_empty(&sequence->list)) {
1094                 struct nfs_seqid *next;
1095 
1096                 next = list_first_entry(&sequence->list,
1097                                 struct nfs_seqid, list);
1098                 rpc_wake_up_queued_task(&sequence->wait, next->task);
1099         }
1100         spin_unlock(&sequence->lock);
1101 }
1102 
1103 void nfs_free_seqid(struct nfs_seqid *seqid)
1104 {
1105         nfs_release_seqid(seqid);
1106         kfree(seqid);
1107 }
1108 
1109 /*
1110  * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
1111  * failed with a seqid incrementing error -
1112  * see comments nfs4.h:seqid_mutating_error()
1113  */
1114 static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
1115 {
1116         switch (status) {
1117                 case 0:
1118                         break;
1119                 case -NFS4ERR_BAD_SEQID:
1120                         if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
1121                                 return;
1122                         pr_warn_ratelimited("NFS: v4 server returned a bad"
1123                                         " sequence-id error on an"
1124                                         " unconfirmed sequence %p!\n",
1125                                         seqid->sequence);
1126                 case -NFS4ERR_STALE_CLIENTID:
1127                 case -NFS4ERR_STALE_STATEID:
1128                 case -NFS4ERR_BAD_STATEID:
1129                 case -NFS4ERR_BADXDR:
1130                 case -NFS4ERR_RESOURCE:
1131                 case -NFS4ERR_NOFILEHANDLE:
1132                 case -NFS4ERR_MOVED:
1133                         /* Non-seqid mutating errors */
1134                         return;
1135         };
1136         /*
1137          * Note: no locking needed as we are guaranteed to be first
1138          * on the sequence list
1139          */
1140         seqid->sequence->counter++;
1141 }
1142 
1143 void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
1144 {
1145         struct nfs4_state_owner *sp;
1146 
1147         if (seqid == NULL)
1148                 return;
1149 
1150         sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
1151         if (status == -NFS4ERR_BAD_SEQID)
1152                 nfs4_reset_state_owner(sp);
1153         if (!nfs4_has_session(sp->so_server->nfs_client))
1154                 nfs_increment_seqid(status, seqid);
1155 }
1156 
1157 /*
1158  * Increment the seqid if the LOCK/LOCKU succeeded, or
1159  * failed with a seqid incrementing error -
1160  * see comments nfs4.h:seqid_mutating_error()
1161  */
1162 void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
1163 {
1164         if (seqid != NULL)
1165                 nfs_increment_seqid(status, seqid);
1166 }
1167 
1168 int nfs_wait_on_sequence(struct nfs_seqid *seqid, struct rpc_task *task)
1169 {
1170         struct nfs_seqid_counter *sequence;
1171         int status = 0;
1172 
1173         if (seqid == NULL)
1174                 goto out;
1175         sequence = seqid->sequence;
1176         spin_lock(&sequence->lock);
1177         seqid->task = task;
1178         if (list_empty(&seqid->list))
1179                 list_add_tail(&seqid->list, &sequence->list);
1180         if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
1181                 goto unlock;
1182         rpc_sleep_on(&sequence->wait, task, NULL);
1183         status = -EAGAIN;
1184 unlock:
1185         spin_unlock(&sequence->lock);
1186 out:
1187         return status;
1188 }
1189 
1190 static int nfs4_run_state_manager(void *);
1191 
1192 static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
1193 {
1194         smp_mb__before_atomic();
1195         clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
1196         smp_mb__after_atomic();
1197         wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
1198         rpc_wake_up(&clp->cl_rpcwaitq);
1199 }
1200 
1201 /*
1202  * Schedule the nfs_client asynchronous state management routine
1203  */
1204 void nfs4_schedule_state_manager(struct nfs_client *clp)
1205 {
1206         struct task_struct *task;
1207         char buf[INET6_ADDRSTRLEN + sizeof("-manager") + 1];
1208 
1209         set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
1210         if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
1211                 return;
1212         __module_get(THIS_MODULE);
1213         refcount_inc(&clp->cl_count);
1214 
1215         /* The rcu_read_lock() is not strictly necessary, as the state
1216          * manager is the only thread that ever changes the rpc_xprt
1217          * after it's initialized.  At this point, we're single threaded. */
1218         rcu_read_lock();
1219         snprintf(buf, sizeof(buf), "%s-manager",
1220                         rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
1221         rcu_read_unlock();
1222         task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
1223         if (IS_ERR(task)) {
1224                 printk(KERN_ERR "%s: kthread_run: %ld\n",
1225                         __func__, PTR_ERR(task));
1226                 nfs4_clear_state_manager_bit(clp);
1227                 nfs_put_client(clp);
1228                 module_put(THIS_MODULE);
1229         }
1230 }
1231 
1232 /*
1233  * Schedule a lease recovery attempt
1234  */
1235 void nfs4_schedule_lease_recovery(struct nfs_client *clp)
1236 {
1237         if (!clp)
1238                 return;
1239         if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1240                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1241         dprintk("%s: scheduling lease recovery for server %s\n", __func__,
1242                         clp->cl_hostname);
1243         nfs4_schedule_state_manager(clp);
1244 }
1245 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery);
1246 
1247 /**
1248  * nfs4_schedule_migration_recovery - trigger migration recovery
1249  *
1250  * @server: FSID that is migrating
1251  *
1252  * Returns zero if recovery has started, otherwise a negative NFS4ERR
1253  * value is returned.
1254  */
1255 int nfs4_schedule_migration_recovery(const struct nfs_server *server)
1256 {
1257         struct nfs_client *clp = server->nfs_client;
1258 
1259         if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
1260                 pr_err("NFS: volatile file handles not supported (server %s)\n",
1261                                 clp->cl_hostname);
1262                 return -NFS4ERR_IO;
1263         }
1264 
1265         if (test_bit(NFS_MIG_FAILED, &server->mig_status))
1266                 return -NFS4ERR_IO;
1267 
1268         dprintk("%s: scheduling migration recovery for (%llx:%llx) on %s\n",
1269                         __func__,
1270                         (unsigned long long)server->fsid.major,
1271                         (unsigned long long)server->fsid.minor,
1272                         clp->cl_hostname);
1273 
1274         set_bit(NFS_MIG_IN_TRANSITION,
1275                         &((struct nfs_server *)server)->mig_status);
1276         set_bit(NFS4CLNT_MOVED, &clp->cl_state);
1277 
1278         nfs4_schedule_state_manager(clp);
1279         return 0;
1280 }
1281 EXPORT_SYMBOL_GPL(nfs4_schedule_migration_recovery);
1282 
1283 /**
1284  * nfs4_schedule_lease_moved_recovery - start lease-moved recovery
1285  *
1286  * @clp: server to check for moved leases
1287  *
1288  */
1289 void nfs4_schedule_lease_moved_recovery(struct nfs_client *clp)
1290 {
1291         dprintk("%s: scheduling lease-moved recovery for client ID %llx on %s\n",
1292                 __func__, clp->cl_clientid, clp->cl_hostname);
1293 
1294         set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
1295         nfs4_schedule_state_manager(clp);
1296 }
1297 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
1298 
1299 int nfs4_wait_clnt_recover(struct nfs_client *clp)
1300 {
1301         int res;
1302 
1303         might_sleep();
1304 
1305         refcount_inc(&clp->cl_count);
1306         res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
1307                                  nfs_wait_bit_killable, TASK_KILLABLE);
1308         if (res)
1309                 goto out;
1310         if (clp->cl_cons_state < 0)
1311                 res = clp->cl_cons_state;
1312 out:
1313         nfs_put_client(clp);
1314         return res;
1315 }
1316 
1317 int nfs4_client_recover_expired_lease(struct nfs_client *clp)
1318 {
1319         unsigned int loop;
1320         int ret;
1321 
1322         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
1323                 ret = nfs4_wait_clnt_recover(clp);
1324                 if (ret != 0)
1325                         break;
1326                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1327                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
1328                         break;
1329                 nfs4_schedule_state_manager(clp);
1330                 ret = -EIO;
1331         }
1332         return ret;
1333 }
1334 
1335 /*
1336  * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1337  * @clp: client to process
1338  *
1339  * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1340  * resend of the SETCLIENTID and hence re-establish the
1341  * callback channel. Then return all existing delegations.
1342  */
1343 static void nfs40_handle_cb_pathdown(struct nfs_client *clp)
1344 {
1345         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1346         nfs_expire_all_delegations(clp);
1347         dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
1348                         clp->cl_hostname);
1349 }
1350 
1351 void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
1352 {
1353         nfs40_handle_cb_pathdown(clp);
1354         nfs4_schedule_state_manager(clp);
1355 }
1356 
1357 static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
1358 {
1359 
1360         if (!nfs4_valid_open_stateid(state))
1361                 return 0;
1362         set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1363         /* Don't recover state that expired before the reboot */
1364         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
1365                 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1366                 return 0;
1367         }
1368         set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
1369         set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1370         return 1;
1371 }
1372 
1373 int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
1374 {
1375         if (!nfs4_valid_open_stateid(state))
1376                 return 0;
1377         set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1378         clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
1379         set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
1380         set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
1381         return 1;
1382 }
1383 
1384 int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
1385 {
1386         struct nfs_client *clp = server->nfs_client;
1387 
1388         if (!nfs4_state_mark_reclaim_nograce(clp, state))
1389                 return -EBADF;
1390         nfs_inode_find_delegation_state_and_recover(state->inode,
1391                         &state->stateid);
1392         dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
1393                         clp->cl_hostname);
1394         nfs4_schedule_state_manager(clp);
1395         return 0;
1396 }
1397 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
1398 
1399 static struct nfs4_lock_state *
1400 nfs_state_find_lock_state_by_stateid(struct nfs4_state *state,
1401                 const nfs4_stateid *stateid)
1402 {
1403         struct nfs4_lock_state *pos;
1404 
1405         list_for_each_entry(pos, &state->lock_states, ls_locks) {
1406                 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
1407                         continue;
1408                 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
1409                         return pos;
1410         }
1411         return NULL;
1412 }
1413 
1414 static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
1415                 const nfs4_stateid *stateid)
1416 {
1417         bool found = false;
1418 
1419         if (test_bit(LK_STATE_IN_USE, &state->flags)) {
1420                 spin_lock(&state->state_lock);
1421                 if (nfs_state_find_lock_state_by_stateid(state, stateid))
1422                         found = true;
1423                 spin_unlock(&state->state_lock);
1424         }
1425         return found;
1426 }
1427 
1428 void nfs_inode_find_state_and_recover(struct inode *inode,
1429                 const nfs4_stateid *stateid)
1430 {
1431         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1432         struct nfs_inode *nfsi = NFS_I(inode);
1433         struct nfs_open_context *ctx;
1434         struct nfs4_state *state;
1435         bool found = false;
1436 
1437         rcu_read_lock();
1438         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1439                 state = ctx->state;
1440                 if (state == NULL)
1441                         continue;
1442                 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
1443                     nfs4_state_mark_reclaim_nograce(clp, state)) {
1444                         found = true;
1445                         continue;
1446                 }
1447                 if (nfs4_stateid_match_other(&state->open_stateid, stateid) &&
1448                     nfs4_state_mark_reclaim_nograce(clp, state)) {
1449                         found = true;
1450                         continue;
1451                 }
1452                 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
1453                     nfs4_state_mark_reclaim_nograce(clp, state))
1454                         found = true;
1455         }
1456         rcu_read_unlock();
1457 
1458         nfs_inode_find_delegation_state_and_recover(inode, stateid);
1459         if (found)
1460                 nfs4_schedule_state_manager(clp);
1461 }
1462 
1463 static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
1464 {
1465         struct inode *inode = state->inode;
1466         struct nfs_inode *nfsi = NFS_I(inode);
1467         struct nfs_open_context *ctx;
1468 
1469         rcu_read_lock();
1470         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
1471                 if (ctx->state != state)
1472                         continue;
1473                 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
1474                 pr_warn("NFSv4: state recovery failed for open file %pd2, "
1475                                 "error = %d\n", ctx->dentry, err);
1476         }
1477         rcu_read_unlock();
1478 }
1479 
1480 static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
1481 {
1482         set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
1483         nfs4_state_mark_open_context_bad(state, error);
1484 }
1485 
1486 
1487 static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
1488 {
1489         struct inode *inode = state->inode;
1490         struct nfs_inode *nfsi = NFS_I(inode);
1491         struct file_lock *fl;
1492         struct nfs4_lock_state *lsp;
1493         int status = 0;
1494         struct file_lock_context *flctx = inode->i_flctx;
1495         struct list_head *list;
1496 
1497         if (flctx == NULL)
1498                 return 0;
1499 
1500         list = &flctx->flc_posix;
1501 
1502         /* Guard against delegation returns and new lock/unlock calls */
1503         down_write(&nfsi->rwsem);
1504         spin_lock(&flctx->flc_lock);
1505 restart:
1506         list_for_each_entry(fl, list, fl_list) {
1507                 if (nfs_file_open_context(fl->fl_file)->state != state)
1508                         continue;
1509                 spin_unlock(&flctx->flc_lock);
1510                 status = ops->recover_lock(state, fl);
1511                 switch (status) {
1512                 case 0:
1513                         break;
1514                 case -ETIMEDOUT:
1515                 case -ESTALE:
1516                 case -NFS4ERR_ADMIN_REVOKED:
1517                 case -NFS4ERR_STALE_STATEID:
1518                 case -NFS4ERR_BAD_STATEID:
1519                 case -NFS4ERR_EXPIRED:
1520                 case -NFS4ERR_NO_GRACE:
1521                 case -NFS4ERR_STALE_CLIENTID:
1522                 case -NFS4ERR_BADSESSION:
1523                 case -NFS4ERR_BADSLOT:
1524                 case -NFS4ERR_BAD_HIGH_SLOT:
1525                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1526                         goto out;
1527                 default:
1528                         pr_err("NFS: %s: unhandled error %d\n",
1529                                         __func__, status);
1530                         /* Fall through */
1531                 case -ENOMEM:
1532                 case -NFS4ERR_DENIED:
1533                 case -NFS4ERR_RECLAIM_BAD:
1534                 case -NFS4ERR_RECLAIM_CONFLICT:
1535                         lsp = fl->fl_u.nfs4_fl.owner;
1536                         if (lsp)
1537                                 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
1538                         status = 0;
1539                 }
1540                 spin_lock(&flctx->flc_lock);
1541         }
1542         if (list == &flctx->flc_posix) {
1543                 list = &flctx->flc_flock;
1544                 goto restart;
1545         }
1546         spin_unlock(&flctx->flc_lock);
1547 out:
1548         up_write(&nfsi->rwsem);
1549         return status;
1550 }
1551 
1552 #ifdef CONFIG_NFS_V4_2
1553 static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
1554 {
1555         struct nfs4_copy_state *copy;
1556 
1557         if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags))
1558                 return;
1559 
1560         spin_lock(&sp->so_server->nfs_client->cl_lock);
1561         list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
1562                 if (!nfs4_stateid_match_other(&state->stateid, &copy->parent_state->stateid))
1563                         continue;
1564                 copy->flags = 1;
1565                 complete(&copy->completion);
1566                 break;
1567         }
1568         spin_unlock(&sp->so_server->nfs_client->cl_lock);
1569 }
1570 #else /* !CONFIG_NFS_V4_2 */
1571 static inline void nfs42_complete_copies(struct nfs4_state_owner *sp,
1572                                          struct nfs4_state *state)
1573 {
1574 }
1575 #endif /* CONFIG_NFS_V4_2 */
1576 
1577 static int __nfs4_reclaim_open_state(struct nfs4_state_owner *sp, struct nfs4_state *state,
1578                                      const struct nfs4_state_recovery_ops *ops)
1579 {
1580         struct nfs4_lock_state *lock;
1581         int status;
1582 
1583         status = ops->recover_open(sp, state);
1584         if (status < 0)
1585                 return status;
1586 
1587         status = nfs4_reclaim_locks(state, ops);
1588         if (status < 0)
1589                 return status;
1590 
1591         if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
1592                 spin_lock(&state->state_lock);
1593                 list_for_each_entry(lock, &state->lock_states, ls_locks) {
1594                         if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
1595                                 pr_warn_ratelimited("NFS: %s: Lock reclaim failed!\n", __func__);
1596                 }
1597                 spin_unlock(&state->state_lock);
1598         }
1599 
1600         nfs42_complete_copies(sp, state);
1601         clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
1602         return status;
1603 }
1604 
1605 static int nfs4_reclaim_open_state(struct nfs4_state_owner *sp, const struct nfs4_state_recovery_ops *ops)
1606 {
1607         struct nfs4_state *state;
1608         unsigned int loop = 0;
1609         int status = 0;
1610 
1611         /* Note: we rely on the sp->so_states list being ordered 
1612          * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1613          * states first.
1614          * This is needed to ensure that the server won't give us any
1615          * read delegations that we have to return if, say, we are
1616          * recovering after a network partition or a reboot from a
1617          * server that doesn't support a grace period.
1618          */
1619         spin_lock(&sp->so_lock);
1620         raw_write_seqcount_begin(&sp->so_reclaim_seqcount);
1621 restart:
1622         list_for_each_entry(state, &sp->so_states, open_states) {
1623                 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
1624                         continue;
1625                 if (!nfs4_valid_open_stateid(state))
1626                         continue;
1627                 if (state->state == 0)
1628                         continue;
1629                 refcount_inc(&state->count);
1630                 spin_unlock(&sp->so_lock);
1631                 status = __nfs4_reclaim_open_state(sp, state, ops);
1632 
1633                 switch (status) {
1634                 default:
1635                         if (status >= 0) {
1636                                 loop = 0;
1637                                 break;
1638                         }
1639                         printk(KERN_ERR "NFS: %s: unhandled error %d\n", __func__, status);
1640                         /* Fall through */
1641                 case -ENOENT:
1642                 case -ENOMEM:
1643                 case -EACCES:
1644                 case -EROFS:
1645                 case -EIO:
1646                 case -ESTALE:
1647                         /* Open state on this file cannot be recovered */
1648                         nfs4_state_mark_recovery_failed(state, status);
1649                         break;
1650                 case -EAGAIN:
1651                         ssleep(1);
1652                         if (loop++ < 10) {
1653                                 set_bit(ops->state_flag_bit, &state->flags);
1654                                 break;
1655                         }
1656                         /* Fall through */
1657                 case -NFS4ERR_ADMIN_REVOKED:
1658                 case -NFS4ERR_STALE_STATEID:
1659                 case -NFS4ERR_OLD_STATEID:
1660                 case -NFS4ERR_BAD_STATEID:
1661                 case -NFS4ERR_RECLAIM_BAD:
1662                 case -NFS4ERR_RECLAIM_CONFLICT:
1663                         nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1664                         break;
1665                 case -NFS4ERR_EXPIRED:
1666                 case -NFS4ERR_NO_GRACE:
1667                         nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
1668                         /* Fall through */
1669                 case -NFS4ERR_STALE_CLIENTID:
1670                 case -NFS4ERR_BADSESSION:
1671                 case -NFS4ERR_BADSLOT:
1672                 case -NFS4ERR_BAD_HIGH_SLOT:
1673                 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1674                 case -ETIMEDOUT:
1675                         goto out_err;
1676                 }
1677                 nfs4_put_open_state(state);
1678                 spin_lock(&sp->so_lock);
1679                 goto restart;
1680         }
1681         raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1682         spin_unlock(&sp->so_lock);
1683         return 0;
1684 out_err:
1685         nfs4_put_open_state(state);
1686         spin_lock(&sp->so_lock);
1687         raw_write_seqcount_end(&sp->so_reclaim_seqcount);
1688         spin_unlock(&sp->so_lock);
1689         return status;
1690 }
1691 
1692 static void nfs4_clear_open_state(struct nfs4_state *state)
1693 {
1694         struct nfs4_lock_state *lock;
1695 
1696         clear_bit(NFS_DELEGATED_STATE, &state->flags);
1697         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1698         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1699         clear_bit(NFS_O_RDWR_STATE, &state->flags);
1700         spin_lock(&state->state_lock);
1701         list_for_each_entry(lock, &state->lock_states, ls_locks) {
1702                 lock->ls_seqid.flags = 0;
1703                 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
1704         }
1705         spin_unlock(&state->state_lock);
1706 }
1707 
1708 static void nfs4_reset_seqids(struct nfs_server *server,
1709         int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1710 {
1711         struct nfs_client *clp = server->nfs_client;
1712         struct nfs4_state_owner *sp;
1713         struct rb_node *pos;
1714         struct nfs4_state *state;
1715 
1716         spin_lock(&clp->cl_lock);
1717         for (pos = rb_first(&server->state_owners);
1718              pos != NULL;
1719              pos = rb_next(pos)) {
1720                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1721                 sp->so_seqid.flags = 0;
1722                 spin_lock(&sp->so_lock);
1723                 list_for_each_entry(state, &sp->so_states, open_states) {
1724                         if (mark_reclaim(clp, state))
1725                                 nfs4_clear_open_state(state);
1726                 }
1727                 spin_unlock(&sp->so_lock);
1728         }
1729         spin_unlock(&clp->cl_lock);
1730 }
1731 
1732 static void nfs4_state_mark_reclaim_helper(struct nfs_client *clp,
1733         int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
1734 {
1735         struct nfs_server *server;
1736 
1737         rcu_read_lock();
1738         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1739                 nfs4_reset_seqids(server, mark_reclaim);
1740         rcu_read_unlock();
1741 }
1742 
1743 static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
1744 {
1745         /* Mark all delegations for reclaim */
1746         nfs_delegation_mark_reclaim(clp);
1747         nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_reboot);
1748 }
1749 
1750 static int nfs4_reclaim_complete(struct nfs_client *clp,
1751                                  const struct nfs4_state_recovery_ops *ops,
1752                                  const struct cred *cred)
1753 {
1754         /* Notify the server we're done reclaiming our state */
1755         if (ops->reclaim_complete)
1756                 return ops->reclaim_complete(clp, cred);
1757         return 0;
1758 }
1759 
1760 static void nfs4_clear_reclaim_server(struct nfs_server *server)
1761 {
1762         struct nfs_client *clp = server->nfs_client;
1763         struct nfs4_state_owner *sp;
1764         struct rb_node *pos;
1765         struct nfs4_state *state;
1766 
1767         spin_lock(&clp->cl_lock);
1768         for (pos = rb_first(&server->state_owners);
1769              pos != NULL;
1770              pos = rb_next(pos)) {
1771                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
1772                 spin_lock(&sp->so_lock);
1773                 list_for_each_entry(state, &sp->so_states, open_states) {
1774                         if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
1775                                                 &state->flags))
1776                                 continue;
1777                         nfs4_state_mark_reclaim_nograce(clp, state);
1778                 }
1779                 spin_unlock(&sp->so_lock);
1780         }
1781         spin_unlock(&clp->cl_lock);
1782 }
1783 
1784 static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
1785 {
1786         struct nfs_server *server;
1787 
1788         if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
1789                 return 0;
1790 
1791         rcu_read_lock();
1792         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1793                 nfs4_clear_reclaim_server(server);
1794         rcu_read_unlock();
1795 
1796         nfs_delegation_reap_unclaimed(clp);
1797         return 1;
1798 }
1799 
1800 static void nfs4_state_end_reclaim_reboot(struct nfs_client *clp)
1801 {
1802         const struct nfs4_state_recovery_ops *ops;
1803         const struct cred *cred;
1804         int err;
1805 
1806         if (!nfs4_state_clear_reclaim_reboot(clp))
1807                 return;
1808         ops = clp->cl_mvops->reboot_recovery_ops;
1809         cred = nfs4_get_clid_cred(clp);
1810         err = nfs4_reclaim_complete(clp, ops, cred);
1811         put_cred(cred);
1812         if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
1813                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
1814 }
1815 
1816 static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
1817 {
1818         nfs_mark_test_expired_all_delegations(clp);
1819         nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
1820 }
1821 
1822 static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
1823 {
1824         switch (error) {
1825         case 0:
1826                 break;
1827         case -NFS4ERR_CB_PATH_DOWN:
1828                 nfs40_handle_cb_pathdown(clp);
1829                 break;
1830         case -NFS4ERR_NO_GRACE:
1831                 nfs4_state_end_reclaim_reboot(clp);
1832                 break;
1833         case -NFS4ERR_STALE_CLIENTID:
1834                 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1835                 nfs4_state_start_reclaim_reboot(clp);
1836                 break;
1837         case -NFS4ERR_EXPIRED:
1838                 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1839                 nfs4_state_start_reclaim_nograce(clp);
1840                 break;
1841         case -NFS4ERR_BADSESSION:
1842         case -NFS4ERR_BADSLOT:
1843         case -NFS4ERR_BAD_HIGH_SLOT:
1844         case -NFS4ERR_DEADSESSION:
1845         case -NFS4ERR_SEQ_FALSE_RETRY:
1846         case -NFS4ERR_SEQ_MISORDERED:
1847                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
1848                 /* Zero session reset errors */
1849                 break;
1850         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1851                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
1852                 break;
1853         default:
1854                 dprintk("%s: failed to handle error %d for server %s\n",
1855                                 __func__, error, clp->cl_hostname);
1856                 return error;
1857         }
1858         dprintk("%s: handled error %d for server %s\n", __func__, error,
1859                         clp->cl_hostname);
1860         return 0;
1861 }
1862 
1863 static int nfs4_do_reclaim(struct nfs_client *clp, const struct nfs4_state_recovery_ops *ops)
1864 {
1865         struct nfs4_state_owner *sp;
1866         struct nfs_server *server;
1867         struct rb_node *pos;
1868         LIST_HEAD(freeme);
1869         int status = 0;
1870 
1871 restart:
1872         rcu_read_lock();
1873         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1874                 nfs4_purge_state_owners(server, &freeme);
1875                 spin_lock(&clp->cl_lock);
1876                 for (pos = rb_first(&server->state_owners);
1877                      pos != NULL;
1878                      pos = rb_next(pos)) {
1879                         sp = rb_entry(pos,
1880                                 struct nfs4_state_owner, so_server_node);
1881                         if (!test_and_clear_bit(ops->owner_flag_bit,
1882                                                         &sp->so_flags))
1883                                 continue;
1884                         if (!atomic_inc_not_zero(&sp->so_count))
1885                                 continue;
1886                         spin_unlock(&clp->cl_lock);
1887                         rcu_read_unlock();
1888 
1889                         status = nfs4_reclaim_open_state(sp, ops);
1890                         if (status < 0) {
1891                                 set_bit(ops->owner_flag_bit, &sp->so_flags);
1892                                 nfs4_put_state_owner(sp);
1893                                 status = nfs4_recovery_handle_error(clp, status);
1894                                 return (status != 0) ? status : -EAGAIN;
1895                         }
1896 
1897                         nfs4_put_state_owner(sp);
1898                         goto restart;
1899                 }
1900                 spin_unlock(&clp->cl_lock);
1901         }
1902         rcu_read_unlock();
1903         nfs4_free_state_owners(&freeme);
1904         return 0;
1905 }
1906 
1907 static int nfs4_check_lease(struct nfs_client *clp)
1908 {
1909         const struct cred *cred;
1910         const struct nfs4_state_maintenance_ops *ops =
1911                 clp->cl_mvops->state_renewal_ops;
1912         int status;
1913 
1914         /* Is the client already known to have an expired lease? */
1915         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
1916                 return 0;
1917         cred = ops->get_state_renewal_cred(clp);
1918         if (cred == NULL) {
1919                 cred = nfs4_get_clid_cred(clp);
1920                 status = -ENOKEY;
1921                 if (cred == NULL)
1922                         goto out;
1923         }
1924         status = ops->renew_lease(clp, cred);
1925         put_cred(cred);
1926         if (status == -ETIMEDOUT) {
1927                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
1928                 return 0;
1929         }
1930 out:
1931         return nfs4_recovery_handle_error(clp, status);
1932 }
1933 
1934 /* Set NFS4CLNT_LEASE_EXPIRED and reclaim reboot state for all v4.0 errors
1935  * and for recoverable errors on EXCHANGE_ID for v4.1
1936  */
1937 static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
1938 {
1939         switch (status) {
1940         case -NFS4ERR_SEQ_MISORDERED:
1941                 if (test_and_set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state))
1942                         return -ESERVERFAULT;
1943                 /* Lease confirmation error: retry after purging the lease */
1944                 ssleep(1);
1945                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1946                 break;
1947         case -NFS4ERR_STALE_CLIENTID:
1948                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1949                 nfs4_state_start_reclaim_reboot(clp);
1950                 break;
1951         case -NFS4ERR_CLID_INUSE:
1952                 pr_err("NFS: Server %s reports our clientid is in use\n",
1953                         clp->cl_hostname);
1954                 nfs_mark_client_ready(clp, -EPERM);
1955                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
1956                 return -EPERM;
1957         case -EACCES:
1958         case -NFS4ERR_DELAY:
1959         case -EAGAIN:
1960                 ssleep(1);
1961                 break;
1962 
1963         case -NFS4ERR_MINOR_VERS_MISMATCH:
1964                 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
1965                         nfs_mark_client_ready(clp, -EPROTONOSUPPORT);
1966                 dprintk("%s: exit with error %d for server %s\n",
1967                                 __func__, -EPROTONOSUPPORT, clp->cl_hostname);
1968                 return -EPROTONOSUPPORT;
1969         case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
1970                                  * in nfs4_exchange_id */
1971         default:
1972                 dprintk("%s: exit with error %d for server %s\n", __func__,
1973                                 status, clp->cl_hostname);
1974                 return status;
1975         }
1976         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
1977         dprintk("%s: handled error %d for server %s\n", __func__, status,
1978                         clp->cl_hostname);
1979         return 0;
1980 }
1981 
1982 static int nfs4_establish_lease(struct nfs_client *clp)
1983 {
1984         const struct cred *cred;
1985         const struct nfs4_state_recovery_ops *ops =
1986                 clp->cl_mvops->reboot_recovery_ops;
1987         int status;
1988 
1989         status = nfs4_begin_drain_session(clp);
1990         if (status != 0)
1991                 return status;
1992         cred = nfs4_get_clid_cred(clp);
1993         if (cred == NULL)
1994                 return -ENOENT;
1995         status = ops->establish_clid(clp, cred);
1996         put_cred(cred);
1997         if (status != 0)
1998                 return status;
1999         pnfs_destroy_all_layouts(clp);
2000         return 0;
2001 }
2002 
2003 /*
2004  * Returns zero or a negative errno.  NFS4ERR values are converted
2005  * to local errno values.
2006  */
2007 static int nfs4_reclaim_lease(struct nfs_client *clp)
2008 {
2009         int status;
2010 
2011         status = nfs4_establish_lease(clp);
2012         if (status < 0)
2013                 return nfs4_handle_reclaim_lease_error(clp, status);
2014         if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
2015                 nfs4_state_start_reclaim_nograce(clp);
2016         if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
2017                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
2018         clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2019         clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2020         return 0;
2021 }
2022 
2023 static int nfs4_purge_lease(struct nfs_client *clp)
2024 {
2025         int status;
2026 
2027         status = nfs4_establish_lease(clp);
2028         if (status < 0)
2029                 return nfs4_handle_reclaim_lease_error(clp, status);
2030         clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2031         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
2032         nfs4_state_start_reclaim_nograce(clp);
2033         return 0;
2034 }
2035 
2036 /*
2037  * Try remote migration of one FSID from a source server to a
2038  * destination server.  The source server provides a list of
2039  * potential destinations.
2040  *
2041  * Returns zero or a negative NFS4ERR status code.
2042  */
2043 static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
2044 {
2045         struct nfs_client *clp = server->nfs_client;
2046         struct nfs4_fs_locations *locations = NULL;
2047         struct inode *inode;
2048         struct page *page;
2049         int status, result;
2050 
2051         dprintk("--> %s: FSID %llx:%llx on \"%s\"\n", __func__,
2052                         (unsigned long long)server->fsid.major,
2053                         (unsigned long long)server->fsid.minor,
2054                         clp->cl_hostname);
2055 
2056         result = 0;
2057         page = alloc_page(GFP_KERNEL);
2058         locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2059         if (page == NULL || locations == NULL) {
2060                 dprintk("<-- %s: no memory\n", __func__);
2061                 goto out;
2062         }
2063 
2064         inode = d_inode(server->super->s_root);
2065         result = nfs4_proc_get_locations(inode, locations, page, cred);
2066         if (result) {
2067                 dprintk("<-- %s: failed to retrieve fs_locations: %d\n",
2068                         __func__, result);
2069                 goto out;
2070         }
2071 
2072         result = -NFS4ERR_NXIO;
2073         if (!(locations->fattr.valid & NFS_ATTR_FATTR_V4_LOCATIONS)) {
2074                 dprintk("<-- %s: No fs_locations data, migration skipped\n",
2075                         __func__);
2076                 goto out;
2077         }
2078 
2079         status = nfs4_begin_drain_session(clp);
2080         if (status != 0) {
2081                 result = status;
2082                 goto out;
2083         }
2084 
2085         status = nfs4_replace_transport(server, locations);
2086         if (status != 0) {
2087                 dprintk("<-- %s: failed to replace transport: %d\n",
2088                         __func__, status);
2089                 goto out;
2090         }
2091 
2092         result = 0;
2093         dprintk("<-- %s: migration succeeded\n", __func__);
2094 
2095 out:
2096         if (page != NULL)
2097                 __free_page(page);
2098         kfree(locations);
2099         if (result) {
2100                 pr_err("NFS: migration recovery failed (server %s)\n",
2101                                 clp->cl_hostname);
2102                 set_bit(NFS_MIG_FAILED, &server->mig_status);
2103         }
2104         return result;
2105 }
2106 
2107 /*
2108  * Returns zero or a negative NFS4ERR status code.
2109  */
2110 static int nfs4_handle_migration(struct nfs_client *clp)
2111 {
2112         const struct nfs4_state_maintenance_ops *ops =
2113                                 clp->cl_mvops->state_renewal_ops;
2114         struct nfs_server *server;
2115         const struct cred *cred;
2116 
2117         dprintk("%s: migration reported on \"%s\"\n", __func__,
2118                         clp->cl_hostname);
2119 
2120         cred = ops->get_state_renewal_cred(clp);
2121         if (cred == NULL)
2122                 return -NFS4ERR_NOENT;
2123 
2124         clp->cl_mig_gen++;
2125 restart:
2126         rcu_read_lock();
2127         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2128                 int status;
2129 
2130                 if (server->mig_gen == clp->cl_mig_gen)
2131                         continue;
2132                 server->mig_gen = clp->cl_mig_gen;
2133 
2134                 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
2135                                                 &server->mig_status))
2136                         continue;
2137 
2138                 rcu_read_unlock();
2139                 status = nfs4_try_migration(server, cred);
2140                 if (status < 0) {
2141                         put_cred(cred);
2142                         return status;
2143                 }
2144                 goto restart;
2145         }
2146         rcu_read_unlock();
2147         put_cred(cred);
2148         return 0;
2149 }
2150 
2151 /*
2152  * Test each nfs_server on the clp's cl_superblocks list to see
2153  * if it's moved to another server.  Stop when the server no longer
2154  * returns NFS4ERR_LEASE_MOVED.
2155  */
2156 static int nfs4_handle_lease_moved(struct nfs_client *clp)
2157 {
2158         const struct nfs4_state_maintenance_ops *ops =
2159                                 clp->cl_mvops->state_renewal_ops;
2160         struct nfs_server *server;
2161         const struct cred *cred;
2162 
2163         dprintk("%s: lease moved reported on \"%s\"\n", __func__,
2164                         clp->cl_hostname);
2165 
2166         cred = ops->get_state_renewal_cred(clp);
2167         if (cred == NULL)
2168                 return -NFS4ERR_NOENT;
2169 
2170         clp->cl_mig_gen++;
2171 restart:
2172         rcu_read_lock();
2173         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
2174                 struct inode *inode;
2175                 int status;
2176 
2177                 if (server->mig_gen == clp->cl_mig_gen)
2178                         continue;
2179                 server->mig_gen = clp->cl_mig_gen;
2180 
2181                 rcu_read_unlock();
2182 
2183                 inode = d_inode(server->super->s_root);
2184                 status = nfs4_proc_fsid_present(inode, cred);
2185                 if (status != -NFS4ERR_MOVED)
2186                         goto restart;   /* wasn't this one */
2187                 if (nfs4_try_migration(server, cred) == -NFS4ERR_LEASE_MOVED)
2188                         goto restart;   /* there are more */
2189                 goto out;
2190         }
2191         rcu_read_unlock();
2192 
2193 out:
2194         put_cred(cred);
2195         return 0;
2196 }
2197 
2198 /**
2199  * nfs4_discover_server_trunking - Detect server IP address trunking
2200  *
2201  * @clp: nfs_client under test
2202  * @result: OUT: found nfs_client, or clp
2203  *
2204  * Returns zero or a negative errno.  If zero is returned,
2205  * an nfs_client pointer is planted in "result".
2206  *
2207  * Note: since we are invoked in process context, and
2208  * not from inside the state manager, we cannot use
2209  * nfs4_handle_reclaim_lease_error().
2210  */
2211 int nfs4_discover_server_trunking(struct nfs_client *clp,
2212                                   struct nfs_client **result)
2213 {
2214         const struct nfs4_state_recovery_ops *ops =
2215                                 clp->cl_mvops->reboot_recovery_ops;
2216         struct rpc_clnt *clnt;
2217         const struct cred *cred;
2218         int i, status;
2219 
2220         dprintk("NFS: %s: testing '%s'\n", __func__, clp->cl_hostname);
2221 
2222         clnt = clp->cl_rpcclient;
2223         i = 0;
2224 
2225         mutex_lock(&nfs_clid_init_mutex);
2226 again:
2227         status  = -ENOENT;
2228         cred = nfs4_get_clid_cred(clp);
2229         if (cred == NULL)
2230                 goto out_unlock;
2231 
2232         status = ops->detect_trunking(clp, result, cred);
2233         put_cred(cred);
2234         switch (status) {
2235         case 0:
2236         case -EINTR:
2237         case -ERESTARTSYS:
2238                 break;
2239         case -ETIMEDOUT:
2240                 if (clnt->cl_softrtry)
2241                         break;
2242                 /* Fall through */
2243         case -NFS4ERR_DELAY:
2244         case -EAGAIN:
2245                 ssleep(1);
2246                 /* Fall through */
2247         case -NFS4ERR_STALE_CLIENTID:
2248                 dprintk("NFS: %s after status %d, retrying\n",
2249                         __func__, status);
2250                 goto again;
2251         case -EACCES:
2252                 if (i++ == 0) {
2253                         nfs4_root_machine_cred(clp);
2254                         goto again;
2255                 }
2256                 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX)
2257                         break;
2258                 /* Fall through */
2259         case -NFS4ERR_CLID_INUSE:
2260         case -NFS4ERR_WRONGSEC:
2261                 /* No point in retrying if we already used RPC_AUTH_UNIX */
2262                 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
2263                         status = -EPERM;
2264                         break;
2265                 }
2266                 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
2267                 if (IS_ERR(clnt)) {
2268                         status = PTR_ERR(clnt);
2269                         break;
2270                 }
2271                 /* Note: this is safe because we haven't yet marked the
2272                  * client as ready, so we are the only user of
2273                  * clp->cl_rpcclient
2274                  */
2275                 clnt = xchg(&clp->cl_rpcclient, clnt);
2276                 rpc_shutdown_client(clnt);
2277                 clnt = clp->cl_rpcclient;
2278                 goto again;
2279 
2280         case -NFS4ERR_MINOR_VERS_MISMATCH:
2281                 status = -EPROTONOSUPPORT;
2282                 break;
2283 
2284         case -EKEYEXPIRED:
2285         case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
2286                                  * in nfs4_exchange_id */
2287                 status = -EKEYEXPIRED;
2288                 break;
2289         default:
2290                 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
2291                                 __func__, status);
2292                 status = -EIO;
2293         }
2294 
2295 out_unlock:
2296         mutex_unlock(&nfs_clid_init_mutex);
2297         dprintk("NFS: %s: status = %d\n", __func__, status);
2298         return status;
2299 }
2300 
2301 #ifdef CONFIG_NFS_V4_1
2302 void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
2303 {
2304         struct nfs_client *clp = session->clp;
2305 
2306         switch (err) {
2307         default:
2308                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2309                 break;
2310         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2311                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2312         }
2313         nfs4_schedule_state_manager(clp);
2314 }
2315 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
2316 
2317 void nfs41_notify_server(struct nfs_client *clp)
2318 {
2319         /* Use CHECK_LEASE to ping the server with a SEQUENCE */
2320         set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
2321         nfs4_schedule_state_manager(clp);
2322 }
2323 
2324 static void nfs4_reset_all_state(struct nfs_client *clp)
2325 {
2326         if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2327                 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
2328                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
2329                 nfs4_state_start_reclaim_nograce(clp);
2330                 dprintk("%s: scheduling reset of all state for server %s!\n",
2331                                 __func__, clp->cl_hostname);
2332                 nfs4_schedule_state_manager(clp);
2333         }
2334 }
2335 
2336 static void nfs41_handle_server_reboot(struct nfs_client *clp)
2337 {
2338         if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
2339                 nfs4_state_start_reclaim_reboot(clp);
2340                 dprintk("%s: server %s rebooted!\n", __func__,
2341                                 clp->cl_hostname);
2342                 nfs4_schedule_state_manager(clp);
2343         }
2344 }
2345 
2346 static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
2347 {
2348         nfs4_reset_all_state(clp);
2349         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2350 }
2351 
2352 static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
2353 {
2354         nfs4_state_start_reclaim_nograce(clp);
2355         nfs4_schedule_state_manager(clp);
2356 
2357         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
2358 }
2359 
2360 static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
2361 {
2362         /* FIXME: For now, we destroy all layouts. */
2363         pnfs_destroy_all_layouts(clp);
2364         nfs_test_expired_all_delegations(clp);
2365         dprintk("%s: Recallable state revoked on server %s!\n", __func__,
2366                         clp->cl_hostname);
2367 }
2368 
2369 static void nfs41_handle_backchannel_fault(struct nfs_client *clp)
2370 {
2371         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2372         nfs4_schedule_state_manager(clp);
2373 
2374         dprintk("%s: server %s declared a backchannel fault\n", __func__,
2375                         clp->cl_hostname);
2376 }
2377 
2378 static void nfs41_handle_cb_path_down(struct nfs_client *clp)
2379 {
2380         if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2381                 &clp->cl_state) == 0)
2382                 nfs4_schedule_state_manager(clp);
2383 }
2384 
2385 void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
2386                 bool recovery)
2387 {
2388         if (!flags)
2389                 return;
2390 
2391         dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
2392                 __func__, clp->cl_hostname, clp->cl_clientid, flags);
2393         /*
2394          * If we're called from the state manager thread, then assume we're
2395          * already handling the RECLAIM_NEEDED and/or STATE_REVOKED.
2396          * Those flags are expected to remain set until we're done
2397          * recovering (see RFC5661, section 18.46.3).
2398          */
2399         if (recovery)
2400                 goto out_recovery;
2401 
2402         if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
2403                 nfs41_handle_server_reboot(clp);
2404         if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
2405                 nfs41_handle_all_state_revoked(clp);
2406         if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
2407                             SEQ4_STATUS_ADMIN_STATE_REVOKED))
2408                 nfs41_handle_some_state_revoked(clp);
2409         if (flags & SEQ4_STATUS_LEASE_MOVED)
2410                 nfs4_schedule_lease_moved_recovery(clp);
2411         if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
2412                 nfs41_handle_recallable_state_revoked(clp);
2413 out_recovery:
2414         if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
2415                 nfs41_handle_backchannel_fault(clp);
2416         else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
2417                                 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
2418                 nfs41_handle_cb_path_down(clp);
2419 }
2420 
2421 static int nfs4_reset_session(struct nfs_client *clp)
2422 {
2423         const struct cred *cred;
2424         int status;
2425 
2426         if (!nfs4_has_session(clp))
2427                 return 0;
2428         status = nfs4_begin_drain_session(clp);
2429         if (status != 0)
2430                 return status;
2431         cred = nfs4_get_clid_cred(clp);
2432         status = nfs4_proc_destroy_session(clp->cl_session, cred);
2433         switch (status) {
2434         case 0:
2435         case -NFS4ERR_BADSESSION:
2436         case -NFS4ERR_DEADSESSION:
2437                 break;
2438         case -NFS4ERR_BACK_CHAN_BUSY:
2439         case -NFS4ERR_DELAY:
2440                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
2441                 status = 0;
2442                 ssleep(1);
2443                 goto out;
2444         default:
2445                 status = nfs4_recovery_handle_error(clp, status);
2446                 goto out;
2447         }
2448 
2449         memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
2450         status = nfs4_proc_create_session(clp, cred);
2451         if (status) {
2452                 dprintk("%s: session reset failed with status %d for server %s!\n",
2453                         __func__, status, clp->cl_hostname);
2454                 status = nfs4_handle_reclaim_lease_error(clp, status);
2455                 goto out;
2456         }
2457         nfs41_finish_session_reset(clp);
2458         dprintk("%s: session reset was successful for server %s!\n",
2459                         __func__, clp->cl_hostname);
2460 out:
2461         put_cred(cred);
2462         return status;
2463 }
2464 
2465 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2466 {
2467         const struct cred *cred;
2468         int ret;
2469 
2470         if (!nfs4_has_session(clp))
2471                 return 0;
2472         ret = nfs4_begin_drain_session(clp);
2473         if (ret != 0)
2474                 return ret;
2475         cred = nfs4_get_clid_cred(clp);
2476         ret = nfs4_proc_bind_conn_to_session(clp, cred);
2477         put_cred(cred);
2478         clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2479         switch (ret) {
2480         case 0:
2481                 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
2482                         __func__, clp->cl_hostname);
2483                 break;
2484         case -NFS4ERR_DELAY:
2485                 ssleep(1);
2486                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
2487                 break;
2488         default:
2489                 return nfs4_recovery_handle_error(clp, ret);
2490         }
2491         return 0;
2492 }
2493 #else /* CONFIG_NFS_V4_1 */
2494 static int nfs4_reset_session(struct nfs_client *clp) { return 0; }
2495 
2496 static int nfs4_bind_conn_to_session(struct nfs_client *clp)
2497 {
2498         return 0;
2499 }
2500 #endif /* CONFIG_NFS_V4_1 */
2501 
2502 static void nfs4_state_manager(struct nfs_client *clp)
2503 {
2504         int status = 0;
2505         const char *section = "", *section_sep = "";
2506 
2507         /* Ensure exclusive access to NFSv4 state */
2508         do {
2509                 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2510                 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
2511                         section = "purge state";
2512                         status = nfs4_purge_lease(clp);
2513                         if (status < 0)
2514                                 goto out_error;
2515                         continue;
2516                 }
2517 
2518                 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) {
2519                         section = "lease expired";
2520                         /* We're going to have to re-establish a clientid */
2521                         status = nfs4_reclaim_lease(clp);
2522                         if (status < 0)
2523                                 goto out_error;
2524                         continue;
2525                 }
2526 
2527                 /* Initialize or reset the session */
2528                 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
2529                         section = "reset session";
2530                         status = nfs4_reset_session(clp);
2531                         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
2532                                 continue;
2533                         if (status < 0)
2534                                 goto out_error;
2535                 }
2536 
2537                 /* Send BIND_CONN_TO_SESSION */
2538                 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
2539                                 &clp->cl_state)) {
2540                         section = "bind conn to session";
2541                         status = nfs4_bind_conn_to_session(clp);
2542                         if (status < 0)
2543                                 goto out_error;
2544                         continue;
2545                 }
2546 
2547                 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
2548                         section = "check lease";
2549                         status = nfs4_check_lease(clp);
2550                         if (status < 0)
2551                                 goto out_error;
2552                         continue;
2553                 }
2554 
2555                 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
2556                         section = "migration";
2557                         status = nfs4_handle_migration(clp);
2558                         if (status < 0)
2559                                 goto out_error;
2560                 }
2561 
2562                 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
2563                         section = "lease moved";
2564                         status = nfs4_handle_lease_moved(clp);
2565                         if (status < 0)
2566                                 goto out_error;
2567                 }
2568 
2569                 /* First recover reboot state... */
2570                 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
2571                         section = "reclaim reboot";
2572                         status = nfs4_do_reclaim(clp,
2573                                 clp->cl_mvops->reboot_recovery_ops);
2574                         if (status == -EAGAIN)
2575                                 continue;
2576                         if (status < 0)
2577                                 goto out_error;
2578                         nfs4_state_end_reclaim_reboot(clp);
2579                 }
2580 
2581                 /* Detect expired delegations... */
2582                 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
2583                         section = "detect expired delegations";
2584                         nfs_reap_expired_delegations(clp);
2585                         continue;
2586                 }
2587 
2588                 /* Now recover expired state... */
2589                 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
2590                         section = "reclaim nograce";
2591                         status = nfs4_do_reclaim(clp,
2592                                 clp->cl_mvops->nograce_recovery_ops);
2593                         if (status == -EAGAIN)
2594                                 continue;
2595                         if (status < 0)
2596                                 goto out_error;
2597                         clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
2598                 }
2599 
2600                 nfs4_end_drain_session(clp);
2601                 nfs4_clear_state_manager_bit(clp);
2602 
2603                 if (!test_and_set_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state)) {
2604                         if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
2605                                 nfs_client_return_marked_delegations(clp);
2606                                 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
2607                         }
2608                         clear_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state);
2609                 }
2610 
2611                 /* Did we race with an attempt to give us more work? */
2612                 if (!test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state))
2613                         return;
2614                 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
2615                         return;
2616         } while (refcount_read(&clp->cl_count) > 1 && !signalled());
2617         goto out_drain;
2618 
2619 out_error:
2620         if (strlen(section))
2621                 section_sep = ": ";
2622         pr_warn_ratelimited("NFS: state manager%s%s failed on NFSv4 server %s"
2623                         " with error %d\n", section_sep, section,
2624                         clp->cl_hostname, -status);
2625         ssleep(1);
2626 out_drain:
2627         nfs4_end_drain_session(clp);
2628         nfs4_clear_state_manager_bit(clp);
2629 }
2630 
2631 static int nfs4_run_state_manager(void *ptr)
2632 {
2633         struct nfs_client *clp = ptr;
2634 
2635         allow_signal(SIGKILL);
2636         nfs4_state_manager(clp);
2637         nfs_put_client(clp);
2638         module_put_and_exit(0);
2639         return 0;
2640 }
2641 
2642 /*
2643  * Local variables:
2644  *  c-basic-offset: 8
2645  * End:
2646  */

/* [<][>][^][v][top][bottom][index][help] */