4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/import.c
38 * Author: Mike Shaver <shaver@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_RPC
43 #include <obd_support.h>
44 #include <lustre_ha.h>
45 #include <lustre_net.h>
46 #include <lustre_import.h>
47 #include <lustre_export.h>
49 #include <obd_cksum.h>
50 #include <obd_class.h>
52 #include "ptlrpc_internal.h"
54 struct ptlrpc_connect_async_args {
55 __u64 pcaa_peer_committed;
56 int pcaa_initial_connect;
60 * Updates import \a imp current state to provided \a state value
61 * Helper function. Must be called under imp_lock.
63 static void __import_set_state(struct obd_import *imp,
64 enum lustre_imp_state state)
67 case LUSTRE_IMP_CLOSED:
69 case LUSTRE_IMP_DISCON:
70 case LUSTRE_IMP_CONNECTING:
72 case LUSTRE_IMP_REPLAY_WAIT:
73 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
76 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
78 imp->imp_state = state;
79 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
80 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
81 cfs_time_current_sec();
82 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
86 /* A CLOSED import should remain so. */
87 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
89 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
90 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
91 imp, obd2cli_tgt(imp->imp_obd), \
92 ptlrpc_import_state_name(imp->imp_state), \
93 ptlrpc_import_state_name(state)); \
94 __import_set_state(imp, state); \
98 #define IMPORT_SET_STATE(imp, state) \
100 spin_lock(&imp->imp_lock); \
101 IMPORT_SET_STATE_NOLOCK(imp, state); \
102 spin_unlock(&imp->imp_lock); \
106 static int ptlrpc_connect_interpret(const struct lu_env *env,
107 struct ptlrpc_request *request,
108 void * data, int rc);
109 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
111 /* Only this function is allowed to change the import state when it is
112 * CLOSED. I would rather refcount the import and free it after
113 * disconnection like we do with exports. To do that, the client_obd
114 * will need to save the peer info somewhere other than in the import,
116 int ptlrpc_init_import(struct obd_import *imp)
118 spin_lock(&imp->imp_lock);
120 imp->imp_generation++;
121 imp->imp_state = LUSTRE_IMP_NEW;
123 spin_unlock(&imp->imp_lock);
127 EXPORT_SYMBOL(ptlrpc_init_import);
129 #define UUID_STR "_UUID"
130 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
132 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
133 ? uuid : uuid + strlen(prefix);
135 *uuid_len = strlen(*uuid_start);
137 if (*uuid_len < strlen(UUID_STR))
140 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
141 UUID_STR, strlen(UUID_STR)))
142 *uuid_len -= strlen(UUID_STR);
146 * Returns true if import was FULL, false if import was already not
148 * @imp - import to be disconnected
149 * @conn_cnt - connection count (epoch) of the request that timed out
150 * and caused the disconnection. In some cases, multiple
151 * inflight requests can fail to a single target (e.g. OST
152 * bulk requests) and if one has already caused a reconnection
153 * (increasing the import->conn_cnt) the older failure should
154 * not also cause a reconnection. If zero it forces a reconnect.
156 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
160 spin_lock(&imp->imp_lock);
162 if (imp->imp_state == LUSTRE_IMP_FULL &&
163 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
167 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
168 &target_start, &target_len);
170 if (imp->imp_replayable) {
171 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
172 "lost; in progress operations using this "
173 "service will wait for recovery to complete\n",
174 imp->imp_obd->obd_name, target_len, target_start,
175 libcfs_nid2str(imp->imp_connection->c_peer.nid));
177 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
178 "%.*s (at %s) was lost; in progress "
179 "operations using this service will fail\n",
180 imp->imp_obd->obd_name,
181 target_len, target_start,
182 libcfs_nid2str(imp->imp_connection->c_peer.nid));
184 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
185 spin_unlock(&imp->imp_lock);
187 if (obd_dump_on_timeout)
188 libcfs_debug_dumplog();
190 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
193 spin_unlock(&imp->imp_lock);
194 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
195 imp->imp_client->cli_name, imp,
196 (imp->imp_state == LUSTRE_IMP_FULL &&
197 imp->imp_conn_cnt > conn_cnt) ?
198 "reconnected" : "not connected", imp->imp_conn_cnt,
199 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
205 /* Must be called with imp_lock held! */
206 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
209 assert_spin_locked(&imp->imp_lock);
211 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
212 imp->imp_invalid = 1;
213 imp->imp_generation++;
214 spin_unlock(&imp->imp_lock);
216 ptlrpc_abort_inflight(imp);
217 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
223 * This acts as a barrier; all existing requests are rejected, and
224 * no new requests will be accepted until the import is valid again.
226 void ptlrpc_deactivate_import(struct obd_import *imp)
228 spin_lock(&imp->imp_lock);
229 ptlrpc_deactivate_and_unlock_import(imp);
231 EXPORT_SYMBOL(ptlrpc_deactivate_import);
234 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
238 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
239 (req->rq_phase == RQ_PHASE_BULK) ||
240 (req->rq_phase == RQ_PHASE_NEW)))
243 if (req->rq_timedout)
246 if (req->rq_phase == RQ_PHASE_NEW)
249 dl = req->rq_deadline;
257 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
259 time_t now = cfs_time_current_sec();
260 struct list_head *tmp, *n;
261 struct ptlrpc_request *req;
262 unsigned int timeout = 0;
264 spin_lock(&imp->imp_lock);
265 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
266 req = list_entry(tmp, struct ptlrpc_request, rq_list);
267 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
269 spin_unlock(&imp->imp_lock);
274 * This function will invalidate the import, if necessary, then block
275 * for all the RPC completions, and finally notify the obd to
276 * invalidate its state (ie cancel locks, clear pending requests,
279 void ptlrpc_invalidate_import(struct obd_import *imp)
281 struct list_head *tmp, *n;
282 struct ptlrpc_request *req;
283 struct l_wait_info lwi;
284 unsigned int timeout;
287 atomic_inc(&imp->imp_inval_count);
289 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
290 ptlrpc_deactivate_import(imp);
292 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
293 LASSERT(imp->imp_invalid);
295 /* Wait forever until inflight == 0. We really can't do it another
296 * way because in some cases we need to wait for very long reply
297 * unlink. We can't do anything before that because there is really
298 * no guarantee that some rdma transfer is not in progress right now. */
300 /* Calculate max timeout for waiting on rpcs to error
301 * out. Use obd_timeout if calculated value is smaller
303 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
304 timeout = ptlrpc_inflight_timeout(imp);
305 timeout += timeout / 3;
308 timeout = obd_timeout;
310 /* decrease the interval to increase race condition */
314 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
317 /* Wait for all requests to error out and call completion
318 * callbacks. Cap it at obd_timeout -- these should all
319 * have been locally cancelled by ptlrpc_abort_inflight. */
320 lwi = LWI_TIMEOUT_INTERVAL(
321 cfs_timeout_cap(cfs_time_seconds(timeout)),
322 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
324 rc = l_wait_event(imp->imp_recovery_waitq,
325 (atomic_read(&imp->imp_inflight) == 0),
328 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
330 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
331 cli_tgt, rc, atomic_read(&imp->imp_inflight));
333 spin_lock(&imp->imp_lock);
334 if (atomic_read(&imp->imp_inflight) == 0) {
335 int count = atomic_read(&imp->imp_unregistering);
337 /* We know that "unregistering" rpcs only can
338 * survive in sending or delaying lists (they
339 * maybe waiting for long reply unlink in
340 * sluggish nets). Let's check this. If there
341 * is no inflight and unregistering != 0, this
343 LASSERTF(count == 0, "Some RPCs are still "
344 "unregistering: %d\n", count);
346 /* Let's save one loop as soon as inflight have
347 * dropped to zero. No new inflights possible at
351 list_for_each_safe(tmp, n,
352 &imp->imp_sending_list) {
353 req = list_entry(tmp,
354 struct ptlrpc_request,
356 DEBUG_REQ(D_ERROR, req,
357 "still on sending list");
359 list_for_each_safe(tmp, n,
360 &imp->imp_delayed_list) {
361 req = list_entry(tmp,
362 struct ptlrpc_request,
364 DEBUG_REQ(D_ERROR, req,
365 "still on delayed list");
368 CERROR("%s: RPCs in \"%s\" phase found (%d). "
369 "Network is sluggish? Waiting them "
370 "to error out.\n", cli_tgt,
371 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
372 atomic_read(&imp->imp_unregistering));
374 spin_unlock(&imp->imp_lock);
379 * Let's additionally check that no new rpcs added to import in
380 * "invalidate" state.
382 LASSERT(atomic_read(&imp->imp_inflight) == 0);
383 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
384 sptlrpc_import_flush_all_ctx(imp);
386 atomic_dec(&imp->imp_inval_count);
387 wake_up_all(&imp->imp_recovery_waitq);
389 EXPORT_SYMBOL(ptlrpc_invalidate_import);
391 /* unset imp_invalid */
392 void ptlrpc_activate_import(struct obd_import *imp)
394 struct obd_device *obd = imp->imp_obd;
396 spin_lock(&imp->imp_lock);
397 if (imp->imp_deactive != 0) {
398 spin_unlock(&imp->imp_lock);
402 imp->imp_invalid = 0;
403 spin_unlock(&imp->imp_lock);
404 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
406 EXPORT_SYMBOL(ptlrpc_activate_import);
408 static void ptlrpc_pinger_force(struct obd_import *imp)
410 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
411 ptlrpc_import_state_name(imp->imp_state));
413 spin_lock(&imp->imp_lock);
414 imp->imp_force_verify = 1;
415 spin_unlock(&imp->imp_lock);
417 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
418 ptlrpc_pinger_wake_up();
421 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
425 LASSERT(!imp->imp_dlm_fake);
427 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
428 if (!imp->imp_replayable) {
429 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
430 "auto-deactivating\n",
431 obd2cli_tgt(imp->imp_obd),
432 imp->imp_connection->c_remote_uuid.uuid,
433 imp->imp_obd->obd_name);
434 ptlrpc_deactivate_import(imp);
437 ptlrpc_pinger_force(imp);
442 int ptlrpc_reconnect_import(struct obd_import *imp)
445 struct l_wait_info lwi;
446 int secs = cfs_time_seconds(obd_timeout);
449 ptlrpc_pinger_force(imp);
451 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
452 obd2cli_tgt(imp->imp_obd), secs);
454 lwi = LWI_TIMEOUT(secs, NULL, NULL);
455 rc = l_wait_event(imp->imp_recovery_waitq,
456 !ptlrpc_import_in_recovery(imp), &lwi);
457 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
458 ptlrpc_import_state_name(imp->imp_state));
461 ptlrpc_set_import_discon(imp, 0);
462 /* Force a new connect attempt */
463 ptlrpc_invalidate_import(imp);
464 /* Do a fresh connect next time by zeroing the handle */
465 ptlrpc_disconnect_import(imp, 1);
466 /* Wait for all invalidate calls to finish */
467 if (atomic_read(&imp->imp_inval_count) > 0) {
468 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
471 rc = l_wait_event(imp->imp_recovery_waitq,
472 (atomic_read(&imp->imp_inval_count) == 0),
475 CERROR("Interrupted, inval=%d\n",
476 atomic_read(&imp->imp_inval_count));
479 /* Allow reconnect attempts */
480 imp->imp_obd->obd_no_recov = 0;
481 /* Remove 'invalid' flag */
482 ptlrpc_activate_import(imp);
483 /* Attempt a new connect */
484 ptlrpc_recover_import(imp, NULL, 0);
488 EXPORT_SYMBOL(ptlrpc_reconnect_import);
491 * Connection on import \a imp is changed to another one (if more than one is
492 * present). We typically chose connection that we have not tried to connect to
495 static int import_select_connection(struct obd_import *imp)
497 struct obd_import_conn *imp_conn = NULL, *conn;
498 struct obd_export *dlmexp;
500 int target_len, tried_all = 1;
503 spin_lock(&imp->imp_lock);
505 if (list_empty(&imp->imp_conn_list)) {
506 CERROR("%s: no connections available\n",
507 imp->imp_obd->obd_name);
508 spin_unlock(&imp->imp_lock);
512 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
513 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
514 imp->imp_obd->obd_name,
515 libcfs_nid2str(conn->oic_conn->c_peer.nid),
516 conn->oic_last_attempt);
518 /* If we have not tried this connection since
519 the last successful attempt, go with this one */
520 if ((conn->oic_last_attempt == 0) ||
521 cfs_time_beforeq_64(conn->oic_last_attempt,
522 imp->imp_last_success_conn)) {
528 /* If all of the connections have already been tried
529 since the last successful connection; just choose the
530 least recently used */
533 else if (cfs_time_before_64(conn->oic_last_attempt,
534 imp_conn->oic_last_attempt))
538 /* if not found, simply choose the current one */
539 if (!imp_conn || imp->imp_force_reconnect) {
540 LASSERT(imp->imp_conn_current);
541 imp_conn = imp->imp_conn_current;
544 LASSERT(imp_conn->oic_conn);
546 /* If we've tried everything, and we're back to the beginning of the
547 list, increase our timeout and try again. It will be reset when
548 we do finally connect. (FIXME: really we should wait for all network
549 state associated with the last connection attempt to drain before
550 trying to reconnect on it.) */
551 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
552 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
553 if (at_get(at) < CONNECTION_SWITCH_MAX) {
554 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
555 if (at_get(at) > CONNECTION_SWITCH_MAX)
556 at_reset(at, CONNECTION_SWITCH_MAX);
558 LASSERT(imp_conn->oic_last_attempt);
559 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
560 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
563 imp_conn->oic_last_attempt = cfs_time_current_64();
565 /* switch connection, don't mind if it's same as the current one */
566 if (imp->imp_connection)
567 ptlrpc_connection_put(imp->imp_connection);
568 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
570 dlmexp = class_conn2export(&imp->imp_dlm_handle);
571 LASSERT(dlmexp != NULL);
572 if (dlmexp->exp_connection)
573 ptlrpc_connection_put(dlmexp->exp_connection);
574 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
575 class_export_put(dlmexp);
577 if (imp->imp_conn_current != imp_conn) {
578 if (imp->imp_conn_current) {
579 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
580 &target_start, &target_len);
582 CDEBUG(D_HA, "%s: Connection changing to"
584 imp->imp_obd->obd_name,
585 target_len, target_start,
586 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
589 imp->imp_conn_current = imp_conn;
592 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
593 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
594 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
596 spin_unlock(&imp->imp_lock);
602 * must be called under imp_lock
604 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
606 struct ptlrpc_request *req;
607 struct list_head *tmp;
609 /* The requests in committed_list always have smaller transnos than
610 * the requests in replay_list */
611 if (!list_empty(&imp->imp_committed_list)) {
612 tmp = imp->imp_committed_list.next;
613 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
614 *transno = req->rq_transno;
615 if (req->rq_transno == 0) {
616 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
621 if (!list_empty(&imp->imp_replay_list)) {
622 tmp = imp->imp_replay_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 replay_list");
635 * Attempt to (re)connect import \a imp. This includes all preparations,
636 * initializing CONNECT RPC request and passing it to ptlrpcd for
638 * Returns 0 on success or error code.
640 int ptlrpc_connect_import(struct obd_import *imp)
642 struct obd_device *obd = imp->imp_obd;
643 int initial_connect = 0;
645 __u64 committed_before_reconnect = 0;
646 struct ptlrpc_request *request;
647 char *bufs[] = { NULL,
648 obd2cli_tgt(imp->imp_obd),
650 (char *)&imp->imp_dlm_handle,
651 (char *)&imp->imp_connect_data };
652 struct ptlrpc_connect_async_args *aa;
656 spin_lock(&imp->imp_lock);
657 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
658 spin_unlock(&imp->imp_lock);
659 CERROR("can't connect to a closed import\n");
661 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
662 spin_unlock(&imp->imp_lock);
663 CERROR("already connected\n");
665 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
666 spin_unlock(&imp->imp_lock);
667 CERROR("already connecting\n");
671 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
674 imp->imp_resend_replay = 0;
676 if (!lustre_handle_is_used(&imp->imp_remote_handle))
679 committed_before_reconnect = imp->imp_peer_committed_transno;
681 set_transno = ptlrpc_first_transno(imp,
682 &imp->imp_connect_data.ocd_transno);
683 spin_unlock(&imp->imp_lock);
685 rc = import_select_connection(imp);
689 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
693 /* Reset connect flags to the originally requested flags, in case
694 * the server is updated on-the-fly we will get the new features. */
695 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
696 /* Reset ocd_version each time so the server knows the exact versions */
697 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
698 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
699 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
701 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
702 &obd->obd_uuid, &imp->imp_connect_data, NULL);
706 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
708 GOTO(out, rc = -ENOMEM);
710 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
711 imp->imp_connect_op, bufs, NULL);
713 ptlrpc_request_free(request);
717 /* Report the rpc service time to the server so that it knows how long
718 * to wait for clients to join recovery */
719 lustre_msg_set_service_time(request->rq_reqmsg,
720 at_timeout2est(request->rq_timeout));
722 /* The amount of time we give the server to process the connect req.
723 * import_select_connection will increase the net latency on
724 * repeated reconnect attempts to cover slow networks.
725 * We override/ignore the server rpc completion estimate here,
726 * which may be large if this is a reconnect attempt */
727 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
728 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
730 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
732 request->rq_no_resend = request->rq_no_delay = 1;
733 request->rq_send_state = LUSTRE_IMP_CONNECTING;
734 /* Allow a slightly larger reply for future growth compatibility */
735 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
736 sizeof(struct obd_connect_data)+16*sizeof(__u64));
737 ptlrpc_request_set_replen(request);
738 request->rq_interpret_reply = ptlrpc_connect_interpret;
740 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
741 aa = ptlrpc_req_async_args(request);
742 memset(aa, 0, sizeof *aa);
744 aa->pcaa_peer_committed = committed_before_reconnect;
745 aa->pcaa_initial_connect = initial_connect;
747 if (aa->pcaa_initial_connect) {
748 spin_lock(&imp->imp_lock);
749 imp->imp_replayable = 1;
750 spin_unlock(&imp->imp_lock);
751 lustre_msg_add_op_flags(request->rq_reqmsg,
752 MSG_CONNECT_INITIAL);
756 lustre_msg_add_op_flags(request->rq_reqmsg,
757 MSG_CONNECT_TRANSNO);
759 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
760 request->rq_timeout);
761 ptlrpcd_add_req(request, PDL_POLICY_ROUND, -1);
765 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
770 EXPORT_SYMBOL(ptlrpc_connect_import);
772 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
776 spin_lock(&imp->imp_lock);
777 force_verify = imp->imp_force_verify != 0;
778 spin_unlock(&imp->imp_lock);
781 ptlrpc_pinger_wake_up();
784 static int ptlrpc_busy_reconnect(int rc)
786 return (rc == -EBUSY) || (rc == -EAGAIN);
789 static int ptlrpc_connect_set_flags(struct obd_import *imp,
790 struct obd_connect_data *ocd,
791 __u64 old_connect_flags,
792 struct obd_export *exp, int init_connect)
795 struct client_obd *cli = &imp->imp_obd->u.cli;
797 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
798 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
799 LCONSOLE_WARN("%s: MDS %s does not support ibits "
800 "lock, either very old or invalid: "
801 "requested "LPX64", replied "LPX64"\n",
802 imp->imp_obd->obd_name,
803 imp->imp_connection->c_remote_uuid.uuid,
804 imp->imp_connect_flags_orig,
805 ocd->ocd_connect_flags);
809 spin_lock(&imp->imp_lock);
810 list_del(&imp->imp_conn_current->oic_item);
811 list_add(&imp->imp_conn_current->oic_item,
812 &imp->imp_conn_list);
813 imp->imp_last_success_conn =
814 imp->imp_conn_current->oic_last_attempt;
816 spin_unlock(&imp->imp_lock);
819 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
820 (ocd->ocd_version > LUSTRE_VERSION_CODE +
821 LUSTRE_VERSION_OFFSET_WARN ||
822 ocd->ocd_version < LUSTRE_VERSION_CODE -
823 LUSTRE_VERSION_OFFSET_WARN)) {
824 /* Sigh, some compilers do not like #ifdef in the middle
825 of macro arguments */
826 const char *older = "older than client. "
827 "Consider upgrading server";
828 const char *newer = "newer than client. "
829 "Consider recompiling application";
831 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
833 obd2cli_tgt(imp->imp_obd),
834 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
835 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
836 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
837 OBD_OCD_VERSION_FIX(ocd->ocd_version),
838 ocd->ocd_version > LUSTRE_VERSION_CODE ?
839 newer : older, LUSTRE_VERSION_STRING);
843 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
844 /* Check if server has LU-1252 fix applied to not always swab
845 * the IR MNE entries. Do this only once per connection. This
846 * fixup is version-limited, because we don't want to carry the
847 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
848 * need interop with unpatched 2.2 servers. For newer servers,
849 * the client will do MNE swabbing only as needed. LU-1644 */
850 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
851 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
852 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
853 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
854 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
855 strcmp(imp->imp_obd->obd_type->typ_name,
856 LUSTRE_MGC_NAME) == 0))
857 imp->imp_need_mne_swab = 1;
858 else /* clear if server was upgraded since last connect */
859 imp->imp_need_mne_swab = 0;
862 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
863 /* We sent to the server ocd_cksum_types with bits set
864 * for algorithms we understand. The server masked off
865 * the checksum types it doesn't support */
866 if ((ocd->ocd_cksum_types &
867 cksum_types_supported_client()) == 0) {
868 LCONSOLE_WARN("The negotiation of the checksum "
869 "alogrithm to use with server %s "
870 "failed (%x/%x), disabling "
872 obd2cli_tgt(imp->imp_obd),
873 ocd->ocd_cksum_types,
874 cksum_types_supported_client());
875 cli->cl_checksum = 0;
876 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
878 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
881 /* The server does not support OBD_CONNECT_CKSUM.
882 * Enforce ADLER for backward compatibility*/
883 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
885 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
887 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
888 cli->cl_max_pages_per_rpc =
889 min(ocd->ocd_brw_size >> PAGE_CACHE_SHIFT,
890 cli->cl_max_pages_per_rpc);
891 else if (imp->imp_connect_op == MDS_CONNECT ||
892 imp->imp_connect_op == MGS_CONNECT)
893 cli->cl_max_pages_per_rpc = 1;
895 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
896 (cli->cl_max_pages_per_rpc > 0));
898 client_adjust_max_dirty(cli);
901 /* Reset ns_connect_flags only for initial connect. It might be
902 * changed in while using FS and if we reset it in reconnect
903 * this leads to losing user settings done before such as
904 * disable lru_resize, etc. */
905 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
906 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
907 "flags: "LPX64"\n", imp->imp_obd->obd_name,
908 ocd->ocd_connect_flags);
909 imp->imp_obd->obd_namespace->ns_connect_flags =
910 ocd->ocd_connect_flags;
911 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
912 ocd->ocd_connect_flags;
915 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
916 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
917 /* We need a per-message support flag, because
918 * a. we don't know if the incoming connect reply
919 * supports AT or not (in reply_in_callback)
920 * until we unpack it.
921 * b. failovered server means export and flags are gone
922 * (in ptlrpc_send_reply).
923 * Can only be set when we know AT is supported at
925 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
927 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
929 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
930 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
931 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
933 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
939 * interpret_reply callback for connect RPCs.
940 * Looks into returned status of connect operation and decides
941 * what to do with the import - i.e enter recovery, promote it to
942 * full state for normal operations of disconnect it due to an error.
944 static int ptlrpc_connect_interpret(const struct lu_env *env,
945 struct ptlrpc_request *request,
948 struct ptlrpc_connect_async_args *aa = data;
949 struct obd_import *imp = request->rq_import;
950 struct lustre_handle old_hdl;
951 __u64 old_connect_flags;
953 struct obd_connect_data *ocd;
954 struct obd_export *exp;
958 spin_lock(&imp->imp_lock);
959 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
960 imp->imp_connect_tried = 1;
961 spin_unlock(&imp->imp_lock);
966 /* if this reconnect to busy export - not need select new target
968 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
969 spin_unlock(&imp->imp_lock);
970 ptlrpc_maybe_ping_import_soon(imp);
973 spin_unlock(&imp->imp_lock);
975 LASSERT(imp->imp_conn_current);
977 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
979 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
981 /* server replied obd_connect_data is always bigger */
982 ocd = req_capsule_server_sized_get(&request->rq_pill,
983 &RMF_CONNECT_DATA, ret);
986 CERROR("%s: no connect data from server\n",
987 imp->imp_obd->obd_name);
992 spin_lock(&imp->imp_lock);
994 /* All imports are pingable */
995 imp->imp_pingable = 1;
996 imp->imp_force_reconnect = 0;
997 imp->imp_force_verify = 0;
999 imp->imp_connect_data = *ocd;
1001 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1002 imp->imp_obd->obd_name, ocd->ocd_instance);
1003 exp = class_conn2export(&imp->imp_dlm_handle);
1005 spin_unlock(&imp->imp_lock);
1007 /* check that server granted subset of flags we asked for. */
1008 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1009 ocd->ocd_connect_flags) {
1010 CERROR("%s: Server didn't granted asked subset of flags: "
1011 "asked="LPX64" grranted="LPX64"\n",
1012 imp->imp_obd->obd_name,imp->imp_connect_flags_orig,
1013 ocd->ocd_connect_flags);
1014 GOTO(out, rc = -EPROTO);
1018 /* This could happen if export is cleaned during the
1020 CERROR("%s: missing export after connect\n",
1021 imp->imp_obd->obd_name);
1022 GOTO(out, rc = -ENODEV);
1024 old_connect_flags = exp_connect_flags(exp);
1025 exp->exp_connect_data = *ocd;
1026 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1028 /* The net statistics after (re-)connect is not valid anymore,
1029 * because may reflect other routing, etc. */
1030 at_init(&imp->imp_at.iat_net_latency, 0, 0);
1031 ptlrpc_at_adj_net_latency(request,
1032 lustre_msg_get_service_time(request->rq_repmsg));
1034 /* Import flags should be updated before waking import at FULL state */
1035 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1036 aa->pcaa_initial_connect);
1037 class_export_put(exp);
1041 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1043 if (aa->pcaa_initial_connect) {
1044 spin_lock(&imp->imp_lock);
1045 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1046 imp->imp_replayable = 1;
1047 spin_unlock(&imp->imp_lock);
1048 CDEBUG(D_HA, "connected to replayable target: %s\n",
1049 obd2cli_tgt(imp->imp_obd));
1051 imp->imp_replayable = 0;
1052 spin_unlock(&imp->imp_lock);
1055 /* if applies, adjust the imp->imp_msg_magic here
1056 * according to reply flags */
1058 imp->imp_remote_handle =
1059 *lustre_msg_get_handle(request->rq_repmsg);
1061 /* Initial connects are allowed for clients with non-random
1062 * uuids when servers are in recovery. Simply signal the
1063 * servers replay is complete and wait in REPLAY_WAIT. */
1064 if (msg_flags & MSG_CONNECT_RECOVERING) {
1065 CDEBUG(D_HA, "connect to %s during recovery\n",
1066 obd2cli_tgt(imp->imp_obd));
1067 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1069 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1070 ptlrpc_activate_import(imp);
1073 GOTO(finish, rc = 0);
1076 /* Determine what recovery state to move the import to. */
1077 if (MSG_CONNECT_RECONNECT & msg_flags) {
1078 memset(&old_hdl, 0, sizeof(old_hdl));
1079 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1080 sizeof (old_hdl))) {
1081 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1082 "bad handle "LPX64"\n",
1083 obd2cli_tgt(imp->imp_obd),
1084 imp->imp_connection->c_remote_uuid.uuid,
1085 imp->imp_dlm_handle.cookie);
1086 GOTO(out, rc = -ENOTCONN);
1089 if (memcmp(&imp->imp_remote_handle,
1090 lustre_msg_get_handle(request->rq_repmsg),
1091 sizeof(imp->imp_remote_handle))) {
1092 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1095 /* Bug 16611/14775: if server handle have changed,
1096 * that means some sort of disconnection happened.
1097 * If the server is not in recovery, that also means it
1098 * already erased all of our state because of previous
1099 * eviction. If it is in recovery - we are safe to
1100 * participate since we can reestablish all of our state
1101 * with server again */
1102 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1103 CDEBUG(level,"%s@%s changed server handle from "
1105 " but is still in recovery\n",
1106 obd2cli_tgt(imp->imp_obd),
1107 imp->imp_connection->c_remote_uuid.uuid,
1108 imp->imp_remote_handle.cookie,
1109 lustre_msg_get_handle(
1110 request->rq_repmsg)->cookie);
1112 LCONSOLE_WARN("Evicted from %s (at %s) "
1113 "after server handle changed from "
1114 LPX64" to "LPX64"\n",
1115 obd2cli_tgt(imp->imp_obd),
1116 imp->imp_connection-> \
1118 imp->imp_remote_handle.cookie,
1119 lustre_msg_get_handle(
1120 request->rq_repmsg)->cookie);
1124 imp->imp_remote_handle =
1125 *lustre_msg_get_handle(request->rq_repmsg);
1127 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1128 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1129 GOTO(finish, rc = 0);
1133 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1134 obd2cli_tgt(imp->imp_obd),
1135 imp->imp_connection->c_remote_uuid.uuid);
1138 if (imp->imp_invalid) {
1139 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1140 "marking evicted\n", imp->imp_obd->obd_name);
1141 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1142 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1143 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1144 imp->imp_obd->obd_name,
1145 obd2cli_tgt(imp->imp_obd));
1147 spin_lock(&imp->imp_lock);
1148 imp->imp_resend_replay = 1;
1149 spin_unlock(&imp->imp_lock);
1151 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1153 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1155 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1156 LASSERT(imp->imp_replayable);
1157 imp->imp_remote_handle =
1158 *lustre_msg_get_handle(request->rq_repmsg);
1159 imp->imp_last_replay_transno = 0;
1160 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1162 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1163 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1164 imp->imp_remote_handle =
1165 *lustre_msg_get_handle(request->rq_repmsg);
1166 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1169 /* Sanity checks for a reconnected import. */
1170 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1171 CERROR("imp_replayable flag does not match server "
1172 "after reconnect. We should LBUG right here.\n");
1175 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1176 lustre_msg_get_last_committed(request->rq_repmsg) <
1177 aa->pcaa_peer_committed) {
1178 CERROR("%s went back in time (transno "LPD64
1179 " was previously committed, server now claims "LPD64
1180 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1182 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1183 lustre_msg_get_last_committed(request->rq_repmsg));
1187 rc = ptlrpc_import_recovery_state_machine(imp);
1188 if (rc == -ENOTCONN) {
1189 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1190 "invalidating and reconnecting\n",
1191 obd2cli_tgt(imp->imp_obd),
1192 imp->imp_connection->c_remote_uuid.uuid);
1193 ptlrpc_connect_import(imp);
1194 imp->imp_connect_tried = 1;
1199 imp->imp_connect_tried = 1;
1202 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1203 if (rc == -EACCES) {
1205 * Give up trying to reconnect
1206 * EACCES means client has no permission for connection
1208 imp->imp_obd->obd_no_recov = 1;
1209 ptlrpc_deactivate_import(imp);
1212 if (rc == -EPROTO) {
1213 struct obd_connect_data *ocd;
1215 /* reply message might not be ready */
1216 if (request->rq_repmsg == NULL)
1219 ocd = req_capsule_server_get(&request->rq_pill,
1222 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1223 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1224 /* Actually servers are only supposed to refuse
1225 connection from liblustre clients, so we should
1226 never see this from VFS context */
1227 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1229 " refused connection from this client "
1230 "with an incompatible version (%s). "
1231 "Client must be recompiled\n",
1232 obd2cli_tgt(imp->imp_obd),
1233 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1234 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1235 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1236 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1237 LUSTRE_VERSION_STRING);
1238 ptlrpc_deactivate_import(imp);
1239 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1244 ptlrpc_maybe_ping_import_soon(imp);
1246 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1247 obd2cli_tgt(imp->imp_obd),
1248 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1251 wake_up_all(&imp->imp_recovery_waitq);
1256 * interpret callback for "completed replay" RPCs.
1257 * \see signal_completed_replay
1259 static int completed_replay_interpret(const struct lu_env *env,
1260 struct ptlrpc_request *req,
1261 void * data, int rc)
1264 atomic_dec(&req->rq_import->imp_replay_inflight);
1265 if (req->rq_status == 0 &&
1266 !req->rq_import->imp_vbr_failed) {
1267 ptlrpc_import_recovery_state_machine(req->rq_import);
1269 if (req->rq_import->imp_vbr_failed) {
1271 "%s: version recovery fails, reconnecting\n",
1272 req->rq_import->imp_obd->obd_name);
1274 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1276 req->rq_import->imp_obd->obd_name,
1279 ptlrpc_connect_import(req->rq_import);
1286 * Let server know that we have no requests to replay anymore.
1287 * Achieved by just sending a PING request
1289 static int signal_completed_replay(struct obd_import *imp)
1291 struct ptlrpc_request *req;
1294 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1297 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1298 atomic_inc(&imp->imp_replay_inflight);
1300 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1303 atomic_dec(&imp->imp_replay_inflight);
1307 ptlrpc_request_set_replen(req);
1308 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1309 lustre_msg_add_flags(req->rq_reqmsg,
1310 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1312 req->rq_timeout *= 3;
1313 req->rq_interpret_reply = completed_replay_interpret;
1315 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
1320 * In kernel code all import invalidation happens in its own
1321 * separate thread, so that whatever application happened to encounter
1322 * a problem could still be killed or otherwise continue
1324 static int ptlrpc_invalidate_import_thread(void *data)
1326 struct obd_import *imp = data;
1330 unshare_fs_struct();
1332 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1333 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1334 imp->imp_connection->c_remote_uuid.uuid);
1336 ptlrpc_invalidate_import(imp);
1338 if (obd_dump_on_eviction) {
1339 CERROR("dump the log upon eviction\n");
1340 libcfs_debug_dumplog();
1343 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1344 ptlrpc_import_recovery_state_machine(imp);
1346 class_import_put(imp);
1351 * This is the state machine for client-side recovery on import.
1353 * Typicaly we have two possibly paths. If we came to server and it is not
1354 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1355 * state and reconnect from scratch.
1356 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1357 * We go through our list of requests to replay and send them to server one by
1359 * After sending all request from the list we change import state to
1360 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1361 * and also all the locks we don't yet have and wait for server to grant us.
1362 * After that we send a special "replay completed" request and change import
1363 * state to IMP_REPLAY_WAIT.
1364 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1365 * state and resend all requests from sending list.
1366 * After that we promote import to FULL state and send all delayed requests
1367 * and import is fully operational after that.
1370 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1378 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1379 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1380 &target_start, &target_len);
1381 /* Don't care about MGC eviction */
1382 if (strcmp(imp->imp_obd->obd_type->typ_name,
1383 LUSTRE_MGC_NAME) != 0) {
1384 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1385 "by %.*s; in progress operations "
1386 "using this service will fail.\n",
1387 imp->imp_obd->obd_name, target_len,
1390 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1391 obd2cli_tgt(imp->imp_obd),
1392 imp->imp_connection->c_remote_uuid.uuid);
1393 /* reset vbr_failed flag upon eviction */
1394 spin_lock(&imp->imp_lock);
1395 imp->imp_vbr_failed = 0;
1396 spin_unlock(&imp->imp_lock);
1399 struct task_struct *task;
1400 /* bug 17802: XXX client_disconnect_export vs connect request
1401 * race. if client will evicted at this time, we start
1402 * invalidate thread without reference to import and import can
1403 * be freed at same time. */
1404 class_import_get(imp);
1405 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1408 class_import_put(imp);
1409 CERROR("error starting invalidate thread: %d\n", rc);
1418 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1419 CDEBUG(D_HA, "replay requested by %s\n",
1420 obd2cli_tgt(imp->imp_obd));
1421 rc = ptlrpc_replay_next(imp, &inflight);
1422 if (inflight == 0 &&
1423 atomic_read(&imp->imp_replay_inflight) == 0) {
1424 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1425 rc = ldlm_replay_locks(imp);
1432 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1433 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1434 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1435 rc = signal_completed_replay(imp);
1441 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1442 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1443 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1447 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1448 CDEBUG(D_HA, "reconnected to %s@%s\n",
1449 obd2cli_tgt(imp->imp_obd),
1450 imp->imp_connection->c_remote_uuid.uuid);
1452 rc = ptlrpc_resend(imp);
1455 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1456 ptlrpc_activate_import(imp);
1458 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1459 &target_start, &target_len);
1460 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1461 imp->imp_obd->obd_name,
1462 target_len, target_start,
1463 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1466 if (imp->imp_state == LUSTRE_IMP_FULL) {
1467 wake_up_all(&imp->imp_recovery_waitq);
1468 ptlrpc_wake_delayed(imp);
1475 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1477 struct ptlrpc_request *req;
1481 if (imp->imp_obd->obd_force)
1482 GOTO(set_state, rc);
1484 switch (imp->imp_connect_op) {
1486 rq_opc = OST_DISCONNECT;
1489 rq_opc = MDS_DISCONNECT;
1492 rq_opc = MGS_DISCONNECT;
1496 CERROR("%s: don't know how to disconnect from %s "
1497 "(connect_op %d): rc = %d\n",
1498 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1499 imp->imp_connect_op, rc);
1503 if (ptlrpc_import_in_recovery(imp)) {
1504 struct l_wait_info lwi;
1505 cfs_duration_t timeout;
1508 if (imp->imp_server_timeout)
1509 timeout = cfs_time_seconds(obd_timeout / 2);
1511 timeout = cfs_time_seconds(obd_timeout);
1513 int idx = import_at_get_index(imp,
1514 imp->imp_client->cli_request_portal);
1515 timeout = cfs_time_seconds(
1516 at_get(&imp->imp_at.iat_service_estimate[idx]));
1519 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1520 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1521 rc = l_wait_event(imp->imp_recovery_waitq,
1522 !ptlrpc_import_in_recovery(imp), &lwi);
1526 spin_lock(&imp->imp_lock);
1527 if (imp->imp_state != LUSTRE_IMP_FULL)
1529 spin_unlock(&imp->imp_lock);
1531 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1532 LUSTRE_OBD_VERSION, rq_opc);
1534 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1535 * it fails. We can get through the above with a down server
1536 * if the client doesn't know the server is gone yet. */
1537 req->rq_no_resend = 1;
1539 /* We want client umounts to happen quickly, no matter the
1541 req->rq_timeout = min_t(int, req->rq_timeout,
1542 INITIAL_CONNECT_TIMEOUT);
1544 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1545 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1546 ptlrpc_request_set_replen(req);
1547 rc = ptlrpc_queue_wait(req);
1548 ptlrpc_req_finished(req);
1552 spin_lock(&imp->imp_lock);
1555 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1557 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1558 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1559 spin_unlock(&imp->imp_lock);
1561 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1565 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1567 void ptlrpc_cleanup_imp(struct obd_import *imp)
1571 spin_lock(&imp->imp_lock);
1572 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1573 imp->imp_generation++;
1574 spin_unlock(&imp->imp_lock);
1575 ptlrpc_abort_inflight(imp);
1580 /* Adaptive Timeout utils */
1581 extern unsigned int at_min, at_max, at_history;
1583 /* Update at_current with the specified value (bounded by at_min and at_max),
1584 * as well as the AT history "bins".
1585 * - Bin into timeslices using AT_BINS bins.
1586 * - This gives us a max of the last at_history seconds without the storage,
1587 * but still smoothing out a return to normalcy from a slow response.
1588 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1590 int at_measured(struct adaptive_timeout *at, unsigned int val)
1592 unsigned int old = at->at_current;
1593 time_t now = cfs_time_current_sec();
1594 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1597 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1598 val, at, now - at->at_binstart, at->at_current,
1599 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1602 /* 0's don't count, because we never want our timeout to
1603 drop to 0, and because 0 could mean an error */
1606 spin_lock(&at->at_lock);
1608 if (unlikely(at->at_binstart == 0)) {
1609 /* Special case to remove default from history */
1610 at->at_current = val;
1611 at->at_worst_ever = val;
1612 at->at_worst_time = now;
1613 at->at_hist[0] = val;
1614 at->at_binstart = now;
1615 } else if (now - at->at_binstart < binlimit ) {
1617 at->at_hist[0] = max(val, at->at_hist[0]);
1618 at->at_current = max(val, at->at_current);
1621 unsigned int maxv = val;
1622 /* move bins over */
1623 shift = (now - at->at_binstart) / binlimit;
1625 for(i = AT_BINS - 1; i >= 0; i--) {
1627 at->at_hist[i] = at->at_hist[i - shift];
1628 maxv = max(maxv, at->at_hist[i]);
1633 at->at_hist[0] = val;
1634 at->at_current = maxv;
1635 at->at_binstart += shift * binlimit;
1638 if (at->at_current > at->at_worst_ever) {
1639 at->at_worst_ever = at->at_current;
1640 at->at_worst_time = now;
1643 if (at->at_flags & AT_FLG_NOHIST)
1644 /* Only keep last reported val; keeping the rest of the history
1646 at->at_current = val;
1649 at->at_current = min(at->at_current, at_max);
1650 at->at_current = max(at->at_current, at_min);
1652 if (at->at_current != old)
1653 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1654 "(val=%u) hist %u %u %u %u\n", at,
1655 old, at->at_current, at->at_current - old, val,
1656 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1659 /* if we changed, report the old value */
1660 old = (at->at_current != old) ? old : 0;
1662 spin_unlock(&at->at_lock);
1666 /* Find the imp_at index for a given portal; assign if space available */
1667 int import_at_get_index(struct obd_import *imp, int portal)
1669 struct imp_at *at = &imp->imp_at;
1672 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1673 if (at->iat_portal[i] == portal)
1675 if (at->iat_portal[i] == 0)
1680 /* Not found in list, add it under a lock */
1681 spin_lock(&imp->imp_lock);
1683 /* Check unused under lock */
1684 for (; i < IMP_AT_MAX_PORTALS; i++) {
1685 if (at->iat_portal[i] == portal)
1687 if (at->iat_portal[i] == 0)
1692 /* Not enough portals? */
1693 LASSERT(i < IMP_AT_MAX_PORTALS);
1695 at->iat_portal[i] = portal;
1697 spin_unlock(&imp->imp_lock);