Whamcloud - gitweb
LU-11128 ptlrpc: new request vs disconnect race
[fs/lustre-release.git] / lustre / ptlrpc / import.c
index 525ff1f..8ca1dec 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2016, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -75,7 +75,7 @@ static void __import_set_state(struct obd_import *imp,
         imp->imp_state = state;
         imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
         imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
-                cfs_time_current_sec();
+               ktime_get_real_seconds();
         imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
                 IMP_STATE_HIST_LEN;
 }
@@ -175,16 +175,15 @@ int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
                                "lost; in progress operations using this "
                                "service will wait for recovery to complete\n",
                                imp->imp_obd->obd_name, target_len, target_start,
-                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
-                } else {
-                        LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
-                               "%.*s (at %s) was lost; in progress "
-                               "operations using this service will fail\n",
-                               imp->imp_obd->obd_name,
-                               target_len, target_start,
-                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
-                }
-                IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
+                              obd_import_nid2str(imp));
+               } else {
+                       LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
+                              "%.*s (at %s) was lost; in progress "
+                              "operations using this service will fail\n",
+                              imp->imp_obd->obd_name, target_len, target_start,
+                              obd_import_nid2str(imp));
+               }
+               IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
                spin_unlock(&imp->imp_lock);
 
                if (obd_dump_on_timeout)
@@ -233,10 +232,10 @@ void ptlrpc_deactivate_import(struct obd_import *imp)
 }
 EXPORT_SYMBOL(ptlrpc_deactivate_import);
 
-static unsigned int
-ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
+static time64_t ptlrpc_inflight_deadline(struct ptlrpc_request *req,
+                                        time64_t now)
 {
-        long dl;
+       time64_t dl;
 
         if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
               (req->rq_phase == RQ_PHASE_BULK) ||
@@ -257,12 +256,12 @@ ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
         return dl - now;
 }
 
-static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
+static time64_t ptlrpc_inflight_timeout(struct obd_import *imp)
 {
-       time_t now = cfs_time_current_sec();
+       time64_t now = ktime_get_real_seconds();
        struct list_head *tmp, *n;
        struct ptlrpc_request *req;
-       unsigned int timeout = 0;
+       time64_t timeout = 0;
 
        spin_lock(&imp->imp_lock);
        list_for_each_safe(tmp, n, &imp->imp_sending_list) {
@@ -284,7 +283,7 @@ void ptlrpc_invalidate_import(struct obd_import *imp)
        struct list_head *tmp, *n;
        struct ptlrpc_request *req;
        struct l_wait_info lwi;
-       unsigned int timeout;
+       time64_t timeout;
        int rc;
 
        atomic_inc(&imp->imp_inval_count);
@@ -300,30 +299,35 @@ void ptlrpc_invalidate_import(struct obd_import *imp)
          * unlink. We can't do anything before that because there is really
          * no guarantee that some rdma transfer is not in progress right now. */
         do {
+               long timeout_jiffies;
+
                 /* Calculate max timeout for waiting on rpcs to error
                  * out. Use obd_timeout if calculated value is smaller
-                 * than it. */
-                if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
-                        timeout = ptlrpc_inflight_timeout(imp);
-                        timeout += timeout / 3;
-
-                        if (timeout == 0)
-                                timeout = obd_timeout;
-                } else {
-                        /* decrease the interval to increase race condition */
-                        timeout = 1;
-                }
+                * than it.
+                */
+               if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
+                       timeout = ptlrpc_inflight_timeout(imp);
+                       timeout += div_u64(timeout, 3);
+
+                       if (timeout == 0)
+                               timeout = obd_timeout;
+               } else {
+                       /* decrease the interval to increase race condition */
+                       timeout = 1;
+               }
 
-                CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
-                       timeout);
+               CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
+                      timeout);
 
                /* Wait for all requests to error out and call completion
                 * callbacks. Cap it at obd_timeout -- these should all
-                * have been locally cancelled by ptlrpc_abort_inflight. */
-               lwi = LWI_TIMEOUT_INTERVAL(
-                       cfs_timeout_cap(cfs_time_seconds(timeout)),
-                       (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
-                       NULL, NULL);
+                * have been locally cancelled by ptlrpc_abort_inflight.
+                */
+               timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
+               lwi = LWI_TIMEOUT_INTERVAL(timeout_jiffies,
+                                          (timeout > 1) ? cfs_time_seconds(1) :
+                                                          cfs_time_seconds(1) / 2,
+                                                          NULL, NULL);
                rc = l_wait_event(imp->imp_recovery_waitq,
                                  (atomic_read(&imp->imp_inflight) == 0),
                                  &lwi);
@@ -445,16 +449,16 @@ void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
 int ptlrpc_reconnect_import(struct obd_import *imp)
 {
 #ifdef ENABLE_PINGER
+       long timeout_jiffies = cfs_time_seconds(obd_timeout);
        struct l_wait_info lwi;
-       int secs = cfs_time_seconds(obd_timeout);
        int rc;
 
        ptlrpc_pinger_force(imp);
 
        CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
-              obd2cli_tgt(imp->imp_obd), secs);
+              obd2cli_tgt(imp->imp_obd), obd_timeout);
 
