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