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