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
59 * Helper function. Must be called under imp_lock.
61 static void __import_set_state(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;
76 imp->imp_state = state;
77 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
78 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
79 ktime_get_real_seconds();
80 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
84 /* A CLOSED import should remain so. */
85 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
87 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
88 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
89 imp, obd2cli_tgt(imp->imp_obd), \
90 ptlrpc_import_state_name(imp->imp_state), \
91 ptlrpc_import_state_name(state)); \
92 __import_set_state(imp, state); \
96 #define IMPORT_SET_STATE(imp, state) \
98 spin_lock(&imp->imp_lock); \
99 IMPORT_SET_STATE_NOLOCK(imp, state); \
100 spin_unlock(&imp->imp_lock); \
103 void ptlrpc_import_enter_resend(struct obd_import *imp)
105 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
107 EXPORT_SYMBOL(ptlrpc_import_enter_resend);
110 static int ptlrpc_connect_interpret(const struct lu_env *env,
111 struct ptlrpc_request *request,
113 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
115 /* Only this function is allowed to change the import state when it is
116 * CLOSED. I would rather refcount the import and free it after
117 * disconnection like we do with exports. To do that, the client_obd
118 * will need to save the peer info somewhere other than in the import,
120 int ptlrpc_init_import(struct obd_import *imp)
122 spin_lock(&imp->imp_lock);
124 imp->imp_generation++;
125 imp->imp_state = LUSTRE_IMP_NEW;
127 spin_unlock(&imp->imp_lock);
131 EXPORT_SYMBOL(ptlrpc_init_import);
133 #define UUID_STR "_UUID"
134 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
136 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
137 ? uuid : uuid + strlen(prefix);
139 *uuid_len = strlen(*uuid_start);
141 if (*uuid_len < strlen(UUID_STR))
144 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
145 UUID_STR, strlen(UUID_STR)))
146 *uuid_len -= strlen(UUID_STR);
150 * Returns true if import was FULL, false if import was already not
152 * @imp - import to be disconnected
153 * @conn_cnt - connection count (epoch) of the request that timed out
154 * and caused the disconnection. In some cases, multiple
155 * inflight requests can fail to a single target (e.g. OST
156 * bulk requests) and if one has already caused a reconnection
157 * (increasing the import->conn_cnt) the older failure should
158 * not also cause a reconnection. If zero it forces a reconnect.
160 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
164 spin_lock(&imp->imp_lock);
166 if (imp->imp_state == LUSTRE_IMP_FULL &&
167 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
171 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
172 &target_start, &target_len);
174 if (imp->imp_replayable) {
175 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
176 "lost; in progress operations using this "
177 "service will wait for recovery to complete\n",
178 imp->imp_obd->obd_name, target_len, target_start,
179 obd_import_nid2str(imp));
181 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
182 "%.*s (at %s) was lost; in progress "
183 "operations using this service will fail\n",
184 imp->imp_obd->obd_name, target_len, target_start,
185 obd_import_nid2str(imp));
187 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
188 spin_unlock(&imp->imp_lock);
190 if (obd_dump_on_timeout)
191 libcfs_debug_dumplog();
193 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
196 spin_unlock(&imp->imp_lock);
197 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
198 imp->imp_client->cli_name, imp,
199 (imp->imp_state == LUSTRE_IMP_FULL &&
200 imp->imp_conn_cnt > conn_cnt) ?
201 "reconnected" : "not connected", imp->imp_conn_cnt,
202 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
208 /* Must be called with imp_lock held! */
209 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
212 assert_spin_locked(&imp->imp_lock);
214 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
215 imp->imp_invalid = 1;
216 imp->imp_generation++;
217 spin_unlock(&imp->imp_lock);
219 ptlrpc_abort_inflight(imp);
220 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
226 * This acts as a barrier; all existing requests are rejected, and
227 * no new requests will be accepted until the import is valid again.
229 void ptlrpc_deactivate_import(struct obd_import *imp)
231 spin_lock(&imp->imp_lock);
232 ptlrpc_deactivate_and_unlock_import(imp);
234 EXPORT_SYMBOL(ptlrpc_deactivate_import);
236 static time64_t ptlrpc_inflight_deadline(struct ptlrpc_request *req,
241 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
242 (req->rq_phase == RQ_PHASE_BULK) ||
243 (req->rq_phase == RQ_PHASE_NEW)))
246 if (req->rq_timedout)
249 if (req->rq_phase == RQ_PHASE_NEW)
252 dl = req->rq_deadline;
260 static time64_t ptlrpc_inflight_timeout(struct obd_import *imp)
262 time64_t now = ktime_get_real_seconds();
263 struct list_head *tmp, *n;
264 struct ptlrpc_request *req;
265 time64_t timeout = 0;
267 spin_lock(&imp->imp_lock);
268 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
269 req = list_entry(tmp, struct ptlrpc_request, rq_list);
270 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
272 spin_unlock(&imp->imp_lock);
277 * This function will invalidate the import, if necessary, then block
278 * for all the RPC completions, and finally notify the obd to
279 * invalidate its state (ie cancel locks, clear pending requests,
282 void ptlrpc_invalidate_import(struct obd_import *imp)
284 struct list_head *tmp, *n;
285 struct ptlrpc_request *req;
286 struct l_wait_info lwi;
290 atomic_inc(&imp->imp_inval_count);
292 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
293 ptlrpc_deactivate_import(imp);
295 if (OBD_FAIL_PRECHECK(OBD_FAIL_PTLRPC_CONNECT_RACE)) {
296 OBD_RACE(OBD_FAIL_PTLRPC_CONNECT_RACE);
297 msleep(10 * MSEC_PER_SEC);
299 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
300 LASSERT(imp->imp_invalid);
302 /* Wait forever until inflight == 0. We really can't do it another
303 * way because in some cases we need to wait for very long reply
304 * unlink. We can't do anything before that because there is really
305 * no guarantee that some rdma transfer is not in progress right now. */
307 long timeout_jiffies;
309 /* Calculate max timeout for waiting on rpcs to error
310 * out. Use obd_timeout if calculated value is smaller
313 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
314 timeout = ptlrpc_inflight_timeout(imp);
315 timeout += div_u64(timeout, 3);
318 timeout = obd_timeout;
320 /* decrease the interval to increase race condition */
324 CDEBUG(D_RPCTRACE, "Sleeping %llds for inflight to error out\n",
327 /* Wait for all requests to error out and call completion
328 * callbacks. Cap it at obd_timeout -- these should all
329 * have been locally cancelled by ptlrpc_abort_inflight.
331 timeout_jiffies = max_t(long, cfs_time_seconds(timeout), 1);
332 lwi = LWI_TIMEOUT_INTERVAL(timeout_jiffies,
333 (timeout > 1) ? cfs_time_seconds(1) :
334 cfs_time_seconds(1) / 2,
336 rc = l_wait_event(imp->imp_recovery_waitq,
337 (atomic_read(&imp->imp_inflight) == 0),
340 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
342 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
343 cli_tgt, rc, atomic_read(&imp->imp_inflight));
345 spin_lock(&imp->imp_lock);
346 if (atomic_read(&imp->imp_inflight) == 0) {
347 int count = atomic_read(&imp->imp_unregistering);
349 /* We know that "unregistering" rpcs only can
350 * survive in sending or delaying lists (they
351 * maybe waiting for long reply unlink in
352 * sluggish nets). Let's check this. If there
353 * is no inflight and unregistering != 0, this
355 LASSERTF(count == 0, "Some RPCs are still "
356 "unregistering: %d\n", count);
358 /* Let's save one loop as soon as inflight have
359 * dropped to zero. No new inflights possible at
363 list_for_each_safe(tmp, n,
364 &imp->imp_sending_list) {
365 req = list_entry(tmp,
366 struct ptlrpc_request,
368 DEBUG_REQ(D_ERROR, req,
369 "still on sending list");
371 list_for_each_safe(tmp, n,
372 &imp->imp_delayed_list) {
373 req = list_entry(tmp,
374 struct ptlrpc_request,
376 DEBUG_REQ(D_ERROR, req,
377 "still on delayed list");
380 CERROR("%s: Unregistering RPCs found (%d). "
381 "Network is sluggish? Waiting them "
382 "to error out.\n", cli_tgt,
383 atomic_read(&imp->imp_unregistering));
385 spin_unlock(&imp->imp_lock);
390 * Let's additionally check that no new rpcs added to import in
391 * "invalidate" state.
393 LASSERT(atomic_read(&imp->imp_inflight) == 0);
394 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
395 sptlrpc_import_flush_all_ctx(imp);
397 atomic_dec(&imp->imp_inval_count);
398 wake_up_all(&imp->imp_recovery_waitq);
400 EXPORT_SYMBOL(ptlrpc_invalidate_import);
402 /* unset imp_invalid */
403 void ptlrpc_activate_import(struct obd_import *imp)
405 struct obd_device *obd = imp->imp_obd;
407 spin_lock(&imp->imp_lock);
408 if (imp->imp_deactive != 0) {
409 spin_unlock(&imp->imp_lock);
413 imp->imp_invalid = 0;
414 spin_unlock(&imp->imp_lock);
415 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
417 EXPORT_SYMBOL(ptlrpc_activate_import);
419 void ptlrpc_pinger_force(struct obd_import *imp)
421 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
422 ptlrpc_import_state_name(imp->imp_state));
424 spin_lock(&imp->imp_lock);
425 imp->imp_force_verify = 1;
426 spin_unlock(&imp->imp_lock);
428 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
429 ptlrpc_pinger_wake_up();
431 EXPORT_SYMBOL(ptlrpc_pinger_force);
433 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
437 LASSERT(!imp->imp_dlm_fake);
439 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
440 if (!imp->imp_replayable) {
441 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
442 "auto-deactivating\n",
443 obd2cli_tgt(imp->imp_obd),
444 imp->imp_connection->c_remote_uuid.uuid,
445 imp->imp_obd->obd_name);
446 ptlrpc_deactivate_import(imp);
449 ptlrpc_pinger_force(imp);
454 int ptlrpc_reconnect_import(struct obd_import *imp)
457 long timeout_jiffies = cfs_time_seconds(obd_timeout);
458 struct l_wait_info lwi;
461 ptlrpc_pinger_force(imp);
463 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
464 obd2cli_tgt(imp->imp_obd), obd_timeout);
466 lwi = LWI_TIMEOUT(timeout_jiffies, NULL, NULL);
467 rc = l_wait_event(imp->imp_recovery_waitq,
468 !ptlrpc_import_in_recovery(imp), &lwi);
469 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
470 ptlrpc_import_state_name(imp->imp_state));
473 ptlrpc_set_import_discon(imp, 0);
474 /* Force a new connect attempt */
475 ptlrpc_invalidate_import(imp);
476 /* Do a fresh connect next time by zeroing the handle */
477 ptlrpc_disconnect_import(imp, 1);
478 /* Wait for all invalidate calls to finish */
479 if (atomic_read(&imp->imp_inval_count) > 0) {
480 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
483 rc = l_wait_event(imp->imp_recovery_waitq,
484 (atomic_read(&imp->imp_inval_count) == 0),
487 CERROR("Interrupted, inval=%d\n",
488 atomic_read(&imp->imp_inval_count));
491 /* Allow reconnect attempts */
492 imp->imp_obd->obd_no_recov = 0;
493 /* Remove 'invalid' flag */
494 ptlrpc_activate_import(imp);
495 /* Attempt a new connect */
496 ptlrpc_recover_import(imp, NULL, 0);
500 EXPORT_SYMBOL(ptlrpc_reconnect_import);
503 * Connection on import \a imp is changed to another one (if more than one is
504 * present). We typically chose connection that we have not tried to connect to
507 static int import_select_connection(struct obd_import *imp)
509 struct obd_import_conn *imp_conn = NULL, *conn;
510 struct obd_export *dlmexp;
512 int target_len, tried_all = 1;
515 spin_lock(&imp->imp_lock);
517 if (list_empty(&imp->imp_conn_list)) {
518 CERROR("%s: no connections available\n",
519 imp->imp_obd->obd_name);
520 spin_unlock(&imp->imp_lock);
524 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
525 CDEBUG(D_HA, "%s: connect to NID %s last attempt %lld\n",
526 imp->imp_obd->obd_name,
527 libcfs_nid2str(conn->oic_conn->c_peer.nid),
528 conn->oic_last_attempt);
530 /* If we have not tried this connection since
531 the last successful attempt, go with this one */
532 if ((conn->oic_last_attempt == 0) ||
533 conn->oic_last_attempt <= imp->imp_last_success_conn) {
539 /* If all of the connections have already been tried
540 since the last successful connection; just choose the
541 least recently used */
544 else if (imp_conn->oic_last_attempt > conn->oic_last_attempt)
548 /* if not found, simply choose the current one */
549 if (!imp_conn || imp->imp_force_reconnect) {
550 LASSERT(imp->imp_conn_current);
551 imp_conn = imp->imp_conn_current;
554 LASSERT(imp_conn->oic_conn);
556 /* If we've tried everything, and we're back to the beginning of the
557 list, increase our timeout and try again. It will be reset when
558 we do finally connect. (FIXME: really we should wait for all network
559 state associated with the last connection attempt to drain before
560 trying to reconnect on it.) */
561 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
562 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
563 if (at_get(at) < CONNECTION_SWITCH_MAX) {
564 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
565 if (at_get(at) > CONNECTION_SWITCH_MAX)
566 at_reset(at, CONNECTION_SWITCH_MAX);
568 LASSERT(imp_conn->oic_last_attempt);
569 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
570 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
573 imp_conn->oic_last_attempt = ktime_get_seconds();
575 /* switch connection, don't mind if it's same as the current one */
576 if (imp->imp_connection)
577 ptlrpc_connection_put(imp->imp_connection);
578 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
580 dlmexp = class_conn2export(&imp->imp_dlm_handle);
581 LASSERT(dlmexp != NULL);
582 if (dlmexp->exp_connection)
583 ptlrpc_connection_put(dlmexp->exp_connection);
584 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
585 class_export_put(dlmexp);
587 if (imp->imp_conn_current != imp_conn) {
588 if (imp->imp_conn_current) {
589 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
590 &target_start, &target_len);
592 CDEBUG(D_HA, "%s: Connection changing to"
594 imp->imp_obd->obd_name,
595 target_len, target_start,
596 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
599 imp->imp_conn_current = imp_conn;
602 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
603 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
604 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
606 spin_unlock(&imp->imp_lock);
612 * must be called under imp_lock
614 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
616 struct ptlrpc_request *req;
617 struct list_head *tmp;
619 /* The requests in committed_list always have smaller transnos than
620 * the requests in replay_list */
621 if (!list_empty(&imp->imp_committed_list)) {
622 tmp = imp->imp_committed_list.next;
623 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
624 *transno = req->rq_transno;
625 if (req->rq_transno == 0) {
626 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
631 if (!list_empty(&imp->imp_replay_list)) {
632 tmp = imp->imp_replay_list.next;
633 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
634 *transno = req->rq_transno;
635 if (req->rq_transno == 0) {
636 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
645 * Attempt to (re)connect import \a imp. This includes all preparations,
646 * initializing CONNECT RPC request and passing it to ptlrpcd for
648 * Returns 0 on success or error code.
650 int ptlrpc_connect_import(struct obd_import *imp)
652 struct obd_device *obd = imp->imp_obd;
653 int initial_connect = 0;
655 __u64 committed_before_reconnect = 0;
656 struct ptlrpc_request *request;
657 struct obd_connect_data ocd;
658 char *bufs[] = { NULL,
659 obd2cli_tgt(imp->imp_obd),
661 (char *)&imp->imp_dlm_handle,
664 struct ptlrpc_connect_async_args *aa;
668 spin_lock(&imp->imp_lock);
669 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
670 spin_unlock(&imp->imp_lock);
671 CERROR("can't connect to a closed import\n");
673 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
674 spin_unlock(&imp->imp_lock);
675 CERROR("already connected\n");
677 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING ||
678 imp->imp_state == LUSTRE_IMP_EVICTED ||
679 imp->imp_connected) {
680 spin_unlock(&imp->imp_lock);
681 CERROR("already connecting\n");
685 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
688 imp->imp_resend_replay = 0;
690 if (!lustre_handle_is_used(&imp->imp_remote_handle))
693 committed_before_reconnect = imp->imp_peer_committed_transno;
695 set_transno = ptlrpc_first_transno(imp,
696 &imp->imp_connect_data.ocd_transno);
697 spin_unlock(&imp->imp_lock);
699 rc = import_select_connection(imp);
703 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
707 /* Reset connect flags to the originally requested flags, in case
708 * the server is updated on-the-fly we will get the new features. */
709 ocd = imp->imp_connect_data;
710 ocd.ocd_connect_flags = imp->imp_connect_flags_orig;
711 ocd.ocd_connect_flags2 = imp->imp_connect_flags2_orig;
712 /* Reset ocd_version each time so the server knows the exact versions */
713 ocd.ocd_version = LUSTRE_VERSION_CODE;
714 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
715 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
717 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
718 &obd->obd_uuid, &ocd, NULL);
722 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
724 GOTO(out, rc = -ENOMEM);
726 /* get SELinux policy info if any */
727 rc = sptlrpc_get_sepol(request);
729 ptlrpc_request_free(request);
733 bufs[5] = request->rq_sepol;
735 req_capsule_set_size(&request->rq_pill, &RMF_SELINUX_POL, RCL_CLIENT,
736 strlen(request->rq_sepol) ?
737 strlen(request->rq_sepol) + 1 : 0);
739 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
740 imp->imp_connect_op, bufs, NULL);
742 ptlrpc_request_free(request);
746 /* Report the rpc service time to the server so that it knows how long
747 * to wait for clients to join recovery */
748 lustre_msg_set_service_time(request->rq_reqmsg,
749 at_timeout2est(request->rq_timeout));
751 /* The amount of time we give the server to process the connect req.
752 * import_select_connection will increase the net latency on
753 * repeated reconnect attempts to cover slow networks.
754 * We override/ignore the server rpc completion estimate here,
755 * which may be large if this is a reconnect attempt */
756 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
757 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
759 request->rq_no_resend = request->rq_no_delay = 1;
760 request->rq_send_state = LUSTRE_IMP_CONNECTING;
761 /* Allow a slightly larger reply for future growth compatibility */
762 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
763 sizeof(struct obd_connect_data)+16*sizeof(__u64));
764 ptlrpc_request_set_replen(request);
765 request->rq_interpret_reply = ptlrpc_connect_interpret;
767 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
768 aa = ptlrpc_req_async_args(request);
769 memset(aa, 0, sizeof *aa);
771 aa->pcaa_peer_committed = committed_before_reconnect;
772 aa->pcaa_initial_connect = initial_connect;
774 if (aa->pcaa_initial_connect) {
775 spin_lock(&imp->imp_lock);
776 imp->imp_replayable = 1;
777 spin_unlock(&imp->imp_lock);
778 lustre_msg_add_op_flags(request->rq_reqmsg,
779 MSG_CONNECT_INITIAL);
783 lustre_msg_add_op_flags(request->rq_reqmsg,
784 MSG_CONNECT_TRANSNO);
786 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %ld)",
787 request->rq_timeout);
788 ptlrpcd_add_req(request);
792 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
796 EXPORT_SYMBOL(ptlrpc_connect_import);
798 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
802 spin_lock(&imp->imp_lock);
803 force_verify = imp->imp_force_verify != 0;
804 spin_unlock(&imp->imp_lock);
807 ptlrpc_pinger_wake_up();
810 static int ptlrpc_busy_reconnect(int rc)
812 return (rc == -EBUSY) || (rc == -EAGAIN);
815 static int ptlrpc_connect_set_flags(struct obd_import *imp,
816 struct obd_connect_data *ocd,
817 __u64 old_connect_flags,
818 struct obd_export *exp, int init_connect)
821 struct client_obd *cli = &imp->imp_obd->u.cli;
823 spin_lock(&imp->imp_lock);
824 list_del(&imp->imp_conn_current->oic_item);
825 list_add(&imp->imp_conn_current->oic_item,
826 &imp->imp_conn_list);
827 imp->imp_last_success_conn =
828 imp->imp_conn_current->oic_last_attempt;
830 spin_unlock(&imp->imp_lock);
832 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
833 (ocd->ocd_version > LUSTRE_VERSION_CODE +
834 LUSTRE_VERSION_OFFSET_WARN ||
835 ocd->ocd_version < LUSTRE_VERSION_CODE -
836 LUSTRE_VERSION_OFFSET_WARN)) {
837 /* Sigh, some compilers do not like #ifdef in the middle
838 of macro arguments */
839 const char *older = "older than client. "
840 "Consider upgrading server";
841 const char *newer = "newer than client. "
842 "Consider upgrading client";
844 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
846 obd2cli_tgt(imp->imp_obd),
847 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
848 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
849 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
850 OBD_OCD_VERSION_FIX(ocd->ocd_version),
851 ocd->ocd_version > LUSTRE_VERSION_CODE ?
852 newer : older, LUSTRE_VERSION_STRING);
856 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
857 /* We sent to the server ocd_cksum_types with bits set
858 * for algorithms we understand. The server masked off
859 * the checksum types it doesn't support */
860 if ((ocd->ocd_cksum_types &
861 obd_cksum_types_supported_client()) == 0) {
862 LCONSOLE_ERROR("The negotiation of the checksum "
863 "alogrithm to use with server %s "
865 obd2cli_tgt(imp->imp_obd),
866 ocd->ocd_cksum_types,
867 obd_cksum_types_supported_client());
870 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
873 /* The server does not support OBD_CONNECT_CKSUM.
874 * Enforce ADLER for backward compatibility*/
875 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
877 cli->cl_cksum_type = obd_cksum_type_select(imp->imp_obd->obd_name,
878 cli->cl_supp_cksum_types);
880 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
881 cli->cl_max_pages_per_rpc =
882 min(ocd->ocd_brw_size >> PAGE_SHIFT,
883 cli->cl_max_pages_per_rpc);
884 else if (imp->imp_connect_op == MDS_CONNECT ||
885 imp->imp_connect_op == MGS_CONNECT)
886 cli->cl_max_pages_per_rpc = 1;
888 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
889 (cli->cl_max_pages_per_rpc > 0));
891 client_adjust_max_dirty(cli);
893 /* Update client max modify RPCs in flight with value returned
895 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
896 cli->cl_max_mod_rpcs_in_flight = min(
897 cli->cl_max_mod_rpcs_in_flight,
898 ocd->ocd_maxmodrpcs);
900 cli->cl_max_mod_rpcs_in_flight = 1;
902 /* Reset ns_connect_flags only for initial connect. It might be
903 * changed in while using FS and if we reset it in reconnect
904 * this leads to losing user settings done before such as
905 * disable lru_resize, etc. */
906 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
907 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
911 ns->ns_connect_flags ^ ns->ns_orig_connect_flags;
912 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
913 "flags: %#llx\n", imp->imp_obd->obd_name,
914 ocd->ocd_connect_flags);
915 ns->ns_connect_flags = (ns->ns_connect_flags & changed_flags) |
916 (ocd->ocd_connect_flags & ~changed_flags);
917 ns->ns_orig_connect_flags = ocd->ocd_connect_flags;
920 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
921 /* We need a per-message support flag, because
922 * a. we don't know if the incoming connect reply
923 * supports AT or not (in reply_in_callback)
924 * until we unpack it.
925 * b. failovered server means export and flags are gone
926 * (in ptlrpc_send_reply).
927 * Can only be set when we know AT is supported at
929 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
931 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
933 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
939 * Add all replay requests back to unreplied list before start replay,
940 * so that we can make sure the known replied XID is always increased
941 * only even if when replaying requests.
943 static void ptlrpc_prepare_replay(struct obd_import *imp)
945 struct ptlrpc_request *req;
947 if (imp->imp_state != LUSTRE_IMP_REPLAY ||
948 imp->imp_resend_replay)
951 /* If the server was restart during repaly, the requests may
952 * have been added to the unreplied list in former replay. */
953 spin_lock(&imp->imp_lock);
955 list_for_each_entry(req, &imp->imp_committed_list, rq_replay_list) {
956 if (list_empty(&req->rq_unreplied_list))
957 ptlrpc_add_unreplied(req);
960 list_for_each_entry(req, &imp->imp_replay_list, rq_replay_list) {
961 if (list_empty(&req->rq_unreplied_list))
962 ptlrpc_add_unreplied(req);
965 imp->imp_known_replied_xid = ptlrpc_known_replied_xid(imp);
966 spin_unlock(&imp->imp_lock);
970 * interpret_reply callback for connect RPCs.
971 * Looks into returned status of connect operation and decides
972 * what to do with the import - i.e enter recovery, promote it to
973 * full state for normal operations of disconnect it due to an error.
975 static int ptlrpc_connect_interpret(const struct lu_env *env,
976 struct ptlrpc_request *request,
979 struct ptlrpc_connect_async_args *aa = data;
980 struct obd_import *imp = request->rq_import;
981 struct lustre_handle old_hdl;
982 __u64 old_connect_flags;
984 struct obd_connect_data *ocd;
985 struct obd_export *exp = NULL;
989 spin_lock(&imp->imp_lock);
990 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
991 imp->imp_connect_tried = 1;
992 spin_unlock(&imp->imp_lock);
996 imp->imp_connect_error = rc;
998 struct ptlrpc_request *free_req;
999 struct ptlrpc_request *tmp;
1001 /* abort all delayed requests initiated connection */
1002 list_for_each_entry_safe(free_req, tmp, &imp->imp_delayed_list,
1004 spin_lock(&free_req->rq_lock);
1005 if (free_req->rq_no_resend) {
1006 free_req->rq_err = 1;
1007 free_req->rq_status = -EIO;
1008 ptlrpc_client_wake_req(free_req);
1010 spin_unlock(&free_req->rq_lock);
1013 /* if this reconnect to busy export - not need select new target
1015 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
1016 spin_unlock(&imp->imp_lock);
1017 ptlrpc_maybe_ping_import_soon(imp);
1021 /* LU-7558: indicate that we are interpretting connect reply,
1022 * pltrpc_connect_import() will not try to reconnect until
1023 * interpret will finish. */
1024 imp->imp_connected = 1;
1025 spin_unlock(&imp->imp_lock);
1027 LASSERT(imp->imp_conn_current);
1029 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
1031 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
1033 /* server replied obd_connect_data is always bigger */
1034 ocd = req_capsule_server_sized_get(&request->rq_pill,
1035 &RMF_CONNECT_DATA, ret);
1038 CERROR("%s: no connect data from server\n",
1039 imp->imp_obd->obd_name);
1044 spin_lock(&imp->imp_lock);
1046 /* All imports are pingable */
1047 imp->imp_pingable = 1;
1048 imp->imp_force_reconnect = 0;
1049 imp->imp_force_verify = 0;
1051 imp->imp_connect_data = *ocd;
1053 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1054 imp->imp_obd->obd_name, ocd->ocd_instance);
1055 exp = class_conn2export(&imp->imp_dlm_handle);
1057 spin_unlock(&imp->imp_lock);
1060 /* This could happen if export is cleaned during the
1062 CERROR("%s: missing export after connect\n",
1063 imp->imp_obd->obd_name);
1064 GOTO(out, rc = -ENODEV);
1067 /* check that server granted subset of flags we asked for. */
1068 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1069 ocd->ocd_connect_flags) {
1070 CERROR("%s: Server didn't grant requested subset of flags: "
1071 "asked=%#llx granted=%#llx\n",
1072 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
1073 ocd->ocd_connect_flags);
1074 GOTO(out, rc = -EPROTO);
1077 if ((ocd->ocd_connect_flags2 & imp->imp_connect_flags2_orig) !=
1078 ocd->ocd_connect_flags2) {
1079 CERROR("%s: Server didn't grant requested subset of flags2: "
1080 "asked=%#llx granted=%#llx\n",
1081 imp->imp_obd->obd_name, imp->imp_connect_flags2_orig,
1082 ocd->ocd_connect_flags2);
1083 GOTO(out, rc = -EPROTO);
1086 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1087 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1088 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1089 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1090 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1091 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1092 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1094 /* We do not support the MDT-MDT interoperations with
1095 * different version MDT because of protocol changes. */
1096 if (unlikely(major != LUSTRE_MAJOR ||
1097 minor != LUSTRE_MINOR ||
1098 abs(patch - LUSTRE_PATCH) > 3)) {
1099 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1100 "connection to different version MDT "
1101 "(%d.%d.%d.%d) %s\n",
1102 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1103 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1104 major, minor, patch,
1105 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1106 imp->imp_connection->c_remote_uuid.uuid);
1108 GOTO(out, rc = -EPROTO);
1112 old_connect_flags = exp_connect_flags(exp);
1113 exp->exp_connect_data = *ocd;
1114 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1116 /* The net statistics after (re-)connect is not valid anymore,
1117 * because may reflect other routing, etc. */
1118 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1119 ptlrpc_at_adj_net_latency(request,
1120 lustre_msg_get_service_time(request->rq_repmsg));
1122 /* Import flags should be updated before waking import at FULL state */
1123 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1124 aa->pcaa_initial_connect);
1125 class_export_put(exp);
1131 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1133 if (aa->pcaa_initial_connect) {
1134 spin_lock(&imp->imp_lock);
1135 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1136 imp->imp_replayable = 1;
1137 spin_unlock(&imp->imp_lock);
1138 CDEBUG(D_HA, "connected to replayable target: %s\n",
1139 obd2cli_tgt(imp->imp_obd));
1141 imp->imp_replayable = 0;
1142 spin_unlock(&imp->imp_lock);
1145 /* if applies, adjust the imp->imp_msg_magic here
1146 * according to reply flags */
1148 imp->imp_remote_handle =
1149 *lustre_msg_get_handle(request->rq_repmsg);
1151 /* Initial connects are allowed for clients with non-random
1152 * uuids when servers are in recovery. Simply signal the
1153 * servers replay is complete and wait in REPLAY_WAIT. */
1154 if (msg_flags & MSG_CONNECT_RECOVERING) {
1155 CDEBUG(D_HA, "connect to %s during recovery\n",
1156 obd2cli_tgt(imp->imp_obd));
1157 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1159 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1160 ptlrpc_activate_import(imp);
1163 GOTO(finish, rc = 0);
1166 /* Determine what recovery state to move the import to. */
1167 if (MSG_CONNECT_RECONNECT & msg_flags) {
1168 memset(&old_hdl, 0, sizeof(old_hdl));
1169 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1170 sizeof (old_hdl))) {
1171 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1172 "bad handle %#llx\n",
1173 obd2cli_tgt(imp->imp_obd),
1174 imp->imp_connection->c_remote_uuid.uuid,
1175 imp->imp_dlm_handle.cookie);
1176 GOTO(out, rc = -ENOTCONN);
1179 if (memcmp(&imp->imp_remote_handle,
1180 lustre_msg_get_handle(request->rq_repmsg),
1181 sizeof(imp->imp_remote_handle))) {
1182 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1185 /* Bug 16611/14775: if server handle have changed,
1186 * that means some sort of disconnection happened.
1187 * If the server is not in recovery, that also means it
1188 * already erased all of our state because of previous
1189 * eviction. If it is in recovery - we are safe to
1190 * participate since we can reestablish all of our state
1191 * with server again */
1192 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1193 CDEBUG(level,"%s@%s changed server handle from "
1195 " but is still in recovery\n",
1196 obd2cli_tgt(imp->imp_obd),
1197 imp->imp_connection->c_remote_uuid.uuid,
1198 imp->imp_remote_handle.cookie,
1199 lustre_msg_get_handle(
1200 request->rq_repmsg)->cookie);
1202 LCONSOLE_WARN("Evicted from %s (at %s) "
1203 "after server handle changed from "
1205 obd2cli_tgt(imp->imp_obd),
1206 imp->imp_connection-> \
1208 imp->imp_remote_handle.cookie,
1209 lustre_msg_get_handle(
1210 request->rq_repmsg)->cookie);
1214 imp->imp_remote_handle =
1215 *lustre_msg_get_handle(request->rq_repmsg);
1217 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1218 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1219 GOTO(finish, rc = 0);
1223 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1224 obd2cli_tgt(imp->imp_obd),
1225 imp->imp_connection->c_remote_uuid.uuid);
1228 if (imp->imp_invalid) {
1229 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1230 "marking evicted\n", imp->imp_obd->obd_name);
1231 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1232 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1233 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1234 imp->imp_obd->obd_name,
1235 obd2cli_tgt(imp->imp_obd));
1237 spin_lock(&imp->imp_lock);
1238 imp->imp_resend_replay = 1;
1239 spin_unlock(&imp->imp_lock);
1241 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1243 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1245 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1246 LASSERT(imp->imp_replayable);
1247 imp->imp_remote_handle =
1248 *lustre_msg_get_handle(request->rq_repmsg);
1249 imp->imp_last_replay_transno = 0;
1250 imp->imp_replay_cursor = &imp->imp_committed_list;
1251 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1252 } else if ((ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0 &&
1253 !imp->imp_invalid) {
1255 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
1256 DEBUG_REQ(D_HA, request, "%s: lwp recover",
1257 imp->imp_obd->obd_name);
1258 imp->imp_remote_handle =
1259 *lustre_msg_get_handle(request->rq_repmsg);
1260 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1262 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1263 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1264 imp->imp_remote_handle =
1265 *lustre_msg_get_handle(request->rq_repmsg);
1266 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1269 /* Sanity checks for a reconnected import. */
1270 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1271 CERROR("imp_replayable flag does not match server "
1272 "after reconnect. We should LBUG right here.\n");
1275 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1276 lustre_msg_get_last_committed(request->rq_repmsg) <
1277 aa->pcaa_peer_committed) {
1278 CERROR("%s went back in time (transno %lld"
1279 " was previously committed, server now claims %lld"
1280 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1282 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1283 lustre_msg_get_last_committed(request->rq_repmsg));
1287 ptlrpc_prepare_replay(imp);
1288 rc = ptlrpc_import_recovery_state_machine(imp);
1289 if (rc == -ENOTCONN) {
1290 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1291 "invalidating and reconnecting\n",
1292 obd2cli_tgt(imp->imp_obd),
1293 imp->imp_connection->c_remote_uuid.uuid);
1294 ptlrpc_connect_import(imp);
1295 spin_lock(&imp->imp_lock);
1296 imp->imp_connected = 0;
1297 imp->imp_connect_tried = 1;
1298 spin_unlock(&imp->imp_lock);
1303 spin_lock(&imp->imp_lock);
1304 imp->imp_connected = 0;
1305 imp->imp_connect_tried = 1;
1306 spin_unlock(&imp->imp_lock);
1309 class_export_put(exp);
1312 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1313 if (rc == -EACCES) {
1315 * Give up trying to reconnect
1316 * EACCES means client has no permission for connection
1318 imp->imp_obd->obd_no_recov = 1;
1319 ptlrpc_deactivate_import(imp);
1322 if (rc == -EPROTO) {
1323 struct obd_connect_data *ocd;
1325 /* reply message might not be ready */
1326 if (request->rq_repmsg == NULL)
1329 ocd = req_capsule_server_get(&request->rq_pill,
1331 /* Servers are not supposed to refuse connections from
1332 * clients based on version, only connection feature
1333 * flags. We should never see this from llite, but it
1334 * may be useful for debugging in the future. */
1336 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1337 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1338 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1340 " refused connection from this client "
1341 "with an incompatible version (%s). "
1342 "Client must be recompiled\n",
1343 obd2cli_tgt(imp->imp_obd),
1344 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1345 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1346 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1347 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1348 LUSTRE_VERSION_STRING);
1349 ptlrpc_deactivate_import(imp);
1350 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1355 ptlrpc_maybe_ping_import_soon(imp);
1357 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1358 obd2cli_tgt(imp->imp_obd),
1359 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1362 wake_up_all(&imp->imp_recovery_waitq);
1367 * interpret callback for "completed replay" RPCs.
1368 * \see signal_completed_replay
1370 static int completed_replay_interpret(const struct lu_env *env,
1371 struct ptlrpc_request *req,
1375 atomic_dec(&req->rq_import->imp_replay_inflight);
1376 if (req->rq_status == 0 && !req->rq_import->imp_vbr_failed) {
1377 ptlrpc_import_recovery_state_machine(req->rq_import);
1379 if (req->rq_import->imp_vbr_failed) {
1381 "%s: version recovery fails, reconnecting\n",
1382 req->rq_import->imp_obd->obd_name);
1384 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1386 req->rq_import->imp_obd->obd_name,
1389 ptlrpc_connect_import(req->rq_import);
1396 * Let server know that we have no requests to replay anymore.
1397 * Achieved by just sending a PING request
1399 static int signal_completed_replay(struct obd_import *imp)
1401 struct ptlrpc_request *req;
1404 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1407 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1408 atomic_inc(&imp->imp_replay_inflight);
1410 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1413 atomic_dec(&imp->imp_replay_inflight);
1417 ptlrpc_request_set_replen(req);
1418 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1419 lustre_msg_add_flags(req->rq_reqmsg,
1420 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1422 req->rq_timeout *= 3;
1423 req->rq_interpret_reply = completed_replay_interpret;
1425 ptlrpcd_add_req(req);
1430 * In kernel code all import invalidation happens in its own
1431 * separate thread, so that whatever application happened to encounter
1432 * a problem could still be killed or otherwise continue
1434 static int ptlrpc_invalidate_import_thread(void *data)
1436 struct obd_import *imp = data;
1440 unshare_fs_struct();
1442 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1443 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1444 imp->imp_connection->c_remote_uuid.uuid);
1446 ptlrpc_invalidate_import(imp);
1448 if (obd_dump_on_eviction) {
1449 CERROR("dump the log upon eviction\n");
1450 libcfs_debug_dumplog();
1453 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1454 ptlrpc_import_recovery_state_machine(imp);
1456 class_import_put(imp);
1461 * This is the state machine for client-side recovery on import.
1463 * Typicaly we have two possibly paths. If we came to server and it is not
1464 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1465 * state and reconnect from scratch.
1466 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1467 * We go through our list of requests to replay and send them to server one by
1469 * After sending all request from the list we change import state to
1470 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1471 * and also all the locks we don't yet have and wait for server to grant us.
1472 * After that we send a special "replay completed" request and change import
1473 * state to IMP_REPLAY_WAIT.
1474 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1475 * state and resend all requests from sending list.
1476 * After that we promote import to FULL state and send all delayed requests
1477 * and import is fully operational after that.
1480 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1488 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1489 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1490 &target_start, &target_len);
1491 /* Don't care about MGC eviction */
1492 if (strcmp(imp->imp_obd->obd_type->typ_name,
1493 LUSTRE_MGC_NAME) != 0) {
1494 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1495 "by %.*s; in progress operations "
1496 "using this service will fail.\n",
1497 imp->imp_obd->obd_name, target_len,
1499 LASSERTF(!obd_lbug_on_eviction, "LBUG upon eviction");
1501 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1502 obd2cli_tgt(imp->imp_obd),
1503 imp->imp_connection->c_remote_uuid.uuid);
1504 /* reset vbr_failed flag upon eviction */
1505 spin_lock(&imp->imp_lock);
1506 imp->imp_vbr_failed = 0;
1507 spin_unlock(&imp->imp_lock);
1510 struct task_struct *task;
1511 /* bug 17802: XXX client_disconnect_export vs connect request
1512 * race. if client is evicted at this time then we start
1513 * invalidate thread without reference to import and import can
1514 * be freed at same time. */
1515 class_import_get(imp);
1516 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1519 class_import_put(imp);
1520 CERROR("error starting invalidate thread: %d\n", rc);
1529 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1530 CDEBUG(D_HA, "replay requested by %s\n",
1531 obd2cli_tgt(imp->imp_obd));
1532 rc = ptlrpc_replay_next(imp, &inflight);
1533 if (inflight == 0 &&
1534 atomic_read(&imp->imp_replay_inflight) == 0) {
1535 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1536 rc = ldlm_replay_locks(imp);
1543 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1544 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1545 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1546 rc = signal_completed_replay(imp);
1552 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1553 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1554 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1558 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1559 struct ptlrpc_connection *conn = imp->imp_connection;
1561 rc = ptlrpc_resend(imp);
1564 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1565 ptlrpc_activate_import(imp);
1567 LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
1568 imp->imp_obd->obd_name,
1569 obd_uuid2str(&conn->c_remote_uuid),
1570 obd_import_nid2str(imp));
1573 if (imp->imp_state == LUSTRE_IMP_FULL) {
1574 wake_up_all(&imp->imp_recovery_waitq);
1575 ptlrpc_wake_delayed(imp);
1582 static struct ptlrpc_request *ptlrpc_disconnect_prep_req(struct obd_import *imp)
1584 struct ptlrpc_request *req;
1588 switch (imp->imp_connect_op) {
1590 rq_opc = OST_DISCONNECT;
1593 rq_opc = MDS_DISCONNECT;
1596 rq_opc = MGS_DISCONNECT;
1600 CERROR("%s: don't know how to disconnect from %s "
1601 "(connect_op %d): rc = %d\n",
1602 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1603 imp->imp_connect_op, rc);
1604 RETURN(ERR_PTR(rc));
1607 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1608 LUSTRE_OBD_VERSION, rq_opc);
1612 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1613 * it fails. We can get through the above with a down server
1614 * if the client doesn't know the server is gone yet. */
1615 req->rq_no_resend = 1;
1617 /* We want client umounts to happen quickly, no matter the
1619 req->rq_timeout = min_t(int, req->rq_timeout,
1620 INITIAL_CONNECT_TIMEOUT);
1622 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1623 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1624 ptlrpc_request_set_replen(req);
1629 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1631 struct ptlrpc_request *req;
1635 if (imp->imp_obd->obd_force)
1636 GOTO(set_state, rc);
1638 /* probably the import has been disconnected already being idle */
1639 spin_lock(&imp->imp_lock);
1640 if (imp->imp_state == LUSTRE_IMP_IDLE)
1642 spin_unlock(&imp->imp_lock);
1644 if (ptlrpc_import_in_recovery(imp)) {
1645 struct l_wait_info lwi;
1646 long timeout_jiffies;
1650 if (imp->imp_server_timeout)
1651 timeout = obd_timeout >> 1;
1653 timeout = obd_timeout;
1658 req_portal = imp->imp_client->cli_request_portal;
1659 idx = import_at_get_index(imp, req_portal);
1660 timeout = at_get(&imp->imp_at.iat_service_estimate[idx]);
1663 timeout_jiffies = cfs_time_seconds(timeout);
1664 lwi = LWI_TIMEOUT_INTR(max_t(long, timeout_jiffies, 1),
1665 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1666 rc = l_wait_event(imp->imp_recovery_waitq,
1667 !ptlrpc_import_in_recovery(imp), &lwi);
1671 spin_lock(&imp->imp_lock);
1672 if (imp->imp_state != LUSTRE_IMP_FULL)
1674 spin_unlock(&imp->imp_lock);
1676 req = ptlrpc_disconnect_prep_req(imp);
1678 GOTO(set_state, rc = PTR_ERR(req));
1679 rc = ptlrpc_queue_wait(req);
1680 ptlrpc_req_finished(req);
1683 spin_lock(&imp->imp_lock);
1686 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1688 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1689 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1690 spin_unlock(&imp->imp_lock);
1692 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1696 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1698 static void ptlrpc_reset_reqs_generation(struct obd_import *imp)
1700 struct ptlrpc_request *old, *tmp;
1702 /* tag all resendable requests generated before disconnection
1703 * notice this code is part of disconnect-at-idle path only */
1704 list_for_each_entry_safe(old, tmp, &imp->imp_delayed_list,
1706 spin_lock(&old->rq_lock);
1707 if (old->rq_import_generation == imp->imp_generation - 1 &&
1709 old->rq_import_generation = imp->imp_generation;
1710 spin_unlock(&old->rq_lock);
1714 static int ptlrpc_disconnect_idle_interpret(const struct lu_env *env,
1715 struct ptlrpc_request *req,
1718 struct obd_import *imp = req->rq_import;
1721 DEBUG_REQ(D_HA, req, "inflight=%d, refcount=%d: rc = %d ",
1722 atomic_read(&imp->imp_inflight),
1723 atomic_read(&imp->imp_refcount), rc);
1725 spin_lock(&imp->imp_lock);
1726 /* DISCONNECT reply can be late and another connection can just
1727 * be initiated. so we have to abort disconnection. */
1728 if (req->rq_import_generation == imp->imp_generation &&
1729 imp->imp_state != LUSTRE_IMP_CLOSED) {
1730 LASSERTF(imp->imp_state == LUSTRE_IMP_CONNECTING,
1731 "%s\n", ptlrpc_import_state_name(imp->imp_state));
1732 imp->imp_state = LUSTRE_IMP_IDLE;
1733 memset(&imp->imp_remote_handle, 0,
1734 sizeof(imp->imp_remote_handle));
1735 /* take our DISCONNECT into account */
1736 if (atomic_read(&imp->imp_inflight) > 1) {
1737 imp->imp_generation++;
1738 imp->imp_initiated_at = imp->imp_generation;
1739 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_NEW);
1740 ptlrpc_reset_reqs_generation(imp);
1744 spin_unlock(&imp->imp_lock);
1747 rc = ptlrpc_connect_import(imp);
1749 ptlrpc_pinger_add_import(imp);
1755 int ptlrpc_disconnect_and_idle_import(struct obd_import *imp)
1757 struct ptlrpc_request *req;
1760 if (imp->imp_obd->obd_force)
1763 if (ptlrpc_import_in_recovery(imp))
1766 spin_lock(&imp->imp_lock);
1767 if (imp->imp_state != LUSTRE_IMP_FULL) {
1768 spin_unlock(&imp->imp_lock);
1771 spin_unlock(&imp->imp_lock);
1773 req = ptlrpc_disconnect_prep_req(imp);
1775 RETURN(PTR_ERR(req));
1777 CDEBUG_LIMIT(imp->imp_idle_debug, "%s: disconnect after %llus idle\n",
1778 imp->imp_obd->obd_name,
1779 ktime_get_real_seconds() - imp->imp_last_reply_time);
1780 req->rq_interpret_reply = ptlrpc_disconnect_idle_interpret;
1781 ptlrpcd_add_req(req);
1785 EXPORT_SYMBOL(ptlrpc_disconnect_and_idle_import);
1787 void ptlrpc_cleanup_imp(struct obd_import *imp)
1791 spin_lock(&imp->imp_lock);
1792 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1793 imp->imp_generation++;
1794 spin_unlock(&imp->imp_lock);
1795 ptlrpc_abort_inflight(imp);
1800 /* Adaptive Timeout utils */
1802 /* Update at_current with the specified value (bounded by at_min and at_max),
1803 * as well as the AT history "bins".
1804 * - Bin into timeslices using AT_BINS bins.
1805 * - This gives us a max of the last at_history seconds without the storage,
1806 * but still smoothing out a return to normalcy from a slow response.
1807 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1809 int at_measured(struct adaptive_timeout *at, unsigned int val)
1811 unsigned int old = at->at_current;
1812 time64_t now = ktime_get_real_seconds();
1813 long binlimit = max_t(long, at_history / AT_BINS, 1);
1816 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1817 val, at, (long)(now - at->at_binstart), at->at_current,
1818 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1821 /* 0's don't count, because we never want our timeout to
1822 drop to 0, and because 0 could mean an error */
1825 spin_lock(&at->at_lock);
1827 if (unlikely(at->at_binstart == 0)) {
1828 /* Special case to remove default from history */
1829 at->at_current = val;
1830 at->at_worst_ever = val;
1831 at->at_worst_time = now;
1832 at->at_hist[0] = val;
1833 at->at_binstart = now;
1834 } else if (now - at->at_binstart < binlimit ) {
1836 at->at_hist[0] = max(val, at->at_hist[0]);
1837 at->at_current = max(val, at->at_current);
1840 unsigned int maxv = val;
1842 /* move bins over */
1843 shift = (u32)(now - at->at_binstart) / binlimit;
1845 for(i = AT_BINS - 1; i >= 0; i--) {
1847 at->at_hist[i] = at->at_hist[i - shift];
1848 maxv = max(maxv, at->at_hist[i]);
1853 at->at_hist[0] = val;
1854 at->at_current = maxv;
1855 at->at_binstart += shift * binlimit;
1858 if (at->at_current > at->at_worst_ever) {
1859 at->at_worst_ever = at->at_current;
1860 at->at_worst_time = now;
1863 if (at->at_flags & AT_FLG_NOHIST)
1864 /* Only keep last reported val; keeping the rest of the history
1866 at->at_current = val;
1869 at->at_current = min(at->at_current, at_max);
1870 at->at_current = max(at->at_current, at_min);
1872 if (at->at_current != old)
1873 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1874 "(val=%u) hist %u %u %u %u\n", at,
1875 old, at->at_current, at->at_current - old, val,
1876 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1879 /* if we changed, report the old value */
1880 old = (at->at_current != old) ? old : 0;
1882 spin_unlock(&at->at_lock);
1886 /* Find the imp_at index for a given portal; assign if space available */
1887 int import_at_get_index(struct obd_import *imp, int portal)
1889 struct imp_at *at = &imp->imp_at;
1892 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1893 if (at->iat_portal[i] == portal)
1895 if (at->iat_portal[i] == 0)
1900 /* Not found in list, add it under a lock */
1901 spin_lock(&imp->imp_lock);
1903 /* Check unused under lock */
1904 for (; i < IMP_AT_MAX_PORTALS; i++) {
1905 if (at->iat_portal[i] == portal)
1907 if (at->iat_portal[i] == 0)
1912 /* Not enough portals? */
1913 LASSERT(i < IMP_AT_MAX_PORTALS);
1915 at->iat_portal[i] = portal;
1917 spin_unlock(&imp->imp_lock);