4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/import.c
34 * Author: Mike Shaver <shaver@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <linux/kthread.h>
40 #include <obd_support.h>
41 #include <lustre_ha.h>
42 #include <lustre_net.h>
43 #include <lustre_import.h>
44 #include <lustre_export.h>
46 #include <obd_cksum.h>
47 #include <obd_class.h>
49 #include "ptlrpc_internal.h"
51 struct ptlrpc_connect_async_args {
52 __u64 pcaa_peer_committed;
53 int pcaa_initial_connect;
57 * Updates import \a imp current state to provided \a state value
58 * Helper function. Must be called under imp_lock.
60 static void __import_set_state(struct obd_import *imp,
61 enum lustre_imp_state state)
64 case LUSTRE_IMP_CLOSED:
66 case LUSTRE_IMP_DISCON:
67 case LUSTRE_IMP_CONNECTING:
69 case LUSTRE_IMP_REPLAY_WAIT:
70 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
73 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
75 imp->imp_state = state;
76 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
77 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
78 ktime_get_real_seconds();
79 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
83 /* A CLOSED import should remain so. */
84 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
86 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
87 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
88 imp, obd2cli_tgt(imp->imp_obd), \
89 ptlrpc_import_state_name(imp->imp_state), \
90 ptlrpc_import_state_name(state)); \
91 __import_set_state(imp, state); \
95 #define IMPORT_SET_STATE(imp, state) \
97 spin_lock(&imp->imp_lock); \
98 IMPORT_SET_STATE_NOLOCK(imp, state); \
99 spin_unlock(&imp->imp_lock); \
102 void ptlrpc_import_enter_resend(struct obd_import *imp)
104 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
106 EXPORT_SYMBOL(ptlrpc_import_enter_resend);
109 static int ptlrpc_connect_interpret(const struct lu_env *env,
110 struct ptlrpc_request *request,
111 void * data, int rc);
112 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
114 /* Only this function is allowed to change the import state when it is
115 * CLOSED. I would rather refcount the import and free it after
116 * disconnection like we do with exports. To do that, the client_obd
117 * will need to save the peer info somewhere other than in the import,
119 int ptlrpc_init_import(struct obd_import *imp)
121 spin_lock(&imp->imp_lock);
123 imp->imp_generation++;
124 imp->imp_state = LUSTRE_IMP_NEW;
126 spin_unlock(&imp->imp_lock);
130 EXPORT_SYMBOL(ptlrpc_init_import);
132 #define UUID_STR "_UUID"
133 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
135 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
136 ? uuid : uuid + strlen(prefix);
138 *uuid_len = strlen(*uuid_start);
140 if (*uuid_len < strlen(UUID_STR))
143 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
144 UUID_STR, strlen(UUID_STR)))
145 *uuid_len -= strlen(UUID_STR);
149 * Returns true if import was FULL, false if import was already not
151 * @imp - import to be disconnected
152 * @conn_cnt - connection count (epoch) of the request that timed out
153 * and caused the disconnection. In some cases, multiple
154 * inflight requests can fail to a single target (e.g. OST
155 * bulk requests) and if one has already caused a reconnection
156 * (increasing the import->conn_cnt) the older failure should
157 * not also cause a reconnection. If zero it forces a reconnect.
159 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
163 spin_lock(&imp->imp_lock);
165 if (imp->imp_state == LUSTRE_IMP_FULL &&
166 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
170 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
171 &target_start, &target_len);
173 if (imp->imp_replayable) {
174 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
175 "lost; in progress operations using this "
176 "service will wait for recovery to complete\n",
177 imp->imp_obd->obd_name, target_len, target_start,
178 libcfs_nid2str(imp->imp_connection->c_peer.nid));
180 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
181 "%.*s (at %s) was lost; in progress "
182 "operations using this service will fail\n",
183 imp->imp_obd->obd_name,
184 target_len, target_start,
185 libcfs_nid2str(imp->imp_connection->c_peer.nid));
187 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
188 spin_unlock(&imp->imp_lock);
190 if (obd_dump_on_timeout)
191 libcfs_debug_dumplog();
193 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
196 spin_unlock(&imp->imp_lock);
197 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
198 imp->imp_client->cli_name, imp,
199 (imp->imp_state == LUSTRE_IMP_FULL &&
200 imp->imp_conn_cnt > conn_cnt) ?
201 "reconnected" : "not connected", imp->imp_conn_cnt,
202 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
208 /* Must be called with imp_lock held! */
209 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
212 assert_spin_locked(&imp->imp_lock);
214 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
215 imp->imp_invalid = 1;
216 imp->imp_generation++;
217 spin_unlock(&imp->imp_lock);
219 ptlrpc_abort_inflight(imp);
220 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
226 * This acts as a barrier; all existing requests are rejected, and
227 * no new requests will be accepted until the import is valid again.
229 void ptlrpc_deactivate_import(struct obd_import *imp)
231 spin_lock(&imp->imp_lock);
232 ptlrpc_deactivate_and_unlock_import(imp);
234 EXPORT_SYMBOL(ptlrpc_deactivate_import);
236 static time64_t ptlrpc_inflight_deadline(struct ptlrpc_request *req,
241 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
242 (req->rq_phase == RQ_PHASE_BULK) ||
243 (req->rq_phase == RQ_PHASE_NEW)))
246 if (req->rq_timedout)
249 if (req->rq_phase == RQ_PHASE_NEW)
252 dl = req->rq_deadline;
260 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
262 time64_t now = ktime_get_real_seconds();
263 struct list_head *tmp, *n;
264 struct ptlrpc_request *req;
265 time64_t timeout = 0;
267 spin_lock(&imp->imp_lock);
268 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
269 req = list_entry(tmp, struct ptlrpc_request, rq_list);
270 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
272 spin_unlock(&imp->imp_lock);
277 * This function will invalidate the import, if necessary, then block
278 * for all the RPC completions, and finally notify the obd to
279 * invalidate its state (ie cancel locks, clear pending requests,
282 void ptlrpc_invalidate_import(struct obd_import *imp)
284 struct list_head *tmp, *n;
285 struct ptlrpc_request *req;
286 struct l_wait_info lwi;
290 atomic_inc(&imp->imp_inval_count);
292 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
293 ptlrpc_deactivate_import(imp);
295 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
296 LASSERT(imp->imp_invalid);
298 /* Wait forever until inflight == 0. We really can't do it another
299 * way because in some cases we need to wait for very long reply
300 * unlink. We can't do anything before that because there is really
301 * no guarantee that some rdma transfer is not in progress right now. */
303 long timeout_jiffies;
305 /* Calculate max timeout for waiting on rpcs to error
306 * out. Use obd_timeout if calculated value is smaller
309 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
310 timeout = ptlrpc_inflight_timeout(imp);
311 timeout += timeout / 3;
314 timeout = obd_timeout;
316 /* decrease the interval to increase race condition */
320 CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
323 /* Wait for all requests to error out and call completion
324 * callbacks. Cap it at obd_timeout -- these should all
325 * have been locally cancelled by ptlrpc_abort_inflight.
327 timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
328 lwi = LWI_TIMEOUT_INTERVAL(timeout_jiffies,
329 (timeout > 1) ? cfs_time_seconds(1) :
330 cfs_time_seconds(1) / 2,
332 rc = l_wait_event(imp->imp_recovery_waitq,
333 (atomic_read(&imp->imp_inflight) == 0),
336 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
338 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
339 cli_tgt, rc, atomic_read(&imp->imp_inflight));
341 spin_lock(&imp->imp_lock);
342 if (atomic_read(&imp->imp_inflight) == 0) {
343 int count = atomic_read(&imp->imp_unregistering);
345 /* We know that "unregistering" rpcs only can
346 * survive in sending or delaying lists (they
347 * maybe waiting for long reply unlink in
348 * sluggish nets). Let's check this. If there
349 * is no inflight and unregistering != 0, this
351 LASSERTF(count == 0, "Some RPCs are still "
352 "unregistering: %d\n", count);
354 /* Let's save one loop as soon as inflight have
355 * dropped to zero. No new inflights possible at
359 list_for_each_safe(tmp, n,
360 &imp->imp_sending_list) {
361 req = list_entry(tmp,
362 struct ptlrpc_request,
364 DEBUG_REQ(D_ERROR, req,
365 "still on sending list");
367 list_for_each_safe(tmp, n,
368 &imp->imp_delayed_list) {
369 req = list_entry(tmp,
370 struct ptlrpc_request,
372 DEBUG_REQ(D_ERROR, req,
373 "still on delayed list");
376 CERROR("%s: Unregistering RPCs found (%d). "
377 "Network is sluggish? Waiting them "
378 "to error out.\n", cli_tgt,
379 atomic_read(&imp->imp_unregistering));
381 spin_unlock(&imp->imp_lock);
386 * Let's additionally check that no new rpcs added to import in
387 * "invalidate" state.
389 LASSERT(atomic_read(&imp->imp_inflight) == 0);
390 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
391 sptlrpc_import_flush_all_ctx(imp);
393 atomic_dec(&imp->imp_inval_count);
394 wake_up_all(&imp->imp_recovery_waitq);
396 EXPORT_SYMBOL(ptlrpc_invalidate_import);
398 /* unset imp_invalid */
399 void ptlrpc_activate_import(struct obd_import *imp)
401 struct obd_device *obd = imp->imp_obd;
403 spin_lock(&imp->imp_lock);
404 if (imp->imp_deactive != 0) {
405 spin_unlock(&imp->imp_lock);
409 imp->imp_invalid = 0;
410 spin_unlock(&imp->imp_lock);
411 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
413 EXPORT_SYMBOL(ptlrpc_activate_import);
415 void ptlrpc_pinger_force(struct obd_import *imp)
417 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
418 ptlrpc_import_state_name(imp->imp_state));
420 spin_lock(&imp->imp_lock);
421 imp->imp_force_verify = 1;
422 spin_unlock(&imp->imp_lock);
424 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
425 ptlrpc_pinger_wake_up();
427 EXPORT_SYMBOL(ptlrpc_pinger_force);
429 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
433 LASSERT(!imp->imp_dlm_fake);
435 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
436 if (!imp->imp_replayable) {
437 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
438 "auto-deactivating\n",
439 obd2cli_tgt(imp->imp_obd),
440 imp->imp_connection->c_remote_uuid.uuid,
441 imp->imp_obd->obd_name);
442 ptlrpc_deactivate_import(imp);
445 ptlrpc_pinger_force(imp);
450 int ptlrpc_reconnect_import(struct obd_import *imp)
453 long timeout_jiffies = cfs_time_seconds(obd_timeout);
454 struct l_wait_info lwi;
457 ptlrpc_pinger_force(imp);
459 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
460 obd2cli_tgt(imp->imp_obd), obd_timeout);
462 lwi = LWI_TIMEOUT(timeout_jiffies, NULL, NULL);
463 rc = l_wait_event(imp->imp_recovery_waitq,
464 !ptlrpc_import_in_recovery(imp), &lwi);
465 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
466 ptlrpc_import_state_name(imp->imp_state));
469 ptlrpc_set_import_discon(imp, 0);
470 /* Force a new connect attempt */
471 ptlrpc_invalidate_import(imp);
472 /* Do a fresh connect next time by zeroing the handle */
473 ptlrpc_disconnect_import(imp, 1);
474 /* Wait for all invalidate calls to finish */
475 if (atomic_read(&imp->imp_inval_count) > 0) {
476 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
479 rc = l_wait_event(imp->imp_recovery_waitq,
480 (atomic_read(&imp->imp_inval_count) == 0),
483 CERROR("Interrupted, inval=%d\n",
484 atomic_read(&imp->imp_inval_count));
487 /* Allow reconnect attempts */
488 imp->imp_obd->obd_no_recov = 0;
489 /* Remove 'invalid' flag */
490 ptlrpc_activate_import(imp);
491 /* Attempt a new connect */
492 ptlrpc_recover_import(imp, NULL, 0);
496 EXPORT_SYMBOL(ptlrpc_reconnect_import);
499 * Connection on import \a imp is changed to another one (if more than one is
500 * present). We typically chose connection that we have not tried to connect to
503 static int import_select_connection(struct obd_import *imp)
505 struct obd_import_conn *imp_conn = NULL, *conn;
506 struct obd_export *dlmexp;
508 int target_len, tried_all = 1;
511 spin_lock(&imp->imp_lock);
513 if (list_empty(&imp->imp_conn_list)) {
514 CERROR("%s: no connections available\n",
515 imp->imp_obd->obd_name);
516 spin_unlock(&imp->imp_lock);
520 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
521 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
522 imp->imp_obd->obd_name,
523 libcfs_nid2str(conn->oic_conn->c_peer.nid),
524 conn->oic_last_attempt);
526 /* If we have not tried this connection since
527 the last successful attempt, go with this one */
528 if ((conn->oic_last_attempt == 0) ||
529 conn->oic_last_attempt <= imp->imp_last_success_conn) {
535 /* If all of the connections have already been tried
536 since the last successful connection; just choose the
537 least recently used */
540 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
544 /* if not found, simply choose the current one */
545 if (!imp_conn || imp->imp_force_reconnect) {
546 LASSERT(imp->imp_conn_current);
547 imp_conn = imp->imp_conn_current;
550 LASSERT(imp_conn->oic_conn);
552 /* If we've tried everything, and we're back to the beginning of the
553 list, increase our timeout and try again. It will be reset when
554 we do finally connect. (FIXME: really we should wait for all network
555 state associated with the last connection attempt to drain before
556 trying to reconnect on it.) */
557 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
558 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
559 if (at_get(at) < CONNECTION_SWITCH_MAX) {
560 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
561 if (at_get(at) > CONNECTION_SWITCH_MAX)
562 at_reset(at, CONNECTION_SWITCH_MAX);
564 LASSERT(imp_conn->oic_last_attempt);
565 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
566 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
569 imp_conn->oic_last_attempt = ktime_get_seconds();
571 /* switch connection, don't mind if it's same as the current one */
572 if (imp->imp_connection)
573 ptlrpc_connection_put(imp->imp_connection);
574 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
576 dlmexp = class_conn2export(&imp->imp_dlm_handle);
577 LASSERT(dlmexp != NULL);
578 if (dlmexp->exp_connection)
579 ptlrpc_connection_put(dlmexp->exp_connection);
580 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
581 class_export_put(dlmexp);
583 if (imp->imp_conn_current != imp_conn) {
584 if (imp->imp_conn_current) {
585 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
586 &target_start, &target_len);
588 CDEBUG(D_HA, "%s: Connection changing to"
590 imp->imp_obd->obd_name,
591 target_len, target_start,
592 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
595 imp->imp_conn_current = imp_conn;
598 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
599 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
600 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
602 spin_unlock(&imp->imp_lock);
608 * must be called under imp_lock
610 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
612 struct ptlrpc_request *req;
613 struct list_head *tmp;
615 /* The requests in committed_list always have smaller transnos than
616 * the requests in replay_list */
617 if (!list_empty(&imp->imp_committed_list)) {
618 tmp = imp->imp_committed_list.next;
619 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
620 *transno = req->rq_transno;
621 if (req->rq_transno == 0) {
622 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
627 if (!list_empty(&imp->imp_replay_list)) {
628 tmp = imp->imp_replay_list.next;
629 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
630 *transno = req->rq_transno;
631 if (req->rq_transno == 0) {
632 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
641 * Attempt to (re)connect import \a imp. This includes all preparations,
642 * initializing CONNECT RPC request and passing it to ptlrpcd for
644 * Returns 0 on success or error code.
646 int ptlrpc_connect_import(struct obd_import *imp)
648 struct obd_device *obd = imp->imp_obd;
649 int initial_connect = 0;
651 __u64 committed_before_reconnect = 0;
652 struct ptlrpc_request *request;
653 char *bufs[] = { NULL,
654 obd2cli_tgt(imp->imp_obd),
656 (char *)&imp->imp_dlm_handle,
657 (char *)&imp->imp_connect_data };
658 struct ptlrpc_connect_async_args *aa;
662 spin_lock(&imp->imp_lock);
663 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
664 spin_unlock(&imp->imp_lock);
665 CERROR("can't connect to a closed import\n");
667 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
668 spin_unlock(&imp->imp_lock);
669 CERROR("already connected\n");
671 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
672 imp->imp_connected) {
673 spin_unlock(&imp->imp_lock);
674 CERROR("already connecting\n");
678 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
681 imp->imp_resend_replay = 0;
683 if (!lustre_handle_is_used(&imp->imp_remote_handle))
686 committed_before_reconnect = imp->imp_peer_committed_transno;
688 set_transno = ptlrpc_first_transno(imp,
689 &imp->imp_connect_data.ocd_transno);
690 spin_unlock(&imp->imp_lock);
692 rc = import_select_connection(imp);
696 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
700 /* Reset connect flags to the originally requested flags, in case
701 * the server is updated on-the-fly we will get the new features. */
702 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
703 imp->imp_connect_data.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
704 /* Reset ocd_version each time so the server knows the exact versions */
705 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
706 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
707 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
709 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
710 &obd->obd_uuid, &imp->imp_connect_data, NULL);
714 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
716 GOTO(out, rc = -ENOMEM);
718 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
719 imp->imp_connect_op, bufs, NULL);
721 ptlrpc_request_free(request);
725 /* Report the rpc service time to the server so that it knows how long
726 * to wait for clients to join recovery */
727 lustre_msg_set_service_time(request->rq_reqmsg,
728 at_timeout2est(request->rq_timeout));
730 /* The amount of time we give the server to process the connect req.
731 * import_select_connection will increase the net latency on
732 * repeated reconnect attempts to cover slow networks.
733 * We override/ignore the server rpc completion estimate here,
734 * which may be large if this is a reconnect attempt */
735 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
736 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
738 request->rq_no_resend = request->rq_no_delay = 1;
739 request->rq_send_state = LUSTRE_IMP_CONNECTING;
740 /* Allow a slightly larger reply for future growth compatibility */
741 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
742 sizeof(struct obd_connect_data)+16*sizeof(__u64));
743 ptlrpc_request_set_replen(request);
744 request->rq_interpret_reply = ptlrpc_connect_interpret;
746 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
747 aa = ptlrpc_req_async_args(request);
748 memset(aa, 0, sizeof *aa);
750 aa->pcaa_peer_committed = committed_before_reconnect;
751 aa->pcaa_initial_connect = initial_connect;
753 if (aa->pcaa_initial_connect) {
754 spin_lock(&imp->imp_lock);
755 imp->imp_replayable = 1;
756 spin_unlock(&imp->imp_lock);
757 lustre_msg_add_op_flags(request->rq_reqmsg,
758 MSG_CONNECT_INITIAL);
762 lustre_msg_add_op_flags(request->rq_reqmsg,
763 MSG_CONNECT_TRANSNO);
765 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %ld)",
766 request->rq_timeout);
767 ptlrpcd_add_req(request);
771 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
775 EXPORT_SYMBOL(ptlrpc_connect_import);
777 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
781 spin_lock(&imp->imp_lock);
782 force_verify = imp->imp_force_verify != 0;
783 spin_unlock(&imp->imp_lock);
786 ptlrpc_pinger_wake_up();
789 static int ptlrpc_busy_reconnect(int rc)
791 return (rc == -EBUSY) || (rc == -EAGAIN);
794 static int ptlrpc_connect_set_flags(struct obd_import *imp,
795 struct obd_connect_data *ocd,
796 __u64 old_connect_flags,
797 struct obd_export *exp, int init_connect)
800 struct client_obd *cli = &imp->imp_obd->u.cli;
802 spin_lock(&imp->imp_lock);
803 list_del(&imp->imp_conn_current->oic_item);
804 list_add(&imp->imp_conn_current->oic_item,
805 &imp->imp_conn_list);
806 imp->imp_last_success_conn =
807 imp->imp_conn_current->oic_last_attempt;
809 spin_unlock(&imp->imp_lock);
811 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
812 (ocd->ocd_version > LUSTRE_VERSION_CODE +
813 LUSTRE_VERSION_OFFSET_WARN ||
814 ocd->ocd_version < LUSTRE_VERSION_CODE -
815 LUSTRE_VERSION_OFFSET_WARN)) {
816 /* Sigh, some compilers do not like #ifdef in the middle
817 of macro arguments */
818 const char *older = "older than client. "
819 "Consider upgrading server";
820 const char *newer = "newer than client. "
821 "Consider upgrading client";
823 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
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 ocd->ocd_version > LUSTRE_VERSION_CODE ?
831 newer : older, LUSTRE_VERSION_STRING);
835 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
836 /* Check if server has LU-1252 fix applied to not always swab
837 * the IR MNE entries. Do this only once per connection. This
838 * fixup is version-limited, because we don't want to carry the
839 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
840 * need interop with unpatched 2.2 servers. For newer servers,
841 * the client will do MNE swabbing only as needed. LU-1644 */
842 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
843 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
844 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
845 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
846 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
847 strcmp(imp->imp_obd->obd_type->typ_name,
848 LUSTRE_MGC_NAME) == 0))
849 imp->imp_need_mne_swab = 1;
850 else /* clear if server was upgraded since last connect */
851 imp->imp_need_mne_swab = 0;
854 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
855 /* We sent to the server ocd_cksum_types with bits set
856 * for algorithms we understand. The server masked off
857 * the checksum types it doesn't support */
858 if ((ocd->ocd_cksum_types &
859 cksum_types_supported_client()) == 0) {
860 LCONSOLE_ERROR("The negotiation of the checksum "
861 "alogrithm to use with server %s "
863 obd2cli_tgt(imp->imp_obd),
864 ocd->ocd_cksum_types,
865 cksum_types_supported_client());
868 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
871 /* The server does not support OBD_CONNECT_CKSUM.
872 * Enforce ADLER for backward compatibility*/
873 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
875 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
877 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
878 cli->cl_max_pages_per_rpc =
879 min(ocd->ocd_brw_size >> PAGE_SHIFT,
880 cli->cl_max_pages_per_rpc);
881 else if (imp->imp_connect_op == MDS_CONNECT ||
882 imp->imp_connect_op == MGS_CONNECT)
883 cli->cl_max_pages_per_rpc = 1;
885 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
886 (cli->cl_max_pages_per_rpc > 0));
888 client_adjust_max_dirty(cli);
890 /* Update client max modify RPCs in flight with value returned
892 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
893 cli->cl_max_mod_rpcs_in_flight = min(
894 cli->cl_max_mod_rpcs_in_flight,
895 ocd->ocd_maxmodrpcs);
897 cli->cl_max_mod_rpcs_in_flight = 1;
899 /* Reset ns_connect_flags only for initial connect. It might be
900 * changed in while using FS and if we reset it in reconnect
901 * this leads to losing user settings done before such as
902 * disable lru_resize, etc. */
903 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
904 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
905 "flags: %#llx\n", imp->imp_obd->obd_name,
906 ocd->ocd_connect_flags);
907 imp->imp_obd->obd_namespace->ns_connect_flags =
908 ocd->ocd_connect_flags;
909 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
910 ocd->ocd_connect_flags;
913 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
914 /* We need a per-message support flag, because
915 * a. we don't know if the incoming connect reply
916 * supports AT or not (in reply_in_callback)
917 * until we unpack it.
918 * b. failovered server means export and flags are gone
919 * (in ptlrpc_send_reply).
920 * Can only be set when we know AT is supported at
922 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
924 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
926 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
932 * Add all replay requests back to unreplied list before start replay,
933 * so that we can make sure the known replied XID is always increased
934 * only even if when replaying requests.
936 static void ptlrpc_prepare_replay(struct obd_import *imp)
938 struct ptlrpc_request *req;
940 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
941 imp->imp_resend_replay)
944 /* If the server was restart during repaly, the requests may
945 * have been added to the unreplied list in former replay. */
946 spin_lock(&imp->imp_lock);
948 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
949 if (list_empty(&req->rq_unreplied_list))
950 ptlrpc_add_unreplied(req);
953 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
954 if (list_empty(&req->rq_unreplied_list))
955 ptlrpc_add_unreplied(req);
958 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
959 spin_unlock(&imp->imp_lock);
963 * interpret_reply callback for connect RPCs.
964 * Looks into returned status of connect operation and decides
965 * what to do with the import - i.e enter recovery, promote it to
966 * full state for normal operations of disconnect it due to an error.
968 static int ptlrpc_connect_interpret(const struct lu_env *env,
969 struct ptlrpc_request *request,
972 struct ptlrpc_connect_async_args *aa = data;
973 struct obd_import *imp = request->rq_import;
974 struct lustre_handle old_hdl;
975 __u64 old_connect_flags;
977 struct obd_connect_data *ocd;
978 struct obd_export *exp = NULL;
982 spin_lock(&imp->imp_lock);
983 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
984 imp->imp_connect_tried = 1;
985 spin_unlock(&imp->imp_lock);
990 /* if this reconnect to busy export - not need select new target
992 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
993 spin_unlock(&imp->imp_lock);
994 ptlrpc_maybe_ping_import_soon(imp);
998 /* LU-7558: indicate that we are interpretting connect reply,
999 * pltrpc_connect_import() will not try to reconnect until
1000 * interpret will finish. */
1001 imp->imp_connected = 1;
1002 spin_unlock(&imp->imp_lock);
1004 LASSERT(imp->imp_conn_current);
1006 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
1008 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
1010 /* server replied obd_connect_data is always bigger */
1011 ocd = req_capsule_server_sized_get(&request->rq_pill,
1012 &RMF_CONNECT_DATA, ret);
1015 CERROR("%s: no connect data from server\n",
1016 imp->imp_obd->obd_name);
1021 spin_lock(&imp->imp_lock);
1023 /* All imports are pingable */
1024 imp->imp_pingable = 1;
1025 imp->imp_force_reconnect = 0;
1026 imp->imp_force_verify = 0;
1028 imp->imp_connect_data = *ocd;
1030 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1031 imp->imp_obd->obd_name, ocd->ocd_instance);
1032 exp = class_conn2export(&imp->imp_dlm_handle);
1034 spin_unlock(&imp->imp_lock);
1037 /* This could happen if export is cleaned during the
1039 CERROR("%s: missing export after connect\n",
1040 imp->imp_obd->obd_name);
1041 GOTO(out, rc = -ENODEV);
1044 /* check that server granted subset of flags we asked for. */
1045 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1046 ocd->ocd_connect_flags) {
1047 CERROR("%s: Server didn't grant requested subset of flags: "
1048 "asked=%#llx granted=%#llx\n",
1049 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1050 ocd->ocd_connect_flags);
1051 GOTO(out, rc = -EPROTO);
1054 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1055 ocd->ocd_connect_flags2) {
1056 CERROR("%s: Server didn't grant requested subset of flags2: "
1057 "asked=%#llx granted=%#llx\n",
1058 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1059 ocd->ocd_connect_flags2);
1060 GOTO(out, rc = -EPROTO);
1063 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1064 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1065 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1066 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1067 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1068 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1069 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1071 /* We do not support the MDT-MDT interoperations with
1072 * different version MDT because of protocol changes. */
1073 if (unlikely(major != LUSTRE_MAJOR ||
1074 minor != LUSTRE_MINOR ||
1075 abs(patch - LUSTRE_PATCH) > 3)) {
1076 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1077 "connection to different version MDT "
1078 "(%d.%d.%d.%d) %s\n",
1079 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1080 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1081 major, minor, patch,
1082 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1083 imp->imp_connection->c_remote_uuid.uuid);
1085 GOTO(out, rc = -EPROTO);
1089 old_connect_flags = exp_connect_flags(exp);
1090 exp->exp_connect_data = *ocd;
1091 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1093 /* The net statistics after (re-)connect is not valid anymore,
1094 * because may reflect other routing, etc. */
1095 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1096 ptlrpc_at_adj_net_latency(request,
1097 lustre_msg_get_service_time(request->rq_repmsg));
1099 /* Import flags should be updated before waking import at FULL state */
1100 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1101 aa->pcaa_initial_connect);
1102 class_export_put(exp);
1108 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1110 if (aa->pcaa_initial_connect) {
1111 spin_lock(&imp->imp_lock);
1112 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1113 imp->imp_replayable = 1;
1114 spin_unlock(&imp->imp_lock);
1115 CDEBUG(D_HA, "connected to replayable target: %s\n",
1116 obd2cli_tgt(imp->imp_obd));
1118 imp->imp_replayable = 0;
1119 spin_unlock(&imp->imp_lock);
1122 /* if applies, adjust the imp->imp_msg_magic here
1123 * according to reply flags */
1125 imp->imp_remote_handle =
1126 *lustre_msg_get_handle(request->rq_repmsg);
1128 /* Initial connects are allowed for clients with non-random
1129 * uuids when servers are in recovery. Simply signal the
1130 * servers replay is complete and wait in REPLAY_WAIT. */
1131 if (msg_flags & MSG_CONNECT_RECOVERING) {
1132 CDEBUG(D_HA, "connect to %s during recovery\n",
1133 obd2cli_tgt(imp->imp_obd));
1134 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1136 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1137 ptlrpc_activate_import(imp);
1140 GOTO(finish, rc = 0);
1143 /* Determine what recovery state to move the import to. */
1144 if (MSG_CONNECT_RECONNECT & msg_flags) {
1145 memset(&old_hdl, 0, sizeof(old_hdl));
1146 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1147 sizeof (old_hdl))) {
1148 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1149 "bad handle %#llx\n",
1150 obd2cli_tgt(imp->imp_obd),
1151 imp->imp_connection->c_remote_uuid.uuid,
1152 imp->imp_dlm_handle.cookie);
1153 GOTO(out, rc = -ENOTCONN);
1156 if (memcmp(&imp->imp_remote_handle,
1157 lustre_msg_get_handle(request->rq_repmsg),
1158 sizeof(imp->imp_remote_handle))) {
1159 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1162 /* Bug 16611/14775: if server handle have changed,
1163 * that means some sort of disconnection happened.
1164 * If the server is not in recovery, that also means it
1165 * already erased all of our state because of previous
1166 * eviction. If it is in recovery - we are safe to
1167 * participate since we can reestablish all of our state
1168 * with server again */
1169 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1170 CDEBUG(level,"%s@%s changed server handle from "
1172 " but is still in recovery\n",
1173 obd2cli_tgt(imp->imp_obd),
1174 imp->imp_connection->c_remote_uuid.uuid,
1175 imp->imp_remote_handle.cookie,
1176 lustre_msg_get_handle(
1177 request->rq_repmsg)->cookie);
1179 LCONSOLE_WARN("Evicted from %s (at %s) "
1180 "after server handle changed from "
1182 obd2cli_tgt(imp->imp_obd),
1183 imp->imp_connection-> \
1185 imp->imp_remote_handle.cookie,
1186 lustre_msg_get_handle(
1187 request->rq_repmsg)->cookie);
1191 imp->imp_remote_handle =
1192 *lustre_msg_get_handle(request->rq_repmsg);
1194 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1195 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1196 GOTO(finish, rc = 0);
1200 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1201 obd2cli_tgt(imp->imp_obd),
1202 imp->imp_connection->c_remote_uuid.uuid);
1205 if (imp->imp_invalid) {
1206 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1207 "marking evicted\n", imp->imp_obd->obd_name);
1208 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1209 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1210 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1211 imp->imp_obd->obd_name,
1212 obd2cli_tgt(imp->imp_obd));
1214 spin_lock(&imp->imp_lock);
1215 imp->imp_resend_replay = 1;
1216 spin_unlock(&imp->imp_lock);
1218 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1220 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1222 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1223 LASSERT(imp->imp_replayable);
1224 imp->imp_remote_handle =
1225 *lustre_msg_get_handle(request->rq_repmsg);
1226 imp->imp_last_replay_transno = 0;
1227 imp->imp_replay_cursor = &imp->imp_committed_list;
1228 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1230 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1231 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1232 imp->imp_remote_handle =
1233 *lustre_msg_get_handle(request->rq_repmsg);
1234 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1237 /* Sanity checks for a reconnected import. */
1238 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1239 CERROR("imp_replayable flag does not match server "
1240 "after reconnect. We should LBUG right here.\n");
1243 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1244 lustre_msg_get_last_committed(request->rq_repmsg) <
1245 aa->pcaa_peer_committed) {
1246 CERROR("%s went back in time (transno %lld"
1247 " was previously committed, server now claims %lld"
1248 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1250 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1251 lustre_msg_get_last_committed(request->rq_repmsg));
1255 ptlrpc_prepare_replay(imp);
1256 rc = ptlrpc_import_recovery_state_machine(imp);
1257 if (rc == -ENOTCONN) {
1258 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1259 "invalidating and reconnecting\n",
1260 obd2cli_tgt(imp->imp_obd),
1261 imp->imp_connection->c_remote_uuid.uuid);
1262 ptlrpc_connect_import(imp);
1263 spin_lock(&imp->imp_lock);
1264 imp->imp_connected = 0;
1265 imp->imp_connect_tried = 1;
1266 spin_unlock(&imp->imp_lock);
1271 spin_lock(&imp->imp_lock);
1272 imp->imp_connected = 0;
1273 imp->imp_connect_tried = 1;
1274 spin_unlock(&imp->imp_lock);
1277 class_export_put(exp);
1280 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1281 if (rc == -EACCES) {
1283 * Give up trying to reconnect
1284 * EACCES means client has no permission for connection
1286 imp->imp_obd->obd_no_recov = 1;
1287 ptlrpc_deactivate_import(imp);
1290 if (rc == -EPROTO) {
1291 struct obd_connect_data *ocd;
1293 /* reply message might not be ready */
1294 if (request->rq_repmsg == NULL)
1297 ocd = req_capsule_server_get(&request->rq_pill,
1299 /* Servers are not supposed to refuse connections from
1300 * clients based on version, only connection feature
1301 * flags. We should never see this from llite, but it
1302 * may be useful for debugging in the future. */
1304 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1305 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1306 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1308 " refused connection from this client "
1309 "with an incompatible version (%s). "
1310 "Client must be recompiled\n",
1311 obd2cli_tgt(imp->imp_obd),
1312 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1313 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1314 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1315 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1316 LUSTRE_VERSION_STRING);
1317 ptlrpc_deactivate_import(imp);
1318 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1323 ptlrpc_maybe_ping_import_soon(imp);
1325 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1326 obd2cli_tgt(imp->imp_obd),
1327 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1330 wake_up_all(&imp->imp_recovery_waitq);
1335 * interpret callback for "completed replay" RPCs.
1336 * \see signal_completed_replay
1338 static int completed_replay_interpret(const struct lu_env *env,
1339 struct ptlrpc_request *req,
1340 void * data, int rc)
1343 atomic_dec(&req->rq_import->imp_replay_inflight);
1344 if (req->rq_status == 0 &&
1345 !req->rq_import->imp_vbr_failed) {
1346 ptlrpc_import_recovery_state_machine(req->rq_import);
1348 if (req->rq_import->imp_vbr_failed) {
1350 "%s: version recovery fails, reconnecting\n",
1351 req->rq_import->imp_obd->obd_name);
1353 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1355 req->rq_import->imp_obd->obd_name,
1358 ptlrpc_connect_import(req->rq_import);
1365 * Let server know that we have no requests to replay anymore.
1366 * Achieved by just sending a PING request
1368 static int signal_completed_replay(struct obd_import *imp)
1370 struct ptlrpc_request *req;
1373 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1376 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1377 atomic_inc(&imp->imp_replay_inflight);
1379 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1382 atomic_dec(&imp->imp_replay_inflight);
1386 ptlrpc_request_set_replen(req);
1387 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1388 lustre_msg_add_flags(req->rq_reqmsg,
1389 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1391 req->rq_timeout *= 3;
1392 req->rq_interpret_reply = completed_replay_interpret;
1394 ptlrpcd_add_req(req);
1399 * In kernel code all import invalidation happens in its own
1400 * separate thread, so that whatever application happened to encounter
1401 * a problem could still be killed or otherwise continue
1403 static int ptlrpc_invalidate_import_thread(void *data)
1405 struct obd_import *imp = data;
1409 unshare_fs_struct();
1411 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1412 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1413 imp->imp_connection->c_remote_uuid.uuid);
1415 ptlrpc_invalidate_import(imp);
1417 if (obd_dump_on_eviction) {
1418 CERROR("dump the log upon eviction\n");
1419 libcfs_debug_dumplog();
1422 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1423 ptlrpc_import_recovery_state_machine(imp);
1425 class_import_put(imp);
1430 * This is the state machine for client-side recovery on import.
1432 * Typicaly we have two possibly paths. If we came to server and it is not
1433 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1434 * state and reconnect from scratch.
1435 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1436 * We go through our list of requests to replay and send them to server one by
1438 * After sending all request from the list we change import state to
1439 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1440 * and also all the locks we don't yet have and wait for server to grant us.
1441 * After that we send a special "replay completed" request and change import
1442 * state to IMP_REPLAY_WAIT.
1443 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1444 * state and resend all requests from sending list.
1445 * After that we promote import to FULL state and send all delayed requests
1446 * and import is fully operational after that.
1449 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1457 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1458 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1459 &target_start, &target_len);
1460 /* Don't care about MGC eviction */
1461 if (strcmp(imp->imp_obd->obd_type->typ_name,
1462 LUSTRE_MGC_NAME) != 0) {
1463 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1464 "by %.*s; in progress operations "
1465 "using this service will fail.\n",
1466 imp->imp_obd->obd_name, target_len,
1469 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1470 obd2cli_tgt(imp->imp_obd),
1471 imp->imp_connection->c_remote_uuid.uuid);
1472 /* reset vbr_failed flag upon eviction */
1473 spin_lock(&imp->imp_lock);
1474 imp->imp_vbr_failed = 0;
1475 spin_unlock(&imp->imp_lock);
1478 struct task_struct *task;
1479 /* bug 17802: XXX client_disconnect_export vs connect request
1480 * race. if client is evicted at this time then we start
1481 * invalidate thread without reference to import and import can
1482 * be freed at same time. */
1483 class_import_get(imp);
1484 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1487 class_import_put(imp);
1488 CERROR("error starting invalidate thread: %d\n", rc);
1497 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1498 CDEBUG(D_HA, "replay requested by %s\n",
1499 obd2cli_tgt(imp->imp_obd));
1500 rc = ptlrpc_replay_next(imp, &inflight);
1501 if (inflight == 0 &&
1502 atomic_read(&imp->imp_replay_inflight) == 0) {
1503 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1504 rc = ldlm_replay_locks(imp);
1511 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1512 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1513 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1514 rc = signal_completed_replay(imp);
1520 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1521 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1522 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1526 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1527 struct ptlrpc_connection *conn = imp->imp_connection;
1529 rc = ptlrpc_resend(imp);
1532 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1533 ptlrpc_activate_import(imp);
1535 LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
1536 imp->imp_obd->obd_name,
1537 obd_uuid2str(&conn->c_remote_uuid),
1538 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1541 if (imp->imp_state == LUSTRE_IMP_FULL) {
1542 wake_up_all(&imp->imp_recovery_waitq);
1543 ptlrpc_wake_delayed(imp);
1550 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1552 struct ptlrpc_request *req;
1556 if (imp->imp_obd->obd_force)
1557 GOTO(set_state, rc);
1559 switch (imp->imp_connect_op) {
1561 rq_opc = OST_DISCONNECT;
1564 rq_opc = MDS_DISCONNECT;
1567 rq_opc = MGS_DISCONNECT;
1571 CERROR("%s: don't know how to disconnect from %s "
1572 "(connect_op %d): rc = %d\n",
1573 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1574 imp->imp_connect_op, rc);
1578 if (ptlrpc_import_in_recovery(imp)) {
1579 struct l_wait_info lwi;
1580 long timeout_jiffies;
1584 if (imp->imp_server_timeout)
1585 timeout = obd_timeout >> 1;
1587 timeout = obd_timeout;
1592 req_portal = imp->imp_client->cli_request_portal;
1593 idx = import_at_get_index(imp, req_portal);
1594 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1597 timeout_jiffies = cfs_time_seconds(timeout);
1598 lwi = LWI_TIMEOUT_INTR(max_t(long, timeout_jiffies, 1),
1599 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1600 rc = l_wait_event(imp->imp_recovery_waitq,
1601 !ptlrpc_import_in_recovery(imp), &lwi);
1605 spin_lock(&imp->imp_lock);
1606 if (imp->imp_state != LUSTRE_IMP_FULL)
1608 spin_unlock(&imp->imp_lock);
1610 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1611 LUSTRE_OBD_VERSION, rq_opc);
1613 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1614 * it fails. We can get through the above with a down server
1615 * if the client doesn't know the server is gone yet. */
1616 req->rq_no_resend = 1;
1618 /* We want client umounts to happen quickly, no matter the
1620 req->rq_timeout = min_t(int, req->rq_timeout,
1621 INITIAL_CONNECT_TIMEOUT);
1623 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1624 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1625 ptlrpc_request_set_replen(req);
1626 rc = ptlrpc_queue_wait(req);
1627 ptlrpc_req_finished(req);
1631 spin_lock(&imp->imp_lock);
1634 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1636 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1637 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1638 spin_unlock(&imp->imp_lock);
1640 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1644 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1646 void ptlrpc_cleanup_imp(struct obd_import *imp)
1650 spin_lock(&imp->imp_lock);
1651 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1652 imp->imp_generation++;
1653 spin_unlock(&imp->imp_lock);
1654 ptlrpc_abort_inflight(imp);
1659 /* Adaptive Timeout utils */
1660 extern unsigned int at_min, at_max, at_history;
1662 /* Update at_current with the specified value (bounded by at_min and at_max),
1663 * as well as the AT history "bins".
1664 * - Bin into timeslices using AT_BINS bins.
1665 * - This gives us a max of the last at_history seconds without the storage,
1666 * but still smoothing out a return to normalcy from a slow response.
1667 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1669 int at_measured(struct adaptive_timeout *at, unsigned int val)
1671 unsigned int old = at->at_current;
1672 time64_t now = ktime_get_real_seconds();
1673 long binlimit = max_t(long, at_history / AT_BINS, 1);
1676 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1677 val, at, (long)(now - at->at_binstart), at->at_current,
1678 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1681 /* 0's don't count, because we never want our timeout to
1682 drop to 0, and because 0 could mean an error */
1685 spin_lock(&at->at_lock);
1687 if (unlikely(at->at_binstart == 0)) {
1688 /* Special case to remove default from history */
1689 at->at_current = val;
1690 at->at_worst_ever = val;
1691 at->at_worst_time = now;
1692 at->at_hist[0] = val;
1693 at->at_binstart = now;
1694 } else if (now - at->at_binstart < binlimit ) {
1696 at->at_hist[0] = max(val, at->at_hist[0]);
1697 at->at_current = max(val, at->at_current);
1700 unsigned int maxv = val;
1702 /* move bins over */
1703 shift = (u32)(now - at->at_binstart) / binlimit;
1705 for(i = AT_BINS - 1; i >= 0; i--) {
1707 at->at_hist[i] = at->at_hist[i - shift];
1708 maxv = max(maxv, at->at_hist[i]);
1713 at->at_hist[0] = val;
1714 at->at_current = maxv;
1715 at->at_binstart += shift * binlimit;
1718 if (at->at_current > at->at_worst_ever) {
1719 at->at_worst_ever = at->at_current;
1720 at->at_worst_time = now;
1723 if (at->at_flags & AT_FLG_NOHIST)
1724 /* Only keep last reported val; keeping the rest of the history
1726 at->at_current = val;
1729 at->at_current = min(at->at_current, at_max);
1730 at->at_current = max(at->at_current, at_min);
1732 if (at->at_current != old)
1733 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1734 "(val=%u) hist %u %u %u %u\n", at,
1735 old, at->at_current, at->at_current - old, val,
1736 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1739 /* if we changed, report the old value */
1740 old = (at->at_current != old) ? old : 0;
1742 spin_unlock(&at->at_lock);
1746 /* Find the imp_at index for a given portal; assign if space available */
1747 int import_at_get_index(struct obd_import *imp, int portal)
1749 struct imp_at *at = &imp->imp_at;
1752 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1753 if (at->iat_portal[i] == portal)
1755 if (at->iat_portal[i] == 0)
1760 /* Not found in list, add it under a lock */
1761 spin_lock(&imp->imp_lock);
1763 /* Check unused under lock */
1764 for (; i < IMP_AT_MAX_PORTALS; i++) {
1765 if (at->iat_portal[i] == portal)
1767 if (at->iat_portal[i] == 0)
1772 /* Not enough portals? */
1773 LASSERT(i < IMP_AT_MAX_PORTALS);
1775 at->iat_portal[i] = portal;
1777 spin_unlock(&imp->imp_lock);