Whamcloud - gitweb
timeout and assert in invalidate import.
[fs/lustre-release.git] / lustre / ptlrpc / import.c
index c3fbc7d..213f381 100644 (file)
@@ -34,6 +34,7 @@
 #include <lustre_import.h>
 #include <lustre_export.h>
 #include <obd.h>
+#include <obd_cksum.h>
 #include <obd_class.h>
 
 #include "ptlrpc_internal.h"
@@ -125,6 +126,7 @@ int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
 
                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
                           &target_start, &target_len);
+
                 if (imp->imp_replayable) {
                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
                                "%s was lost; in progress operations using this "
@@ -132,12 +134,15 @@ int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
                                imp->imp_obd->obd_name, target_len, target_start,
                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
                 } else {
-                        LCONSOLE_ERROR("%s: Connection to service %.*s via nid "
-                               "%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));
+                        LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
+                                           "%.*s via nid %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));
                 }
+                ptlrpc_deactivate_timeouts(imp);
                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
                 spin_unlock(&imp->imp_lock);
     
@@ -159,15 +164,18 @@ int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
         return rc;
 }
 
-/*
- * This acts as a barrier; all existing requests are rejected, and
- * no new requests will be accepted until the import is valid again.
- */
-void ptlrpc_deactivate_import(struct obd_import *imp)
+/* Must be called with imp_lock held! */
+static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
 {
         ENTRY;
+        LASSERT_SPIN_LOCKED(&imp->imp_lock);
+
+        if (imp->imp_invalid) {
+                spin_unlock(&imp->imp_lock);
+                EXIT;
+                return;
+        }
 
-        spin_lock(&imp->imp_lock);
         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
         imp->imp_invalid = 1;
         imp->imp_generation++;
@@ -175,6 +183,18 @@ void ptlrpc_deactivate_import(struct obd_import *imp)
 
         ptlrpc_abort_inflight(imp);
         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
+
+        EXIT;
+}
+
+/*
+ * This acts as a barrier; all existing requests are rejected, and
+ * no new requests will be accepted until the import is valid again.
+ */
+void ptlrpc_deactivate_import(struct obd_import *imp)
+{
+        spin_lock(&imp->imp_lock);
+        ptlrpc_deactivate_and_unlock_import(imp);
 }
 
 /*
@@ -188,7 +208,16 @@ void ptlrpc_invalidate_import(struct obd_import *imp)
         struct l_wait_info lwi;
         int rc;
 
-        if (!imp->imp_invalid)
+        atomic_inc(&imp->imp_inval_count);
+
+        /*
+         * If this is an invalid MGC connection, then don't bother
+         * waiting for imp_inflight to drop to 0.
+         */
+        if (imp->imp_invalid && imp->imp_recon_bk && !imp->imp_obd->obd_no_recov)
+                goto out;
+
+        if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
                 ptlrpc_deactivate_import(imp);
 
         LASSERT(imp->imp_invalid);
@@ -197,15 +226,34 @@ void ptlrpc_invalidate_import(struct obd_import *imp)
         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
                                    HZ, NULL, NULL);
         rc = l_wait_event(imp->imp_recovery_waitq,
-                          (atomic_read(&imp->imp_inflight) == 0),
-                          &lwi);
+                          (atomic_read(&imp->imp_inflight) == 0), &lwi);
 
-        if (rc)
-                CDEBUG(D_HA, "%s: rc = %d waiting for callback (%d != 0)\n",
+        if (rc) {
+                struct list_head *tmp, *n;
+                struct ptlrpc_request *req;
+
+                CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
                        obd2cli_tgt(imp->imp_obd), rc,
                        atomic_read(&imp->imp_inflight));
+                spin_lock(&imp->imp_lock);
+                list_for_each_safe(tmp, n, &imp->imp_sending_list) {
+                        req = list_entry(tmp, struct ptlrpc_request, rq_list);
+                        DEBUG_REQ(D_ERROR, req, "still on sending list");
+                }
+                list_for_each_safe(tmp, n, &imp->imp_delayed_list) {
+                        req = list_entry(tmp, struct ptlrpc_request, rq_list);
+                        DEBUG_REQ(D_ERROR, req, "still on delayed list");
+                }
+                spin_unlock(&imp->imp_lock);
+                LASSERT(atomic_read(&imp->imp_inflight) == 0);
+        }
 
