LCOV - code coverage report
Current view: top level - fs/nfs - nfs4state.c (source / functions) Hit Total Coverage
Test: Real Lines: 0 967 0.0 %
Date: 2020-10-17 15:46:16 Functions: 0 97 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :          0 : static int nfs4_setup_state_renewal(struct nfs_client *clp)
      91                 :            : {
      92                 :            :         int status;
      93                 :            :         struct nfs_fsinfo fsinfo;
      94                 :            : 
      95                 :          0 :         if (!test_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state)) {
      96                 :          0 :                 nfs4_schedule_state_renewal(clp);
      97                 :          0 :                 return 0;
      98                 :            :         }
      99                 :            : 
     100                 :          0 :         status = nfs4_proc_get_lease_time(clp, &fsinfo);
     101                 :          0 :         if (status == 0) {
     102                 :          0 :                 nfs4_set_lease_period(clp, fsinfo.lease_time * HZ);
     103                 :          0 :                 nfs4_schedule_state_renewal(clp);
     104                 :            :         }
     105                 :            : 
     106                 :          0 :         return status;
     107                 :            : }
     108                 :            : 
     109                 :          0 : int nfs4_init_clientid(struct nfs_client *clp, const struct cred *cred)
     110                 :            : {
     111                 :          0 :         struct nfs4_setclientid_res clid = {
     112                 :          0 :                 .clientid = clp->cl_clientid,
     113                 :            :                 .confirm = clp->cl_confirm,
     114                 :            :         };
     115                 :            :         unsigned short port;
     116                 :            :         int status;
     117                 :          0 :         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
     118                 :            : 
     119                 :          0 :         if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
     120                 :            :                 goto do_confirm;
     121                 :          0 :         port = nn->nfs_callback_tcpport;
     122                 :          0 :         if (clp->cl_addr.ss_family == AF_INET6)
     123                 :          0 :                 port = nn->nfs_callback_tcpport6;
     124                 :            : 
     125                 :          0 :         status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
     126                 :          0 :         if (status != 0)
     127                 :            :                 goto out;
     128                 :          0 :         clp->cl_clientid = clid.clientid;
     129                 :          0 :         clp->cl_confirm = clid.confirm;
     130                 :          0 :         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
     131                 :            : do_confirm:
     132                 :          0 :         status = nfs4_proc_setclientid_confirm(clp, &clid, cred);
     133                 :          0 :         if (status != 0)
     134                 :            :                 goto out;
     135                 :          0 :         clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
     136                 :          0 :         nfs4_setup_state_renewal(clp);
     137                 :            : out:
     138                 :          0 :         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                 :          0 : int nfs40_discover_server_trunking(struct nfs_client *clp,
     155                 :            :                                    struct nfs_client **result,
     156                 :            :                                    const struct cred *cred)
     157                 :            : {
     158                 :          0 :         struct nfs4_setclientid_res clid = {
     159                 :          0 :                 .clientid = clp->cl_clientid,
     160                 :            :                 .confirm = clp->cl_confirm,
     161                 :            :         };
     162                 :          0 :         struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
     163                 :            :         unsigned short port;
     164                 :            :         int status;
     165                 :            : 
     166                 :          0 :         port = nn->nfs_callback_tcpport;
     167                 :          0 :         if (clp->cl_addr.ss_family == AF_INET6)
     168                 :          0 :                 port = nn->nfs_callback_tcpport6;
     169                 :            : 
     170                 :          0 :         status = nfs4_proc_setclientid(clp, NFS4_CALLBACK, port, cred, &clid);
     171                 :          0 :         if (status != 0)
     172                 :            :                 goto out;
     173                 :          0 :         clp->cl_clientid = clid.clientid;
     174                 :          0 :         clp->cl_confirm = clid.confirm;
     175                 :            : 
     176                 :          0 :         status = nfs40_walk_client_list(clp, result, cred);
     177                 :          0 :         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                 :          0 :                 nfs4_schedule_state_renewal(*result);
     181                 :            : 
     182                 :            :                 /* If the client state need to recover, do it. */
     183                 :          0 :                 if (clp->cl_state)
     184                 :          0 :                         nfs4_schedule_state_manager(clp);
     185                 :            :         }
     186                 :            : out:
     187                 :          0 :         return status;
     188                 :            : }
     189                 :            : 
     190                 :          0 : const struct cred *nfs4_get_machine_cred(struct nfs_client *clp)
     191                 :            : {
     192                 :          0 :         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                 :          0 :         clp->cl_principal = NULL;
     200                 :          0 :         clp->cl_rpcclient->cl_principal = NULL;
     201                 :            : }
     202                 :            : 
     203                 :            : static const struct cred *
     204                 :          0 : 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                 :          0 :         for (pos = rb_first(&server->state_owners);
     211                 :            :              pos != NULL;
     212                 :          0 :              pos = rb_next(pos)) {
     213                 :            :                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
     214                 :          0 :                 if (list_empty(&sp->so_states))
     215                 :          0 :                         continue;
     216                 :          0 :                 cred = get_cred(sp->so_cred);
     217                 :          0 :                 break;
     218                 :            :         }
     219                 :          0 :         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                 :          0 : 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                 :          0 :         if (cred != NULL)
     237                 :            :                 goto out;
     238                 :            : 
     239                 :            :         spin_lock(&clp->cl_lock);
     240                 :            :         rcu_read_lock();
     241                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
     242                 :          0 :                 cred = nfs4_get_renew_cred_server_locked(server);
     243                 :          0 :                 if (cred != NULL)
     244                 :            :                         break;
     245                 :            :         }
     246                 :            :         rcu_read_unlock();
     247                 :            :         spin_unlock(&clp->cl_lock);
     248                 :            : 
     249                 :            : out:
     250                 :          0 :         return cred;
     251                 :            : }
     252                 :            : 
     253                 :          0 : static void nfs4_end_drain_slot_table(struct nfs4_slot_table *tbl)
     254                 :            : {
     255                 :          0 :         if (test_and_clear_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state)) {
     256                 :            :                 spin_lock(&tbl->slot_tbl_lock);
     257                 :          0 :                 nfs41_wake_slot_table(tbl);
     258                 :            :                 spin_unlock(&tbl->slot_tbl_lock);
     259                 :            :         }
     260                 :          0 : }
     261                 :            : 
     262                 :          0 : static void nfs4_end_drain_session(struct nfs_client *clp)
     263                 :            : {
     264                 :          0 :         struct nfs4_session *ses = clp->cl_session;
     265                 :            : 
     266                 :          0 :         if (clp->cl_slot_tbl) {
     267                 :          0 :                 nfs4_end_drain_slot_table(clp->cl_slot_tbl);
     268                 :          0 :                 return;
     269                 :            :         }
     270                 :            : 
     271                 :          0 :         if (ses != NULL) {
     272                 :          0 :                 nfs4_end_drain_slot_table(&ses->bc_slot_table);
     273                 :          0 :                 nfs4_end_drain_slot_table(&ses->fc_slot_table);
     274                 :            :         }
     275                 :            : }
     276                 :            : 
     277                 :          0 : static int nfs4_drain_slot_tbl(struct nfs4_slot_table *tbl)
     278                 :            : {
     279                 :          0 :         set_bit(NFS4_SLOT_TBL_DRAINING, &tbl->slot_tbl_state);
     280                 :            :         spin_lock(&tbl->slot_tbl_lock);
     281                 :          0 :         if (tbl->highest_used_slotid != NFS4_NO_SLOT) {
     282                 :            :                 reinit_completion(&tbl->complete);
     283                 :            :                 spin_unlock(&tbl->slot_tbl_lock);
     284                 :          0 :                 return wait_for_completion_interruptible(&tbl->complete);
     285                 :            :         }
     286                 :            :         spin_unlock(&tbl->slot_tbl_lock);
     287                 :          0 :         return 0;
     288                 :            : }
     289                 :            : 
     290                 :          0 : static int nfs4_begin_drain_session(struct nfs_client *clp)
     291                 :            : {
     292                 :          0 :         struct nfs4_session *ses = clp->cl_session;
     293                 :            :         int ret;
     294                 :            : 
     295                 :          0 :         if (clp->cl_slot_tbl)
     296                 :          0 :                 return nfs4_drain_slot_tbl(clp->cl_slot_tbl);
     297                 :            : 
     298                 :            :         /* back channel */
     299                 :          0 :         ret = nfs4_drain_slot_tbl(&ses->bc_slot_table);
     300                 :          0 :         if (ret)
     301                 :            :                 return ret;
     302                 :            :         /* fore channel */
     303                 :          0 :         return nfs4_drain_slot_tbl(&ses->fc_slot_table);
     304                 :            : }
     305                 :            : 
     306                 :            : #if defined(CONFIG_NFS_V4_1)
     307                 :            : 
     308                 :          0 : static void nfs41_finish_session_reset(struct nfs_client *clp)
     309                 :            : {
     310                 :          0 :         clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
     311                 :          0 :         clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
     312                 :            :         /* create_session negotiated new slot table */
     313                 :          0 :         clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
     314                 :          0 :         nfs4_setup_state_renewal(clp);
     315                 :          0 : }
     316                 :            : 
     317                 :          0 : int nfs41_init_clientid(struct nfs_client *clp, const struct cred *cred)
     318                 :            : {
     319                 :            :         int status;
     320                 :            : 
     321                 :          0 :         if (test_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state))
     322                 :            :                 goto do_confirm;
     323                 :          0 :         status = nfs4_proc_exchange_id(clp, cred);
     324                 :          0 :         if (status != 0)
     325                 :            :                 goto out;
     326                 :          0 :         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
     327                 :            : do_confirm:
     328                 :          0 :         status = nfs4_proc_create_session(clp, cred);
     329                 :          0 :         if (status != 0)
     330                 :            :                 goto out;
     331                 :          0 :         nfs41_finish_session_reset(clp);
     332                 :          0 :         nfs_mark_client_ready(clp, NFS_CS_READY);
     333                 :            : out:
     334                 :          0 :         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                 :          0 : 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                 :          0 :         status = nfs4_proc_exchange_id(clp, cred);
     357                 :          0 :         if (status != NFS4_OK)
     358                 :            :                 return status;
     359                 :            : 
     360                 :          0 :         status = nfs41_walk_client_list(clp, result, cred);
     361                 :          0 :         if (status < 0)
     362                 :            :                 return status;
     363                 :          0 :         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                 :          0 :         if (clp->cl_exchange_flags & EXCHGID4_FLAG_CONFIRMED_R) {
     372                 :          0 :                 if (!test_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags))
     373                 :          0 :                         set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
     374                 :            :                 else
     375                 :          0 :                         set_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
     376                 :            :         }
     377                 :          0 :         nfs4_schedule_state_manager(clp);
     378                 :          0 :         status = nfs_wait_client_init_complete(clp);
     379                 :          0 :         if (status < 0)
     380                 :          0 :                 nfs_put_client(clp);
     381                 :          0 :         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                 :          0 : 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                 :          0 :         return cred;
     398                 :            : }
     399                 :            : 
     400                 :            : static struct nfs4_state_owner *
     401                 :          0 : nfs4_find_state_owner_locked(struct nfs_server *server, const struct cred *cred)
     402                 :            : {
     403                 :          0 :         struct rb_node **p = &server->state_owners.rb_node,
     404                 :            :                        *parent = NULL;
     405                 :            :         struct nfs4_state_owner *sp;
     406                 :            :         int cmp;
     407                 :            : 
     408                 :          0 :         while (*p != NULL) {
     409                 :            :                 parent = *p;
     410                 :          0 :                 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
     411                 :          0 :                 cmp = cred_fscmp(cred, sp->so_cred);
     412                 :            : 
     413                 :          0 :                 if (cmp < 0)
     414                 :          0 :                         p = &parent->rb_left;
     415                 :          0 :                 else if (cmp > 0)
     416                 :          0 :                         p = &parent->rb_right;
     417                 :            :                 else {
     418                 :          0 :                         if (!list_empty(&sp->so_lru))
     419                 :            :                                 list_del_init(&sp->so_lru);
     420                 :          0 :                         atomic_inc(&sp->so_count);
     421                 :          0 :                         return sp;
     422                 :            :                 }
     423                 :            :         }
     424                 :            :         return NULL;
     425                 :            : }
     426                 :            : 
     427                 :            : static struct nfs4_state_owner *
     428                 :          0 : nfs4_insert_state_owner_locked(struct nfs4_state_owner *new)
     429                 :            : {
     430                 :          0 :         struct nfs_server *server = new->so_server;
     431                 :          0 :         struct rb_node **p = &server->state_owners.rb_node,
     432                 :            :                        *parent = NULL;
     433                 :            :         struct nfs4_state_owner *sp;
     434                 :            :         int cmp;
     435                 :            : 
     436                 :          0 :         while (*p != NULL) {
     437                 :            :                 parent = *p;
     438                 :          0 :                 sp = rb_entry(parent, struct nfs4_state_owner, so_server_node);
     439                 :          0 :                 cmp = cred_fscmp(new->so_cred, sp->so_cred);
     440                 :            : 
     441                 :          0 :                 if (cmp < 0)
     442                 :          0 :                         p = &parent->rb_left;
     443                 :          0 :                 else if (cmp > 0)
     444                 :          0 :                         p = &parent->rb_right;
     445                 :            :                 else {
     446                 :          0 :                         if (!list_empty(&sp->so_lru))
     447                 :            :                                 list_del_init(&sp->so_lru);
     448                 :          0 :                         atomic_inc(&sp->so_count);
     449                 :          0 :                         return sp;
     450                 :            :                 }
     451                 :            :         }
     452                 :          0 :         rb_link_node(&new->so_server_node, parent, p);
     453                 :          0 :         rb_insert_color(&new->so_server_node, &server->state_owners);
     454                 :          0 :         return new;
     455                 :            : }
     456                 :            : 
     457                 :            : static void
     458                 :            : nfs4_remove_state_owner_locked(struct nfs4_state_owner *sp)
     459                 :            : {
     460                 :          0 :         struct nfs_server *server = sp->so_server;
     461                 :            : 
     462                 :          0 :         if (!RB_EMPTY_NODE(&sp->so_server_node))
     463                 :          0 :                 rb_erase(&sp->so_server_node, &server->state_owners);
     464                 :            : }
     465                 :            : 
     466                 :            : static void
     467                 :          0 : nfs4_init_seqid_counter(struct nfs_seqid_counter *sc)
     468                 :            : {
     469                 :          0 :         sc->create_time = ktime_get();
     470                 :          0 :         sc->flags = 0;
     471                 :          0 :         sc->counter = 0;
     472                 :          0 :         spin_lock_init(&sc->lock);
     473                 :          0 :         INIT_LIST_HEAD(&sc->list);
     474                 :          0 :         rpc_init_wait_queue(&sc->wait, "Seqid_waitqueue");
     475                 :          0 : }
     476                 :            : 
     477                 :            : static void
     478                 :            : nfs4_destroy_seqid_counter(struct nfs_seqid_counter *sc)
     479                 :            : {
     480                 :          0 :         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                 :          0 : 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                 :          0 :         sp = kzalloc(sizeof(*sp), gfp_flags);
     496                 :          0 :         if (!sp)
     497                 :            :                 return NULL;
     498                 :          0 :         sp->so_seqid.owner_id = ida_simple_get(&server->openowner_id, 0, 0,
     499                 :            :                                                 gfp_flags);
     500                 :          0 :         if (sp->so_seqid.owner_id < 0) {
     501                 :          0 :                 kfree(sp);
     502                 :          0 :                 return NULL;
     503                 :            :         }
     504                 :          0 :         sp->so_server = server;
     505                 :          0 :         sp->so_cred = get_cred(cred);
     506                 :          0 :         spin_lock_init(&sp->so_lock);
     507                 :          0 :         INIT_LIST_HEAD(&sp->so_states);
     508                 :          0 :         nfs4_init_seqid_counter(&sp->so_seqid);
     509                 :            :         atomic_set(&sp->so_count, 1);
     510                 :          0 :         INIT_LIST_HEAD(&sp->so_lru);
     511                 :            :         seqcount_init(&sp->so_reclaim_seqcount);
     512                 :          0 :         mutex_init(&sp->so_delegreturn_mutex);
     513                 :          0 :         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                 :          0 :         sp->so_seqid.create_time = ktime_get();
     529                 :            : }
     530                 :            : 
     531                 :          0 : static void nfs4_free_state_owner(struct nfs4_state_owner *sp)
     532                 :            : {
     533                 :            :         nfs4_destroy_seqid_counter(&sp->so_seqid);
     534                 :          0 :         put_cred(sp->so_cred);
     535                 :          0 :         ida_simple_remove(&sp->so_server->openowner_id, sp->so_seqid.owner_id);
     536                 :          0 :         kfree(sp);
     537                 :          0 : }
     538                 :            : 
     539                 :          0 : static void nfs4_gc_state_owners(struct nfs_server *server)
     540                 :            : {
     541                 :          0 :         struct nfs_client *clp = server->nfs_client;
     542                 :            :         struct nfs4_state_owner *sp, *tmp;
     543                 :            :         unsigned long time_min, time_max;
     544                 :          0 :         LIST_HEAD(doomed);
     545                 :            : 
     546                 :            :         spin_lock(&clp->cl_lock);
     547                 :          0 :         time_max = jiffies;
     548                 :          0 :         time_min = (long)time_max - (long)clp->cl_lease_time;
     549                 :          0 :         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                 :          0 :                 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                 :          0 :         list_for_each_entry_safe(sp, tmp, &doomed, so_lru) {
     559                 :            :                 list_del(&sp->so_lru);
     560                 :          0 :                 nfs4_free_state_owner(sp);
     561                 :            :         }
     562                 :          0 : }
     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                 :          0 : struct nfs4_state_owner *nfs4_get_state_owner(struct nfs_server *server,
     573                 :            :                                               const struct cred *cred,
     574                 :            :                                               gfp_t gfp_flags)
     575                 :            : {
     576                 :          0 :         struct nfs_client *clp = server->nfs_client;
     577                 :            :         struct nfs4_state_owner *sp, *new;
     578                 :            : 
     579                 :            :         spin_lock(&clp->cl_lock);
     580                 :          0 :         sp = nfs4_find_state_owner_locked(server, cred);
     581                 :            :         spin_unlock(&clp->cl_lock);
     582                 :          0 :         if (sp != NULL)
     583                 :            :                 goto out;
     584                 :          0 :         new = nfs4_alloc_state_owner(server, cred, gfp_flags);
     585                 :          0 :         if (new == NULL)
     586                 :            :                 goto out;
     587                 :            :         spin_lock(&clp->cl_lock);
     588                 :          0 :         sp = nfs4_insert_state_owner_locked(new);
     589                 :            :         spin_unlock(&clp->cl_lock);
     590                 :          0 :         if (sp != new)
     591                 :          0 :                 nfs4_free_state_owner(new);
     592                 :            : out:
     593                 :          0 :         nfs4_gc_state_owners(server);
     594                 :          0 :         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                 :          0 : void nfs4_put_state_owner(struct nfs4_state_owner *sp)
     610                 :            : {
     611                 :          0 :         struct nfs_server *server = sp->so_server;
     612                 :          0 :         struct nfs_client *clp = server->nfs_client;
     613                 :            : 
     614                 :          0 :         if (!atomic_dec_and_lock(&sp->so_count, &clp->cl_lock))
     615                 :          0 :                 return;
     616                 :            : 
     617                 :          0 :         sp->so_expires = jiffies;
     618                 :          0 :         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                 :          0 : void nfs4_purge_state_owners(struct nfs_server *server, struct list_head *head)
     634                 :            : {
     635                 :          0 :         struct nfs_client *clp = server->nfs_client;
     636                 :            :         struct nfs4_state_owner *sp, *tmp;
     637                 :            : 
     638                 :            :         spin_lock(&clp->cl_lock);
     639                 :          0 :         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                 :          0 : }
     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                 :          0 : void nfs4_free_state_owners(struct list_head *head)
     654                 :            : {
     655                 :            :         struct nfs4_state_owner *sp, *tmp;
     656                 :            : 
     657                 :          0 :         list_for_each_entry_safe(sp, tmp, head, so_lru) {
     658                 :            :                 list_del(&sp->so_lru);
     659                 :          0 :                 nfs4_free_state_owner(sp);
     660                 :            :         }
     661                 :          0 : }
     662                 :            : 
     663                 :            : static struct nfs4_state *
     664                 :          0 : nfs4_alloc_open_state(void)
     665                 :            : {
     666                 :            :         struct nfs4_state *state;
     667                 :            : 
     668                 :          0 :         state = kzalloc(sizeof(*state), GFP_NOFS);
     669                 :          0 :         if (!state)
     670                 :            :                 return NULL;
     671                 :            :         refcount_set(&state->count, 1);
     672                 :          0 :         INIT_LIST_HEAD(&state->lock_states);
     673                 :          0 :         spin_lock_init(&state->state_lock);
     674                 :          0 :         seqlock_init(&state->seqlock);
     675                 :          0 :         init_waitqueue_head(&state->waitq);
     676                 :          0 :         return state;
     677                 :            : }
     678                 :            : 
     679                 :            : void
     680                 :          0 : nfs4_state_set_mode_locked(struct nfs4_state *state, fmode_t fmode)
     681                 :            : {
     682                 :          0 :         if (state->state == fmode)
     683                 :          0 :                 return;
     684                 :            :         /* NB! List reordering - see the reclaim code for why.  */
     685                 :          0 :         if ((fmode & FMODE_WRITE) != (state->state & FMODE_WRITE)) {
     686                 :          0 :                 if (fmode & FMODE_WRITE)
     687                 :          0 :                         list_move(&state->open_states, &state->owner->so_states);
     688                 :            :                 else
     689                 :          0 :                         list_move_tail(&state->open_states, &state->owner->so_states);
     690                 :            :         }
     691                 :          0 :         state->state = fmode;
     692                 :            : }
     693                 :            : 
     694                 :            : static struct nfs4_state *
     695                 :          0 : __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                 :          0 :         list_for_each_entry_rcu(state, &nfsi->open_states, inode_states) {
     701                 :          0 :                 if (state->owner != owner)
     702                 :          0 :                         continue;
     703                 :          0 :                 if (!nfs4_valid_open_stateid(state))
     704                 :          0 :                         continue;
     705                 :          0 :                 if (refcount_inc_not_zero(&state->count))
     706                 :          0 :                         return state;
     707                 :            :         }
     708                 :            :         return NULL;
     709                 :            : }
     710                 :            : 
     711                 :            : static void
     712                 :            : nfs4_free_open_state(struct nfs4_state *state)
     713                 :            : {
     714                 :          0 :         kfree_rcu(state, rcu_head);
     715                 :            : }
     716                 :            : 
     717                 :            : struct nfs4_state *
     718                 :          0 : 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                 :          0 :         state = __nfs4_find_state_byowner(inode, owner);
     725                 :            :         rcu_read_unlock();
     726                 :          0 :         if (state)
     727                 :            :                 goto out;
     728                 :          0 :         new = nfs4_alloc_open_state();
     729                 :            :         spin_lock(&owner->so_lock);
     730                 :            :         spin_lock(&inode->i_lock);
     731                 :          0 :         state = __nfs4_find_state_byowner(inode, owner);
     732                 :          0 :         if (state == NULL && new != NULL) {
     733                 :            :                 state = new;
     734                 :          0 :                 state->owner = owner;
     735                 :          0 :                 atomic_inc(&owner->so_count);
     736                 :          0 :                 ihold(inode);
     737                 :          0 :                 state->inode = inode;
     738                 :          0 :                 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                 :          0 :                 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                 :          0 :                 if (new)
     748                 :            :                         nfs4_free_open_state(new);
     749                 :            :         }
     750                 :            : out:
     751                 :          0 :         return state;
     752                 :            : }
     753                 :            : 
     754                 :          0 : void nfs4_put_open_state(struct nfs4_state *state)
     755                 :            : {
     756                 :          0 :         struct inode *inode = state->inode;
     757                 :          0 :         struct nfs4_state_owner *owner = state->owner;
     758                 :            : 
     759                 :          0 :         if (!refcount_dec_and_lock(&state->count, &owner->so_lock))
     760                 :          0 :                 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                 :          0 :         iput(inode);
     767                 :            :         nfs4_free_open_state(state);
     768                 :          0 :         nfs4_put_state_owner(owner);
     769                 :            : }
     770                 :            : 
     771                 :            : /*
     772                 :            :  * Close the current file.
     773                 :            :  */
     774                 :          0 : static void __nfs4_close(struct nfs4_state *state,
     775                 :            :                 fmode_t fmode, gfp_t gfp_mask, int wait)
     776                 :            : {
     777                 :          0 :         struct nfs4_state_owner *owner = state->owner;
     778                 :            :         int call_close = 0;
     779                 :            :         fmode_t newstate;
     780                 :            : 
     781                 :          0 :         atomic_inc(&owner->so_count);
     782                 :            :         /* Protect against nfs4_find_state() */
     783                 :            :         spin_lock(&owner->so_lock);
     784                 :          0 :         switch (fmode & (FMODE_READ | FMODE_WRITE)) {
     785                 :            :                 case FMODE_READ:
     786                 :          0 :                         state->n_rdonly--;
     787                 :          0 :                         break;
     788                 :            :                 case FMODE_WRITE:
     789                 :          0 :                         state->n_wronly--;
     790                 :          0 :                         break;
     791                 :            :                 case FMODE_READ|FMODE_WRITE:
     792                 :          0 :                         state->n_rdwr--;
     793                 :            :         }
     794                 :            :         newstate = FMODE_READ|FMODE_WRITE;
     795                 :          0 :         if (state->n_rdwr == 0) {
     796                 :          0 :                 if (state->n_rdonly == 0) {
     797                 :            :                         newstate &= ~FMODE_READ;
     798                 :            :                         call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
     799                 :          0 :                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
     800                 :            :                 }
     801                 :          0 :                 if (state->n_wronly == 0) {
     802                 :          0 :                         newstate &= ~FMODE_WRITE;
     803                 :          0 :                         call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
     804                 :          0 :                         call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
     805                 :            :                 }
     806                 :          0 :                 if (newstate == 0)
     807                 :          0 :                         clear_bit(NFS_DELEGATED_STATE, &state->flags);
     808                 :            :         }
     809                 :          0 :         nfs4_state_set_mode_locked(state, newstate);
     810                 :            :         spin_unlock(&owner->so_lock);
     811                 :            : 
     812                 :          0 :         if (!call_close) {
     813                 :          0 :                 nfs4_put_open_state(state);
     814                 :          0 :                 nfs4_put_state_owner(owner);
     815                 :            :         } else
     816                 :          0 :                 nfs4_do_close(state, gfp_mask, wait);
     817                 :          0 : }
     818                 :            : 
     819                 :          0 : void nfs4_close_state(struct nfs4_state *state, fmode_t fmode)
     820                 :            : {
     821                 :          0 :         __nfs4_close(state, fmode, GFP_NOFS, 0);
     822                 :          0 : }
     823                 :            : 
     824                 :          0 : void nfs4_close_sync(struct nfs4_state *state, fmode_t fmode)
     825                 :            : {
     826                 :          0 :         __nfs4_close(state, fmode, GFP_KERNEL, 1);
     827                 :          0 : }
     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                 :          0 : __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                 :          0 :         list_for_each_entry(pos, &state->lock_states, ls_locks) {
     848                 :          0 :                 if (pos->ls_owner == fl_owner) {
     849                 :          0 :                         ret = pos;
     850                 :          0 :                         break;
     851                 :            :                 }
     852                 :          0 :                 if (pos->ls_owner == fl_owner2)
     853                 :            :                         ret = pos;
     854                 :            :         }
     855                 :          0 :         if (ret)
     856                 :          0 :                 refcount_inc(&ret->ls_count);
     857                 :          0 :         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                 :          0 : 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                 :          0 :         struct nfs_server *server = state->owner->so_server;
     869                 :            : 
     870                 :          0 :         lsp = kzalloc(sizeof(*lsp), GFP_NOFS);
     871                 :          0 :         if (lsp == NULL)
     872                 :            :                 return NULL;
     873                 :          0 :         nfs4_init_seqid_counter(&lsp->ls_seqid);
     874                 :            :         refcount_set(&lsp->ls_count, 1);
     875                 :          0 :         lsp->ls_state = state;
     876                 :          0 :         lsp->ls_owner = fl_owner;
     877                 :          0 :         lsp->ls_seqid.owner_id = ida_simple_get(&server->lockowner_id, 0, 0, GFP_NOFS);
     878                 :          0 :         if (lsp->ls_seqid.owner_id < 0)
     879                 :            :                 goto out_free;
     880                 :          0 :         INIT_LIST_HEAD(&lsp->ls_locks);
     881                 :          0 :         return lsp;
     882                 :            : out_free:
     883                 :          0 :         kfree(lsp);
     884                 :          0 :         return NULL;
     885                 :            : }
     886                 :            : 
     887                 :          0 : void nfs4_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
     888                 :            : {
     889                 :          0 :         ida_simple_remove(&server->lockowner_id, lsp->ls_seqid.owner_id);
     890                 :            :         nfs4_destroy_seqid_counter(&lsp->ls_seqid);
     891                 :          0 :         kfree(lsp);
     892                 :          0 : }
     893                 :            : 
     894                 :            : /*
     895                 :            :  * Return a compatible lock_state. If no initialized lock_state structure
     896                 :            :  * exists, return an uninitialized one.
     897                 :            :  *
     898                 :            :  */
     899                 :          0 : 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                 :          0 :                 lsp = __nfs4_find_lock_state(state, owner, NULL);
     906                 :          0 :                 if (lsp != NULL)
     907                 :            :                         break;
     908                 :          0 :                 if (new != NULL) {
     909                 :          0 :                         list_add(&new->ls_locks, &state->lock_states);
     910                 :          0 :                         set_bit(LK_STATE_IN_USE, &state->flags);
     911                 :          0 :                         lsp = new;
     912                 :            :                         new = NULL;
     913                 :          0 :                         break;
     914                 :            :                 }
     915                 :            :                 spin_unlock(&state->state_lock);
     916                 :          0 :                 new = nfs4_alloc_lock_state(state, owner);
     917                 :          0 :                 if (new == NULL)
     918                 :            :                         return NULL;
     919                 :            :         }
     920                 :            :         spin_unlock(&state->state_lock);
     921                 :          0 :         if (new != NULL)
     922                 :          0 :                 nfs4_free_lock_state(state->owner->so_server, new);
     923                 :          0 :         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                 :          0 : void nfs4_put_lock_state(struct nfs4_lock_state *lsp)
     931                 :            : {
     932                 :            :         struct nfs_server *server;
     933                 :            :         struct nfs4_state *state;
     934                 :            : 
     935                 :          0 :         if (lsp == NULL)
     936                 :            :                 return;
     937                 :          0 :         state = lsp->ls_state;
     938                 :          0 :         if (!refcount_dec_and_lock(&lsp->ls_count, &state->state_lock))
     939                 :            :                 return;
     940                 :            :         list_del(&lsp->ls_locks);
     941                 :          0 :         if (list_empty(&state->lock_states))
     942                 :          0 :                 clear_bit(LK_STATE_IN_USE, &state->flags);
     943                 :            :         spin_unlock(&state->state_lock);
     944                 :          0 :         server = state->owner->so_server;
     945                 :          0 :         if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
     946                 :          0 :                 struct nfs_client *clp = server->nfs_client;
     947                 :            : 
     948                 :          0 :                 clp->cl_mvops->free_lock_state(server, lsp);
     949                 :            :         } else
     950                 :          0 :                 nfs4_free_lock_state(server, lsp);
     951                 :            : }
     952                 :            : 
     953                 :          0 : static void nfs4_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
     954                 :            : {
     955                 :          0 :         struct nfs4_lock_state *lsp = src->fl_u.nfs4_fl.owner;
     956                 :            : 
     957                 :          0 :         dst->fl_u.nfs4_fl.owner = lsp;
     958                 :          0 :         refcount_inc(&lsp->ls_count);
     959                 :          0 : }
     960                 :            : 
     961                 :          0 : static void nfs4_fl_release_lock(struct file_lock *fl)
     962                 :            : {
     963                 :          0 :         nfs4_put_lock_state(fl->fl_u.nfs4_fl.owner);
     964                 :          0 : }
     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                 :          0 : int nfs4_set_lock_state(struct nfs4_state *state, struct file_lock *fl)
     972                 :            : {
     973                 :            :         struct nfs4_lock_state *lsp;
     974                 :            : 
     975                 :          0 :         if (fl->fl_ops != NULL)
     976                 :            :                 return 0;
     977                 :          0 :         lsp = nfs4_get_lock_state(state, fl->fl_owner);
     978                 :          0 :         if (lsp == NULL)
     979                 :            :                 return -ENOMEM;
     980                 :          0 :         fl->fl_u.nfs4_fl.owner = lsp;
     981                 :          0 :         fl->fl_ops = &nfs4_fl_lock_ops;
     982                 :          0 :         return 0;
     983                 :            : }
     984                 :            : 
     985                 :          0 : 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                 :          0 :         if (l_ctx == NULL)
     994                 :            :                 goto out;
     995                 :            : 
     996                 :          0 :         if (test_bit(LK_STATE_IN_USE, &state->flags) == 0)
     997                 :            :                 goto out;
     998                 :            : 
     999                 :          0 :         fl_owner = l_ctx->lockowner;
    1000                 :          0 :         fl_flock_owner = l_ctx->open_context->flock_owner;
    1001                 :            : 
    1002                 :            :         spin_lock(&state->state_lock);
    1003                 :          0 :         lsp = __nfs4_find_lock_state(state, fl_owner, fl_flock_owner);
    1004                 :          0 :         if (lsp && test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
    1005                 :            :                 ret = -EIO;
    1006                 :          0 :         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                 :          0 :         nfs4_put_lock_state(lsp);
    1012                 :            : out:
    1013                 :          0 :         return ret;
    1014                 :            : }
    1015                 :            : 
    1016                 :          0 : 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                 :          0 :                 if (test_bit(NFS_OPEN_STATE, &state->flags)) {
    1027                 :          0 :                         src = &state->open_stateid;
    1028                 :            :                         ret = true;
    1029                 :            :                 }
    1030                 :            :                 nfs4_stateid_copy(dst, src);
    1031                 :          0 :         } while (read_seqretry(&state->seqlock, seq));
    1032                 :          0 :         return ret;
    1033                 :            : }
    1034                 :            : 
    1035                 :            : /*
    1036                 :            :  * Byte-range lock aware utility to initialize the stateid of read/write
    1037                 :            :  * requests.
    1038                 :            :  */
    1039                 :          0 : 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                 :          0 :         if (!nfs4_valid_open_stateid(state))
    1046                 :            :                 return -EIO;
    1047                 :          0 :         if (cred != NULL)
    1048                 :          0 :                 *cred = NULL;
    1049                 :          0 :         ret = nfs4_copy_lock_stateid(dst, state, l_ctx);
    1050                 :          0 :         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                 :          0 :         if (nfs4_copy_delegation_stateid(state->inode, fmode, dst, cred)) {
    1055                 :            :                 ret = 0;
    1056                 :            :                 goto out;
    1057                 :            :         }
    1058                 :          0 :         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                 :          0 :         ret = nfs4_copy_open_stateid(dst, state) ? 0 : -EAGAIN;
    1065                 :            : out:
    1066                 :          0 :         if (nfs_server_capable(state->inode, NFS_CAP_STATEID_NFSV41))
    1067                 :          0 :                 dst->seqid = 0;
    1068                 :          0 :         return ret;
    1069                 :            : }
    1070                 :            : 
    1071                 :          0 : 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                 :          0 :         if (new == NULL)
    1077                 :            :                 return ERR_PTR(-ENOMEM);
    1078                 :          0 :         new->sequence = counter;
    1079                 :          0 :         INIT_LIST_HEAD(&new->list);
    1080                 :          0 :         new->task = NULL;
    1081                 :          0 :         return new;
    1082                 :            : }
    1083                 :            : 
    1084                 :          0 : void nfs_release_seqid(struct nfs_seqid *seqid)
    1085                 :            : {
    1086                 :            :         struct nfs_seqid_counter *sequence;
    1087                 :            : 
    1088                 :          0 :         if (seqid == NULL || list_empty(&seqid->list))
    1089                 :          0 :                 return;
    1090                 :          0 :         sequence = seqid->sequence;
    1091                 :            :         spin_lock(&sequence->lock);
    1092                 :            :         list_del_init(&seqid->list);
    1093                 :          0 :         if (!list_empty(&sequence->list)) {
    1094                 :            :                 struct nfs_seqid *next;
    1095                 :            : 
    1096                 :          0 :                 next = list_first_entry(&sequence->list,
    1097                 :            :                                 struct nfs_seqid, list);
    1098                 :          0 :                 rpc_wake_up_queued_task(&sequence->wait, next->task);
    1099                 :            :         }
    1100                 :            :         spin_unlock(&sequence->lock);
    1101                 :            : }
    1102                 :            : 
    1103                 :          0 : void nfs_free_seqid(struct nfs_seqid *seqid)
    1104                 :            : {
    1105                 :          0 :         nfs_release_seqid(seqid);
    1106                 :          0 :         kfree(seqid);
    1107                 :          0 : }
    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                 :          0 : static void nfs_increment_seqid(int status, struct nfs_seqid *seqid)
    1115                 :            : {
    1116                 :          0 :         switch (status) {
    1117                 :            :                 case 0:
    1118                 :            :                         break;
    1119                 :            :                 case -NFS4ERR_BAD_SEQID:
    1120                 :          0 :                         if (seqid->sequence->flags & NFS_SEQID_CONFIRMED)
    1121                 :            :                                 return;
    1122                 :          0 :                         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                 :          0 :         seqid->sequence->counter++;
    1141                 :            : }
    1142                 :            : 
    1143                 :          0 : void nfs_increment_open_seqid(int status, struct nfs_seqid *seqid)
    1144                 :            : {
    1145                 :            :         struct nfs4_state_owner *sp;
    1146                 :            : 
    1147                 :          0 :         if (seqid == NULL)
    1148                 :          0 :                 return;
    1149                 :            : 
    1150                 :          0 :         sp = container_of(seqid->sequence, struct nfs4_state_owner, so_seqid);
    1151                 :          0 :         if (status == -NFS4ERR_BAD_SEQID)
    1152                 :            :                 nfs4_reset_state_owner(sp);
    1153                 :          0 :         if (!nfs4_has_session(sp->so_server->nfs_client))
    1154                 :          0 :                 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                 :          0 : void nfs_increment_lock_seqid(int status, struct nfs_seqid *seqid)
    1163                 :            : {
    1164                 :          0 :         if (seqid != NULL)
    1165                 :          0 :                 nfs_increment_seqid(status, seqid);
    1166                 :          0 : }
    1167                 :            : 
    1168                 :          0 : 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                 :          0 :         if (seqid == NULL)
    1174                 :            :                 goto out;
    1175                 :          0 :         sequence = seqid->sequence;
    1176                 :            :         spin_lock(&sequence->lock);
    1177                 :          0 :         seqid->task = task;
    1178                 :          0 :         if (list_empty(&seqid->list))
    1179                 :          0 :                 list_add_tail(&seqid->list, &sequence->list);
    1180                 :          0 :         if (list_first_entry(&sequence->list, struct nfs_seqid, list) == seqid)
    1181                 :            :                 goto unlock;
    1182                 :          0 :         rpc_sleep_on(&sequence->wait, task, NULL);
    1183                 :            :         status = -EAGAIN;
    1184                 :            : unlock:
    1185                 :            :         spin_unlock(&sequence->lock);
    1186                 :            : out:
    1187                 :          0 :         return status;
    1188                 :            : }
    1189                 :            : 
    1190                 :            : static int nfs4_run_state_manager(void *);
    1191                 :            : 
    1192                 :          0 : static void nfs4_clear_state_manager_bit(struct nfs_client *clp)
    1193                 :            : {
    1194                 :          0 :         smp_mb__before_atomic();
    1195                 :          0 :         clear_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state);
    1196                 :          0 :         smp_mb__after_atomic();
    1197                 :          0 :         wake_up_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING);
    1198                 :          0 :         rpc_wake_up(&clp->cl_rpcwaitq);
    1199                 :          0 : }
    1200                 :            : 
    1201                 :            : /*
    1202                 :            :  * Schedule the nfs_client asynchronous state management routine
    1203                 :            :  */
    1204                 :          0 : 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                 :          0 :         set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
    1210                 :          0 :         if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
    1211                 :          0 :                 return;
    1212                 :          0 :         __module_get(THIS_MODULE);
    1213                 :          0 :         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                 :          0 :         snprintf(buf, sizeof(buf), "%s-manager",
    1220                 :            :                         rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR));
    1221                 :            :         rcu_read_unlock();
    1222                 :          0 :         task = kthread_run(nfs4_run_state_manager, clp, "%s", buf);
    1223                 :          0 :         if (IS_ERR(task)) {
    1224                 :          0 :                 printk(KERN_ERR "%s: kthread_run: %ld\n",
    1225                 :            :                         __func__, PTR_ERR(task));
    1226                 :          0 :                 nfs4_clear_state_manager_bit(clp);
    1227                 :          0 :                 nfs_put_client(clp);
    1228                 :          0 :                 module_put(THIS_MODULE);
    1229                 :            :         }
    1230                 :            : }
    1231                 :            : 
    1232                 :            : /*
    1233                 :            :  * Schedule a lease recovery attempt
    1234                 :            :  */
    1235                 :          0 : void nfs4_schedule_lease_recovery(struct nfs_client *clp)
    1236                 :            : {
    1237                 :          0 :         if (!clp)
    1238                 :          0 :                 return;
    1239                 :          0 :         if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
    1240                 :          0 :                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
    1241                 :            :         dprintk("%s: scheduling lease recovery for server %s\n", __func__,
    1242                 :            :                         clp->cl_hostname);
    1243                 :          0 :         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                 :          0 : int nfs4_schedule_migration_recovery(const struct nfs_server *server)
    1256                 :            : {
    1257                 :          0 :         struct nfs_client *clp = server->nfs_client;
    1258                 :            : 
    1259                 :          0 :         if (server->fh_expire_type != NFS4_FH_PERSISTENT) {
    1260                 :          0 :                 pr_err("NFS: volatile file handles not supported (server %s)\n",
    1261                 :            :                                 clp->cl_hostname);
    1262                 :          0 :                 return -NFS4ERR_IO;
    1263                 :            :         }
    1264                 :            : 
    1265                 :          0 :         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                 :          0 :         set_bit(NFS_MIG_IN_TRANSITION,
    1275                 :            :                         &((struct nfs_server *)server)->mig_status);
    1276                 :          0 :         set_bit(NFS4CLNT_MOVED, &clp->cl_state);
    1277                 :            : 
    1278                 :          0 :         nfs4_schedule_state_manager(clp);
    1279                 :          0 :         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                 :          0 : 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                 :          0 :         set_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state);
    1295                 :          0 :         nfs4_schedule_state_manager(clp);
    1296                 :          0 : }
    1297                 :            : EXPORT_SYMBOL_GPL(nfs4_schedule_lease_moved_recovery);
    1298                 :            : 
    1299                 :          0 : int nfs4_wait_clnt_recover(struct nfs_client *clp)
    1300                 :            : {
    1301                 :            :         int res;
    1302                 :            : 
    1303                 :          0 :         might_sleep();
    1304                 :            : 
    1305                 :          0 :         refcount_inc(&clp->cl_count);
    1306                 :          0 :         res = wait_on_bit_action(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
    1307                 :            :                                  nfs_wait_bit_killable, TASK_KILLABLE);
    1308                 :          0 :         if (res)
    1309                 :            :                 goto out;
    1310                 :          0 :         if (clp->cl_cons_state < 0)
    1311                 :            :                 res = clp->cl_cons_state;
    1312                 :            : out:
    1313                 :          0 :         nfs_put_client(clp);
    1314                 :          0 :         return res;
    1315                 :            : }
    1316                 :            : 
    1317                 :          0 : int nfs4_client_recover_expired_lease(struct nfs_client *clp)
    1318                 :            : {
    1319                 :            :         unsigned int loop;
    1320                 :            :         int ret;
    1321                 :            : 
    1322                 :          0 :         for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
    1323                 :          0 :                 ret = nfs4_wait_clnt_recover(clp);
    1324                 :          0 :                 if (ret != 0)
    1325                 :            :                         break;
    1326                 :          0 :                 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
    1327                 :            :                     !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
    1328                 :            :                         break;
    1329                 :          0 :                 nfs4_schedule_state_manager(clp);
    1330                 :            :                 ret = -EIO;
    1331                 :            :         }
    1332                 :          0 :         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                 :          0 :         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
    1346                 :          0 :         nfs_expire_all_delegations(clp);
    1347                 :            :         dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__,
    1348                 :            :                         clp->cl_hostname);
    1349                 :            : }
    1350                 :            : 
    1351                 :          0 : void nfs4_schedule_path_down_recovery(struct nfs_client *clp)
    1352                 :            : {
    1353                 :            :         nfs40_handle_cb_pathdown(clp);
    1354                 :          0 :         nfs4_schedule_state_manager(clp);
    1355                 :          0 : }
    1356                 :            : 
    1357                 :          0 : static int nfs4_state_mark_reclaim_reboot(struct nfs_client *clp, struct nfs4_state *state)
    1358                 :            : {
    1359                 :            : 
    1360                 :          0 :         if (!nfs4_valid_open_stateid(state))
    1361                 :            :                 return 0;
    1362                 :          0 :         set_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
    1363                 :            :         /* Don't recover state that expired before the reboot */
    1364                 :          0 :         if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags)) {
    1365                 :          0 :                 clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
    1366                 :          0 :                 return 0;
    1367                 :            :         }
    1368                 :          0 :         set_bit(NFS_OWNER_RECLAIM_REBOOT, &state->owner->so_flags);
    1369                 :          0 :         set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
    1370                 :          0 :         return 1;
    1371                 :            : }
    1372                 :            : 
    1373                 :          0 : int nfs4_state_mark_reclaim_nograce(struct nfs_client *clp, struct nfs4_state *state)
    1374                 :            : {
    1375                 :          0 :         if (!nfs4_valid_open_stateid(state))
    1376                 :            :                 return 0;
    1377                 :          0 :         set_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
    1378                 :          0 :         clear_bit(NFS_STATE_RECLAIM_REBOOT, &state->flags);
    1379                 :          0 :         set_bit(NFS_OWNER_RECLAIM_NOGRACE, &state->owner->so_flags);
    1380                 :          0 :         set_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
    1381                 :          0 :         return 1;
    1382                 :            : }
    1383                 :            : 
    1384                 :          0 : int nfs4_schedule_stateid_recovery(const struct nfs_server *server, struct nfs4_state *state)
    1385                 :            : {
    1386                 :          0 :         struct nfs_client *clp = server->nfs_client;
    1387                 :            : 
    1388                 :          0 :         if (!nfs4_state_mark_reclaim_nograce(clp, state))
    1389                 :            :                 return -EBADF;
    1390                 :          0 :         nfs_inode_find_delegation_state_and_recover(state->inode,
    1391                 :          0 :                         &state->stateid);
    1392                 :            :         dprintk("%s: scheduling stateid recovery for server %s\n", __func__,
    1393                 :            :                         clp->cl_hostname);
    1394                 :          0 :         nfs4_schedule_state_manager(clp);
    1395                 :          0 :         return 0;
    1396                 :            : }
    1397                 :            : EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery);
    1398                 :            : 
    1399                 :            : static struct nfs4_lock_state *
    1400                 :          0 : 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                 :          0 :         list_for_each_entry(pos, &state->lock_states, ls_locks) {
    1406                 :          0 :                 if (!test_bit(NFS_LOCK_INITIALIZED, &pos->ls_flags))
    1407                 :          0 :                         continue;
    1408                 :          0 :                 if (nfs4_stateid_match_other(&pos->ls_stateid, stateid))
    1409                 :          0 :                         return pos;
    1410                 :            :         }
    1411                 :            :         return NULL;
    1412                 :            : }
    1413                 :            : 
    1414                 :          0 : static bool nfs_state_lock_state_matches_stateid(struct nfs4_state *state,
    1415                 :            :                 const nfs4_stateid *stateid)
    1416                 :            : {
    1417                 :            :         bool found = false;
    1418                 :            : 
    1419                 :          0 :         if (test_bit(LK_STATE_IN_USE, &state->flags)) {
    1420                 :            :                 spin_lock(&state->state_lock);
    1421                 :          0 :                 if (nfs_state_find_lock_state_by_stateid(state, stateid))
    1422                 :            :                         found = true;
    1423                 :            :                 spin_unlock(&state->state_lock);
    1424                 :            :         }
    1425                 :          0 :         return found;
    1426                 :            : }
    1427                 :            : 
    1428                 :          0 : void nfs_inode_find_state_and_recover(struct inode *inode,
    1429                 :            :                 const nfs4_stateid *stateid)
    1430                 :            : {
    1431                 :          0 :         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                 :          0 :         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
    1439                 :          0 :                 state = ctx->state;
    1440                 :          0 :                 if (state == NULL)
    1441                 :          0 :                         continue;
    1442                 :          0 :                 if (nfs4_stateid_match_other(&state->stateid, stateid) &&
    1443                 :          0 :                     nfs4_state_mark_reclaim_nograce(clp, state)) {
    1444                 :            :                         found = true;
    1445                 :          0 :                         continue;
    1446                 :            :                 }
    1447                 :          0 :                 if (nfs4_stateid_match_other(&state->open_stateid, stateid) &&
    1448                 :          0 :                     nfs4_state_mark_reclaim_nograce(clp, state)) {
    1449                 :            :                         found = true;
    1450                 :          0 :                         continue;
    1451                 :            :                 }
    1452                 :          0 :                 if (nfs_state_lock_state_matches_stateid(state, stateid) &&
    1453                 :          0 :                     nfs4_state_mark_reclaim_nograce(clp, state))
    1454                 :            :                         found = true;
    1455                 :            :         }
    1456                 :            :         rcu_read_unlock();
    1457                 :            : 
    1458                 :          0 :         nfs_inode_find_delegation_state_and_recover(inode, stateid);
    1459                 :          0 :         if (found)
    1460                 :          0 :                 nfs4_schedule_state_manager(clp);
    1461                 :          0 : }
    1462                 :            : 
    1463                 :          0 : static void nfs4_state_mark_open_context_bad(struct nfs4_state *state, int err)
    1464                 :            : {
    1465                 :          0 :         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                 :          0 :         list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
    1471                 :          0 :                 if (ctx->state != state)
    1472                 :          0 :                         continue;
    1473                 :          0 :                 set_bit(NFS_CONTEXT_BAD, &ctx->flags);
    1474                 :          0 :                 pr_warn("NFSv4: state recovery failed for open file %pd2, "
    1475                 :            :                                 "error = %d\n", ctx->dentry, err);
    1476                 :            :         }
    1477                 :            :         rcu_read_unlock();
    1478                 :          0 : }
    1479                 :            : 
    1480                 :            : static void nfs4_state_mark_recovery_failed(struct nfs4_state *state, int error)
    1481                 :            : {
    1482                 :          0 :         set_bit(NFS_STATE_RECOVERY_FAILED, &state->flags);
    1483                 :          0 :         nfs4_state_mark_open_context_bad(state, error);
    1484                 :            : }
    1485                 :            : 
    1486                 :            : 
    1487                 :          0 : static int nfs4_reclaim_locks(struct nfs4_state *state, const struct nfs4_state_recovery_ops *ops)
    1488                 :            : {
    1489                 :          0 :         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                 :          0 :         struct file_lock_context *flctx = inode->i_flctx;
    1495                 :            :         struct list_head *list;
    1496                 :            : 
    1497                 :          0 :         if (flctx == NULL)
    1498                 :            :                 return 0;
    1499                 :            : 
    1500                 :          0 :         list = &flctx->flc_posix;
    1501                 :            : 
    1502                 :            :         /* Guard against delegation returns and new lock/unlock calls */
    1503                 :          0 :         down_write(&nfsi->rwsem);
    1504                 :            :         spin_lock(&flctx->flc_lock);
    1505                 :            : restart:
    1506                 :          0 :         list_for_each_entry(fl, list, fl_list) {
    1507                 :          0 :                 if (nfs_file_open_context(fl->fl_file)->state != state)
    1508                 :          0 :                         continue;
    1509                 :            :                 spin_unlock(&flctx->flc_lock);
    1510                 :          0 :                 status = ops->recover_lock(state, fl);
    1511                 :          0 :                 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                 :          0 :                         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                 :          0 :                         lsp = fl->fl_u.nfs4_fl.owner;
    1536                 :          0 :                         if (lsp)
    1537                 :          0 :                                 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
    1538                 :            :                         status = 0;
    1539                 :            :                 }
    1540                 :            :                 spin_lock(&flctx->flc_lock);
    1541                 :            :         }
    1542                 :          0 :         if (list == &flctx->flc_posix) {
    1543                 :          0 :                 list = &flctx->flc_flock;
    1544                 :          0 :                 goto restart;
    1545                 :            :         }
    1546                 :            :         spin_unlock(&flctx->flc_lock);
    1547                 :            : out:
    1548                 :          0 :         up_write(&nfsi->rwsem);
    1549                 :          0 :         return status;
    1550                 :            : }
    1551                 :            : 
    1552                 :            : #ifdef CONFIG_NFS_V4_2
    1553                 :          0 : static void nfs42_complete_copies(struct nfs4_state_owner *sp, struct nfs4_state *state)
    1554                 :            : {
    1555                 :            :         struct nfs4_copy_state *copy;
    1556                 :            : 
    1557                 :          0 :         if (!test_bit(NFS_CLNT_DST_SSC_COPY_STATE, &state->flags))
    1558                 :          0 :                 return;
    1559                 :            : 
    1560                 :          0 :         spin_lock(&sp->so_server->nfs_client->cl_lock);
    1561                 :          0 :         list_for_each_entry(copy, &sp->so_server->ss_copies, copies) {
    1562                 :          0 :                 if (!nfs4_stateid_match_other(&state->stateid, &copy->parent_state->stateid))
    1563                 :          0 :                         continue;
    1564                 :          0 :                 copy->flags = 1;
    1565                 :          0 :                 complete(&copy->completion);
    1566                 :          0 :                 break;
    1567                 :            :         }
    1568                 :          0 :         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                 :          0 : 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                 :          0 :         status = ops->recover_open(sp, state);
    1584                 :          0 :         if (status < 0)
    1585                 :            :                 return status;
    1586                 :            : 
    1587                 :          0 :         status = nfs4_reclaim_locks(state, ops);
    1588                 :          0 :         if (status < 0)
    1589                 :            :                 return status;
    1590                 :            : 
    1591                 :          0 :         if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) {
    1592                 :            :                 spin_lock(&state->state_lock);
    1593                 :          0 :                 list_for_each_entry(lock, &state->lock_states, ls_locks) {
    1594                 :          0 :                         if (!test_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags))
    1595                 :          0 :                                 pr_warn_ratelimited("NFS: %s: Lock reclaim failed!\n", __func__);
    1596                 :            :                 }
    1597                 :            :                 spin_unlock(&state->state_lock);
    1598                 :            :         }
    1599                 :            : 
    1600                 :          0 :         nfs42_complete_copies(sp, state);
    1601                 :          0 :         clear_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags);
    1602                 :          0 :         return status;
    1603                 :            : }
    1604                 :            : 
    1605                 :          0 : 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                 :          0 :         list_for_each_entry(state, &sp->so_states, open_states) {
    1623                 :          0 :                 if (!test_and_clear_bit(ops->state_flag_bit, &state->flags))
    1624                 :          0 :                         continue;
    1625                 :          0 :                 if (!nfs4_valid_open_stateid(state))
    1626                 :          0 :                         continue;
    1627                 :          0 :                 if (state->state == 0)
    1628                 :          0 :                         continue;
    1629                 :          0 :                 refcount_inc(&state->count);
    1630                 :            :                 spin_unlock(&sp->so_lock);
    1631                 :          0 :                 status = __nfs4_reclaim_open_state(sp, state, ops);
    1632                 :            : 
    1633                 :          0 :                 switch (status) {
    1634                 :            :                 default:
    1635                 :          0 :                         if (status >= 0) {
    1636                 :            :                                 loop = 0;
    1637                 :            :                                 break;
    1638                 :            :                         }
    1639                 :          0 :                         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                 :          0 :                         if (loop++ < 10) {
    1653                 :          0 :                                 set_bit(ops->state_flag_bit, &state->flags);
    1654                 :          0 :                                 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                 :          0 :                         nfs4_state_mark_reclaim_nograce(sp->so_server->nfs_client, state);
    1664                 :          0 :                         break;
    1665                 :            :                 case -NFS4ERR_EXPIRED:
    1666                 :            :                 case -NFS4ERR_NO_GRACE:
    1667                 :          0 :                         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                 :          0 :                 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                 :          0 :         return 0;
    1684                 :            : out_err:
    1685                 :          0 :         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                 :          0 :         return status;
    1690                 :            : }
    1691                 :            : 
    1692                 :          0 : static void nfs4_clear_open_state(struct nfs4_state *state)
    1693                 :            : {
    1694                 :            :         struct nfs4_lock_state *lock;
    1695                 :            : 
    1696                 :          0 :         clear_bit(NFS_DELEGATED_STATE, &state->flags);
    1697                 :          0 :         clear_bit(NFS_O_RDONLY_STATE, &state->flags);
    1698                 :          0 :         clear_bit(NFS_O_WRONLY_STATE, &state->flags);
    1699                 :          0 :         clear_bit(NFS_O_RDWR_STATE, &state->flags);
    1700                 :            :         spin_lock(&state->state_lock);
    1701                 :          0 :         list_for_each_entry(lock, &state->lock_states, ls_locks) {
    1702                 :          0 :                 lock->ls_seqid.flags = 0;
    1703                 :          0 :                 clear_bit(NFS_LOCK_INITIALIZED, &lock->ls_flags);
    1704                 :            :         }
    1705                 :            :         spin_unlock(&state->state_lock);
    1706                 :          0 : }
    1707                 :            : 
    1708                 :          0 : static void nfs4_reset_seqids(struct nfs_server *server,
    1709                 :            :         int (*mark_reclaim)(struct nfs_client *clp, struct nfs4_state *state))
    1710                 :            : {
    1711                 :          0 :         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                 :          0 :         for (pos = rb_first(&server->state_owners);
    1718                 :            :              pos != NULL;
    1719                 :          0 :              pos = rb_next(pos)) {
    1720                 :            :                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
    1721                 :          0 :                 sp->so_seqid.flags = 0;
    1722                 :            :                 spin_lock(&sp->so_lock);
    1723                 :          0 :                 list_for_each_entry(state, &sp->so_states, open_states) {
    1724                 :          0 :                         if (mark_reclaim(clp, state))
    1725                 :          0 :                                 nfs4_clear_open_state(state);
    1726                 :            :                 }
    1727                 :            :                 spin_unlock(&sp->so_lock);
    1728                 :            :         }
    1729                 :            :         spin_unlock(&clp->cl_lock);
    1730                 :          0 : }
    1731                 :            : 
    1732                 :          0 : 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                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
    1739                 :          0 :                 nfs4_reset_seqids(server, mark_reclaim);
    1740                 :            :         rcu_read_unlock();
    1741                 :          0 : }
    1742                 :            : 
    1743                 :            : static void nfs4_state_start_reclaim_reboot(struct nfs_client *clp)
    1744                 :            : {
    1745                 :            :         /* Mark all delegations for reclaim */
    1746                 :          0 :         nfs_delegation_mark_reclaim(clp);
    1747                 :          0 :         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                 :          0 :         if (ops->reclaim_complete)
    1756                 :          0 :                 return ops->reclaim_complete(clp, cred);
    1757                 :            :         return 0;
    1758                 :            : }
    1759                 :            : 
    1760                 :          0 : static void nfs4_clear_reclaim_server(struct nfs_server *server)
    1761                 :            : {
    1762                 :          0 :         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                 :          0 :         for (pos = rb_first(&server->state_owners);
    1769                 :            :              pos != NULL;
    1770                 :          0 :              pos = rb_next(pos)) {
    1771                 :            :                 sp = rb_entry(pos, struct nfs4_state_owner, so_server_node);
    1772                 :            :                 spin_lock(&sp->so_lock);
    1773                 :          0 :                 list_for_each_entry(state, &sp->so_states, open_states) {
    1774                 :          0 :                         if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT,
    1775                 :            :                                                 &state->flags))
    1776                 :          0 :                                 continue;
    1777                 :          0 :                         nfs4_state_mark_reclaim_nograce(clp, state);
    1778                 :            :                 }
    1779                 :            :                 spin_unlock(&sp->so_lock);
    1780                 :            :         }
    1781                 :            :         spin_unlock(&clp->cl_lock);
    1782                 :          0 : }
    1783                 :            : 
    1784                 :          0 : static int nfs4_state_clear_reclaim_reboot(struct nfs_client *clp)
    1785                 :            : {
    1786                 :            :         struct nfs_server *server;
    1787                 :            : 
    1788                 :          0 :         if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state))
    1789                 :            :                 return 0;
    1790                 :            : 
    1791                 :            :         rcu_read_lock();
    1792                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
    1793                 :          0 :                 nfs4_clear_reclaim_server(server);
    1794                 :            :         rcu_read_unlock();
    1795                 :            : 
    1796                 :          0 :         nfs_delegation_reap_unclaimed(clp);
    1797                 :          0 :         return 1;
    1798                 :            : }
    1799                 :            : 
    1800                 :          0 : 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                 :          0 :         if (!nfs4_state_clear_reclaim_reboot(clp))
    1807                 :          0 :                 return;
    1808                 :          0 :         ops = clp->cl_mvops->reboot_recovery_ops;
    1809                 :            :         cred = nfs4_get_clid_cred(clp);
    1810                 :            :         err = nfs4_reclaim_complete(clp, ops, cred);
    1811                 :          0 :         put_cred(cred);
    1812                 :          0 :         if (err == -NFS4ERR_CONN_NOT_BOUND_TO_SESSION)
    1813                 :          0 :                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
    1814                 :            : }
    1815                 :            : 
    1816                 :            : static void nfs4_state_start_reclaim_nograce(struct nfs_client *clp)
    1817                 :            : {
    1818                 :          0 :         nfs_mark_test_expired_all_delegations(clp);
    1819                 :          0 :         nfs4_state_mark_reclaim_helper(clp, nfs4_state_mark_reclaim_nograce);
    1820                 :            : }
    1821                 :            : 
    1822                 :          0 : static int nfs4_recovery_handle_error(struct nfs_client *clp, int error)
    1823                 :            : {
    1824                 :          0 :         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                 :          0 :                 nfs4_state_end_reclaim_reboot(clp);
    1832                 :          0 :                 break;
    1833                 :            :         case -NFS4ERR_STALE_CLIENTID:
    1834                 :          0 :                 set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
    1835                 :            :                 nfs4_state_start_reclaim_reboot(clp);
    1836                 :            :                 break;
    1837                 :            :         case -NFS4ERR_EXPIRED:
    1838                 :          0 :                 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                 :          0 :                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
    1848                 :            :                 /* Zero session reset errors */
    1849                 :          0 :                 break;
    1850                 :            :         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    1851                 :          0 :                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
    1852                 :          0 :                 break;
    1853                 :            :         default:
    1854                 :            :                 dprintk("%s: failed to handle error %d for server %s\n",
    1855                 :            :                                 __func__, error, clp->cl_hostname);
    1856                 :          0 :                 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                 :          0 : 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                 :          0 :         LIST_HEAD(freeme);
    1869                 :            :         int status = 0;
    1870                 :            : 
    1871                 :            : restart:
    1872                 :            :         rcu_read_lock();
    1873                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
    1874                 :          0 :                 nfs4_purge_state_owners(server, &freeme);
    1875                 :            :                 spin_lock(&clp->cl_lock);
    1876                 :          0 :                 for (pos = rb_first(&server->state_owners);
    1877                 :            :                      pos != NULL;
    1878                 :          0 :                      pos = rb_next(pos)) {
    1879                 :          0 :                         sp = rb_entry(pos,
    1880                 :            :                                 struct nfs4_state_owner, so_server_node);
    1881                 :          0 :                         if (!test_and_clear_bit(ops->owner_flag_bit,
    1882                 :            :                                                         &sp->so_flags))
    1883                 :          0 :                                 continue;
    1884                 :          0 :                         if (!atomic_inc_not_zero(&sp->so_count))
    1885                 :          0 :                                 continue;
    1886                 :            :                         spin_unlock(&clp->cl_lock);
    1887                 :            :                         rcu_read_unlock();
    1888                 :            : 
    1889                 :          0 :                         status = nfs4_reclaim_open_state(sp, ops);
    1890                 :          0 :                         if (status < 0) {
    1891                 :          0 :                                 set_bit(ops->owner_flag_bit, &sp->so_flags);
    1892                 :          0 :                                 nfs4_put_state_owner(sp);
    1893                 :          0 :                                 status = nfs4_recovery_handle_error(clp, status);
    1894                 :          0 :                                 return (status != 0) ? status : -EAGAIN;
    1895                 :            :                         }
    1896                 :            : 
    1897                 :          0 :                         nfs4_put_state_owner(sp);
    1898                 :          0 :                         goto restart;
    1899                 :            :                 }
    1900                 :            :                 spin_unlock(&clp->cl_lock);
    1901                 :            :         }
    1902                 :            :         rcu_read_unlock();
    1903                 :          0 :         nfs4_free_state_owners(&freeme);
    1904                 :          0 :         return 0;
    1905                 :            : }
    1906                 :            : 
    1907                 :          0 : static int nfs4_check_lease(struct nfs_client *clp)
    1908                 :            : {
    1909                 :            :         const struct cred *cred;
    1910                 :          0 :         const struct nfs4_state_maintenance_ops *ops =
    1911                 :          0 :                 clp->cl_mvops->state_renewal_ops;
    1912                 :            :         int status;
    1913                 :            : 
    1914                 :            :         /* Is the client already known to have an expired lease? */
    1915                 :          0 :         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
    1916                 :            :                 return 0;
    1917                 :          0 :         cred = ops->get_state_renewal_cred(clp);
    1918                 :          0 :         if (cred == NULL) {
    1919                 :            :                 cred = nfs4_get_clid_cred(clp);
    1920                 :            :                 status = -ENOKEY;
    1921                 :          0 :                 if (cred == NULL)
    1922                 :            :                         goto out;
    1923                 :            :         }
    1924                 :          0 :         status = ops->renew_lease(clp, cred);
    1925                 :          0 :         put_cred(cred);
    1926                 :          0 :         if (status == -ETIMEDOUT) {
    1927                 :          0 :                 set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
    1928                 :          0 :                 return 0;
    1929                 :            :         }
    1930                 :            : out:
    1931                 :          0 :         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                 :          0 : static int nfs4_handle_reclaim_lease_error(struct nfs_client *clp, int status)
    1938                 :            : {
    1939                 :          0 :         switch (status) {
    1940                 :            :         case -NFS4ERR_SEQ_MISORDERED:
    1941                 :          0 :                 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                 :          0 :                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
    1946                 :          0 :                 break;
    1947                 :            :         case -NFS4ERR_STALE_CLIENTID:
    1948                 :          0 :                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
    1949                 :            :                 nfs4_state_start_reclaim_reboot(clp);
    1950                 :            :                 break;
    1951                 :            :         case -NFS4ERR_CLID_INUSE:
    1952                 :          0 :                 pr_err("NFS: Server %s reports our clientid is in use\n",
    1953                 :            :                         clp->cl_hostname);
    1954                 :          0 :                 nfs_mark_client_ready(clp, -EPERM);
    1955                 :          0 :                 clear_bit(NFS4CLNT_LEASE_CONFIRM, &clp->cl_state);
    1956                 :          0 :                 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                 :          0 :                 if (clp->cl_cons_state == NFS_CS_SESSION_INITING)
    1965                 :          0 :                         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                 :          0 :         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                 :          0 :         return 0;
    1980                 :            : }
    1981                 :            : 
    1982                 :          0 : static int nfs4_establish_lease(struct nfs_client *clp)
    1983                 :            : {
    1984                 :            :         const struct cred *cred;
    1985                 :          0 :         const struct nfs4_state_recovery_ops *ops =
    1986                 :          0 :                 clp->cl_mvops->reboot_recovery_ops;
    1987                 :            :         int status;
    1988                 :            : 
    1989                 :          0 :         status = nfs4_begin_drain_session(clp);
    1990                 :          0 :         if (status != 0)
    1991                 :            :                 return status;
    1992                 :            :         cred = nfs4_get_clid_cred(clp);
    1993                 :          0 :         if (cred == NULL)
    1994                 :            :                 return -ENOENT;
    1995                 :          0 :         status = ops->establish_clid(clp, cred);
    1996                 :          0 :         put_cred(cred);
    1997                 :          0 :         if (status != 0)
    1998                 :            :                 return status;
    1999                 :          0 :         pnfs_destroy_all_layouts(clp);
    2000                 :          0 :         return 0;
    2001                 :            : }
    2002                 :            : 
    2003                 :            : /*
    2004                 :            :  * Returns zero or a negative errno.  NFS4ERR values are converted
    2005                 :            :  * to local errno values.
    2006                 :            :  */
    2007                 :          0 : static int nfs4_reclaim_lease(struct nfs_client *clp)
    2008                 :            : {
    2009                 :            :         int status;
    2010                 :            : 
    2011                 :          0 :         status = nfs4_establish_lease(clp);
    2012                 :          0 :         if (status < 0)
    2013                 :          0 :                 return nfs4_handle_reclaim_lease_error(clp, status);
    2014                 :          0 :         if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state))
    2015                 :            :                 nfs4_state_start_reclaim_nograce(clp);
    2016                 :          0 :         if (!test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state))
    2017                 :          0 :                 set_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state);
    2018                 :          0 :         clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
    2019                 :          0 :         clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
    2020                 :          0 :         return 0;
    2021                 :            : }
    2022                 :            : 
    2023                 :          0 : static int nfs4_purge_lease(struct nfs_client *clp)
    2024                 :            : {
    2025                 :            :         int status;
    2026                 :            : 
    2027                 :          0 :         status = nfs4_establish_lease(clp);
    2028                 :          0 :         if (status < 0)
    2029                 :          0 :                 return nfs4_handle_reclaim_lease_error(clp, status);
    2030                 :          0 :         clear_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
    2031                 :          0 :         set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
    2032                 :            :         nfs4_state_start_reclaim_nograce(clp);
    2033                 :          0 :         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                 :          0 : static int nfs4_try_migration(struct nfs_server *server, const struct cred *cred)
    2044                 :            : {
    2045                 :          0 :         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                 :          0 :         if (page == NULL || locations == NULL) {
    2060                 :            :                 dprintk("<-- %s: no memory\n", __func__);
    2061                 :            :                 goto out;
    2062                 :            :         }
    2063                 :            : 
    2064                 :          0 :         inode = d_inode(server->super->s_root);
    2065                 :          0 :         result = nfs4_proc_get_locations(inode, locations, page, cred);
    2066                 :          0 :         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                 :          0 :         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                 :          0 :         status = nfs4_begin_drain_session(clp);
    2080                 :          0 :         if (status != 0) {
    2081                 :            :                 result = status;
    2082                 :            :                 goto out;
    2083                 :            :         }
    2084                 :            : 
    2085                 :          0 :         status = nfs4_replace_transport(server, locations);
    2086                 :          0 :         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                 :          0 :         if (page != NULL)
    2097                 :          0 :                 __free_page(page);
    2098                 :          0 :         kfree(locations);
    2099                 :          0 :         if (result) {
    2100                 :          0 :                 pr_err("NFS: migration recovery failed (server %s)\n",
    2101                 :            :                                 clp->cl_hostname);
    2102                 :          0 :                 set_bit(NFS_MIG_FAILED, &server->mig_status);
    2103                 :            :         }
    2104                 :          0 :         return result;
    2105                 :            : }
    2106                 :            : 
    2107                 :            : /*
    2108                 :            :  * Returns zero or a negative NFS4ERR status code.
    2109                 :            :  */
    2110                 :          0 : static int nfs4_handle_migration(struct nfs_client *clp)
    2111                 :            : {
    2112                 :          0 :         const struct nfs4_state_maintenance_ops *ops =
    2113                 :          0 :                                 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                 :          0 :         cred = ops->get_state_renewal_cred(clp);
    2121                 :          0 :         if (cred == NULL)
    2122                 :            :                 return -NFS4ERR_NOENT;
    2123                 :            : 
    2124                 :          0 :         clp->cl_mig_gen++;
    2125                 :            : restart:
    2126                 :            :         rcu_read_lock();
    2127                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
    2128                 :            :                 int status;
    2129                 :            : 
    2130                 :          0 :                 if (server->mig_gen == clp->cl_mig_gen)
    2131                 :          0 :                         continue;
    2132                 :          0 :                 server->mig_gen = clp->cl_mig_gen;
    2133                 :            : 
    2134                 :          0 :                 if (!test_and_clear_bit(NFS_MIG_IN_TRANSITION,
    2135                 :            :                                                 &server->mig_status))
    2136                 :          0 :                         continue;
    2137                 :            : 
    2138                 :            :                 rcu_read_unlock();
    2139                 :          0 :                 status = nfs4_try_migration(server, cred);
    2140                 :          0 :                 if (status < 0) {
    2141                 :          0 :                         put_cred(cred);
    2142                 :          0 :                         return status;
    2143                 :            :                 }
    2144                 :            :                 goto restart;
    2145                 :            :         }
    2146                 :            :         rcu_read_unlock();
    2147                 :          0 :         put_cred(cred);
    2148                 :          0 :         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                 :          0 : static int nfs4_handle_lease_moved(struct nfs_client *clp)
    2157                 :            : {
    2158                 :          0 :         const struct nfs4_state_maintenance_ops *ops =
    2159                 :          0 :                                 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                 :          0 :         cred = ops->get_state_renewal_cred(clp);
    2167                 :          0 :         if (cred == NULL)
    2168                 :            :                 return -NFS4ERR_NOENT;
    2169                 :            : 
    2170                 :          0 :         clp->cl_mig_gen++;
    2171                 :            : restart:
    2172                 :            :         rcu_read_lock();
    2173                 :          0 :         list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
    2174                 :            :                 struct inode *inode;
    2175                 :            :                 int status;
    2176                 :            : 
    2177                 :          0 :                 if (server->mig_gen == clp->cl_mig_gen)
    2178                 :          0 :                         continue;
    2179                 :          0 :                 server->mig_gen = clp->cl_mig_gen;
    2180                 :            : 
    2181                 :            :                 rcu_read_unlock();
    2182                 :            : 
    2183                 :          0 :                 inode = d_inode(server->super->s_root);
    2184                 :          0 :                 status = nfs4_proc_fsid_present(inode, cred);
    2185                 :          0 :                 if (status != -NFS4ERR_MOVED)
    2186                 :            :                         goto restart;   /* wasn't this one */
    2187                 :          0 :                 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                 :          0 :         put_cred(cred);
    2195                 :          0 :         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                 :          0 : int nfs4_discover_server_trunking(struct nfs_client *clp,
    2212                 :            :                                   struct nfs_client **result)
    2213                 :            : {
    2214                 :          0 :         const struct nfs4_state_recovery_ops *ops =
    2215                 :          0 :                                 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                 :          0 :         clnt = clp->cl_rpcclient;
    2223                 :            :         i = 0;
    2224                 :            : 
    2225                 :          0 :         mutex_lock(&nfs_clid_init_mutex);
    2226                 :            : again:
    2227                 :            :         status  = -ENOENT;
    2228                 :            :         cred = nfs4_get_clid_cred(clp);
    2229                 :          0 :         if (cred == NULL)
    2230                 :            :                 goto out_unlock;
    2231                 :            : 
    2232                 :          0 :         status = ops->detect_trunking(clp, result, cred);
    2233                 :          0 :         put_cred(cred);
    2234                 :          0 :         switch (status) {
    2235                 :            :         case 0:
    2236                 :            :         case -EINTR:
    2237                 :            :         case -ERESTARTSYS:
    2238                 :            :                 break;
    2239                 :            :         case -ETIMEDOUT:
    2240                 :          0 :                 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                 :          0 :                 if (i++ == 0) {
    2253                 :            :                         nfs4_root_machine_cred(clp);
    2254                 :            :                         goto again;
    2255                 :            :                 }
    2256                 :          0 :                 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                 :          0 :                 if (clnt->cl_auth->au_flavor == RPC_AUTH_UNIX) {
    2263                 :            :                         status = -EPERM;
    2264                 :            :                         break;
    2265                 :            :                 }
    2266                 :          0 :                 clnt = rpc_clone_client_set_auth(clnt, RPC_AUTH_UNIX);
    2267                 :          0 :                 if (IS_ERR(clnt)) {
    2268                 :            :                         status = PTR_ERR(clnt);
    2269                 :          0 :                         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                 :          0 :                 clnt = xchg(&clp->cl_rpcclient, clnt);
    2276                 :          0 :                 rpc_shutdown_client(clnt);
    2277                 :          0 :                 clnt = clp->cl_rpcclient;
    2278                 :          0 :                 goto again;
    2279                 :            : 
    2280                 :            :         case -NFS4ERR_MINOR_VERS_MISMATCH:
    2281                 :            :                 status = -EPROTONOSUPPORT;
    2282                 :          0 :                 break;
    2283                 :            : 
    2284                 :            :         case -EKEYEXPIRED:
    2285                 :            :         case -NFS4ERR_NOT_SAME: /* FixMe: implement recovery
    2286                 :            :                                  * in nfs4_exchange_id */
    2287                 :            :                 status = -EKEYEXPIRED;
    2288                 :          0 :                 break;
    2289                 :            :         default:
    2290                 :          0 :                 pr_warn("NFS: %s unhandled error %d. Exiting with error EIO\n",
    2291                 :            :                                 __func__, status);
    2292                 :            :                 status = -EIO;
    2293                 :            :         }
    2294                 :            : 
    2295                 :            : out_unlock:
    2296                 :          0 :         mutex_unlock(&nfs_clid_init_mutex);
    2297                 :            :         dprintk("NFS: %s: status = %d\n", __func__, status);
    2298                 :          0 :         return status;
    2299                 :            : }
    2300                 :            : 
    2301                 :            : #ifdef CONFIG_NFS_V4_1
    2302                 :          0 : void nfs4_schedule_session_recovery(struct nfs4_session *session, int err)
    2303                 :            : {
    2304                 :          0 :         struct nfs_client *clp = session->clp;
    2305                 :            : 
    2306                 :          0 :         switch (err) {
    2307                 :            :         default:
    2308                 :          0 :                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
    2309                 :          0 :                 break;
    2310                 :            :         case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
    2311                 :          0 :                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
    2312                 :            :         }
    2313                 :          0 :         nfs4_schedule_state_manager(clp);
    2314                 :          0 : }
    2315                 :            : EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery);
    2316                 :            : 
    2317                 :          0 : void nfs41_notify_server(struct nfs_client *clp)
    2318                 :            : {
    2319                 :            :         /* Use CHECK_LEASE to ping the server with a SEQUENCE */
    2320                 :          0 :         set_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state);
    2321                 :          0 :         nfs4_schedule_state_manager(clp);
    2322                 :          0 : }
    2323                 :            : 
    2324                 :          0 : static void nfs4_reset_all_state(struct nfs_client *clp)
    2325                 :            : {
    2326                 :          0 :         if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) {
    2327                 :          0 :                 set_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state);
    2328                 :          0 :                 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                 :          0 :                 nfs4_schedule_state_manager(clp);
    2333                 :            :         }
    2334                 :          0 : }
    2335                 :            : 
    2336                 :          0 : static void nfs41_handle_server_reboot(struct nfs_client *clp)
    2337                 :            : {
    2338                 :          0 :         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                 :          0 :                 nfs4_schedule_state_manager(clp);
    2343                 :            :         }
    2344                 :          0 : }
    2345                 :            : 
    2346                 :            : static void nfs41_handle_all_state_revoked(struct nfs_client *clp)
    2347                 :            : {
    2348                 :          0 :         nfs4_reset_all_state(clp);
    2349                 :            :         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
    2350                 :            : }
    2351                 :            : 
    2352                 :          0 : static void nfs41_handle_some_state_revoked(struct nfs_client *clp)
    2353                 :            : {
    2354                 :            :         nfs4_state_start_reclaim_nograce(clp);
    2355                 :          0 :         nfs4_schedule_state_manager(clp);
    2356                 :            : 
    2357                 :            :         dprintk("%s: state revoked on server %s\n", __func__, clp->cl_hostname);
    2358                 :          0 : }
    2359                 :            : 
    2360                 :            : static void nfs41_handle_recallable_state_revoked(struct nfs_client *clp)
    2361                 :            : {
    2362                 :            :         /* FIXME: For now, we destroy all layouts. */
    2363                 :          0 :         pnfs_destroy_all_layouts(clp);
    2364                 :          0 :         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                 :          0 :         set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
    2372                 :          0 :         nfs4_schedule_state_manager(clp);
    2373                 :            : 
    2374                 :            :         dprintk("%s: server %s declared a backchannel fault\n", __func__,
    2375                 :            :                         clp->cl_hostname);
    2376                 :            : }
    2377                 :            : 
    2378                 :          0 : static void nfs41_handle_cb_path_down(struct nfs_client *clp)
    2379                 :            : {
    2380                 :          0 :         if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
    2381                 :            :                 &clp->cl_state) == 0)
    2382                 :          0 :                 nfs4_schedule_state_manager(clp);
    2383                 :          0 : }
    2384                 :            : 
    2385                 :          0 : void nfs41_handle_sequence_flag_errors(struct nfs_client *clp, u32 flags,
    2386                 :            :                 bool recovery)
    2387                 :            : {
    2388                 :          0 :         if (!flags)
    2389                 :          0 :                 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                 :          0 :         if (recovery)
    2400                 :            :                 goto out_recovery;
    2401                 :            : 
    2402                 :          0 :         if (flags & SEQ4_STATUS_RESTART_RECLAIM_NEEDED)
    2403                 :          0 :                 nfs41_handle_server_reboot(clp);
    2404                 :          0 :         if (flags & (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED))
    2405                 :            :                 nfs41_handle_all_state_revoked(clp);
    2406                 :          0 :         if (flags & (SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED |
    2407                 :            :                             SEQ4_STATUS_ADMIN_STATE_REVOKED))
    2408                 :          0 :                 nfs41_handle_some_state_revoked(clp);
    2409                 :          0 :         if (flags & SEQ4_STATUS_LEASE_MOVED)
    2410                 :            :                 nfs4_schedule_lease_moved_recovery(clp);
    2411                 :          0 :         if (flags & SEQ4_STATUS_RECALLABLE_STATE_REVOKED)
    2412                 :            :                 nfs41_handle_recallable_state_revoked(clp);
    2413                 :            : out_recovery:
    2414                 :          0 :         if (flags & SEQ4_STATUS_BACKCHANNEL_FAULT)
    2415                 :            :                 nfs41_handle_backchannel_fault(clp);
    2416                 :          0 :         else if (flags & (SEQ4_STATUS_CB_PATH_DOWN |
    2417                 :            :                                 SEQ4_STATUS_CB_PATH_DOWN_SESSION))
    2418                 :          0 :                 nfs41_handle_cb_path_down(clp);
    2419                 :            : }
    2420                 :            : 
    2421                 :          0 : static int nfs4_reset_session(struct nfs_client *clp)
    2422                 :            : {
    2423                 :            :         const struct cred *cred;
    2424                 :            :         int status;
    2425                 :            : 
    2426                 :          0 :         if (!nfs4_has_session(clp))
    2427                 :            :                 return 0;
    2428                 :          0 :         status = nfs4_begin_drain_session(clp);
    2429                 :          0 :         if (status != 0)
    2430                 :            :                 return status;
    2431                 :            :         cred = nfs4_get_clid_cred(clp);
    2432                 :          0 :         status = nfs4_proc_destroy_session(clp->cl_session, cred);
    2433                 :          0 :         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                 :          0 :                 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
    2441                 :            :                 status = 0;
    2442                 :            :                 ssleep(1);
    2443                 :            :                 goto out;
    2444                 :            :         default:
    2445                 :          0 :                 status = nfs4_recovery_handle_error(clp, status);
    2446                 :          0 :                 goto out;
    2447                 :            :         }
    2448                 :            : 
    2449                 :          0 :         memset(clp->cl_session->sess_id.data, 0, NFS4_MAX_SESSIONID_LEN);
    2450                 :          0 :         status = nfs4_proc_create_session(clp, cred);
    2451                 :          0 :         if (status) {
    2452                 :            :                 dprintk("%s: session reset failed with status %d for server %s!\n",
    2453                 :            :                         __func__, status, clp->cl_hostname);
    2454                 :          0 :                 status = nfs4_handle_reclaim_lease_error(clp, status);
    2455                 :          0 :                 goto out;
    2456                 :            :         }
    2457                 :          0 :         nfs41_finish_session_reset(clp);
    2458                 :            :         dprintk("%s: session reset was successful for server %s!\n",
    2459                 :            :                         __func__, clp->cl_hostname);
    2460                 :            : out:
    2461                 :          0 :         put_cred(cred);
    2462                 :          0 :         return status;
    2463                 :            : }
    2464                 :            : 
    2465                 :          0 : static int nfs4_bind_conn_to_session(struct nfs_client *clp)
    2466                 :            : {
    2467                 :            :         const struct cred *cred;
    2468                 :            :         int ret;
    2469                 :            : 
    2470                 :          0 :         if (!nfs4_has_session(clp))
    2471                 :            :                 return 0;
    2472                 :          0 :         ret = nfs4_begin_drain_session(clp);
    2473                 :          0 :         if (ret != 0)
    2474                 :            :                 return ret;
    2475                 :            :         cred = nfs4_get_clid_cred(clp);
    2476                 :          0 :         ret = nfs4_proc_bind_conn_to_session(clp, cred);
    2477                 :          0 :         put_cred(cred);
    2478                 :          0 :         clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
    2479                 :          0 :         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                 :          0 :                 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION, &clp->cl_state);
    2487                 :          0 :                 break;
    2488                 :            :         default:
    2489                 :          0 :                 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                 :          0 : 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                 :          0 :                 clear_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
    2510                 :          0 :                 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
    2511                 :            :                         section = "purge state";
    2512                 :          0 :                         status = nfs4_purge_lease(clp);
    2513                 :          0 :                         if (status < 0)
    2514                 :            :                                 goto out_error;
    2515                 :          0 :                         continue;
    2516                 :            :                 }
    2517                 :            : 
    2518                 :          0 :                 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                 :          0 :                         status = nfs4_reclaim_lease(clp);
    2522                 :          0 :                         if (status < 0)
    2523                 :            :                                 goto out_error;
    2524                 :          0 :                         continue;
    2525                 :            :                 }
    2526                 :            : 
    2527                 :            :                 /* Initialize or reset the session */
    2528                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state)) {
    2529                 :            :                         section = "reset session";
    2530                 :          0 :                         status = nfs4_reset_session(clp);
    2531                 :          0 :                         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state))
    2532                 :          0 :                                 continue;
    2533                 :          0 :                         if (status < 0)
    2534                 :            :                                 goto out_error;
    2535                 :            :                 }
    2536                 :            : 
    2537                 :            :                 /* Send BIND_CONN_TO_SESSION */
    2538                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION,
    2539                 :            :                                 &clp->cl_state)) {
    2540                 :            :                         section = "bind conn to session";
    2541                 :          0 :                         status = nfs4_bind_conn_to_session(clp);
    2542                 :          0 :                         if (status < 0)
    2543                 :            :                                 goto out_error;
    2544                 :          0 :                         continue;
    2545                 :            :                 }
    2546                 :            : 
    2547                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE, &clp->cl_state)) {
    2548                 :            :                         section = "check lease";
    2549                 :          0 :                         status = nfs4_check_lease(clp);
    2550                 :          0 :                         if (status < 0)
    2551                 :            :                                 goto out_error;
    2552                 :          0 :                         continue;
    2553                 :            :                 }
    2554                 :            : 
    2555                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_MOVED, &clp->cl_state)) {
    2556                 :            :                         section = "migration";
    2557                 :          0 :                         status = nfs4_handle_migration(clp);
    2558                 :          0 :                         if (status < 0)
    2559                 :            :                                 goto out_error;
    2560                 :            :                 }
    2561                 :            : 
    2562                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_LEASE_MOVED, &clp->cl_state)) {
    2563                 :            :                         section = "lease moved";
    2564                 :          0 :                         status = nfs4_handle_lease_moved(clp);
    2565                 :          0 :                         if (status < 0)
    2566                 :            :                                 goto out_error;
    2567                 :            :                 }
    2568                 :            : 
    2569                 :            :                 /* First recover reboot state... */
    2570                 :          0 :                 if (test_bit(NFS4CLNT_RECLAIM_REBOOT, &clp->cl_state)) {
    2571                 :            :                         section = "reclaim reboot";
    2572                 :          0 :                         status = nfs4_do_reclaim(clp,
    2573                 :          0 :                                 clp->cl_mvops->reboot_recovery_ops);
    2574                 :          0 :                         if (status == -EAGAIN)
    2575                 :          0 :                                 continue;
    2576                 :          0 :                         if (status < 0)
    2577                 :            :                                 goto out_error;
    2578                 :          0 :                         nfs4_state_end_reclaim_reboot(clp);
    2579                 :            :                 }
    2580                 :            : 
    2581                 :            :                 /* Detect expired delegations... */
    2582                 :          0 :                 if (test_and_clear_bit(NFS4CLNT_DELEGATION_EXPIRED, &clp->cl_state)) {
    2583                 :            :                         section = "detect expired delegations";
    2584                 :          0 :                         nfs_reap_expired_delegations(clp);
    2585                 :          0 :                         continue;
    2586                 :            :                 }
    2587                 :            : 
    2588                 :            :                 /* Now recover expired state... */
    2589                 :          0 :                 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state)) {
    2590                 :            :                         section = "reclaim nograce";
    2591                 :          0 :                         status = nfs4_do_reclaim(clp,
    2592                 :          0 :                                 clp->cl_mvops->nograce_recovery_ops);
    2593                 :          0 :                         if (status == -EAGAIN)
    2594                 :          0 :                                 continue;
    2595                 :          0 :                         if (status < 0)
    2596                 :            :                                 goto out_error;
    2597                 :          0 :                         clear_bit(NFS4CLNT_RECLAIM_NOGRACE, &clp->cl_state);
    2598                 :            :                 }
    2599                 :            : 
    2600                 :          0 :                 nfs4_end_drain_session(clp);
    2601                 :          0 :                 nfs4_clear_state_manager_bit(clp);
    2602                 :            : 
    2603                 :          0 :                 if (!test_and_set_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state)) {
    2604                 :          0 :                         if (test_and_clear_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) {
    2605                 :          0 :                                 nfs_client_return_marked_delegations(clp);
    2606                 :          0 :                                 set_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state);
    2607                 :            :                         }
    2608                 :          0 :                         clear_bit(NFS4CLNT_DELEGRETURN_RUNNING, &clp->cl_state);
    2609                 :            :                 }
    2610                 :            : 
    2611                 :            :                 /* Did we race with an attempt to give us more work? */
    2612                 :          0 :                 if (!test_bit(NFS4CLNT_RUN_MANAGER, &clp->cl_state))
    2613                 :            :                         return;
    2614                 :          0 :                 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) != 0)
    2615                 :            :                         return;
    2616                 :          0 :         } while (refcount_read(&clp->cl_count) > 1 && !signalled());
    2617                 :            :         goto out_drain;
    2618                 :            : 
    2619                 :            : out_error:
    2620                 :          0 :         if (strlen(section))
    2621                 :            :                 section_sep = ": ";
    2622                 :          0 :         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                 :          0 :         nfs4_end_drain_session(clp);
    2628                 :          0 :         nfs4_clear_state_manager_bit(clp);
    2629                 :            : }
    2630                 :            : 
    2631                 :          0 : static int nfs4_run_state_manager(void *ptr)
    2632                 :            : {
    2633                 :            :         struct nfs_client *clp = ptr;
    2634                 :            : 
    2635                 :            :         allow_signal(SIGKILL);
    2636                 :          0 :         nfs4_state_manager(clp);
    2637                 :          0 :         nfs_put_client(clp);
    2638                 :          0 :         module_put_and_exit(0);
    2639                 :            :         return 0;
    2640                 :            : }
    2641                 :            : 
    2642                 :            : /*
    2643                 :            :  * Local variables:
    2644                 :            :  *  c-basic-offset: 8
    2645                 :            :  * End:
    2646                 :            :  */
    

Generated by: LCOV version 1.14