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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/import.c
38 * Author: Mike Shaver <shaver@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_RPC
43 #include "../include/obd_support.h"
44 #include "../include/lustre_ha.h"
45 #include "../include/lustre_net.h"
46 #include "../include/lustre_import.h"
47 #include "../include/lustre_export.h"
48 #include "../include/obd.h"
49 #include "../include/obd_cksum.h"
50 #include "../include/obd_class.h"
52 #include "ptlrpc_internal.h"
54 struct ptlrpc_connect_async_args {
55 __u64 pcaa_peer_committed;
56 int pcaa_initial_connect;
60 * Updates import \a imp current state to provided \a state value
61 * Helper function. Must be called under imp_lock.
63 static void __import_set_state(struct obd_import *imp,
64 enum lustre_imp_state state)
67 case LUSTRE_IMP_CLOSED:
69 case LUSTRE_IMP_DISCON:
70 case LUSTRE_IMP_CONNECTING:
72 case LUSTRE_IMP_REPLAY_WAIT:
73 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
76 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
79 imp->imp_state = state;
80 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
81 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
82 ktime_get_real_seconds();
83 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
87 /* A CLOSED import should remain so. */
88 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
90 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
91 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
92 imp, obd2cli_tgt(imp->imp_obd), \
93 ptlrpc_import_state_name(imp->imp_state), \
94 ptlrpc_import_state_name(state)); \
95 __import_set_state(imp, state); \
99 #define IMPORT_SET_STATE(imp, state) \
101 spin_lock(&imp->imp_lock); \
102 IMPORT_SET_STATE_NOLOCK(imp, state); \
103 spin_unlock(&imp->imp_lock); \
106 static int ptlrpc_connect_interpret(const struct lu_env *env,
107 struct ptlrpc_request *request,
109 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
111 /* Only this function is allowed to change the import state when it is
112 * CLOSED. I would rather refcount the import and free it after
113 * disconnection like we do with exports. To do that, the client_obd
114 * will need to save the peer info somewhere other than in the import,
117 int ptlrpc_init_import(struct obd_import *imp)
119 spin_lock(&imp->imp_lock);
121 imp->imp_generation++;
122 imp->imp_state = LUSTRE_IMP_NEW;
124 spin_unlock(&imp->imp_lock);
128 EXPORT_SYMBOL(ptlrpc_init_import);
130 #define UUID_STR "_UUID"
131 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
134 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
135 ? uuid : uuid + strlen(prefix);
137 *uuid_len = strlen(*uuid_start);
139 if (*uuid_len < strlen(UUID_STR))
142 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
143 UUID_STR, strlen(UUID_STR)))
144 *uuid_len -= strlen(UUID_STR);
148 * Returns true if import was FULL, false if import was already not
150 * @imp - import to be disconnected
151 * @conn_cnt - connection count (epoch) of the request that timed out
152 * and caused the disconnection. In some cases, multiple
153 * inflight requests can fail to a single target (e.g. OST
154 * bulk requests) and if one has already caused a reconnection
155 * (increasing the import->conn_cnt) the older failure should
156 * not also cause a reconnection. If zero it forces a reconnect.
158 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
162 spin_lock(&imp->imp_lock);
164 if (imp->imp_state == LUSTRE_IMP_FULL &&
165 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
169 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
170 &target_start, &target_len);
172 if (imp->imp_replayable) {
173 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was lost; in progress operations using this service will wait for recovery to complete\n",
174 imp->imp_obd->obd_name, target_len, target_start,
175 libcfs_nid2str(imp->imp_connection->c_peer.nid));
177 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to %.*s (at %s) was lost; in progress operations using this service will fail\n",
178 imp->imp_obd->obd_name,
179 target_len, target_start,
180 libcfs_nid2str(imp->imp_connection->c_peer.nid));
182 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
183 spin_unlock(&imp->imp_lock);
185 if (obd_dump_on_timeout)
186 libcfs_debug_dumplog();
188 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
191 spin_unlock(&imp->imp_lock);
192 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
193 imp->imp_client->cli_name, imp,
194 (imp->imp_state == LUSTRE_IMP_FULL &&
195 imp->imp_conn_cnt > conn_cnt) ?
196 "reconnected" : "not connected", imp->imp_conn_cnt,
197 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
204 * This acts as a barrier; all existing requests are rejected, and
205 * no new requests will be accepted until the import is valid again.
207 void ptlrpc_deactivate_import(struct obd_import *imp)
209 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
211 spin_lock(&imp->imp_lock);
212 imp->imp_invalid = 1;
213 imp->imp_generation++;
214 spin_unlock(&imp->imp_lock);
216 ptlrpc_abort_inflight(imp);
217 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
219 EXPORT_SYMBOL(ptlrpc_deactivate_import);
222 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time64_t now)
226 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
227 (req->rq_phase == RQ_PHASE_BULK) ||
228 (req->rq_phase == RQ_PHASE_NEW)))
231 if (req->rq_timedout)
234 if (req->rq_phase == RQ_PHASE_NEW)
237 dl = req->rq_deadline;
245 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
247 time64_t now = ktime_get_real_seconds();
248 struct list_head *tmp, *n;
249 struct ptlrpc_request *req;
250 unsigned int timeout = 0;
252 spin_lock(&imp->imp_lock);
253 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
254 req = list_entry(tmp, struct ptlrpc_request, rq_list);
255 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
257 spin_unlock(&imp->imp_lock);
262 * This function will invalidate the import, if necessary, then block
263 * for all the RPC completions, and finally notify the obd to
264 * invalidate its state (ie cancel locks, clear pending requests,
267 void ptlrpc_invalidate_import(struct obd_import *imp)
269 struct list_head *tmp, *n;
270 struct ptlrpc_request *req;
271 struct l_wait_info lwi;
272 unsigned int timeout;
275 atomic_inc(&imp->imp_inval_count);
277 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
278 ptlrpc_deactivate_import(imp);
280 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
281 LASSERT(imp->imp_invalid);
283 /* Wait forever until inflight == 0. We really can't do it another
284 * way because in some cases we need to wait for very long reply
285 * unlink. We can't do anything before that because there is really
286 * no guarantee that some rdma transfer is not in progress right now.
289 /* Calculate max timeout for waiting on rpcs to error
290 * out. Use obd_timeout if calculated value is smaller
293 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
294 timeout = ptlrpc_inflight_timeout(imp);
295 timeout += timeout / 3;
298 timeout = obd_timeout;
300 /* decrease the interval to increase race condition */
305 "Sleeping %d sec for inflight to error out\n",
308 /* Wait for all requests to error out and call completion
309 * callbacks. Cap it at obd_timeout -- these should all
310 * have been locally cancelled by ptlrpc_abort_inflight.
312 lwi = LWI_TIMEOUT_INTERVAL(
313 cfs_timeout_cap(cfs_time_seconds(timeout)),
314 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
316 rc = l_wait_event(imp->imp_recovery_waitq,
317 (atomic_read(&imp->imp_inflight) == 0),
320 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
322 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
324 atomic_read(&imp->imp_inflight));
326 spin_lock(&imp->imp_lock);
327 if (atomic_read(&imp->imp_inflight) == 0) {
328 int count = atomic_read(&imp->imp_unregistering);
330 /* We know that "unregistering" rpcs only can
331 * survive in sending or delaying lists (they
332 * maybe waiting for long reply unlink in
333 * sluggish nets). Let's check this. If there
334 * is no inflight and unregistering != 0, this
337 LASSERTF(count == 0, "Some RPCs are still unregistering: %d\n",
340 /* Let's save one loop as soon as inflight have
341 * dropped to zero. No new inflights possible at
346 list_for_each_safe(tmp, n,
347 &imp->imp_sending_list) {
348 req = list_entry(tmp,
349 struct ptlrpc_request,
351 DEBUG_REQ(D_ERROR, req,
352 "still on sending list");
354 list_for_each_safe(tmp, n,
355 &imp->imp_delayed_list) {
356 req = list_entry(tmp,
357 struct ptlrpc_request,
359 DEBUG_REQ(D_ERROR, req,
360 "still on delayed list");
363 CERROR("%s: RPCs in \"%s\" phase found (%d). Network is sluggish? Waiting them to error out.\n",
365 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
369 spin_unlock(&imp->imp_lock);
374 * Let's additionally check that no new rpcs added to import in
375 * "invalidate" state.
377 LASSERT(atomic_read(&imp->imp_inflight) == 0);
378 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
379 sptlrpc_import_flush_all_ctx(imp);
381 atomic_dec(&imp->imp_inval_count);
382 wake_up_all(&imp->imp_recovery_waitq);
384 EXPORT_SYMBOL(ptlrpc_invalidate_import);
386 /* unset imp_invalid */
387 void ptlrpc_activate_import(struct obd_import *imp)
389 struct obd_device *obd = imp->imp_obd;
391 spin_lock(&imp->imp_lock);
392 if (imp->imp_deactive != 0) {
393 spin_unlock(&imp->imp_lock);
397 imp->imp_invalid = 0;
398 spin_unlock(&imp->imp_lock);
399 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
401 EXPORT_SYMBOL(ptlrpc_activate_import);
403 static void ptlrpc_pinger_force(struct obd_import *imp)
405 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
406 ptlrpc_import_state_name(imp->imp_state));
408 spin_lock(&imp->imp_lock);
409 imp->imp_force_verify = 1;
410 spin_unlock(&imp->imp_lock);
412 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
413 ptlrpc_pinger_wake_up();
416 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
418 LASSERT(!imp->imp_dlm_fake);
420 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
421 if (!imp->imp_replayable) {
422 CDEBUG(D_HA, "import %s@%s for %s not replayable, auto-deactivating\n",
423 obd2cli_tgt(imp->imp_obd),
424 imp->imp_connection->c_remote_uuid.uuid,
425 imp->imp_obd->obd_name);
426 ptlrpc_deactivate_import(imp);
429 ptlrpc_pinger_force(imp);
432 EXPORT_SYMBOL(ptlrpc_fail_import);
434 int ptlrpc_reconnect_import(struct obd_import *imp)
436 struct l_wait_info lwi;
437 int secs = cfs_time_seconds(obd_timeout);
440 ptlrpc_pinger_force(imp);
442 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
443 obd2cli_tgt(imp->imp_obd), secs);
445 lwi = LWI_TIMEOUT(secs, NULL, NULL);
446 rc = l_wait_event(imp->imp_recovery_waitq,
447 !ptlrpc_import_in_recovery(imp), &lwi);
448 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
449 ptlrpc_import_state_name(imp->imp_state));
452 EXPORT_SYMBOL(ptlrpc_reconnect_import);
455 * Connection on import \a imp is changed to another one (if more than one is
456 * present). We typically chose connection that we have not tried to connect to
459 static int import_select_connection(struct obd_import *imp)
461 struct obd_import_conn *imp_conn = NULL, *conn;
462 struct obd_export *dlmexp;
464 int target_len, tried_all = 1;
466 spin_lock(&imp->imp_lock);
468 if (list_empty(&imp->imp_conn_list)) {
469 CERROR("%s: no connections available\n",
470 imp->imp_obd->obd_name);
471 spin_unlock(&imp->imp_lock);
475 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
476 CDEBUG(D_HA, "%s: connect to NID %s last attempt %llu\n",
477 imp->imp_obd->obd_name,
478 libcfs_nid2str(conn->oic_conn->c_peer.nid),
479 conn->oic_last_attempt);
481 /* If we have not tried this connection since
482 * the last successful attempt, go with this one
484 if ((conn->oic_last_attempt == 0) ||
485 cfs_time_beforeq_64(conn->oic_last_attempt,
486 imp->imp_last_success_conn)) {
492 /* If all of the connections have already been tried
493 * since the last successful connection; just choose the
494 * least recently used
498 else if (cfs_time_before_64(conn->oic_last_attempt,
499 imp_conn->oic_last_attempt))
503 /* if not found, simply choose the current one */
504 if (!imp_conn || imp->imp_force_reconnect) {
505 LASSERT(imp->imp_conn_current);
506 imp_conn = imp->imp_conn_current;
509 LASSERT(imp_conn->oic_conn);
511 /* If we've tried everything, and we're back to the beginning of the
512 * list, increase our timeout and try again. It will be reset when
513 * we do finally connect. (FIXME: really we should wait for all network
514 * state associated with the last connection attempt to drain before
515 * trying to reconnect on it.)
517 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
518 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
520 if (at_get(at) < CONNECTION_SWITCH_MAX) {
521 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
522 if (at_get(at) > CONNECTION_SWITCH_MAX)
523 at_reset(at, CONNECTION_SWITCH_MAX);
525 LASSERT(imp_conn->oic_last_attempt);
526 CDEBUG(D_HA, "%s: tried all connections, increasing latency to %ds\n",
527 imp->imp_obd->obd_name, at_get(at));
530 imp_conn->oic_last_attempt = cfs_time_current_64();
532 /* switch connection, don't mind if it's same as the current one */
533 ptlrpc_connection_put(imp->imp_connection);
534 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
536 dlmexp = class_conn2export(&imp->imp_dlm_handle);
537 ptlrpc_connection_put(dlmexp->exp_connection);
538 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
539 class_export_put(dlmexp);
541 if (imp->imp_conn_current != imp_conn) {
542 if (imp->imp_conn_current) {
543 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
544 &target_start, &target_len);
546 CDEBUG(D_HA, "%s: Connection changing to %.*s (at %s)\n",
547 imp->imp_obd->obd_name,
548 target_len, target_start,
549 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
552 imp->imp_conn_current = imp_conn;
555 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
556 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
557 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
559 spin_unlock(&imp->imp_lock);
565 * must be called under imp_lock
567 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
569 struct ptlrpc_request *req;
570 struct list_head *tmp;
572 /* The requests in committed_list always have smaller transnos than
573 * the requests in replay_list
575 if (!list_empty(&imp->imp_committed_list)) {
576 tmp = imp->imp_committed_list.next;
577 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
578 *transno = req->rq_transno;
579 if (req->rq_transno == 0) {
580 DEBUG_REQ(D_ERROR, req,
581 "zero transno in committed_list");
586 if (!list_empty(&imp->imp_replay_list)) {
587 tmp = imp->imp_replay_list.next;
588 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
589 *transno = req->rq_transno;
590 if (req->rq_transno == 0) {
591 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
600 * Attempt to (re)connect import \a imp. This includes all preparations,
601 * initializing CONNECT RPC request and passing it to ptlrpcd for
603 * Returns 0 on success or error code.
605 int ptlrpc_connect_import(struct obd_import *imp)
607 struct obd_device *obd = imp->imp_obd;
608 int initial_connect = 0;
610 __u64 committed_before_reconnect = 0;
611 struct ptlrpc_request *request;
612 char *bufs[] = { NULL,
613 obd2cli_tgt(imp->imp_obd),
615 (char *)&imp->imp_dlm_handle,
616 (char *)&imp->imp_connect_data };
617 struct ptlrpc_connect_async_args *aa;
620 spin_lock(&imp->imp_lock);
621 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
622 spin_unlock(&imp->imp_lock);
623 CERROR("can't connect to a closed import\n");
625 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
626 spin_unlock(&imp->imp_lock);
627 CERROR("already connected\n");
629 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
630 spin_unlock(&imp->imp_lock);
631 CERROR("already connecting\n");
635 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
638 imp->imp_resend_replay = 0;
640 if (!lustre_handle_is_used(&imp->imp_remote_handle))
643 committed_before_reconnect = imp->imp_peer_committed_transno;
645 set_transno = ptlrpc_first_transno(imp,
646 &imp->imp_connect_data.ocd_transno);
647 spin_unlock(&imp->imp_lock);
649 rc = import_select_connection(imp);
653 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
657 /* Reset connect flags to the originally requested flags, in case
658 * the server is updated on-the-fly we will get the new features.
660 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
661 /* Reset ocd_version each time so the server knows the exact versions */
662 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
663 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
664 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
666 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
667 &obd->obd_uuid, &imp->imp_connect_data, NULL);
671 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
677 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
678 imp->imp_connect_op, bufs, NULL);
680 ptlrpc_request_free(request);
684 /* Report the rpc service time to the server so that it knows how long
685 * to wait for clients to join recovery
687 lustre_msg_set_service_time(request->rq_reqmsg,
688 at_timeout2est(request->rq_timeout));
690 /* The amount of time we give the server to process the connect req.
691 * import_select_connection will increase the net latency on
692 * repeated reconnect attempts to cover slow networks.
693 * We override/ignore the server rpc completion estimate here,
694 * which may be large if this is a reconnect attempt
696 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
697 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
699 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
701 request->rq_no_resend = request->rq_no_delay = 1;
702 request->rq_send_state = LUSTRE_IMP_CONNECTING;
703 /* Allow a slightly larger reply for future growth compatibility */
704 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
705 sizeof(struct obd_connect_data)+16*sizeof(__u64));
706 ptlrpc_request_set_replen(request);
707 request->rq_interpret_reply = ptlrpc_connect_interpret;
709 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
710 aa = ptlrpc_req_async_args(request);
711 memset(aa, 0, sizeof(*aa));
713 aa->pcaa_peer_committed = committed_before_reconnect;
714 aa->pcaa_initial_connect = initial_connect;
716 if (aa->pcaa_initial_connect) {
717 spin_lock(&imp->imp_lock);
718 imp->imp_replayable = 1;
719 spin_unlock(&imp->imp_lock);
720 lustre_msg_add_op_flags(request->rq_reqmsg,
721 MSG_CONNECT_INITIAL);
725 lustre_msg_add_op_flags(request->rq_reqmsg,
726 MSG_CONNECT_TRANSNO);
728 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
729 request->rq_timeout);
730 ptlrpcd_add_req(request);
734 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
738 EXPORT_SYMBOL(ptlrpc_connect_import);
740 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
744 spin_lock(&imp->imp_lock);
745 force_verify = imp->imp_force_verify != 0;
746 spin_unlock(&imp->imp_lock);
749 ptlrpc_pinger_wake_up();
752 static int ptlrpc_busy_reconnect(int rc)
754 return (rc == -EBUSY) || (rc == -EAGAIN);
758 * interpret_reply callback for connect RPCs.
759 * Looks into returned status of connect operation and decides
760 * what to do with the import - i.e enter recovery, promote it to
761 * full state for normal operations of disconnect it due to an error.
763 static int ptlrpc_connect_interpret(const struct lu_env *env,
764 struct ptlrpc_request *request,
767 struct ptlrpc_connect_async_args *aa = data;
768 struct obd_import *imp = request->rq_import;
769 struct client_obd *cli = &imp->imp_obd->u.cli;
770 struct lustre_handle old_hdl;
771 __u64 old_connect_flags;
773 struct obd_connect_data *ocd;
774 struct obd_export *exp;
777 spin_lock(&imp->imp_lock);
778 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
779 imp->imp_connect_tried = 1;
780 spin_unlock(&imp->imp_lock);
785 /* if this reconnect to busy export - not need select new target
788 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
789 spin_unlock(&imp->imp_lock);
790 ptlrpc_maybe_ping_import_soon(imp);
793 spin_unlock(&imp->imp_lock);
795 LASSERT(imp->imp_conn_current);
797 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
799 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
801 /* server replied obd_connect_data is always bigger */
802 ocd = req_capsule_server_sized_get(&request->rq_pill,
803 &RMF_CONNECT_DATA, ret);
806 CERROR("%s: no connect data from server\n",
807 imp->imp_obd->obd_name);
812 spin_lock(&imp->imp_lock);
814 /* All imports are pingable */
815 imp->imp_pingable = 1;
816 imp->imp_force_reconnect = 0;
817 imp->imp_force_verify = 0;
819 imp->imp_connect_data = *ocd;
821 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
822 imp->imp_obd->obd_name, ocd->ocd_instance);
823 exp = class_conn2export(&imp->imp_dlm_handle);
825 spin_unlock(&imp->imp_lock);
827 /* check that server granted subset of flags we asked for. */
828 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
829 ocd->ocd_connect_flags) {
830 CERROR("%s: Server didn't granted asked subset of flags: asked=%#llx grranted=%#llx\n",
831 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
832 ocd->ocd_connect_flags);
838 /* This could happen if export is cleaned during the
841 CERROR("%s: missing export after connect\n",
842 imp->imp_obd->obd_name);
846 old_connect_flags = exp_connect_flags(exp);
847 exp->exp_connect_data = *ocd;
848 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
849 class_export_put(exp);
851 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
853 if (aa->pcaa_initial_connect) {
854 spin_lock(&imp->imp_lock);
855 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
856 imp->imp_replayable = 1;
857 spin_unlock(&imp->imp_lock);
858 CDEBUG(D_HA, "connected to replayable target: %s\n",
859 obd2cli_tgt(imp->imp_obd));
861 imp->imp_replayable = 0;
862 spin_unlock(&imp->imp_lock);
865 /* if applies, adjust the imp->imp_msg_magic here
866 * according to reply flags
869 imp->imp_remote_handle =
870 *lustre_msg_get_handle(request->rq_repmsg);
872 /* Initial connects are allowed for clients with non-random
873 * uuids when servers are in recovery. Simply signal the
874 * servers replay is complete and wait in REPLAY_WAIT.
876 if (msg_flags & MSG_CONNECT_RECOVERING) {
877 CDEBUG(D_HA, "connect to %s during recovery\n",
878 obd2cli_tgt(imp->imp_obd));
879 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
881 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
882 ptlrpc_activate_import(imp);
889 /* Determine what recovery state to move the import to. */
890 if (msg_flags & MSG_CONNECT_RECONNECT) {
891 memset(&old_hdl, 0, sizeof(old_hdl));
892 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
894 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due bad handle %#llx\n",
895 obd2cli_tgt(imp->imp_obd),
896 imp->imp_connection->c_remote_uuid.uuid,
897 imp->imp_dlm_handle.cookie);
902 if (memcmp(&imp->imp_remote_handle,
903 lustre_msg_get_handle(request->rq_repmsg),
904 sizeof(imp->imp_remote_handle))) {
905 int level = msg_flags & MSG_CONNECT_RECOVERING ?
908 /* Bug 16611/14775: if server handle have changed,
909 * that means some sort of disconnection happened.
910 * If the server is not in recovery, that also means it
911 * already erased all of our state because of previous
912 * eviction. If it is in recovery - we are safe to
913 * participate since we can reestablish all of our state
916 if ((msg_flags & MSG_CONNECT_RECOVERING)) {
917 CDEBUG(level, "%s@%s changed server handle from %#llx to %#llx but is still in recovery\n",
918 obd2cli_tgt(imp->imp_obd),
919 imp->imp_connection->c_remote_uuid.uuid,
920 imp->imp_remote_handle.cookie,
921 lustre_msg_get_handle(
922 request->rq_repmsg)->cookie);
924 LCONSOLE_WARN("Evicted from %s (at %s) after server handle changed from %#llx to %#llx\n",
925 obd2cli_tgt(imp->imp_obd),
926 imp->imp_connection-> \
928 imp->imp_remote_handle.cookie,
929 lustre_msg_get_handle(
930 request->rq_repmsg)->cookie);
933 imp->imp_remote_handle =
934 *lustre_msg_get_handle(request->rq_repmsg);
936 if (!(msg_flags & MSG_CONNECT_RECOVERING)) {
937 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
943 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
944 obd2cli_tgt(imp->imp_obd),
945 imp->imp_connection->c_remote_uuid.uuid);
948 if (imp->imp_invalid) {
949 CDEBUG(D_HA, "%s: reconnected but import is invalid; marking evicted\n",
950 imp->imp_obd->obd_name);
951 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
952 } else if (msg_flags & MSG_CONNECT_RECOVERING) {
953 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
954 imp->imp_obd->obd_name,
955 obd2cli_tgt(imp->imp_obd));
957 spin_lock(&imp->imp_lock);
958 imp->imp_resend_replay = 1;
959 spin_unlock(&imp->imp_lock);
961 IMPORT_SET_STATE(imp, imp->imp_replay_state);
963 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
965 } else if ((msg_flags & MSG_CONNECT_RECOVERING) && !imp->imp_invalid) {
966 LASSERT(imp->imp_replayable);
967 imp->imp_remote_handle =
968 *lustre_msg_get_handle(request->rq_repmsg);
969 imp->imp_last_replay_transno = 0;
970 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
972 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags not set: %x)",
973 imp->imp_obd->obd_name, msg_flags);
974 imp->imp_remote_handle =
975 *lustre_msg_get_handle(request->rq_repmsg);
976 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
979 /* Sanity checks for a reconnected import. */
980 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
981 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
984 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
985 lustre_msg_get_last_committed(request->rq_repmsg) <
986 aa->pcaa_peer_committed) {
987 CERROR("%s went back in time (transno %lld was previously committed, server now claims %lld)! See https://bugzilla.lustre.org/show_bug.cgi?id=9646\n",
988 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
989 lustre_msg_get_last_committed(request->rq_repmsg));
993 rc = ptlrpc_import_recovery_state_machine(imp);
995 if (rc == -ENOTCONN) {
996 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery; invalidating and reconnecting\n",
997 obd2cli_tgt(imp->imp_obd),
998 imp->imp_connection->c_remote_uuid.uuid);
999 ptlrpc_connect_import(imp);
1000 imp->imp_connect_tried = 1;
1005 spin_lock(&imp->imp_lock);
1006 list_del(&imp->imp_conn_current->oic_item);
1007 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
1008 imp->imp_last_success_conn =
1009 imp->imp_conn_current->oic_last_attempt;
1011 spin_unlock(&imp->imp_lock);
1013 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
1014 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
1015 LCONSOLE_WARN("%s: MDS %s does not support ibits lock, either very old or invalid: requested %llx, replied %llx\n",
1016 imp->imp_obd->obd_name,
1017 imp->imp_connection->c_remote_uuid.uuid,
1018 imp->imp_connect_flags_orig,
1019 ocd->ocd_connect_flags);
1024 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1025 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1026 LUSTRE_VERSION_OFFSET_WARN ||
1027 ocd->ocd_version < LUSTRE_VERSION_CODE -
1028 LUSTRE_VERSION_OFFSET_WARN)) {
1029 /* Sigh, some compilers do not like #ifdef in the middle
1030 * of macro arguments
1032 const char *older = "older. Consider upgrading server or downgrading client"
1034 const char *newer = "newer than client version. Consider upgrading client"
1037 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) is much %s (%s)\n",
1038 obd2cli_tgt(imp->imp_obd),
1039 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1040 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1041 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1042 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1043 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1044 newer : older, LUSTRE_VERSION_STRING);
1047 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1048 /* Check if server has LU-1252 fix applied to not always swab
1049 * the IR MNE entries. Do this only once per connection. This
1050 * fixup is version-limited, because we don't want to carry the
1051 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
1052 * need interop with unpatched 2.2 servers. For newer servers,
1053 * the client will do MNE swabbing only as needed. LU-1644
1055 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1056 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
1057 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
1058 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
1059 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
1060 strcmp(imp->imp_obd->obd_type->typ_name,
1061 LUSTRE_MGC_NAME) == 0))
1062 imp->imp_need_mne_swab = 1;
1063 else /* clear if server was upgraded since last connect */
1064 imp->imp_need_mne_swab = 0;
1066 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1069 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1070 /* We sent to the server ocd_cksum_types with bits set
1071 * for algorithms we understand. The server masked off
1072 * the checksum types it doesn't support
1074 if ((ocd->ocd_cksum_types &
1075 cksum_types_supported_client()) == 0) {
1076 LCONSOLE_WARN("The negotiation of the checksum algorithm to use with server %s failed (%x/%x), disabling checksums\n",
1077 obd2cli_tgt(imp->imp_obd),
1078 ocd->ocd_cksum_types,
1079 cksum_types_supported_client());
1080 cli->cl_checksum = 0;
1081 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1083 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1086 /* The server does not support OBD_CONNECT_CKSUM.
1087 * Enforce ADLER for backward compatibility
1089 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1091 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
1093 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
1094 cli->cl_max_pages_per_rpc =
1095 min(ocd->ocd_brw_size >> PAGE_CACHE_SHIFT,
1096 cli->cl_max_pages_per_rpc);
1097 else if (imp->imp_connect_op == MDS_CONNECT ||
1098 imp->imp_connect_op == MGS_CONNECT)
1099 cli->cl_max_pages_per_rpc = 1;
1101 /* Reset ns_connect_flags only for initial connect. It might be
1102 * changed in while using FS and if we reset it in reconnect
1103 * this leads to losing user settings done before such as
1104 * disable lru_resize, etc.
1106 if (old_connect_flags != exp_connect_flags(exp) ||
1107 aa->pcaa_initial_connect) {
1108 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server flags: %#llx\n",
1109 imp->imp_obd->obd_name, ocd->ocd_connect_flags);
1110 imp->imp_obd->obd_namespace->ns_connect_flags =
1111 ocd->ocd_connect_flags;
1112 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1113 ocd->ocd_connect_flags;
1116 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1117 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1118 /* We need a per-message support flag, because
1119 * a. we don't know if the incoming connect reply
1120 * supports AT or not (in reply_in_callback)
1121 * until we unpack it.
1122 * b. failovered server means export and flags are gone
1123 * (in ptlrpc_send_reply).
1124 * Can only be set when we know AT is supported at
1127 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1129 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1131 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1132 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1133 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1135 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1137 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1138 (cli->cl_max_pages_per_rpc > 0));
1142 imp->imp_connect_tried = 1;
1145 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1146 if (rc == -EACCES) {
1148 * Give up trying to reconnect
1149 * EACCES means client has no permission for connection
1151 imp->imp_obd->obd_no_recov = 1;
1152 ptlrpc_deactivate_import(imp);
1155 if (rc == -EPROTO) {
1156 struct obd_connect_data *ocd;
1158 /* reply message might not be ready */
1159 if (!request->rq_repmsg)
1162 ocd = req_capsule_server_get(&request->rq_pill,
1165 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1166 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1168 * Actually servers are only supposed to refuse
1169 * connection from liblustre clients, so we
1170 * should never see this from VFS context
1172 LCONSOLE_ERROR_MSG(0x16a, "Server %s version (%d.%d.%d.%d) refused connection from this client with an incompatible version (%s). Client must be recompiled\n",
1173 obd2cli_tgt(imp->imp_obd),
1174 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1175 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1176 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1177 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1178 LUSTRE_VERSION_STRING);
1179 ptlrpc_deactivate_import(imp);
1180 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1185 ptlrpc_maybe_ping_import_soon(imp);
1187 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1188 obd2cli_tgt(imp->imp_obd),
1189 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1192 wake_up_all(&imp->imp_recovery_waitq);
1197 * interpret callback for "completed replay" RPCs.
1198 * \see signal_completed_replay
1200 static int completed_replay_interpret(const struct lu_env *env,
1201 struct ptlrpc_request *req,
1204 atomic_dec(&req->rq_import->imp_replay_inflight);
1205 if (req->rq_status == 0 &&
1206 !req->rq_import->imp_vbr_failed) {
1207 ptlrpc_import_recovery_state_machine(req->rq_import);
1209 if (req->rq_import->imp_vbr_failed) {
1211 "%s: version recovery fails, reconnecting\n",
1212 req->rq_import->imp_obd->obd_name);
1214 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, reconnecting\n",
1215 req->rq_import->imp_obd->obd_name,
1218 ptlrpc_connect_import(req->rq_import);
1225 * Let server know that we have no requests to replay anymore.
1226 * Achieved by just sending a PING request
1228 static int signal_completed_replay(struct obd_import *imp)
1230 struct ptlrpc_request *req;
1232 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1235 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1236 atomic_inc(&imp->imp_replay_inflight);
1238 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1241 atomic_dec(&imp->imp_replay_inflight);
1245 ptlrpc_request_set_replen(req);
1246 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1247 lustre_msg_add_flags(req->rq_reqmsg,
1248 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1250 req->rq_timeout *= 3;
1251 req->rq_interpret_reply = completed_replay_interpret;
1253 ptlrpcd_add_req(req);
1258 * In kernel code all import invalidation happens in its own
1259 * separate thread, so that whatever application happened to encounter
1260 * a problem could still be killed or otherwise continue
1262 static int ptlrpc_invalidate_import_thread(void *data)
1264 struct obd_import *imp = data;
1266 unshare_fs_struct();
1268 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1269 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1270 imp->imp_connection->c_remote_uuid.uuid);
1272 ptlrpc_invalidate_import(imp);
1274 if (obd_dump_on_eviction) {
1275 CERROR("dump the log upon eviction\n");
1276 libcfs_debug_dumplog();
1279 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1280 ptlrpc_import_recovery_state_machine(imp);
1282 class_import_put(imp);
1287 * This is the state machine for client-side recovery on import.
1289 * Typically we have two possibly paths. If we came to server and it is not
1290 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1291 * state and reconnect from scratch.
1292 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1293 * We go through our list of requests to replay and send them to server one by
1295 * After sending all request from the list we change import state to
1296 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1297 * and also all the locks we don't yet have and wait for server to grant us.
1298 * After that we send a special "replay completed" request and change import
1299 * state to IMP_REPLAY_WAIT.
1300 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1301 * state and resend all requests from sending list.
1302 * After that we promote import to FULL state and send all delayed requests
1303 * and import is fully operational after that.
1306 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1313 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1314 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1315 &target_start, &target_len);
1316 /* Don't care about MGC eviction */
1317 if (strcmp(imp->imp_obd->obd_type->typ_name,
1318 LUSTRE_MGC_NAME) != 0) {
1319 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted by %.*s; in progress operations using this service will fail.\n",
1320 imp->imp_obd->obd_name, target_len,
1323 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1324 obd2cli_tgt(imp->imp_obd),
1325 imp->imp_connection->c_remote_uuid.uuid);
1326 /* reset vbr_failed flag upon eviction */
1327 spin_lock(&imp->imp_lock);
1328 imp->imp_vbr_failed = 0;
1329 spin_unlock(&imp->imp_lock);
1332 struct task_struct *task;
1333 /* bug 17802: XXX client_disconnect_export vs connect request
1334 * race. if client is evicted at this time, we start
1335 * invalidate thread without reference to import and import can
1336 * be freed at same time.
1338 class_import_get(imp);
1339 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1342 class_import_put(imp);
1343 CERROR("error starting invalidate thread: %d\n", rc);
1352 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1353 CDEBUG(D_HA, "replay requested by %s\n",
1354 obd2cli_tgt(imp->imp_obd));
1355 rc = ptlrpc_replay_next(imp, &inflight);
1356 if (inflight == 0 &&
1357 atomic_read(&imp->imp_replay_inflight) == 0) {
1358 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1359 rc = ldlm_replay_locks(imp);
1366 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1367 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1368 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1369 rc = signal_completed_replay(imp);
1376 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1377 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1378 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1382 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1383 CDEBUG(D_HA, "reconnected to %s@%s\n",
1384 obd2cli_tgt(imp->imp_obd),
1385 imp->imp_connection->c_remote_uuid.uuid);
1387 rc = ptlrpc_resend(imp);
1390 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1391 ptlrpc_activate_import(imp);
1393 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1394 &target_start, &target_len);
1395 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1396 imp->imp_obd->obd_name,
1397 target_len, target_start,
1398 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1401 if (imp->imp_state == LUSTRE_IMP_FULL) {
1402 wake_up_all(&imp->imp_recovery_waitq);
1403 ptlrpc_wake_delayed(imp);
1410 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1412 struct ptlrpc_request *req;
1415 if (imp->imp_obd->obd_force)
1418 switch (imp->imp_connect_op) {
1420 rq_opc = OST_DISCONNECT;
1423 rq_opc = MDS_DISCONNECT;
1426 rq_opc = MGS_DISCONNECT;
1430 CERROR("%s: don't know how to disconnect from %s (connect_op %d): rc = %d\n",
1431 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1432 imp->imp_connect_op, rc);
1436 if (ptlrpc_import_in_recovery(imp)) {
1437 struct l_wait_info lwi;
1441 if (imp->imp_server_timeout)
1442 timeout = cfs_time_seconds(obd_timeout / 2);
1444 timeout = cfs_time_seconds(obd_timeout);
1446 int idx = import_at_get_index(imp,
1447 imp->imp_client->cli_request_portal);
1448 timeout = cfs_time_seconds(
1449 at_get(&imp->imp_at.iat_service_estimate[idx]));
1452 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1453 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1454 rc = l_wait_event(imp->imp_recovery_waitq,
1455 !ptlrpc_import_in_recovery(imp), &lwi);
1459 spin_lock(&imp->imp_lock);
1460 if (imp->imp_state != LUSTRE_IMP_FULL)
1462 spin_unlock(&imp->imp_lock);
1464 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1465 LUSTRE_OBD_VERSION, rq_opc);
1467 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1468 * it fails. We can get through the above with a down server
1469 * if the client doesn't know the server is gone yet.
1471 req->rq_no_resend = 1;
1473 /* We want client umounts to happen quickly, no matter the
1476 req->rq_timeout = min_t(int, req->rq_timeout,
1477 INITIAL_CONNECT_TIMEOUT);
1479 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1480 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1481 ptlrpc_request_set_replen(req);
1482 rc = ptlrpc_queue_wait(req);
1483 ptlrpc_req_finished(req);
1487 spin_lock(&imp->imp_lock);
1490 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1492 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1493 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1494 spin_unlock(&imp->imp_lock);
1496 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1501 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1503 /* Adaptive Timeout utils */
1504 extern unsigned int at_min, at_max, at_history;
1506 /* Bin into timeslices using AT_BINS bins.
1507 * This gives us a max of the last binlimit*AT_BINS secs without the storage,
1508 * but still smoothing out a return to normalcy from a slow response.
1509 * (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1511 int at_measured(struct adaptive_timeout *at, unsigned int val)
1513 unsigned int old = at->at_current;
1514 time64_t now = ktime_get_real_seconds();
1515 long binlimit = max_t(long, at_history / AT_BINS, 1);
1518 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1519 val, at, (long)(now - at->at_binstart), at->at_current,
1520 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1523 /* 0's don't count, because we never want our timeout to
1524 * drop to 0, and because 0 could mean an error
1528 spin_lock(&at->at_lock);
1530 if (unlikely(at->at_binstart == 0)) {
1531 /* Special case to remove default from history */
1532 at->at_current = val;
1533 at->at_worst_ever = val;
1534 at->at_worst_time = now;
1535 at->at_hist[0] = val;
1536 at->at_binstart = now;
1537 } else if (now - at->at_binstart < binlimit) {
1539 at->at_hist[0] = max(val, at->at_hist[0]);
1540 at->at_current = max(val, at->at_current);
1543 unsigned int maxv = val;
1544 /* move bins over */
1545 shift = (u32)(now - at->at_binstart) / binlimit;
1547 for (i = AT_BINS - 1; i >= 0; i--) {
1549 at->at_hist[i] = at->at_hist[i - shift];
1550 maxv = max(maxv, at->at_hist[i]);
1555 at->at_hist[0] = val;
1556 at->at_current = maxv;
1557 at->at_binstart += shift * binlimit;
1560 if (at->at_current > at->at_worst_ever) {
1561 at->at_worst_ever = at->at_current;
1562 at->at_worst_time = now;
1565 if (at->at_flags & AT_FLG_NOHIST)
1566 /* Only keep last reported val; keeping the rest of the history
1569 at->at_current = val;
1572 at->at_current = min(at->at_current, at_max);
1573 at->at_current = max(at->at_current, at_min);
1575 if (at->at_current != old)
1576 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d (val=%u) hist %u %u %u %u\n",
1578 old, at->at_current, at->at_current - old, val,
1579 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1582 /* if we changed, report the old value */
1583 old = (at->at_current != old) ? old : 0;
1585 spin_unlock(&at->at_lock);
1589 /* Find the imp_at index for a given portal; assign if space available */
1590 int import_at_get_index(struct obd_import *imp, int portal)
1592 struct imp_at *at = &imp->imp_at;
1595 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1596 if (at->iat_portal[i] == portal)
1598 if (at->iat_portal[i] == 0)
1603 /* Not found in list, add it under a lock */
1604 spin_lock(&imp->imp_lock);
1606 /* Check unused under lock */
1607 for (; i < IMP_AT_MAX_PORTALS; i++) {
1608 if (at->iat_portal[i] == portal)
1610 if (at->iat_portal[i] == 0)
1615 /* Not enough portals? */
1616 LASSERT(i < IMP_AT_MAX_PORTALS);
1618 at->iat_portal[i] = portal;
1620 spin_unlock(&imp->imp_lock);