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