+out:
         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
+        sptlrpc_import_flush_all_ctx(imp);
+
+        atomic_dec(&imp->imp_inval_count);
+        cfs_waitq_signal(&imp->imp_recovery_waitq);
 }
 
 /* unset imp_invalid */
@@ -215,8 +263,8 @@ void ptlrpc_activate_import(struct obd_import *imp)
 
         spin_lock(&imp->imp_lock);
         imp->imp_invalid = 0;
+        ptlrpc_activate_timeouts(imp);
         spin_unlock(&imp->imp_lock);
-
         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
 }
 
@@ -248,6 +296,37 @@ void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
         EXIT;
 }
 
+int ptlrpc_reconnect_import(struct obd_import *imp)
+{
+        
+        ptlrpc_set_import_discon(imp, 0); 
+        /* Force a new connect attempt */
+        ptlrpc_invalidate_import(imp);
+        /* Do a fresh connect next time by zeroing the handle */
+        ptlrpc_disconnect_import(imp, 1);
+        /* Wait for all invalidate calls to finish */
+        if (atomic_read(&imp->imp_inval_count) > 0) {
+                int rc;
+                struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
+                rc = l_wait_event(imp->imp_recovery_waitq,
+                                  (atomic_read(&imp->imp_inval_count) == 0),
+                                  &lwi);
+                if (rc)
+                        CERROR("Interrupted, inval=%d\n", 
+                               atomic_read(&imp->imp_inval_count));
+        }
+
+        /* Allow reconnect attempts */
+        imp->imp_obd->obd_no_recov = 0;
+        /* Remove 'invalid' flag */
+        ptlrpc_activate_import(imp);
+        /* Attempt a new connect */
+        ptlrpc_recover_import(imp, NULL);
+        return 0;
+}
+
+EXPORT_SYMBOL(ptlrpc_reconnect_import);
+
 static int import_select_connection(struct obd_import *imp)
 {
         struct obd_import_conn *imp_conn = NULL, *conn;
@@ -274,7 +353,7 @@ static int import_select_connection(struct obd_import *imp)
                                        cfs_time_current_64())) {
                         /* If we have never tried this connection since the
                            the last successful attempt, go with this one */
-                        if (cfs_time_before_64(conn->oic_last_attempt,
+                        if (cfs_time_beforeq_64(conn->oic_last_attempt,
                                                imp->imp_last_success_conn)) {
                                 imp_conn = conn;
                                 break;
@@ -330,26 +409,43 @@ static int import_select_connection(struct obd_import *imp)
         RETURN(0);
 }
 
+/*
+ * must be called under imp_lock
+ */
+int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
+{
+        struct ptlrpc_request *req;
+        struct list_head *tmp;
+
+        if (list_empty(&imp->imp_replay_list))
+                return 0;
+        tmp = imp->imp_replay_list.next;
+        req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
+        *transno = req->rq_transno;
+        if (req->rq_transno == 0) {
+                DEBUG_REQ(D_ERROR, req, "zero transno in replay");
+                LBUG();
+        }
+
+        return 1;
+}
+
 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
 {
         struct obd_device *obd = imp->imp_obd;
         int initial_connect = 0;
-        int rc;
+        int set_transno = 0;
         __u64 committed_before_reconnect = 0;
         struct ptlrpc_request *request;
-        int size[] = { sizeof(struct ptlrpc_body),
-                       sizeof(imp->imp_obd->u.cli.cl_target_uuid),
-                       sizeof(obd->obd_uuid),
-                       sizeof(imp->imp_dlm_handle),
-                       sizeof(imp->imp_connect_data) };
-        char *tmp[] = { NULL,
-                        obd2cli_tgt(imp->imp_obd),
-                        obd->obd_uuid.uuid,
-                        (char *)&imp->imp_dlm_handle,
-                        (char *)&imp->imp_connect_data };
+        char *bufs[] = { NULL,
+                         obd2cli_tgt(imp->imp_obd),
+                         obd->obd_uuid.uuid,
+                         (char *)&imp->imp_dlm_handle,
+                         (char *)&imp->imp_connect_data };
         struct ptlrpc_connect_async_args *aa;
-
+        int rc;
         ENTRY;
+
         spin_lock(&imp->imp_lock);
         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
                 spin_unlock(&imp->imp_lock);
@@ -375,6 +471,7 @@ int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
         else
                 committed_before_reconnect = imp->imp_peer_committed_transno;
 
+        set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
         spin_unlock(&imp->imp_lock);
 
         if (new_uuid) {
@@ -405,23 +502,35 @@ int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
                 if (imp->imp_recon_bk) {
                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
+                        spin_lock(&imp->imp_lock);
                         imp->imp_last_recon = 1;
+                        spin_unlock(&imp->imp_lock);
                 }
         }
 
+        rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
+        if (rc)
+                GOTO(out, rc);
+
         /* Reset connect flags to the originally requested flags, in case
          * the server is updated on-the-fly we will get the new features. */
         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
-        rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
+        rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
                            &obd->obd_uuid, &imp->imp_connect_data);
         if (rc)
                 GOTO(out, rc);
 
-        request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
-                                  5, size, tmp);
-        if (!request)
+        request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
+        if (request == NULL)
                 GOTO(out, rc = -ENOMEM);
 
+        rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
+                                      imp->imp_connect_op, bufs, NULL);
+        if (rc) {
+                ptlrpc_request_free(request);
+                GOTO(out, rc);
+        }
+
 #ifndef __KERNEL__
         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
 #endif
@@ -429,9 +538,9 @@ int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
 
         request->rq_send_state = LUSTRE_IMP_CONNECTING;
         /* Allow a slightly larger reply for future growth compatibility */
-        size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
-                              16 * sizeof(__u64);
-        ptlrpc_req_set_repsize(request, 2, size);
+        req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
+                             sizeof(struct obd_connect_data)+16*sizeof(__u64));
+        ptlrpc_request_set_replen(request);
         request->rq_interpret_reply = ptlrpc_connect_interpret;
 
         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
