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 (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 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;
307 atomic_inc(&imp->imp_inval_count);
309 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
310 ptlrpc_deactivate_import(imp);
312 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CONNECT_RACE)) {
313 OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
314 msleep(10 * MSEC_PER_SEC);
316 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
317 LASSERT(imp->imp_invalid);
319 /* Wait forever until inflight == 0. We really can't do it another
320 * way because in some cases we need to wait for very long reply
321 * unlink. We can't do anything before that because there is really
322 * 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 rc = wait_event_idle_timeout(
351 imp->imp_recovery_waitq,
352 (atomic_read(&imp->imp_inflight) == 0),
356 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
358 CERROR("%s: timeout waiting for callback (%d != 0)\n",
359 cli_tgt, atomic_read(&imp->imp_inflight));
361 spin_lock(&imp->imp_lock);
362 if (atomic_read(&imp->imp_inflight) == 0) {
363 int count = atomic_read(&imp->imp_unregistering);
365 /* We know that "unregistering" rpcs only can
366 * survive in sending or delaying lists (they
367 * maybe waiting for long reply unlink in
368 * sluggish nets). Let's check this. If there
369 * is no inflight and unregistering != 0, this
371 LASSERTF(count == 0, "Some RPCs are still "
372 "unregistering: %d\n", count);
374 /* Let's save one loop as soon as inflight have
375 * dropped to zero. No new inflights possible at
379 list_for_each_safe(tmp, n,
380 &imp->imp_sending_list) {
381 req = list_entry(tmp,
382 struct ptlrpc_request,
384 DEBUG_REQ(D_ERROR, req,
385 "still on sending list");
387 list_for_each_safe(tmp, n,
388 &imp->imp_delayed_list) {
389 req = list_entry(tmp,
390 struct ptlrpc_request,
392 DEBUG_REQ(D_ERROR, req,
393 "still on delayed list");
396 CERROR("%s: Unregistering RPCs found (%d). "
397 "Network is sluggish? Waiting for them "
398 "to error out.\n", cli_tgt,
399 atomic_read(&imp->imp_unregistering));
401 spin_unlock(&imp->imp_lock);
406 * Let's additionally check that no new rpcs added to import in
407 * "invalidate" state.
409 LASSERT(atomic_read(&imp->imp_inflight) == 0);
410 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
411 sptlrpc_import_flush_all_ctx(imp);
413 atomic_dec(&imp->imp_inval_count);
414 wake_up(&imp->imp_recovery_waitq);
416 EXPORT_SYMBOL(ptlrpc_invalidate_import);
418 /* unset imp_invalid */
419 void ptlrpc_activate_import(struct obd_import *imp, bool set_state_full)
421 struct obd_device *obd = imp->imp_obd;
423 spin_lock(&imp->imp_lock);
424 if (imp->imp_deactive != 0) {
425 LASSERT(imp->imp_state != LUSTRE_IMP_FULL);
426 if (imp->imp_state != LUSTRE_IMP_DISCON)
427 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
428 spin_unlock(&imp->imp_lock);
432 import_set_state_nolock(imp, LUSTRE_IMP_FULL);
434 imp->imp_invalid = 0;
436 spin_unlock(&imp->imp_lock);
437 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
439 EXPORT_SYMBOL(ptlrpc_activate_import);
441 void ptlrpc_pinger_force(struct obd_import *imp)
443 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
444 ptlrpc_import_state_name(imp->imp_state));
446 spin_lock(&imp->imp_lock);
447 imp->imp_force_verify = 1;
448 spin_unlock(&imp->imp_lock);
450 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
451 ptlrpc_pinger_wake_up();
453 EXPORT_SYMBOL(ptlrpc_pinger_force);
455 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
459 LASSERT(!imp->imp_dlm_fake);
461 if (ptlrpc_set_import_discon(imp, conn_cnt, true))
462 ptlrpc_pinger_force(imp);
467 int ptlrpc_reconnect_import(struct obd_import *imp)
469 #ifdef CONFIG_LUSTRE_FS_PINGER
470 long timeout_jiffies = cfs_time_seconds(obd_timeout);
473 ptlrpc_pinger_force(imp);
475 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
476 obd2cli_tgt(imp->imp_obd), obd_timeout);
478 rc = wait_event_idle_timeout(imp->imp_recovery_waitq,
479 !ptlrpc_import_in_recovery(imp),
485 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
486 ptlrpc_import_state_name(imp->imp_state));
489 ptlrpc_set_import_discon(imp, 0, false);
490 /* Force a new connect attempt */
491 ptlrpc_invalidate_import(imp);
492 /* Do a fresh connect next time by zeroing the handle */
493 ptlrpc_disconnect_import(imp, 1);
494 /* Wait for all invalidate calls to finish */
495 if (atomic_read(&imp->imp_inval_count) > 0) {
498 rc = l_wait_event_abortable(
499 imp->imp_recovery_waitq,
500 (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;
531 spin_lock(&imp->imp_lock);
533 if (list_empty(&imp->imp_conn_list)) {
535 CERROR("%s: no connections available: rc = %d\n",
536 imp->imp_obd->obd_name, rc);
537 GOTO(out_unlock, rc);
540 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
541 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
542 imp->imp_obd->obd_name,
543 libcfs_nid2str(conn->oic_conn->c_peer.nid),
544 conn->oic_last_attempt);
546 /* If we have not tried this connection since
547 * the last successful attempt, go with this one
549 if ((conn->oic_last_attempt == 0) ||
550 conn->oic_last_attempt <= imp->imp_last_success_conn) {
556 /* If all of the connections have already been tried
557 * since the last successful connection; just choose the
558 * least recently used
562 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
566 /* if not found, simply choose the current one */
567 if (!imp_conn || imp->imp_force_reconnect) {
568 LASSERT(imp->imp_conn_current);
569 imp_conn = imp->imp_conn_current;
572 LASSERT(imp_conn->oic_conn);
574 /* If we've tried everything, and we're back to the beginning of the
575 * list, increase our timeout and try again. It will be reset when
576 * we do finally connect. (FIXME: really we should wait for all network
577 * state associated with the last connection attempt to drain before
578 * trying to reconnect on it.)
580 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
581 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
583 if (at_get(at) < CONNECTION_SWITCH_MAX) {
584 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
585 if (at_get(at) > CONNECTION_SWITCH_MAX)
586 at_reset(at, CONNECTION_SWITCH_MAX);
588 LASSERT(imp_conn->oic_last_attempt);
590 "%s: tried all connections, increasing latency to %ds\n",
591 imp->imp_obd->obd_name, at_get(at));
594 imp_conn->oic_last_attempt = ktime_get_seconds();
596 /* switch connection, don't mind if it's same as the current one */
597 if (imp->imp_connection)
598 ptlrpc_connection_put(imp->imp_connection);
599 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
601 dlmexp = class_conn2export(&imp->imp_dlm_handle);
603 GOTO(out_unlock, rc = -EINVAL);
604 if (dlmexp->exp_connection)
605 ptlrpc_connection_put(dlmexp->exp_connection);
606 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
607 class_export_put(dlmexp);
609 if (imp->imp_conn_current != imp_conn) {
610 if (imp->imp_conn_current) {
611 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
612 &target_start, &target_len);
614 CDEBUG(D_HA, "%s: Connection changing to"
616 imp->imp_obd->obd_name,
617 target_len, target_start,
618 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
621 imp->imp_conn_current = imp_conn;
624 /* The below message is checked in conf-sanity.sh test_35[ab] */
625 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
626 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
627 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
630 spin_unlock(&imp->imp_lock);
635 * must be called under imp_lock
637 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
639 struct ptlrpc_request *req;
640 struct list_head *tmp;
642 /* The requests in committed_list always have smaller transnos than
643 * the requests in replay_list */
644 if (!list_empty(&imp->imp_committed_list)) {
645 tmp = imp->imp_committed_list.next;
646 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
647 *transno = req->rq_transno;
648 if (req->rq_transno == 0) {
649 DEBUG_REQ(D_ERROR, req,
650 "zero transno in committed_list");
655 if (!list_empty(&imp->imp_replay_list)) {
656 tmp = imp->imp_replay_list.next;
657 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
658 *transno = req->rq_transno;
659 if (req->rq_transno == 0) {
660 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
668 int ptlrpc_connect_import(struct obd_import *imp)
670 spin_lock(&imp->imp_lock);
671 return ptlrpc_connect_import_locked(imp);
675 * Attempt to (re)connect import \a imp. This includes all preparations,
676 * initializing CONNECT RPC request and passing it to ptlrpcd for
679 * Assumes imp->imp_lock is held, and releases it.
681 * Returns 0 on success or error code.
683 int ptlrpc_connect_import_locked(struct obd_import *imp)
685 struct obd_device *obd = imp->imp_obd;
686 int initial_connect = 0;
688 __u64 committed_before_reconnect = 0;
689 struct ptlrpc_request *request;
690 struct obd_connect_data ocd;
691 char *bufs[] = { NULL,
692 obd2cli_tgt(imp->imp_obd),
694 (char *)&imp->imp_dlm_handle,
697 struct ptlrpc_connect_async_args *aa;
701 assert_spin_locked(&imp->imp_lock);
703 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
704 spin_unlock(&imp->imp_lock);
705 CERROR("can't connect to a closed import\n");
707 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
708 spin_unlock(&imp->imp_lock);
709 CERROR("already connected\n");
711 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
712 imp->imp_state == LUSTRE_IMP_EVICTED ||
713 imp->imp_connected) {
714 spin_unlock(&imp->imp_lock);
715 CERROR("already connecting\n");
719 import_set_state_nolock(imp, LUSTRE_IMP_CONNECTING);
722 imp->imp_resend_replay = 0;
724 if (!lustre_handle_is_used(&imp->imp_remote_handle))
727 committed_before_reconnect = imp->imp_peer_committed_transno;
729 set_transno = ptlrpc_first_transno(imp,
730 &imp->imp_connect_data.ocd_transno);
731 spin_unlock(&imp->imp_lock);
733 rc = import_select_connection(imp);
737 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
741 /* Reset connect flags to the originally requested flags, in case
742 * the server is updated on-the-fly we will get the new features. */
743 ocd = imp->imp_connect_data;
744 ocd.ocd_connect_flags = imp->imp_connect_flags_orig;
745 ocd.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
746 /* Reset ocd_version each time so the server knows the exact versions */
747 ocd.ocd_version = LUSTRE_VERSION_CODE;
748 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
749 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
751 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
752 &obd->obd_uuid, &ocd, NULL);
756 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
758 GOTO(out, rc = -ENOMEM);
760 /* get SELinux policy info if any */
761 rc = sptlrpc_get_sepol(request);
763 ptlrpc_request_free(request);
767 bufs[5] = request->rq_sepol;
769 req_capsule_set_size(&request->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
770 strlen(request->rq_sepol) ?
771 strlen(request->rq_sepol) + 1 : 0);
773 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
774 imp->imp_connect_op, bufs, NULL);
776 ptlrpc_request_free(request);
780 /* Report the rpc service time to the server so that it knows how long
781 * to wait for clients to join recovery */
782 lustre_msg_set_service_timeout(request->rq_reqmsg,
783 at_timeout2est(request->rq_timeout));
785 /* The amount of time we give the server to process the connect req.
786 * import_select_connection will increase the net latency on
787 * repeated reconnect attempts to cover slow networks.
788 * We override/ignore the server rpc completion estimate here,
789 * which may be large if this is a reconnect attempt */
790 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
791 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
793 request->rq_no_resend = request->rq_no_delay = 1;
794 request->rq_send_state = LUSTRE_IMP_CONNECTING;
795 /* Allow a slightly larger reply for future growth compatibility */
796 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
797 sizeof(struct obd_connect_data)+16*sizeof(__u64));
798 ptlrpc_request_set_replen(request);
799 request->rq_interpret_reply = ptlrpc_connect_interpret;
801 aa = ptlrpc_req_async_args(aa, request);
802 memset(aa, 0, sizeof *aa);
804 aa->pcaa_peer_committed = committed_before_reconnect;
805 aa->pcaa_initial_connect = initial_connect;
807 if (aa->pcaa_initial_connect) {
808 spin_lock(&imp->imp_lock);
809 imp->imp_replayable = 1;
810 spin_unlock(&imp->imp_lock);
811 lustre_msg_add_op_flags(request->rq_reqmsg,
812 MSG_CONNECT_INITIAL);
816 lustre_msg_add_op_flags(request->rq_reqmsg,
817 MSG_CONNECT_TRANSNO);
819 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
820 request->rq_timeout);
821 ptlrpcd_add_req(request);
825 import_set_state(imp, LUSTRE_IMP_DISCON);
829 EXPORT_SYMBOL(ptlrpc_connect_import);
831 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
835 spin_lock(&imp->imp_lock);
836 force_verify = imp->imp_force_verify != 0;
837 spin_unlock(&imp->imp_lock);
840 ptlrpc_pinger_wake_up();
843 static int ptlrpc_busy_reconnect(int rc)
845 return (rc == -EBUSY) || (rc == -EAGAIN);
848 static int ptlrpc_connect_set_flags(struct obd_import *imp,
849 struct obd_connect_data *ocd,
850 __u64 old_connect_flags,
851 struct obd_export *exp, int init_connect)
854 struct client_obd *cli = &imp->imp_obd->u.cli;
856 spin_lock(&imp->imp_lock);
857 list_move(&imp->imp_conn_current->oic_item,
858 &imp->imp_conn_list);
859 imp->imp_last_success_conn =
860 imp->imp_conn_current->oic_last_attempt;
862 spin_unlock(&imp->imp_lock);
864 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
865 (ocd->ocd_version > LUSTRE_VERSION_CODE +
866 LUSTRE_VERSION_OFFSET_WARN ||
867 ocd->ocd_version < LUSTRE_VERSION_CODE -
868 LUSTRE_VERSION_OFFSET_WARN)) {
869 /* Sigh, some compilers do not like #ifdef in the middle
870 of macro arguments */
871 const char *older = "older than client. "
872 "Consider upgrading server";
873 const char *newer = "newer than client. "
874 "Consider upgrading client";
876 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
878 obd2cli_tgt(imp->imp_obd),
879 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
880 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
881 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
882 OBD_OCD_VERSION_FIX(ocd->ocd_version),
883 ocd->ocd_version > LUSTRE_VERSION_CODE ?
884 newer : older, LUSTRE_VERSION_STRING);
888 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
889 /* We sent to the server ocd_cksum_types with bits set
890 * for algorithms we understand. The server masked off
891 * the checksum types it doesn't support */
892 if ((ocd->ocd_cksum_types &
893 obd_cksum_types_supported_client()) == 0) {
894 LCONSOLE_ERROR("The negotiation of the checksum "
895 "alogrithm to use with server %s "
897 obd2cli_tgt(imp->imp_obd),
898 ocd->ocd_cksum_types,
899 obd_cksum_types_supported_client());
902 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
905 /* The server does not support OBD_CONNECT_CKSUM.
906 * Enforce ADLER for backward compatibility*/
907 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
909 cli->cl_cksum_type = obd_cksum_type_select(imp->imp_obd->obd_name,
910 cli->cl_supp_cksum_types,
911 cli->cl_preferred_cksum_type);
913 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
914 cli->cl_max_pages_per_rpc =
915 min(ocd->ocd_brw_size >> PAGE_SHIFT,
916 cli->cl_max_pages_per_rpc);
917 else if (imp->imp_connect_op == MDS_CONNECT ||
918 imp->imp_connect_op == MGS_CONNECT)
919 cli->cl_max_pages_per_rpc = 1;
921 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
922 (cli->cl_max_pages_per_rpc > 0));
924 client_adjust_max_dirty(cli);
926 /* Update client max modify RPCs in flight with value returned
928 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
929 cli->cl_max_mod_rpcs_in_flight = min(
930 cli->cl_max_mod_rpcs_in_flight,
931 ocd->ocd_maxmodrpcs);
933 cli->cl_max_mod_rpcs_in_flight = 1;
935 /* Reset ns_connect_flags only for initial connect. It might be
936 * changed in while using FS and if we reset it in reconnect
937 * this leads to losing user settings done before such as
938 * disable lru_resize, etc. */
939 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
940 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
944 ns->ns_connect_flags ^ ns->ns_orig_connect_flags;
945 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
946 "flags: %#llx\n", imp->imp_obd->obd_name,
947 ocd->ocd_connect_flags);
948 ns->ns_connect_flags = (ns->ns_connect_flags & changed_flags) |
949 (ocd->ocd_connect_flags & ~changed_flags);
950 ns->ns_orig_connect_flags = ocd->ocd_connect_flags;
953 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
954 /* We need a per-message support flag, because
955 * a. we don't know if the incoming connect reply
956 * supports AT or not (in reply_in_callback)
957 * until we unpack it.
958 * b. failovered server means export and flags are gone
959 * (in ptlrpc_send_reply).
960 * Can only be set when we know AT is supported at
962 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
964 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
966 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
972 * Add all replay requests back to unreplied list before start replay,
973 * so that we can make sure the known replied XID is always increased
974 * only even if when replaying requests.
976 static void ptlrpc_prepare_replay(struct obd_import *imp)
978 struct ptlrpc_request *req;
980 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
981 imp->imp_resend_replay)
984 /* If the server was restart during repaly, the requests may
985 * have been added to the unreplied list in former replay. */
986 spin_lock(&imp->imp_lock);
988 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
989 if (list_empty(&req->rq_unreplied_list))
990 ptlrpc_add_unreplied(req);
993 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
994 if (list_empty(&req->rq_unreplied_list))
995 ptlrpc_add_unreplied(req);
998 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
999 spin_unlock(&imp->imp_lock);
1003 * interpret_reply callback for connect RPCs.
1004 * Looks into returned status of connect operation and decides
1005 * what to do with the import - i.e enter recovery, promote it to
1006 * full state for normal operations of disconnect it due to an error.
1008 static int ptlrpc_connect_interpret(const struct lu_env *env,
1009 struct ptlrpc_request *request,
1012 struct ptlrpc_connect_async_args *aa = data;
1013 struct obd_import *imp = request->rq_import;
1014 struct lustre_handle old_hdl;
1015 __u64 old_connect_flags;
1016 timeout_t service_timeout;
1018 struct obd_connect_data *ocd;
1019 struct obd_export *exp = NULL;
1023 spin_lock(&imp->imp_lock);
1024 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
1025 imp->imp_connect_tried = 1;
1026 spin_unlock(&imp->imp_lock);
1030 imp->imp_connect_error = rc;
1032 struct ptlrpc_request *free_req;
1033 struct ptlrpc_request *tmp;
1035 /* abort all delayed requests initiated connection */
1036 list_for_each_entry_safe(free_req, tmp, &imp->imp_delayed_list,
1038 spin_lock(&free_req->rq_lock);
1039 if (free_req->rq_no_resend) {
1040 free_req->rq_err = 1;
1041 free_req->rq_status = -EIO;
1042 ptlrpc_client_wake_req(free_req);
1044 spin_unlock(&free_req->rq_lock);
1047 /* if this reconnect to busy export - not need select new target
1049 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
1050 spin_unlock(&imp->imp_lock);
1051 ptlrpc_maybe_ping_import_soon(imp);
1055 /* LU-7558: indicate that we are interpretting connect reply,
1056 * pltrpc_connect_import() will not try to reconnect until
1057 * interpret will finish. */
1058 imp->imp_connected = 1;
1059 spin_unlock(&imp->imp_lock);
1061 LASSERT(imp->imp_conn_current);
1063 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
1065 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
1067 /* server replied obd_connect_data is always bigger */
1068 ocd = req_capsule_server_sized_get(&request->rq_pill,
1069 &RMF_CONNECT_DATA, ret);
1072 CERROR("%s: no connect data from server\n",
1073 imp->imp_obd->obd_name);
1078 spin_lock(&imp->imp_lock);
1080 /* All imports are pingable */
1081 imp->imp_pingable = 1;
1082 imp->imp_force_reconnect = 0;
1083 imp->imp_force_verify = 0;
1085 imp->imp_connect_data = *ocd;
1087 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1088 imp->imp_obd->obd_name, ocd->ocd_instance);
1089 exp = class_conn2export(&imp->imp_dlm_handle);
1091 spin_unlock(&imp->imp_lock);
1094 /* This could happen if export is cleaned during the
1096 CERROR("%s: missing export after connect\n",
1097 imp->imp_obd->obd_name);
1098 GOTO(out, rc = -ENODEV);
1101 /* check that server granted subset of flags we asked for. */
1102 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1103 ocd->ocd_connect_flags) {
1104 CERROR("%s: Server didn't grant requested subset of flags: "
1105 "asked=%#llx granted=%#llx\n",
1106 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1107 ocd->ocd_connect_flags);
1108 GOTO(out, rc = -EPROTO);
1111 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1112 ocd->ocd_connect_flags2) {
1113 CERROR("%s: Server didn't grant requested subset of flags2: "
1114 "asked=%#llx granted=%#llx\n",
1115 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1116 ocd->ocd_connect_flags2);
1117 GOTO(out, rc = -EPROTO);
1120 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1121 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1122 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1123 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1124 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1125 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1126 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1128 /* We do not support the MDT-MDT interoperations with
1129 * different version MDT because of protocol changes. */
1130 if (unlikely(major != LUSTRE_MAJOR ||
1131 minor != LUSTRE_MINOR ||
1132 abs(patch - LUSTRE_PATCH) > 3)) {
1133 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1134 "connection to different version MDT "
1135 "(%d.%d.%d.%d) %s\n",
1136 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1137 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1138 major, minor, patch,
1139 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1140 imp->imp_connection->c_remote_uuid.uuid);
1142 GOTO(out, rc = -EPROTO);
1146 old_connect_flags = exp_connect_flags(exp);
1147 exp->exp_connect_data = *ocd;
1148 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1150 /* The net statistics after (re-)connect is not valid anymore,
1151 * because may reflect other routing, etc.
1153 service_timeout = lustre_msg_get_service_timeout(request->rq_repmsg);
1154 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1155 ptlrpc_at_adj_net_latency(request, service_timeout);
1157 /* Import flags should be updated before waking import at FULL state */
1158 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1159 aa->pcaa_initial_connect);
1160 class_export_put(exp);
1166 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1168 if (aa->pcaa_initial_connect) {
1169 spin_lock(&imp->imp_lock);
1170 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1171 imp->imp_replayable = 1;
1172 CDEBUG(D_HA, "connected to replayable target: %s\n",
1173 obd2cli_tgt(imp->imp_obd));
1175 imp->imp_replayable = 0;
1178 /* if applies, adjust the imp->imp_msg_magic here
1179 * according to reply flags
1182 imp->imp_remote_handle =
1183 *lustre_msg_get_handle(request->rq_repmsg);
1185 /* Initial connects are allowed for clients with non-random
1186 * uuids when servers are in recovery. Simply signal the
1187 * servers replay is complete and wait in REPLAY_WAIT.
1189 if (msg_flags & MSG_CONNECT_RECOVERING) {
1190 CDEBUG(D_HA, "connect to %s during recovery\n",
1191 obd2cli_tgt(imp->imp_obd));
1192 import_set_state_nolock(imp, LUSTRE_IMP_REPLAY_LOCKS);
1193 spin_unlock(&imp->imp_lock);
1195 spin_unlock(&imp->imp_lock);
1196 ptlrpc_activate_import(imp, true);
1199 GOTO(finish, rc = 0);
1202 /* Determine what recovery state to move the import to. */
1203 if (MSG_CONNECT_RECONNECT & msg_flags) {
1204 memset(&old_hdl, 0, sizeof(old_hdl));
1205 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1207 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1208 "bad handle %#llx\n",
1209 obd2cli_tgt(imp->imp_obd),
1210 imp->imp_connection->c_remote_uuid.uuid,
1211 imp->imp_dlm_handle.cookie);
1212 GOTO(out, rc = -ENOTCONN);
1215 if (memcmp(&imp->imp_remote_handle,
1216 lustre_msg_get_handle(request->rq_repmsg),
1217 sizeof(imp->imp_remote_handle))) {
1218 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1221 /* Bug 16611/14775: if server handle have changed,
1222 * that means some sort of disconnection happened.
1223 * If the server is not in recovery, that also means it
1224 * already erased all of our state because of previous
1225 * eviction. If it is in recovery - we are safe to
1226 * participate since we can reestablish all of our state
1229 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1231 "%s@%s changed server handle from "
1233 " but is still in recovery\n",
1234 obd2cli_tgt(imp->imp_obd),
1235 imp->imp_connection->c_remote_uuid.uuid,
1236 imp->imp_remote_handle.cookie,
1237 lustre_msg_get_handle(
1238 request->rq_repmsg)->cookie);
1240 LCONSOLE_WARN("Evicted from %s (at %s) "
1241 "after server handle changed from "
1243 obd2cli_tgt(imp->imp_obd),
1244 imp->imp_connection->
1246 imp->imp_remote_handle.cookie,
1247 lustre_msg_get_handle(
1248 request->rq_repmsg)->cookie);
1251 imp->imp_remote_handle =
1252 *lustre_msg_get_handle(request->rq_repmsg);
1254 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1255 import_set_state(imp, LUSTRE_IMP_EVICTED);
1256 GOTO(finish, rc = 0);
1259 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1260 obd2cli_tgt(imp->imp_obd),
1261 imp->imp_connection->c_remote_uuid.uuid);
1264 if (imp->imp_invalid) {
1265 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1266 "marking evicted\n", imp->imp_obd->obd_name);
1267 import_set_state(imp, LUSTRE_IMP_EVICTED);
1268 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1269 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1270 imp->imp_obd->obd_name,
1271 obd2cli_tgt(imp->imp_obd));
1273 spin_lock(&imp->imp_lock);
1274 imp->imp_resend_replay = 1;
1275 spin_unlock(&imp->imp_lock);
1277 import_set_state(imp, imp->imp_replay_state);
1279 import_set_state(imp, LUSTRE_IMP_RECOVER);
1281 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1282 LASSERT(imp->imp_replayable);
1283 imp->imp_remote_handle =
1284 *lustre_msg_get_handle(request->rq_repmsg);
1285 imp->imp_last_replay_transno = 0;
1286 imp->imp_replay_cursor = &imp->imp_committed_list;
1287 import_set_state(imp, LUSTRE_IMP_REPLAY);
1288 } else if ((ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0 &&
1289 !imp->imp_invalid) {
1291 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
1292 /* The below message is checked in recovery-small.sh test_106 */
1293 DEBUG_REQ(D_HA, request, "%s: lwp recover",
1294 imp->imp_obd->obd_name);
1295 imp->imp_remote_handle =
1296 *lustre_msg_get_handle(request->rq_repmsg);
1297 import_set_state(imp, LUSTRE_IMP_RECOVER);
1299 DEBUG_REQ(D_HA, request,
1300 "%s: evicting (reconnect/recover flags not set: %x)",
1301 imp->imp_obd->obd_name, msg_flags);
1302 imp->imp_remote_handle =
1303 *lustre_msg_get_handle(request->rq_repmsg);
1304 import_set_state(imp, LUSTRE_IMP_EVICTED);
1307 /* Sanity checks for a reconnected import. */
1308 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE))
1309 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
1311 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1312 lustre_msg_get_last_committed(request->rq_repmsg) <
1313 aa->pcaa_peer_committed) {
1314 static bool printed;
1316 /* The below message is checked in recovery-small.sh test_54 */
1317 CERROR("%s: went back in time (transno %lld was previously committed, server now claims %lld)!\n",
1318 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1319 lustre_msg_get_last_committed(request->rq_repmsg));
1321 CERROR("For further information, see http://doc.lustre.org/lustre_manual.xhtml#went_back_in_time\n");
1327 ptlrpc_prepare_replay(imp);
1328 rc = ptlrpc_import_recovery_state_machine(imp);
1329 if (rc == -ENOTCONN) {
1331 "evicted/aborted by %s@%s during recovery; invalidating and reconnecting\n",
1332 obd2cli_tgt(imp->imp_obd),
1333 imp->imp_connection->c_remote_uuid.uuid);
1334 ptlrpc_connect_import(imp);
1335 spin_lock(&imp->imp_lock);
1336 imp->imp_connected = 0;
1337 imp->imp_connect_tried = 1;
1338 spin_unlock(&imp->imp_lock);
1344 class_export_put(exp);
1346 spin_lock(&imp->imp_lock);
1347 imp->imp_connected = 0;
1348 imp->imp_connect_tried = 1;
1353 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1354 if (rc == -EACCES) {
1356 * Give up trying to reconnect
1357 * EACCES means client has no permission for connection
1359 imp->imp_obd->obd_no_recov = 1;
1360 ptlrpc_deactivate_import_nolock(imp);
1362 } else if (rc == -EPROTO) {
1363 struct obd_connect_data *ocd;
1365 /* reply message might not be ready */
1366 if (request->rq_repmsg == NULL) {
1367 spin_unlock(&imp->imp_lock);
1371 ocd = req_capsule_server_get(&request->rq_pill,
1373 /* Servers are not supposed to refuse connections from
1374 * clients based on version, only connection feature
1375 * flags. We should never see this from llite, but it
1376 * may be useful for debugging in the future. */
1378 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1379 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1380 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1382 " refused connection from this client "
1383 "with an incompatible version (%s). "
1384 "Client must be recompiled\n",
1385 obd2cli_tgt(imp->imp_obd),
1386 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1387 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1388 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1389 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1390 LUSTRE_VERSION_STRING);
1391 ptlrpc_deactivate_import_nolock(imp);
1392 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1396 spin_unlock(&imp->imp_lock);
1399 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
1404 ptlrpc_maybe_ping_import_soon(imp);
1406 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1407 obd2cli_tgt(imp->imp_obd),
1408 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1410 spin_unlock(&imp->imp_lock);
1413 wake_up(&imp->imp_recovery_waitq);
1418 * interpret callback for "completed replay" RPCs.
1419 * \see signal_completed_replay
1421 static int completed_replay_interpret(const struct lu_env *env,
1422 struct ptlrpc_request *req,
1426 atomic_dec(&req->rq_import->imp_replay_inflight);
1427 if (req->rq_status == 0 && !req->rq_import->imp_vbr_failed) {
1428 ptlrpc_import_recovery_state_machine(req->rq_import);
1430 if (req->rq_import->imp_vbr_failed) {
1432 "%s: version recovery fails, reconnecting\n",
1433 req->rq_import->imp_obd->obd_name);
1435 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1437 req->rq_import->imp_obd->obd_name,
1440 ptlrpc_connect_import(req->rq_import);
1447 * Let server know that we have no requests to replay anymore.
1448 * Achieved by just sending a PING request
1450 static int signal_completed_replay(struct obd_import *imp)
1452 struct ptlrpc_request *req;
1455 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1458 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1459 atomic_inc(&imp->imp_replay_inflight);
1461 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1464 atomic_dec(&imp->imp_replay_inflight);
1468 ptlrpc_request_set_replen(req);
1469 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1470 lustre_msg_add_flags(req->rq_reqmsg,
1471 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1473 req->rq_timeout *= 3;
1474 req->rq_interpret_reply = completed_replay_interpret;
1476 ptlrpcd_add_req(req);
1481 * In kernel code all import invalidation happens in its own
1482 * separate thread, so that whatever application happened to encounter
1483 * a problem could still be killed or otherwise continue
1485 static int ptlrpc_invalidate_import_thread(void *data)
1487 struct obd_import *imp = data;
1491 unshare_fs_struct();
1493 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1494 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1495 imp->imp_connection->c_remote_uuid.uuid);
1497 ptlrpc_invalidate_import(imp);
1499 if (obd_dump_on_eviction) {
1500 CERROR("dump the log upon eviction\n");
1501 libcfs_debug_dumplog();
1504 import_set_state(imp, LUSTRE_IMP_RECOVER);
1505 ptlrpc_import_recovery_state_machine(imp);
1507 class_import_put(imp);
1512 * This is the state machine for client-side recovery on import.
1514 * Typicaly we have two possibly paths. If we came to server and it is not
1515 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1516 * state and reconnect from scratch.
1517 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1518 * We go through our list of requests to replay and send them to server one by
1520 * After sending all request from the list we change import state to
1521 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1522 * and also all the locks we don't yet have and wait for server to grant us.
1523 * After that we send a special "replay completed" request and change import
1524 * state to IMP_REPLAY_WAIT.
1525 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1526 * state and resend all requests from sending list.
1527 * After that we promote import to FULL state and send all delayed requests
1528 * and import is fully operational after that.
1531 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1539 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1540 struct task_struct *task;
1542 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1543 &target_start, &target_len);
1544 /* Don't care about MGC eviction */
1545 if (strcmp(imp->imp_obd->obd_type->typ_name,
1546 LUSTRE_MGC_NAME) != 0) {
1547 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1548 "by %.*s; in progress operations "
1549 "using this service will fail.\n",
1550 imp->imp_obd->obd_name, target_len,
1552 LASSERTF(!obd_lbug_on_eviction, "LBUG upon eviction\n");
1554 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1555 obd2cli_tgt(imp->imp_obd),
1556 imp->imp_connection->c_remote_uuid.uuid);
1557 /* reset vbr_failed flag upon eviction */
1558 spin_lock(&imp->imp_lock);
1559 imp->imp_vbr_failed = 0;
1560 spin_unlock(&imp->imp_lock);
1562 /* bug 17802: XXX client_disconnect_export vs connect request
1563 * race. if client is evicted at this time then we start
1564 * invalidate thread without reference to import and import can
1565 * be freed at same time. */
1566 class_import_get(imp);
1567 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1570 class_import_put(imp);
1572 CERROR("%s: can't start invalidate thread: rc = %d\n",
1573 imp->imp_obd->obd_name, rc);
1580 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1581 CDEBUG(D_HA, "replay requested by %s\n",
1582 obd2cli_tgt(imp->imp_obd));
1583 rc = ptlrpc_replay_next(imp, &inflight);
1584 if (inflight == 0 &&
1585 atomic_read(&imp->imp_replay_inflight) == 0) {
1586 import_set_state(imp, LUSTRE_IMP_REPLAY_LOCKS);
1587 rc = ldlm_replay_locks(imp);
1594 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1595 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1596 import_set_state(imp, LUSTRE_IMP_REPLAY_WAIT);
1597 rc = signal_completed_replay(imp);
1603 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1604 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1605 import_set_state(imp, LUSTRE_IMP_RECOVER);
1609 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1610 struct ptlrpc_connection *conn = imp->imp_connection;
1612 rc = ptlrpc_resend(imp);
1615 ptlrpc_activate_import(imp, true);
1617 CDEBUG_LIMIT(imp->imp_was_idle ?
1618 imp->imp_idle_debug : D_CONSOLE,
1619 "%s: Connection restored to %s (at %s)\n",
1620 imp->imp_obd->obd_name,
1621 obd_uuid2str(&conn->c_remote_uuid),
1622 obd_import_nid2str(imp));
1623 spin_lock(&imp->imp_lock);
1624 imp->imp_was_idle = 0;
1625 spin_unlock(&imp->imp_lock);
1628 if (imp->imp_state == LUSTRE_IMP_FULL) {
1629 wake_up(&imp->imp_recovery_waitq);
1630 ptlrpc_wake_delayed(imp);
1637 static struct ptlrpc_request *ptlrpc_disconnect_prep_req(struct obd_import *imp)
1639 struct ptlrpc_request *req;
1643 switch (imp->imp_connect_op) {
1645 rq_opc = OST_DISCONNECT;
1648 rq_opc = MDS_DISCONNECT;
1651 rq_opc = MGS_DISCONNECT;
1655 CERROR("%s: don't know how to disconnect from %s "
1656 "(connect_op %d): rc = %d\n",
1657 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1658 imp->imp_connect_op, rc);
1659 RETURN(ERR_PTR(rc));
1662 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1663 LUSTRE_OBD_VERSION, rq_opc);
1665 RETURN(ERR_PTR(-ENOMEM));
1667 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1668 * it fails. We can get through the above with a down server
1669 * if the client doesn't know the server is gone yet. */
1670 req->rq_no_resend = 1;
1672 /* We want client umounts to happen quickly, no matter the
1674 req->rq_timeout = min_t(timeout_t, req->rq_timeout,
1675 INITIAL_CONNECT_TIMEOUT);
1677 import_set_state(imp, LUSTRE_IMP_CONNECTING);
1678 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1679 ptlrpc_request_set_replen(req);
1684 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1686 struct ptlrpc_request *req;
1690 if (imp->imp_obd->obd_force)
1691 GOTO(set_state, rc);
1693 /* probably the import has been disconnected already being idle */
1694 spin_lock(&imp->imp_lock);
1695 if (imp->imp_state == LUSTRE_IMP_IDLE)
1697 spin_unlock(&imp->imp_lock);
1699 if (ptlrpc_import_in_recovery(imp)) {
1700 long timeout_jiffies;
1704 if (imp->imp_server_timeout)
1705 timeout = obd_timeout >> 1;
1707 timeout = obd_timeout;
1712 req_portal = imp->imp_client->cli_request_portal;
1713 idx = import_at_get_index(imp, req_portal);
1714 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1717 timeout_jiffies = cfs_time_seconds(timeout);
1718 if (wait_event_idle_timeout(imp->imp_recovery_waitq,
1719 !ptlrpc_import_in_recovery(imp),
1720 timeout_jiffies) == 0 &&
1721 l_wait_event_abortable(imp->imp_recovery_waitq,
1722 !ptlrpc_import_in_recovery(imp)) < 0)
1726 spin_lock(&imp->imp_lock);
1727 if (imp->imp_state != LUSTRE_IMP_FULL)
1729 spin_unlock(&imp->imp_lock);
1731 req = ptlrpc_disconnect_prep_req(imp);
1733 GOTO(set_state, rc = PTR_ERR(req));
1734 rc = ptlrpc_queue_wait(req);
1735 ptlrpc_req_finished(req);
1738 spin_lock(&imp->imp_lock);
1741 import_set_state_nolock(imp, LUSTRE_IMP_DISCON);
1743 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1744 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1745 spin_unlock(&imp->imp_lock);
1747 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1751 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1753 static void ptlrpc_reset_reqs_generation(struct obd_import *imp)
1755 struct ptlrpc_request *old, *tmp;
1757 /* tag all resendable requests generated before disconnection
1758 * notice this code is part of disconnect-at-idle path only */
1759 list_for_each_entry_safe(old, tmp, &imp->imp_delayed_list,
1761 spin_lock(&old->rq_lock);
1762 if (old->rq_import_generation == imp->imp_generation - 1 &&
1763 ((imp->imp_initiated_at == imp->imp_generation) ||
1764 !old->rq_no_resend))
1765 old->rq_import_generation = imp->imp_generation;
1766 spin_unlock(&old->rq_lock);
1770 static int ptlrpc_disconnect_idle_interpret(const struct lu_env *env,
1771 struct ptlrpc_request *req,
1774 struct obd_import *imp = req->rq_import;
1777 DEBUG_REQ(D_HA, req, "inflight=%d, refcount=%d: rc = %d",
1778 atomic_read(&imp->imp_inflight),
1779 refcount_read(&imp->imp_refcount), rc);
1781 spin_lock(&imp->imp_lock);
1782 /* DISCONNECT reply can be late and another connection can just
1783 * be initiated. so we have to abort disconnection. */
1784 if (req->rq_import_generation == imp->imp_generation &&
1785 imp->imp_state != LUSTRE_IMP_CLOSED) {
1786 LASSERTF(imp->imp_state == LUSTRE_IMP_CONNECTING,
1787 "%s\n", ptlrpc_import_state_name(imp->imp_state));
1788 memset(&imp->imp_remote_handle, 0,
1789 sizeof(imp->imp_remote_handle));
1790 /* take our DISCONNECT into account */
1791 if (atomic_read(&imp->imp_reqs) > 1) {
1792 imp->imp_generation++;
1793 imp->imp_initiated_at = imp->imp_generation;
1794 import_set_state_nolock(imp, LUSTRE_IMP_NEW);
1795 ptlrpc_reset_reqs_generation(imp);
1798 /* do not expose transient IDLE state */
1799 import_set_state_nolock(imp, LUSTRE_IMP_IDLE);
1804 rc = ptlrpc_connect_import_locked(imp);
1806 ptlrpc_pinger_add_import(imp);
1808 spin_unlock(&imp->imp_lock);
1814 int ptlrpc_disconnect_and_idle_import(struct obd_import *imp)
1816 struct ptlrpc_request *req;
1819 if (imp->imp_obd->obd_force)
1822 if (ptlrpc_import_in_recovery(imp))
1825 spin_lock(&imp->imp_lock);
1826 if (imp->imp_state != LUSTRE_IMP_FULL) {
1827 spin_unlock(&imp->imp_lock);
1830 spin_unlock(&imp->imp_lock);
1832 req = ptlrpc_disconnect_prep_req(imp);
1834 RETURN(PTR_ERR(req));
1836 CDEBUG_LIMIT(imp->imp_idle_debug, "%s: disconnect after %llus idle\n",
1837 imp->imp_obd->obd_name,
1838 ktime_get_real_seconds() - imp->imp_last_reply_time);
1840 /* don't make noise at reconnection */
1841 spin_lock(&imp->imp_lock);
1842 imp->imp_was_idle = 1;
1843 spin_unlock(&imp->imp_lock);
1845 req->rq_interpret_reply = ptlrpc_disconnect_idle_interpret;
1846 ptlrpcd_add_req(req);
1850 EXPORT_SYMBOL(ptlrpc_disconnect_and_idle_import);
1852 void ptlrpc_cleanup_imp(struct obd_import *imp)
1856 spin_lock(&imp->imp_lock);
1858 import_set_state_nolock(imp, LUSTRE_IMP_CLOSED);
1859 imp->imp_generation++;
1860 ptlrpc_abort_inflight(imp);
1862 spin_unlock(&imp->imp_lock);
1867 /* Adaptive Timeout utils */
1869 /* Update at_current_timeout with the specified value (bounded by at_min and
1870 * at_max), as well as the AT history "bins".
1871 * - Bin into timeslices using AT_BINS bins.
1872 * - This gives us a max of the last at_history seconds without the storage,
1873 * but still smoothing out a return to normalcy from a slow response.
1874 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1876 timeout_t at_measured(struct adaptive_timeout *at, timeout_t timeout)
1878 timeout_t old_timeout = at->at_current_timeout;
1879 time64_t now = ktime_get_real_seconds();
1880 long binlimit = max_t(long, at_history / AT_BINS, 1);
1883 CDEBUG(D_OTHER, "add %u to %p time=%lld v=%u (%u %u %u %u)\n",
1884 timeout, at, now - at->at_binstart, at->at_current_timeout,
1885 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1888 /* Negative timeouts and 0's don't count, because we never
1889 * want our timeout to drop to 0 or below, and because 0 could
1894 spin_lock(&at->at_lock);
1896 if (unlikely(at->at_binstart == 0)) {
1897 /* Special case to remove default from history */
1898 at->at_current_timeout = timeout;
1899 at->at_worst_timeout_ever = timeout;
1900 at->at_worst_timestamp = now;
1901 at->at_hist[0] = timeout;
1902 at->at_binstart = now;
1903 } else if (now - at->at_binstart < binlimit ) {
1905 at->at_hist[0] = max_t(timeout_t, timeout, at->at_hist[0]);
1906 at->at_current_timeout = max_t(timeout_t, timeout,
1907 at->at_current_timeout);
1910 timeout_t maxv = timeout;
1912 /* move bins over */
1913 shift = (u32)(now - at->at_binstart) / binlimit;
1915 for(i = AT_BINS - 1; i >= 0; i--) {
1917 at->at_hist[i] = at->at_hist[i - shift];
1918 maxv = max_t(timeout_t, maxv, at->at_hist[i]);
1923 at->at_hist[0] = timeout;
1924 at->at_current_timeout = maxv;
1925 at->at_binstart += shift * binlimit;
1928 if (at->at_current_timeout > at->at_worst_timeout_ever) {
1929 at->at_worst_timeout_ever = at->at_current_timeout;
1930 at->at_worst_timestamp = now;
1933 if (at->at_flags & AT_FLG_NOHIST)
1934 /* Only keep last reported val; keeping the rest of the history
1937 at->at_current_timeout = timeout;
1940 at->at_current_timeout = min_t(timeout_t,
1941 at->at_current_timeout, at_max);
1942 at->at_current_timeout = max_t(timeout_t, at->at_current_timeout,
1944 if (at->at_current_timeout != old_timeout)
1946 "AT %p change: old=%u new=%u delta=%d (val=%d) hist %u %u %u %u\n",
1947 at, old_timeout, at->at_current_timeout,
1948 at->at_current_timeout - old_timeout, timeout,
1949 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1952 /* if we changed, report the old timeout value */
1953 old_timeout = (at->at_current_timeout != old_timeout) ? old_timeout : 0;
1955 spin_unlock(&at->at_lock);
1959 /* Find the imp_at index for a given portal; assign if space available */
1960 int import_at_get_index(struct obd_import *imp, int portal)
1962 struct imp_at *at = &imp->imp_at;
1965 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1966 if (at->iat_portal[i] == portal)
1968 if (at->iat_portal[i] == 0)
1973 /* Not found in list, add it under a lock */
1974 spin_lock(&imp->imp_lock);
1976 /* Check unused under lock */
1977 for (; i < IMP_AT_MAX_PORTALS; i++) {
1978 if (at->iat_portal[i] == portal)
1980 if (at->iat_portal[i] == 0)
1985 /* Not enough portals? */
1986 LASSERT(i < IMP_AT_MAX_PORTALS);
1988 at->iat_portal[i] = portal;
1990 spin_unlock(&imp->imp_lock);