-       lwi = LWI_TIMEOUT(secs, NULL, NULL);
+       lwi = LWI_TIMEOUT(timeout_jiffies, NULL, NULL);
        rc = l_wait_event(imp->imp_recovery_waitq,
                          !ptlrpc_import_in_recovery(imp), &lwi);
        CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
@@ -513,7 +517,7 @@ static int import_select_connection(struct obd_import *imp)
        }
 
        list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
-               CDEBUG(D_HA, "%s: connect to NID %s last attempt %llu\n",
+               CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
                        imp->imp_obd->obd_name,
                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
                        conn->oic_last_attempt);
@@ -521,8 +525,7 @@ static int import_select_connection(struct obd_import *imp)
                 /* If we have not tried this connection since
                    the last successful attempt, go with this one */
                 if ((conn->oic_last_attempt == 0) ||
-                    cfs_time_beforeq_64(conn->oic_last_attempt,
-                                       imp->imp_last_success_conn)) {
+                   conn->oic_last_attempt <= imp->imp_last_success_conn) {
                         imp_conn = conn;
                         tried_all = 0;
                         break;
@@ -533,8 +536,7 @@ static int import_select_connection(struct obd_import *imp)
                    least recently used */
                 if (!imp_conn)
                         imp_conn = conn;
-                else if (cfs_time_before_64(conn->oic_last_attempt,
-                                            imp_conn->oic_last_attempt))
+               else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
                         imp_conn = conn;
         }
 
@@ -563,7 +565,7 @@ static int import_select_connection(struct obd_import *imp)
                        "to %ds\n", imp->imp_obd->obd_name, at_get(at));
        }
 
-        imp_conn->oic_last_attempt = cfs_time_current_64();
+       imp_conn->oic_last_attempt = ktime_get_seconds();
 
         /* switch connection, don't mind if it's same as the current one */
         if (imp->imp_connection)
@@ -759,7 +761,7 @@ int ptlrpc_connect_import(struct obd_import *imp)
                lustre_msg_add_op_flags(request->rq_reqmsg,
                                        MSG_CONNECT_TRANSNO);
 
-       DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
+       DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %ld)",
                  request->rq_timeout);
        ptlrpcd_add_req(request);
        rc = 0;
