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/
31 * lustre/ptlrpc/import.c
33 * Author: Mike Shaver <shaver@clusterfs.com>
36 #define DEBUG_SUBSYSTEM S_RPC
38 #include <linux/fs_struct.h>
39 #include <linux/kthread.h>
40 #include <linux/delay.h>
41 #include <obd_support.h>
42 #include <lustre_ha.h>
43 #include <lustre_net.h>
44 #include <lustre_import.h>
45 #include <lustre_export.h>
47 #include <obd_cksum.h>
48 #include <obd_class.h>
50 #include "ptlrpc_internal.h"
52 struct ptlrpc_connect_async_args {
53 __u64 pcaa_peer_committed;
54 int pcaa_initial_connect;
58 * Updates import \a imp current state to provided \a state value
61 static void import_set_state_nolock(struct obd_import *imp,
62 enum lustre_imp_state state)
65 case LUSTRE_IMP_CLOSED:
67 case LUSTRE_IMP_DISCON:
68 case LUSTRE_IMP_CONNECTING:
70 case LUSTRE_IMP_REPLAY_WAIT:
71 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
74 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
78 /* A CLOSED import should remain so. */
79 if (imp->imp_state == LUSTRE_IMP_CLOSED)
82 if (imp->imp_state != LUSTRE_IMP_NEW) {
83 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n",
84 imp, obd2cli_tgt(imp->imp_obd),
85 ptlrpc_import_state_name(imp->imp_state),
86 ptlrpc_import_state_name(state));
89 imp->imp_state = state;
90 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
91 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
92 ktime_get_real_seconds();
93 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
97 static void import_set_state(struct obd_import *imp,
98 enum lustre_imp_state new_state)
100 spin_lock(&imp->imp_lock);
101 import_set_state_nolock(imp, new_state);
102 spin_unlock(&imp->imp_lock);
105 void ptlrpc_import_enter_resend(struct obd_import *imp)
107 import_set_state(imp, LUSTRE_IMP_RECOVER);
109 EXPORT_SYMBOL(ptlrpc_import_enter_resend);
112 static int ptlrpc_connect_interpret(const struct lu_env *env,
113 struct ptlrpc_request *request,
115 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
117 /* Only this function is allowed to change the import state when it is
118 * CLOSED. I would rather refcount the import and free it after
119 * disconnection like we do with exports. To do that, the client_obd
120 * will need to save the peer info somewhere other than in the import,
122 int ptlrpc_init_import(struct obd_import *imp)
124 spin_lock(&imp->imp_lock);
126 imp->imp_generation++;
127 imp->imp_state = LUSTRE_IMP_NEW;
129 spin_unlock(&imp->imp_lock);
133 EXPORT_SYMBOL(ptlrpc_init_import);
135 #define UUID_STR "_UUID"
136 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
138 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
139 ? uuid : uuid + strlen(prefix);
141 *uuid_len = strlen(*uuid_start);
143 if (*uuid_len < strlen(UUID_STR))
146 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
147 UUID_STR, strlen(UUID_STR)))
148 *uuid_len -= strlen(UUID_STR);
151 /* Must be called with imp_lock held! */
152 static void ptlrpc_deactivate_import_nolock(struct obd_import *imp)
156 assert_spin_locked(&imp->imp_lock);
157 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
158 imp->imp_invalid = 1;
159 imp->imp_generation++;
161 ptlrpc_abort_inflight(imp);
167 * Returns true if import was FULL, false if import was already not
169 * @imp - import to be disconnected
170 * @conn_cnt - connection count (epoch) of the request that timed out
171 * and caused the disconnection. In some cases, multiple
172 * inflight requests can fail to a single target (e.g. OST
173 * bulk requests) and if one has already caused a reconnection
174 * (increasing the import->conn_cnt) the older failure should
175 * not also cause a reconnection. If zero it forces a reconnect.
176 * @invalid - set import invalid flag
178 int ptlrpc_set_import_discon(struct obd_import *imp,
179 __u32 conn_cnt, bool invalid)
183 spin_lock(&imp->imp_lock);
185 if (imp->imp_state == LUSTRE_IMP_FULL &&
186 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
191 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
192 &target_start, &target_len);
194 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
195 if (imp->imp_replayable) {
196 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
197 "lost; in progress operations using this "
198 "service will wait for recovery to complete\n",
199 imp->imp_obd->obd_name, target_len, target_start,
200 obd_import_nid2str(imp));
202 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
203 "%.*s (at %s) was lost; in progress "
204 "operations using this service will fail\n",
205 imp->imp_obd->obd_name, target_len, target_start,
206 obd_import_nid2str(imp));
208 CDEBUG(D_HA, "import %s@%s for %s not "
209 "replayable, auto-deactivating\n",
210 obd2cli_tgt(imp->imp_obd),
211 imp->imp_connection->c_remote_uuid.uuid,
212 imp->imp_obd->obd_name);
213 ptlrpc_deactivate_import_nolock(imp);
217 spin_unlock(&imp->imp_lock);
219 if (obd_dump_on_timeout)
220 libcfs_debug_dumplog();
222 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
225 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
228 spin_unlock(&imp->imp_lock);
229 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
230 imp->imp_client->cli_name, imp,
231 (imp->imp_state == LUSTRE_IMP_FULL &&
232 imp->imp_conn_cnt > conn_cnt) ?
233 "reconnected" : "not connected", imp->imp_conn_cnt,
234 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
241 * This acts as a barrier; all existing requests are rejected, and
242 * no new requests will be accepted until the import is valid again.
244 void ptlrpc_deactivate_import(struct obd_import *imp)
246 spin_lock(&imp->imp_lock);
247 ptlrpc_deactivate_import_nolock(imp);
248 spin_unlock(&imp->imp_lock);
250 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
252 EXPORT_SYMBOL(ptlrpc_deactivate_import);
254 static time64_t ptlrpc_inflight_deadline(struct ptlrpc_request *req,
259 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
260 (req->rq_phase == RQ_PHASE_BULK) ||
261 (req->rq_phase == RQ_PHASE_NEW)))
264 if (req->rq_timedout)
267 if (req->rq_phase == RQ_PHASE_NEW)
270 dl = req->rq_deadline;
278 static time64_t ptlrpc_inflight_timeout(struct obd_import *imp)
280 time64_t now = ktime_get_real_seconds();
281 struct ptlrpc_request *req;
282 time64_t timeout = 0;
284 spin_lock(&imp->imp_lock);
285 list_for_each_entry(req, &imp->imp_sending_list, rq_list)
286 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
287 spin_unlock(&imp->imp_lock);
292 * This function will invalidate the import, if necessary, then block
293 * for all the RPC completions, and finally notify the obd to
294 * invalidate its state (ie cancel locks, clear pending requests,
297 void ptlrpc_invalidate_import(struct obd_import *imp)
299 struct ptlrpc_request *req;
303 atomic_inc(&imp->imp_inval_count);
305 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
306 ptlrpc_deactivate_import(imp);
308 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CONNECT_RACE)) {
309 OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
310 msleep(10 * MSEC_PER_SEC);
312 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
313 LASSERT(imp->imp_invalid);
315 /* Wait forever until inflight == 0. We really can't do it another
316 * way because in some cases we need to wait for very long reply
317 * unlink. We can't do anything before that because there is really
318 * no guarantee that some rdma transfer is not in progress right now.
321 long timeout_jiffies;
323 /* Calculate max timeout for waiting on rpcs to error
324 * out. Use obd_timeout if calculated value is smaller
327 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
328 timeout = ptlrpc_inflight_timeout(imp);
329 timeout += div_u64(timeout, 3);
332 timeout = obd_timeout;
334 /* decrease the interval to increase race condition */
338 CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
341 /* Wait for all requests to error out and call completion
342 * callbacks. Cap it at obd_timeout -- these should all
343 * have been locally cancelled by ptlrpc_abort_inflight.
345 timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
346 rc = wait_event_idle_timeout(
347 imp->imp_recovery_waitq,
348 (atomic_read(&imp->imp_inflight) == 0),
352 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
354 CERROR("%s: timeout waiting for callback (%d != 0)\n",
355 cli_tgt, atomic_read(&imp->imp_inflight));
357 spin_lock(&imp->imp_lock);
358 if (atomic_read(&imp->imp_inflight) == 0) {
359 int count = atomic_read(&imp->imp_unregistering);
361 /* We know that "unregistering" rpcs only can
362 * survive in sending or delaying lists (they
363 * maybe waiting for long reply unlink in
364 * sluggish nets). Let's check this. If there
365 * is no inflight and unregistering != 0, this
367 LASSERTF(count == 0, "Some RPCs are still "
368 "unregistering: %d\n", count);
370 /* Let's save one loop as soon as inflight have
371 * dropped to zero. No new inflights possible at
375 list_for_each_entry(req, &imp->imp_sending_list,
377 DEBUG_REQ(D_ERROR, req,
378 "still on sending list");
380 list_for_each_entry(req, &imp->imp_delayed_list,
382 DEBUG_REQ(D_ERROR, req,
383 "still on delayed list");
386 CERROR("%s: Unregistering RPCs found (%d). "
387 "Network is sluggish? Waiting for them "
388 "to error out.\n", cli_tgt,
389 atomic_read(&imp->imp_unregistering));
391 spin_unlock(&imp->imp_lock);
396 * Let's additionally check that no new rpcs added to import in
397 * "invalidate" state.
399 LASSERT(atomic_read(&imp->imp_inflight) == 0);
400 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
401 sptlrpc_import_flush_all_ctx(imp);
403 atomic_dec(&imp->imp_inval_count);
404 wake_up(&imp->imp_recovery_waitq);
406 EXPORT_SYMBOL(ptlrpc_invalidate_import);
408 /* unset imp_invalid */
409 void ptlrpc_activate_import(struct obd_import *imp, bool set_state_full)
411 struct obd_device *obd = imp->imp_obd;
413 spin_lock(&imp->imp_lock);
414 if (imp->imp_deactive != 0) {
415 LASSERT(imp->imp_state != LUSTRE_IMP_FULL);
416 if (imp->imp_state != LUSTRE_IMP_DISCON)
417 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
418 spin_unlock(&imp->imp_lock);
422 import_set_state_nolock(imp, LUSTRE_IMP_FULL);
424 imp->imp_invalid = 0;
426 spin_unlock(&imp->imp_lock);
427 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
429 EXPORT_SYMBOL(ptlrpc_activate_import);
431 void ptlrpc_pinger_force(struct obd_import *imp)
433 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
434 ptlrpc_import_state_name(imp->imp_state));
436 spin_lock(&imp->imp_lock);
437 imp->imp_force_verify = 1;
438 spin_unlock(&imp->imp_lock);
440 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
441 ptlrpc_pinger_wake_up();
443 EXPORT_SYMBOL(ptlrpc_pinger_force);
445 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
449 LASSERT(!imp->imp_dlm_fake);
451 if (ptlrpc_set_import_discon(imp, conn_cnt, true))
452 ptlrpc_pinger_force(imp);
457 int ptlrpc_reconnect_import(struct obd_import *imp)
459 #ifdef CONFIG_LUSTRE_FS_PINGER
460 long timeout_jiffies = cfs_time_seconds(obd_timeout);
463 ptlrpc_pinger_force(imp);
465 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
466 obd2cli_tgt(imp->imp_obd), obd_timeout);
468 rc = wait_event_idle_timeout(imp->imp_recovery_waitq,
469 !ptlrpc_import_in_recovery(imp),
475 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
476 ptlrpc_import_state_name(imp->imp_state));
479 ptlrpc_set_import_discon(imp, 0, false);
480 /* Force a new connect attempt */
481 ptlrpc_invalidate_import(imp);
482 /* Do a fresh connect next time by zeroing the handle */
483 ptlrpc_disconnect_import(imp, 1);
484 /* Wait for all invalidate calls to finish */
485 if (atomic_read(&imp->imp_inval_count) > 0) {
488 rc = l_wait_event_abortable(
489 imp->imp_recovery_waitq,
490 (atomic_read(&imp->imp_inval_count) == 0));
492 CERROR("Interrupted, inval=%d\n",
493 atomic_read(&imp->imp_inval_count));
496 /* Allow reconnect attempts */
497 imp->imp_obd->obd_no_recov = 0;
498 /* Remove 'invalid' flag */
499 ptlrpc_activate_import(imp, false);
500 /* Attempt a new connect */
501 ptlrpc_recover_import(imp, NULL, 0);
505 EXPORT_SYMBOL(ptlrpc_reconnect_import);
508 * Connection on import \a imp is changed to another one (if more than one is
509 * present). We typically chose connection that we have not tried to connect to
512 static int import_select_connection(struct obd_import *imp)
514 struct obd_import_conn *imp_conn = NULL, *conn;
515 struct obd_export *dlmexp;
517 int target_len, tried_all = 1;
521 spin_lock(&imp->imp_lock);
523 if (list_empty(&imp->imp_conn_list)) {
525 CERROR("%s: no connections available: rc = %d\n",
526 imp->imp_obd->obd_name, rc);
527 GOTO(out_unlock, rc);
530 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
531 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
532 imp->imp_obd->obd_name,
533 libcfs_nidstr(&conn->oic_conn->c_peer.nid),
534 conn->oic_last_attempt);
536 /* If we have not tried this connection since
537 * the last successful attempt, go with this one
539 if ((conn->oic_last_attempt == 0) ||
540 conn->oic_last_attempt <= imp->imp_last_success_conn) {
546 /* If all of the connections have already been tried
547 * since the last successful connection; just choose the
548 * least recently used
552 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
556 /* if not found, simply choose the current one */
557 if (!imp_conn || imp->imp_force_reconnect) {
558 LASSERT(imp->imp_conn_current);
559 imp_conn = imp->imp_conn_current;
562 LASSERT(imp_conn->oic_conn);
564 /* If we've tried everything, and we're back to the beginning of the
565 * list, increase our timeout and try again. It will be reset when
566 * we do finally connect. (FIXME: really we should wait for all network
567 * state associated with the last connection attempt to drain before
568 * trying to reconnect on it.)
570 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
571 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
573 if (at_get(at) < CONNECTION_SWITCH_MAX) {
574 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
575 if (at_get(at) > CONNECTION_SWITCH_MAX)
576 at_reset(at, CONNECTION_SWITCH_MAX);
578 LASSERT(imp_conn->oic_last_attempt);
580 "%s: tried all connections, increasing latency to %ds\n",
581 imp->imp_obd->obd_name, at_get(at));
584 imp_conn->oic_last_attempt = ktime_get_seconds();
586 /* switch connection, don't mind if it's same as the current one */
587 ptlrpc_connection_put(imp->imp_connection);
588 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
590 dlmexp = class_conn2export(&imp->imp_dlm_handle);
592 GOTO(out_unlock, rc = -EINVAL);
593 ptlrpc_connection_put(dlmexp->exp_connection);
594 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
595 class_export_put(dlmexp);
597 if (imp->imp_conn_current != imp_conn) {
598 if (imp->imp_conn_current) {
599 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
600 &target_start, &target_len);
602 CDEBUG(D_HA, "%s: Connection changing to"
604 imp->imp_obd->obd_name,
605 target_len, target_start,
606 libcfs_nidstr(&imp_conn->oic_conn->c_peer.nid));
609 imp->imp_conn_current = imp_conn;
612 /* The below message is checked in conf-sanity.sh test_35[ab] */
613 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
614 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
615 libcfs_nidstr(&imp_conn->oic_conn->c_peer.nid));
618 spin_unlock(&imp->imp_lock);
623 * must be called under imp_lock
625 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
627 struct ptlrpc_request *req;
629 /* The requests in committed_list always have smaller transnos than
630 * the requests in replay_list */
631 if (!list_empty(&imp->imp_committed_list)) {
632 req = list_first_entry(&imp->imp_committed_list,
633 struct ptlrpc_request, rq_replay_list);
634 *transno = req->rq_transno;
635 if (req->rq_transno == 0) {
636 DEBUG_REQ(D_ERROR, req,
637 "zero transno in committed_list");
642 if (!list_empty(&imp->imp_replay_list)) {
643 req = list_first_entry(&imp->imp_committed_list,
644 struct ptlrpc_request, rq_replay_list);
645 *transno = req->rq_transno;
646 if (req->rq_transno == 0) {
647 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
655 int ptlrpc_connect_import(struct obd_import *imp)
657 spin_lock(&imp->imp_lock);
658 return ptlrpc_connect_import_locked(imp);
662 * Attempt to (re)connect import \a imp. This includes all preparations,
663 * initializing CONNECT RPC request and passing it to ptlrpcd for
666 * Assumes imp->imp_lock is held, and releases it.
668 * Returns 0 on success or error code.
670 int ptlrpc_connect_import_locked(struct obd_import *imp)
672 struct obd_device *obd = imp->imp_obd;
673 int initial_connect = 0;
675 __u64 committed_before_reconnect = 0;
676 struct ptlrpc_request *request;
677 struct obd_connect_data ocd;
678 char *bufs[] = { NULL,
679 obd2cli_tgt(imp->imp_obd),
681 (char *)&imp->imp_dlm_handle,
684 struct ptlrpc_connect_async_args *aa;
688 assert_spin_locked(&imp->imp_lock);
690 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
691 spin_unlock(&imp->imp_lock);
692 CERROR("can't connect to a closed import\n");
694 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
695 spin_unlock(&imp->imp_lock);
696 CERROR("already connected\n");
698 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
699 imp->imp_state == LUSTRE_IMP_EVICTED ||
700 imp->imp_connected) {
701 spin_unlock(&imp->imp_lock);
702 CERROR("already connecting\n");
706 import_set_state_nolock(imp, LUSTRE_IMP_CONNECTING);
709 imp->imp_resend_replay = 0;
711 if (!lustre_handle_is_used(&imp->imp_remote_handle))
714 committed_before_reconnect = imp->imp_peer_committed_transno;
716 set_transno = ptlrpc_first_transno(imp,
717 &imp->imp_connect_data.ocd_transno);
718 spin_unlock(&imp->imp_lock);
720 rc = import_select_connection(imp);
724 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
728 /* Reset connect flags to the originally requested flags, in case
729 * the server is updated on-the-fly we will get the new features. */
730 ocd = imp->imp_connect_data;
731 ocd.ocd_connect_flags = imp->imp_connect_flags_orig;
732 ocd.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
733 /* Reset ocd_version each time so the server knows the exact versions */
734 ocd.ocd_version = LUSTRE_VERSION_CODE;
735 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
736 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
738 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
739 &obd->obd_uuid, &ocd, NULL);
743 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
745 GOTO(out, rc = -ENOMEM);
747 /* get SELinux policy info if any */
748 rc = sptlrpc_get_sepol(request);
750 ptlrpc_request_free(request);
754 bufs[5] = request->rq_sepol;
756 req_capsule_set_size(&request->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
757 strlen(request->rq_sepol) ?
758 strlen(request->rq_sepol) + 1 : 0);
760 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
761 imp->imp_connect_op, bufs, NULL);
763 ptlrpc_request_free(request);
767 /* Report the rpc service time to the server so that it knows how long
768 * to wait for clients to join recovery */
769 lustre_msg_set_service_timeout(request->rq_reqmsg,
770 at_timeout2est(request->rq_timeout));
772 /* The amount of time we give the server to process the connect req.
773 * import_select_connection will increase the net latency on
774 * repeated reconnect attempts to cover slow networks.
775 * We override/ignore the server rpc completion estimate here,
776 * which may be large if this is a reconnect attempt */
777 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
778 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
780 request->rq_no_resend = request->rq_no_delay = 1;
781 request->rq_send_state = LUSTRE_IMP_CONNECTING;
782 /* Allow a slightly larger reply for future growth compatibility */
783 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
784 sizeof(struct obd_connect_data)+16*sizeof(__u64));
785 ptlrpc_request_set_replen(request);
786 request->rq_interpret_reply = ptlrpc_connect_interpret;
788 aa = ptlrpc_req_async_args(aa, request);
789 memset(aa, 0, sizeof *aa);
791 aa->pcaa_peer_committed = committed_before_reconnect;
792 aa->pcaa_initial_connect = initial_connect;
794 if (aa->pcaa_initial_connect) {
795 spin_lock(&imp->imp_lock);
796 imp->imp_replayable = 1;
797 spin_unlock(&imp->imp_lock);
798 lustre_msg_add_op_flags(request->rq_reqmsg,
799 MSG_CONNECT_INITIAL);
803 lustre_msg_add_op_flags(request->rq_reqmsg,
804 MSG_CONNECT_TRANSNO);
806 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
807 request->rq_timeout);
808 ptlrpcd_add_req(request);
812 import_set_state(imp, LUSTRE_IMP_DISCON);
816 EXPORT_SYMBOL(ptlrpc_connect_import);
818 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
822 spin_lock(&imp->imp_lock);
823 force_verify = imp->imp_force_verify != 0;
824 spin_unlock(&imp->imp_lock);
827 ptlrpc_pinger_wake_up();
830 static int ptlrpc_busy_reconnect(int rc)
832 return (rc == -EBUSY) || (rc == -EAGAIN);
835 static int ptlrpc_connect_set_flags(struct obd_import *imp,
836 struct obd_connect_data *ocd,
837 __u64 old_connect_flags,
838 struct obd_export *exp, int init_connect)
841 struct client_obd *cli = &imp->imp_obd->u.cli;
843 spin_lock(&imp->imp_lock);
844 list_move(&imp->imp_conn_current->oic_item,
845 &imp->imp_conn_list);
846 imp->imp_last_success_conn =
847 imp->imp_conn_current->oic_last_attempt;
849 spin_unlock(&imp->imp_lock);
851 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
852 (ocd->ocd_version > LUSTRE_VERSION_CODE +
853 LUSTRE_VERSION_OFFSET_WARN ||
854 ocd->ocd_version < LUSTRE_VERSION_CODE -
855 LUSTRE_VERSION_OFFSET_WARN)) {
856 /* Sigh, some compilers do not like #ifdef in the middle
857 of macro arguments */
858 const char *older = "older than client. "
859 "Consider upgrading server";
860 const char *newer = "newer than client. "
861 "Consider upgrading client";
863 LCONSOLE_WARN("Client version (%s). Server %s version (%d.%d.%d.%d) is much %s\n",
864 LUSTRE_VERSION_STRING,
865 obd2cli_tgt(imp->imp_obd),
866 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
867 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
868 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
869 OBD_OCD_VERSION_FIX(ocd->ocd_version),
870 ocd->ocd_version > LUSTRE_VERSION_CODE ?
875 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
876 /* We sent to the server ocd_cksum_types with bits set
877 * for algorithms we understand. The server masked off
878 * the checksum types it doesn't support */
879 if ((ocd->ocd_cksum_types &
880 obd_cksum_types_supported_client()) == 0) {
881 LCONSOLE_ERROR("The negotiation of the checksum "
882 "alogrithm to use with server %s "
884 obd2cli_tgt(imp->imp_obd),
885 ocd->ocd_cksum_types,
886 obd_cksum_types_supported_client());
889 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
892 /* The server does not support OBD_CONNECT_CKSUM.
893 * Enforce ADLER for backward compatibility*/
894 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
896 cli->cl_cksum_type = obd_cksum_type_select(imp->imp_obd->obd_name,
897 cli->cl_supp_cksum_types,
898 cli->cl_preferred_cksum_type);
900 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
901 cli->cl_max_pages_per_rpc =
902 min(ocd->ocd_brw_size >> PAGE_SHIFT,
903 cli->cl_max_pages_per_rpc);
904 else if (imp->imp_connect_op == MDS_CONNECT ||
905 imp->imp_connect_op == MGS_CONNECT)
906 cli->cl_max_pages_per_rpc = 1;
908 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
909 (cli->cl_max_pages_per_rpc > 0));
911 client_adjust_max_dirty(cli);
913 /* Update client max modify RPCs in flight with value returned
915 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
916 cli->cl_max_mod_rpcs_in_flight = min(
917 cli->cl_max_mod_rpcs_in_flight,
918 ocd->ocd_maxmodrpcs);
920 cli->cl_max_mod_rpcs_in_flight = 1;
922 /* Reset ns_connect_flags only for initial connect. It might be
923 * changed in while using FS and if we reset it in reconnect
924 * this leads to losing user settings done before such as
925 * disable lru_resize, etc. */
926 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
927 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
931 ns->ns_connect_flags ^ ns->ns_orig_connect_flags;
932 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
933 "flags: %#llx\n", imp->imp_obd->obd_name,
934 ocd->ocd_connect_flags);
935 ns->ns_connect_flags = (ns->ns_connect_flags & changed_flags) |
936 (ocd->ocd_connect_flags & ~changed_flags);
937 ns->ns_orig_connect_flags = ocd->ocd_connect_flags;
940 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
941 /* We need a per-message support flag, because
942 * a. we don't know if the incoming connect reply
943 * supports AT or not (in reply_in_callback)
944 * until we unpack it.
945 * b. failovered server means export and flags are gone
946 * (in ptlrpc_send_reply).
947 * Can only be set when we know AT is supported at
949 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
951 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
953 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
959 * Add all replay requests back to unreplied list before start replay,
960 * so that we can make sure the known replied XID is always increased
961 * only even if when replaying requests.
963 static void ptlrpc_prepare_replay(struct obd_import *imp)
965 struct ptlrpc_request *req;
967 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
968 imp->imp_resend_replay)
971 /* If the server was restart during repaly, the requests may
972 * have been added to the unreplied list in former replay. */
973 spin_lock(&imp->imp_lock);
975 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
976 if (list_empty(&req->rq_unreplied_list))
977 ptlrpc_add_unreplied(req);
980 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
981 if (list_empty(&req->rq_unreplied_list))
982 ptlrpc_add_unreplied(req);
985 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
986 spin_unlock(&imp->imp_lock);
990 * interpret_reply callback for connect RPCs.
991 * Looks into returned status of connect operation and decides
992 * what to do with the import - i.e enter recovery, promote it to
993 * full state for normal operations of disconnect it due to an error.
995 static int ptlrpc_connect_interpret(const struct lu_env *env,
996 struct ptlrpc_request *request,
999 struct ptlrpc_connect_async_args *aa = data;
1000 struct obd_import *imp = request->rq_import;
1001 struct lustre_handle old_hdl;
1002 __u64 old_connect_flags;
1003 timeout_t service_timeout;
1005 struct obd_connect_data *ocd;
1006 struct obd_export *exp = NULL;
1010 spin_lock(&imp->imp_lock);
1011 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1012 imp->imp_connect_tried = 1;
1013 spin_unlock(&imp->imp_lock);
1017 imp->imp_connect_error = rc;
1019 struct ptlrpc_request *free_req;
1020 struct ptlrpc_request *tmp;
1022 /* abort all delayed requests initiated connection */
1023 list_for_each_entry_safe(free_req, tmp, &imp->imp_delayed_list,
1025 spin_lock(&free_req->rq_lock);
1026 if (free_req->rq_no_resend) {
1027 free_req->rq_err = 1;
1028 free_req->rq_status = -EIO;
1029 ptlrpc_client_wake_req(free_req);
1031 spin_unlock(&free_req->rq_lock);
1034 /* if this reconnect to busy export - not need select new target
1036 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
1037 spin_unlock(&imp->imp_lock);
1041 /* LU-7558: indicate that we are interpretting connect reply,
1042 * pltrpc_connect_import() will not try to reconnect until
1043 * interpret will finish. */
1044 imp->imp_connected = 1;
1045 spin_unlock(&imp->imp_lock);
1047 LASSERT(imp->imp_conn_current);
1049 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
1051 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
1053 /* server replied obd_connect_data is always bigger */
1054 ocd = req_capsule_server_sized_get(&request->rq_pill,
1055 &RMF_CONNECT_DATA, ret);
1058 CERROR("%s: no connect data from server\n",
1059 imp->imp_obd->obd_name);
1064 spin_lock(&imp->imp_lock);
1066 /* All imports are pingable */
1067 imp->imp_pingable = 1;
1068 imp->imp_force_reconnect = 0;
1069 imp->imp_force_verify = 0;
1071 imp->imp_connect_data = *ocd;
1073 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1074 imp->imp_obd->obd_name, ocd->ocd_instance);
1075 exp = class_conn2export(&imp->imp_dlm_handle);
1077 spin_unlock(&imp->imp_lock);
1080 /* This could happen if export is cleaned during the
1082 CERROR("%s: missing export after connect\n",
1083 imp->imp_obd->obd_name);
1084 GOTO(out, rc = -ENODEV);
1087 /* check that server granted subset of flags we asked for. */
1088 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1089 ocd->ocd_connect_flags) {
1090 CERROR("%s: Server didn't grant requested subset of flags: "
1091 "asked=%#llx granted=%#llx\n",
1092 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1093 ocd->ocd_connect_flags);
1094 GOTO(out, rc = -EPROTO);
1097 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1098 ocd->ocd_connect_flags2) {
1099 CERROR("%s: Server didn't grant requested subset of flags2: "
1100 "asked=%#llx granted=%#llx\n",
1101 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1102 ocd->ocd_connect_flags2);
1103 GOTO(out, rc = -EPROTO);
1106 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1107 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1108 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1109 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1110 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1111 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1112 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1114 /* We do not support the MDT-MDT interoperations with
1115 * different version MDT because of protocol changes. */
1116 if (unlikely(major != LUSTRE_MAJOR ||
1117 minor != LUSTRE_MINOR ||
1118 abs(patch - LUSTRE_PATCH) > 3)) {
1119 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1120 "connection to different version MDT "
1121 "(%d.%d.%d.%d) %s\n",
1122 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1123 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1124 major, minor, patch,
1125 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1126 imp->imp_connection->c_remote_uuid.uuid);
1128 GOTO(out, rc = -EPROTO);
1132 old_connect_flags = exp_connect_flags(exp);
1133 exp->exp_connect_data = *ocd;
1134 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1136 /* The net statistics after (re-)connect is not valid anymore,
1137 * because may reflect other routing, etc.
1139 service_timeout = lustre_msg_get_service_timeout(request->rq_repmsg);
1140 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1141 ptlrpc_at_adj_net_latency(request, service_timeout);
1143 /* Import flags should be updated before waking import at FULL state */
1144 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1145 aa->pcaa_initial_connect);
1146 class_export_put(exp);
1152 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1154 if (aa->pcaa_initial_connect) {
1155 spin_lock(&imp->imp_lock);
1156 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1157 imp->imp_replayable = 1;
1158 CDEBUG(D_HA, "connected to replayable target: %s\n",
1159 obd2cli_tgt(imp->imp_obd));
1161 imp->imp_replayable = 0;
1164 /* if applies, adjust the imp->imp_msg_magic here
1165 * according to reply flags
1168 imp->imp_remote_handle =
1169 *lustre_msg_get_handle(request->rq_repmsg);
1171 /* Initial connects are allowed for clients with non-random
1172 * uuids when servers are in recovery. Simply signal the
1173 * servers replay is complete and wait in REPLAY_WAIT.
1175 if (msg_flags & MSG_CONNECT_RECOVERING) {
1176 CDEBUG(D_HA, "connect to %s during recovery\n",
1177 obd2cli_tgt(imp->imp_obd));
1178 import_set_state_nolock(imp, LUSTRE_IMP_REPLAY_LOCKS);
1179 spin_unlock(&imp->imp_lock);
1181 spin_unlock(&imp->imp_lock);
1182 ptlrpc_activate_import(imp, true);
1185 GOTO(finish, rc = 0);
1188 /* Determine what recovery state to move the import to. */
1189 if (MSG_CONNECT_RECONNECT & msg_flags) {
1190 memset(&old_hdl, 0, sizeof(old_hdl));
1191 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1193 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1194 "bad handle %#llx\n",
1195 obd2cli_tgt(imp->imp_obd),
1196 imp->imp_connection->c_remote_uuid.uuid,
1197 imp->imp_dlm_handle.cookie);
1198 GOTO(out, rc = -ENOTCONN);
1201 if (memcmp(&imp->imp_remote_handle,
1202 lustre_msg_get_handle(request->rq_repmsg),
1203 sizeof(imp->imp_remote_handle))) {
1204 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1207 /* Bug 16611/14775: if server handle have changed,
1208 * that means some sort of disconnection happened.
1209 * If the server is not in recovery, that also means it
1210 * already erased all of our state because of previous
1211 * eviction. If it is in recovery - we are safe to
1212 * participate since we can reestablish all of our state
1215 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1217 "%s@%s changed server handle from "
1219 " but is still in recovery\n",
1220 obd2cli_tgt(imp->imp_obd),
1221 imp->imp_connection->c_remote_uuid.uuid,
1222 imp->imp_remote_handle.cookie,
1223 lustre_msg_get_handle(
1224 request->rq_repmsg)->cookie);
1226 LCONSOLE_WARN("Evicted from %s (at %s) "
1227 "after server handle changed from "
1229 obd2cli_tgt(imp->imp_obd),
1230 imp->imp_connection->
1232 imp->imp_remote_handle.cookie,
1233 lustre_msg_get_handle(
1234 request->rq_repmsg)->cookie);
1237 imp->imp_remote_handle =
1238 *lustre_msg_get_handle(request->rq_repmsg);
1240 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1241 import_set_state(imp, LUSTRE_IMP_EVICTED);
1242 GOTO(finish, rc = 0);
1245 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1246 obd2cli_tgt(imp->imp_obd),
1247 imp->imp_connection->c_remote_uuid.uuid);
1250 if (imp->imp_invalid) {
1251 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1252 "marking evicted\n", imp->imp_obd->obd_name);
1253 import_set_state(imp, LUSTRE_IMP_EVICTED);
1254 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1255 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1256 imp->imp_obd->obd_name,
1257 obd2cli_tgt(imp->imp_obd));
1259 spin_lock(&imp->imp_lock);
1260 imp->imp_resend_replay = 1;
1261 spin_unlock(&imp->imp_lock);
1263 import_set_state(imp, imp->imp_replay_state);
1265 import_set_state(imp, LUSTRE_IMP_RECOVER);
1267 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1268 LASSERT(imp->imp_replayable);
1269 imp->imp_remote_handle =
1270 *lustre_msg_get_handle(request->rq_repmsg);
1271 imp->imp_last_replay_transno = 0;
1272 imp->imp_replay_cursor = &imp->imp_committed_list;
1273 import_set_state(imp, LUSTRE_IMP_REPLAY);
1274 } else if ((ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0 &&
1275 !imp->imp_invalid) {
1277 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
1278 /* The below message is checked in recovery-small.sh test_106 */
1279 DEBUG_REQ(D_HA, request, "%s: lwp recover",
1280 imp->imp_obd->obd_name);
1281 imp->imp_remote_handle =
1282 *lustre_msg_get_handle(request->rq_repmsg);
1283 import_set_state(imp, LUSTRE_IMP_RECOVER);
1285 DEBUG_REQ(D_HA, request,
1286 "%s: evicting (reconnect/recover flags not set: %x)",
1287 imp->imp_obd->obd_name, msg_flags);
1288 imp->imp_remote_handle =
1289 *lustre_msg_get_handle(request->rq_repmsg);
1290 import_set_state(imp, LUSTRE_IMP_EVICTED);
1293 /* Sanity checks for a reconnected import. */
1294 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE))
1295 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
1297 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1298 lustre_msg_get_last_committed(request->rq_repmsg) <
1299 aa->pcaa_peer_committed) {
1300 static bool printed;
1302 /* The below message is checked in recovery-small.sh test_54 */
1303 CERROR("%s: went back in time (transno %lld was previously committed, server now claims %lld)!\n",
1304 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1305 lustre_msg_get_last_committed(request->rq_repmsg));
1307 CERROR("For further information, see http://doc.lustre.org/lustre_manual.xhtml#went_back_in_time\n");
1313 ptlrpc_prepare_replay(imp);
1314 rc = ptlrpc_import_recovery_state_machine(imp);
1315 if (rc == -ENOTCONN) {
1317 "evicted/aborted by %s@%s during recovery; invalidating and reconnecting\n",
1318 obd2cli_tgt(imp->imp_obd),
1319 imp->imp_connection->c_remote_uuid.uuid);
1320 ptlrpc_connect_import(imp);
1321 spin_lock(&imp->imp_lock);
1322 imp->imp_connected = 0;
1323 imp->imp_connect_tried = 1;
1324 spin_unlock(&imp->imp_lock);
1330 class_export_put(exp);
1332 spin_lock(&imp->imp_lock);
1333 imp->imp_connected = 0;
1334 imp->imp_connect_tried = 1;
1338 time64_t now = ktime_get_seconds();
1339 time64_t next_connect;
1341 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1342 if (rc == -EACCES || rc == -EROFS) {
1344 * Give up trying to reconnect
1345 * EACCES means client has no permission for connection
1346 * EROFS means client must mount read-only
1348 imp->imp_obd->obd_no_recov = 1;
1349 ptlrpc_deactivate_import_nolock(imp);
1351 } else if (rc == -EPROTO) {
1352 struct obd_connect_data *ocd;
1354 /* reply message might not be ready */
1355 if (request->rq_repmsg == NULL) {
1356 spin_unlock(&imp->imp_lock);
1360 ocd = req_capsule_server_get(&request->rq_pill,
1362 /* Servers are not supposed to refuse connections from
1363 * clients based on version, only connection feature
1364 * flags. We should never see this from llite, but it
1365 * may be useful for debugging in the future. */
1367 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1368 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1369 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1371 " refused connection from this client "
1372 "with an incompatible version (%s). "
1373 "Client must be recompiled\n",
1374 obd2cli_tgt(imp->imp_obd),
1375 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1376 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1377 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1378 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1379 LUSTRE_VERSION_STRING);
1380 ptlrpc_deactivate_import_nolock(imp);
1381 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1384 } else if (rc == -ENODEV || rc == -ETIMEDOUT) {
1385 /* ENODEV means there is no service, force reconnection
1386 * to a pair if attempt happen ptlrpc_next_reconnect
1387 * before now. ETIMEDOUT could be set during network
1388 * error and do not guarantee request deadline happened.
1390 struct obd_import_conn *conn;
1391 time64_t reconnect_time;
1393 /* Same as ptlrpc_next_reconnect, but in past */
1394 reconnect_time = now - INITIAL_CONNECT_TIMEOUT;
1395 list_for_each_entry(conn, &imp->imp_conn_list,
1397 if (conn->oic_last_attempt <= reconnect_time) {
1398 imp->imp_force_verify = 1;
1404 next_connect = imp->imp_conn_current->oic_last_attempt +
1405 (request->rq_deadline - request->rq_sent);
1406 spin_unlock(&imp->imp_lock);
1409 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
1414 /* adjust imp_next_ping to request deadline + 1 and reschedule
1415 * a pinger if import lost processing during CONNECTING or far
1416 * away from request deadline. It could happen when connection
1417 * was initiated outside of pinger, like
1418 * ptlrpc_set_import_discon().
1420 if (!imp->imp_force_verify && (imp->imp_next_ping <= now ||
1421 imp->imp_next_ping > next_connect)) {
1422 imp->imp_next_ping = max(now, next_connect) + 1;
1423 ptlrpc_pinger_wake_up();
1426 ptlrpc_maybe_ping_import_soon(imp);
1428 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1429 obd2cli_tgt(imp->imp_obd),
1430 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1432 spin_unlock(&imp->imp_lock);
1435 wake_up(&imp->imp_recovery_waitq);
1440 * interpret callback for "completed replay" RPCs.
1441 * \see signal_completed_replay
1443 static int completed_replay_interpret(const struct lu_env *env,
1444 struct ptlrpc_request *req,
1448 atomic_dec(&req->rq_import->imp_replay_inflight);
1449 if (req->rq_status == 0 && !req->rq_import->imp_vbr_failed) {
1450 ptlrpc_import_recovery_state_machine(req->rq_import);
1452 if (req->rq_import->imp_vbr_failed) {
1454 "%s: version recovery fails, reconnecting\n",
1455 req->rq_import->imp_obd->obd_name);
1457 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1459 req->rq_import->imp_obd->obd_name,
1462 ptlrpc_connect_import(req->rq_import);
1469 * Let server know that we have no requests to replay anymore.
1470 * Achieved by just sending a PING request
1472 static int signal_completed_replay(struct obd_import *imp)
1474 struct ptlrpc_request *req;
1477 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1480 if (!atomic_add_unless(&imp->imp_replay_inflight, 1, 1))
1483 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1486 atomic_dec(&imp->imp_replay_inflight);
1490 ptlrpc_request_set_replen(req);
1491 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1492 lustre_msg_add_flags(req->rq_reqmsg,
1493 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1495 req->rq_timeout *= 3;
1496 req->rq_interpret_reply = completed_replay_interpret;
1498 ptlrpcd_add_req(req);
1503 * In kernel code all import invalidation happens in its own
1504 * separate thread, so that whatever application happened to encounter
1505 * a problem could still be killed or otherwise continue
1507 static int ptlrpc_invalidate_import_thread(void *data)
1509 struct obd_import *imp = data;
1512 unshare_fs_struct();
1513 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1514 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1515 imp->imp_connection->c_remote_uuid.uuid);
1517 if (do_dump_on_eviction(imp->imp_obd)) {
1518 CERROR("dump the log upon eviction\n");
1519 libcfs_debug_dumplog();
1522 ptlrpc_invalidate_import(imp);
1523 import_set_state(imp, LUSTRE_IMP_RECOVER);
1524 ptlrpc_import_recovery_state_machine(imp);
1526 class_import_put(imp);
1531 * This is the state machine for client-side recovery on import.
1533 * Typicaly we have two possibly paths. If we came to server and it is not
1534 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1535 * state and reconnect from scratch.
1536 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1537 * We go through our list of requests to replay and send them to server one by
1539 * After sending all request from the list we change import state to
1540 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1541 * and also all the locks we don't yet have and wait for server to grant us.
1542 * After that we send a special "replay completed" request and change import
1543 * state to IMP_REPLAY_WAIT.
1544 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1545 * state and resend all requests from sending list.
1546 * After that we promote import to FULL state and send all delayed requests
1547 * and import is fully operational after that.
1550 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1558 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1559 struct task_struct *task;
1562 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1563 &target_start, &target_len);
1564 connect_flags = imp->imp_connect_data.ocd_connect_flags;
1565 /* Don't care about MGC eviction */
1566 if (strcmp(imp->imp_obd->obd_type->typ_name,
1567 LUSTRE_MGC_NAME) != 0 &&
1568 (connect_flags & OBD_CONNECT_LIGHTWEIGHT) == 0) {
1569 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1570 "by %.*s; in progress operations "
1571 "using this service will fail.\n",
1572 imp->imp_obd->obd_name, target_len,
1574 LASSERTF(!obd_lbug_on_eviction, "LBUG upon eviction\n");
1576 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1577 obd2cli_tgt(imp->imp_obd),
1578 imp->imp_connection->c_remote_uuid.uuid);
1579 /* reset vbr_failed flag upon eviction */
1580 spin_lock(&imp->imp_lock);
1581 imp->imp_vbr_failed = 0;
1582 spin_unlock(&imp->imp_lock);
1584 /* bug 17802: XXX client_disconnect_export vs connect request
1585 * race. if client is evicted at this time then we start
1586 * invalidate thread without reference to import and import can
1587 * be freed at same time. */
1588 class_import_get(imp);
1589 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1592 class_import_put(imp);
1594 CERROR("%s: can't start invalidate thread: rc = %d\n",
1595 imp->imp_obd->obd_name, rc);
1602 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1603 CDEBUG(D_HA, "replay requested by %s\n",
1604 obd2cli_tgt(imp->imp_obd));
1605 rc = ptlrpc_replay_next(imp, &inflight);
1606 if (inflight == 0 &&
1607 atomic_read(&imp->imp_replay_inflight) == 0) {
1608 import_set_state(imp, LUSTRE_IMP_REPLAY_LOCKS);
1609 rc = ldlm_replay_locks(imp);
1616 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1617 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1618 import_set_state(imp, LUSTRE_IMP_REPLAY_WAIT);
1619 rc = signal_completed_replay(imp);
1625 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1626 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1627 import_set_state(imp, LUSTRE_IMP_RECOVER);
1631 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1632 struct ptlrpc_connection *conn = imp->imp_connection;
1634 rc = ptlrpc_resend(imp);
1637 ptlrpc_activate_import(imp, true);
1639 /* Reverse import are flagged with dlm_fake == 1.
1640 * They do not do recovery and connection are not "restored".
1642 if (!imp->imp_dlm_fake)
1643 CDEBUG_LIMIT(imp->imp_was_idle ?
1644 imp->imp_idle_debug : D_CONSOLE,
1645 "%s: Connection restored to %s (at %s)\n",
1646 imp->imp_obd->obd_name,
1647 obd_uuid2str(&conn->c_remote_uuid),
1648 obd_import_nid2str(imp));
1649 spin_lock(&imp->imp_lock);
1650 imp->imp_was_idle = 0;
1651 spin_unlock(&imp->imp_lock);
1654 if (imp->imp_state == LUSTRE_IMP_FULL) {
1655 wake_up(&imp->imp_recovery_waitq);
1656 ptlrpc_wake_delayed(imp);
1663 static struct ptlrpc_request *ptlrpc_disconnect_prep_req(struct obd_import *imp)
1665 struct ptlrpc_request *req;
1669 switch (imp->imp_connect_op) {
1671 rq_opc = OST_DISCONNECT;
1674 rq_opc = MDS_DISCONNECT;
1677 rq_opc = MGS_DISCONNECT;
1681 CERROR("%s: don't know how to disconnect from %s "
1682 "(connect_op %d): rc = %d\n",
1683 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1684 imp->imp_connect_op, rc);
1685 RETURN(ERR_PTR(rc));
1688 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1689 LUSTRE_OBD_VERSION, rq_opc);
1691 RETURN(ERR_PTR(-ENOMEM));
1693 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1694 * it fails. We can get through the above with a down server
1695 * if the client doesn't know the server is gone yet. */
1696 req->rq_no_resend = 1;
1698 /* We want client umounts to happen quickly, no matter the
1700 req->rq_timeout = min_t(timeout_t, req->rq_timeout,
1701 INITIAL_CONNECT_TIMEOUT);
1703 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1704 ptlrpc_request_set_replen(req);
1709 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1711 struct ptlrpc_request *req;
1715 if (imp->imp_obd->obd_force)
1716 GOTO(set_state, rc);
1718 /* probably the import has been disconnected already being idle */
1719 spin_lock(&imp->imp_lock);
1720 if (imp->imp_state == LUSTRE_IMP_IDLE)
1722 spin_unlock(&imp->imp_lock);
1724 if (ptlrpc_import_in_recovery(imp)) {
1725 long timeout_jiffies;
1729 if (imp->imp_server_timeout)
1730 timeout = obd_timeout >> 1;
1732 timeout = obd_timeout;
1737 req_portal = imp->imp_client->cli_request_portal;
1738 idx = import_at_get_index(imp, req_portal);
1739 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1742 timeout_jiffies = cfs_time_seconds(timeout);
1743 if (wait_event_idle_timeout(imp->imp_recovery_waitq,
1744 !ptlrpc_import_in_recovery(imp),
1745 timeout_jiffies) == 0 &&
1746 l_wait_event_abortable(imp->imp_recovery_waitq,
1747 !ptlrpc_import_in_recovery(imp)) < 0)
1751 req = ptlrpc_disconnect_prep_req(imp);
1753 GOTO(set_state, rc = PTR_ERR(req));
1755 spin_lock(&imp->imp_lock);
1756 if (imp->imp_state != LUSTRE_IMP_FULL) {
1757 ptlrpc_req_finished_with_imp_lock(req);
1760 import_set_state_nolock(imp, LUSTRE_IMP_CONNECTING);
1761 spin_unlock(&imp->imp_lock);
1763 rc = ptlrpc_queue_wait(req);
1764 ptlrpc_req_finished(req);
1767 spin_lock(&imp->imp_lock);
1770 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1772 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1773 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1774 spin_unlock(&imp->imp_lock);
1776 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
1778 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
1780 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1784 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1786 static void ptlrpc_reset_reqs_generation(struct obd_import *imp)
1788 struct ptlrpc_request *old, *tmp;
1790 /* tag all resendable requests generated before disconnection
1791 * notice this code is part of disconnect-at-idle path only */
1792 list_for_each_entry_safe(old, tmp, &imp->imp_delayed_list,
1794 spin_lock(&old->rq_lock);
1795 if (old->rq_import_generation == imp->imp_generation - 1 &&
1796 ((imp->imp_initiated_at == imp->imp_generation) ||
1797 !old->rq_no_resend))
1798 old->rq_import_generation = imp->imp_generation;
1799 spin_unlock(&old->rq_lock);
1803 static int ptlrpc_disconnect_idle_interpret(const struct lu_env *env,
1804 struct ptlrpc_request *req,
1807 struct obd_import *imp = req->rq_import;
1810 DEBUG_REQ(D_HA, req, "inflight=%d, refcount=%d: rc = %d",
1811 atomic_read(&imp->imp_inflight),
1812 refcount_read(&imp->imp_refcount), rc);
1814 spin_lock(&imp->imp_lock);
1815 /* DISCONNECT reply can be late and another connection can just
1816 * be initiated. so we have to abort disconnection. */
1817 if (req->rq_import_generation == imp->imp_generation &&
1818 imp->imp_state != LUSTRE_IMP_CLOSED) {
1819 LASSERTF(imp->imp_state == LUSTRE_IMP_CONNECTING,
1820 "%s\n", ptlrpc_import_state_name(imp->imp_state));
1821 memset(&imp->imp_remote_handle, 0,
1822 sizeof(imp->imp_remote_handle));
1823 /* take our DISCONNECT into account */
1824 if (atomic_read(&imp->imp_reqs) > 1) {
1825 imp->imp_generation++;
1826 imp->imp_initiated_at = imp->imp_generation;
1827 import_set_state_nolock(imp, LUSTRE_IMP_NEW);
1828 ptlrpc_reset_reqs_generation(imp);
1831 /* do not expose transient IDLE state */
1832 import_set_state_nolock(imp, LUSTRE_IMP_IDLE);
1837 rc = ptlrpc_connect_import_locked(imp);
1839 ptlrpc_pinger_add_import(imp);
1841 spin_unlock(&imp->imp_lock);
1847 static bool ptlrpc_can_idle(struct obd_import *imp)
1849 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1851 /* one request for disconnect rpc */
1852 if (atomic_read(&imp->imp_reqs) > 1)
1855 /* any lock increases ns_bref being a resource holder */
1856 if (ns && atomic_read(&ns->ns_bref) > 0)
1862 int ptlrpc_disconnect_and_idle_import(struct obd_import *imp)
1864 struct ptlrpc_request *req;
1867 if (imp->imp_obd->obd_force)
1870 if (ptlrpc_import_in_recovery(imp))
1873 req = ptlrpc_disconnect_prep_req(imp);
1875 RETURN(PTR_ERR(req));
1877 req->rq_interpret_reply = ptlrpc_disconnect_idle_interpret;
1879 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_IDLE_RACE)) {
1882 server_name2index(imp->imp_obd->obd_name, &idx, NULL);
1884 OBD_RACE(OBD_FAIL_PTLRPC_IDLE_RACE);
1887 spin_lock(&imp->imp_lock);
1888 if (imp->imp_state != LUSTRE_IMP_FULL || !ptlrpc_can_idle(imp)) {
1889 ptlrpc_req_finished_with_imp_lock(req);
1890 spin_unlock(&imp->imp_lock);
1893 import_set_state_nolock(imp, LUSTRE_IMP_CONNECTING);
1894 /* don't make noise at reconnection */
1895 imp->imp_was_idle = 1;
1896 spin_unlock(&imp->imp_lock);
1898 CDEBUG_LIMIT(imp->imp_idle_debug, "%s: disconnect after %llus idle\n",
1899 imp->imp_obd->obd_name,
1900 ktime_get_real_seconds() - imp->imp_last_reply_time);
1902 ptlrpcd_add_req(req);
1906 EXPORT_SYMBOL(ptlrpc_disconnect_and_idle_import);
1908 void ptlrpc_cleanup_imp(struct obd_import *imp)
1912 spin_lock(&imp->imp_lock);
1914 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1915 imp->imp_generation++;
1916 ptlrpc_abort_inflight(imp);
1918 spin_unlock(&imp->imp_lock);
1923 /* Adaptive Timeout utils */
1925 /* Update at_current_timeout with the specified value (bounded by at_min and
1926 * at_max), as well as the AT history "bins".
1927 * - Bin into timeslices using AT_BINS bins.
1928 * - This gives us a max of the last at_history seconds without the storage,
1929 * but still smoothing out a return to normalcy from a slow response.
1930 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1932 timeout_t at_measured(struct adaptive_timeout *at, timeout_t timeout)
1934 timeout_t old_timeout = at->at_current_timeout;
1935 time64_t now = ktime_get_real_seconds();
1936 long binlimit = max_t(long, at_history / AT_BINS, 1);
1939 CDEBUG(D_OTHER, "add %u to %p time=%lld v=%u (%u %u %u %u)\n",
1940 timeout, at, now - at->at_binstart, at->at_current_timeout,
1941 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1944 /* Negative timeouts and 0's don't count, because we never
1945 * want our timeout to drop to 0 or below, and because 0 could
1950 spin_lock(&at->at_lock);
1952 if (unlikely(at->at_binstart == 0)) {
1953 /* Special case to remove default from history */
1954 at->at_current_timeout = timeout;
1955 at->at_worst_timeout_ever = timeout;
1956 at->at_worst_timestamp = now;
1957 at->at_hist[0] = timeout;
1958 at->at_binstart = now;
1959 } else if (now - at->at_binstart < binlimit ) {
1961 at->at_hist[0] = max_t(timeout_t, timeout, at->at_hist[0]);
1962 at->at_current_timeout = max_t(timeout_t, timeout,
1963 at->at_current_timeout);
1966 timeout_t maxv = timeout;
1968 /* move bins over */
1969 shift = (u32)(now - at->at_binstart) / binlimit;
1971 for(i = AT_BINS - 1; i >= 0; i--) {
1973 at->at_hist[i] = at->at_hist[i - shift];
1974 maxv = max_t(timeout_t, maxv, at->at_hist[i]);
1979 at->at_hist[0] = timeout;
1980 at->at_current_timeout = maxv;
1981 at->at_binstart += shift * binlimit;
1984 if (at->at_current_timeout > at->at_worst_timeout_ever) {
1985 at->at_worst_timeout_ever = at->at_current_timeout;
1986 at->at_worst_timestamp = now;
1989 if (at->at_flags & AT_FLG_NOHIST)
1990 /* Only keep last reported val; keeping the rest of the history
1993 at->at_current_timeout = timeout;
1996 at->at_current_timeout = min_t(timeout_t,
1997 at->at_current_timeout, at_max);
1998 at->at_current_timeout = max_t(timeout_t, at->at_current_timeout,
2000 if (at->at_current_timeout != old_timeout)
2002 "AT %p change: old=%u new=%u delta=%d (val=%d) hist %u %u %u %u\n",
2003 at, old_timeout, at->at_current_timeout,
2004 at->at_current_timeout - old_timeout, timeout,
2005 at->at_hist[0], at->at_hist[1], at->at_hist[2],
2008 /* if we changed, report the old timeout value */
2009 old_timeout = (at->at_current_timeout != old_timeout) ? old_timeout : 0;
2011 spin_unlock(&at->at_lock);
2015 /* Find the imp_at index for a given portal; assign if space available */
2016 int import_at_get_index(struct obd_import *imp, int portal)
2018 struct imp_at *at = &imp->imp_at;
2021 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
2022 if (at->iat_portal[i] == portal)
2024 if (at->iat_portal[i] == 0)
2029 /* Not found in list, add it under a lock */
2030 spin_lock(&imp->imp_lock);
2032 /* Check unused under lock */
2033 for (; i < IMP_AT_MAX_PORTALS; i++) {
2034 if (at->iat_portal[i] == portal)
2036 if (at->iat_portal[i] == 0)
2041 /* Not enough portals? */
2042 LASSERT(i < IMP_AT_MAX_PORTALS);
2044 at->iat_portal[i] = portal;
2046 spin_unlock(&imp->imp_lock);