Whamcloud - gitweb
5082bc5b60c4ec69a760fc381b4afaa932fb4d5a
[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
129                 if (imp->imp_replayable) {
130                         LCONSOLE_WARN("%s: Connection to service %.*s via nid "
131                                "%s was lost; in progress operations using this "
132                                "service will wait for recovery to complete.\n",
133                                imp->imp_obd->obd_name, target_len, target_start,
134                                libcfs_nid2str(imp->imp_connection->c_peer.nid));
135                 } else {
136                         LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
137                                            "%.*s via nid %s was lost; in progress"
138                                            "operations using this service will"
139                                            "fail.\n",
140                                            imp->imp_obd->obd_name,
141                                            target_len, target_start,
142                                  libcfs_nid2str(imp->imp_connection->c_peer.nid));
143                 }
144                 ptlrpc_deactivate_timeouts(imp);
145                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
146                 spin_unlock(&imp->imp_lock);
147     
148                 if (obd_dump_on_timeout)
149                         libcfs_debug_dumplog();
150
151                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
152                 rc = 1;
153         } else {
154                 spin_unlock(&imp->imp_lock);
155                 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
156                        imp->imp_client->cli_name, imp,
157                        (imp->imp_state == LUSTRE_IMP_FULL &&
158                         imp->imp_conn_cnt > conn_cnt) ?
159                        "reconnected" : "not connected", imp->imp_conn_cnt,
160                        conn_cnt, ptlrpc_import_state_name(imp->imp_state));
161         }
162
163         return rc;
164 }
165
166 /*
167  * This acts as a barrier; all existing requests are rejected, and
168  * no new requests will be accepted until the import is valid again.
169  */
170 void ptlrpc_deactivate_import(struct obd_import *imp)
171 {
172         ENTRY;
173
174         spin_lock(&imp->imp_lock);
175         if (imp->imp_invalid) {
176                 spin_unlock(&imp->imp_lock);
177                 EXIT;
178                 return;
179         }
180
181         CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
182         imp->imp_invalid = 1;
183         imp->imp_generation++;
184         spin_unlock(&imp->imp_lock);
185
186         ptlrpc_abort_inflight(imp);
187         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
188
189         EXIT;
190 }
191
192 /*
193  * This function will invalidate the import, if necessary, then block
194  * for all the RPC completions, and finally notify the obd to
195  * invalidate its state (ie cancel locks, clear pending requests,
196  * etc).
197  */
198 void ptlrpc_invalidate_import(struct obd_import *imp)
199 {
200         struct l_wait_info lwi;
201         int rc;
202
203         atomic_inc(&imp->imp_inval_count);
204
205         ptlrpc_deactivate_import(imp);
206
207         LASSERT(imp->imp_invalid);
208
209         /* wait for all requests to error out and call completion callbacks */
210         lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)), 
211                                    HZ, NULL, NULL);
212         rc = l_wait_event(imp->imp_recovery_waitq,
213                           (atomic_read(&imp->imp_inflight) == 0), &lwi);
214
215         if (rc)
216                 CDEBUG(D_HA, "%s: rc = %d waiting for callback (%d != 0)\n",
217                        obd2cli_tgt(imp->imp_obd), rc,
218                        atomic_read(&imp->imp_inflight));
219
220         obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
221         sptlrpc_import_flush_all_ctx(imp);
222
223         atomic_dec(&imp->imp_inval_count);
224         cfs_waitq_signal(&imp->imp_recovery_waitq);
225 }
226
227 /* unset imp_invalid */
228 void ptlrpc_activate_import(struct obd_import *imp)
229 {
230         struct obd_device *obd = imp->imp_obd;
231
232         spin_lock(&imp->imp_lock);
233         imp->imp_invalid = 0;
234         ptlrpc_activate_timeouts(imp);
235         spin_unlock(&imp->imp_lock);
236         obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
237 }
238
239 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
240 {
241         ENTRY;
242
243         LASSERT(!imp->imp_dlm_fake);
244
245         if (ptlrpc_set_import_discon(imp, conn_cnt)) {
246                 if (!imp->imp_replayable) {
247                         CDEBUG(D_HA, "import %s@%s for %s not replayable, "
248                                "auto-deactivating\n",
249                                obd2cli_tgt(imp->imp_obd),
250                                imp->imp_connection->c_remote_uuid.uuid,
251                                imp->imp_obd->obd_name);
252                         ptlrpc_deactivate_import(imp);
253                 }
254
255                 CDEBUG(D_HA, "%s: waking up pinger\n",
256                        obd2cli_tgt(imp->imp_obd));
257
258                 spin_lock(&imp->imp_lock);
259                 imp->imp_force_verify = 1;
260                 spin_unlock(&imp->imp_lock);
261
262                 ptlrpc_pinger_wake_up();
263         }
264         EXIT;
265 }
266
267 static int import_select_connection(struct obd_import *imp)
268 {
269         struct obd_import_conn *imp_conn = NULL, *conn;
270         struct obd_export *dlmexp;
271         ENTRY;
272
273         spin_lock(&imp->imp_lock);
274
275         if (list_empty(&imp->imp_conn_list)) {
276                 CERROR("%s: no connections available\n",
277                         imp->imp_obd->obd_name);
278                 spin_unlock(&imp->imp_lock);
279                 RETURN(-EINVAL);
280         }
281
282         list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
283                 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
284                        imp->imp_obd->obd_name,
285                        libcfs_nid2str(conn->oic_conn->c_peer.nid),
286                        conn->oic_last_attempt);
287                 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
288                 if (cfs_time_before_64(conn->oic_last_attempt + 
289                                        RECONNECT_INTERVAL * HZ,
290                                        cfs_time_current_64())) {
291                         /* If we have never tried this connection since the
292                            the last successful attempt, go with this one */
293                         if (cfs_time_beforeq_64(conn->oic_last_attempt,
294                                                imp->imp_last_success_conn)) {
295                                 imp_conn = conn;
296                                 break;
297                         }
298
299                         /* Both of these connections have already been tried
300                            since the last successful connection; just choose the
301                            least recently used */
302                         if (!imp_conn)
303                                 imp_conn = conn;
304                         else if (cfs_time_before_64(conn->oic_last_attempt,
305                                                     imp_conn->oic_last_attempt))
306                                 imp_conn = conn;
307                 }
308         }
309
310         /* if not found, simply choose the current one */
311         if (!imp_conn) {
312                 LASSERT(imp->imp_conn_current);
313                 imp_conn = imp->imp_conn_current;
314         }
315         LASSERT(imp_conn->oic_conn);
316
317         imp_conn->oic_last_attempt = cfs_time_current_64();
318
319         /* switch connection, don't mind if it's same as the current one */
320         if (imp->imp_connection)
321                 ptlrpc_put_connection(imp->imp_connection);
322         imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
323
324         dlmexp =  class_conn2export(&imp->imp_dlm_handle);
325         LASSERT(dlmexp != NULL);
326         if (dlmexp->exp_connection)
327                 ptlrpc_put_connection(dlmexp->exp_connection);
328         dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
329         class_export_put(dlmexp);
330
331         if (imp->imp_conn_current != imp_conn) {
332                 if (imp->imp_conn_current)
333                         LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
334                                       imp->imp_obd->obd_name,
335                                       imp_conn->oic_uuid.uuid,
336                                       libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
337                 imp->imp_conn_current = imp_conn;
338         }
339
340         CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
341                imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
342                libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
343
344         spin_unlock(&imp->imp_lock);
345
346         RETURN(0);
347 }
348
349 /*
350  * must be called under imp_lock
351  */
352 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
353 {
354         struct ptlrpc_request *req;
355         struct list_head *tmp;
356
357         if (list_empty(&imp->imp_replay_list))
358                 return 0;
359         tmp = imp->imp_replay_list.next;
360         req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
361         *transno = req->rq_transno;
362         if (req->rq_transno == 0) {
363                 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
364                 LBUG();
365         }
366
367         return 1;
368 }
369
370 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
371 {
372         struct obd_device *obd = imp->imp_obd;
373         int initial_connect = 0;
374         int set_transno = 0;
375         __u64 committed_before_reconnect = 0;
376         struct ptlrpc_request *request;
377         char *bufs[] = { NULL,
378                          obd2cli_tgt(imp->imp_obd),
379                          obd->obd_uuid.uuid,
380                          (char *)&imp->imp_dlm_handle,
381                          (char *)&imp->imp_connect_data };
382         struct ptlrpc_connect_async_args *aa;
383         int rc;
384         ENTRY;
385
386         spin_lock(&imp->imp_lock);
387         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
388                 spin_unlock(&imp->imp_lock);
389                 CERROR("can't connect to a closed import\n");
390                 RETURN(-EINVAL);
391         } else if (imp->imp_state == LUSTRE_IMP_FULL) {
392                 spin_unlock(&imp->imp_lock);
393                 CERROR("already connected\n");
394                 RETURN(0);
395         } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
396                 spin_unlock(&imp->imp_lock);
397                 CERROR("already connecting\n");
398                 RETURN(-EALREADY);
399         }
400
401         IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
402
403         imp->imp_conn_cnt++;
404         imp->imp_resend_replay = 0;
405
406         if (!lustre_handle_is_used(&imp->imp_remote_handle))
407                 initial_connect = 1;
408         else
409                 committed_before_reconnect = imp->imp_peer_committed_transno;
410
411         set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
412         spin_unlock(&imp->imp_lock);
413
414         if (new_uuid) {
415                 struct obd_uuid uuid;
416
417                 obd_str2uuid(&uuid, new_uuid);
418                 rc = import_set_conn_priority(imp, &uuid);
419                 if (rc)
420                         GOTO(out, rc);
421         }
422
423         rc = import_select_connection(imp);
424         if (rc)
425                 GOTO(out, rc);
426
427         /* last in connection list */
428         if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
429                 if (imp->imp_initial_recov_bk && initial_connect) {
430                         CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
431                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
432                         /* Don't retry if connect fails */
433                         rc = 0;
434                         obd_set_info_async(obd->obd_self_export,
435                                            strlen(KEY_INIT_RECOV),
436                                            KEY_INIT_RECOV,
437                                            sizeof(rc), &rc, NULL);
438                 }
439                 if (imp->imp_recon_bk) {
440                         CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
441                                imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
442                         spin_lock(&imp->imp_lock);
443                         imp->imp_last_recon = 1;
444                         spin_unlock(&imp->imp_lock);
445                 }
446         }
447
448         rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
449         if (rc)
450                 GOTO(out, rc);
451
452         /* Reset connect flags to the originally requested flags, in case
453          * the server is updated on-the-fly we will get the new features. */
454         imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
455         rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
456                            &obd->obd_uuid, &imp->imp_connect_data);
457         if (rc)
458                 GOTO(out, rc);
459
460         request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
461         if (request == NULL)
462                 GOTO(out, rc = -ENOMEM);
463
464         rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
465                                       imp->imp_connect_op, bufs, NULL);
466         if (rc) {
467                 ptlrpc_request_free(request);
468                 GOTO(out, rc);
469         }
470
471 #ifndef __KERNEL__
472         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
473 #endif
474         lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
475
476         request->rq_send_state = LUSTRE_IMP_CONNECTING;
477         /* Allow a slightly larger reply for future growth compatibility */
478         req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
479                              sizeof(struct obd_connect_data)+16*sizeof(__u64));
480         ptlrpc_request_set_replen(request);
481         request->rq_interpret_reply = ptlrpc_connect_interpret;
482
483         CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
484         aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
485         memset(aa, 0, sizeof *aa);
486
487         aa->pcaa_peer_committed = committed_before_reconnect;
488         aa->pcaa_initial_connect = initial_connect;
489
490         if (aa->pcaa_initial_connect) {
491                 spin_lock(&imp->imp_lock);
492                 imp->imp_replayable = 1;
493                 spin_unlock(&imp->imp_lock);
494                 /* On an initial connect, we don't know which one of a
495                    failover server pair is up.  Don't wait long. */
496 #ifdef CRAY_XT3
497                 request->rq_timeout = max((int)(obd_timeout / 2), 5);
498 #else
499                 request->rq_timeout = max((int)(obd_timeout / 20), 5);
500 #endif
501                 lustre_msg_add_op_flags(request->rq_reqmsg, 
502                                         MSG_CONNECT_INITIAL);
503         }
504
505         if (set_transno)
506                 lustre_msg_add_op_flags(request->rq_reqmsg, 
507                                         MSG_CONNECT_TRANSNO);
508
509         DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
510         ptlrpcd_add_req(request);
511         rc = 0;
512 out:
513         if (rc != 0) {
514                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
515         }
516
517         RETURN(rc);
518 }
519 EXPORT_SYMBOL(ptlrpc_connect_import);
520
521 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
522 {
523 #ifdef __KERNEL__
524         struct obd_import_conn *imp_conn;
525 #endif
526         int wake_pinger = 0;
527
528         ENTRY;
529
530         spin_lock(&imp->imp_lock);
531         if (list_empty(&imp->imp_conn_list))
532                 GOTO(unlock, 0);
533
534 #ifdef __KERNEL__
535         imp_conn = list_entry(imp->imp_conn_list.prev,
536                               struct obd_import_conn,
537                               oic_item);
538
539         if (imp->imp_conn_current != imp_conn) {
540                 ptlrpc_ping_import_soon(imp);
541                 wake_pinger = 1;
542         }
543 #else
544         /* liblustre has no pinger thead, so we wakup pinger anyway */
545         wake_pinger = 1;
546 #endif 
547
548  unlock:
549         spin_unlock(&imp->imp_lock);
550
551         if (wake_pinger)
552                 ptlrpc_pinger_wake_up();
553
554         EXIT;
555 }
556
557 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
558                                     void * data, int rc)
559 {
560         struct ptlrpc_connect_async_args *aa = data;
561         struct obd_import *imp = request->rq_import;
562         struct client_obd *cli = &imp->imp_obd->u.cli;
563         struct lustre_handle old_hdl;
564         int msg_flags;
565         ENTRY;
566
567         spin_lock(&imp->imp_lock);
568         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
569                 spin_unlock(&imp->imp_lock);
570                 RETURN(0);
571         }
572         spin_unlock(&imp->imp_lock);
573
574         if (rc)
575                 GOTO(out, rc);
576
577         LASSERT(imp->imp_conn_current);
578
579         msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
580
581         /* All imports are pingable */
582         spin_lock(&imp->imp_lock);
583         imp->imp_pingable = 1;
584
585         if (aa->pcaa_initial_connect) {
586                 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
587                         imp->imp_replayable = 1;
588                         spin_unlock(&imp->imp_lock);
589                         CDEBUG(D_HA, "connected to replayable target: %s\n",
590                                obd2cli_tgt(imp->imp_obd));
591                 } else {
592                         imp->imp_replayable = 0;
593                         spin_unlock(&imp->imp_lock);
594                 }
595
596                 if (msg_flags & MSG_CONNECT_NEXT_VER) {
597                         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
598                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
599                                obd2cli_tgt(imp->imp_obd));
600                 } else {
601                         CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
602                                obd2cli_tgt(imp->imp_obd));
603                 }
604
605                 imp->imp_remote_handle =
606                                 *lustre_msg_get_handle(request->rq_repmsg);
607
608                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
609                 ptlrpc_activate_import(imp);
610                 GOTO(finish, rc = 0);
611         } else {
612                 spin_unlock(&imp->imp_lock);
613         }
614
615         /* Determine what recovery state to move the import to. */
616         if (MSG_CONNECT_RECONNECT & msg_flags) {
617                 memset(&old_hdl, 0, sizeof(old_hdl));
618                 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
619                             sizeof (old_hdl))) {
620                         CERROR("%s@%s didn't like our handle "LPX64
621                                ", failed\n", obd2cli_tgt(imp->imp_obd),
622                                imp->imp_connection->c_remote_uuid.uuid,
623                                imp->imp_dlm_handle.cookie);
624                         GOTO(out, rc = -ENOTCONN);
625                 }
626
627                 if (memcmp(&imp->imp_remote_handle,
628                            lustre_msg_get_handle(request->rq_repmsg),
629                            sizeof(imp->imp_remote_handle))) {
630                         int level = D_ERROR;
631                         /* Old MGC can reconnect to a restarted MGS */
632                         if (strcmp(imp->imp_obd->obd_type->typ_name,
633                                    LUSTRE_MGC_NAME) == 0) {
634                                 level = D_CONFIG;
635                         }
636                         CDEBUG(level, 
637                                "%s@%s changed handle from "LPX64" to "LPX64
638                                "; copying, but this may foreshadow disaster\n",
639                                obd2cli_tgt(imp->imp_obd),
640                                imp->imp_connection->c_remote_uuid.uuid,
641                                imp->imp_remote_handle.cookie,
642                                lustre_msg_get_handle(request->rq_repmsg)->
643                                         cookie);
644                         imp->imp_remote_handle =
645                                      *lustre_msg_get_handle(request->rq_repmsg);
646                 } else {
647                         CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
648                                obd2cli_tgt(imp->imp_obd),
649                                imp->imp_connection->c_remote_uuid.uuid);
650                 }
651
652                 if (imp->imp_invalid) {
653                         IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
654                 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
655                         CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
656                                imp->imp_obd->obd_name,
657                                obd2cli_tgt(imp->imp_obd));
658
659                         spin_lock(&imp->imp_lock);
660                         imp->imp_resend_replay = 1;
661                         spin_unlock(&imp->imp_lock);
662
663                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
664                 } else {
665                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
666                 }
667         } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
668                 LASSERT(imp->imp_replayable);
669                 imp->imp_remote_handle =
670                                 *lustre_msg_get_handle(request->rq_repmsg);
671                 imp->imp_last_replay_transno = 0;
672                 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
673         } else {
674                 imp->imp_remote_handle =
675                                 *lustre_msg_get_handle(request->rq_repmsg);
676                 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
677         }
678
679         /* Sanity checks for a reconnected import. */
680         if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
681                 CERROR("imp_replayable flag does not match server "
682                        "after reconnect. We should LBUG right here.\n");
683         }
684
685         if (lustre_msg_get_last_committed(request->rq_repmsg) <
686             aa->pcaa_peer_committed) {
687                 CERROR("%s went back in time (transno "LPD64
688                        " was previously committed, server now claims "LPD64
689                        ")!  See https://bugzilla.clusterfs.com/"
690                        "long_list.cgi?buglist=9646\n",
691                        obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
692                        lustre_msg_get_last_committed(request->rq_repmsg));
693         }
694
695 finish:
696         rc = ptlrpc_import_recovery_state_machine(imp);
697         if (rc != 0) {
698                 if (rc == -ENOTCONN) {
699                         CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
700                                "invalidating and reconnecting\n",
701                                obd2cli_tgt(imp->imp_obd),
702                                imp->imp_connection->c_remote_uuid.uuid);
703                         ptlrpc_connect_import(imp, NULL);
704                         RETURN(0);
705                 }
706         } else {
707                 struct obd_connect_data *ocd;
708                 struct obd_export *exp;
709                 int ret;
710                 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
711                                            RCL_SERVER);
712                 /* server replied obd_connect_data is always bigger */
713                 ocd = req_capsule_server_sized_get(&request->rq_pill,
714                                                    &RMF_CONNECT_DATA, ret);
715
716                 spin_lock(&imp->imp_lock);
717                 list_del(&imp->imp_conn_current->oic_item);
718                 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
719                 imp->imp_last_success_conn =
720                         imp->imp_conn_current->oic_last_attempt;
721
722                 if (ocd == NULL) {
723                         spin_unlock(&imp->imp_lock);
724                         CERROR("Wrong connect data from server\n");
725                         rc = -EPROTO;
726                         GOTO(out, rc);
727                 }
728
729                 imp->imp_connect_data = *ocd;
730
731                 exp = class_conn2export(&imp->imp_dlm_handle);
732                 spin_unlock(&imp->imp_lock);
733
734                 /* check that server granted subset of flags we asked for. */
735                 LASSERTF((ocd->ocd_connect_flags &
736                           imp->imp_connect_flags_orig) ==
737                          ocd->ocd_connect_flags, LPX64" != "LPX64,
738                          imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
739
740                 if (!exp) {
741                         /* This could happen if export is cleaned during the 
742                            connect attempt */
743                         CERROR("Missing export for %s\n", 
744                                imp->imp_obd->obd_name);
745                         GOTO(out, rc = -ENODEV);
746                 }
747                 exp->exp_connect_flags = ocd->ocd_connect_flags;
748                 imp->imp_obd->obd_self_export->exp_connect_flags = 
749                                                         ocd->ocd_connect_flags;
750                 class_export_put(exp);
751
752                 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
753
754                 if (!ocd->ocd_ibits_known &&
755                     ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
756                         CERROR("Inodebits aware server returned zero compatible"
757                                " bits?\n");
758
759                 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
760                     (ocd->ocd_version > LUSTRE_VERSION_CODE +
761                                         LUSTRE_VERSION_OFFSET_WARN ||
762                      ocd->ocd_version < LUSTRE_VERSION_CODE -
763                                         LUSTRE_VERSION_OFFSET_WARN)) {
764                         /* Sigh, some compilers do not like #ifdef in the middle
765                            of macro arguments */
766 #ifdef __KERNEL__
767                         const char *older =
768                                 "older. Consider upgrading this client";
769 #else
770                         const char *older =
771                                 "older. Consider recompiling this application";
772 #endif
773                         const char *newer = "newer than client version";
774
775                         LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
776                                       "is much %s (%s)\n",
777                                       obd2cli_tgt(imp->imp_obd),
778                                       OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
779                                       OBD_OCD_VERSION_MINOR(ocd->ocd_version),
780                                       OBD_OCD_VERSION_PATCH(ocd->ocd_version),
781                                       OBD_OCD_VERSION_FIX(ocd->ocd_version),
782                                       ocd->ocd_version > LUSTRE_VERSION_CODE ?
783                                       newer : older, LUSTRE_VERSION_STRING);
784                 }
785
786                 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
787                         cli->cl_max_pages_per_rpc = 
788                                 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
789                 }
790
791                 imp->imp_obd->obd_namespace->ns_connect_flags = 
792                                                         ocd->ocd_connect_flags;
793                 imp->imp_obd->obd_namespace->ns_orig_connect_flags = 
794                                                         ocd->ocd_connect_flags;
795
796                 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
797                         (cli->cl_max_pages_per_rpc > 0));
798         }
799
800 out:
801         if (rc != 0) {
802                 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
803                 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
804                     (request->rq_import_generation == imp->imp_generation))
805                         ptlrpc_deactivate_import(imp);
806
807                 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
808                     (rc == -EACCES)) {
809                         /*
810                          * Give up trying to reconnect
811                          * EACCES means client has no permission for connection
812                          */
813                         imp->imp_obd->obd_no_recov = 1;
814                         ptlrpc_deactivate_import(imp);
815                 }
816
817                 if (rc == -EPROTO) {
818                         struct obd_connect_data *ocd;
819
820                         /* reply message might not be ready */
821                         if (request->rq_repmsg == NULL)
822                                 RETURN(-EPROTO);
823
824                         ocd = req_capsule_server_get(&request->rq_pill,
825                                                      &RMF_CONNECT_DATA);
826                         if (ocd &&
827                             (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
828                             (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
829                            /* Actually servers are only supposed to refuse
830                               connection from liblustre clients, so we should
831                               never see this from VFS context */
832                                 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
833                                         "(%d.%d.%d.%d)"
834                                         " refused connection from this client "
835                                         "with an incompatible version (%s).  "
836                                         "Client must be recompiled\n",
837                                         obd2cli_tgt(imp->imp_obd),
838                                         OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
839                                         OBD_OCD_VERSION_MINOR(ocd->ocd_version),
840                                         OBD_OCD_VERSION_PATCH(ocd->ocd_version),
841                                         OBD_OCD_VERSION_FIX(ocd->ocd_version),
842                                         LUSTRE_VERSION_STRING);
843                                 ptlrpc_deactivate_import(imp);
844                                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
845                         }
846                         RETURN(-EPROTO);
847                 }
848
849                 ptlrpc_maybe_ping_import_soon(imp);
850
851                 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
852                        obd2cli_tgt(imp->imp_obd),
853                        (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
854         }
855         
856         spin_lock(&imp->imp_lock);
857         imp->imp_last_recon = 0;
858         spin_unlock(&imp->imp_lock);
859
860         cfs_waitq_signal(&imp->imp_recovery_waitq);
861         RETURN(rc);
862 }
863
864 static int completed_replay_interpret(struct ptlrpc_request *req,
865                                     void * data, int rc)
866 {
867         ENTRY;
868         atomic_dec(&req->rq_import->imp_replay_inflight);
869         if (req->rq_status == 0) {
870                 ptlrpc_import_recovery_state_machine(req->rq_import);
871         } else {
872                 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
873                        "reconnecting\n",
874                        req->rq_import->imp_obd->obd_name, req->rq_status);
875                 ptlrpc_connect_import(req->rq_import, NULL);
876         }
877
878         RETURN(0);
879 }
880
881 static int signal_completed_replay(struct obd_import *imp)
882 {
883         struct ptlrpc_request *req;
884         ENTRY;
885
886         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
887         atomic_inc(&imp->imp_replay_inflight);
888
889         req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
890                                         OBD_PING);
891         if (req == NULL) {
892                 atomic_dec(&imp->imp_replay_inflight);
893                 RETURN(-ENOMEM);
894         }
895
896         ptlrpc_request_set_replen(req);
897         req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
898         lustre_msg_add_flags(req->rq_reqmsg, 
899                              MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
900         req->rq_timeout *= 3;
901         req->rq_interpret_reply = completed_replay_interpret;
902
903         ptlrpcd_add_req(req);
904         RETURN(0);
905 }
906
907 #ifdef __KERNEL__
908 static int ptlrpc_invalidate_import_thread(void *data)
909 {
910         struct obd_import *imp = data;
911
912         ENTRY;
913
914         ptlrpc_daemonize("ll_imp_inval");
915         
916         CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
917                imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
918                imp->imp_connection->c_remote_uuid.uuid);
919
920         ptlrpc_invalidate_import(imp);
921
922         if (obd_dump_on_eviction) {
923                 CERROR("dump the log upon eviction\n");
924                 libcfs_debug_dumplog();
925         }
926
927         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
928         ptlrpc_import_recovery_state_machine(imp);
929
930         RETURN(0);
931 }
932 #endif
933
934 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
935 {
936         int rc = 0;
937         int inflight;
938         char *target_start;
939         int target_len;
940
941         ENTRY;
942         if (imp->imp_state == LUSTRE_IMP_EVICTED) {
943                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
944                           &target_start, &target_len);
945                 /* Don't care about MGC eviction */
946                 if (strcmp(imp->imp_obd->obd_type->typ_name,
947                            LUSTRE_MGC_NAME) != 0) {
948                         LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
949                                            "%.*s; in progress operations using "
950                                            "this service will fail.\n",
951                                            target_len, target_start);
952                 }
953                 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
954                        obd2cli_tgt(imp->imp_obd),
955                        imp->imp_connection->c_remote_uuid.uuid);
956
957 #ifdef __KERNEL__
958                 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
959                                        CLONE_VM | CLONE_FILES);
960                 if (rc < 0)
961                         CERROR("error starting invalidate thread: %d\n", rc);
962                 else
963                         rc = 0;
964                 RETURN(rc);
965 #else
966                 ptlrpc_invalidate_import(imp);
967
968                 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
969 #endif
970         }
971
972         if (imp->imp_state == LUSTRE_IMP_REPLAY) {
973                 CDEBUG(D_HA, "replay requested by %s\n",
974                        obd2cli_tgt(imp->imp_obd));
975                 rc = ptlrpc_replay_next(imp, &inflight);
976                 if (inflight == 0 &&
977                     atomic_read(&imp->imp_replay_inflight) == 0) {
978                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
979                         rc = ldlm_replay_locks(imp);
980                         if (rc)
981                                 GOTO(out, rc);
982                 }
983                 rc = 0;
984         }
985
986         if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
987                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
988                         IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
989                         rc = signal_completed_replay(imp);
990                         if (rc)
991                                 GOTO(out, rc);
992                 }
993
994         }
995
996         if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
997                 if (atomic_read(&imp->imp_replay_inflight) == 0) {
998                         IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
999                 }
1000         }
1001
1002         if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1003                 CDEBUG(D_HA, "reconnected to %s@%s\n",
1004                        obd2cli_tgt(imp->imp_obd),
1005                        imp->imp_connection->c_remote_uuid.uuid);
1006
1007                 rc = ptlrpc_resend(imp);
1008                 if (rc)
1009                         GOTO(out, rc);
1010                 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1011                 ptlrpc_activate_import(imp);
1012
1013                 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1014                           &target_start, &target_len);
1015                 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1016                               "using nid %s.\n", imp->imp_obd->obd_name,
1017                               target_len, target_start,
1018                               libcfs_nid2str(imp->imp_connection->c_peer.nid));
1019         }
1020
1021         if (imp->imp_state == LUSTRE_IMP_FULL) {
1022                 cfs_waitq_signal(&imp->imp_recovery_waitq);
1023                 ptlrpc_wake_delayed(imp);
1024         }
1025
1026 out:
1027         RETURN(rc);
1028 }
1029
1030 static int back_to_sleep(void *unused)
1031 {
1032         return 0;
1033 }
1034
1035 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1036 {
1037         struct ptlrpc_request *req;
1038         int rq_opc, rc = 0;
1039         int nowait = imp->imp_obd->obd_force;
1040         ENTRY;
1041
1042         if (nowait)
1043                 GOTO(set_state, rc);
1044
1045         switch (imp->imp_connect_op) {
1046         case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1047         case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1048         case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1049         default:
1050                 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1051                        obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1052                 RETURN(-EINVAL);
1053         }
1054
1055         if (ptlrpc_import_in_recovery(imp)) {
1056                 struct l_wait_info lwi;
1057                 cfs_duration_t timeout;
1058                 if (imp->imp_server_timeout)
1059                         timeout = cfs_time_seconds(obd_timeout / 2);
1060                 else
1061                         timeout = cfs_time_seconds(obd_timeout);
1062                 
1063                 timeout = MAX(timeout * HZ, 1);
1064                 
1065                 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout), 
1066                                        back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1067                 rc = l_wait_event(imp->imp_recovery_waitq,
1068                                   !ptlrpc_import_in_recovery(imp), &lwi);
1069
1070         }
1071
1072         spin_lock(&imp->imp_lock);
1073         if (imp->imp_state != LUSTRE_IMP_FULL)
1074                 GOTO(out, 0);
1075
1076         spin_unlock(&imp->imp_lock);
1077
1078         req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1079                                         LUSTRE_OBD_VERSION, rq_opc);
1080         if (req) {
1081                 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1082                  * it fails.  We can get through the above with a down server
1083                  * if the client doesn't know the server is gone yet. */
1084                 req->rq_no_resend = 1;
1085 #ifdef CRAY_XT3
1086                 req->rq_timeout = obd_timeout / 3;
1087 #else
1088                 req->rq_timeout = 5;
1089 #endif
1090                 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1091                 req->rq_send_state =  LUSTRE_IMP_CONNECTING;
1092                 ptlrpc_request_set_replen(req);
1093                 rc = ptlrpc_queue_wait(req);
1094                 ptlrpc_req_finished(req);
1095         }
1096
1097 set_state:
1098         spin_lock(&imp->imp_lock);
1099 out:
1100         if (noclose) 
1101                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1102         else
1103                 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1104         memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1105         imp->imp_conn_cnt = 0;
1106         imp->imp_last_recon = 0;
1107         spin_unlock(&imp->imp_lock);
1108
1109         RETURN(rc);
1110 }
1111