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));
166 /* Must be called with imp_lock held! */
167 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
170 LASSERT_SPIN_LOCKED(&imp->imp_lock);
172 if (imp->imp_invalid) {
173 spin_unlock(&imp->imp_lock);
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);
183 ptlrpc_abort_inflight(imp);
184 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
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.
193 void ptlrpc_deactivate_import(struct obd_import *imp)
195 spin_lock(&imp->imp_lock);
196 ptlrpc_deactivate_and_unlock_import(imp);
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,
205 void ptlrpc_invalidate_import(struct obd_import *imp)
207 struct l_wait_info lwi;
210 atomic_inc(&imp->imp_inval_count);
212 ptlrpc_deactivate_import(imp);
214 LASSERT(imp->imp_invalid);
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)),
219 rc = l_wait_event(imp->imp_recovery_waitq,
220 (atomic_read(&imp->imp_inflight) == 0), &lwi);
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));
227 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
228 sptlrpc_import_flush_all_ctx(imp);
230 atomic_dec(&imp->imp_inval_count);
231 cfs_waitq_signal(&imp->imp_recovery_waitq);
234 /* unset imp_invalid */
235 void ptlrpc_activate_import(struct obd_import *imp)
237 struct obd_device *obd = imp->imp_obd;
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);
246 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
250 LASSERT(!imp->imp_dlm_fake);
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);
262 CDEBUG(D_HA, "%s: waking up pinger\n",
263 obd2cli_tgt(imp->imp_obd));
265 spin_lock(&imp->imp_lock);
266 imp->imp_force_verify = 1;
267 spin_unlock(&imp->imp_lock);
269 ptlrpc_pinger_wake_up();
274 int ptlrpc_reconnect_import(struct obd_import *imp)
277 ptlrpc_set_import_discon(imp, 0);
278 /* Force a new connect attempt */
279 ptlrpc_invalidate_import(imp);
280 /* Do a fresh connect next time by zeroing the handle */
281 ptlrpc_disconnect_import(imp, 1);
282 /* Wait for all invalidate calls to finish */
283 if (atomic_read(&imp->imp_inval_count) > 0) {
285 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
286 rc = l_wait_event(imp->imp_recovery_waitq,
287 (atomic_read(&imp->imp_inval_count) == 0),
290 CERROR("Interrupted, inval=%d\n",
291 atomic_read(&imp->imp_inval_count));
294 /* Allow reconnect attempts */
295 imp->imp_obd->obd_no_recov = 0;
296 /* Remove 'invalid' flag */
297 ptlrpc_activate_import(imp);
298 /* Attempt a new connect */
299 ptlrpc_recover_import(imp, NULL);
303 EXPORT_SYMBOL(ptlrpc_reconnect_import);
305 static int import_select_connection(struct obd_import *imp)
307 struct obd_import_conn *imp_conn = NULL, *conn;
308 struct obd_export *dlmexp;
311 spin_lock(&imp->imp_lock);
313 if (list_empty(&imp->imp_conn_list)) {
314 CERROR("%s: no connections available\n",
315 imp->imp_obd->obd_name);
316 spin_unlock(&imp->imp_lock);
320 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
321 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
322 imp->imp_obd->obd_name,
323 libcfs_nid2str(conn->oic_conn->c_peer.nid),
324 conn->oic_last_attempt);
325 /* Throttle the reconnect rate to once per RECONNECT_INTERVAL */
326 if (cfs_time_before_64(conn->oic_last_attempt +
327 RECONNECT_INTERVAL * HZ,
328 cfs_time_current_64())) {
329 /* If we have never tried this connection since the
330 the last successful attempt, go with this one */
331 if (cfs_time_beforeq_64(conn->oic_last_attempt,
332 imp->imp_last_success_conn)) {
337 /* Both of these connections have already been tried
338 since the last successful connection; just choose the
339 least recently used */
342 else if (cfs_time_before_64(conn->oic_last_attempt,
343 imp_conn->oic_last_attempt))
348 /* if not found, simply choose the current one */
350 LASSERT(imp->imp_conn_current);
351 imp_conn = imp->imp_conn_current;
353 LASSERT(imp_conn->oic_conn);
355 imp_conn->oic_last_attempt = cfs_time_current_64();
357 /* switch connection, don't mind if it's same as the current one */
358 if (imp->imp_connection)
359 ptlrpc_put_connection(imp->imp_connection);
360 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
362 dlmexp = class_conn2export(&imp->imp_dlm_handle);
363 LASSERT(dlmexp != NULL);
364 if (dlmexp->exp_connection)
365 ptlrpc_put_connection(dlmexp->exp_connection);
366 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
367 class_export_put(dlmexp);
369 if (imp->imp_conn_current != imp_conn) {
370 if (imp->imp_conn_current)
371 LCONSOLE_INFO("Changing connection for %s to %s/%s\n",
372 imp->imp_obd->obd_name,
373 imp_conn->oic_uuid.uuid,
374 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
375 imp->imp_conn_current = imp_conn;
378 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
379 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
380 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
382 spin_unlock(&imp->imp_lock);
388 * must be called under imp_lock
390 int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
392 struct ptlrpc_request *req;
393 struct list_head *tmp;
395 if (list_empty(&imp->imp_replay_list))
397 tmp = imp->imp_replay_list.next;
398 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
399 *transno = req->rq_transno;
400 if (req->rq_transno == 0) {
401 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
408 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
410 struct obd_device *obd = imp->imp_obd;
411 int initial_connect = 0;
413 __u64 committed_before_reconnect = 0;
414 struct ptlrpc_request *request;
415 char *bufs[] = { NULL,
416 obd2cli_tgt(imp->imp_obd),
418 (char *)&imp->imp_dlm_handle,
419 (char *)&imp->imp_connect_data };
420 struct ptlrpc_connect_async_args *aa;
424 spin_lock(&imp->imp_lock);
425 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
426 spin_unlock(&imp->imp_lock);
427 CERROR("can't connect to a closed import\n");
429 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
430 spin_unlock(&imp->imp_lock);
431 CERROR("already connected\n");
433 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
434 spin_unlock(&imp->imp_lock);
435 CERROR("already connecting\n");
439 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
442 imp->imp_resend_replay = 0;
444 if (!lustre_handle_is_used(&imp->imp_remote_handle))
447 committed_before_reconnect = imp->imp_peer_committed_transno;
449 set_transno = ptlrpc_first_transno(imp, &imp->imp_connect_data.ocd_transno);
450 spin_unlock(&imp->imp_lock);
453 struct obd_uuid uuid;
455 obd_str2uuid(&uuid, new_uuid);
456 rc = import_set_conn_priority(imp, &uuid);
461 rc = import_select_connection(imp);
465 /* last in connection list */
466 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
467 if (imp->imp_initial_recov_bk && initial_connect) {
468 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
469 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
470 /* Don't retry if connect fails */
472 obd_set_info_async(obd->obd_self_export,
473 strlen(KEY_INIT_RECOV),
475 sizeof(rc), &rc, NULL);
477 if (imp->imp_recon_bk) {
478 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
479 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
480 spin_lock(&imp->imp_lock);
481 imp->imp_last_recon = 1;
482 spin_unlock(&imp->imp_lock);
486 rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
490 /* Reset connect flags to the originally requested flags, in case
491 * the server is updated on-the-fly we will get the new features. */
492 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
493 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
494 &obd->obd_uuid, &imp->imp_connect_data);
498 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
500 GOTO(out, rc = -ENOMEM);
502 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
503 imp->imp_connect_op, bufs, NULL);
505 ptlrpc_request_free(request);
510 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
512 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
514 request->rq_send_state = LUSTRE_IMP_CONNECTING;
515 /* Allow a slightly larger reply for future growth compatibility */
516 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
517 sizeof(struct obd_connect_data)+16*sizeof(__u64));
518 ptlrpc_request_set_replen(request);
519 request->rq_interpret_reply = ptlrpc_connect_interpret;
521 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
522 aa = (struct ptlrpc_connect_async_args *)&request->rq_async_args;
523 memset(aa, 0, sizeof *aa);
525 aa->pcaa_peer_committed = committed_before_reconnect;
526 aa->pcaa_initial_connect = initial_connect;
528 if (aa->pcaa_initial_connect) {
529 spin_lock(&imp->imp_lock);
530 imp->imp_replayable = 1;
531 spin_unlock(&imp->imp_lock);
532 /* On an initial connect, we don't know which one of a
533 failover server pair is up. Don't wait long. */
535 request->rq_timeout = max((int)(obd_timeout / 2), 5);
537 request->rq_timeout = max((int)(obd_timeout / 20), 5);
539 lustre_msg_add_op_flags(request->rq_reqmsg,
540 MSG_CONNECT_INITIAL);
544 lustre_msg_add_op_flags(request->rq_reqmsg,
545 MSG_CONNECT_TRANSNO);
547 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
548 ptlrpcd_add_req(request);
552 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
557 EXPORT_SYMBOL(ptlrpc_connect_import);
559 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
562 struct obd_import_conn *imp_conn;
568 spin_lock(&imp->imp_lock);
569 if (list_empty(&imp->imp_conn_list))
573 imp_conn = list_entry(imp->imp_conn_list.prev,
574 struct obd_import_conn,
577 /* XXX: When the failover node is the primary node, it is possible
578 * to have two identical connections in imp_conn_list. We must
579 * compare not conn's pointers but NIDs, otherwise we can defeat
580 * connection throttling. (See bug 14774.) */
581 if (imp->imp_conn_current->oic_conn->c_self !=
582 imp_conn->oic_conn->c_self) {
583 ptlrpc_ping_import_soon(imp);
587 /* liblustre has no pinger thead, so we wakup pinger anyway */
592 spin_unlock(&imp->imp_lock);
595 ptlrpc_pinger_wake_up();
600 static int ptlrpc_connect_interpret(struct ptlrpc_request *request,
603 struct ptlrpc_connect_async_args *aa = data;
604 struct obd_import *imp = request->rq_import;
605 struct client_obd *cli = &imp->imp_obd->u.cli;
606 struct lustre_handle old_hdl;
610 spin_lock(&imp->imp_lock);
611 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
612 spin_unlock(&imp->imp_lock);
615 spin_unlock(&imp->imp_lock);
620 LASSERT(imp->imp_conn_current);
622 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
624 /* All imports are pingable */
625 spin_lock(&imp->imp_lock);
626 imp->imp_pingable = 1;
628 if (aa->pcaa_initial_connect) {
629 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
630 imp->imp_replayable = 1;
631 spin_unlock(&imp->imp_lock);
632 CDEBUG(D_HA, "connected to replayable target: %s\n",
633 obd2cli_tgt(imp->imp_obd));
635 imp->imp_replayable = 0;
636 spin_unlock(&imp->imp_lock);
639 if (msg_flags & MSG_CONNECT_NEXT_VER) {
640 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
641 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v2\n",
642 obd2cli_tgt(imp->imp_obd));
644 CDEBUG(D_RPCTRACE, "connect to %s with lustre_msg_v1\n",
645 obd2cli_tgt(imp->imp_obd));
648 imp->imp_remote_handle =
649 *lustre_msg_get_handle(request->rq_repmsg);
651 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
652 ptlrpc_activate_import(imp);
653 GOTO(finish, rc = 0);
655 spin_unlock(&imp->imp_lock);
658 /* Determine what recovery state to move the import to. */
659 if (MSG_CONNECT_RECONNECT & msg_flags) {
660 memset(&old_hdl, 0, sizeof(old_hdl));
661 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
663 CERROR("%s@%s didn't like our handle "LPX64
664 ", failed\n", obd2cli_tgt(imp->imp_obd),
665 imp->imp_connection->c_remote_uuid.uuid,
666 imp->imp_dlm_handle.cookie);
667 GOTO(out, rc = -ENOTCONN);
670 if (memcmp(&imp->imp_remote_handle,
671 lustre_msg_get_handle(request->rq_repmsg),
672 sizeof(imp->imp_remote_handle))) {
674 CWARN("%s@%s changed server handle from "
675 LPX64" to "LPX64" - evicting.\n",
676 obd2cli_tgt(imp->imp_obd),
677 imp->imp_connection->c_remote_uuid.uuid,
678 imp->imp_remote_handle.cookie,
679 lustre_msg_get_handle(request->rq_repmsg)->
681 imp->imp_remote_handle =
682 *lustre_msg_get_handle(request->rq_repmsg);
684 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
685 GOTO(finish, rc = 0);
687 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
688 obd2cli_tgt(imp->imp_obd),
689 imp->imp_connection->c_remote_uuid.uuid);
692 if (imp->imp_invalid) {
693 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
694 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
695 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
696 imp->imp_obd->obd_name,
697 obd2cli_tgt(imp->imp_obd));
699 spin_lock(&imp->imp_lock);
700 imp->imp_resend_replay = 1;
701 spin_unlock(&imp->imp_lock);
703 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
705 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
707 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
708 LASSERT(imp->imp_replayable);
709 imp->imp_remote_handle =
710 *lustre_msg_get_handle(request->rq_repmsg);
711 imp->imp_last_replay_transno = 0;
712 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
714 imp->imp_remote_handle =
715 *lustre_msg_get_handle(request->rq_repmsg);
716 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
719 /* Sanity checks for a reconnected import. */
720 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
721 CERROR("imp_replayable flag does not match server "
722 "after reconnect. We should LBUG right here.\n");
725 if (lustre_msg_get_last_committed(request->rq_repmsg) <
726 aa->pcaa_peer_committed) {
727 CERROR("%s went back in time (transno "LPD64
728 " was previously committed, server now claims "LPD64
729 ")! See https://bugzilla.clusterfs.com/"
730 "long_list.cgi?buglist=9646\n",
731 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
732 lustre_msg_get_last_committed(request->rq_repmsg));
736 rc = ptlrpc_import_recovery_state_machine(imp);
738 if (rc == -ENOTCONN) {
739 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
740 "invalidating and reconnecting\n",
741 obd2cli_tgt(imp->imp_obd),
742 imp->imp_connection->c_remote_uuid.uuid);
743 ptlrpc_connect_import(imp, NULL);
747 struct obd_connect_data *ocd;
748 struct obd_export *exp;
750 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
752 /* server replied obd_connect_data is always bigger */
753 ocd = req_capsule_server_sized_get(&request->rq_pill,
754 &RMF_CONNECT_DATA, ret);
756 spin_lock(&imp->imp_lock);
757 list_del(&imp->imp_conn_current->oic_item);
758 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
759 imp->imp_last_success_conn =
760 imp->imp_conn_current->oic_last_attempt;
763 spin_unlock(&imp->imp_lock);
764 CERROR("Wrong connect data from server\n");
769 imp->imp_connect_data = *ocd;
771 exp = class_conn2export(&imp->imp_dlm_handle);
772 spin_unlock(&imp->imp_lock);
774 /* check that server granted subset of flags we asked for. */
775 LASSERTF((ocd->ocd_connect_flags &
776 imp->imp_connect_flags_orig) ==
777 ocd->ocd_connect_flags, LPX64" != "LPX64,
778 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
781 /* This could happen if export is cleaned during the
783 CERROR("Missing export for %s\n",
784 imp->imp_obd->obd_name);
785 GOTO(out, rc = -ENODEV);
787 exp->exp_connect_flags = ocd->ocd_connect_flags;
788 imp->imp_obd->obd_self_export->exp_connect_flags =
789 ocd->ocd_connect_flags;
790 class_export_put(exp);
792 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
794 if (!ocd->ocd_ibits_known &&
795 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
796 CERROR("Inodebits aware server returned zero compatible"
799 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
800 (ocd->ocd_version > LUSTRE_VERSION_CODE +
801 LUSTRE_VERSION_OFFSET_WARN ||
802 ocd->ocd_version < LUSTRE_VERSION_CODE -
803 LUSTRE_VERSION_OFFSET_WARN)) {
804 /* Sigh, some compilers do not like #ifdef in the middle
805 of macro arguments */
808 "older. Consider upgrading this client";
811 "older. Consider recompiling this application";
813 const char *newer = "newer than client version";
815 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
817 obd2cli_tgt(imp->imp_obd),
818 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
819 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
820 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
821 OBD_OCD_VERSION_FIX(ocd->ocd_version),
822 ocd->ocd_version > LUSTRE_VERSION_CODE ?
823 newer : older, LUSTRE_VERSION_STRING);
826 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
827 /* We sent to the server ocd_cksum_types with bits set
828 * for algorithms we understand. The server masked off
829 * the checksum types it doesn't support */
830 if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
831 LCONSOLE_WARN("The negotiation of the checksum "
832 "alogrithm to use with server %s "
833 "failed (%x/%x), disabling "
835 obd2cli_tgt(imp->imp_obd),
836 ocd->ocd_cksum_types,
838 cli->cl_checksum = 0;
839 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
840 cli->cl_cksum_type = OBD_CKSUM_CRC32;
842 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
844 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
845 cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
846 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
847 cli->cl_cksum_type = OBD_CKSUM_ADLER;
849 cli->cl_cksum_type = OBD_CKSUM_CRC32;
852 /* The server does not support OBD_CONNECT_CKSUM.
853 * Enforce CRC32 for backward compatibility*/
854 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
855 cli->cl_cksum_type = OBD_CKSUM_CRC32;
858 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
859 cli->cl_max_pages_per_rpc =
860 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
863 imp->imp_obd->obd_namespace->ns_connect_flags =
864 ocd->ocd_connect_flags;
865 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
866 ocd->ocd_connect_flags;
868 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
869 (cli->cl_max_pages_per_rpc > 0));
874 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
875 spin_lock(&imp->imp_lock);
876 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
877 (request->rq_import_generation == imp->imp_generation))
878 ptlrpc_deactivate_and_unlock_import(imp);
880 spin_unlock(&imp->imp_lock);
882 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
885 * Give up trying to reconnect
886 * EACCES means client has no permission for connection
888 imp->imp_obd->obd_no_recov = 1;
889 ptlrpc_deactivate_import(imp);
893 struct obd_connect_data *ocd;
895 /* reply message might not be ready */
896 if (request->rq_repmsg == NULL)
899 ocd = req_capsule_server_get(&request->rq_pill,
902 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
903 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
904 /* Actually servers are only supposed to refuse
905 connection from liblustre clients, so we should
906 never see this from VFS context */
907 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
909 " refused connection from this client "
910 "with an incompatible version (%s). "
911 "Client must be recompiled\n",
912 obd2cli_tgt(imp->imp_obd),
913 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
914 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
915 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
916 OBD_OCD_VERSION_FIX(ocd->ocd_version),
917 LUSTRE_VERSION_STRING);
918 ptlrpc_deactivate_import(imp);
919 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
924 ptlrpc_maybe_ping_import_soon(imp);
926 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
927 obd2cli_tgt(imp->imp_obd),
928 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
931 spin_lock(&imp->imp_lock);
932 imp->imp_last_recon = 0;
933 spin_unlock(&imp->imp_lock);
935 cfs_waitq_signal(&imp->imp_recovery_waitq);
939 static int completed_replay_interpret(struct ptlrpc_request *req,
943 atomic_dec(&req->rq_import->imp_replay_inflight);
944 if (req->rq_status == 0) {
945 ptlrpc_import_recovery_state_machine(req->rq_import);
947 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
949 req->rq_import->imp_obd->obd_name, req->rq_status);
950 ptlrpc_connect_import(req->rq_import, NULL);
956 static int signal_completed_replay(struct obd_import *imp)
958 struct ptlrpc_request *req;
961 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
962 atomic_inc(&imp->imp_replay_inflight);
964 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
967 atomic_dec(&imp->imp_replay_inflight);
971 ptlrpc_request_set_replen(req);
972 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
973 lustre_msg_add_flags(req->rq_reqmsg,
974 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
975 req->rq_timeout *= 3;
976 req->rq_interpret_reply = completed_replay_interpret;
978 ptlrpcd_add_req(req);
983 static int ptlrpc_invalidate_import_thread(void *data)
985 struct obd_import *imp = data;
989 ptlrpc_daemonize("ll_imp_inval");
991 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
992 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
993 imp->imp_connection->c_remote_uuid.uuid);
995 ptlrpc_invalidate_import(imp);
997 if (obd_dump_on_eviction) {
998 CERROR("dump the log upon eviction\n");
999 libcfs_debug_dumplog();
1002 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1003 ptlrpc_import_recovery_state_machine(imp);
1009 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1017 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1018 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1019 &target_start, &target_len);
1020 /* Don't care about MGC eviction */
1021 if (strcmp(imp->imp_obd->obd_type->typ_name,
1022 LUSTRE_MGC_NAME) != 0) {
1023 LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1024 "%.*s; in progress operations using "
1025 "this service will fail.\n",
1026 target_len, target_start);
1028 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1029 obd2cli_tgt(imp->imp_obd),
1030 imp->imp_connection->c_remote_uuid.uuid);
1033 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1034 CLONE_VM | CLONE_FILES);
1036 CERROR("error starting invalidate thread: %d\n", rc);
1041 ptlrpc_invalidate_import(imp);
1043 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1047 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1048 CDEBUG(D_HA, "replay requested by %s\n",
1049 obd2cli_tgt(imp->imp_obd));
1050 rc = ptlrpc_replay_next(imp, &inflight);
1051 if (inflight == 0 &&
1052 atomic_read(&imp->imp_replay_inflight) == 0) {
1053 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1054 rc = ldlm_replay_locks(imp);
1061 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1062 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1063 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1064 rc = signal_completed_replay(imp);
1071 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1072 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1073 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1077 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1078 CDEBUG(D_HA, "reconnected to %s@%s\n",
1079 obd2cli_tgt(imp->imp_obd),
1080 imp->imp_connection->c_remote_uuid.uuid);
1082 rc = ptlrpc_resend(imp);
1085 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1086 ptlrpc_activate_import(imp);
1088 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1089 &target_start, &target_len);
1090 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1091 "using nid %s.\n", imp->imp_obd->obd_name,
1092 target_len, target_start,
1093 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1096 if (imp->imp_state == LUSTRE_IMP_FULL) {
1097 cfs_waitq_signal(&imp->imp_recovery_waitq);
1098 ptlrpc_wake_delayed(imp);
1105 static int back_to_sleep(void *unused)
1110 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1112 struct ptlrpc_request *req;
1114 int nowait = imp->imp_obd->obd_force;
1118 GOTO(set_state, rc);
1120 switch (imp->imp_connect_op) {
1121 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1122 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1123 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1125 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1126 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1130 if (ptlrpc_import_in_recovery(imp)) {
1131 struct l_wait_info lwi;
1132 cfs_duration_t timeout;
1133 if (imp->imp_server_timeout)
1134 timeout = cfs_time_seconds(obd_timeout / 2);
1136 timeout = cfs_time_seconds(obd_timeout);
1138 timeout = MAX(timeout * HZ, 1);
1140 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1141 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1142 rc = l_wait_event(imp->imp_recovery_waitq,
1143 !ptlrpc_import_in_recovery(imp), &lwi);
1147 spin_lock(&imp->imp_lock);
1148 if (imp->imp_state != LUSTRE_IMP_FULL)
1151 spin_unlock(&imp->imp_lock);
1153 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1154 LUSTRE_OBD_VERSION, rq_opc);
1156 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1157 * it fails. We can get through the above with a down server
1158 * if the client doesn't know the server is gone yet. */
1159 req->rq_no_resend = 1;
1161 req->rq_timeout = obd_timeout / 3;
1163 req->rq_timeout = 5;
1165 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1166 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1167 ptlrpc_request_set_replen(req);
1168 rc = ptlrpc_queue_wait(req);
1169 ptlrpc_req_finished(req);
1173 spin_lock(&imp->imp_lock);
1176 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1178 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1179 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1180 imp->imp_conn_cnt = 0;
1181 imp->imp_last_recon = 0;
1182 spin_unlock(&imp->imp_lock);