Whamcloud - gitweb
merge b_devel into HEAD, which will become 0.7.3
[fs/lustre-release.git] / lustre / ptlrpc / recover.c
index a90df0e..70e9b5c 100644 (file)
 
 #define DEBUG_SUBSYSTEM S_RPC
 #ifdef __KERNEL__
-#include <linux/config.h>
-#include <linux/module.h>
-#include <linux/kmod.h>
+# include <linux/config.h>
+# include <linux/module.h>
+# include <linux/kmod.h>
 #else
-#include <liblustre.h>
+# include <liblustre.h>
 #endif
 
 #include <linux/obd_support.h>
 
 #include "ptlrpc_internal.h"
 
-int ptlrpc_reconnect_import(struct obd_import *imp,
-                            struct ptlrpc_request **reqptr)
+enum reconnect_result {
+        RECON_RESULT_RECOVERING  =  1,
+        RECON_RESULT_RECONNECTED  = 2,
+        RECON_RESULT_EVICTED      = 3,
+};
+
+int ptlrpc_reconnect_import(struct obd_import *imp)
 {
         struct obd_device *obd = imp->imp_obd;
-        int flags, rc, size[] = {sizeof(imp->imp_target_uuid),
+        int rc, size[] = {sizeof(imp->imp_target_uuid),
                                  sizeof(obd->obd_uuid),
                                  sizeof(imp->imp_dlm_handle)};
         char *tmp[] = {imp->imp_target_uuid.uuid,
@@ -55,12 +60,9 @@ int ptlrpc_reconnect_import(struct obd_import *imp,
         struct ptlrpc_connection *conn = imp->imp_connection;
         struct ptlrpc_request *req;
         struct lustre_handle old_hdl;
+        __u64 committed_before_reconnect = imp->imp_peer_committed_transno;
 
-        spin_lock_irqsave(&imp->imp_lock, flags);
-        imp->imp_generation++;
-        spin_unlock_irqrestore(&imp->imp_lock, flags);
-
-        CERROR("reconnect handle "LPX64"\n", 
+        CERROR("reconnect handle "LPX64"\n",
                imp->imp_dlm_handle.cookie);
 
         req = ptlrpc_prep_req(imp, imp->imp_connect_op, 3, size, tmp);
@@ -70,6 +72,7 @@ int ptlrpc_reconnect_import(struct obd_import *imp,
         req->rq_replen = lustre_msg_size(0, NULL);
         rc = ptlrpc_queue_wait(req);
         if (rc) {
+                /* what if rc > 0 ??*/
                 CERROR("cannot connect to %s@%s: rc = %d\n",
                        imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid, rc);
                 GOTO(out_disc, rc);
@@ -86,7 +89,7 @@ int ptlrpc_reconnect_import(struct obd_import *imp,
                         GOTO(out_disc, rc = -ENOTCONN);
                 }
 
-                if (memcmp(&imp->imp_remote_handle, &req->rq_repmsg->handle, 
+                if (memcmp(&imp->imp_remote_handle, &req->rq_repmsg->handle,
                            sizeof(imp->imp_remote_handle))) {
                         CERROR("%s@%s changed handle from "LPX64" to "LPX64
                                "; copying, but this may foreshadow disaster\n",
@@ -95,12 +98,17 @@ int ptlrpc_reconnect_import(struct obd_import *imp,
                                imp->imp_remote_handle.cookie,
                                req->rq_repmsg->handle.cookie);
                         imp->imp_remote_handle = req->rq_repmsg->handle;
-                        GOTO(out_disc, rc = 0);
+                        GOTO(out_disc, rc = RECON_RESULT_RECONNECTED);
                 }
 
                 CERROR("reconnected to %s@%s after partition\n",
                        imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid);
-                GOTO(out_disc, rc = 0);
+                GOTO(out_disc, rc = RECON_RESULT_RECONNECTED);
+        } else if (lustre_msg_get_op_flags(req->rq_repmsg) &
+                   MSG_CONNECT_RECOVERING) {
+                rc = RECON_RESULT_RECOVERING;
+        } else {
+                rc = RECON_RESULT_EVICTED;
         }
 
         old_hdl = imp->imp_remote_handle;
@@ -108,10 +116,19 @@ int ptlrpc_reconnect_import(struct obd_import *imp,
         CERROR("reconnected to %s@%s ("LPX64", was "LPX64")!\n",
                imp->imp_target_uuid.uuid, conn->c_remote_uuid.uuid,
                imp->imp_remote_handle.cookie, old_hdl.cookie);
-        GOTO(out_disc, rc = 0);
+        if (req->rq_repmsg->last_committed < committed_before_reconnect) {
+                CERROR("%s went back in time (transno "LPD64
+                       " was committed, server claims "LPD64
+                       ")! is shared storage not coherent?\n",
+                       imp->imp_target_uuid.uuid,
+                       imp->imp_peer_committed_transno,
+                       req->rq_repmsg->last_committed);
+        }
+
+        GOTO(out_disc, rc);
 
  out_disc:
-        *reqptr = req;
+        ptlrpc_req_finished(req);
         return rc;
 }
 
@@ -134,9 +151,9 @@ void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
         rc = USERMODEHELPER(argv[0], argv, envp);
         if (rc < 0) {
                 CERROR("Error invoking recovery upcall %s %s %s: %d; check "
-                       "/proc/sys/lustre/upcall\n",                
+                       "/proc/sys/lustre/upcall\n",
                        argv[0], argv[1], argv[2], rc);
-                
+
         } else {
                 CERROR("Invoked upcall %s %s %s",
                        argv[0], argv[1], argv[2]);
@@ -145,7 +162,7 @@ void ptlrpc_run_recovery_over_upcall(struct obd_device *obd)
 
 void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
 {
-        char *argv[6];
+        char *argv[7];
         char *envp[3];
         int rc;
 
@@ -153,9 +170,10 @@ void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
         argv[0] = obd_lustre_upcall;
         argv[1] = "FAILED_IMPORT";
         argv[2] = imp->imp_target_uuid.uuid;
-        argv[3] = imp->imp_obd->obd_uuid.uuid;
+        argv[3] = imp->imp_obd->obd_name;
         argv[4] = imp->imp_connection->c_remote_uuid.uuid;
-        argv[5] = NULL;
+        argv[5] = imp->imp_obd->obd_uuid.uuid;
+        argv[6] = NULL;
 
         envp[0] = "HOME=/";
         envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
@@ -163,10 +181,10 @@ void ptlrpc_run_failed_import_upcall(struct obd_import* imp)
 
         rc = USERMODEHELPER(argv[0], argv, envp);
         if (rc < 0) {
-                CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; check "
-                       "/proc/sys/lustre/lustre_upcall\n",                
+                CERROR("Error invoking recovery upcall %s %s %s %s %s: %d; "
+                       "check /proc/sys/lustre/lustre_upcall\n",
                        argv[0], argv[1], argv[2], argv[3], argv[4],rc);
-                
+
         } else {
                 CERROR("Invoked upcall %s %s %s %s %s\n",
                        argv[0], argv[1], argv[2], argv[3], argv[4]);
@@ -179,7 +197,6 @@ int ptlrpc_replay(struct obd_import *imp)
         struct list_head *tmp, *pos;
         struct ptlrpc_request *req;
         unsigned long flags;
-        __u64 committed = imp->imp_peer_committed_transno;
         ENTRY;
 
         /* It might have committed some after we last spoke, so make sure we
@@ -190,7 +207,7 @@ int ptlrpc_replay(struct obd_import *imp)
         spin_unlock_irqrestore(&imp->imp_lock, flags);
 
         CDEBUG(D_HA, "import %p from %s has committed "LPD64"\n",
-               imp, imp->imp_target_uuid.uuid, committed);
+               imp, imp->imp_target_uuid.uuid, imp->imp_peer_committed_transno);
 
         list_for_each(tmp, &imp->imp_replay_list) {
                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
@@ -204,7 +221,7 @@ int ptlrpc_replay(struct obd_import *imp)
          * than the one we're replaying (it can't be committed until it's
          * replayed, and we're doing that here).  l_f_e_safe protects against
          * problems with the current request being committed, in the unlikely
-         * event of that race.  So, in conclusion, I think that it's safe to 
+         * event of that race.  So, in conclusion, I think that it's safe to
          * perform this list-walk without the imp_lock held.
          *
          * But, the {mdc,osc}_replay_open callbacks both iterate
@@ -218,7 +235,7 @@ int ptlrpc_replay(struct obd_import *imp)
                 DEBUG_REQ(D_HA, req, "REPLAY:");
 
                 rc = ptlrpc_replay_req(req);
-        
+
                 if (rc) {
                         CERROR("recovery replay error %d for req "LPD64"\n",
                                rc, req->rq_xid);
@@ -241,8 +258,10 @@ int ptlrpc_resend(struct obd_import *imp)
          * list, so we don't need to hold the lock during this iteration and
          * resend process.
          */
+        /* Well... what if lctl recover is called twice at the same time?
+         */
         spin_lock_irqsave(&imp->imp_lock, flags);
-        LASSERT(imp->imp_level < LUSTRE_CONN_FULL);
+        LASSERT(imp->imp_level == LUSTRE_CONN_RECOVER);
         spin_unlock_irqrestore(&imp->imp_lock, flags);
 
         list_for_each_safe(tmp, pos, &imp->imp_sending_list) {
@@ -288,50 +307,45 @@ inline void ptlrpc_invalidate_import_state(struct obd_import *imp)
         ptlrpc_abort_inflight(imp);
 }
 
-int ptlrpc_request_handle_eviction(struct ptlrpc_request *failed_req)
+void ptlrpc_handle_failed_import(struct obd_import *imp)
 {
-        int rc = 0, in_recovery = 0;
-        struct obd_import *imp= failed_req->rq_import;
-        unsigned long flags;
-        struct ptlrpc_request *req;
-
-        spin_lock_irqsave(&imp->imp_lock, flags);
-
-        if (imp->imp_level == LUSTRE_CONN_NOTCONN)
-                in_recovery = 1;
-
-        if (failed_req->rq_import_generation == imp->imp_generation)
-                imp->imp_level = LUSTRE_CONN_NOTCONN;
-        else
-                in_recovery = 1;
+        ENTRY;
+        if (!imp->imp_replayable) {
+                CDEBUG(D_HA,
+                       "import %s@%s for %s not replayable, deactivating\n",
+                       imp->imp_target_uuid.uuid,
+                       imp->imp_connection->c_remote_uuid.uuid,
+                       imp->imp_obd->obd_name);
+                ptlrpc_set_import_active(imp, 0);
+        }
 
-        spin_unlock_irqrestore(&imp->imp_lock, flags);
+        ptlrpc_run_failed_import_upcall(imp);
+        EXIT;
+}
 
-        if (in_recovery) {
-                ptlrpc_resend_req(failed_req);
-                RETURN(rc);
-        }
+void ptlrpc_request_handle_eviction(struct ptlrpc_request *failed_req)
+{
+        int rc;
+        struct obd_import *imp= failed_req->rq_import;
+        unsigned long flags;
+        ENTRY;
 
         CDEBUG(D_HA, "import %s of %s@%s evicted: reconnecting\n",
                imp->imp_obd->obd_name,
                imp->imp_target_uuid.uuid,
                imp->imp_connection->c_remote_uuid.uuid);
-        rc = ptlrpc_reconnect_import(imp, &req);
+        rc = ptlrpc_recover_import(imp, NULL);
         if (rc) {
                 ptlrpc_resend_req(failed_req);
-                ptlrpc_fail_import(imp, imp->imp_generation);
+                if (rc != -EALREADY)
+                        ptlrpc_handle_failed_import(imp);
         } else {
+                LASSERT(failed_req->rq_import_generation < imp->imp_generation);
                 spin_lock_irqsave (&failed_req->rq_lock, flags);
                 failed_req->rq_err = 1;
                 spin_unlock_irqrestore (&failed_req->rq_lock, flags);
-                spin_lock_irqsave(&imp->imp_lock, flags);
-                imp->imp_level = LUSTRE_CONN_FULL;
-                imp->imp_invalid = 0;
-                spin_unlock_irqrestore(&imp->imp_lock, flags);
-                ptlrpc_invalidate_import_state(imp/*, req->rq_import_generation*/);
         }
-        ptlrpc_req_finished(req);
-        RETURN(rc);
+        EXIT;
 }
 
 int ptlrpc_set_import_active(struct obd_import *imp, int active)
@@ -344,17 +358,23 @@ int ptlrpc_set_import_active(struct obd_import *imp, int active)
 
         notify_obd = imp->imp_obd->u.cli.cl_containing_lov;
 
-        /* When deactivating, mark import invalid, and 
-           abort in-flight requests. */
+        /* When deactivating, mark import invalid, and abort in-flight
+         * requests. */
         if (!active) {
                 spin_lock_irqsave(&imp->imp_lock, flags);
-                imp->imp_invalid = 1;
+                /* This is a bit of a hack, but invalidating replayable
+                 * imports makes a temporary reconnect failure into a much more
+                 * ugly -- and hard to remedy -- situation. */
+                if (!imp->imp_replayable) {
+                        CDEBUG(D_HA, "setting import %s INVALID\n",
+                               imp->imp_target_uuid.uuid);
+                        imp->imp_invalid = 1;
+                }
+                imp->imp_generation++;
                 spin_unlock_irqrestore(&imp->imp_lock, flags);
-
-                ptlrpc_abort_inflight(imp);
-        } 
-
-        imp->imp_invalid = !active;
+                ptlrpc_invalidate_import_state(imp);
+                //ptlrpc_abort_inflight(imp);
+        }
 
         if (notify_obd == NULL)
                 GOTO(out, rc = 0);
@@ -386,7 +406,9 @@ int ptlrpc_set_import_active(struct obd_import *imp, int active)
 
 out:
         /* When activating, mark import valid */
-        if (active) {
+        if (active && !rc) {
+                CDEBUG(D_HA, "setting import %s VALID\n",
+                       imp->imp_target_uuid.uuid);
                 spin_lock_irqsave(&imp->imp_lock, flags);
                 imp->imp_invalid = 0;
                 spin_unlock_irqrestore(&imp->imp_lock, flags);
@@ -402,22 +424,12 @@ void ptlrpc_fail_import(struct obd_import *imp, int generation)
         ENTRY;
 
         LASSERT (!imp->imp_dlm_fake);
-        
-        /* If we were already in recovery, or if the import's connection to its
-         * service is newer than the failing operation's original attempt, then
-         * we don't want to recover again. */
-        spin_lock_irqsave(&imp->imp_lock, flags);
-
-        if (imp->imp_level == LUSTRE_CONN_RECOVD)
-                in_recovery = 1;
 
-        if (generation == imp->imp_generation) {
-                imp->imp_level = LUSTRE_CONN_RECOVD;
-                imp->imp_generation++;
-        } else {
+        spin_lock_irqsave(&imp->imp_lock, flags);
+        if (imp->imp_level != LUSTRE_CONN_FULL)
                 in_recovery = 1;
-        }
-
+        else
+                imp->imp_level = LUSTRE_CONN_NOTCONN;
         spin_unlock_irqrestore(&imp->imp_lock, flags);
 
         if (in_recovery) {
@@ -425,16 +437,7 @@ void ptlrpc_fail_import(struct obd_import *imp, int generation)
                 return;
         }
 
-        if (!imp->imp_replayable) {
-                CDEBUG(D_HA,
-                       "import %s@%s for %s not replayable, deactivating\n",
-                       imp->imp_target_uuid.uuid,
-                       imp->imp_connection->c_remote_uuid.uuid,
-                       imp->imp_obd->obd_name);
-                ptlrpc_set_import_active(imp, 0);
-        }
-
-        ptlrpc_run_failed_import_upcall(imp);
+        ptlrpc_handle_failed_import(imp);
         EXIT;
 }
 
@@ -449,7 +452,7 @@ static int signal_completed_replay(struct obd_import *imp)
                 RETURN(-ENOMEM);
 
         req->rq_replen = lustre_msg_size(0, NULL);
-        req->rq_level = LUSTRE_CONN_RECOVD;
+        req->rq_level = LUSTRE_CONN_RECOVER;
         req->rq_reqmsg->flags |= MSG_LAST_REPLAY;
 
         rc = ptlrpc_queue_wait(req);
@@ -460,18 +463,23 @@ static int signal_completed_replay(struct obd_import *imp)
 
 int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
 {
-        int msg_flags = 0, rc;
+        int rc;
         unsigned long flags;
-        struct ptlrpc_request *req;
+        int in_recover = 0;
+        int recon_result;
         ENTRY;
 
         spin_lock_irqsave(&imp->imp_lock, flags);
-        if (imp->imp_level == LUSTRE_CONN_FULL) {
-                imp->imp_level = LUSTRE_CONN_RECOVD;
-                imp->imp_generation++;
-        }
+        if (imp->imp_level == LUSTRE_CONN_FULL ||
+            imp->imp_level == LUSTRE_CONN_NOTCONN)
+                    imp->imp_level = LUSTRE_CONN_RECOVER;
+        else
+                in_recover = 1;
         spin_unlock_irqrestore(&imp->imp_lock, flags);
 
+        if (in_recover == 1)
+                RETURN(-EALREADY);
+
         if (new_uuid) {
                 struct ptlrpc_connection *conn;
                 struct obd_uuid uuid;
@@ -509,19 +517,19 @@ int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
 
         }
 
-        rc = ptlrpc_reconnect_import(imp, &req);
+        recon_result = ptlrpc_reconnect_import(imp);
 
-        if (rc) {
+        if (recon_result < 0) {
                 CERROR("failed to reconnect to %s@%s: %d\n",
                        imp->imp_target_uuid.uuid,
-                       imp->imp_connection->c_remote_uuid.uuid, rc);
-                RETURN(rc);
+                       imp->imp_connection->c_remote_uuid.uuid, recon_result);
+                spin_lock_irqsave(&imp->imp_lock, flags);
+                imp->imp_level = LUSTRE_CONN_NOTCONN;
+                spin_unlock_irqrestore(&imp->imp_lock, flags);
+                RETURN(recon_result);
         }
 
-        if (req->rq_repmsg)
-                msg_flags = lustre_msg_get_op_flags(req->rq_repmsg);
-
-        if (msg_flags & MSG_CONNECT_RECOVERING) {
+        if (recon_result == RECON_RESULT_RECOVERING) {
                 CDEBUG(D_HA, "replay requested by %s\n",
                        imp->imp_target_uuid.uuid);
                 rc = ptlrpc_replay(imp);
@@ -538,28 +546,31 @@ int ptlrpc_recover_import(struct obd_import *imp, char *new_uuid)
                 rc = signal_completed_replay(imp);
                 if (rc)
                         GOTO(out, rc);
-        } else if (msg_flags & MSG_CONNECT_RECONNECT) {
+        } else if (recon_result == RECON_RESULT_RECONNECTED) {
                 CDEBUG(D_HA, "reconnected to %s@%s\n",
                        imp->imp_target_uuid.uuid,
                        imp->imp_connection->c_remote_uuid.uuid);
-        } else {
+        } else if (recon_result == RECON_RESULT_EVICTED) {
                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
                        imp->imp_target_uuid.uuid,
                        imp->imp_connection->c_remote_uuid.uuid);
-                ptlrpc_invalidate_import_state(imp);
+                ptlrpc_set_import_active(imp, 0);
+//                ptlrpc_invalidate_import_state(imp);
+        } else {
+                LBUG();
         }
 
+        ptlrpc_set_import_active(imp, 1);
+
         rc = ptlrpc_resend(imp);
 
         spin_lock_irqsave(&imp->imp_lock, flags);
         imp->imp_level = LUSTRE_CONN_FULL;
-        imp->imp_invalid = 0;
         spin_unlock_irqrestore(&imp->imp_lock, flags);
 
         ptlrpc_wake_delayed(imp);
         EXIT;
  out:
-        ptlrpc_req_finished(req);
         return rc;
 }