1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Mike Shaver <shaver@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
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.
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.
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.
26 #define DEBUG_SUBSYSTEM S_RPC
28 # include <liblustre.h>
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>
37 #include <obd_class.h>
39 #include "ptlrpc_internal.h"
41 struct ptlrpc_connect_async_args {
42 __u64 pcaa_peer_committed;
43 int pcaa_initial_connect;
46 /* A CLOSED import should remain so. */
47 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
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; \
58 #define IMPORT_SET_STATE(imp, state) \
60 spin_lock(&imp->imp_lock); \
61 IMPORT_SET_STATE_NOLOCK(imp, state); \
62 spin_unlock(&imp->imp_lock); \
66 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
68 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
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,
75 int ptlrpc_init_import(struct obd_import *imp)
77 spin_lock(&imp->imp_lock);
79 imp->imp_generation++;
80 imp->imp_state = LUSTRE_IMP_NEW;
82 spin_unlock(&imp->imp_lock);
86 EXPORT_SYMBOL(ptlrpc_init_import);
88 #define UUID_STR "_UUID"
89 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
92 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
93 ? uuid : uuid + strlen(prefix);
95 *uuid_len = strlen(*uuid_start);
97 if (*uuid_len < strlen(UUID_STR))
100 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
101 UUID_STR, strlen(UUID_STR)))
102 *uuid_len -= strlen(UUID_STR);
105 /* Returns true if import was FULL, false if import was already not
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.
115 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
119 spin_lock(&imp->imp_lock);
121 if (imp->imp_state == LUSTRE_IMP_FULL &&
122 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
126 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
127 &target_start, &target_len);
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));
136 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
137 "%.*s via nid %s was lost; in progress"
138 "operations using this service will"
140 imp->imp_obd->obd_name,
141 target_len, target_start,
142 libcfs_nid2str(imp->imp_connection->c_peer.nid));
144 ptlrpc_deactivate_timeouts(imp);
145 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
146 spin_unlock(&imp->imp_lock);
148 if (obd_dump_on_timeout)
149 libcfs_debug_dumplog();
151 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
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));
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.
170 void ptlrpc_deactivate_import(struct obd_import *imp)
174 spin_lock(&imp->imp_lock);
175 if (imp->imp_invalid) {
176 spin_unlock(&imp->imp_lock);
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);
186 ptlrpc_abort_inflight(imp);
187 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
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,
198 void ptlrpc_invalidate_import(struct obd_import *imp)
200 struct l_wait_info lwi;
203 atomic_inc(&imp->imp_inval_count);
205 ptlrpc_deactivate_import(imp);
207 LASSERT(imp->imp_invalid);
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)),
212 rc = l_wait_event(imp->imp_recovery_waitq,
213 (atomic_read(&imp->imp_inflight) == 0), &lwi);
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));
220 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
221 sptlrpc_import_flush_all_ctx(imp);
223 atomic_dec(&imp->imp_inval_count);
224 cfs_waitq_signal(&imp->imp_recovery_waitq);
227 /* unset imp_invalid */
228 void ptlrpc_activate_import(struct obd_import *imp)
230 struct obd_device *obd = imp->imp_obd;
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);
239 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
243 LASSERT(!imp->imp_dlm_fake);
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);
255 CDEBUG(D_HA, "%s: waking up pinger\n",
256 obd2cli_tgt(imp->imp_obd));
258 spin_lock(&imp->imp_lock);
259 imp->imp_force_verify = 1;
260 spin_unlock(&imp->imp_lock);
262 ptlrpc_pinger_wake_up();
267 static int import_select_connection(struct obd_import *imp)
269 struct obd_import_conn *imp_conn = NULL, *conn;
270 struct obd_export *dlmexp;
273 spin_lock(&imp->imp_lock);
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);
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)) {
299 /* Both of these connections have already been tried
300 since the last successful connection; just choose the
301 least recently used */
304 else if (cfs_time_before_64(conn->oic_last_attempt,
305 imp_conn->oic_last_attempt))
310 /* if not found, simply choose the current one */
312 LASSERT(imp->imp_conn_current);
313 imp_conn = imp->imp_conn_current;
315 LASSERT(imp_conn->oic_conn);
317 imp_conn->oic_last_attempt = cfs_time_current_64();
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);
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);
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;
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));
344 spin_unlock(&imp->imp_lock);
350 * must be called under imp_lock
352 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
354 struct ptlrpc_request *req;
355 struct list_head *tmp;
357 if (list_empty(&imp->imp_replay_list))
359 tmp = imp->imp_replay_list.next;
360 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
361 *transno = req->rq_transno;
365 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
367 struct obd_device *obd = imp->imp_obd;
368 int initial_connect = 0;
371 __u64 committed_before_reconnect = 0;
372 struct ptlrpc_request *request;
373 int size[] = { sizeof(struct ptlrpc_body),
374 sizeof(imp->imp_obd->u.cli.cl_target_uuid),
375 sizeof(obd->obd_uuid),
376 sizeof(imp->imp_dlm_handle),
377 sizeof(imp->imp_connect_data) };
378 char *tmp[] = { NULL,
379 obd2cli_tgt(imp->imp_obd),
381 (char *)&imp->imp_dlm_handle,
382 (char *)&imp->imp_connect_data };
383 struct ptlrpc_connect_async_args *aa;
386 spin_lock(&imp->imp_lock);
387 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
388 spin_unlock(&imp->imp_lock);
389 CERROR("can't connect to a closed import\n");
391 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
392 spin_unlock(&imp->imp_lock);
393 CERROR("already connected\n");
395 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
396 spin_unlock(&imp->imp_lock);
397 CERROR("already connecting\n");
401 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
404 imp->imp_resend_replay = 0;
406 if (!lustre_handle_is_used(&imp->imp_remote_handle))
409 committed_before_reconnect = imp->imp_peer_committed_transno;
411 set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
412 spin_unlock(&imp->imp_lock);
415 struct obd_uuid uuid;
417 obd_str2uuid(&uuid, new_uuid);
418 rc = import_set_conn_priority(imp, &uuid);
423 rc = import_select_connection(imp);
427 /* last in connection list */
428 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
429 if (imp->imp_initial_recov_bk && initial_connect) {
430 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
431 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
432 /* Don't retry if connect fails */
434 obd_set_info_async(obd->obd_self_export,
435 strlen(KEY_INIT_RECOV),
437 sizeof(rc), &rc, NULL);
439 if (imp->imp_recon_bk) {
440 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
441 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
442 spin_lock(&imp->imp_lock);
443 imp->imp_last_recon = 1;
444 spin_unlock(&imp->imp_lock);
448 /* Reset connect flags to the originally requested flags, in case
449 * the server is updated on-the-fly we will get the new features. */
450 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
451 rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
452 &obd->obd_uuid, &imp->imp_connect_data);
456 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
459 GOTO(out, rc = -ENOMEM);
462 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
464 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
466 request->rq_send_state = LUSTRE_IMP_CONNECTING;
467 /* Allow a slightly larger reply for future growth compatibility */
468 size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
470 ptlrpc_req_set_repsize(request, 2, size);
471 request->rq_interpret_reply = ptlrpc_connect_interpret;
473 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
474 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
475 memset(aa, 0, sizeof *aa);
477 aa->pcaa_peer_committed = committed_before_reconnect;
478 aa->pcaa_initial_connect = initial_connect;
480 if (aa->pcaa_initial_connect) {
481 spin_lock(&imp->imp_lock);
482 imp->imp_replayable = 1;
483 spin_unlock(&imp->imp_lock);
484 /* On an initial connect, we don't know which one of a
485 failover server pair is up. Don't wait long. */
487 request->rq_timeout = max((int)(obd_timeout / 2), 5);
489 request->rq_timeout = max((int)(obd_timeout / 20), 5);
491 lustre_msg_add_op_flags(request->rq_reqmsg,
492 MSG_CONNECT_INITIAL);
496 lustre_msg_add_op_flags(request->rq_reqmsg,
497 MSG_CONNECT_TRANSNO);
499 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
500 ptlrpcd_add_req(request);
504 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
509 EXPORT_SYMBOL(ptlrpc_connect_import);
511 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
514 struct obd_import_conn *imp_conn;
520 spin_lock(&imp->imp_lock);
521 if (list_empty(&imp->imp_conn_list))
525 imp_conn = list_entry(imp->imp_conn_list.prev,
526 struct obd_import_conn,
529 if (imp->imp_conn_current != imp_conn) {
530 ptlrpc_ping_import_soon(imp);
534 /* liblustre has no pinger thead, so we wakup pinger anyway */
539 spin_unlock(&imp->imp_lock);
542 ptlrpc_pinger_wake_up();
547 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
550 struct ptlrpc_connect_async_args *aa = data;
551 struct obd_import *imp = request->rq_import;
552 struct client_obd *cli = &imp->imp_obd->u.cli;
553 struct lustre_handle old_hdl;
557 spin_lock(&imp->imp_lock);
558 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
559 spin_unlock(&imp->imp_lock);
562 spin_unlock(&imp->imp_lock);
567 LASSERT(imp->imp_conn_current);
569 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
571 /* All imports are pingable */
572 spin_lock(&imp->imp_lock);
573 imp->imp_pingable = 1;
575 if (aa->pcaa_initial_connect) {
576 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
577 imp->imp_replayable = 1;
578 spin_unlock(&imp->imp_lock);
579 CDEBUG(D_HA, "connected to replayable target: %s\n",
580 obd2cli_tgt(imp->imp_obd));
582 imp->imp_replayable = 0;
583 spin_unlock(&imp->imp_lock);
586 if (msg_flags & MSG_CONNECT_NEXT_VER) {
587 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
588 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
589 obd2cli_tgt(imp->imp_obd));
591 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
592 obd2cli_tgt(imp->imp_obd));
595 imp->imp_remote_handle =
596 *lustre_msg_get_handle(request->rq_repmsg);
598 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
599 ptlrpc_activate_import(imp);
600 GOTO(finish, rc = 0);
602 spin_unlock(&imp->imp_lock);
605 /* Determine what recovery state to move the import to. */
606 if (MSG_CONNECT_RECONNECT & msg_flags) {
607 memset(&old_hdl, 0, sizeof(old_hdl));
608 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
610 CERROR("%s@%s didn't like our handle "LPX64
611 ", failed\n", obd2cli_tgt(imp->imp_obd),
612 imp->imp_connection->c_remote_uuid.uuid,
613 imp->imp_dlm_handle.cookie);
614 GOTO(out, rc = -ENOTCONN);
617 if (memcmp(&imp->imp_remote_handle,
618 lustre_msg_get_handle(request->rq_repmsg),
619 sizeof(imp->imp_remote_handle))) {
621 /* Old MGC can reconnect to a restarted MGS */
622 if (strcmp(imp->imp_obd->obd_type->typ_name,
623 LUSTRE_MGC_NAME) == 0) {
627 "%s@%s changed handle from "LPX64" to "LPX64
628 "; copying, but this may foreshadow disaster\n",
629 obd2cli_tgt(imp->imp_obd),
630 imp->imp_connection->c_remote_uuid.uuid,
631 imp->imp_remote_handle.cookie,
632 lustre_msg_get_handle(request->rq_repmsg)->
634 imp->imp_remote_handle =
635 *lustre_msg_get_handle(request->rq_repmsg);
637 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
638 obd2cli_tgt(imp->imp_obd),
639 imp->imp_connection->c_remote_uuid.uuid);
642 if (imp->imp_invalid) {
643 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
644 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
645 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
646 imp->imp_obd->obd_name,
647 obd2cli_tgt(imp->imp_obd));
649 spin_lock(&imp->imp_lock);
650 imp->imp_resend_replay = 1;
651 spin_unlock(&imp->imp_lock);
653 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
655 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
657 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
658 LASSERT(imp->imp_replayable);
659 imp->imp_remote_handle =
660 *lustre_msg_get_handle(request->rq_repmsg);
661 imp->imp_last_replay_transno = 0;
662 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
664 imp->imp_remote_handle =
665 *lustre_msg_get_handle(request->rq_repmsg);
666 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
669 /* Sanity checks for a reconnected import. */
670 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
671 CERROR("imp_replayable flag does not match server "
672 "after reconnect. We should LBUG right here.\n");
675 if (lustre_msg_get_last_committed(request->rq_repmsg) <
676 aa->pcaa_peer_committed) {
677 CERROR("%s went back in time (transno "LPD64
678 " was previously committed, server now claims "LPD64
679 ")! See https://bugzilla.clusterfs.com/"
680 "long_list.cgi?buglist=9646\n",
681 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
682 lustre_msg_get_last_committed(request->rq_repmsg));
686 rc = ptlrpc_import_recovery_state_machine(imp);
688 if (rc == -ENOTCONN) {
689 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
690 "invalidating and reconnecting\n",
691 obd2cli_tgt(imp->imp_obd),
692 imp->imp_connection->c_remote_uuid.uuid);
693 ptlrpc_connect_import(imp, NULL);
697 struct obd_connect_data *ocd;
698 struct obd_export *exp;
700 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
701 lustre_swab_connect);
703 spin_lock(&imp->imp_lock);
704 list_del(&imp->imp_conn_current->oic_item);
705 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
706 imp->imp_last_success_conn =
707 imp->imp_conn_current->oic_last_attempt;
710 spin_unlock(&imp->imp_lock);
711 CERROR("Wrong connect data from server\n");
716 imp->imp_connect_data = *ocd;
718 exp = class_conn2export(&imp->imp_dlm_handle);
719 spin_unlock(&imp->imp_lock);
721 /* check that server granted subset of flags we asked for. */
722 LASSERTF((ocd->ocd_connect_flags &
723 imp->imp_connect_flags_orig) ==
724 ocd->ocd_connect_flags, LPX64" != "LPX64,
725 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
728 /* This could happen if export is cleaned during the
730 CERROR("Missing export for %s\n",
731 imp->imp_obd->obd_name);
732 GOTO(out, rc = -ENODEV);
734 exp->exp_connect_flags = ocd->ocd_connect_flags;
735 imp->imp_obd->obd_self_export->exp_connect_flags =
736 ocd->ocd_connect_flags;
737 class_export_put(exp);
739 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
741 if (!ocd->ocd_ibits_known &&
742 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
743 CERROR("Inodebits aware server returned zero compatible"
746 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
747 (ocd->ocd_version > LUSTRE_VERSION_CODE +
748 LUSTRE_VERSION_OFFSET_WARN ||
749 ocd->ocd_version < LUSTRE_VERSION_CODE -
750 LUSTRE_VERSION_OFFSET_WARN)) {
751 /* Sigh, some compilers do not like #ifdef in the middle
752 of macro arguments */
755 "older. Consider upgrading this client";
758 "older. Consider recompiling this application";
760 const char *newer = "newer than client version";
762 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
764 obd2cli_tgt(imp->imp_obd),
765 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
766 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
767 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
768 OBD_OCD_VERSION_FIX(ocd->ocd_version),
769 ocd->ocd_version > LUSTRE_VERSION_CODE ?
770 newer : older, LUSTRE_VERSION_STRING);
773 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
774 cli->cl_max_pages_per_rpc =
775 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
778 imp->imp_obd->obd_namespace->ns_connect_flags =
779 ocd->ocd_connect_flags;
780 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
781 ocd->ocd_connect_flags;
783 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
784 (cli->cl_max_pages_per_rpc > 0));
789 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
790 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
791 (request->rq_import_generation == imp->imp_generation))
792 ptlrpc_deactivate_import(imp);
794 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
797 * Give up trying to reconnect
798 * EACCES means client has no permission for connection
800 imp->imp_obd->obd_no_recov = 1;
801 ptlrpc_deactivate_import(imp);
805 struct obd_connect_data *ocd;
807 /* reply message might not be ready */
808 if (request->rq_repmsg != NULL)
811 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
813 lustre_swab_connect);
815 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
816 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
817 /* Actually servers are only supposed to refuse
818 connection from liblustre clients, so we should
819 never see this from VFS context */
820 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
822 " refused connection from this client "
823 "with an incompatible version (%s). "
824 "Client must be recompiled\n",
825 obd2cli_tgt(imp->imp_obd),
826 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
827 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
828 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
829 OBD_OCD_VERSION_FIX(ocd->ocd_version),
830 LUSTRE_VERSION_STRING);
831 ptlrpc_deactivate_import(imp);
832 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
837 ptlrpc_maybe_ping_import_soon(imp);
839 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
840 obd2cli_tgt(imp->imp_obd),
841 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
844 spin_lock(&imp->imp_lock);
845 imp->imp_last_recon = 0;
846 spin_unlock(&imp->imp_lock);
848 cfs_waitq_signal(&imp->imp_recovery_waitq);
852 static int completed_replay_interpret(struct ptlrpc_request *req,
856 atomic_dec(&req->rq_import->imp_replay_inflight);
857 if (req->rq_status == 0) {
858 ptlrpc_import_recovery_state_machine(req->rq_import);
860 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
862 req->rq_import->imp_obd->obd_name, req->rq_status);
863 ptlrpc_connect_import(req->rq_import, NULL);
869 static int signal_completed_replay(struct obd_import *imp)
871 struct ptlrpc_request *req;
874 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
875 atomic_inc(&imp->imp_replay_inflight);
877 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
879 atomic_dec(&imp->imp_replay_inflight);
883 ptlrpc_req_set_repsize(req, 1, NULL);
884 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
885 lustre_msg_add_flags(req->rq_reqmsg,
886 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
887 req->rq_timeout *= 3;
888 req->rq_interpret_reply = completed_replay_interpret;
890 ptlrpcd_add_req(req);
895 static int ptlrpc_invalidate_import_thread(void *data)
897 struct obd_import *imp = data;
901 ptlrpc_daemonize("ll_imp_inval");
903 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
904 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
905 imp->imp_connection->c_remote_uuid.uuid);
907 ptlrpc_invalidate_import(imp);
909 if (obd_dump_on_eviction) {
910 CERROR("dump the log upon eviction\n");
911 libcfs_debug_dumplog();
914 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
915 ptlrpc_import_recovery_state_machine(imp);
921 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
929 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
930 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
931 &target_start, &target_len);
932 /* Don't care about MGC eviction */
933 if (strcmp(imp->imp_obd->obd_type->typ_name,
934 LUSTRE_MGC_NAME) != 0) {
935 LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
936 "%.*s; in progress operations using "
937 "this service will fail.\n",
938 target_len, target_start);
940 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
941 obd2cli_tgt(imp->imp_obd),
942 imp->imp_connection->c_remote_uuid.uuid);
945 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
946 CLONE_VM | CLONE_FILES);
948 CERROR("error starting invalidate thread: %d\n", rc);
953 ptlrpc_invalidate_import(imp);
955 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
959 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
960 CDEBUG(D_HA, "replay requested by %s\n",
961 obd2cli_tgt(imp->imp_obd));
962 rc = ptlrpc_replay_next(imp, &inflight);
964 atomic_read(&imp->imp_replay_inflight) == 0) {
965 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
966 rc = ldlm_replay_locks(imp);
973 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
974 if (atomic_read(&imp->imp_replay_inflight) == 0) {
975 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
976 rc = signal_completed_replay(imp);
983 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
984 if (atomic_read(&imp->imp_replay_inflight) == 0) {
985 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
989 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
990 CDEBUG(D_HA, "reconnected to %s@%s\n",
991 obd2cli_tgt(imp->imp_obd),
992 imp->imp_connection->c_remote_uuid.uuid);
994 rc = ptlrpc_resend(imp);
997 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
998 ptlrpc_activate_import(imp);
1000 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1001 &target_start, &target_len);
1002 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1003 "using nid %s.\n", imp->imp_obd->obd_name,
1004 target_len, target_start,
1005 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1008 if (imp->imp_state == LUSTRE_IMP_FULL) {
1009 cfs_waitq_signal(&imp->imp_recovery_waitq);
1010 ptlrpc_wake_delayed(imp);
1017 static int back_to_sleep(void *unused)
1022 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1024 struct ptlrpc_request *req;
1026 int nowait = imp->imp_obd->obd_force;
1030 GOTO(set_state, rc);
1032 switch (imp->imp_connect_op) {
1033 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1034 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1035 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1037 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1038 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1042 if (ptlrpc_import_in_recovery(imp)) {
1043 struct l_wait_info lwi;
1044 cfs_duration_t timeout;
1045 if (imp->imp_server_timeout)
1046 timeout = cfs_time_seconds(obd_timeout / 2);
1048 timeout = cfs_time_seconds(obd_timeout);
1050 timeout = MAX(timeout * HZ, 1);
1052 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1053 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1054 rc = l_wait_event(imp->imp_recovery_waitq,
1055 !ptlrpc_import_in_recovery(imp), &lwi);
1059 spin_lock(&imp->imp_lock);
1060 if (imp->imp_state != LUSTRE_IMP_FULL)
1063 spin_unlock(&imp->imp_lock);
1065 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
1067 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1068 * it fails. We can get through the above with a down server
1069 * if the client doesn't know the server is gone yet. */
1070 req->rq_no_resend = 1;
1072 req->rq_timeout = obd_timeout / 3;
1074 req->rq_timeout = 5;
1076 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1077 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1078 ptlrpc_req_set_repsize(req, 1, NULL);
1079 rc = ptlrpc_queue_wait(req);
1080 ptlrpc_req_finished(req);
1084 spin_lock(&imp->imp_lock);
1087 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1089 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1090 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1091 imp->imp_conn_cnt = 0;
1092 imp->imp_last_recon = 0;
1093 spin_unlock(&imp->imp_lock);