@@ -442,7 +551,9 @@ int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
         aa->pcaa_initial_connect = initial_connect;
 
         if (aa->pcaa_initial_connect) {
+                spin_lock(&imp->imp_lock);
                 imp->imp_replayable = 1;
+                spin_unlock(&imp->imp_lock);
                 /* On an initial connect, we don't know which one of a
                    failover server pair is up.  Don't wait long. */
 #ifdef CRAY_XT3
@@ -450,8 +561,14 @@ int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
 #else
                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
 #endif
+                lustre_msg_add_op_flags(request->rq_reqmsg, 
+                                        MSG_CONNECT_INITIAL);
         }
 
+        if (set_transno)
+                lustre_msg_add_op_flags(request->rq_reqmsg, 
+                                        MSG_CONNECT_TRANSNO);
+
         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
         ptlrpcd_add_req(request);
         rc = 0;
@@ -482,15 +599,20 @@ static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
                               struct obd_import_conn,
                               oic_item);
 
-        if (imp->imp_conn_current != imp_conn) {
+        /* XXX: When the failover node is the primary node, it is possible
+         * to have two identical connections in imp_conn_list. We must 
+         * compare not conn's pointers but NIDs, otherwise we can defeat
+         * connection throttling. (See bug 14774.) */
+        if (imp->imp_conn_current->oic_conn->c_self != 
+                                imp_conn->oic_conn->c_self) {
                 ptlrpc_ping_import_soon(imp);
                 wake_pinger = 1;
         }
-
 #else
         /* liblustre has no pinger thead, so we wakup pinger anyway */
         wake_pinger = 1;
 #endif 
+
  unlock:
         spin_unlock(&imp->imp_lock);
 
@@ -525,31 +647,38 @@ static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
 
         /* All imports are pingable */
+        spin_lock(&imp->imp_lock);
         imp->imp_pingable = 1;
 
         if (aa->pcaa_initial_connect) {
                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
+                        imp->imp_replayable = 1;
+                        spin_unlock(&imp->imp_lock);
                         CDEBUG(D_HA, "connected to replayable target: %s\n",
                                obd2cli_tgt(imp->imp_obd));
-                        imp->imp_replayable = 1;
                 } else {
                         imp->imp_replayable = 0;
+                        spin_unlock(&imp->imp_lock);
                 }
 
-                if (msg_flags & MSG_CONNECT_NEXT_VER) {
-                        imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
-                        CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
-                               obd2cli_tgt(imp->imp_obd));
-                } else {
-                        CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
-                               obd2cli_tgt(imp->imp_obd));
-                }
+                /* if applies, adjust the imp->imp_msg_magic here
+                 * according to reply flags */
 
                 imp->imp_remote_handle =
                                 *lustre_msg_get_handle(request->rq_repmsg);
 
                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
