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 <linux/kthread.h>
44 #include <obd_support.h>
45 #include <lustre_ha.h>
46 #include <lustre_net.h>
47 #include <lustre_import.h>
48 #include <lustre_export.h>
50 #include <obd_cksum.h>
51 #include <obd_class.h>
53 #include "ptlrpc_internal.h"
55 struct ptlrpc_connect_async_args {
56 __u64 pcaa_peer_committed;
57 int pcaa_initial_connect;
61 * Updates import \a imp current state to provided \a state value
62 * Helper function. Must be called under imp_lock.
64 static void __import_set_state(struct obd_import *imp,
65 enum lustre_imp_state state)
68 case LUSTRE_IMP_CLOSED:
70 case LUSTRE_IMP_DISCON:
71 case LUSTRE_IMP_CONNECTING:
73 case LUSTRE_IMP_REPLAY_WAIT:
74 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
77 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
79 imp->imp_state = state;
80 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
81 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
82 cfs_time_current_sec();
83 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
87 /* A CLOSED import should remain so. */
88 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
90 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
91 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
92 imp, obd2cli_tgt(imp->imp_obd), \
93 ptlrpc_import_state_name(imp->imp_state), \
94 ptlrpc_import_state_name(state)); \
95 __import_set_state(imp, state); \
99 #define IMPORT_SET_STATE(imp, state) \
101 spin_lock(&imp->imp_lock); \
102 IMPORT_SET_STATE_NOLOCK(imp, state); \
103 spin_unlock(&imp->imp_lock); \
107 static int ptlrpc_connect_interpret(const struct lu_env *env,
108 struct ptlrpc_request *request,
109 void * data, int rc);
110 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
112 /* Only this function is allowed to change the import state when it is
113 * CLOSED. I would rather refcount the import and free it after
114 * disconnection like we do with exports. To do that, the client_obd
115 * will need to save the peer info somewhere other than in the import,
117 int ptlrpc_init_import(struct obd_import *imp)
119 spin_lock(&imp->imp_lock);
121 imp->imp_generation++;
122 imp->imp_state = LUSTRE_IMP_NEW;
124 spin_unlock(&imp->imp_lock);
128 EXPORT_SYMBOL(ptlrpc_init_import);
130 #define UUID_STR "_UUID"
131 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
133 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
134 ? uuid : uuid + strlen(prefix);
136 *uuid_len = strlen(*uuid_start);
138 if (*uuid_len < strlen(UUID_STR))
141 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
142 UUID_STR, strlen(UUID_STR)))
143 *uuid_len -= strlen(UUID_STR);
147 * Returns true if import was FULL, false if import was already not
149 * @imp - import to be disconnected
150 * @conn_cnt - connection count (epoch) of the request that timed out
151 * and caused the disconnection. In some cases, multiple
152 * inflight requests can fail to a single target (e.g. OST
153 * bulk requests) and if one has already caused a reconnection
154 * (increasing the import->conn_cnt) the older failure should
155 * not also cause a reconnection. If zero it forces a reconnect.
157 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
161 spin_lock(&imp->imp_lock);
163 if (imp->imp_state == LUSTRE_IMP_FULL &&
164 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
168 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
169 &target_start, &target_len);
171 if (imp->imp_replayable) {
172 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
173 "lost; in progress operations using this "
174 "service will wait for recovery to complete\n",
175 imp->imp_obd->obd_name, target_len, target_start,
176 libcfs_nid2str(imp->imp_connection->c_peer.nid));
178 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
179 "%.*s (at %s) was lost; in progress "
180 "operations using this service will fail\n",
181 imp->imp_obd->obd_name,
182 target_len, target_start,
183 libcfs_nid2str(imp->imp_connection->c_peer.nid));
185 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
186 spin_unlock(&imp->imp_lock);
188 if (obd_dump_on_timeout)
189 libcfs_debug_dumplog();
191 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
194 spin_unlock(&imp->imp_lock);
195 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
196 imp->imp_client->cli_name, imp,
197 (imp->imp_state == LUSTRE_IMP_FULL &&
198 imp->imp_conn_cnt > conn_cnt) ?
199 "reconnected" : "not connected", imp->imp_conn_cnt,
200 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
206 /* Must be called with imp_lock held! */
207 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
210 assert_spin_locked(&imp->imp_lock);
212 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
213 imp->imp_invalid = 1;
214 imp->imp_generation++;
215 spin_unlock(&imp->imp_lock);
217 ptlrpc_abort_inflight(imp);
218 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
224 * This acts as a barrier; all existing requests are rejected, and
225 * no new requests will be accepted until the import is valid again.
227 void ptlrpc_deactivate_import(struct obd_import *imp)
229 spin_lock(&imp->imp_lock);
230 ptlrpc_deactivate_and_unlock_import(imp);
232 EXPORT_SYMBOL(ptlrpc_deactivate_import);
235 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
239 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
240 (req->rq_phase == RQ_PHASE_BULK) ||
241 (req->rq_phase == RQ_PHASE_NEW)))
244 if (req->rq_timedout)
247 if (req->rq_phase == RQ_PHASE_NEW)
250 dl = req->rq_deadline;
258 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
260 time_t now = cfs_time_current_sec();
261 struct list_head *tmp, *n;
262 struct ptlrpc_request *req;
263 unsigned int timeout = 0;
265 spin_lock(&imp->imp_lock);
266 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
267 req = list_entry(tmp, struct ptlrpc_request, rq_list);
268 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
270 spin_unlock(&imp->imp_lock);
275 * This function will invalidate the import, if necessary, then block
276 * for all the RPC completions, and finally notify the obd to
277 * invalidate its state (ie cancel locks, clear pending requests,
280 void ptlrpc_invalidate_import(struct obd_import *imp)
282 struct list_head *tmp, *n;
283 struct ptlrpc_request *req;
284 struct l_wait_info lwi;
285 unsigned int timeout;
288 atomic_inc(&imp->imp_inval_count);
290 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
291 ptlrpc_deactivate_import(imp);
293 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
294 LASSERT(imp->imp_invalid);
296 /* Wait forever until inflight == 0. We really can't do it another
297 * way because in some cases we need to wait for very long reply
298 * unlink. We can't do anything before that because there is really
299 * no guarantee that some rdma transfer is not in progress right now. */
301 /* Calculate max timeout for waiting on rpcs to error
302 * out. Use obd_timeout if calculated value is smaller
304 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
305 timeout = ptlrpc_inflight_timeout(imp);
306 timeout += timeout / 3;
309 timeout = obd_timeout;
311 /* decrease the interval to increase race condition */
315 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
318 /* Wait for all requests to error out and call completion
319 * callbacks. Cap it at obd_timeout -- these should all
320 * have been locally cancelled by ptlrpc_abort_inflight. */
321 lwi = LWI_TIMEOUT_INTERVAL(
322 cfs_timeout_cap(cfs_time_seconds(timeout)),
323 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
325 rc = l_wait_event(imp->imp_recovery_waitq,
326 (atomic_read(&imp->imp_inflight) == 0),
329 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
331 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
332 cli_tgt, rc, atomic_read(&imp->imp_inflight));
334 spin_lock(&imp->imp_lock);
335 if (atomic_read(&imp->imp_inflight) == 0) {
336 int count = atomic_read(&imp->imp_unregistering);
338 /* We know that "unregistering" rpcs only can
339 * survive in sending or delaying lists (they
340 * maybe waiting for long reply unlink in
341 * sluggish nets). Let's check this. If there
342 * is no inflight and unregistering != 0, this
344 LASSERTF(count == 0, "Some RPCs are still "
345 "unregistering: %d\n", count);
347 /* Let's save one loop as soon as inflight have
348 * dropped to zero. No new inflights possible at
352 list_for_each_safe(tmp, n,
353 &imp->imp_sending_list) {
354 req = list_entry(tmp,
355 struct ptlrpc_request,
357 DEBUG_REQ(D_ERROR, req,
358 "still on sending list");
360 list_for_each_safe(tmp, n,
361 &imp->imp_delayed_list) {
362 req = list_entry(tmp,
363 struct ptlrpc_request,
365 DEBUG_REQ(D_ERROR, req,
366 "still on delayed list");
369 CERROR("%s: RPCs in \"%s\" phase found (%d). "
370 "Network is sluggish? Waiting them "
371 "to error out.\n", cli_tgt,
372 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
373 atomic_read(&imp->imp_unregistering));
375 spin_unlock(&imp->imp_lock);
380 * Let's additionally check that no new rpcs added to import in
381 * "invalidate" state.
383 LASSERT(atomic_read(&imp->imp_inflight) == 0);
384 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
385 sptlrpc_import_flush_all_ctx(imp);
387 atomic_dec(&imp->imp_inval_count);
388 wake_up_all(&imp->imp_recovery_waitq);
390 EXPORT_SYMBOL(ptlrpc_invalidate_import);
392 /* unset imp_invalid */
393 void ptlrpc_activate_import(struct obd_import *imp)
395 struct obd_device *obd = imp->imp_obd;
397 spin_lock(&imp->imp_lock);
398 if (imp->imp_deactive != 0) {
399 spin_unlock(&imp->imp_lock);
403 imp->imp_invalid = 0;
404 spin_unlock(&imp->imp_lock);
405 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
407 EXPORT_SYMBOL(ptlrpc_activate_import);
409 static void ptlrpc_pinger_force(struct obd_import *imp)
411 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
412 ptlrpc_import_state_name(imp->imp_state));
414 spin_lock(&imp->imp_lock);
415 imp->imp_force_verify = 1;
416 spin_unlock(&imp->imp_lock);
418 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
419 ptlrpc_pinger_wake_up();
422 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
426 LASSERT(!imp->imp_dlm_fake);
428 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
429 if (!imp->imp_replayable) {
430 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
431 "auto-deactivating\n",
432 obd2cli_tgt(imp->imp_obd),
433 imp->imp_connection->c_remote_uuid.uuid,
434 imp->imp_obd->obd_name);
435 ptlrpc_deactivate_import(imp);
438 ptlrpc_pinger_force(imp);
443 int ptlrpc_reconnect_import(struct obd_import *imp)
446 struct l_wait_info lwi;
447 int secs = cfs_time_seconds(obd_timeout);
450 ptlrpc_pinger_force(imp);
452 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
453 obd2cli_tgt(imp->imp_obd), secs);
455 lwi = LWI_TIMEOUT(secs, NULL, NULL);
456 rc = l_wait_event(imp->imp_recovery_waitq,
457 !ptlrpc_import_in_recovery(imp), &lwi);
458 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
459 ptlrpc_import_state_name(imp->imp_state));
462 ptlrpc_set_import_discon(imp, 0);
463 /* Force a new connect attempt */
464 ptlrpc_invalidate_import(imp);
465 /* Do a fresh connect next time by zeroing the handle */
466 ptlrpc_disconnect_import(imp, 1);
467 /* Wait for all invalidate calls to finish */
468 if (atomic_read(&imp->imp_inval_count) > 0) {
469 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
472 rc = l_wait_event(imp->imp_recovery_waitq,
473 (atomic_read(&imp->imp_inval_count) == 0),
476 CERROR("Interrupted, inval=%d\n",
477 atomic_read(&imp->imp_inval_count));
480 /* Allow reconnect attempts */
481 imp->imp_obd->obd_no_recov = 0;
482 /* Remove 'invalid' flag */
483 ptlrpc_activate_import(imp);
484 /* Attempt a new connect */
485 ptlrpc_recover_import(imp, NULL, 0);
489 EXPORT_SYMBOL(ptlrpc_reconnect_import);
492 * Connection on import \a imp is changed to another one (if more than one is
493 * present). We typically chose connection that we have not tried to connect to
496 static int import_select_connection(struct obd_import *imp)
498 struct obd_import_conn *imp_conn = NULL, *conn;
499 struct obd_export *dlmexp;
501 int target_len, tried_all = 1;
504 spin_lock(&imp->imp_lock);
506 if (list_empty(&imp->imp_conn_list)) {
507 CERROR("%s: no connections available\n",
508 imp->imp_obd->obd_name);
509 spin_unlock(&imp->imp_lock);
513 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
514 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
515 imp->imp_obd->obd_name,
516 libcfs_nid2str(conn->oic_conn->c_peer.nid),
517 conn->oic_last_attempt);
519 /* If we have not tried this connection since
520 the last successful attempt, go with this one */
521 if ((conn->oic_last_attempt == 0) ||
522 cfs_time_beforeq_64(conn->oic_last_attempt,
523 imp->imp_last_success_conn)) {
529 /* If all of the connections have already been tried
530 since the last successful connection; just choose the
531 least recently used */
534 else if (cfs_time_before_64(conn->oic_last_attempt,
535 imp_conn->oic_last_attempt))
539 /* if not found, simply choose the current one */
540 if (!imp_conn || imp->imp_force_reconnect) {
541 LASSERT(imp->imp_conn_current);
542 imp_conn = imp->imp_conn_current;
545 LASSERT(imp_conn->oic_conn);
547 /* If we've tried everything, and we're back to the beginning of the
548 list, increase our timeout and try again. It will be reset when
549 we do finally connect. (FIXME: really we should wait for all network
550 state associated with the last connection attempt to drain before
551 trying to reconnect on it.) */
552 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
553 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
554 if (at_get(at) < CONNECTION_SWITCH_MAX) {
555 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
556 if (at_get(at) > CONNECTION_SWITCH_MAX)
557 at_reset(at, CONNECTION_SWITCH_MAX);
559 LASSERT(imp_conn->oic_last_attempt);
560 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
561 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
564 imp_conn->oic_last_attempt = cfs_time_current_64();
566 /* switch connection, don't mind if it's same as the current one */
567 if (imp->imp_connection)
568 ptlrpc_connection_put(imp->imp_connection);
569 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
571 dlmexp = class_conn2export(&imp->imp_dlm_handle);
572 LASSERT(dlmexp != NULL);
573 if (dlmexp->exp_connection)
574 ptlrpc_connection_put(dlmexp->exp_connection);
575 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
576 class_export_put(dlmexp);
578 if (imp->imp_conn_current != imp_conn) {
579 if (imp->imp_conn_current) {
580 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
581 &target_start, &target_len);
583 CDEBUG(D_HA, "%s: Connection changing to"
585 imp->imp_obd->obd_name,
586 target_len, target_start,
587 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
590 imp->imp_conn_current = imp_conn;
593 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
594 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
595 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
597 spin_unlock(&imp->imp_lock);
603 * must be called under imp_lock
605 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
607 struct ptlrpc_request *req;
608 struct list_head *tmp;
610 /* The requests in committed_list always have smaller transnos than
611 * the requests in replay_list */
612 if (!list_empty(&imp->imp_committed_list)) {
613 tmp = imp->imp_committed_list.next;
614 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
615 *transno = req->rq_transno;
616 if (req->rq_transno == 0) {
617 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
622 if (!list_empty(&imp->imp_replay_list)) {
623 tmp = imp->imp_replay_list.next;
624 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
625 *transno = req->rq_transno;
626 if (req->rq_transno == 0) {
627 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
636 * Attempt to (re)connect import \a imp. This includes all preparations,
637 * initializing CONNECT RPC request and passing it to ptlrpcd for
639 * Returns 0 on success or error code.
641 int ptlrpc_connect_import(struct obd_import *imp)
643 struct obd_device *obd = imp->imp_obd;
644 int initial_connect = 0;
646 __u64 committed_before_reconnect = 0;
647 struct ptlrpc_request *request;
648 char *bufs[] = { NULL,
649 obd2cli_tgt(imp->imp_obd),
651 (char *)&imp->imp_dlm_handle,
652 (char *)&imp->imp_connect_data };
653 struct ptlrpc_connect_async_args *aa;
657 spin_lock(&imp->imp_lock);
658 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
659 spin_unlock(&imp->imp_lock);
660 CERROR("can't connect to a closed import\n");
662 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
663 spin_unlock(&imp->imp_lock);
664 CERROR("already connected\n");
666 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
667 spin_unlock(&imp->imp_lock);
668 CERROR("already connecting\n");
672 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
675 imp->imp_resend_replay = 0;
677 if (!lustre_handle_is_used(&imp->imp_remote_handle))
680 committed_before_reconnect = imp->imp_peer_committed_transno;
682 set_transno = ptlrpc_first_transno(imp,
683 &imp->imp_connect_data.ocd_transno);
684 spin_unlock(&imp->imp_lock);
686 rc = import_select_connection(imp);
690 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
694 /* Reset connect flags to the originally requested flags, in case
695 * the server is updated on-the-fly we will get the new features. */
696 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
697 /* Reset ocd_version each time so the server knows the exact versions */
698 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
699 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
700 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
702 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
703 &obd->obd_uuid, &imp->imp_connect_data, NULL);
707 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
709 GOTO(out, rc = -ENOMEM);
711 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
712 imp->imp_connect_op, bufs, NULL);
714 ptlrpc_request_free(request);
718 /* Report the rpc service time to the server so that it knows how long
719 * to wait for clients to join recovery */
720 lustre_msg_set_service_time(request->rq_reqmsg,
721 at_timeout2est(request->rq_timeout));
723 /* The amount of time we give the server to process the connect req.
724 * import_select_connection will increase the net latency on
725 * repeated reconnect attempts to cover slow networks.
726 * We override/ignore the server rpc completion estimate here,
727 * which may be large if this is a reconnect attempt */
728 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
729 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
731 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
733 request->rq_no_resend = request->rq_no_delay = 1;
734 request->rq_send_state = LUSTRE_IMP_CONNECTING;
735 /* Allow a slightly larger reply for future growth compatibility */
736 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
737 sizeof(struct obd_connect_data)+16*sizeof(__u64));
738 ptlrpc_request_set_replen(request);
739 request->rq_interpret_reply = ptlrpc_connect_interpret;
741 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
742 aa = ptlrpc_req_async_args(request);
743 memset(aa, 0, sizeof *aa);
745 aa->pcaa_peer_committed = committed_before_reconnect;
746 aa->pcaa_initial_connect = initial_connect;
748 if (aa->pcaa_initial_connect) {
749 spin_lock(&imp->imp_lock);
750 imp->imp_replayable = 1;
751 spin_unlock(&imp->imp_lock);
752 lustre_msg_add_op_flags(request->rq_reqmsg,
753 MSG_CONNECT_INITIAL);
757 lustre_msg_add_op_flags(request->rq_reqmsg,
758 MSG_CONNECT_TRANSNO);
760 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
761 request->rq_timeout);
762 ptlrpcd_add_req(request, PDL_POLICY_ROUND, -1);
766 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
771 EXPORT_SYMBOL(ptlrpc_connect_import);
773 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
777 spin_lock(&imp->imp_lock);
778 force_verify = imp->imp_force_verify != 0;
779 spin_unlock(&imp->imp_lock);
782 ptlrpc_pinger_wake_up();
785 static int ptlrpc_busy_reconnect(int rc)
787 return (rc == -EBUSY) || (rc == -EAGAIN);
790 static int ptlrpc_connect_set_flags(struct obd_import *imp,
791 struct obd_connect_data *ocd,
792 __u64 old_connect_flags,
793 struct obd_export *exp, int init_connect)
796 struct client_obd *cli = &imp->imp_obd->u.cli;
798 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
799 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
800 LCONSOLE_WARN("%s: MDS %s does not support ibits "
801 "lock, either very old or invalid: "
802 "requested "LPX64", replied "LPX64"\n",
803 imp->imp_obd->obd_name,
804 imp->imp_connection->c_remote_uuid.uuid,
805 imp->imp_connect_flags_orig,
806 ocd->ocd_connect_flags);
810 spin_lock(&imp->imp_lock);
811 list_del(&imp->imp_conn_current->oic_item);
812 list_add(&imp->imp_conn_current->oic_item,
813 &imp->imp_conn_list);
814 imp->imp_last_success_conn =
815 imp->imp_conn_current->oic_last_attempt;
817 spin_unlock(&imp->imp_lock);
820 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
821 (ocd->ocd_version > LUSTRE_VERSION_CODE +
822 LUSTRE_VERSION_OFFSET_WARN ||
823 ocd->ocd_version < LUSTRE_VERSION_CODE -
824 LUSTRE_VERSION_OFFSET_WARN)) {
825 /* Sigh, some compilers do not like #ifdef in the middle
826 of macro arguments */
827 const char *older = "older than client. "
828 "Consider upgrading server";
829 const char *newer = "newer than client. "
830 "Consider recompiling application";
832 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
834 obd2cli_tgt(imp->imp_obd),
835 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
836 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
837 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
838 OBD_OCD_VERSION_FIX(ocd->ocd_version),
839 ocd->ocd_version > LUSTRE_VERSION_CODE ?
840 newer : older, LUSTRE_VERSION_STRING);
844 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
845 /* Check if server has LU-1252 fix applied to not always swab
846 * the IR MNE entries. Do this only once per connection. This
847 * fixup is version-limited, because we don't want to carry the
848 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
849 * need interop with unpatched 2.2 servers. For newer servers,
850 * the client will do MNE swabbing only as needed. LU-1644 */
851 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
852 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
853 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
854 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
855 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
856 strcmp(imp->imp_obd->obd_type->typ_name,
857 LUSTRE_MGC_NAME) == 0))
858 imp->imp_need_mne_swab = 1;
859 else /* clear if server was upgraded since last connect */
860 imp->imp_need_mne_swab = 0;
863 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
864 /* We sent to the server ocd_cksum_types with bits set
865 * for algorithms we understand. The server masked off
866 * the checksum types it doesn't support */
867 if ((ocd->ocd_cksum_types &
868 cksum_types_supported_client()) == 0) {
869 LCONSOLE_WARN("The negotiation of the checksum "
870 "alogrithm to use with server %s "
871 "failed (%x/%x), disabling "
873 obd2cli_tgt(imp->imp_obd),
874 ocd->ocd_cksum_types,
875 cksum_types_supported_client());
876 cli->cl_checksum = 0;
877 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
879 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
882 /* The server does not support OBD_CONNECT_CKSUM.
883 * Enforce ADLER for backward compatibility*/
884 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
886 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
888 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
889 cli->cl_max_pages_per_rpc =
890 min(ocd->ocd_brw_size >> PAGE_CACHE_SHIFT,
891 cli->cl_max_pages_per_rpc);
892 else if (imp->imp_connect_op == MDS_CONNECT ||
893 imp->imp_connect_op == MGS_CONNECT)
894 cli->cl_max_pages_per_rpc = 1;
896 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
897 (cli->cl_max_pages_per_rpc > 0));
899 client_adjust_max_dirty(cli);
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 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
908 "flags: "LPX64"\n", imp->imp_obd->obd_name,
909 ocd->ocd_connect_flags);
910 imp->imp_obd->obd_namespace->ns_connect_flags =
911 ocd->ocd_connect_flags;
912 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
913 ocd->ocd_connect_flags;
916 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
917 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
918 /* We need a per-message support flag, because
919 * a. we don't know if the incoming connect reply
920 * supports AT or not (in reply_in_callback)
921 * until we unpack it.
922 * b. failovered server means export and flags are gone
923 * (in ptlrpc_send_reply).
924 * Can only be set when we know AT is supported at
926 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
928 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
930 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
931 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
932 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
934 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
940 * interpret_reply callback for connect RPCs.
941 * Looks into returned status of connect operation and decides
942 * what to do with the import - i.e enter recovery, promote it to
943 * full state for normal operations of disconnect it due to an error.
945 static int ptlrpc_connect_interpret(const struct lu_env *env,
946 struct ptlrpc_request *request,
949 struct ptlrpc_connect_async_args *aa = data;
950 struct obd_import *imp = request->rq_import;
951 struct lustre_handle old_hdl;
952 __u64 old_connect_flags;
954 struct obd_connect_data *ocd;
955 struct obd_export *exp;
959 spin_lock(&imp->imp_lock);
960 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
961 imp->imp_connect_tried = 1;
962 spin_unlock(&imp->imp_lock);
967 /* if this reconnect to busy export - not need select new target
969 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
970 spin_unlock(&imp->imp_lock);
971 ptlrpc_maybe_ping_import_soon(imp);
974 spin_unlock(&imp->imp_lock);
976 LASSERT(imp->imp_conn_current);
978 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
980 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
982 /* server replied obd_connect_data is always bigger */
983 ocd = req_capsule_server_sized_get(&request->rq_pill,
984 &RMF_CONNECT_DATA, ret);
987 CERROR("%s: no connect data from server\n",
988 imp->imp_obd->obd_name);
993 spin_lock(&imp->imp_lock);
995 /* All imports are pingable */
996 imp->imp_pingable = 1;
997 imp->imp_force_reconnect = 0;
998 imp->imp_force_verify = 0;
1000 imp->imp_connect_data = *ocd;
1002 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1003 imp->imp_obd->obd_name, ocd->ocd_instance);
1004 exp = class_conn2export(&imp->imp_dlm_handle);
1006 spin_unlock(&imp->imp_lock);
1008 /* check that server granted subset of flags we asked for. */
1009 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1010 ocd->ocd_connect_flags) {
1011 CERROR("%s: Server didn't granted asked subset of flags: "
1012 "asked="LPX64" grranted="LPX64"\n",
1013 imp->imp_obd->obd_name,imp->imp_connect_flags_orig,
1014 ocd->ocd_connect_flags);
1015 GOTO(out, rc = -EPROTO);
1018 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1019 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1020 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1021 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1022 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1023 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1024 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1026 /* We do not support the MDT-MDT interoperations with
1027 * different version MDT because of protocol changes. */
1028 if (unlikely(major != LUSTRE_MAJOR ||
1029 minor != LUSTRE_MINOR ||
1030 abs(patch - LUSTRE_PATCH) > 3)) {
1031 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1032 "connection to different version MDT "
1033 "(%d.%d.%d.%d) %s\n",
1034 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1035 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1036 major, minor, patch,
1037 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1038 imp->imp_connection->c_remote_uuid.uuid);
1040 GOTO(out, rc = -EPROTO);
1045 /* This could happen if export is cleaned during the
1047 CERROR("%s: missing export after connect\n",
1048 imp->imp_obd->obd_name);
1049 GOTO(out, rc = -ENODEV);
1051 old_connect_flags = exp_connect_flags(exp);
1052 exp->exp_connect_data = *ocd;
1053 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1055 /* The net statistics after (re-)connect is not valid anymore,
1056 * because may reflect other routing, etc. */
1057 at_init(&imp->imp_at.iat_net_latency, 0, 0);
1058 ptlrpc_at_adj_net_latency(request,
1059 lustre_msg_get_service_time(request->rq_repmsg));
1061 /* Import flags should be updated before waking import at FULL state */
1062 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1063 aa->pcaa_initial_connect);
1064 class_export_put(exp);
1068 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1070 if (aa->pcaa_initial_connect) {
1071 spin_lock(&imp->imp_lock);
1072 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1073 imp->imp_replayable = 1;
1074 spin_unlock(&imp->imp_lock);
1075 CDEBUG(D_HA, "connected to replayable target: %s\n",
1076 obd2cli_tgt(imp->imp_obd));
1078 imp->imp_replayable = 0;
1079 spin_unlock(&imp->imp_lock);
1082 /* if applies, adjust the imp->imp_msg_magic here
1083 * according to reply flags */
1085 imp->imp_remote_handle =
1086 *lustre_msg_get_handle(request->rq_repmsg);
1088 /* Initial connects are allowed for clients with non-random
1089 * uuids when servers are in recovery. Simply signal the
1090 * servers replay is complete and wait in REPLAY_WAIT. */
1091 if (msg_flags & MSG_CONNECT_RECOVERING) {
1092 CDEBUG(D_HA, "connect to %s during recovery\n",
1093 obd2cli_tgt(imp->imp_obd));
1094 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1096 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1097 ptlrpc_activate_import(imp);
1100 GOTO(finish, rc = 0);
1103 /* Determine what recovery state to move the import to. */
1104 if (MSG_CONNECT_RECONNECT & msg_flags) {
1105 memset(&old_hdl, 0, sizeof(old_hdl));
1106 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1107 sizeof (old_hdl))) {
1108 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1109 "bad handle "LPX64"\n",
1110 obd2cli_tgt(imp->imp_obd),
1111 imp->imp_connection->c_remote_uuid.uuid,
1112 imp->imp_dlm_handle.cookie);
1113 GOTO(out, rc = -ENOTCONN);
1116 if (memcmp(&imp->imp_remote_handle,
1117 lustre_msg_get_handle(request->rq_repmsg),
1118 sizeof(imp->imp_remote_handle))) {
1119 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1122 /* Bug 16611/14775: if server handle have changed,
1123 * that means some sort of disconnection happened.
1124 * If the server is not in recovery, that also means it
1125 * already erased all of our state because of previous
1126 * eviction. If it is in recovery - we are safe to
1127 * participate since we can reestablish all of our state
1128 * with server again */
1129 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1130 CDEBUG(level,"%s@%s changed server handle from "
1132 " but is still in recovery\n",
1133 obd2cli_tgt(imp->imp_obd),
1134 imp->imp_connection->c_remote_uuid.uuid,
1135 imp->imp_remote_handle.cookie,
1136 lustre_msg_get_handle(
1137 request->rq_repmsg)->cookie);
1139 LCONSOLE_WARN("Evicted from %s (at %s) "
1140 "after server handle changed from "
1141 LPX64" to "LPX64"\n",
1142 obd2cli_tgt(imp->imp_obd),
1143 imp->imp_connection-> \
1145 imp->imp_remote_handle.cookie,
1146 lustre_msg_get_handle(
1147 request->rq_repmsg)->cookie);
1151 imp->imp_remote_handle =
1152 *lustre_msg_get_handle(request->rq_repmsg);
1154 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1155 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1156 GOTO(finish, rc = 0);
1160 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1161 obd2cli_tgt(imp->imp_obd),
1162 imp->imp_connection->c_remote_uuid.uuid);
1165 if (imp->imp_invalid) {
1166 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1167 "marking evicted\n", imp->imp_obd->obd_name);
1168 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1169 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1170 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1171 imp->imp_obd->obd_name,
1172 obd2cli_tgt(imp->imp_obd));
1174 spin_lock(&imp->imp_lock);
1175 imp->imp_resend_replay = 1;
1176 spin_unlock(&imp->imp_lock);
1178 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1180 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1182 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1183 LASSERT(imp->imp_replayable);
1184 imp->imp_remote_handle =
1185 *lustre_msg_get_handle(request->rq_repmsg);
1186 imp->imp_last_replay_transno = 0;
1187 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1189 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1190 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1191 imp->imp_remote_handle =
1192 *lustre_msg_get_handle(request->rq_repmsg);
1193 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1196 /* Sanity checks for a reconnected import. */
1197 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1198 CERROR("imp_replayable flag does not match server "
1199 "after reconnect. We should LBUG right here.\n");
1202 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1203 lustre_msg_get_last_committed(request->rq_repmsg) <
1204 aa->pcaa_peer_committed) {
1205 CERROR("%s went back in time (transno "LPD64
1206 " was previously committed, server now claims "LPD64
1207 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1209 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1210 lustre_msg_get_last_committed(request->rq_repmsg));
1214 rc = ptlrpc_import_recovery_state_machine(imp);
1215 if (rc == -ENOTCONN) {
1216 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1217 "invalidating and reconnecting\n",
1218 obd2cli_tgt(imp->imp_obd),
1219 imp->imp_connection->c_remote_uuid.uuid);
1220 ptlrpc_connect_import(imp);
1221 imp->imp_connect_tried = 1;
1226 imp->imp_connect_tried = 1;
1229 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1230 if (rc == -EACCES) {
1232 * Give up trying to reconnect
1233 * EACCES means client has no permission for connection
1235 imp->imp_obd->obd_no_recov = 1;
1236 ptlrpc_deactivate_import(imp);
1239 if (rc == -EPROTO) {
1240 struct obd_connect_data *ocd;
1242 /* reply message might not be ready */
1243 if (request->rq_repmsg == NULL)
1246 ocd = req_capsule_server_get(&request->rq_pill,
1249 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1250 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1251 /* Actually servers are only supposed to refuse
1252 connection from liblustre clients, so we should
1253 never see this from VFS context */
1254 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1256 " refused connection from this client "
1257 "with an incompatible version (%s). "
1258 "Client must be recompiled\n",
1259 obd2cli_tgt(imp->imp_obd),
1260 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1261 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1262 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1263 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1264 LUSTRE_VERSION_STRING);
1265 ptlrpc_deactivate_import(imp);
1266 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1271 ptlrpc_maybe_ping_import_soon(imp);
1273 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1274 obd2cli_tgt(imp->imp_obd),
1275 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1278 wake_up_all(&imp->imp_recovery_waitq);
1283 * interpret callback for "completed replay" RPCs.
1284 * \see signal_completed_replay
1286 static int completed_replay_interpret(const struct lu_env *env,
1287 struct ptlrpc_request *req,
1288 void * data, int rc)
1291 atomic_dec(&req->rq_import->imp_replay_inflight);
1292 if (req->rq_status == 0 &&
1293 !req->rq_import->imp_vbr_failed) {
1294 ptlrpc_import_recovery_state_machine(req->rq_import);
1296 if (req->rq_import->imp_vbr_failed) {
1298 "%s: version recovery fails, reconnecting\n",
1299 req->rq_import->imp_obd->obd_name);
1301 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1303 req->rq_import->imp_obd->obd_name,
1306 ptlrpc_connect_import(req->rq_import);
1313 * Let server know that we have no requests to replay anymore.
1314 * Achieved by just sending a PING request
1316 static int signal_completed_replay(struct obd_import *imp)
1318 struct ptlrpc_request *req;
1321 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1324 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1325 atomic_inc(&imp->imp_replay_inflight);
1327 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1330 atomic_dec(&imp->imp_replay_inflight);
1334 ptlrpc_request_set_replen(req);
1335 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1336 lustre_msg_add_flags(req->rq_reqmsg,
1337 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1339 req->rq_timeout *= 3;
1340 req->rq_interpret_reply = completed_replay_interpret;
1342 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
1347 * In kernel code all import invalidation happens in its own
1348 * separate thread, so that whatever application happened to encounter
1349 * a problem could still be killed or otherwise continue
1351 static int ptlrpc_invalidate_import_thread(void *data)
1353 struct obd_import *imp = data;
1357 unshare_fs_struct();
1359 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1360 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1361 imp->imp_connection->c_remote_uuid.uuid);
1363 ptlrpc_invalidate_import(imp);
1365 if (obd_dump_on_eviction) {
1366 CERROR("dump the log upon eviction\n");
1367 libcfs_debug_dumplog();
1370 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1371 ptlrpc_import_recovery_state_machine(imp);
1373 class_import_put(imp);
1378 * This is the state machine for client-side recovery on import.
1380 * Typicaly we have two possibly paths. If we came to server and it is not
1381 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1382 * state and reconnect from scratch.
1383 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1384 * We go through our list of requests to replay and send them to server one by
1386 * After sending all request from the list we change import state to
1387 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1388 * and also all the locks we don't yet have and wait for server to grant us.
1389 * After that we send a special "replay completed" request and change import
1390 * state to IMP_REPLAY_WAIT.
1391 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1392 * state and resend all requests from sending list.
1393 * After that we promote import to FULL state and send all delayed requests
1394 * and import is fully operational after that.
1397 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1405 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1406 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1407 &target_start, &target_len);
1408 /* Don't care about MGC eviction */
1409 if (strcmp(imp->imp_obd->obd_type->typ_name,
1410 LUSTRE_MGC_NAME) != 0) {
1411 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1412 "by %.*s; in progress operations "
1413 "using this service will fail.\n",
1414 imp->imp_obd->obd_name, target_len,
1417 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1418 obd2cli_tgt(imp->imp_obd),
1419 imp->imp_connection->c_remote_uuid.uuid);
1420 /* reset vbr_failed flag upon eviction */
1421 spin_lock(&imp->imp_lock);
1422 imp->imp_vbr_failed = 0;
1423 spin_unlock(&imp->imp_lock);
1426 struct task_struct *task;
1427 /* bug 17802: XXX client_disconnect_export vs connect request
1428 * race. if client is evicted at this time then we start
1429 * invalidate thread without reference to import and import can
1430 * be freed at same time. */
1431 class_import_get(imp);
1432 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1435 class_import_put(imp);
1436 CERROR("error starting invalidate thread: %d\n", rc);
1445 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1446 CDEBUG(D_HA, "replay requested by %s\n",
1447 obd2cli_tgt(imp->imp_obd));
1448 rc = ptlrpc_replay_next(imp, &inflight);
1449 if (inflight == 0 &&
1450 atomic_read(&imp->imp_replay_inflight) == 0) {
1451 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1452 rc = ldlm_replay_locks(imp);
1459 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1460 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1461 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1462 rc = signal_completed_replay(imp);
1468 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1469 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1470 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1474 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1475 CDEBUG(D_HA, "reconnected to %s@%s\n",
1476 obd2cli_tgt(imp->imp_obd),
1477 imp->imp_connection->c_remote_uuid.uuid);
1479 rc = ptlrpc_resend(imp);
1482 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1483 ptlrpc_activate_import(imp);
1485 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1486 &target_start, &target_len);
1487 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1488 imp->imp_obd->obd_name,
1489 target_len, target_start,
1490 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1493 if (imp->imp_state == LUSTRE_IMP_FULL) {
1494 wake_up_all(&imp->imp_recovery_waitq);
1495 ptlrpc_wake_delayed(imp);
1502 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1504 struct ptlrpc_request *req;
1508 if (imp->imp_obd->obd_force)
1509 GOTO(set_state, rc);
1511 switch (imp->imp_connect_op) {
1513 rq_opc = OST_DISCONNECT;
1516 rq_opc = MDS_DISCONNECT;
1519 rq_opc = MGS_DISCONNECT;
1523 CERROR("%s: don't know how to disconnect from %s "
1524 "(connect_op %d): rc = %d\n",
1525 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1526 imp->imp_connect_op, rc);
1530 if (ptlrpc_import_in_recovery(imp)) {
1531 struct l_wait_info lwi;
1532 cfs_duration_t timeout;
1535 if (imp->imp_server_timeout)
1536 timeout = cfs_time_seconds(obd_timeout / 2);
1538 timeout = cfs_time_seconds(obd_timeout);
1540 int idx = import_at_get_index(imp,
1541 imp->imp_client->cli_request_portal);
1542 timeout = cfs_time_seconds(
1543 at_get(&imp->imp_at.iat_service_estimate[idx]));
1546 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1547 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1548 rc = l_wait_event(imp->imp_recovery_waitq,
1549 !ptlrpc_import_in_recovery(imp), &lwi);
1553 spin_lock(&imp->imp_lock);
1554 if (imp->imp_state != LUSTRE_IMP_FULL)
1556 spin_unlock(&imp->imp_lock);
1558 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1559 LUSTRE_OBD_VERSION, rq_opc);
1561 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1562 * it fails. We can get through the above with a down server
1563 * if the client doesn't know the server is gone yet. */
1564 req->rq_no_resend = 1;
1566 /* We want client umounts to happen quickly, no matter the
1568 req->rq_timeout = min_t(int, req->rq_timeout,
1569 INITIAL_CONNECT_TIMEOUT);
1571 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1572 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1573 ptlrpc_request_set_replen(req);
1574 rc = ptlrpc_queue_wait(req);
1575 ptlrpc_req_finished(req);
1579 spin_lock(&imp->imp_lock);
1582 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1584 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1585 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1586 spin_unlock(&imp->imp_lock);
1588 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1592 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1594 void ptlrpc_cleanup_imp(struct obd_import *imp)
1598 spin_lock(&imp->imp_lock);
1599 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1600 imp->imp_generation++;
1601 spin_unlock(&imp->imp_lock);
1602 ptlrpc_abort_inflight(imp);
1607 /* Adaptive Timeout utils */
1608 extern unsigned int at_min, at_max, at_history;
1610 /* Update at_current with the specified value (bounded by at_min and at_max),
1611 * as well as the AT history "bins".
1612 * - Bin into timeslices using AT_BINS bins.
1613 * - This gives us a max of the last at_history seconds without the storage,
1614 * but still smoothing out a return to normalcy from a slow response.
1615 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1617 int at_measured(struct adaptive_timeout *at, unsigned int val)
1619 unsigned int old = at->at_current;
1620 time_t now = cfs_time_current_sec();
1621 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1624 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1625 val, at, now - at->at_binstart, at->at_current,
1626 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1629 /* 0's don't count, because we never want our timeout to
1630 drop to 0, and because 0 could mean an error */
1633 spin_lock(&at->at_lock);
1635 if (unlikely(at->at_binstart == 0)) {
1636 /* Special case to remove default from history */
1637 at->at_current = val;
1638 at->at_worst_ever = val;
1639 at->at_worst_time = now;
1640 at->at_hist[0] = val;
1641 at->at_binstart = now;
1642 } else if (now - at->at_binstart < binlimit ) {
1644 at->at_hist[0] = max(val, at->at_hist[0]);
1645 at->at_current = max(val, at->at_current);
1648 unsigned int maxv = val;
1649 /* move bins over */
1650 shift = (now - at->at_binstart) / binlimit;
1652 for(i = AT_BINS - 1; i >= 0; i--) {
1654 at->at_hist[i] = at->at_hist[i - shift];
1655 maxv = max(maxv, at->at_hist[i]);
1660 at->at_hist[0] = val;
1661 at->at_current = maxv;
1662 at->at_binstart += shift * binlimit;
1665 if (at->at_current > at->at_worst_ever) {
1666 at->at_worst_ever = at->at_current;
1667 at->at_worst_time = now;
1670 if (at->at_flags & AT_FLG_NOHIST)
1671 /* Only keep last reported val; keeping the rest of the history
1673 at->at_current = val;
1676 at->at_current = min(at->at_current, at_max);
1677 at->at_current = max(at->at_current, at_min);
1679 if (at->at_current != old)
1680 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1681 "(val=%u) hist %u %u %u %u\n", at,
1682 old, at->at_current, at->at_current - old, val,
1683 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1686 /* if we changed, report the old value */
1687 old = (at->at_current != old) ? old : 0;
1689 spin_unlock(&at->at_lock);
1693 /* Find the imp_at index for a given portal; assign if space available */
1694 int import_at_get_index(struct obd_import *imp, int portal)
1696 struct imp_at *at = &imp->imp_at;
1699 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1700 if (at->iat_portal[i] == portal)
1702 if (at->iat_portal[i] == 0)
1707 /* Not found in list, add it under a lock */
1708 spin_lock(&imp->imp_lock);
1710 /* Check unused under lock */
1711 for (; i < IMP_AT_MAX_PORTALS; i++) {
1712 if (at->iat_portal[i] == portal)
1714 if (at->iat_portal[i] == 0)
1719 /* Not enough portals? */
1720 LASSERT(i < IMP_AT_MAX_PORTALS);
1722 at->iat_portal[i] = portal;
1724 spin_unlock(&imp->imp_lock);