@@ -789,25 +791,13 @@ static int ptlrpc_busy_reconnect(int rc)
 }
 
 static int ptlrpc_connect_set_flags(struct obd_import *imp,
-                                    struct obd_connect_data *ocd,
-                                    __u64 old_connect_flags,
-                                    struct obd_export *exp, int init_connect)
+                                   struct obd_connect_data *ocd,
+                                   __u64 old_connect_flags,
+                                   struct obd_export *exp, int init_connect)
 {
        static bool warned;
        struct client_obd *cli = &imp->imp_obd->u.cli;
 
-       if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
-           !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
-               LCONSOLE_WARN("%s: MDS %s does not support ibits "
-                             "lock, either very old or invalid: "
-                             "requested %#llx, replied %#llx\n",
-                             imp->imp_obd->obd_name,
-                             imp->imp_connection->c_remote_uuid.uuid,
-                             imp->imp_connect_flags_orig,
-                             ocd->ocd_connect_flags);
-               return -EPROTO;
-       }
-
        spin_lock(&imp->imp_lock);
        list_del(&imp->imp_conn_current->oic_item);
        list_add(&imp->imp_conn_current->oic_item,
@@ -817,7 +807,6 @@ static int ptlrpc_connect_set_flags(struct obd_import *imp,
 
        spin_unlock(&imp->imp_lock);
 
-
        if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
            (ocd->ocd_version > LUSTRE_VERSION_CODE +
                                LUSTRE_VERSION_OFFSET_WARN ||
@@ -828,7 +817,7 @@ static int ptlrpc_connect_set_flags(struct obd_import *imp,
                const char *older = "older than client. "
                                    "Consider upgrading server";
                const char *newer = "newer than client. "
-                                   "Consider recompiling application";
+                                   "Consider upgrading client";
 
                LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
                              "is much %s (%s)\n",
@@ -842,40 +831,19 @@ static int ptlrpc_connect_set_flags(struct obd_import *imp,
                warned = true;
        }
 
-#if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
-       /* Check if server has LU-1252 fix applied to not always swab
-        * the IR MNE entries. Do this only once per connection.  This
-        * fixup is version-limited, because we don't want to carry the
-        * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
-        * need interop with unpatched 2.2 servers.  For newer servers,
-        * the client will do MNE swabbing only as needed.  LU-1644 */
-       if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
-                    !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
-                    OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
-                    OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
-                    OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
-                    strcmp(imp->imp_obd->obd_type->typ_name,
-                           LUSTRE_MGC_NAME) == 0))
-               imp->imp_need_mne_swab = 1;
-       else /* clear if server was upgraded since last connect */
-               imp->imp_need_mne_swab = 0;
-#endif
-
        if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
                /* We sent to the server ocd_cksum_types with bits set
                 * for algorithms we understand. The server masked off
                 * the checksum types it doesn't support */
                if ((ocd->ocd_cksum_types &
-                    cksum_types_supported_client()) == 0) {
-                       LCONSOLE_WARN("The negotiation of the checksum "
-                                     "alogrithm to use with server %s "
-                                     "failed (%x/%x), disabling "
-                                     "checksums\n",
-                                     obd2cli_tgt(imp->imp_obd),
-                                     ocd->ocd_cksum_types,
-                                     cksum_types_supported_client());
-                       cli->cl_checksum = 0;
-                       cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
+                    obd_cksum_types_supported_client()) == 0) {
+                       LCONSOLE_ERROR("The negotiation of the checksum "
+                                      "alogrithm to use with server %s "
+                                      "failed (%x/%x)\n",
+                                      obd2cli_tgt(imp->imp_obd),
+                                      ocd->ocd_cksum_types,
+                                      obd_cksum_types_supported_client());
+                       return -EPROTO;
                } else {
                        cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
                }
@@ -884,7 +852,8 @@ static int ptlrpc_connect_set_flags(struct obd_import *imp,
                 * Enforce ADLER for backward compatibility*/
                cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
        }
-       cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
+       cli->cl_cksum_type = obd_cksum_type_select(imp->imp_obd->obd_name,
+                                                  cli->cl_supp_cksum_types);
 
        if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
                cli->cl_max_pages_per_rpc =
@@ -987,7 +956,7 @@ static int ptlrpc_connect_interpret(const struct lu_env *env,
         __u64 old_connect_flags;
         int msg_flags;
        struct obd_connect_data *ocd;
-       struct obd_export *exp;
+       struct obd_export *exp = NULL;
        int ret;
        ENTRY;
 
@@ -999,6 +968,21 @@ static int ptlrpc_connect_interpret(const struct lu_env *env,
        }
 
        if (rc) {
+               struct ptlrpc_request *free_req;
+               struct ptlrpc_request *tmp;
+
+               /* abort all delayed requests initiated connection */
+               list_for_each_entry_safe(free_req, tmp, &imp->imp_delayed_list,
+                                        rq_list) {
+                       spin_lock(&free_req->rq_lock);
+                       if (free_req->rq_no_resend) {
+                               free_req->rq_err = 1;
+                               free_req->rq_status = -EIO;
+                               ptlrpc_client_wake_req(free_req);
+                       }
+                       spin_unlock(&free_req->rq_lock);
+               }
+
                /* if this reconnect to busy export - not need select new target
                 * for connecting*/
                imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
@@ -1110,8 +1094,10 @@ static int ptlrpc_connect_interpret(const struct lu_env *env,
 
        /* Import flags should be updated before waking import at FULL state */
        rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
-                                aa->pcaa_initial_connect);
+                                     aa->pcaa_initial_connect);
        class_export_put(exp);
+       exp = NULL;
+
        if (rc != 0)
                GOTO(out, rc);
 
@@ -1283,6 +1269,9 @@ out:
        imp->imp_connect_tried = 1;
        spin_unlock(&imp->imp_lock);
 
+       if (exp != NULL)
+               class_export_put(exp);
+
         if (rc != 0) {
                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
                 if (rc == -EACCES) {
@@ -1301,14 +1290,15 @@ out:
                         if (request->rq_repmsg == NULL)
                                 RETURN(-EPROTO);
 
-                        ocd = req_capsule_server_get(&request->rq_pill,
-                                                     &RMF_CONNECT_DATA);
-                        if (ocd &&
-                            (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
-                            (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
-                           /* Actually servers are only supposed to refuse
-                              connection from liblustre clients, so we should
-                              never see this from VFS context */
+                       ocd = req_capsule_server_get(&request->rq_pill,
+                                                    &RMF_CONNECT_DATA);
+                       /* Servers are not supposed to refuse connections from
+                        * clients based on version, only connection feature
+                        * flags.  We should never see this from llite, but it
+                        * may be useful for debugging in the future. */
+                       if (ocd &&
+                           (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
+                           (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
                                         "(%d.%d.%d.%d)"
                                         " refused connection from this client "
@@ -1471,6 +1461,7 @@ int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
                                           "using this service will fail.\n",
                                           imp->imp_obd->obd_name, target_len,
                                           target_start);
+                       LASSERTF(!obd_lbug_on_eviction, "LBUG upon eviction");
                 }
                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
                        obd2cli_tgt(imp->imp_obd),
@@ -1541,7 +1532,7 @@ int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
                LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
                              imp->imp_obd->obd_name,
                              obd_uuid2str(&conn->c_remote_uuid),
-                             libcfs_nid2str(imp->imp_connection->c_peer.nid));
+                             obd_import_nid2str(imp));
         }
 
        if (imp->imp_state == LUSTRE_IMP_FULL) {
@@ -1553,15 +1544,12 @@ out:
        RETURN(rc);
 }
 
-int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
+static struct ptlrpc_request *ptlrpc_disconnect_prep_req(struct obd_import *imp)
 {
        struct ptlrpc_request *req;
        int rq_opc, rc = 0;
        ENTRY;
 
-       if (imp->imp_obd->obd_force)
-               GOTO(set_state, rc);
-
        switch (imp->imp_connect_op) {
        case OST_CONNECT:
                rq_opc = OST_DISCONNECT;
@@ -1578,26 +1566,67 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
                       "(connect_op %d): rc = %d\n",
                       imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
                       imp->imp_connect_op, rc);
-               RETURN(rc);
+               RETURN(ERR_PTR(rc));
        }
 
-        if (ptlrpc_import_in_recovery(imp)) {
-                struct l_wait_info lwi;
-                cfs_duration_t timeout;
+       req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
+                                       LUSTRE_OBD_VERSION, rq_opc);
+       if (req == NULL)
+               RETURN(NULL);
 
-                if (AT_OFF) {
-                        if (imp->imp_server_timeout)
-                                timeout = cfs_time_seconds(obd_timeout / 2);
-                        else
-                                timeout = cfs_time_seconds(obd_timeout);
-                } else {
-                        int idx = import_at_get_index(imp,
-                                imp->imp_client->cli_request_portal);
-                        timeout = cfs_time_seconds(
-                                at_get(&imp->imp_at.iat_service_estimate[idx]));
+       /* We are disconnecting, do not retry a failed DISCONNECT rpc if
+        * it fails.  We can get through the above with a down server
+        * if the client doesn't know the server is gone yet. */
+       req->rq_no_resend = 1;
+
+       /* We want client umounts to happen quickly, no matter the
+          server state... */
+       req->rq_timeout = min_t(int, req->rq_timeout,
+                               INITIAL_CONNECT_TIMEOUT);
+
+       IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
+       req->rq_send_state =  LUSTRE_IMP_CONNECTING;
+       ptlrpc_request_set_replen(req);
+
+       RETURN(req);
+}
+
+int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
+{
+       struct ptlrpc_request *req;
+       int rc = 0;
+       ENTRY;
+
+       if (imp->imp_obd->obd_force)
+               GOTO(set_state, rc);
+
+       /* probably the import has been disconnected already being idle */
+       spin_lock(&imp->imp_lock);
+       if (imp->imp_state == LUSTRE_IMP_IDLE)
+               GOTO(out, rc);
+       spin_unlock(&imp->imp_lock);
+
+       if (ptlrpc_import_in_recovery(imp)) {
+               struct l_wait_info lwi;
+               long timeout_jiffies;
+               time64_t timeout;
+
+               if (AT_OFF) {
+                       if (imp->imp_server_timeout)
+                               timeout = obd_timeout >> 1;
+                       else
+                               timeout = obd_timeout;
+               } else {
+                       u32 req_portal;
+                       int idx;
+
+                       req_portal = imp->imp_client->cli_request_portal;
+                       idx = import_at_get_index(imp, req_portal);
+                       timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
                 }
 
-                lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
+               timeout_jiffies = cfs_time_seconds(timeout);
+               lwi = LWI_TIMEOUT_INTR(max_t(long, timeout_jiffies, 1),
                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
                 rc = l_wait_event(imp->imp_recovery_waitq,
                                   !ptlrpc_import_in_recovery(imp), &lwi);
@@ -1609,25 +1638,11 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
                GOTO(out, rc);
        spin_unlock(&imp->imp_lock);
 
-        req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
-                                        LUSTRE_OBD_VERSION, rq_opc);
-        if (req) {
-                /* We are disconnecting, do not retry a failed DISCONNECT rpc if
-                 * it fails.  We can get through the above with a down server
-                 * if the client doesn't know the server is gone yet. */
-                req->rq_no_resend = 1;
-
-                /* We want client umounts to happen quickly, no matter the
-                   server state... */
-                req->rq_timeout = min_t(int, req->rq_timeout,
-                                        INITIAL_CONNECT_TIMEOUT);
-
-                IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
-                req->rq_send_state =  LUSTRE_IMP_CONNECTING;
-                ptlrpc_request_set_replen(req);
-                rc = ptlrpc_queue_wait(req);
-                ptlrpc_req_finished(req);
-        }
+       req = ptlrpc_disconnect_prep_req(imp);
+       if (IS_ERR(req))
+               GOTO(set_state, rc = PTR_ERR(req));
+       rc = ptlrpc_queue_wait(req);
+       ptlrpc_req_finished(req);
 
 set_state:
        spin_lock(&imp->imp_lock);
@@ -1645,6 +1660,78 @@ out:
 }
 EXPORT_SYMBOL(ptlrpc_disconnect_import);
 
+static int ptlrpc_disconnect_idle_interpret(const struct lu_env *env,
+                                           struct ptlrpc_request *req,
+                                           void *data, int rc)
+{
+       struct obd_import *imp = req->rq_import;
+       int connect = 0;
+
+       DEBUG_REQ(D_HA, req, "inflight=%d, refcount=%d: rc = %d\n",
+                 atomic_read(&imp->imp_inflight),
+                 atomic_read(&imp->imp_refcount), rc);
+
+       spin_lock(&imp->imp_lock);
+       /* DISCONNECT reply can be late and another connection can just
+        * be initiated. so we have to abort disconnection. */
+       if (req->rq_import_generation == imp->imp_generation &&
+           imp->imp_state != LUSTRE_IMP_CLOSED) {
+               LASSERTF(imp->imp_state == LUSTRE_IMP_CONNECTING,
+                        "%s\n", ptlrpc_import_state_name(imp->imp_state));
+               imp->imp_state = LUSTRE_IMP_IDLE;
+               memset(&imp->imp_remote_handle, 0,
+                      sizeof(imp->imp_remote_handle));
+               /* take our DISCONNECT into account */
+               if (atomic_read(&imp->imp_inflight) > 1) {
+                       imp->imp_generation++;
+                       imp->imp_initiated_at = imp->imp_generation;
+                       IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_NEW);
+                       connect = 1;
+               }
+       }
+       spin_unlock(&imp->imp_lock);
+
+       if (connect) {
+               rc = ptlrpc_connect_import(imp);
+               if (rc >= 0)
+                       ptlrpc_pinger_add_import(imp);
+       }
+
+       return 0;
+}
+
+int ptlrpc_disconnect_and_idle_import(struct obd_import *imp)
+{
+       struct ptlrpc_request *req;
+       ENTRY;
+
+       if (imp->imp_obd->obd_force)
+               RETURN(0);
+
+       if (ptlrpc_import_in_recovery(imp))
+               RETURN(0);
+
+       spin_lock(&imp->imp_lock);
+       if (imp->imp_state != LUSTRE_IMP_FULL) {
+               spin_unlock(&imp->imp_lock);
+               RETURN(0);
+       }
+       spin_unlock(&imp->imp_lock);
+
+       req = ptlrpc_disconnect_prep_req(imp);
+       if (IS_ERR(req))
+               RETURN(PTR_ERR(req));
+
+       CDEBUG_LIMIT(imp->imp_idle_debug, "%s: disconnect after %llus idle\n",
+                    imp->imp_obd->obd_name,
+                    ktime_get_real_seconds() - imp->imp_last_reply_time);
+       req->rq_interpret_reply = ptlrpc_disconnect_idle_interpret;
+       ptlrpcd_add_req(req);
+
+       RETURN(0);
+}
+EXPORT_SYMBOL(ptlrpc_disconnect_and_idle_import);
+
 void ptlrpc_cleanup_imp(struct obd_import *imp)
 {
        ENTRY;