+                spin_lock(&imp->imp_lock);
+                if (imp->imp_invalid) {
+                        spin_unlock(&imp->imp_lock);
+                        ptlrpc_activate_import(imp);
+                } else {
+                        spin_unlock(&imp->imp_lock);
+                }
+
                 GOTO(finish, rc = 0);
+        } else {
+                spin_unlock(&imp->imp_lock);
         }
 
         /* Determine what recovery state to move the import to. */
@@ -567,22 +696,19 @@ static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
                 if (memcmp(&imp->imp_remote_handle,
                            lustre_msg_get_handle(request->rq_repmsg),
                            sizeof(imp->imp_remote_handle))) {
-                        int level = D_ERROR;
-                        /* Old MGC can reconnect to a restarted MGS */
-                        if (strcmp(imp->imp_obd->obd_type->typ_name,
-                                   LUSTRE_MGC_NAME) == 0) {
-                                level = D_CONFIG;
-                        }
-                        CDEBUG(level, 
-                               "%s@%s changed handle from "LPX64" to "LPX64
-                               "; copying, but this may foreshadow disaster\n",
+
+                        CWARN("%s@%s changed server handle from "
+                               LPX64" to "LPX64" - evicting.\n",
                                obd2cli_tgt(imp->imp_obd),
                                imp->imp_connection->c_remote_uuid.uuid,
                                imp->imp_remote_handle.cookie,
                                lustre_msg_get_handle(request->rq_repmsg)->
-                                        cookie);
+                                         cookie);
                         imp->imp_remote_handle =
                                      *lustre_msg_get_handle(request->rq_repmsg);
+
+                        IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
+                        GOTO(finish, rc = 0);
                 } else {
                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
                                obd2cli_tgt(imp->imp_obd),
@@ -595,7 +721,11 @@ static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
                                imp->imp_obd->obd_name,
                                obd2cli_tgt(imp->imp_obd));
+
+                        spin_lock(&imp->imp_lock);
                         imp->imp_resend_replay = 1;
+                        spin_unlock(&imp->imp_lock);
+
                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
                 } else {
                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
@@ -642,9 +772,12 @@ finish:
         } else {
                 struct obd_connect_data *ocd;
                 struct obd_export *exp;
-
-                ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
-                                         lustre_swab_connect);
+                int ret;
+                ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
+                                           RCL_SERVER);
+                /* server replied obd_connect_data is always bigger */
+                ocd = req_capsule_server_sized_get(&request->rq_pill,
+                                                   &RMF_CONNECT_DATA, ret);
 
                 spin_lock(&imp->imp_lock);
                 list_del(&imp->imp_conn_current->oic_item);
@@ -673,12 +806,13 @@ finish:
                 if (!exp) {
                         /* This could happen if export is cleaned during the 
                            connect attempt */
-                        spin_unlock(&imp->imp_lock);
                         CERROR("Missing export for %s\n", 
                                imp->imp_obd->obd_name);
                         GOTO(out, rc = -ENODEV);
                 }
                 exp->exp_connect_flags = ocd->ocd_connect_flags;
+                imp->imp_obd->obd_self_export->exp_connect_flags = 
+                                                        ocd->ocd_connect_flags;
                 class_export_put(exp);
 
                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
@@ -696,21 +830,55 @@ finish:
                         /* Sigh, some compilers do not like #ifdef in the middle
                            of macro arguments */
 #ifdef __KERNEL__
-                        const char *action = "upgrading this client";
+                        const char *older =
+                                "older. Consider upgrading this client";
 #else
-                        const char *action = "recompiling this application";
+                        const char *older =
+                                "older. Consider recompiling this application";
 #endif
+                        const char *newer = "newer than client version";
 
