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