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