-                        CWARN("Server %s version (%d.%d.%d.%d) is much %s. "
-                              "Consider %s (%s).\n",
-                              obd2cli_tgt(imp->imp_obd),
-                              OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
-                              OBD_OCD_VERSION_MINOR(ocd->ocd_version),
-                              OBD_OCD_VERSION_PATCH(ocd->ocd_version),
-                              OBD_OCD_VERSION_FIX(ocd->ocd_version),
-                              ocd->ocd_version > LUSTRE_VERSION_CODE ?
-                              "newer" : "older",
-                              action, LUSTRE_VERSION_STRING);
+                        LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
+                                      "is much %s (%s)\n",
+                                      obd2cli_tgt(imp->imp_obd),
+                                      OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
+                                      OBD_OCD_VERSION_MINOR(ocd->ocd_version),
+                                      OBD_OCD_VERSION_PATCH(ocd->ocd_version),
+                                      OBD_OCD_VERSION_FIX(ocd->ocd_version),
+                                      ocd->ocd_version > LUSTRE_VERSION_CODE ?
+                                      newer : older, LUSTRE_VERSION_STRING);
+                }
+
+                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 & OBD_CKSUM_ALL) == 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,
+                                              OBD_CKSUM_ALL);
+                                cli->cl_checksum = 0;
+                                cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
+                                cli->cl_cksum_type = OBD_CKSUM_CRC32;
+                        } else {
+                                cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
+
+                                if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
+                                        cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
+                                else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
+                                        cli->cl_cksum_type = OBD_CKSUM_ADLER;
+                                else
+                                        cli->cl_cksum_type = OBD_CKSUM_CRC32;
+                        }
+                } else {
+                        /* The server does not support OBD_CONNECT_CKSUM.
+                         * Enforce CRC32 for backward compatibility*/
+                        cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
+                        cli->cl_cksum_type = OBD_CKSUM_CRC32;
                 }
 
                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
@@ -718,45 +886,61 @@ finish:
                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
                 }
 
+                imp->imp_obd->obd_namespace->ns_connect_flags = 
+                                                        ocd->ocd_connect_flags;
+                imp->imp_obd->obd_namespace->ns_orig_connect_flags = 
+                                                        ocd->ocd_connect_flags;
+
                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
                         (cli->cl_max_pages_per_rpc > 0));
         }
 
- out:
+out:
         if (rc != 0) {
                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
-                if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
-                        ptlrpc_deactivate_import(imp);
+                spin_lock(&imp->imp_lock);
+                if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
+                    (request->rq_import_generation == imp->imp_generation))
+                        ptlrpc_deactivate_and_unlock_import(imp);
+                else
+                        spin_unlock(&imp->imp_lock);
 
-                if (imp->imp_recon_bk && imp->imp_last_recon) {
-                        /* Give up trying to reconnect */
+                if ((imp->imp_recon_bk && imp->imp_last_recon) ||
+                    (rc == -EACCES)) {
+                        /*
+                         * Give up trying to reconnect
+                         * EACCES means client has no permission for connection
+                         */
                         imp->imp_obd->obd_no_recov = 1;
                         ptlrpc_deactivate_import(imp);
                 }
 
                 if (rc == -EPROTO) {
                         struct obd_connect_data *ocd;
-                        ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
-                                                 sizeof *ocd,
-                                                 lustre_swab_connect);
+
+                        /* reply message might not be ready */
+                        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 */
-                                CERROR("Server %s version (%d.%d.%d.%d) "
-                                       "refused connection from this client "
-                                       "as too %s version (%s).  Client must "
-                                       "be recompiled\n",
-                                      obd2cli_tgt(imp->imp_obd),
-                                      OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
-                                      OBD_OCD_VERSION_MINOR(ocd->ocd_version),
-                                      OBD_OCD_VERSION_PATCH(ocd->ocd_version),
-                                      OBD_OCD_VERSION_FIX(ocd->ocd_version),
-                                      ocd->ocd_version > LUSTRE_VERSION_CODE ?
-                                      "old" : "new",
-                                      LUSTRE_VERSION_STRING);
+                                LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
+                                        "(%d.%d.%d.%d)"
+                                        " refused connection from this client "
+                                        "with an incompatible version (%s).  "
+                                        "Client must be recompiled\n",
+                                        obd2cli_tgt(imp->imp_obd),
+                                        OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
+                                        OBD_OCD_VERSION_MINOR(ocd->ocd_version),
+                                        OBD_OCD_VERSION_PATCH(ocd->ocd_version),
+                                        OBD_OCD_VERSION_FIX(ocd->ocd_version),
+                                        LUSTRE_VERSION_STRING);
                                 ptlrpc_deactivate_import(imp);
                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
                         }
