Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ptlrpc / import.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Mike Shaver <shaver@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27 #ifndef __KERNEL__
28 # include <liblustre.h>
29 #endif
30
31 #include <obd_support.h>
32 #include <lustre_ha.h>
33 #include <lustre_net.h>
34 #include <lustre_import.h>
35 #include <lustre_export.h>
36 #include <obd.h>
37 #include <obd_class.h>
38
39 #include "ptlrpc_internal.h"
40
41 struct ptlrpc_connect_async_args {
42          __u64 pcaa_peer_committed;
43         int pcaa_initial_connect;
44 };
45
46 /* A CLOSED import should remain so. */
47 #define IMPORT_SET_STATE_NOLOCK(imp, state)                                    \
48 do {                                                                           \
49         if (imp->imp_state != LUSTRE_IMP_CLOSED) {                             \
50                CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",    \
51                       imp, obd2cli_tgt(imp->imp_obd),                          \
52                       ptlrpc_import_state_name(imp->imp_state),                \
53                       ptlrpc_import_state_name(state));                        \
54                imp->imp_state = state;                                         \
55         }                                                                      \
56 } while(0)
57
58 #define IMPORT_SET_STATE(imp, state)            \
59 do {                                            \
60         spin_lock(&imp->imp_lock);              \
61         IMPORT_SET_STATE_NOLOCK(imp, state);    \
62         spin_unlock(&imp->imp_lock);            \
63 } while(0)
64
65
66 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
67                                     void * data, int rc);
68 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
69
70 /* Only this function is allowed to change the import state when it is
71  * CLOSED. I would rather refcount the import and free it after
72  * disconnection like we do with exports. To do that, the client_obd
73  * will need to save the peer info somewhere other than in the import,
74  * though. */
75 int ptlrpc_init_import(struct obd_import *imp)
76 {
77         spin_lock(&imp->imp_lock);
78
79         imp->imp_generation++;
80         imp->imp_state =  LUSTRE_IMP_NEW;
81
82         spin_unlock(&imp->imp_lock);
83
84         return 0;
85 }
86 EXPORT_SYMBOL(ptlrpc_init_import);
87
88 #define UUID_STR "_UUID"
89 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
90                       int *uuid_len)
91 {
92         *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
93                 ? uuid : uuid + strlen(prefix);
94
95         *uuid_len = strlen(*uuid_start);
96
97         if (*uuid_len < strlen(UUID_STR))
98                 return;
99
100         if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
101                     UUID_STR, strlen(UUID_STR)))
102                 *uuid_len -= strlen(UUID_STR);
103 }
104
105 /* Returns true if import was FULL, false if import was already not
106  * connected.
107  * @imp - import to be disconnected
108  * @conn_cnt - connection count (epoch) of the request that timed out
109  *             and caused the disconnection.  In some cases, multiple
110  *             inflight requests can fail to a single target (e.g. OST
111  *             bulk requests) and if one has already caused a reconnection
112  *             (increasing the import->conn_cnt) the older failure should
113  *             not also cause a reconnection.  If zero it forces a reconnect.
114  */
115 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
116 {
117         int rc = 0;
118
119         spin_lock(&imp->imp_lock);
120
121         if (imp->imp_state == LUSTRE_IMP_FULL &&
122             (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
123                 char *target_start;
124                 int   target_len;
125
126                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
127                           &target_start, &target_len);
128                 if (imp->imp_replayable) {
129                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
130                                "%s was lost; in progress operations using this "
131                                "service will wait for recovery to complete.\n",
132                                imp->imp_obd->obd_name, target_len, target_start,
133                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
134                 } else {
135                         LCONSOLE_ERROR("%s: Connection to service %.*s via nid "
136                                "%s was lost; in progress operations using this "
137                                "service will fail.\n",
138                                imp->imp_obd->obd_name, target_len, target_start,
139                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
140                 }
141                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
142                 spin_unlock(&imp->imp_lock);
143     
144                 if (obd_dump_on_timeout)
145                         libcfs_debug_dumplog();
146
147                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
148                 rc = 1;
149         } else {
150                 spin_unlock(&imp->imp_lock);
151                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
152                        imp->imp_client->cli_name, imp,
153                        (imp->imp_state == LUSTRE_IMP_FULL &&
154                         imp->imp_conn_cnt > conn_cnt) ?
155                        "reconnected" : "not connected", imp->imp_conn_cnt,
156                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
157         }
158
159         return rc;
160 }
161
162 /*
163  * This acts as a barrier; all existing requests are rejected, and
164  * no new requests will be accepted until the import is valid again.
165  */
166 void ptlrpc_deactivate_import(struct obd_import *imp)
167 {
168         ENTRY;
169
170         spin_lock(&imp->imp_lock);
171         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
172         imp->imp_invalid = 1;
173         imp->imp_generation++;
174         spin_unlock(&imp->imp_lock);
175
176         ptlrpc_abort_inflight(imp);
177         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
178 }
179
180 /*
181  * This function will invalidate the import, if necessary, then block
182  * for all the RPC completions, and finally notify the obd to
183  * invalidate its state (ie cancel locks, clear pending requests,
184  * etc).
185  */
186 void ptlrpc_invalidate_import(struct obd_import *imp)
187 {
188         struct l_wait_info lwi;
189         int rc;
190
191         if (!imp->imp_invalid)
192                 ptlrpc_deactivate_import(imp);
193
194         LASSERT(imp->imp_invalid);
195
196         /* wait for all requests to error out and call completion callbacks */
197         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
198                                    HZ, NULL, NULL);
199         rc = l_wait_event(imp->imp_recovery_waitq,
200                           (atomic_read(&imp->imp_inflight) == 0),
201                           &lwi);
202
203         if (rc)
204                 CDEBUG(D_HA, "%s: rc = %d waiting for callback (%d != 0)\n",
205                        obd2cli_tgt(imp->imp_obd), rc,
206                        atomic_read(&imp->imp_inflight));
207
208         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
209 }
210
211 /* unset imp_invalid */
212 void ptlrpc_activate_import(struct obd_import *imp)
213 {
214         struct obd_device *obd = imp->imp_obd;
215
216         spin_lock(&imp->imp_lock);
217         imp->imp_invalid = 0;
218         spin_unlock(&imp->imp_lock);
219
220         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
221 }
222
223 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
224 {
225         ENTRY;
226
227         LASSERT(!imp->imp_dlm_fake);
228
229         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
230                 if (!imp->imp_replayable) {
231                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
232                                "auto-deactivating\n",
233                                obd2cli_tgt(imp->imp_obd),
234                                imp->imp_connection->c_remote_uuid.uuid,
235                                imp->imp_obd->obd_name);
236                         ptlrpc_deactivate_import(imp);
237                 }
238
239                 CDEBUG(D_HA, "%s: waking up pinger\n",
240                        obd2cli_tgt(imp->imp_obd));
241
242                 spin_lock(&imp->imp_lock);
243                 imp->imp_force_verify = 1;
244                 spin_unlock(&imp->imp_lock);
245
246                 ptlrpc_pinger_wake_up();
247         }
248         EXIT;
249 }
250
251 static int import_select_connection(struct obd_import *imp)
252 {
253         struct obd_import_conn *imp_conn = NULL, *conn;
254         struct obd_export *dlmexp;
255         ENTRY;
256
257         spin_lock(&imp->imp_lock);
258
259         if (list_empty(&imp->imp_conn_list)) {
260                 CERROR("%s: no connections available\n",
261                         imp->imp_obd->obd_name);
262                 spin_unlock(&imp->imp_lock);
263                 RETURN(-EINVAL);
264         }
265
266         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
267                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
268                        imp->imp_obd->obd_name,
269                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
270                        conn->oic_last_attempt);
271                 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
272                 if (cfs_time_before_64(conn->oic_last_attempt + 
273                                        RECONNECT_INTERVAL * HZ,
274                                        cfs_time_current_64())) {
275                         /* If we have never tried this connection since the
276                            the last successful attempt, go with this one */
277                         if (cfs_time_before_64(conn->oic_last_attempt,
278                                                imp->imp_last_success_conn)) {
279                                 imp_conn = conn;
280                                 break;
281                         }
282
283                         /* Both of these connections have already been tried
284                            since the last successful connection; just choose the
285                            least recently used */
286                         if (!imp_conn)
287                                 imp_conn = conn;
288                         else if (cfs_time_before_64(conn->oic_last_attempt,
289                                                     imp_conn->oic_last_attempt))
290                                 imp_conn = conn;
291                 }
292         }
293
294         /* if not found, simply choose the current one */
295         if (!imp_conn) {
296                 LASSERT(imp->imp_conn_current);
297                 imp_conn = imp->imp_conn_current;
298         }
299         LASSERT(imp_conn->oic_conn);
300
301         imp_conn->oic_last_attempt = cfs_time_current_64();
302
303         /* switch connection, don't mind if it's same as the current one */
304         if (imp->imp_connection)
305                 ptlrpc_put_connection(imp->imp_connection);
306         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
307
308         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
309         LASSERT(dlmexp != NULL);
310         if (dlmexp->exp_connection)
311                 ptlrpc_put_connection(dlmexp->exp_connection);
312         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
313         class_export_put(dlmexp);
314
315         if (imp->imp_conn_current != imp_conn) {
316                 if (imp->imp_conn_current)
317                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
318                                       imp->imp_obd->obd_name,
319                                       imp_conn->oic_uuid.uuid,
320                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
321                 imp->imp_conn_current = imp_conn;
322         }
323
324         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
325                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
326                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
327
328         spin_unlock(&imp->imp_lock);
329
330         RETURN(0);
331 }
332
333 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
334 {
335         struct obd_device *obd = imp->imp_obd;
336         int initial_connect = 0;
337         int rc;
338         __u64 committed_before_reconnect = 0;
339         struct ptlrpc_request *request;
340         int size[] = { sizeof(struct ptlrpc_body),
341                        sizeof(imp->imp_obd->u.cli.cl_target_uuid),
342                        sizeof(obd->obd_uuid),
343                        sizeof(imp->imp_dlm_handle),
344                        sizeof(imp->imp_connect_data) };
345         char *tmp[] = { NULL,
346                         obd2cli_tgt(imp->imp_obd),
347                         obd->obd_uuid.uuid,
348                         (char *)&imp->imp_dlm_handle,
349                         (char *)&imp->imp_connect_data };
350         struct ptlrpc_connect_async_args *aa;
351
352         ENTRY;
353         spin_lock(&imp->imp_lock);
354         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
355                 spin_unlock(&imp->imp_lock);
356                 CERROR("can't connect to a closed import\n");
357                 RETURN(-EINVAL);
358         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
359                 spin_unlock(&imp->imp_lock);
360                 CERROR("already connected\n");
361                 RETURN(0);
362         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
363                 spin_unlock(&imp->imp_lock);
364                 CERROR("already connecting\n");
365                 RETURN(-EALREADY);
366         }
367
368         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
369
370         imp->imp_conn_cnt++;
371         imp->imp_resend_replay = 0;
372
373         if (!lustre_handle_is_used(&imp->imp_remote_handle))
374                 initial_connect = 1;
375         else
376                 committed_before_reconnect = imp->imp_peer_committed_transno;
377
378         spin_unlock(&imp->imp_lock);
379
380         if (new_uuid) {
381                 struct obd_uuid uuid;
382
383                 obd_str2uuid(&uuid, new_uuid);
384                 rc = import_set_conn_priority(imp, &uuid);
385                 if (rc)
386                         GOTO(out, rc);
387         }
388
389         rc = import_select_connection(imp);
390         if (rc)
391                 GOTO(out, rc);
392
393         /* last in connection list */
394         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
395                 if (imp->imp_initial_recov_bk && initial_connect) {
396                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
397                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
398                         /* Don't retry if connect fails */
399                         rc = 0;
400                         obd_set_info_async(obd->obd_self_export,
401                                            strlen(KEY_INIT_RECOV),
402                                            KEY_INIT_RECOV,
403                                            sizeof(rc), &rc, NULL);
404                 }
405                 if (imp->imp_recon_bk) {
406                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
407                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
408                         spin_lock(&imp->imp_lock);
409                         imp->imp_last_recon = 1;
410                         spin_unlock(&imp->imp_lock);
411                 }
412         }
413
414         /* Reset connect flags to the originally requested flags, in case
415          * the server is updated on-the-fly we will get the new features. */
416         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
417         rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
418                            &obd->obd_uuid, &imp->imp_connect_data);
419         if (rc)
420                 GOTO(out, rc);
421
422         request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
423                                   5, size, tmp);
424         if (!request)
425                 GOTO(out, rc = -ENOMEM);
426
427 #ifndef __KERNEL__
428         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
429 #endif
430         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
431
432         request->rq_send_state = LUSTRE_IMP_CONNECTING;
433         /* Allow a slightly larger reply for future growth compatibility */
434         size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
435                               16 * sizeof(__u64);
436         ptlrpc_req_set_repsize(request, 2, size);
437         request->rq_interpret_reply = ptlrpc_connect_interpret;
438
439         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
440         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
441         memset(aa, 0, sizeof *aa);
442
443         aa->pcaa_peer_committed = committed_before_reconnect;
444         aa->pcaa_initial_connect = initial_connect;
445
446         if (aa->pcaa_initial_connect) {
447                 spin_lock(&imp->imp_lock);
448                 imp->imp_replayable = 1;
449                 spin_unlock(&imp->imp_lock);
450                 /* On an initial connect, we don't know which one of a
451                    failover server pair is up.  Don't wait long. */
452 #ifdef CRAY_XT3
453                 request->rq_timeout = max((int)(obd_timeout / 2), 5);
454 #else
455                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
456 #endif
457         }
458
459         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
460         ptlrpcd_add_req(request);
461         rc = 0;
462 out:
463         if (rc != 0) {
464                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
465         }
466
467         RETURN(rc);
468 }
469 EXPORT_SYMBOL(ptlrpc_connect_import);
470
471 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
472 {
473 #ifdef __KERNEL__
474         struct obd_import_conn *imp_conn;
475 #endif
476         int wake_pinger = 0;
477
478         ENTRY;
479
480         spin_lock(&imp->imp_lock);
481         if (list_empty(&imp->imp_conn_list))
482                 GOTO(unlock, 0);
483
484 #ifdef __KERNEL__
485         imp_conn = list_entry(imp->imp_conn_list.prev,
486                               struct obd_import_conn,
487                               oic_item);
488
489         if (imp->imp_conn_current != imp_conn) {
490                 ptlrpc_ping_import_soon(imp);
491                 wake_pinger = 1;
492         }
493
494 #else
495         /* liblustre has no pinger thead, so we wakup pinger anyway */
496         wake_pinger = 1;
497 #endif 
498  unlock:
499         spin_unlock(&imp->imp_lock);
500
501         if (wake_pinger)
502                 ptlrpc_pinger_wake_up();
503
504         EXIT;
505 }
506
507 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
508                                     void * data, int rc)
509 {
510         struct ptlrpc_connect_async_args *aa = data;
511         struct obd_import *imp = request->rq_import;
512         struct client_obd *cli = &imp->imp_obd->u.cli;
513         struct lustre_handle old_hdl;
514         int msg_flags;
515         ENTRY;
516
517         spin_lock(&imp->imp_lock);
518         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
519                 spin_unlock(&imp->imp_lock);
520                 RETURN(0);
521         }
522
523         if (rc)
524                 GOTO(out, rc);
525
526         LASSERT(imp->imp_conn_current);
527
528         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
529
530         /* All imports are pingable */
531         imp->imp_pingable = 1;
532
533         if (aa->pcaa_initial_connect) {
534                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
535                         imp->imp_replayable = 1;
536                         spin_unlock(&imp->imp_lock);
537                         CDEBUG(D_HA, "connected to replayable target: %s\n",
538                                obd2cli_tgt(imp->imp_obd));
539                 } else {
540                         imp->imp_replayable = 0;
541                         spin_unlock(&imp->imp_lock);
542                 }
543
544                 if (msg_flags & MSG_CONNECT_NEXT_VER) {
545                         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
546                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
547                                obd2cli_tgt(imp->imp_obd));
548                 } else {
549                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
550                                obd2cli_tgt(imp->imp_obd));
551                 }
552
553                 imp->imp_remote_handle =
554                                 *lustre_msg_get_handle(request->rq_repmsg);
555
556                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
557                 GOTO(finish, rc = 0);
558         } else {
559                 spin_unlock(&imp->imp_lock);
560         }
561
562         /* Determine what recovery state to move the import to. */
563         if (MSG_CONNECT_RECONNECT & msg_flags) {
564                 memset(&old_hdl, 0, sizeof(old_hdl));
565                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
566                             sizeof (old_hdl))) {
567                         CERROR("%s@%s didn't like our handle "LPX64
568                                ", failed\n", obd2cli_tgt(imp->imp_obd),
569                                imp->imp_connection->c_remote_uuid.uuid,
570                                imp->imp_dlm_handle.cookie);
571                         GOTO(out, rc = -ENOTCONN);
572                 }
573
574                 if (memcmp(&imp->imp_remote_handle,
575                            lustre_msg_get_handle(request->rq_repmsg),
576                            sizeof(imp->imp_remote_handle))) {
577                         int level = D_ERROR;
578                         /* Old MGC can reconnect to a restarted MGS */
579                         if (strcmp(imp->imp_obd->obd_type->typ_name,
580                                    LUSTRE_MGC_NAME) == 0) {
581                                 level = D_CONFIG;
582                         }
583                         CDEBUG(level, 
584                                "%s@%s changed handle from "LPX64" to "LPX64
585                                "; copying, but this may foreshadow disaster\n",
586                                obd2cli_tgt(imp->imp_obd),
587                                imp->imp_connection->c_remote_uuid.uuid,
588                                imp->imp_remote_handle.cookie,
589                                lustre_msg_get_handle(request->rq_repmsg)->
590                                         cookie);
591                         imp->imp_remote_handle =
592                                      *lustre_msg_get_handle(request->rq_repmsg);
593                 } else {
594                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
595                                obd2cli_tgt(imp->imp_obd),
596                                imp->imp_connection->c_remote_uuid.uuid);
597                 }
598
599                 if (imp->imp_invalid) {
600                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
601                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
602                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
603                                imp->imp_obd->obd_name,
604                                obd2cli_tgt(imp->imp_obd));
605
606                         spin_lock(&imp->imp_lock);
607                         imp->imp_resend_replay = 1;
608                         spin_unlock(&imp->imp_lock);
609
610                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
611                 } else {
612                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
613                 }
614         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
615                 LASSERT(imp->imp_replayable);
616                 imp->imp_remote_handle =
617                                 *lustre_msg_get_handle(request->rq_repmsg);
618                 imp->imp_last_replay_transno = 0;
619                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
620         } else {
621                 imp->imp_remote_handle =
622                                 *lustre_msg_get_handle(request->rq_repmsg);
623                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
624         }
625
626         /* Sanity checks for a reconnected import. */
627         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
628                 CERROR("imp_replayable flag does not match server "
629                        "after reconnect. We should LBUG right here.\n");
630         }
631
632         if (lustre_msg_get_last_committed(request->rq_repmsg) <
633             aa->pcaa_peer_committed) {
634                 CERROR("%s went back in time (transno "LPD64
635                        " was previously committed, server now claims "LPD64
636                        ")!  See https://bugzilla.clusterfs.com/"
637                        "long_list.cgi?buglist=9646\n",
638                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
639                        lustre_msg_get_last_committed(request->rq_repmsg));
640         }
641
642 finish:
643         rc = ptlrpc_import_recovery_state_machine(imp);
644         if (rc != 0) {
645                 if (rc == -ENOTCONN) {
646                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
647                                "invalidating and reconnecting\n",
648                                obd2cli_tgt(imp->imp_obd),
649                                imp->imp_connection->c_remote_uuid.uuid);
650                         ptlrpc_connect_import(imp, NULL);
651                         RETURN(0);
652                 }
653         } else {
654                 struct obd_connect_data *ocd;
655                 struct obd_export *exp;
656
657                 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
658                                          lustre_swab_connect);
659
660                 spin_lock(&imp->imp_lock);
661                 list_del(&imp->imp_conn_current->oic_item);
662                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
663                 imp->imp_last_success_conn =
664                         imp->imp_conn_current->oic_last_attempt;
665
666                 if (ocd == NULL) {
667                         spin_unlock(&imp->imp_lock);
668                         CERROR("Wrong connect data from server\n");
669                         rc = -EPROTO;
670                         GOTO(out, rc);
671                 }
672
673                 imp->imp_connect_data = *ocd;
674
675                 exp = class_conn2export(&imp->imp_dlm_handle);
676                 spin_unlock(&imp->imp_lock);
677
678                 /* check that server granted subset of flags we asked for. */
679                 LASSERTF((ocd->ocd_connect_flags &
680                           imp->imp_connect_flags_orig) ==
681                          ocd->ocd_connect_flags, LPX64" != "LPX64,
682                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
683
684                 if (!exp) {
685                         /* This could happen if export is cleaned during the 
686                            connect attempt */
687                         spin_unlock(&imp->imp_lock);
688                         CERROR("Missing export for %s\n", 
689                                imp->imp_obd->obd_name);
690                         GOTO(out, rc = -ENODEV);
691                 }
692                 exp->exp_connect_flags = ocd->ocd_connect_flags;
693                 class_export_put(exp);
694
695                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
696
697                 if (!ocd->ocd_ibits_known &&
698                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
699                         CERROR("Inodebits aware server returned zero compatible"
700                                " bits?\n");
701
702                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
703                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
704                                         LUSTRE_VERSION_OFFSET_WARN ||
705                      ocd->ocd_version < LUSTRE_VERSION_CODE -
706                                         LUSTRE_VERSION_OFFSET_WARN)) {
707                         /* Sigh, some compilers do not like #ifdef in the middle
708                            of macro arguments */
709 #ifdef __KERNEL__
710                         const char *older =
711                                 "older.  Consider upgrading this client";
712 #else
713                         const char *older =
714                                 "older.  Consider recompiling this application";
715 #endif
716                         const char *newer = "newer than client version";
717
718                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
719                                       "is much %s (%s)\n",
720                                       obd2cli_tgt(imp->imp_obd),
721                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
722                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
723                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
724                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
725                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
726                                       newer : older, LUSTRE_VERSION_STRING);
727                 }
728
729                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
730                         cli->cl_max_pages_per_rpc = 
731                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
732                 }
733
734                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
735                         (cli->cl_max_pages_per_rpc > 0));
736         }
737
738  out:
739         if (rc != 0) {
740                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
741                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
742                         ptlrpc_deactivate_import(imp);
743
744                 if (imp->imp_recon_bk && imp->imp_last_recon) {
745                         /* Give up trying to reconnect */
746                         imp->imp_obd->obd_no_recov = 1;
747                         ptlrpc_deactivate_import(imp);
748                 }
749
750                 if (rc == -EPROTO) {
751                         struct obd_connect_data *ocd;
752                         ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
753                                                  sizeof *ocd,
754                                                  lustre_swab_connect);
755                         if (ocd &&
756                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
757                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
758                            /* Actually servers are only supposed to refuse
759                               connection from liblustre clients, so we should
760                               never see this from VFS context */
761                                 LCONSOLE_ERROR("Server %s version (%d.%d.%d.%d)"
762                                         " refused connection from this client "
763                                         "with an incompatible version (%s).  "
764                                         "Client must be recompiled\n",
765                                         obd2cli_tgt(imp->imp_obd),
766                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
767                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
768                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
769                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
770                                         LUSTRE_VERSION_STRING);
771                                 ptlrpc_deactivate_import(imp);
772                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
773                         }
774                         RETURN(-EPROTO);
775                 }
776
777                 ptlrpc_maybe_ping_import_soon(imp);
778
779                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
780                        obd2cli_tgt(imp->imp_obd),
781                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
782         }
783         
784         spin_lock(&imp->imp_lock);
785         imp->imp_last_recon = 0;
786         spin_unlock(&imp->imp_lock);
787
788         cfs_waitq_signal(&imp->imp_recovery_waitq);
789         RETURN(rc);
790 }
791
792 static int completed_replay_interpret(struct ptlrpc_request *req,
793                                     void * data, int rc)
794 {
795         ENTRY;
796         atomic_dec(&req->rq_import->imp_replay_inflight);
797         if (req->rq_status == 0) {
798                 ptlrpc_import_recovery_state_machine(req->rq_import);
799         } else {
800                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
801                        "reconnecting\n",
802                        req->rq_import->imp_obd->obd_name, req->rq_status);
803                 ptlrpc_connect_import(req->rq_import, NULL);
804         }
805
806         RETURN(0);
807 }
808
809 static int signal_completed_replay(struct obd_import *imp)
810 {
811         struct ptlrpc_request *req;
812         ENTRY;
813
814         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
815         atomic_inc(&imp->imp_replay_inflight);
816
817         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
818         if (!req) {
819                 atomic_dec(&imp->imp_replay_inflight);
820                 RETURN(-ENOMEM);
821         }
822
823         ptlrpc_req_set_repsize(req, 1, NULL);
824         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
825         lustre_msg_add_flags(req->rq_reqmsg, MSG_LAST_REPLAY);
826         req->rq_timeout *= 3;
827         req->rq_interpret_reply = completed_replay_interpret;
828
829         ptlrpcd_add_req(req);
830         RETURN(0);
831 }
832
833 #ifdef __KERNEL__
834 static int ptlrpc_invalidate_import_thread(void *data)
835 {
836         struct obd_import *imp = data;
837
838         ENTRY;
839
840         ptlrpc_daemonize("ll_imp_inval");
841         
842         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
843                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
844                imp->imp_connection->c_remote_uuid.uuid);
845
846         ptlrpc_invalidate_import(imp);
847
848         if (obd_dump_on_eviction) {
849                 CERROR("dump the log upon eviction\n");
850                 libcfs_debug_dumplog();
851         }
852
853         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
854         ptlrpc_import_recovery_state_machine(imp);
855
856         RETURN(0);
857 }
858 #endif
859
860 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
861 {
862         int rc = 0;
863         int inflight;
864         char *target_start;
865         int target_len;
866
867         ENTRY;
868         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
869                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
870                           &target_start, &target_len);
871                 /* Don't care about MGC eviction */
872                 if (strcmp(imp->imp_obd->obd_type->typ_name,
873                            LUSTRE_MGC_NAME) != 0) {
874                         LCONSOLE_ERROR("This client was evicted by %.*s; "
875                                        "in progress operations using this "
876                                        "service will fail.\n",
877                                        target_len, target_start);
878                 }
879                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
880                        obd2cli_tgt(imp->imp_obd),
881                        imp->imp_connection->c_remote_uuid.uuid);
882
883 #ifdef __KERNEL__
884                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
885                                    CLONE_VM | CLONE_FILES);
886                 if (rc < 0)
887                         CERROR("error starting invalidate thread: %d\n", rc);
888                 else
889                         rc = 0;
890                 RETURN(rc);
891 #else
892                 ptlrpc_invalidate_import(imp);
893
894                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
895 #endif
896         }
897
898         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
899                 CDEBUG(D_HA, "replay requested by %s\n",
900                        obd2cli_tgt(imp->imp_obd));
901                 rc = ptlrpc_replay_next(imp, &inflight);
902                 if (inflight == 0 &&
903                     atomic_read(&imp->imp_replay_inflight) == 0) {
904                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
905                         rc = ldlm_replay_locks(imp);
906                         if (rc)
907                                 GOTO(out, rc);
908                 }
909                 rc = 0;
910         }
911
912         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
913                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
914                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
915                         rc = signal_completed_replay(imp);
916                         if (rc)
917                                 GOTO(out, rc);
918                 }
919
920         }
921
922         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
923                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
924                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
925                 }
926         }
927
928         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
929                 CDEBUG(D_HA, "reconnected to %s@%s\n",
930                        obd2cli_tgt(imp->imp_obd),
931                        imp->imp_connection->c_remote_uuid.uuid);
932
933                 rc = ptlrpc_resend(imp);
934                 if (rc)
935                         GOTO(out, rc);
936                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
937                 ptlrpc_activate_import(imp);
938
939                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
940                           &target_start, &target_len);
941                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
942                               "using nid %s.\n", imp->imp_obd->obd_name,
943                               target_len, target_start,
944                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
945         }
946
947         if (imp->imp_state == LUSTRE_IMP_FULL) {
948                 cfs_waitq_signal(&imp->imp_recovery_waitq);
949                 ptlrpc_wake_delayed(imp);
950         }
951
952  out:
953         RETURN(rc);
954 }
955
956 static int back_to_sleep(void *unused)
957 {
958         return 0;
959 }
960
961 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
962 {
963         struct ptlrpc_request *req;
964         int rq_opc, rc = 0;
965         ENTRY;
966
967         switch (imp->imp_connect_op) {
968         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
969         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
970         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
971         default:
972                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
973                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
974                 RETURN(-EINVAL);
975         }
976
977         if (ptlrpc_import_in_recovery(imp)) {
978                 struct l_wait_info lwi;
979                 cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
980
981                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
982                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
983                 rc = l_wait_event(imp->imp_recovery_waitq,
984                                   !ptlrpc_import_in_recovery(imp), &lwi);
985
986         }
987
988         spin_lock(&imp->imp_lock);
989         if (imp->imp_state != LUSTRE_IMP_FULL)
990                 GOTO(out, 0);
991
992         spin_unlock(&imp->imp_lock);
993
994         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
995         if (req) {
996                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
997                  * it fails.  We can get through the above with a down server
998                  * if the client doesn't know the server is gone yet. */
999                 req->rq_no_resend = 1;
1000 #ifdef CRAY_XT3
1001                 req->rq_timeout = obd_timeout / 3;
1002 #else
1003                 req->rq_timeout = 5;
1004 #endif
1005                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1006                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1007                 ptlrpc_req_set_repsize(req, 1, NULL);
1008                 rc = ptlrpc_queue_wait(req);
1009                 ptlrpc_req_finished(req);
1010         }
1011
1012         spin_lock(&imp->imp_lock);
1013 out:
1014         if (noclose) 
1015                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1016         else
1017                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1018         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1019         spin_unlock(&imp->imp_lock);
1020
1021         RETURN(rc);
1022 }
1023