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 obd_import_nid2str(imp));
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, target_len, target_start,
184 obd_import_nid2str(imp));
186 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
187 spin_unlock(&imp->imp_lock);
189 if (obd_dump_on_timeout)
190 libcfs_debug_dumplog();
192 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
195 spin_unlock(&imp->imp_lock);
196 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
197 imp->imp_client->cli_name, imp,
198 (imp->imp_state == LUSTRE_IMP_FULL &&
199 imp->imp_conn_cnt > conn_cnt) ?
200 "reconnected" : "not connected", imp->imp_conn_cnt,
201 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
207 /* Must be called with imp_lock held! */
208 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
211 assert_spin_locked(&imp->imp_lock);
213 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
214 imp->imp_invalid = 1;
215 imp->imp_generation++;
216 spin_unlock(&imp->imp_lock);
218 ptlrpc_abort_inflight(imp);
219 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
225 * This acts as a barrier; all existing requests are rejected, and
226 * no new requests will be accepted until the import is valid again.
228 void ptlrpc_deactivate_import(struct obd_import *imp)
230 spin_lock(&imp->imp_lock);
231 ptlrpc_deactivate_and_unlock_import(imp);
233 EXPORT_SYMBOL(ptlrpc_deactivate_import);
235 static time64_t ptlrpc_inflight_deadline(struct ptlrpc_request *req,
240 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
241 (req->rq_phase == RQ_PHASE_BULK) ||
242 (req->rq_phase == RQ_PHASE_NEW)))
245 if (req->rq_timedout)
248 if (req->rq_phase == RQ_PHASE_NEW)
251 dl = req->rq_deadline;
259 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
261 time64_t now = ktime_get_real_seconds();
262 struct list_head *tmp, *n;
263 struct ptlrpc_request *req;
264 time64_t timeout = 0;
266 spin_lock(&imp->imp_lock);
267 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
268 req = list_entry(tmp, struct ptlrpc_request, rq_list);
269 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
271 spin_unlock(&imp->imp_lock);
276 * This function will invalidate the import, if necessary, then block
277 * for all the RPC completions, and finally notify the obd to
278 * invalidate its state (ie cancel locks, clear pending requests,
281 void ptlrpc_invalidate_import(struct obd_import *imp)
283 struct list_head *tmp, *n;
284 struct ptlrpc_request *req;
285 struct l_wait_info lwi;
289 atomic_inc(&imp->imp_inval_count);
291 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
292 ptlrpc_deactivate_import(imp);
294 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
295 LASSERT(imp->imp_invalid);
297 /* Wait forever until inflight == 0. We really can't do it another
298 * way because in some cases we need to wait for very long reply
299 * unlink. We can't do anything before that because there is really
300 * no guarantee that some rdma transfer is not in progress right now. */
302 long timeout_jiffies;
304 /* Calculate max timeout for waiting on rpcs to error
305 * out. Use obd_timeout if calculated value is smaller
308 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
309 timeout = ptlrpc_inflight_timeout(imp);
310 timeout += timeout / 3;
313 timeout = obd_timeout;
315 /* decrease the interval to increase race condition */
319 CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
322 /* Wait for all requests to error out and call completion
323 * callbacks. Cap it at obd_timeout -- these should all
324 * have been locally cancelled by ptlrpc_abort_inflight.
326 timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
327 lwi = LWI_TIMEOUT_INTERVAL(timeout_jiffies,
328 (timeout > 1) ? cfs_time_seconds(1) :
329 cfs_time_seconds(1) / 2,
331 rc = l_wait_event(imp->imp_recovery_waitq,
332 (atomic_read(&imp->imp_inflight) == 0),
335 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
337 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
338 cli_tgt, rc, atomic_read(&imp->imp_inflight));
340 spin_lock(&imp->imp_lock);
341 if (atomic_read(&imp->imp_inflight) == 0) {
342 int count = atomic_read(&imp->imp_unregistering);
344 /* We know that "unregistering" rpcs only can
345 * survive in sending or delaying lists (they
346 * maybe waiting for long reply unlink in
347 * sluggish nets). Let's check this. If there
348 * is no inflight and unregistering != 0, this
350 LASSERTF(count == 0, "Some RPCs are still "
351 "unregistering: %d\n", count);
353 /* Let's save one loop as soon as inflight have
354 * dropped to zero. No new inflights possible at
358 list_for_each_safe(tmp, n,
359 &imp->imp_sending_list) {
360 req = list_entry(tmp,
361 struct ptlrpc_request,
363 DEBUG_REQ(D_ERROR, req,
364 "still on sending list");
366 list_for_each_safe(tmp, n,
367 &imp->imp_delayed_list) {
368 req = list_entry(tmp,
369 struct ptlrpc_request,
371 DEBUG_REQ(D_ERROR, req,
372 "still on delayed list");
375 CERROR("%s: Unregistering RPCs found (%d). "
376 "Network is sluggish? Waiting them "
377 "to error out.\n", cli_tgt,
378 atomic_read(&imp->imp_unregistering));
380 spin_unlock(&imp->imp_lock);
385 * Let's additionally check that no new rpcs added to import in
386 * "invalidate" state.
388 LASSERT(atomic_read(&imp->imp_inflight) == 0);
389 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
390 sptlrpc_import_flush_all_ctx(imp);
392 atomic_dec(&imp->imp_inval_count);
393 wake_up_all(&imp->imp_recovery_waitq);
395 EXPORT_SYMBOL(ptlrpc_invalidate_import);
397 /* unset imp_invalid */
398 void ptlrpc_activate_import(struct obd_import *imp)
400 struct obd_device *obd = imp->imp_obd;
402 spin_lock(&imp->imp_lock);
403 if (imp->imp_deactive != 0) {
404 spin_unlock(&imp->imp_lock);
408 imp->imp_invalid = 0;
409 spin_unlock(&imp->imp_lock);
410 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
412 EXPORT_SYMBOL(ptlrpc_activate_import);
414 void ptlrpc_pinger_force(struct obd_import *imp)
416 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
417 ptlrpc_import_state_name(imp->imp_state));
419 spin_lock(&imp->imp_lock);
420 imp->imp_force_verify = 1;
421 spin_unlock(&imp->imp_lock);
423 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
424 ptlrpc_pinger_wake_up();
426 EXPORT_SYMBOL(ptlrpc_pinger_force);
428 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
432 LASSERT(!imp->imp_dlm_fake);
434 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
435 if (!imp->imp_replayable) {
436 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
437 "auto-deactivating\n",
438 obd2cli_tgt(imp->imp_obd),
439 imp->imp_connection->c_remote_uuid.uuid,
440 imp->imp_obd->obd_name);
441 ptlrpc_deactivate_import(imp);
444 ptlrpc_pinger_force(imp);
449 int ptlrpc_reconnect_import(struct obd_import *imp)
452 long timeout_jiffies = cfs_time_seconds(obd_timeout);
453 struct l_wait_info lwi;
456 ptlrpc_pinger_force(imp);
458 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
459 obd2cli_tgt(imp->imp_obd), obd_timeout);
461 lwi = LWI_TIMEOUT(timeout_jiffies, NULL, NULL);
462 rc = l_wait_event(imp->imp_recovery_waitq,
463 !ptlrpc_import_in_recovery(imp), &lwi);
464 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
465 ptlrpc_import_state_name(imp->imp_state));
468 ptlrpc_set_import_discon(imp, 0);
469 /* Force a new connect attempt */
470 ptlrpc_invalidate_import(imp);
471 /* Do a fresh connect next time by zeroing the handle */
472 ptlrpc_disconnect_import(imp, 1);
473 /* Wait for all invalidate calls to finish */
474 if (atomic_read(&imp->imp_inval_count) > 0) {
475 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
478 rc = l_wait_event(imp->imp_recovery_waitq,
479 (atomic_read(&imp->imp_inval_count) == 0),
482 CERROR("Interrupted, inval=%d\n",
483 atomic_read(&imp->imp_inval_count));
486 /* Allow reconnect attempts */
487 imp->imp_obd->obd_no_recov = 0;
488 /* Remove 'invalid' flag */
489 ptlrpc_activate_import(imp);
490 /* Attempt a new connect */
491 ptlrpc_recover_import(imp, NULL, 0);
495 EXPORT_SYMBOL(ptlrpc_reconnect_import);
498 * Connection on import \a imp is changed to another one (if more than one is
499 * present). We typically chose connection that we have not tried to connect to
502 static int import_select_connection(struct obd_import *imp)
504 struct obd_import_conn *imp_conn = NULL, *conn;
505 struct obd_export *dlmexp;
507 int target_len, tried_all = 1;
510 spin_lock(&imp->imp_lock);
512 if (list_empty(&imp->imp_conn_list)) {
513 CERROR("%s: no connections available\n",
514 imp->imp_obd->obd_name);
515 spin_unlock(&imp->imp_lock);
519 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
520 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
521 imp->imp_obd->obd_name,
522 libcfs_nid2str(conn->oic_conn->c_peer.nid),
523 conn->oic_last_attempt);
525 /* If we have not tried this connection since
526 the last successful attempt, go with this one */
527 if ((conn->oic_last_attempt == 0) ||
528 conn->oic_last_attempt <= imp->imp_last_success_conn) {
534 /* If all of the connections have already been tried
535 since the last successful connection; just choose the
536 least recently used */
539 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
543 /* if not found, simply choose the current one */
544 if (!imp_conn || imp->imp_force_reconnect) {
545 LASSERT(imp->imp_conn_current);
546 imp_conn = imp->imp_conn_current;
549 LASSERT(imp_conn->oic_conn);
551 /* If we've tried everything, and we're back to the beginning of the
552 list, increase our timeout and try again. It will be reset when
553 we do finally connect. (FIXME: really we should wait for all network
554 state associated with the last connection attempt to drain before
555 trying to reconnect on it.) */
556 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
557 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
558 if (at_get(at) < CONNECTION_SWITCH_MAX) {
559 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
560 if (at_get(at) > CONNECTION_SWITCH_MAX)
561 at_reset(at, CONNECTION_SWITCH_MAX);
563 LASSERT(imp_conn->oic_last_attempt);
564 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
565 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
568 imp_conn->oic_last_attempt = ktime_get_seconds();
570 /* switch connection, don't mind if it's same as the current one */
571 if (imp->imp_connection)
572 ptlrpc_connection_put(imp->imp_connection);
573 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
575 dlmexp = class_conn2export(&imp->imp_dlm_handle);
576 LASSERT(dlmexp != NULL);
577 if (dlmexp->exp_connection)
578 ptlrpc_connection_put(dlmexp->exp_connection);
579 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
580 class_export_put(dlmexp);
582 if (imp->imp_conn_current != imp_conn) {
583 if (imp->imp_conn_current) {
584 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
585 &target_start, &target_len);
587 CDEBUG(D_HA, "%s: Connection changing to"
589 imp->imp_obd->obd_name,
590 target_len, target_start,
591 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
594 imp->imp_conn_current = imp_conn;
597 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
598 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
599 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
601 spin_unlock(&imp->imp_lock);
607 * must be called under imp_lock
609 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
611 struct ptlrpc_request *req;
612 struct list_head *tmp;
614 /* The requests in committed_list always have smaller transnos than
615 * the requests in replay_list */
616 if (!list_empty(&imp->imp_committed_list)) {
617 tmp = imp->imp_committed_list.next;
618 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
619 *transno = req->rq_transno;
620 if (req->rq_transno == 0) {
621 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
626 if (!list_empty(&imp->imp_replay_list)) {
627 tmp = imp->imp_replay_list.next;
628 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
629 *transno = req->rq_transno;
630 if (req->rq_transno == 0) {
631 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
640 * Attempt to (re)connect import \a imp. This includes all preparations,
641 * initializing CONNECT RPC request and passing it to ptlrpcd for
643 * Returns 0 on success or error code.
645 int ptlrpc_connect_import(struct obd_import *imp)
647 struct obd_device *obd = imp->imp_obd;
648 int initial_connect = 0;
650 __u64 committed_before_reconnect = 0;
651 struct ptlrpc_request *request;
652 char *bufs[] = { NULL,
653 obd2cli_tgt(imp->imp_obd),
655 (char *)&imp->imp_dlm_handle,
656 (char *)&imp->imp_connect_data };
657 struct ptlrpc_connect_async_args *aa;
661 spin_lock(&imp->imp_lock);
662 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
663 spin_unlock(&imp->imp_lock);
664 CERROR("can't connect to a closed import\n");
666 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
667 spin_unlock(&imp->imp_lock);
668 CERROR("already connected\n");
670 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
671 imp->imp_connected) {
672 spin_unlock(&imp->imp_lock);
673 CERROR("already connecting\n");
677 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
680 imp->imp_resend_replay = 0;
682 if (!lustre_handle_is_used(&imp->imp_remote_handle))
685 committed_before_reconnect = imp->imp_peer_committed_transno;
687 set_transno = ptlrpc_first_transno(imp,
688 &imp->imp_connect_data.ocd_transno);
689 spin_unlock(&imp->imp_lock);
691 rc = import_select_connection(imp);
695 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
699 /* Reset connect flags to the originally requested flags, in case
700 * the server is updated on-the-fly we will get the new features. */
701 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
702 imp->imp_connect_data.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
703 /* Reset ocd_version each time so the server knows the exact versions */
704 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
705 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
706 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
708 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
709 &obd->obd_uuid, &imp->imp_connect_data, NULL);
713 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
715 GOTO(out, rc = -ENOMEM);
717 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
718 imp->imp_connect_op, bufs, NULL);
720 ptlrpc_request_free(request);
724 /* Report the rpc service time to the server so that it knows how long
725 * to wait for clients to join recovery */
726 lustre_msg_set_service_time(request->rq_reqmsg,
727 at_timeout2est(request->rq_timeout));
729 /* The amount of time we give the server to process the connect req.
730 * import_select_connection will increase the net latency on
731 * repeated reconnect attempts to cover slow networks.
732 * We override/ignore the server rpc completion estimate here,
733 * which may be large if this is a reconnect attempt */
734 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
735 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
737 request->rq_no_resend = request->rq_no_delay = 1;
738 request->rq_send_state = LUSTRE_IMP_CONNECTING;
739 /* Allow a slightly larger reply for future growth compatibility */
740 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
741 sizeof(struct obd_connect_data)+16*sizeof(__u64));
742 ptlrpc_request_set_replen(request);
743 request->rq_interpret_reply = ptlrpc_connect_interpret;
745 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
746 aa = ptlrpc_req_async_args(request);
747 memset(aa, 0, sizeof *aa);
749 aa->pcaa_peer_committed = committed_before_reconnect;
750 aa->pcaa_initial_connect = initial_connect;
752 if (aa->pcaa_initial_connect) {
753 spin_lock(&imp->imp_lock);
754 imp->imp_replayable = 1;
755 spin_unlock(&imp->imp_lock);
756 lustre_msg_add_op_flags(request->rq_reqmsg,
757 MSG_CONNECT_INITIAL);
761 lustre_msg_add_op_flags(request->rq_reqmsg,
762 MSG_CONNECT_TRANSNO);
764 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %ld)",
765 request->rq_timeout);
766 ptlrpcd_add_req(request);
770 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
774 EXPORT_SYMBOL(ptlrpc_connect_import);
776 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
780 spin_lock(&imp->imp_lock);
781 force_verify = imp->imp_force_verify != 0;
782 spin_unlock(&imp->imp_lock);
785 ptlrpc_pinger_wake_up();
788 static int ptlrpc_busy_reconnect(int rc)
790 return (rc == -EBUSY) || (rc == -EAGAIN);
793 static int ptlrpc_connect_set_flags(struct obd_import *imp,
794 struct obd_connect_data *ocd,
795 __u64 old_connect_flags,
796 struct obd_export *exp, int init_connect)
799 struct client_obd *cli = &imp->imp_obd->u.cli;
801 spin_lock(&imp->imp_lock);
802 list_del(&imp->imp_conn_current->oic_item);
803 list_add(&imp->imp_conn_current->oic_item,
804 &imp->imp_conn_list);
805 imp->imp_last_success_conn =
806 imp->imp_conn_current->oic_last_attempt;
808 spin_unlock(&imp->imp_lock);
810 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
811 (ocd->ocd_version > LUSTRE_VERSION_CODE +
812 LUSTRE_VERSION_OFFSET_WARN ||
813 ocd->ocd_version < LUSTRE_VERSION_CODE -
814 LUSTRE_VERSION_OFFSET_WARN)) {
815 /* Sigh, some compilers do not like #ifdef in the middle
816 of macro arguments */
817 const char *older = "older than client. "
818 "Consider upgrading server";
819 const char *newer = "newer than client. "
820 "Consider upgrading client";
822 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
824 obd2cli_tgt(imp->imp_obd),
825 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
826 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
827 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
828 OBD_OCD_VERSION_FIX(ocd->ocd_version),
829 ocd->ocd_version > LUSTRE_VERSION_CODE ?
830 newer : older, LUSTRE_VERSION_STRING);
834 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
835 /* We sent to the server ocd_cksum_types with bits set
836 * for algorithms we understand. The server masked off
837 * the checksum types it doesn't support */
838 if ((ocd->ocd_cksum_types &
839 cksum_types_supported_client()) == 0) {
840 LCONSOLE_ERROR("The negotiation of the checksum "
841 "alogrithm to use with server %s "
843 obd2cli_tgt(imp->imp_obd),
844 ocd->ocd_cksum_types,
845 cksum_types_supported_client());
848 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
851 /* The server does not support OBD_CONNECT_CKSUM.
852 * Enforce ADLER for backward compatibility*/
853 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
855 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
857 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
858 cli->cl_max_pages_per_rpc =
859 min(ocd->ocd_brw_size >> PAGE_SHIFT,
860 cli->cl_max_pages_per_rpc);
861 else if (imp->imp_connect_op == MDS_CONNECT ||
862 imp->imp_connect_op == MGS_CONNECT)
863 cli->cl_max_pages_per_rpc = 1;
865 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
866 (cli->cl_max_pages_per_rpc > 0));
868 client_adjust_max_dirty(cli);
870 /* Update client max modify RPCs in flight with value returned
872 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
873 cli->cl_max_mod_rpcs_in_flight = min(
874 cli->cl_max_mod_rpcs_in_flight,
875 ocd->ocd_maxmodrpcs);
877 cli->cl_max_mod_rpcs_in_flight = 1;
879 /* Reset ns_connect_flags only for initial connect. It might be
880 * changed in while using FS and if we reset it in reconnect
881 * this leads to losing user settings done before such as
882 * disable lru_resize, etc. */
883 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
884 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
885 "flags: %#llx\n", imp->imp_obd->obd_name,
886 ocd->ocd_connect_flags);
887 imp->imp_obd->obd_namespace->ns_connect_flags =
888 ocd->ocd_connect_flags;
889 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
890 ocd->ocd_connect_flags;
893 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
894 /* We need a per-message support flag, because
895 * a. we don't know if the incoming connect reply
896 * supports AT or not (in reply_in_callback)
897 * until we unpack it.
898 * b. failovered server means export and flags are gone
899 * (in ptlrpc_send_reply).
900 * Can only be set when we know AT is supported at
902 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
904 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
906 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
912 * Add all replay requests back to unreplied list before start replay,
913 * so that we can make sure the known replied XID is always increased
914 * only even if when replaying requests.
916 static void ptlrpc_prepare_replay(struct obd_import *imp)
918 struct ptlrpc_request *req;
920 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
921 imp->imp_resend_replay)
924 /* If the server was restart during repaly, the requests may
925 * have been added to the unreplied list in former replay. */
926 spin_lock(&imp->imp_lock);
928 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
929 if (list_empty(&req->rq_unreplied_list))
930 ptlrpc_add_unreplied(req);
933 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
934 if (list_empty(&req->rq_unreplied_list))
935 ptlrpc_add_unreplied(req);
938 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
939 spin_unlock(&imp->imp_lock);
943 * interpret_reply callback for connect RPCs.
944 * Looks into returned status of connect operation and decides
945 * what to do with the import - i.e enter recovery, promote it to
946 * full state for normal operations of disconnect it due to an error.
948 static int ptlrpc_connect_interpret(const struct lu_env *env,
949 struct ptlrpc_request *request,
952 struct ptlrpc_connect_async_args *aa = data;
953 struct obd_import *imp = request->rq_import;
954 struct lustre_handle old_hdl;
955 __u64 old_connect_flags;
957 struct obd_connect_data *ocd;
958 struct obd_export *exp = NULL;
962 spin_lock(&imp->imp_lock);
963 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
964 imp->imp_connect_tried = 1;
965 spin_unlock(&imp->imp_lock);
970 /* if this reconnect to busy export - not need select new target
972 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
973 spin_unlock(&imp->imp_lock);
974 ptlrpc_maybe_ping_import_soon(imp);
978 /* LU-7558: indicate that we are interpretting connect reply,
979 * pltrpc_connect_import() will not try to reconnect until
980 * interpret will finish. */
981 imp->imp_connected = 1;
982 spin_unlock(&imp->imp_lock);
984 LASSERT(imp->imp_conn_current);
986 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
988 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
990 /* server replied obd_connect_data is always bigger */
991 ocd = req_capsule_server_sized_get(&request->rq_pill,
992 &RMF_CONNECT_DATA, ret);
995 CERROR("%s: no connect data from server\n",
996 imp->imp_obd->obd_name);
1001 spin_lock(&imp->imp_lock);
1003 /* All imports are pingable */
1004 imp->imp_pingable = 1;
1005 imp->imp_force_reconnect = 0;
1006 imp->imp_force_verify = 0;
1008 imp->imp_connect_data = *ocd;
1010 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1011 imp->imp_obd->obd_name, ocd->ocd_instance);
1012 exp = class_conn2export(&imp->imp_dlm_handle);
1014 spin_unlock(&imp->imp_lock);
1017 /* This could happen if export is cleaned during the
1019 CERROR("%s: missing export after connect\n",
1020 imp->imp_obd->obd_name);
1021 GOTO(out, rc = -ENODEV);
1024 /* check that server granted subset of flags we asked for. */
1025 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1026 ocd->ocd_connect_flags) {
1027 CERROR("%s: Server didn't grant requested subset of flags: "
1028 "asked=%#llx granted=%#llx\n",
1029 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1030 ocd->ocd_connect_flags);
1031 GOTO(out, rc = -EPROTO);
1034 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1035 ocd->ocd_connect_flags2) {
1036 CERROR("%s: Server didn't grant requested subset of flags2: "
1037 "asked=%#llx granted=%#llx\n",
1038 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1039 ocd->ocd_connect_flags2);
1040 GOTO(out, rc = -EPROTO);
1043 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1044 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1045 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1046 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1047 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1048 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1049 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1051 /* We do not support the MDT-MDT interoperations with
1052 * different version MDT because of protocol changes. */
1053 if (unlikely(major != LUSTRE_MAJOR ||
1054 minor != LUSTRE_MINOR ||
1055 abs(patch - LUSTRE_PATCH) > 3)) {
1056 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1057 "connection to different version MDT "
1058 "(%d.%d.%d.%d) %s\n",
1059 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1060 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1061 major, minor, patch,
1062 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1063 imp->imp_connection->c_remote_uuid.uuid);
1065 GOTO(out, rc = -EPROTO);
1069 old_connect_flags = exp_connect_flags(exp);
1070 exp->exp_connect_data = *ocd;
1071 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1073 /* The net statistics after (re-)connect is not valid anymore,
1074 * because may reflect other routing, etc. */
1075 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1076 ptlrpc_at_adj_net_latency(request,
1077 lustre_msg_get_service_time(request->rq_repmsg));
1079 /* Import flags should be updated before waking import at FULL state */
1080 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1081 aa->pcaa_initial_connect);
1082 class_export_put(exp);
1088 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1090 if (aa->pcaa_initial_connect) {
1091 spin_lock(&imp->imp_lock);
1092 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1093 imp->imp_replayable = 1;
1094 spin_unlock(&imp->imp_lock);
1095 CDEBUG(D_HA, "connected to replayable target: %s\n",
1096 obd2cli_tgt(imp->imp_obd));
1098 imp->imp_replayable = 0;
1099 spin_unlock(&imp->imp_lock);
1102 /* if applies, adjust the imp->imp_msg_magic here
1103 * according to reply flags */
1105 imp->imp_remote_handle =
1106 *lustre_msg_get_handle(request->rq_repmsg);
1108 /* Initial connects are allowed for clients with non-random
1109 * uuids when servers are in recovery. Simply signal the
1110 * servers replay is complete and wait in REPLAY_WAIT. */
1111 if (msg_flags & MSG_CONNECT_RECOVERING) {
1112 CDEBUG(D_HA, "connect to %s during recovery\n",
1113 obd2cli_tgt(imp->imp_obd));
1114 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1116 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1117 ptlrpc_activate_import(imp);
1120 GOTO(finish, rc = 0);
1123 /* Determine what recovery state to move the import to. */
1124 if (MSG_CONNECT_RECONNECT & msg_flags) {
1125 memset(&old_hdl, 0, sizeof(old_hdl));
1126 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1127 sizeof (old_hdl))) {
1128 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1129 "bad handle %#llx\n",
1130 obd2cli_tgt(imp->imp_obd),
1131 imp->imp_connection->c_remote_uuid.uuid,
1132 imp->imp_dlm_handle.cookie);
1133 GOTO(out, rc = -ENOTCONN);
1136 if (memcmp(&imp->imp_remote_handle,
1137 lustre_msg_get_handle(request->rq_repmsg),
1138 sizeof(imp->imp_remote_handle))) {
1139 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1142 /* Bug 16611/14775: if server handle have changed,
1143 * that means some sort of disconnection happened.
1144 * If the server is not in recovery, that also means it
1145 * already erased all of our state because of previous
1146 * eviction. If it is in recovery - we are safe to
1147 * participate since we can reestablish all of our state
1148 * with server again */
1149 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1150 CDEBUG(level,"%s@%s changed server handle from "
1152 " but is still in recovery\n",
1153 obd2cli_tgt(imp->imp_obd),
1154 imp->imp_connection->c_remote_uuid.uuid,
1155 imp->imp_remote_handle.cookie,
1156 lustre_msg_get_handle(
1157 request->rq_repmsg)->cookie);
1159 LCONSOLE_WARN("Evicted from %s (at %s) "
1160 "after server handle changed from "
1162 obd2cli_tgt(imp->imp_obd),
1163 imp->imp_connection-> \
1165 imp->imp_remote_handle.cookie,
1166 lustre_msg_get_handle(
1167 request->rq_repmsg)->cookie);
1171 imp->imp_remote_handle =
1172 *lustre_msg_get_handle(request->rq_repmsg);
1174 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1175 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1176 GOTO(finish, rc = 0);
1180 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1181 obd2cli_tgt(imp->imp_obd),
1182 imp->imp_connection->c_remote_uuid.uuid);
1185 if (imp->imp_invalid) {
1186 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1187 "marking evicted\n", imp->imp_obd->obd_name);
1188 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1189 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1190 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1191 imp->imp_obd->obd_name,
1192 obd2cli_tgt(imp->imp_obd));
1194 spin_lock(&imp->imp_lock);
1195 imp->imp_resend_replay = 1;
1196 spin_unlock(&imp->imp_lock);
1198 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1200 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1202 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1203 LASSERT(imp->imp_replayable);
1204 imp->imp_remote_handle =
1205 *lustre_msg_get_handle(request->rq_repmsg);
1206 imp->imp_last_replay_transno = 0;
1207 imp->imp_replay_cursor = &imp->imp_committed_list;
1208 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1210 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1211 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1212 imp->imp_remote_handle =
1213 *lustre_msg_get_handle(request->rq_repmsg);
1214 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1217 /* Sanity checks for a reconnected import. */
1218 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1219 CERROR("imp_replayable flag does not match server "
1220 "after reconnect. We should LBUG right here.\n");
1223 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1224 lustre_msg_get_last_committed(request->rq_repmsg) <
1225 aa->pcaa_peer_committed) {
1226 CERROR("%s went back in time (transno %lld"
1227 " was previously committed, server now claims %lld"
1228 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1230 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1231 lustre_msg_get_last_committed(request->rq_repmsg));
1235 ptlrpc_prepare_replay(imp);
1236 rc = ptlrpc_import_recovery_state_machine(imp);
1237 if (rc == -ENOTCONN) {
1238 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1239 "invalidating and reconnecting\n",
1240 obd2cli_tgt(imp->imp_obd),
1241 imp->imp_connection->c_remote_uuid.uuid);
1242 ptlrpc_connect_import(imp);
1243 spin_lock(&imp->imp_lock);
1244 imp->imp_connected = 0;
1245 imp->imp_connect_tried = 1;
1246 spin_unlock(&imp->imp_lock);
1251 spin_lock(&imp->imp_lock);
1252 imp->imp_connected = 0;
1253 imp->imp_connect_tried = 1;
1254 spin_unlock(&imp->imp_lock);
1257 class_export_put(exp);
1260 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1261 if (rc == -EACCES) {
1263 * Give up trying to reconnect
1264 * EACCES means client has no permission for connection
1266 imp->imp_obd->obd_no_recov = 1;
1267 ptlrpc_deactivate_import(imp);
1270 if (rc == -EPROTO) {
1271 struct obd_connect_data *ocd;
1273 /* reply message might not be ready */
1274 if (request->rq_repmsg == NULL)
1277 ocd = req_capsule_server_get(&request->rq_pill,
1279 /* Servers are not supposed to refuse connections from
1280 * clients based on version, only connection feature
1281 * flags. We should never see this from llite, but it
1282 * may be useful for debugging in the future. */
1284 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1285 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1286 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1288 " refused connection from this client "
1289 "with an incompatible version (%s). "
1290 "Client must be recompiled\n",
1291 obd2cli_tgt(imp->imp_obd),
1292 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1293 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1294 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1295 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1296 LUSTRE_VERSION_STRING);
1297 ptlrpc_deactivate_import(imp);
1298 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1303 ptlrpc_maybe_ping_import_soon(imp);
1305 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1306 obd2cli_tgt(imp->imp_obd),
1307 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1310 wake_up_all(&imp->imp_recovery_waitq);
1315 * interpret callback for "completed replay" RPCs.
1316 * \see signal_completed_replay
1318 static int completed_replay_interpret(const struct lu_env *env,
1319 struct ptlrpc_request *req,
1320 void * data, int rc)
1323 atomic_dec(&req->rq_import->imp_replay_inflight);
1324 if (req->rq_status == 0 &&
1325 !req->rq_import->imp_vbr_failed) {
1326 ptlrpc_import_recovery_state_machine(req->rq_import);
1328 if (req->rq_import->imp_vbr_failed) {
1330 "%s: version recovery fails, reconnecting\n",
1331 req->rq_import->imp_obd->obd_name);
1333 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1335 req->rq_import->imp_obd->obd_name,
1338 ptlrpc_connect_import(req->rq_import);
1345 * Let server know that we have no requests to replay anymore.
1346 * Achieved by just sending a PING request
1348 static int signal_completed_replay(struct obd_import *imp)
1350 struct ptlrpc_request *req;
1353 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1356 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1357 atomic_inc(&imp->imp_replay_inflight);
1359 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1362 atomic_dec(&imp->imp_replay_inflight);
1366 ptlrpc_request_set_replen(req);
1367 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1368 lustre_msg_add_flags(req->rq_reqmsg,
1369 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1371 req->rq_timeout *= 3;
1372 req->rq_interpret_reply = completed_replay_interpret;
1374 ptlrpcd_add_req(req);
1379 * In kernel code all import invalidation happens in its own
1380 * separate thread, so that whatever application happened to encounter
1381 * a problem could still be killed or otherwise continue
1383 static int ptlrpc_invalidate_import_thread(void *data)
1385 struct obd_import *imp = data;
1389 unshare_fs_struct();
1391 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1392 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1393 imp->imp_connection->c_remote_uuid.uuid);
1395 ptlrpc_invalidate_import(imp);
1397 if (obd_dump_on_eviction) {
1398 CERROR("dump the log upon eviction\n");
1399 libcfs_debug_dumplog();
1402 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1403 ptlrpc_import_recovery_state_machine(imp);
1405 class_import_put(imp);
1410 * This is the state machine for client-side recovery on import.
1412 * Typicaly we have two possibly paths. If we came to server and it is not
1413 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1414 * state and reconnect from scratch.
1415 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1416 * We go through our list of requests to replay and send them to server one by
1418 * After sending all request from the list we change import state to
1419 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1420 * and also all the locks we don't yet have and wait for server to grant us.
1421 * After that we send a special "replay completed" request and change import
1422 * state to IMP_REPLAY_WAIT.
1423 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1424 * state and resend all requests from sending list.
1425 * After that we promote import to FULL state and send all delayed requests
1426 * and import is fully operational after that.
1429 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1437 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1438 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1439 &target_start, &target_len);
1440 /* Don't care about MGC eviction */
1441 if (strcmp(imp->imp_obd->obd_type->typ_name,
1442 LUSTRE_MGC_NAME) != 0) {
1443 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1444 "by %.*s; in progress operations "
1445 "using this service will fail.\n",
1446 imp->imp_obd->obd_name, target_len,
1449 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1450 obd2cli_tgt(imp->imp_obd),
1451 imp->imp_connection->c_remote_uuid.uuid);
1452 /* reset vbr_failed flag upon eviction */
1453 spin_lock(&imp->imp_lock);
1454 imp->imp_vbr_failed = 0;
1455 spin_unlock(&imp->imp_lock);
1458 struct task_struct *task;
1459 /* bug 17802: XXX client_disconnect_export vs connect request
1460 * race. if client is evicted at this time then we start
1461 * invalidate thread without reference to import and import can
1462 * be freed at same time. */
1463 class_import_get(imp);
1464 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1467 class_import_put(imp);
1468 CERROR("error starting invalidate thread: %d\n", rc);
1477 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1478 CDEBUG(D_HA, "replay requested by %s\n",
1479 obd2cli_tgt(imp->imp_obd));
1480 rc = ptlrpc_replay_next(imp, &inflight);
1481 if (inflight == 0 &&
1482 atomic_read(&imp->imp_replay_inflight) == 0) {
1483 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1484 rc = ldlm_replay_locks(imp);
1491 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1492 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1493 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1494 rc = signal_completed_replay(imp);
1500 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1501 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1502 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1506 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1507 struct ptlrpc_connection *conn = imp->imp_connection;
1509 rc = ptlrpc_resend(imp);
1512 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1513 ptlrpc_activate_import(imp);
1515 LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
1516 imp->imp_obd->obd_name,
1517 obd_uuid2str(&conn->c_remote_uuid),
1518 obd_import_nid2str(imp));
1521 if (imp->imp_state == LUSTRE_IMP_FULL) {
1522 wake_up_all(&imp->imp_recovery_waitq);
1523 ptlrpc_wake_delayed(imp);
1530 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1532 struct ptlrpc_request *req;
1536 if (imp->imp_obd->obd_force)
1537 GOTO(set_state, rc);
1539 switch (imp->imp_connect_op) {
1541 rq_opc = OST_DISCONNECT;
1544 rq_opc = MDS_DISCONNECT;
1547 rq_opc = MGS_DISCONNECT;
1551 CERROR("%s: don't know how to disconnect from %s "
1552 "(connect_op %d): rc = %d\n",
1553 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1554 imp->imp_connect_op, rc);
1558 if (ptlrpc_import_in_recovery(imp)) {
1559 struct l_wait_info lwi;
1560 long timeout_jiffies;
1564 if (imp->imp_server_timeout)
1565 timeout = obd_timeout >> 1;
1567 timeout = obd_timeout;
1572 req_portal = imp->imp_client->cli_request_portal;
1573 idx = import_at_get_index(imp, req_portal);
1574 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1577 timeout_jiffies = cfs_time_seconds(timeout);
1578 lwi = LWI_TIMEOUT_INTR(max_t(long, timeout_jiffies, 1),
1579 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1580 rc = l_wait_event(imp->imp_recovery_waitq,
1581 !ptlrpc_import_in_recovery(imp), &lwi);
1585 spin_lock(&imp->imp_lock);
1586 if (imp->imp_state != LUSTRE_IMP_FULL)
1588 spin_unlock(&imp->imp_lock);
1590 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1591 LUSTRE_OBD_VERSION, rq_opc);
1593 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1594 * it fails. We can get through the above with a down server
1595 * if the client doesn't know the server is gone yet. */
1596 req->rq_no_resend = 1;
1598 /* We want client umounts to happen quickly, no matter the
1600 req->rq_timeout = min_t(int, req->rq_timeout,
1601 INITIAL_CONNECT_TIMEOUT);
1603 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1604 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1605 ptlrpc_request_set_replen(req);
1606 rc = ptlrpc_queue_wait(req);
1607 ptlrpc_req_finished(req);
1611 spin_lock(&imp->imp_lock);
1614 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1616 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1617 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1618 spin_unlock(&imp->imp_lock);
1620 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1624 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1626 void ptlrpc_cleanup_imp(struct obd_import *imp)
1630 spin_lock(&imp->imp_lock);
1631 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1632 imp->imp_generation++;
1633 spin_unlock(&imp->imp_lock);
1634 ptlrpc_abort_inflight(imp);
1639 /* Adaptive Timeout utils */
1640 extern unsigned int at_min, at_max, at_history;
1642 /* Update at_current with the specified value (bounded by at_min and at_max),
1643 * as well as the AT history "bins".
1644 * - Bin into timeslices using AT_BINS bins.
1645 * - This gives us a max of the last at_history seconds without the storage,
1646 * but still smoothing out a return to normalcy from a slow response.
1647 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1649 int at_measured(struct adaptive_timeout *at, unsigned int val)
1651 unsigned int old = at->at_current;
1652 time64_t now = ktime_get_real_seconds();
1653 long binlimit = max_t(long, at_history / AT_BINS, 1);
1656 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1657 val, at, (long)(now - at->at_binstart), at->at_current,
1658 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1661 /* 0's don't count, because we never want our timeout to
1662 drop to 0, and because 0 could mean an error */
1665 spin_lock(&at->at_lock);
1667 if (unlikely(at->at_binstart == 0)) {
1668 /* Special case to remove default from history */
1669 at->at_current = val;
1670 at->at_worst_ever = val;
1671 at->at_worst_time = now;
1672 at->at_hist[0] = val;
1673 at->at_binstart = now;
1674 } else if (now - at->at_binstart < binlimit ) {
1676 at->at_hist[0] = max(val, at->at_hist[0]);
1677 at->at_current = max(val, at->at_current);
1680 unsigned int maxv = val;
1682 /* move bins over */
1683 shift = (u32)(now - at->at_binstart) / binlimit;
1685 for(i = AT_BINS - 1; i >= 0; i--) {
1687 at->at_hist[i] = at->at_hist[i - shift];
1688 maxv = max(maxv, at->at_hist[i]);
1693 at->at_hist[0] = val;
1694 at->at_current = maxv;
1695 at->at_binstart += shift * binlimit;
1698 if (at->at_current > at->at_worst_ever) {
1699 at->at_worst_ever = at->at_current;
1700 at->at_worst_time = now;
1703 if (at->at_flags & AT_FLG_NOHIST)
1704 /* Only keep last reported val; keeping the rest of the history
1706 at->at_current = val;
1709 at->at_current = min(at->at_current, at_max);
1710 at->at_current = max(at->at_current, at_min);
1712 if (at->at_current != old)
1713 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1714 "(val=%u) hist %u %u %u %u\n", at,
1715 old, at->at_current, at->at_current - old, val,
1716 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1719 /* if we changed, report the old value */
1720 old = (at->at_current != old) ? old : 0;
1722 spin_unlock(&at->at_lock);
1726 /* Find the imp_at index for a given portal; assign if space available */
1727 int import_at_get_index(struct obd_import *imp, int portal)
1729 struct imp_at *at = &imp->imp_at;
1732 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1733 if (at->iat_portal[i] == portal)
1735 if (at->iat_portal[i] == 0)
1740 /* Not found in list, add it under a lock */
1741 spin_lock(&imp->imp_lock);
1743 /* Check unused under lock */
1744 for (; i < IMP_AT_MAX_PORTALS; i++) {
1745 if (at->iat_portal[i] == portal)
1747 if (at->iat_portal[i] == 0)
1752 /* Not enough portals? */
1753 LASSERT(i < IMP_AT_MAX_PORTALS);
1755 at->iat_portal[i] = portal;
1757 spin_unlock(&imp->imp_lock);