4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/import.c
34 * Author: Mike Shaver <shaver@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_RPC
39 #include <linux/kthread.h>
40 #include <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 (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 list_head *tmp, *n;
282 struct ptlrpc_request *req;
283 time64_t timeout = 0;
285 spin_lock(&imp->imp_lock);
286 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
287 req = list_entry(tmp, struct ptlrpc_request, rq_list);
288 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
290 spin_unlock(&imp->imp_lock);
295 * This function will invalidate the import, if necessary, then block
296 * for all the RPC completions, and finally notify the obd to
297 * invalidate its state (ie cancel locks, clear pending requests,
300 void ptlrpc_invalidate_import(struct obd_import *imp)
302 struct list_head *tmp, *n;
303 struct ptlrpc_request *req;
304 struct l_wait_info lwi;
308 atomic_inc(&imp->imp_inval_count);
310 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
311 ptlrpc_deactivate_import(imp);
313 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CONNECT_RACE)) {
314 OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
315 msleep(10 * MSEC_PER_SEC);
317 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
318 LASSERT(imp->imp_invalid);
320 /* Wait forever until inflight == 0. We really can't do it another
321 * way because in some cases we need to wait for very long reply
322 * unlink. We can't do anything before that because there is really
323 * no guarantee that some rdma transfer is not in progress right now. */
325 long timeout_jiffies;
327 /* Calculate max timeout for waiting on rpcs to error
328 * out. Use obd_timeout if calculated value is smaller
331 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
332 timeout = ptlrpc_inflight_timeout(imp);
333 timeout += div_u64(timeout, 3);
336 timeout = obd_timeout;
338 /* decrease the interval to increase race condition */
342 CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
345 /* Wait for all requests to error out and call completion
346 * callbacks. Cap it at obd_timeout -- these should all
347 * have been locally cancelled by ptlrpc_abort_inflight.
349 timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
350 lwi = LWI_TIMEOUT_INTERVAL(timeout_jiffies,
351 (timeout > 1) ? cfs_time_seconds(1) :
352 cfs_time_seconds(1) / 2,
354 rc = l_wait_event(imp->imp_recovery_waitq,
355 (atomic_read(&imp->imp_inflight) == 0),
358 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
360 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
361 cli_tgt, rc, atomic_read(&imp->imp_inflight));
363 spin_lock(&imp->imp_lock);
364 if (atomic_read(&imp->imp_inflight) == 0) {
365 int count = atomic_read(&imp->imp_unregistering);
367 /* We know that "unregistering" rpcs only can
368 * survive in sending or delaying lists (they
369 * maybe waiting for long reply unlink in
370 * sluggish nets). Let's check this. If there
371 * is no inflight and unregistering != 0, this
373 LASSERTF(count == 0, "Some RPCs are still "
374 "unregistering: %d\n", count);
376 /* Let's save one loop as soon as inflight have
377 * dropped to zero. No new inflights possible at
381 list_for_each_safe(tmp, n,
382 &imp->imp_sending_list) {
383 req = list_entry(tmp,
384 struct ptlrpc_request,
386 DEBUG_REQ(D_ERROR, req,
387 "still on sending list");
389 list_for_each_safe(tmp, n,
390 &imp->imp_delayed_list) {
391 req = list_entry(tmp,
392 struct ptlrpc_request,
394 DEBUG_REQ(D_ERROR, req,
395 "still on delayed list");
398 CERROR("%s: Unregistering RPCs found (%d). "
399 "Network is sluggish? Waiting them "
400 "to error out.\n", cli_tgt,
401 atomic_read(&imp->imp_unregistering));
403 spin_unlock(&imp->imp_lock);
408 * Let's additionally check that no new rpcs added to import in
409 * "invalidate" state.
411 LASSERT(atomic_read(&imp->imp_inflight) == 0);
412 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
413 sptlrpc_import_flush_all_ctx(imp);
415 atomic_dec(&imp->imp_inval_count);
416 wake_up_all(&imp->imp_recovery_waitq);
418 EXPORT_SYMBOL(ptlrpc_invalidate_import);
420 /* unset imp_invalid */
421 void ptlrpc_activate_import(struct obd_import *imp, bool set_state_full)
423 struct obd_device *obd = imp->imp_obd;
425 spin_lock(&imp->imp_lock);
426 if (imp->imp_deactive != 0) {
427 LASSERT(imp->imp_state != LUSTRE_IMP_FULL);
428 if (imp->imp_state != LUSTRE_IMP_DISCON)
429 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
430 spin_unlock(&imp->imp_lock);
434 import_set_state_nolock(imp, LUSTRE_IMP_FULL);
436 imp->imp_invalid = 0;
438 spin_unlock(&imp->imp_lock);
439 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
441 EXPORT_SYMBOL(ptlrpc_activate_import);
443 void ptlrpc_pinger_force(struct obd_import *imp)
445 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
446 ptlrpc_import_state_name(imp->imp_state));
448 spin_lock(&imp->imp_lock);
449 imp->imp_force_verify = 1;
450 spin_unlock(&imp->imp_lock);
452 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
453 ptlrpc_pinger_wake_up();
455 EXPORT_SYMBOL(ptlrpc_pinger_force);
457 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
461 LASSERT(!imp->imp_dlm_fake);
463 if (ptlrpc_set_import_discon(imp, conn_cnt, true))
464 ptlrpc_pinger_force(imp);
469 int ptlrpc_reconnect_import(struct obd_import *imp)
472 long timeout_jiffies = cfs_time_seconds(obd_timeout);
473 struct l_wait_info lwi;
476 ptlrpc_pinger_force(imp);
478 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
479 obd2cli_tgt(imp->imp_obd), obd_timeout);
481 lwi = LWI_TIMEOUT(timeout_jiffies, NULL, NULL);
482 rc = l_wait_event(imp->imp_recovery_waitq,
483 !ptlrpc_import_in_recovery(imp), &lwi);
484 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
485 ptlrpc_import_state_name(imp->imp_state));
488 ptlrpc_set_import_discon(imp, 0, false);
489 /* Force a new connect attempt */
490 ptlrpc_invalidate_import(imp);
491 /* Do a fresh connect next time by zeroing the handle */
492 ptlrpc_disconnect_import(imp, 1);
493 /* Wait for all invalidate calls to finish */
494 if (atomic_read(&imp->imp_inval_count) > 0) {
495 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
498 rc = l_wait_event(imp->imp_recovery_waitq,
499 (atomic_read(&imp->imp_inval_count) == 0),
502 CERROR("Interrupted, inval=%d\n",
503 atomic_read(&imp->imp_inval_count));
506 /* Allow reconnect attempts */
507 imp->imp_obd->obd_no_recov = 0;
508 /* Remove 'invalid' flag */
509 ptlrpc_activate_import(imp, false);
510 /* Attempt a new connect */
511 ptlrpc_recover_import(imp, NULL, 0);
515 EXPORT_SYMBOL(ptlrpc_reconnect_import);
518 * Connection on import \a imp is changed to another one (if more than one is
519 * present). We typically chose connection that we have not tried to connect to
522 static int import_select_connection(struct obd_import *imp)
524 struct obd_import_conn *imp_conn = NULL, *conn;
525 struct obd_export *dlmexp;
527 int target_len, tried_all = 1;
530 spin_lock(&imp->imp_lock);
532 if (list_empty(&imp->imp_conn_list)) {
533 CERROR("%s: no connections available\n",
534 imp->imp_obd->obd_name);
535 spin_unlock(&imp->imp_lock);
539 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
540 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
541 imp->imp_obd->obd_name,
542 libcfs_nid2str(conn->oic_conn->c_peer.nid),
543 conn->oic_last_attempt);
545 /* If we have not tried this connection since
546 the last successful attempt, go with this one */
547 if ((conn->oic_last_attempt == 0) ||
548 conn->oic_last_attempt <= imp->imp_last_success_conn) {
554 /* If all of the connections have already been tried
555 since the last successful connection; just choose the
556 least recently used */
559 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
563 /* if not found, simply choose the current one */
564 if (!imp_conn || imp->imp_force_reconnect) {
565 LASSERT(imp->imp_conn_current);
566 imp_conn = imp->imp_conn_current;
569 LASSERT(imp_conn->oic_conn);
571 /* If we've tried everything, and we're back to the beginning of the
572 list, increase our timeout and try again. It will be reset when
573 we do finally connect. (FIXME: really we should wait for all network
574 state associated with the last connection attempt to drain before
575 trying to reconnect on it.) */
576 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
577 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
578 if (at_get(at) < CONNECTION_SWITCH_MAX) {
579 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
580 if (at_get(at) > CONNECTION_SWITCH_MAX)
581 at_reset(at, CONNECTION_SWITCH_MAX);
583 LASSERT(imp_conn->oic_last_attempt);
584 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
585 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
588 imp_conn->oic_last_attempt = ktime_get_seconds();
590 /* switch connection, don't mind if it's same as the current one */
591 if (imp->imp_connection)
592 ptlrpc_connection_put(imp->imp_connection);
593 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
595 dlmexp = class_conn2export(&imp->imp_dlm_handle);
596 LASSERT(dlmexp != NULL);
597 if (dlmexp->exp_connection)
598 ptlrpc_connection_put(dlmexp->exp_connection);
599 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
600 class_export_put(dlmexp);
602 if (imp->imp_conn_current != imp_conn) {
603 if (imp->imp_conn_current) {
604 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
605 &target_start, &target_len);
607 CDEBUG(D_HA, "%s: Connection changing to"
609 imp->imp_obd->obd_name,
610 target_len, target_start,
611 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
614 imp->imp_conn_current = imp_conn;
617 /* The below message is checked in conf-sanity.sh test_35[ab] */
618 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
619 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
620 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
622 spin_unlock(&imp->imp_lock);
628 * must be called under imp_lock
630 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
632 struct ptlrpc_request *req;
633 struct list_head *tmp;
635 /* The requests in committed_list always have smaller transnos than
636 * the requests in replay_list */
637 if (!list_empty(&imp->imp_committed_list)) {
638 tmp = imp->imp_committed_list.next;
639 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
640 *transno = req->rq_transno;
641 if (req->rq_transno == 0) {
642 DEBUG_REQ(D_ERROR, req,
643 "zero transno in committed_list");
648 if (!list_empty(&imp->imp_replay_list)) {
649 tmp = imp->imp_replay_list.next;
650 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
651 *transno = req->rq_transno;
652 if (req->rq_transno == 0) {
653 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
662 * Attempt to (re)connect import \a imp. This includes all preparations,
663 * initializing CONNECT RPC request and passing it to ptlrpcd for
665 * Returns 0 on success or error code.
667 int ptlrpc_connect_import(struct obd_import *imp)
669 struct obd_device *obd = imp->imp_obd;
670 int initial_connect = 0;
672 __u64 committed_before_reconnect = 0;
673 struct ptlrpc_request *request;
674 struct obd_connect_data ocd;
675 char *bufs[] = { NULL,
676 obd2cli_tgt(imp->imp_obd),
678 (char *)&imp->imp_dlm_handle,
681 struct ptlrpc_connect_async_args *aa;
685 spin_lock(&imp->imp_lock);
686 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
687 spin_unlock(&imp->imp_lock);
688 CERROR("can't connect to a closed import\n");
690 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
691 spin_unlock(&imp->imp_lock);
692 CERROR("already connected\n");
694 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
695 imp->imp_state == LUSTRE_IMP_EVICTED ||
696 imp->imp_connected) {
697 spin_unlock(&imp->imp_lock);
698 CERROR("already connecting\n");
702 import_set_state_nolock(imp, LUSTRE_IMP_CONNECTING);
705 imp->imp_resend_replay = 0;
707 if (!lustre_handle_is_used(&imp->imp_remote_handle))
710 committed_before_reconnect = imp->imp_peer_committed_transno;
712 set_transno = ptlrpc_first_transno(imp,
713 &imp->imp_connect_data.ocd_transno);
714 spin_unlock(&imp->imp_lock);
716 rc = import_select_connection(imp);
720 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
724 /* Reset connect flags to the originally requested flags, in case
725 * the server is updated on-the-fly we will get the new features. */
726 ocd = imp->imp_connect_data;
727 ocd.ocd_connect_flags = imp->imp_connect_flags_orig;
728 ocd.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
729 /* Reset ocd_version each time so the server knows the exact versions */
730 ocd.ocd_version = LUSTRE_VERSION_CODE;
731 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
732 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
734 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
735 &obd->obd_uuid, &ocd, NULL);
739 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
741 GOTO(out, rc = -ENOMEM);
743 /* get SELinux policy info if any */
744 rc = sptlrpc_get_sepol(request);
746 ptlrpc_request_free(request);
750 bufs[5] = request->rq_sepol;
752 req_capsule_set_size(&request->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
753 strlen(request->rq_sepol) ?
754 strlen(request->rq_sepol) + 1 : 0);
756 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
757 imp->imp_connect_op, bufs, NULL);
759 ptlrpc_request_free(request);
763 /* Report the rpc service time to the server so that it knows how long
764 * to wait for clients to join recovery */
765 lustre_msg_set_service_time(request->rq_reqmsg,
766 at_timeout2est(request->rq_timeout));
768 /* The amount of time we give the server to process the connect req.
769 * import_select_connection will increase the net latency on
770 * repeated reconnect attempts to cover slow networks.
771 * We override/ignore the server rpc completion estimate here,
772 * which may be large if this is a reconnect attempt */
773 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
774 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
776 request->rq_no_resend = request->rq_no_delay = 1;
777 request->rq_send_state = LUSTRE_IMP_CONNECTING;
778 /* Allow a slightly larger reply for future growth compatibility */
779 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
780 sizeof(struct obd_connect_data)+16*sizeof(__u64));
781 ptlrpc_request_set_replen(request);
782 request->rq_interpret_reply = ptlrpc_connect_interpret;
784 aa = ptlrpc_req_async_args(aa, request);
785 memset(aa, 0, sizeof *aa);
787 aa->pcaa_peer_committed = committed_before_reconnect;
788 aa->pcaa_initial_connect = initial_connect;
790 if (aa->pcaa_initial_connect) {
791 spin_lock(&imp->imp_lock);
792 imp->imp_replayable = 1;
793 spin_unlock(&imp->imp_lock);
794 lustre_msg_add_op_flags(request->rq_reqmsg,
795 MSG_CONNECT_INITIAL);
799 lustre_msg_add_op_flags(request->rq_reqmsg,
800 MSG_CONNECT_TRANSNO);
802 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %ld)",
803 request->rq_timeout);
804 ptlrpcd_add_req(request);
808 import_set_state(imp, LUSTRE_IMP_DISCON);
812 EXPORT_SYMBOL(ptlrpc_connect_import);
814 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
818 spin_lock(&imp->imp_lock);
819 force_verify = imp->imp_force_verify != 0;
820 spin_unlock(&imp->imp_lock);
823 ptlrpc_pinger_wake_up();
826 static int ptlrpc_busy_reconnect(int rc)
828 return (rc == -EBUSY) || (rc == -EAGAIN);
831 static int ptlrpc_connect_set_flags(struct obd_import *imp,
832 struct obd_connect_data *ocd,
833 __u64 old_connect_flags,
834 struct obd_export *exp, int init_connect)
837 struct client_obd *cli = &imp->imp_obd->u.cli;
839 spin_lock(&imp->imp_lock);
840 list_move(&imp->imp_conn_current->oic_item,
841 &imp->imp_conn_list);
842 imp->imp_last_success_conn =
843 imp->imp_conn_current->oic_last_attempt;
845 spin_unlock(&imp->imp_lock);
847 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
848 (ocd->ocd_version > LUSTRE_VERSION_CODE +
849 LUSTRE_VERSION_OFFSET_WARN ||
850 ocd->ocd_version < LUSTRE_VERSION_CODE -
851 LUSTRE_VERSION_OFFSET_WARN)) {
852 /* Sigh, some compilers do not like #ifdef in the middle
853 of macro arguments */
854 const char *older = "older than client. "
855 "Consider upgrading server";
856 const char *newer = "newer than client. "
857 "Consider upgrading client";
859 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
861 obd2cli_tgt(imp->imp_obd),
862 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
863 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
864 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
865 OBD_OCD_VERSION_FIX(ocd->ocd_version),
866 ocd->ocd_version > LUSTRE_VERSION_CODE ?
867 newer : older, LUSTRE_VERSION_STRING);
871 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
872 /* We sent to the server ocd_cksum_types with bits set
873 * for algorithms we understand. The server masked off
874 * the checksum types it doesn't support */
875 if ((ocd->ocd_cksum_types &
876 obd_cksum_types_supported_client()) == 0) {
877 LCONSOLE_ERROR("The negotiation of the checksum "
878 "alogrithm to use with server %s "
880 obd2cli_tgt(imp->imp_obd),
881 ocd->ocd_cksum_types,
882 obd_cksum_types_supported_client());
885 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
888 /* The server does not support OBD_CONNECT_CKSUM.
889 * Enforce ADLER for backward compatibility*/
890 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
892 cli->cl_cksum_type = obd_cksum_type_select(imp->imp_obd->obd_name,
893 cli->cl_supp_cksum_types,
894 cli->cl_preferred_cksum_type);
896 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
897 cli->cl_max_pages_per_rpc =
898 min(ocd->ocd_brw_size >> PAGE_SHIFT,
899 cli->cl_max_pages_per_rpc);
900 else if (imp->imp_connect_op == MDS_CONNECT ||
901 imp->imp_connect_op == MGS_CONNECT)
902 cli->cl_max_pages_per_rpc = 1;
904 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
905 (cli->cl_max_pages_per_rpc > 0));
907 client_adjust_max_dirty(cli);
909 /* Update client max modify RPCs in flight with value returned
911 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
912 cli->cl_max_mod_rpcs_in_flight = min(
913 cli->cl_max_mod_rpcs_in_flight,
914 ocd->ocd_maxmodrpcs);
916 cli->cl_max_mod_rpcs_in_flight = 1;
918 /* Reset ns_connect_flags only for initial connect. It might be
919 * changed in while using FS and if we reset it in reconnect
920 * this leads to losing user settings done before such as
921 * disable lru_resize, etc. */
922 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
923 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
927 ns->ns_connect_flags ^ ns->ns_orig_connect_flags;
928 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
929 "flags: %#llx\n", imp->imp_obd->obd_name,
930 ocd->ocd_connect_flags);
931 ns->ns_connect_flags = (ns->ns_connect_flags & changed_flags) |
932 (ocd->ocd_connect_flags & ~changed_flags);
933 ns->ns_orig_connect_flags = ocd->ocd_connect_flags;
936 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
937 /* We need a per-message support flag, because
938 * a. we don't know if the incoming connect reply
939 * supports AT or not (in reply_in_callback)
940 * until we unpack it.
941 * b. failovered server means export and flags are gone
942 * (in ptlrpc_send_reply).
943 * Can only be set when we know AT is supported at
945 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
947 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
949 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
955 * Add all replay requests back to unreplied list before start replay,
956 * so that we can make sure the known replied XID is always increased
957 * only even if when replaying requests.
959 static void ptlrpc_prepare_replay(struct obd_import *imp)
961 struct ptlrpc_request *req;
963 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
964 imp->imp_resend_replay)
967 /* If the server was restart during repaly, the requests may
968 * have been added to the unreplied list in former replay. */
969 spin_lock(&imp->imp_lock);
971 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
972 if (list_empty(&req->rq_unreplied_list))
973 ptlrpc_add_unreplied(req);
976 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
977 if (list_empty(&req->rq_unreplied_list))
978 ptlrpc_add_unreplied(req);
981 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
982 spin_unlock(&imp->imp_lock);
986 * interpret_reply callback for connect RPCs.
987 * Looks into returned status of connect operation and decides
988 * what to do with the import - i.e enter recovery, promote it to
989 * full state for normal operations of disconnect it due to an error.
991 static int ptlrpc_connect_interpret(const struct lu_env *env,
992 struct ptlrpc_request *request,
995 struct ptlrpc_connect_async_args *aa = data;
996 struct obd_import *imp = request->rq_import;
997 struct lustre_handle old_hdl;
998 __u64 old_connect_flags;
1000 struct obd_connect_data *ocd;
1001 struct obd_export *exp = NULL;
1005 spin_lock(&imp->imp_lock);
1006 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1007 imp->imp_connect_tried = 1;
1008 spin_unlock(&imp->imp_lock);
1012 imp->imp_connect_error = rc;
1014 struct ptlrpc_request *free_req;
1015 struct ptlrpc_request *tmp;
1017 /* abort all delayed requests initiated connection */
1018 list_for_each_entry_safe(free_req, tmp, &imp->imp_delayed_list,
1020 spin_lock(&free_req->rq_lock);
1021 if (free_req->rq_no_resend) {
1022 free_req->rq_err = 1;
1023 free_req->rq_status = -EIO;
1024 ptlrpc_client_wake_req(free_req);
1026 spin_unlock(&free_req->rq_lock);
1029 /* if this reconnect to busy export - not need select new target
1031 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
1032 spin_unlock(&imp->imp_lock);
1033 ptlrpc_maybe_ping_import_soon(imp);
1037 /* LU-7558: indicate that we are interpretting connect reply,
1038 * pltrpc_connect_import() will not try to reconnect until
1039 * interpret will finish. */
1040 imp->imp_connected = 1;
1041 spin_unlock(&imp->imp_lock);
1043 LASSERT(imp->imp_conn_current);
1045 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
1047 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
1049 /* server replied obd_connect_data is always bigger */
1050 ocd = req_capsule_server_sized_get(&request->rq_pill,
1051 &RMF_CONNECT_DATA, ret);
1054 CERROR("%s: no connect data from server\n",
1055 imp->imp_obd->obd_name);
1060 spin_lock(&imp->imp_lock);
1062 /* All imports are pingable */
1063 imp->imp_pingable = 1;
1064 imp->imp_force_reconnect = 0;
1065 imp->imp_force_verify = 0;
1067 imp->imp_connect_data = *ocd;
1069 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1070 imp->imp_obd->obd_name, ocd->ocd_instance);
1071 exp = class_conn2export(&imp->imp_dlm_handle);
1073 spin_unlock(&imp->imp_lock);
1076 /* This could happen if export is cleaned during the
1078 CERROR("%s: missing export after connect\n",
1079 imp->imp_obd->obd_name);
1080 GOTO(out, rc = -ENODEV);
1083 /* check that server granted subset of flags we asked for. */
1084 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1085 ocd->ocd_connect_flags) {
1086 CERROR("%s: Server didn't grant requested subset of flags: "
1087 "asked=%#llx granted=%#llx\n",
1088 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1089 ocd->ocd_connect_flags);
1090 GOTO(out, rc = -EPROTO);
1093 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1094 ocd->ocd_connect_flags2) {
1095 CERROR("%s: Server didn't grant requested subset of flags2: "
1096 "asked=%#llx granted=%#llx\n",
1097 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1098 ocd->ocd_connect_flags2);
1099 GOTO(out, rc = -EPROTO);
1102 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1103 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1104 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1105 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1106 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1107 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1108 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1110 /* We do not support the MDT-MDT interoperations with
1111 * different version MDT because of protocol changes. */
1112 if (unlikely(major != LUSTRE_MAJOR ||
1113 minor != LUSTRE_MINOR ||
1114 abs(patch - LUSTRE_PATCH) > 3)) {
1115 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1116 "connection to different version MDT "
1117 "(%d.%d.%d.%d) %s\n",
1118 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1119 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1120 major, minor, patch,
1121 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1122 imp->imp_connection->c_remote_uuid.uuid);
1124 GOTO(out, rc = -EPROTO);
1128 old_connect_flags = exp_connect_flags(exp);
1129 exp->exp_connect_data = *ocd;
1130 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1132 /* The net statistics after (re-)connect is not valid anymore,
1133 * because may reflect other routing, etc. */
1134 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1135 ptlrpc_at_adj_net_latency(request,
1136 lustre_msg_get_service_time(request->rq_repmsg));
1138 /* Import flags should be updated before waking import at FULL state */
1139 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1140 aa->pcaa_initial_connect);
1141 class_export_put(exp);
1147 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1149 if (aa->pcaa_initial_connect) {
1150 spin_lock(&imp->imp_lock);
1151 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1152 imp->imp_replayable = 1;
1153 CDEBUG(D_HA, "connected to replayable target: %s\n",
1154 obd2cli_tgt(imp->imp_obd));
1156 imp->imp_replayable = 0;
1159 /* if applies, adjust the imp->imp_msg_magic here
1160 * according to reply flags */
1162 imp->imp_remote_handle =
1163 *lustre_msg_get_handle(request->rq_repmsg);
1165 /* Initial connects are allowed for clients with non-random
1166 * uuids when servers are in recovery. Simply signal the
1167 * servers replay is complete and wait in REPLAY_WAIT. */
1168 if (msg_flags & MSG_CONNECT_RECOVERING) {
1169 CDEBUG(D_HA, "connect to %s during recovery\n",
1170 obd2cli_tgt(imp->imp_obd));
1171 import_set_state_nolock(imp, LUSTRE_IMP_REPLAY_LOCKS);
1172 spin_unlock(&imp->imp_lock);
1174 spin_unlock(&imp->imp_lock);
1175 ptlrpc_activate_import(imp, true);
1178 GOTO(finish, rc = 0);
1181 /* Determine what recovery state to move the import to. */
1182 if (MSG_CONNECT_RECONNECT & msg_flags) {
1183 memset(&old_hdl, 0, sizeof(old_hdl));
1184 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1185 sizeof (old_hdl))) {
1186 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1187 "bad handle %#llx\n",
1188 obd2cli_tgt(imp->imp_obd),
1189 imp->imp_connection->c_remote_uuid.uuid,
1190 imp->imp_dlm_handle.cookie);
1191 GOTO(out, rc = -ENOTCONN);
1194 if (memcmp(&imp->imp_remote_handle,
1195 lustre_msg_get_handle(request->rq_repmsg),
1196 sizeof(imp->imp_remote_handle))) {
1197 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1200 /* Bug 16611/14775: if server handle have changed,
1201 * that means some sort of disconnection happened.
1202 * If the server is not in recovery, that also means it
1203 * already erased all of our state because of previous
1204 * eviction. If it is in recovery - we are safe to
1205 * participate since we can reestablish all of our state
1206 * with server again */
1207 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1208 CDEBUG(level,"%s@%s changed server handle from "
1210 " but is still in recovery\n",
1211 obd2cli_tgt(imp->imp_obd),
1212 imp->imp_connection->c_remote_uuid.uuid,
1213 imp->imp_remote_handle.cookie,
1214 lustre_msg_get_handle(
1215 request->rq_repmsg)->cookie);
1217 LCONSOLE_WARN("Evicted from %s (at %s) "
1218 "after server handle changed from "
1220 obd2cli_tgt(imp->imp_obd),
1221 imp->imp_connection-> \
1223 imp->imp_remote_handle.cookie,
1224 lustre_msg_get_handle(
1225 request->rq_repmsg)->cookie);
1229 imp->imp_remote_handle =
1230 *lustre_msg_get_handle(request->rq_repmsg);
1232 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1233 import_set_state(imp, LUSTRE_IMP_EVICTED);
1234 GOTO(finish, rc = 0);
1238 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1239 obd2cli_tgt(imp->imp_obd),
1240 imp->imp_connection->c_remote_uuid.uuid);
1243 if (imp->imp_invalid) {
1244 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1245 "marking evicted\n", imp->imp_obd->obd_name);
1246 import_set_state(imp, LUSTRE_IMP_EVICTED);
1247 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1248 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1249 imp->imp_obd->obd_name,
1250 obd2cli_tgt(imp->imp_obd));
1252 spin_lock(&imp->imp_lock);
1253 imp->imp_resend_replay = 1;
1254 spin_unlock(&imp->imp_lock);
1256 import_set_state(imp, imp->imp_replay_state);
1258 import_set_state(imp, LUSTRE_IMP_RECOVER);
1260 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1261 LASSERT(imp->imp_replayable);
1262 imp->imp_remote_handle =
1263 *lustre_msg_get_handle(request->rq_repmsg);
1264 imp->imp_last_replay_transno = 0;
1265 imp->imp_replay_cursor = &imp->imp_committed_list;
1266 import_set_state(imp, LUSTRE_IMP_REPLAY);
1267 } else if ((ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0 &&
1268 !imp->imp_invalid) {
1270 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
1271 /* The below message is checked in recovery-small.sh test_106 */
1272 DEBUG_REQ(D_HA, request, "%s: lwp recover",
1273 imp->imp_obd->obd_name);
1274 imp->imp_remote_handle =
1275 *lustre_msg_get_handle(request->rq_repmsg);
1276 import_set_state(imp, LUSTRE_IMP_RECOVER);
1278 DEBUG_REQ(D_HA, request,
1279 "%s: evicting (reconnect/recover flags not set: %x)",
1280 imp->imp_obd->obd_name, msg_flags);
1281 imp->imp_remote_handle =
1282 *lustre_msg_get_handle(request->rq_repmsg);
1283 import_set_state(imp, LUSTRE_IMP_EVICTED);
1286 /* Sanity checks for a reconnected import. */
1287 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE))
1288 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
1290 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1291 lustre_msg_get_last_committed(request->rq_repmsg) <
1292 aa->pcaa_peer_committed) {
1293 static bool printed;
1295 /* The below message is checked in recovery-small.sh test_54 */
1296 CERROR("%s: went back in time (transno %lld was previously committed, server now claims %lld)!\n",
1297 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1298 lustre_msg_get_last_committed(request->rq_repmsg));
1300 CERROR("For further information, see http://doc.lustre.org/lustre_manual.xhtml#went_back_in_time\n");
1306 ptlrpc_prepare_replay(imp);
1307 rc = ptlrpc_import_recovery_state_machine(imp);
1308 if (rc == -ENOTCONN) {
1309 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1310 "invalidating and reconnecting\n",
1311 obd2cli_tgt(imp->imp_obd),
1312 imp->imp_connection->c_remote_uuid.uuid);
1313 ptlrpc_connect_import(imp);
1314 spin_lock(&imp->imp_lock);
1315 imp->imp_connected = 0;
1316 imp->imp_connect_tried = 1;
1317 spin_unlock(&imp->imp_lock);
1323 class_export_put(exp);
1325 spin_lock(&imp->imp_lock);
1326 imp->imp_connected = 0;
1327 imp->imp_connect_tried = 1;
1332 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1333 if (rc == -EACCES) {
1335 * Give up trying to reconnect
1336 * EACCES means client has no permission for connection
1338 imp->imp_obd->obd_no_recov = 1;
1339 ptlrpc_deactivate_import_nolock(imp);
1341 } else if (rc == -EPROTO) {
1342 struct obd_connect_data *ocd;
1344 /* reply message might not be ready */
1345 if (request->rq_repmsg == NULL) {
1346 spin_unlock(&imp->imp_lock);
1350 ocd = req_capsule_server_get(&request->rq_pill,
1352 /* Servers are not supposed to refuse connections from
1353 * clients based on version, only connection feature
1354 * flags. We should never see this from llite, but it
1355 * may be useful for debugging in the future. */
1357 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1358 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1359 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1361 " refused connection from this client "
1362 "with an incompatible version (%s). "
1363 "Client must be recompiled\n",
1364 obd2cli_tgt(imp->imp_obd),
1365 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1366 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1367 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1368 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1369 LUSTRE_VERSION_STRING);
1370 ptlrpc_deactivate_import_nolock(imp);
1371 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1375 spin_unlock(&imp->imp_lock);
1378 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
1383 ptlrpc_maybe_ping_import_soon(imp);
1385 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1386 obd2cli_tgt(imp->imp_obd),
1387 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1389 spin_unlock(&imp->imp_lock);
1392 wake_up_all(&imp->imp_recovery_waitq);
1397 * interpret callback for "completed replay" RPCs.
1398 * \see signal_completed_replay
1400 static int completed_replay_interpret(const struct lu_env *env,
1401 struct ptlrpc_request *req,
1405 atomic_dec(&req->rq_import->imp_replay_inflight);
1406 if (req->rq_status == 0 && !req->rq_import->imp_vbr_failed) {
1407 ptlrpc_import_recovery_state_machine(req->rq_import);
1409 if (req->rq_import->imp_vbr_failed) {
1411 "%s: version recovery fails, reconnecting\n",
1412 req->rq_import->imp_obd->obd_name);
1414 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1416 req->rq_import->imp_obd->obd_name,
1419 ptlrpc_connect_import(req->rq_import);
1426 * Let server know that we have no requests to replay anymore.
1427 * Achieved by just sending a PING request
1429 static int signal_completed_replay(struct obd_import *imp)
1431 struct ptlrpc_request *req;
1434 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1437 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1438 atomic_inc(&imp->imp_replay_inflight);
1440 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1443 atomic_dec(&imp->imp_replay_inflight);
1447 ptlrpc_request_set_replen(req);
1448 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1449 lustre_msg_add_flags(req->rq_reqmsg,
1450 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1452 req->rq_timeout *= 3;
1453 req->rq_interpret_reply = completed_replay_interpret;
1455 ptlrpcd_add_req(req);
1460 * In kernel code all import invalidation happens in its own
1461 * separate thread, so that whatever application happened to encounter
1462 * a problem could still be killed or otherwise continue
1464 static int ptlrpc_invalidate_import_thread(void *data)
1466 struct obd_import *imp = data;
1470 unshare_fs_struct();
1472 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1473 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1474 imp->imp_connection->c_remote_uuid.uuid);
1476 ptlrpc_invalidate_import(imp);
1478 if (obd_dump_on_eviction) {
1479 CERROR("dump the log upon eviction\n");
1480 libcfs_debug_dumplog();
1483 import_set_state(imp, LUSTRE_IMP_RECOVER);
1484 ptlrpc_import_recovery_state_machine(imp);
1486 class_import_put(imp);
1491 * This is the state machine for client-side recovery on import.
1493 * Typicaly we have two possibly paths. If we came to server and it is not
1494 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1495 * state and reconnect from scratch.
1496 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1497 * We go through our list of requests to replay and send them to server one by
1499 * After sending all request from the list we change import state to
1500 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1501 * and also all the locks we don't yet have and wait for server to grant us.
1502 * After that we send a special "replay completed" request and change import
1503 * state to IMP_REPLAY_WAIT.
1504 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1505 * state and resend all requests from sending list.
1506 * After that we promote import to FULL state and send all delayed requests
1507 * and import is fully operational after that.
1510 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1518 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1519 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1520 &target_start, &target_len);
1521 /* Don't care about MGC eviction */
1522 if (strcmp(imp->imp_obd->obd_type->typ_name,
1523 LUSTRE_MGC_NAME) != 0) {
1524 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1525 "by %.*s; in progress operations "
1526 "using this service will fail.\n",
1527 imp->imp_obd->obd_name, target_len,
1529 LASSERTF(!obd_lbug_on_eviction, "LBUG upon eviction");
1531 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1532 obd2cli_tgt(imp->imp_obd),
1533 imp->imp_connection->c_remote_uuid.uuid);
1534 /* reset vbr_failed flag upon eviction */
1535 spin_lock(&imp->imp_lock);
1536 imp->imp_vbr_failed = 0;
1537 spin_unlock(&imp->imp_lock);
1540 struct task_struct *task;
1541 /* bug 17802: XXX client_disconnect_export vs connect request
1542 * race. if client is evicted at this time then we start
1543 * invalidate thread without reference to import and import can
1544 * be freed at same time. */
1545 class_import_get(imp);
1546 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1549 class_import_put(imp);
1550 CERROR("error starting invalidate thread: %d\n", rc);
1559 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1560 CDEBUG(D_HA, "replay requested by %s\n",
1561 obd2cli_tgt(imp->imp_obd));
1562 rc = ptlrpc_replay_next(imp, &inflight);
1563 if (inflight == 0 &&
1564 atomic_read(&imp->imp_replay_inflight) == 0) {
1565 import_set_state(imp, LUSTRE_IMP_REPLAY_LOCKS);
1566 rc = ldlm_replay_locks(imp);
1573 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1574 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1575 import_set_state(imp, LUSTRE_IMP_REPLAY_WAIT);
1576 rc = signal_completed_replay(imp);
1582 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1583 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1584 import_set_state(imp, LUSTRE_IMP_RECOVER);
1588 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1589 struct ptlrpc_connection *conn = imp->imp_connection;
1591 rc = ptlrpc_resend(imp);
1594 ptlrpc_activate_import(imp, true);
1596 LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
1597 imp->imp_obd->obd_name,
1598 obd_uuid2str(&conn->c_remote_uuid),
1599 obd_import_nid2str(imp));
1602 if (imp->imp_state == LUSTRE_IMP_FULL) {
1603 wake_up_all(&imp->imp_recovery_waitq);
1604 ptlrpc_wake_delayed(imp);
1611 static struct ptlrpc_request *ptlrpc_disconnect_prep_req(struct obd_import *imp)
1613 struct ptlrpc_request *req;
1617 switch (imp->imp_connect_op) {
1619 rq_opc = OST_DISCONNECT;
1622 rq_opc = MDS_DISCONNECT;
1625 rq_opc = MGS_DISCONNECT;
1629 CERROR("%s: don't know how to disconnect from %s "
1630 "(connect_op %d): rc = %d\n",
1631 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1632 imp->imp_connect_op, rc);
1633 RETURN(ERR_PTR(rc));
1636 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1637 LUSTRE_OBD_VERSION, rq_opc);
1641 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1642 * it fails. We can get through the above with a down server
1643 * if the client doesn't know the server is gone yet. */
1644 req->rq_no_resend = 1;
1646 /* We want client umounts to happen quickly, no matter the
1648 req->rq_timeout = min_t(int, req->rq_timeout,
1649 INITIAL_CONNECT_TIMEOUT);
1651 import_set_state(imp, LUSTRE_IMP_CONNECTING);
1652 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1653 ptlrpc_request_set_replen(req);
1658 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1660 struct ptlrpc_request *req;
1664 if (imp->imp_obd->obd_force)
1665 GOTO(set_state, rc);
1667 /* probably the import has been disconnected already being idle */
1668 spin_lock(&imp->imp_lock);
1669 if (imp->imp_state == LUSTRE_IMP_IDLE)
1671 spin_unlock(&imp->imp_lock);
1673 if (ptlrpc_import_in_recovery(imp)) {
1674 struct l_wait_info lwi;
1675 long timeout_jiffies;
1679 if (imp->imp_server_timeout)
1680 timeout = obd_timeout >> 1;
1682 timeout = obd_timeout;
1687 req_portal = imp->imp_client->cli_request_portal;
1688 idx = import_at_get_index(imp, req_portal);
1689 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1692 timeout_jiffies = cfs_time_seconds(timeout);
1693 lwi = LWI_TIMEOUT_INTR(max_t(long, timeout_jiffies, 1),
1694 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1695 rc = l_wait_event(imp->imp_recovery_waitq,
1696 !ptlrpc_import_in_recovery(imp), &lwi);
1700 spin_lock(&imp->imp_lock);
1701 if (imp->imp_state != LUSTRE_IMP_FULL)
1703 spin_unlock(&imp->imp_lock);
1705 req = ptlrpc_disconnect_prep_req(imp);
1707 GOTO(set_state, rc = PTR_ERR(req));
1708 rc = ptlrpc_queue_wait(req);
1709 ptlrpc_req_finished(req);
1712 spin_lock(&imp->imp_lock);
1715 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1717 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1718 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1719 spin_unlock(&imp->imp_lock);
1721 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1725 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1727 static void ptlrpc_reset_reqs_generation(struct obd_import *imp)
1729 struct ptlrpc_request *old, *tmp;
1731 /* tag all resendable requests generated before disconnection
1732 * notice this code is part of disconnect-at-idle path only */
1733 list_for_each_entry_safe(old, tmp, &imp->imp_delayed_list,
1735 spin_lock(&old->rq_lock);
1736 if (old->rq_import_generation == imp->imp_generation - 1 &&
1738 old->rq_import_generation = imp->imp_generation;
1739 spin_unlock(&old->rq_lock);
1743 static int ptlrpc_disconnect_idle_interpret(const struct lu_env *env,
1744 struct ptlrpc_request *req,
1747 struct obd_import *imp = req->rq_import;
1750 DEBUG_REQ(D_HA, req, "inflight=%d, refcount=%d: rc = %d",
1751 atomic_read(&imp->imp_inflight),
1752 atomic_read(&imp->imp_refcount), rc);
1754 spin_lock(&imp->imp_lock);
1755 /* DISCONNECT reply can be late and another connection can just
1756 * be initiated. so we have to abort disconnection. */
1757 if (req->rq_import_generation == imp->imp_generation &&
1758 imp->imp_state != LUSTRE_IMP_CLOSED) {
1759 LASSERTF(imp->imp_state == LUSTRE_IMP_CONNECTING,
1760 "%s\n", ptlrpc_import_state_name(imp->imp_state));
1761 imp->imp_state = LUSTRE_IMP_IDLE;
1762 memset(&imp->imp_remote_handle, 0,
1763 sizeof(imp->imp_remote_handle));
1764 /* take our DISCONNECT into account */
1765 if (atomic_read(&imp->imp_inflight) > 1) {
1766 imp->imp_generation++;
1767 imp->imp_initiated_at = imp->imp_generation;
1768 import_set_state_nolock(imp, LUSTRE_IMP_NEW);
1769 ptlrpc_reset_reqs_generation(imp);
1773 spin_unlock(&imp->imp_lock);
1776 rc = ptlrpc_connect_import(imp);
1778 ptlrpc_pinger_add_import(imp);
1784 int ptlrpc_disconnect_and_idle_import(struct obd_import *imp)
1786 struct ptlrpc_request *req;
1789 if (imp->imp_obd->obd_force)
1792 if (ptlrpc_import_in_recovery(imp))
1795 spin_lock(&imp->imp_lock);
1796 if (imp->imp_state != LUSTRE_IMP_FULL) {
1797 spin_unlock(&imp->imp_lock);
1800 spin_unlock(&imp->imp_lock);
1802 req = ptlrpc_disconnect_prep_req(imp);
1804 RETURN(PTR_ERR(req));
1806 CDEBUG_LIMIT(imp->imp_idle_debug, "%s: disconnect after %llus idle\n",
1807 imp->imp_obd->obd_name,
1808 ktime_get_real_seconds() - imp->imp_last_reply_time);
1809 req->rq_interpret_reply = ptlrpc_disconnect_idle_interpret;
1810 ptlrpcd_add_req(req);
1814 EXPORT_SYMBOL(ptlrpc_disconnect_and_idle_import);
1816 void ptlrpc_cleanup_imp(struct obd_import *imp)
1820 spin_lock(&imp->imp_lock);
1822 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1823 imp->imp_generation++;
1824 ptlrpc_abort_inflight(imp);
1826 spin_unlock(&imp->imp_lock);
1831 /* Adaptive Timeout utils */
1833 /* Update at_current with the specified value (bounded by at_min and at_max),
1834 * as well as the AT history "bins".
1835 * - Bin into timeslices using AT_BINS bins.
1836 * - This gives us a max of the last at_history seconds without the storage,
1837 * but still smoothing out a return to normalcy from a slow response.
1838 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1840 int at_measured(struct adaptive_timeout *at, unsigned int val)
1842 unsigned int old = at->at_current;
1843 time64_t now = ktime_get_real_seconds();
1844 long binlimit = max_t(long, at_history / AT_BINS, 1);
1847 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1848 val, at, (long)(now - at->at_binstart), at->at_current,
1849 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1852 /* 0's don't count, because we never want our timeout to
1853 drop to 0, and because 0 could mean an error */
1856 spin_lock(&at->at_lock);
1858 if (unlikely(at->at_binstart == 0)) {
1859 /* Special case to remove default from history */
1860 at->at_current = val;
1861 at->at_worst_ever = val;
1862 at->at_worst_time = now;
1863 at->at_hist[0] = val;
1864 at->at_binstart = now;
1865 } else if (now - at->at_binstart < binlimit ) {
1867 at->at_hist[0] = max(val, at->at_hist[0]);
1868 at->at_current = max(val, at->at_current);
1871 unsigned int maxv = val;
1873 /* move bins over */
1874 shift = (u32)(now - at->at_binstart) / binlimit;
1876 for(i = AT_BINS - 1; i >= 0; i--) {
1878 at->at_hist[i] = at->at_hist[i - shift];
1879 maxv = max(maxv, at->at_hist[i]);
1884 at->at_hist[0] = val;
1885 at->at_current = maxv;
1886 at->at_binstart += shift * binlimit;
1889 if (at->at_current > at->at_worst_ever) {
1890 at->at_worst_ever = at->at_current;
1891 at->at_worst_time = now;
1894 if (at->at_flags & AT_FLG_NOHIST)
1895 /* Only keep last reported val; keeping the rest of the history
1897 at->at_current = val;
1900 at->at_current = min(at->at_current, at_max);
1901 at->at_current = max(at->at_current, at_min);
1903 if (at->at_current != old)
1904 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1905 "(val=%u) hist %u %u %u %u\n", at,
1906 old, at->at_current, at->at_current - old, val,
1907 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1910 /* if we changed, report the old value */
1911 old = (at->at_current != old) ? old : 0;
1913 spin_unlock(&at->at_lock);
1917 /* Find the imp_at index for a given portal; assign if space available */
1918 int import_at_get_index(struct obd_import *imp, int portal)
1920 struct imp_at *at = &imp->imp_at;
1923 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1924 if (at->iat_portal[i] == portal)
1926 if (at->iat_portal[i] == 0)
1931 /* Not found in list, add it under a lock */
1932 spin_lock(&imp->imp_lock);
1934 /* Check unused under lock */
1935 for (; i < IMP_AT_MAX_PORTALS; i++) {
1936 if (at->iat_portal[i] == portal)
1938 if (at->iat_portal[i] == 0)
1943 /* Not enough portals? */
1944 LASSERT(i < IMP_AT_MAX_PORTALS);
1946 at->iat_portal[i] = portal;
1948 spin_unlock(&imp->imp_lock);