@@ -770,7 +954,9 @@ finish:
                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
         }
         
+        spin_lock(&imp->imp_lock);
         imp->imp_last_recon = 0;
+        spin_unlock(&imp->imp_lock);
 
         cfs_waitq_signal(&imp->imp_recovery_waitq);
         RETURN(rc);
@@ -801,15 +987,17 @@ static int signal_completed_replay(struct obd_import *imp)
         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
         atomic_inc(&imp->imp_replay_inflight);
 
-        req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
-        if (!req) {
+        req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
+                                        OBD_PING);
+        if (req == NULL) {
                 atomic_dec(&imp->imp_replay_inflight);
                 RETURN(-ENOMEM);
         }
 
-        ptlrpc_req_set_repsize(req, 1, NULL);
+        ptlrpc_request_set_replen(req);
         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
-        lustre_msg_add_flags(req->rq_reqmsg, MSG_LAST_REPLAY);
+        lustre_msg_add_flags(req->rq_reqmsg, 
+                             MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
         req->rq_timeout *= 3;
         req->rq_interpret_reply = completed_replay_interpret;
 
@@ -858,10 +1046,10 @@ int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
                 /* Don't care about MGC eviction */
                 if (strcmp(imp->imp_obd->obd_type->typ_name,
                            LUSTRE_MGC_NAME) != 0) {
-                        LCONSOLE_ERROR("This client was evicted by %.*s; "
-                                       "in progress operations using this "
-                                       "service will fail.\n",
-                                       target_len, target_start);
+                        LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
+                                           "%.*s; in progress operations using "
+                                           "this service will fail.\n",
+                                           target_len, target_start);
                 }
                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
                        obd2cli_tgt(imp->imp_obd),
@@ -869,7 +1057,7 @@ int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
 
 #ifdef __KERNEL__
                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
-                                   CLONE_VM | CLONE_FILES);
+                                       CLONE_VM | CLONE_FILES);
                 if (rc < 0)
                         CERROR("error starting invalidate thread: %d\n", rc);
                 else
@@ -936,7 +1124,7 @@ int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
                 ptlrpc_wake_delayed(imp);
         }
 
- out:
+out:
         RETURN(rc);
 }
 
@@ -949,8 +1137,12 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
 {
         struct ptlrpc_request *req;
         int rq_opc, rc = 0;
+        int nowait = imp->imp_obd->obd_force;
         ENTRY;
 
+        if (nowait)
+                GOTO(set_state, rc);
+
         switch (imp->imp_connect_op) {
         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
@@ -963,8 +1155,14 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
 
         if (ptlrpc_import_in_recovery(imp)) {
                 struct l_wait_info lwi;
-                cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
-
+                cfs_duration_t timeout;
+                if (imp->imp_server_timeout)
+                        timeout = cfs_time_seconds(obd_timeout / 2);
+                else
+                        timeout = cfs_time_seconds(obd_timeout);
+                
+                timeout = MAX(timeout * HZ, 1);
+                
                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
                 rc = l_wait_event(imp->imp_recovery_waitq,
@@ -978,7 +1176,8 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
 
         spin_unlock(&imp->imp_lock);
 
-        req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
+        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
@@ -991,11 +1190,12 @@ int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
 #endif
                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
-                ptlrpc_req_set_repsize(req, 1, NULL);
+                ptlrpc_request_set_replen(req);
                 rc = ptlrpc_queue_wait(req);
                 ptlrpc_req_finished(req);
         }
 
+set_state:
         spin_lock(&imp->imp_lock);
 out:
         if (noclose) 
@@ -1003,6 +1203,8 @@ out:
         else
                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
+        imp->imp_conn_cnt = 0;
+        imp->imp_last_recon = 0;
         spin_unlock(&imp->imp_lock);
 
         RETURN(rc);