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);
128 if (imp->imp_replayable) {
129 LCONSOLE_WARN("%s: Connection to service %.*s via nid "
130 "%s was lost; in progress operations using this "
131 "service will wait for recovery to complete.\n",
132 imp->imp_obd->obd_name, target_len, target_start,
133 libcfs_nid2str(imp->imp_connection->c_peer.nid));
135 LCONSOLE_ERROR("%s: Connection to service %.*s via nid "
136 "%s was lost; in progress operations using this "
137 "service will fail.\n",
138 imp->imp_obd->obd_name, target_len, target_start,
139 libcfs_nid2str(imp->imp_connection->c_peer.nid));
141 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
142 spin_unlock(&imp->imp_lock);
144 if (obd_dump_on_timeout)
145 libcfs_debug_dumplog();
147 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
150 spin_unlock(&imp->imp_lock);
151 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
152 imp->imp_client->cli_name, imp,
153 (imp->imp_state == LUSTRE_IMP_FULL &&
154 imp->imp_conn_cnt > conn_cnt) ?
155 "reconnected" : "not connected", imp->imp_conn_cnt,
156 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
163 * This acts as a barrier; all existing requests are rejected, and
164 * no new requests will be accepted until the import is valid again.
166 void ptlrpc_deactivate_import(struct obd_import *imp)
170 spin_lock(&imp->imp_lock);
171 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
172 imp->imp_invalid = 1;
173 imp->imp_generation++;
174 spin_unlock(&imp->imp_lock);
176 ptlrpc_abort_inflight(imp);
177 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
181 * This function will invalidate the import, if necessary, then block
182 * for all the RPC completions, and finally notify the obd to
183 * invalidate its state (ie cancel locks, clear pending requests,
186 void ptlrpc_invalidate_import(struct obd_import *imp)
188 struct l_wait_info lwi;
191 if (!imp->imp_invalid)
192 ptlrpc_deactivate_import(imp);
194 LASSERT(imp->imp_invalid);
196 /* wait for all requests to error out and call completion callbacks */
197 lwi = LWI_TIMEOUT_INTERVAL(cfs_timeout_cap(cfs_time_seconds(obd_timeout)),
199 rc = l_wait_event(imp->imp_recovery_waitq,
200 (atomic_read(&imp->imp_inflight) == 0),
204 CDEBUG(D_HA, "%s: rc = %d waiting for callback (%d != 0)\n",
205 obd2cli_tgt(imp->imp_obd), rc,
206 atomic_read(&imp->imp_inflight));
208 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
211 /* unset imp_invalid */
212 void ptlrpc_activate_import(struct obd_import *imp)
214 struct obd_device *obd = imp->imp_obd;
216 spin_lock(&imp->imp_lock);
217 imp->imp_invalid = 0;
218 spin_unlock(&imp->imp_lock);
220 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
223 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
227 LASSERT(!imp->imp_dlm_fake);
229 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
230 if (!imp->imp_replayable) {
231 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
232 "auto-deactivating\n",
233 obd2cli_tgt(imp->imp_obd),
234 imp->imp_connection->c_remote_uuid.uuid,
235 imp->imp_obd->obd_name);
236 ptlrpc_deactivate_import(imp);
239 CDEBUG(D_HA, "%s: waking up pinger\n",
240 obd2cli_tgt(imp->imp_obd));
242 spin_lock(&imp->imp_lock);
243 imp->imp_force_verify = 1;
244 spin_unlock(&imp->imp_lock);
246 ptlrpc_pinger_wake_up();
251 static int import_select_connection(struct obd_import *imp)
253 struct obd_import_conn *imp_conn = NULL, *conn;
254 struct obd_export *dlmexp;
257 spin_lock(&imp->imp_lock);
259 if (list_empty(&imp->imp_conn_list)) {
260 CERROR("%s: no connections available\n",
261 imp->imp_obd->obd_name);
262 spin_unlock(&imp->imp_lock);
266 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
267 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
268 imp->imp_obd->obd_name,
269 libcfs_nid2str(conn->oic_conn->c_peer.nid),
270 conn->oic_last_attempt);
271 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
272 if (cfs_time_before_64(conn->oic_last_attempt +
273 RECONNECT_INTERVAL * HZ,
274 cfs_time_current_64())) {
275 /* If we have never tried this connection since the
276 the last successful attempt, go with this one */
277 if (cfs_time_before_64(conn->oic_last_attempt,
278 imp->imp_last_success_conn)) {
283 /* Both of these connections have already been tried
284 since the last successful connection; just choose the
285 least recently used */
288 else if (cfs_time_before_64(conn->oic_last_attempt,
289 imp_conn->oic_last_attempt))
294 /* if not found, simply choose the current one */
296 LASSERT(imp->imp_conn_current);
297 imp_conn = imp->imp_conn_current;
299 LASSERT(imp_conn->oic_conn);
301 imp_conn->oic_last_attempt = cfs_time_current_64();
303 /* switch connection, don't mind if it's same as the current one */
304 if (imp->imp_connection)
305 ptlrpc_put_connection(imp->imp_connection);
306 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
308 dlmexp = class_conn2export(&imp->imp_dlm_handle);
309 LASSERT(dlmexp != NULL);
310 if (dlmexp->exp_connection)
311 ptlrpc_put_connection(dlmexp->exp_connection);
312 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
313 class_export_put(dlmexp);
315 if (imp->imp_conn_current != imp_conn) {
316 if (imp->imp_conn_current)
317 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
318 imp->imp_obd->obd_name,
319 imp_conn->oic_uuid.uuid,
320 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
321 imp->imp_conn_current = imp_conn;
324 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
325 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
326 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
328 spin_unlock(&imp->imp_lock);
333 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
335 struct obd_device *obd = imp->imp_obd;
336 int initial_connect = 0;
338 __u64 committed_before_reconnect = 0;
339 struct ptlrpc_request *request;
340 int size[] = { sizeof(struct ptlrpc_body),
341 sizeof(imp->imp_obd->u.cli.cl_target_uuid),
342 sizeof(obd->obd_uuid),
343 sizeof(imp->imp_dlm_handle),
344 sizeof(imp->imp_connect_data) };
345 char *tmp[] = { NULL,
346 obd2cli_tgt(imp->imp_obd),
348 (char *)&imp->imp_dlm_handle,
349 (char *)&imp->imp_connect_data };
350 struct ptlrpc_connect_async_args *aa;
353 spin_lock(&imp->imp_lock);
354 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
355 spin_unlock(&imp->imp_lock);
356 CERROR("can't connect to a closed import\n");
358 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
359 spin_unlock(&imp->imp_lock);
360 CERROR("already connected\n");
362 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
363 spin_unlock(&imp->imp_lock);
364 CERROR("already connecting\n");
368 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
371 imp->imp_resend_replay = 0;
373 if (!lustre_handle_is_used(&imp->imp_remote_handle))
376 committed_before_reconnect = imp->imp_peer_committed_transno;
378 spin_unlock(&imp->imp_lock);
381 struct obd_uuid uuid;
383 obd_str2uuid(&uuid, new_uuid);
384 rc = import_set_conn_priority(imp, &uuid);
389 rc = import_select_connection(imp);
393 /* last in connection list */
394 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
395 if (imp->imp_initial_recov_bk && initial_connect) {
396 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
397 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
398 /* Don't retry if connect fails */
400 obd_set_info_async(obd->obd_self_export,
401 strlen(KEY_INIT_RECOV),
403 sizeof(rc), &rc, NULL);
405 if (imp->imp_recon_bk) {
406 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
407 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
408 imp->imp_last_recon = 1;
412 /* Reset connect flags to the originally requested flags, in case
413 * the server is updated on-the-fly we will get the new features. */
414 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
415 rc = obd_reconnect(imp->imp_obd->obd_self_export, obd,
416 &obd->obd_uuid, &imp->imp_connect_data);
420 request = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, imp->imp_connect_op,
423 GOTO(out, rc = -ENOMEM);
426 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
428 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
430 request->rq_send_state = LUSTRE_IMP_CONNECTING;
431 /* Allow a slightly larger reply for future growth compatibility */
432 size[REPLY_REC_OFF] = sizeof(struct obd_connect_data) +
434 ptlrpc_req_set_repsize(request, 2, size);
435 request->rq_interpret_reply = ptlrpc_connect_interpret;
437 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
438 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
439 memset(aa, 0, sizeof *aa);
441 aa->pcaa_peer_committed = committed_before_reconnect;
442 aa->pcaa_initial_connect = initial_connect;
444 if (aa->pcaa_initial_connect) {
445 imp->imp_replayable = 1;
446 /* On an initial connect, we don't know which one of a
447 failover server pair is up. Don't wait long. */
448 request->rq_timeout = max((int)(obd_timeout / 20), 5);
451 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
452 ptlrpcd_add_req(request);
456 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
461 EXPORT_SYMBOL(ptlrpc_connect_import);
463 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
466 struct obd_import_conn *imp_conn;
472 spin_lock(&imp->imp_lock);
473 if (list_empty(&imp->imp_conn_list))
477 imp_conn = list_entry(imp->imp_conn_list.prev,
478 struct obd_import_conn,
481 if (imp->imp_conn_current != imp_conn) {
482 ptlrpc_ping_import_soon(imp);
487 /* liblustre has no pinger thead, so we wakup pinger anyway */
491 spin_unlock(&imp->imp_lock);
494 ptlrpc_pinger_wake_up();
499 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
502 struct ptlrpc_connect_async_args *aa = data;
503 struct obd_import *imp = request->rq_import;
504 struct client_obd *cli = &imp->imp_obd->u.cli;
505 struct lustre_handle old_hdl;
509 spin_lock(&imp->imp_lock);
510 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
511 spin_unlock(&imp->imp_lock);
514 spin_unlock(&imp->imp_lock);
519 LASSERT(imp->imp_conn_current);
521 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
523 /* All imports are pingable */
524 imp->imp_pingable = 1;
526 if (aa->pcaa_initial_connect) {
527 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
528 CDEBUG(D_HA, "connected to replayable target: %s\n",
529 obd2cli_tgt(imp->imp_obd));
530 imp->imp_replayable = 1;
532 imp->imp_replayable = 0;
535 if (msg_flags & MSG_CONNECT_NEXT_VER) {
536 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
537 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
538 obd2cli_tgt(imp->imp_obd));
540 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
541 obd2cli_tgt(imp->imp_obd));
544 imp->imp_remote_handle =
545 *lustre_msg_get_handle(request->rq_repmsg);
547 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
548 GOTO(finish, rc = 0);
551 /* Determine what recovery state to move the import to. */
552 if (MSG_CONNECT_RECONNECT & msg_flags) {
553 memset(&old_hdl, 0, sizeof(old_hdl));
554 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
556 CERROR("%s@%s didn't like our handle "LPX64
557 ", failed\n", obd2cli_tgt(imp->imp_obd),
558 imp->imp_connection->c_remote_uuid.uuid,
559 imp->imp_dlm_handle.cookie);
560 GOTO(out, rc = -ENOTCONN);
563 if (memcmp(&imp->imp_remote_handle,
564 lustre_msg_get_handle(request->rq_repmsg),
565 sizeof(imp->imp_remote_handle))) {
567 /* Old MGC can reconnect to a restarted MGS */
568 if (strcmp(imp->imp_obd->obd_type->typ_name,
569 LUSTRE_MGC_NAME) == 0) {
573 "%s@%s changed handle from "LPX64" to "LPX64
574 "; copying, but this may foreshadow disaster\n",
575 obd2cli_tgt(imp->imp_obd),
576 imp->imp_connection->c_remote_uuid.uuid,
577 imp->imp_remote_handle.cookie,
578 lustre_msg_get_handle(request->rq_repmsg)->
580 imp->imp_remote_handle =
581 *lustre_msg_get_handle(request->rq_repmsg);
583 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
584 obd2cli_tgt(imp->imp_obd),
585 imp->imp_connection->c_remote_uuid.uuid);
588 if (imp->imp_invalid) {
589 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
590 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
591 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
592 imp->imp_obd->obd_name,
593 obd2cli_tgt(imp->imp_obd));
594 imp->imp_resend_replay = 1;
595 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
597 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
599 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
600 LASSERT(imp->imp_replayable);
601 imp->imp_remote_handle =
602 *lustre_msg_get_handle(request->rq_repmsg);
603 imp->imp_last_replay_transno = 0;
604 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
606 imp->imp_remote_handle =
607 *lustre_msg_get_handle(request->rq_repmsg);
608 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
611 /* Sanity checks for a reconnected import. */
612 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
613 CERROR("imp_replayable flag does not match server "
614 "after reconnect. We should LBUG right here.\n");
617 if (lustre_msg_get_last_committed(request->rq_repmsg) <
618 aa->pcaa_peer_committed) {
619 CERROR("%s went back in time (transno "LPD64
620 " was previously committed, server now claims "LPD64
621 ")! See https://bugzilla.clusterfs.com/"
622 "long_list.cgi?buglist=9646\n",
623 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
624 lustre_msg_get_last_committed(request->rq_repmsg));
628 rc = ptlrpc_import_recovery_state_machine(imp);
630 if (rc == -ENOTCONN) {
631 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
632 "invalidating and reconnecting\n",
633 obd2cli_tgt(imp->imp_obd),
634 imp->imp_connection->c_remote_uuid.uuid);
635 ptlrpc_connect_import(imp, NULL);
639 struct obd_connect_data *ocd;
640 struct obd_export *exp;
642 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF, sizeof(*ocd),
643 lustre_swab_connect);
645 spin_lock(&imp->imp_lock);
646 list_del(&imp->imp_conn_current->oic_item);
647 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
648 imp->imp_last_success_conn =
649 imp->imp_conn_current->oic_last_attempt;
652 spin_unlock(&imp->imp_lock);
653 CERROR("Wrong connect data from server\n");
658 imp->imp_connect_data = *ocd;
660 exp = class_conn2export(&imp->imp_dlm_handle);
661 spin_unlock(&imp->imp_lock);
663 /* check that server granted subset of flags we asked for. */
664 LASSERTF((ocd->ocd_connect_flags &
665 imp->imp_connect_flags_orig) ==
666 ocd->ocd_connect_flags, LPX64" != "LPX64,
667 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
670 /* This could happen if export is cleaned during the
672 spin_unlock(&imp->imp_lock);
673 CERROR("Missing export for %s\n",
674 imp->imp_obd->obd_name);
675 GOTO(out, rc = -ENODEV);
677 exp->exp_connect_flags = ocd->ocd_connect_flags;
678 class_export_put(exp);
680 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
682 if (!ocd->ocd_ibits_known &&
683 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
684 CERROR("Inodebits aware server returned zero compatible"
687 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
688 (ocd->ocd_version > LUSTRE_VERSION_CODE +
689 LUSTRE_VERSION_OFFSET_WARN ||
690 ocd->ocd_version < LUSTRE_VERSION_CODE -
691 LUSTRE_VERSION_OFFSET_WARN)) {
692 /* Sigh, some compilers do not like #ifdef in the middle
693 of macro arguments */
695 const char *action = "upgrading this client";
697 const char *action = "recompiling this application";
700 CWARN("Server %s version (%d.%d.%d.%d) is much %s. "
701 "Consider %s (%s).\n",
702 obd2cli_tgt(imp->imp_obd),
703 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
704 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
705 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
706 OBD_OCD_VERSION_FIX(ocd->ocd_version),
707 ocd->ocd_version > LUSTRE_VERSION_CODE ?
709 action, LUSTRE_VERSION_STRING);
712 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
713 cli->cl_max_pages_per_rpc =
714 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
717 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
718 (cli->cl_max_pages_per_rpc > 0));
723 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
724 if (aa->pcaa_initial_connect && !imp->imp_initial_recov)
725 ptlrpc_deactivate_import(imp);
727 if (imp->imp_recon_bk && imp->imp_last_recon) {
728 /* Give up trying to reconnect */
729 imp->imp_obd->obd_no_recov = 1;
730 ptlrpc_deactivate_import(imp);
734 struct obd_connect_data *ocd;
735 ocd = lustre_swab_repbuf(request, REPLY_REC_OFF,
737 lustre_swab_connect);
739 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
740 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
741 /* Actually servers are only supposed to refuse
742 connection from liblustre clients, so we should
743 never see this from VFS context */
744 CERROR("Server %s version (%d.%d.%d.%d) "
745 "refused connection from this client "
746 "as too %s version (%s). Client must "
748 obd2cli_tgt(imp->imp_obd),
749 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
750 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
751 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
752 OBD_OCD_VERSION_FIX(ocd->ocd_version),
753 ocd->ocd_version > LUSTRE_VERSION_CODE ?
755 LUSTRE_VERSION_STRING);
756 ptlrpc_deactivate_import(imp);
757 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
762 ptlrpc_maybe_ping_import_soon(imp);
764 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
765 obd2cli_tgt(imp->imp_obd),
766 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
769 imp->imp_last_recon = 0;
771 cfs_waitq_signal(&imp->imp_recovery_waitq);
775 static int completed_replay_interpret(struct ptlrpc_request *req,
779 atomic_dec(&req->rq_import->imp_replay_inflight);
780 if (req->rq_status == 0) {
781 ptlrpc_import_recovery_state_machine(req->rq_import);
783 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
785 req->rq_import->imp_obd->obd_name, req->rq_status);
786 ptlrpc_connect_import(req->rq_import, NULL);
792 static int signal_completed_replay(struct obd_import *imp)
794 struct ptlrpc_request *req;
797 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
798 atomic_inc(&imp->imp_replay_inflight);
800 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, OBD_PING, 1, NULL, NULL);
802 atomic_dec(&imp->imp_replay_inflight);
806 ptlrpc_req_set_repsize(req, 1, NULL);
807 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
808 lustre_msg_add_flags(req->rq_reqmsg, MSG_LAST_REPLAY);
809 req->rq_timeout *= 3;
810 req->rq_interpret_reply = completed_replay_interpret;
812 ptlrpcd_add_req(req);
817 static int ptlrpc_invalidate_import_thread(void *data)
819 struct obd_import *imp = data;
823 ptlrpc_daemonize("ll_imp_inval");
825 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
826 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
827 imp->imp_connection->c_remote_uuid.uuid);
829 ptlrpc_invalidate_import(imp);
831 if (obd_dump_on_eviction) {
832 CERROR("dump the log upon eviction\n");
833 libcfs_debug_dumplog();
836 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
837 ptlrpc_import_recovery_state_machine(imp);
843 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
851 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
852 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
853 &target_start, &target_len);
854 /* Don't care about MGC eviction */
855 if (strcmp(imp->imp_obd->obd_type->typ_name,
856 LUSTRE_MGC_NAME) != 0) {
857 LCONSOLE_ERROR("This client was evicted by %.*s; "
858 "in progress operations using this "
859 "service will fail.\n",
860 target_len, target_start);
862 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
863 obd2cli_tgt(imp->imp_obd),
864 imp->imp_connection->c_remote_uuid.uuid);
867 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
868 CLONE_VM | CLONE_FILES);
870 CERROR("error starting invalidate thread: %d\n", rc);
875 ptlrpc_invalidate_import(imp);
877 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
881 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
882 CDEBUG(D_HA, "replay requested by %s\n",
883 obd2cli_tgt(imp->imp_obd));
884 rc = ptlrpc_replay_next(imp, &inflight);
886 atomic_read(&imp->imp_replay_inflight) == 0) {
887 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
888 rc = ldlm_replay_locks(imp);
895 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
896 if (atomic_read(&imp->imp_replay_inflight) == 0) {
897 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
898 rc = signal_completed_replay(imp);
905 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
906 if (atomic_read(&imp->imp_replay_inflight) == 0) {
907 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
911 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
912 CDEBUG(D_HA, "reconnected to %s@%s\n",
913 obd2cli_tgt(imp->imp_obd),
914 imp->imp_connection->c_remote_uuid.uuid);
916 rc = ptlrpc_resend(imp);
919 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
920 ptlrpc_activate_import(imp);
922 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
923 &target_start, &target_len);
924 LCONSOLE_INFO("%s: Connection restored to service %.*s "
925 "using nid %s.\n", imp->imp_obd->obd_name,
926 target_len, target_start,
927 libcfs_nid2str(imp->imp_connection->c_peer.nid));
930 if (imp->imp_state == LUSTRE_IMP_FULL) {
931 cfs_waitq_signal(&imp->imp_recovery_waitq);
932 ptlrpc_wake_delayed(imp);
939 static int back_to_sleep(void *unused)
944 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
946 struct ptlrpc_request *req;
950 switch (imp->imp_connect_op) {
951 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
952 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
953 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
955 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
956 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
960 if (ptlrpc_import_in_recovery(imp)) {
961 struct l_wait_info lwi;
962 cfs_duration_t timeout = cfs_time_seconds(obd_timeout);
964 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
965 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
966 rc = l_wait_event(imp->imp_recovery_waitq,
967 !ptlrpc_import_in_recovery(imp), &lwi);
971 spin_lock(&imp->imp_lock);
972 if (imp->imp_state != LUSTRE_IMP_FULL)
975 spin_unlock(&imp->imp_lock);
977 req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, rq_opc, 1, NULL, NULL);
979 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
980 * it fails. We can get through the above with a down server
981 * if the client doesn't know the server is gone yet. */
982 req->rq_no_resend = 1;
984 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
985 req->rq_send_state = LUSTRE_IMP_CONNECTING;
986 ptlrpc_req_set_repsize(req, 1, NULL);
987 rc = ptlrpc_queue_wait(req);
988 ptlrpc_req_finished(req);
991 spin_lock(&imp->imp_lock);
994 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
996 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
997 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
998 spin_unlock(&imp->imp_lock);