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