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, 2012, 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 <liblustre.h>
46 #include <obd_support.h>
47 #include <lustre_ha.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_export.h>
52 #include <obd_cksum.h>
53 #include <obd_class.h>
55 #include "ptlrpc_internal.h"
57 struct ptlrpc_connect_async_args {
58 __u64 pcaa_peer_committed;
59 int pcaa_initial_connect;
63 * Updates import \a imp current state to provided \a state value
64 * Helper function. Must be called under imp_lock.
66 static void __import_set_state(struct obd_import *imp,
67 enum lustre_imp_state state)
69 imp->imp_state = state;
70 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
71 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
72 cfs_time_current_sec();
73 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
77 /* A CLOSED import should remain so. */
78 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
80 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
81 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
82 imp, obd2cli_tgt(imp->imp_obd), \
83 ptlrpc_import_state_name(imp->imp_state), \
84 ptlrpc_import_state_name(state)); \
85 __import_set_state(imp, state); \
89 #define IMPORT_SET_STATE(imp, state) \
91 spin_lock(&imp->imp_lock); \
92 IMPORT_SET_STATE_NOLOCK(imp, state); \
93 spin_unlock(&imp->imp_lock); \
97 static int ptlrpc_connect_interpret(const struct lu_env *env,
98 struct ptlrpc_request *request,
100 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
102 /* Only this function is allowed to change the import state when it is
103 * CLOSED. I would rather refcount the import and free it after
104 * disconnection like we do with exports. To do that, the client_obd
105 * will need to save the peer info somewhere other than in the import,
107 int ptlrpc_init_import(struct obd_import *imp)
109 spin_lock(&imp->imp_lock);
111 imp->imp_generation++;
112 imp->imp_state = LUSTRE_IMP_NEW;
114 spin_unlock(&imp->imp_lock);
118 EXPORT_SYMBOL(ptlrpc_init_import);
120 #define UUID_STR "_UUID"
121 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
123 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
124 ? uuid : uuid + strlen(prefix);
126 *uuid_len = strlen(*uuid_start);
128 if (*uuid_len < strlen(UUID_STR))
131 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
132 UUID_STR, strlen(UUID_STR)))
133 *uuid_len -= strlen(UUID_STR);
135 EXPORT_SYMBOL(deuuidify);
138 * Returns true if import was FULL, false if import was already not
140 * @imp - import to be disconnected
141 * @conn_cnt - connection count (epoch) of the request that timed out
142 * and caused the disconnection. In some cases, multiple
143 * inflight requests can fail to a single target (e.g. OST
144 * bulk requests) and if one has already caused a reconnection
145 * (increasing the import->conn_cnt) the older failure should
146 * not also cause a reconnection. If zero it forces a reconnect.
148 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
152 spin_lock(&imp->imp_lock);
154 if (imp->imp_state == LUSTRE_IMP_FULL &&
155 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
159 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
160 &target_start, &target_len);
162 if (imp->imp_replayable) {
163 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
164 "lost; in progress operations using this "
165 "service will wait for recovery to complete\n",
166 imp->imp_obd->obd_name, target_len, target_start,
167 libcfs_nid2str(imp->imp_connection->c_peer.nid));
169 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
170 "%.*s (at %s) was lost; in progress "
171 "operations using this service will fail\n",
172 imp->imp_obd->obd_name,
173 target_len, target_start,
174 libcfs_nid2str(imp->imp_connection->c_peer.nid));
176 ptlrpc_deactivate_timeouts(imp);
177 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
178 spin_unlock(&imp->imp_lock);
180 if (obd_dump_on_timeout)
181 libcfs_debug_dumplog();
183 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
186 spin_unlock(&imp->imp_lock);
187 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
188 imp->imp_client->cli_name, imp,
189 (imp->imp_state == LUSTRE_IMP_FULL &&
190 imp->imp_conn_cnt > conn_cnt) ?
191 "reconnected" : "not connected", imp->imp_conn_cnt,
192 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
198 /* Must be called with imp_lock held! */
199 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
202 LASSERT_SPIN_LOCKED(&imp->imp_lock);
204 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
205 imp->imp_invalid = 1;
206 imp->imp_generation++;
207 spin_unlock(&imp->imp_lock);
209 ptlrpc_abort_inflight(imp);
210 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
216 * This acts as a barrier; all existing requests are rejected, and
217 * no new requests will be accepted until the import is valid again.
219 void ptlrpc_deactivate_import(struct obd_import *imp)
221 spin_lock(&imp->imp_lock);
222 ptlrpc_deactivate_and_unlock_import(imp);
224 EXPORT_SYMBOL(ptlrpc_deactivate_import);
227 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
231 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
232 (req->rq_phase == RQ_PHASE_BULK) ||
233 (req->rq_phase == RQ_PHASE_NEW)))
236 if (req->rq_timedout)
239 if (req->rq_phase == RQ_PHASE_NEW)
242 dl = req->rq_deadline;
250 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
252 time_t now = cfs_time_current_sec();
254 struct ptlrpc_request *req;
255 unsigned int timeout = 0;
257 spin_lock(&imp->imp_lock);
258 cfs_list_for_each_safe(tmp, n, &imp->imp_sending_list) {
259 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
260 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
262 spin_unlock(&imp->imp_lock);
267 * This function will invalidate the import, if necessary, then block
268 * for all the RPC completions, and finally notify the obd to
269 * invalidate its state (ie cancel locks, clear pending requests,
272 void ptlrpc_invalidate_import(struct obd_import *imp)
275 struct ptlrpc_request *req;
276 struct l_wait_info lwi;
277 unsigned int timeout;
280 cfs_atomic_inc(&imp->imp_inval_count);
282 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
283 ptlrpc_deactivate_import(imp);
285 LASSERT(imp->imp_invalid);
287 /* Wait forever until inflight == 0. We really can't do it another
288 * way because in some cases we need to wait for very long reply
289 * unlink. We can't do anything before that because there is really
290 * no guarantee that some rdma transfer is not in progress right now. */
292 /* Calculate max timeout for waiting on rpcs to error
293 * out. Use obd_timeout if calculated value is smaller
295 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
296 timeout = ptlrpc_inflight_timeout(imp);
297 timeout += timeout / 3;
300 timeout = obd_timeout;
302 /* decrease the interval to increase race condition */
306 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
309 /* Wait for all requests to error out and call completion
310 * callbacks. Cap it at obd_timeout -- these should all
311 * have been locally cancelled by ptlrpc_abort_inflight. */
312 lwi = LWI_TIMEOUT_INTERVAL(
313 cfs_timeout_cap(cfs_time_seconds(timeout)),
314 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
316 rc = l_wait_event(imp->imp_recovery_waitq,
317 (cfs_atomic_read(&imp->imp_inflight) == 0),
320 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
322 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
324 cfs_atomic_read(&imp->imp_inflight));
326 spin_lock(&imp->imp_lock);
327 if (cfs_atomic_read(&imp->imp_inflight) == 0) {
328 int count = cfs_atomic_read(&imp->imp_unregistering);
330 /* We know that "unregistering" rpcs only can
331 * survive in sending or delaying lists (they
332 * maybe waiting for long reply unlink in
333 * sluggish nets). Let's check this. If there
334 * is no inflight and unregistering != 0, this
336 LASSERTF(count == 0, "Some RPCs are still "
337 "unregistering: %d\n", count);
339 /* Let's save one loop as soon as inflight have
340 * dropped to zero. No new inflights possible at
344 cfs_list_for_each_safe(tmp, n,
345 &imp->imp_sending_list) {
346 req = cfs_list_entry(tmp,
347 struct ptlrpc_request,
349 DEBUG_REQ(D_ERROR, req,
350 "still on sending list");
352 cfs_list_for_each_safe(tmp, n,
353 &imp->imp_delayed_list) {
354 req = cfs_list_entry(tmp,
355 struct ptlrpc_request,
357 DEBUG_REQ(D_ERROR, req,
358 "still on delayed list");
361 CERROR("%s: RPCs in \"%s\" phase found (%d). "
362 "Network is sluggish? Waiting them "
363 "to error out.\n", cli_tgt,
364 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
365 cfs_atomic_read(&imp->
368 spin_unlock(&imp->imp_lock);
373 * Let's additionally check that no new rpcs added to import in
374 * "invalidate" state.
376 LASSERT(cfs_atomic_read(&imp->imp_inflight) == 0);
377 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
378 sptlrpc_import_flush_all_ctx(imp);
380 cfs_atomic_dec(&imp->imp_inval_count);
381 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
383 EXPORT_SYMBOL(ptlrpc_invalidate_import);
385 /* unset imp_invalid */
386 void ptlrpc_activate_import(struct obd_import *imp)
388 struct obd_device *obd = imp->imp_obd;
390 spin_lock(&imp->imp_lock);
391 imp->imp_invalid = 0;
392 ptlrpc_activate_timeouts(imp);
393 spin_unlock(&imp->imp_lock);
394 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
396 EXPORT_SYMBOL(ptlrpc_activate_import);
398 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
402 LASSERT(!imp->imp_dlm_fake);
404 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
405 if (!imp->imp_replayable) {
406 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
407 "auto-deactivating\n",
408 obd2cli_tgt(imp->imp_obd),
409 imp->imp_connection->c_remote_uuid.uuid,
410 imp->imp_obd->obd_name);
411 ptlrpc_deactivate_import(imp);
414 CDEBUG(D_HA, "%s: waking up pinger\n",
415 obd2cli_tgt(imp->imp_obd));
417 spin_lock(&imp->imp_lock);
418 imp->imp_force_verify = 1;
419 spin_unlock(&imp->imp_lock);
421 ptlrpc_pinger_wake_up();
425 EXPORT_SYMBOL(ptlrpc_fail_import);
427 int ptlrpc_reconnect_import(struct obd_import *imp)
429 ptlrpc_set_import_discon(imp, 0);
430 /* Force a new connect attempt */
431 ptlrpc_invalidate_import(imp);
432 /* Do a fresh connect next time by zeroing the handle */
433 ptlrpc_disconnect_import(imp, 1);
434 /* Wait for all invalidate calls to finish */
435 if (cfs_atomic_read(&imp->imp_inval_count) > 0) {
437 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
438 rc = l_wait_event(imp->imp_recovery_waitq,
439 (cfs_atomic_read(&imp->imp_inval_count) == 0),
442 CERROR("Interrupted, inval=%d\n",
443 cfs_atomic_read(&imp->imp_inval_count));
446 /* Allow reconnect attempts */
447 imp->imp_obd->obd_no_recov = 0;
448 /* Remove 'invalid' flag */
449 ptlrpc_activate_import(imp);
450 /* Attempt a new connect */
451 ptlrpc_recover_import(imp, NULL, 0);
454 EXPORT_SYMBOL(ptlrpc_reconnect_import);
457 * Connection on import \a imp is changed to another one (if more than one is
458 * present). We typically chose connection that we have not tried to connect to
461 static int import_select_connection(struct obd_import *imp)
463 struct obd_import_conn *imp_conn = NULL, *conn;
464 struct obd_export *dlmexp;
466 int target_len, tried_all = 1;
469 spin_lock(&imp->imp_lock);
471 if (cfs_list_empty(&imp->imp_conn_list)) {
472 CERROR("%s: no connections available\n",
473 imp->imp_obd->obd_name);
474 spin_unlock(&imp->imp_lock);
478 cfs_list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
479 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
480 imp->imp_obd->obd_name,
481 libcfs_nid2str(conn->oic_conn->c_peer.nid),
482 conn->oic_last_attempt);
484 /* If we have not tried this connection since
485 the last successful attempt, go with this one */
486 if ((conn->oic_last_attempt == 0) ||
487 cfs_time_beforeq_64(conn->oic_last_attempt,
488 imp->imp_last_success_conn)) {
494 /* If all of the connections have already been tried
495 since the last successful connection; just choose the
496 least recently used */
499 else if (cfs_time_before_64(conn->oic_last_attempt,
500 imp_conn->oic_last_attempt))
504 /* if not found, simply choose the current one */
505 if (!imp_conn || imp->imp_force_reconnect) {
506 LASSERT(imp->imp_conn_current);
507 imp_conn = imp->imp_conn_current;
510 LASSERT(imp_conn->oic_conn);
512 /* If we've tried everything, and we're back to the beginning of the
513 list, increase our timeout and try again. It will be reset when
514 we do finally connect. (FIXME: really we should wait for all network
515 state associated with the last connection attempt to drain before
516 trying to reconnect on it.) */
517 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
518 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
519 if (at_get(at) < CONNECTION_SWITCH_MAX) {
520 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
521 if (at_get(at) > CONNECTION_SWITCH_MAX)
522 at_reset(at, CONNECTION_SWITCH_MAX);
524 LASSERT(imp_conn->oic_last_attempt);
525 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
526 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
529 imp_conn->oic_last_attempt = cfs_time_current_64();
531 /* switch connection, don't mind if it's same as the current one */
532 if (imp->imp_connection)
533 ptlrpc_connection_put(imp->imp_connection);
534 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
536 dlmexp = class_conn2export(&imp->imp_dlm_handle);
537 LASSERT(dlmexp != NULL);
538 if (dlmexp->exp_connection)
539 ptlrpc_connection_put(dlmexp->exp_connection);
540 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
541 class_export_put(dlmexp);
543 if (imp->imp_conn_current != imp_conn) {
544 if (imp->imp_conn_current) {
545 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
546 &target_start, &target_len);
548 CDEBUG(D_HA, "%s: Connection changing to"
550 imp->imp_obd->obd_name,
551 target_len, target_start,
552 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
555 imp->imp_conn_current = imp_conn;
558 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
559 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
560 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
562 spin_unlock(&imp->imp_lock);
568 * must be called under imp_lock
570 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
572 struct ptlrpc_request *req;
575 if (cfs_list_empty(&imp->imp_replay_list))
577 tmp = imp->imp_replay_list.next;
578 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_replay_list);
579 *transno = req->rq_transno;
580 if (req->rq_transno == 0) {
581 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
589 * Attempt to (re)connect import \a imp. This includes all preparations,
590 * initializing CONNECT RPC request and passing it to ptlrpcd for
592 * Returns 0 on success or error code.
594 int ptlrpc_connect_import(struct obd_import *imp)
596 struct obd_device *obd = imp->imp_obd;
597 int initial_connect = 0;
599 __u64 committed_before_reconnect = 0;
600 struct ptlrpc_request *request;
601 char *bufs[] = { NULL,
602 obd2cli_tgt(imp->imp_obd),
604 (char *)&imp->imp_dlm_handle,
605 (char *)&imp->imp_connect_data };
606 struct ptlrpc_connect_async_args *aa;
610 spin_lock(&imp->imp_lock);
611 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
612 spin_unlock(&imp->imp_lock);
613 CERROR("can't connect to a closed import\n");
615 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
616 spin_unlock(&imp->imp_lock);
617 CERROR("already connected\n");
619 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
620 spin_unlock(&imp->imp_lock);
621 CERROR("already connecting\n");
625 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
628 imp->imp_resend_replay = 0;
630 if (!lustre_handle_is_used(&imp->imp_remote_handle))
633 committed_before_reconnect = imp->imp_peer_committed_transno;
635 set_transno = ptlrpc_first_transno(imp,
636 &imp->imp_connect_data.ocd_transno);
637 spin_unlock(&imp->imp_lock);
639 rc = import_select_connection(imp);
643 rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
647 /* Reset connect flags to the originally requested flags, in case
648 * the server is updated on-the-fly we will get the new features. */
649 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
650 /* Reset ocd_version each time so the server knows the exact versions */
651 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
652 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
653 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
655 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
656 &obd->obd_uuid, &imp->imp_connect_data, NULL);
660 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
662 GOTO(out, rc = -ENOMEM);
664 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
665 imp->imp_connect_op, bufs, NULL);
667 ptlrpc_request_free(request);
671 /* Report the rpc service time to the server so that it knows how long
672 * to wait for clients to join recovery */
673 lustre_msg_set_service_time(request->rq_reqmsg,
674 at_timeout2est(request->rq_timeout));
676 /* The amount of time we give the server to process the connect req.
677 * import_select_connection will increase the net latency on
678 * repeated reconnect attempts to cover slow networks.
679 * We override/ignore the server rpc completion estimate here,
680 * which may be large if this is a reconnect attempt */
681 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
682 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
685 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
687 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
689 request->rq_no_resend = request->rq_no_delay = 1;
690 request->rq_send_state = LUSTRE_IMP_CONNECTING;
691 /* Allow a slightly larger reply for future growth compatibility */
692 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
693 sizeof(struct obd_connect_data)+16*sizeof(__u64));
694 ptlrpc_request_set_replen(request);
695 request->rq_interpret_reply = ptlrpc_connect_interpret;
697 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
698 aa = ptlrpc_req_async_args(request);
699 memset(aa, 0, sizeof *aa);
701 aa->pcaa_peer_committed = committed_before_reconnect;
702 aa->pcaa_initial_connect = initial_connect;
704 if (aa->pcaa_initial_connect) {
705 spin_lock(&imp->imp_lock);
706 imp->imp_replayable = 1;
707 spin_unlock(&imp->imp_lock);
708 lustre_msg_add_op_flags(request->rq_reqmsg,
709 MSG_CONNECT_INITIAL);
713 lustre_msg_add_op_flags(request->rq_reqmsg,
714 MSG_CONNECT_TRANSNO);
716 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
717 request->rq_timeout);
718 ptlrpcd_add_req(request, PDL_POLICY_ROUND, -1);
722 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
727 EXPORT_SYMBOL(ptlrpc_connect_import);
729 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
734 spin_lock(&imp->imp_lock);
735 force_verify = imp->imp_force_verify != 0;
736 spin_unlock(&imp->imp_lock);
739 ptlrpc_pinger_wake_up();
741 /* liblustre has no pinger thread, so we wakeup pinger anyway */
742 ptlrpc_pinger_wake_up();
746 static int ptlrpc_busy_reconnect(int rc)
748 return (rc == -EBUSY) || (rc == -EAGAIN);
752 * interpret_reply callback for connect RPCs.
753 * Looks into returned status of connect operation and decides
754 * what to do with the import - i.e enter recovery, promote it to
755 * full state for normal operations of disconnect it due to an error.
757 static int ptlrpc_connect_interpret(const struct lu_env *env,
758 struct ptlrpc_request *request,
761 struct ptlrpc_connect_async_args *aa = data;
762 struct obd_import *imp = request->rq_import;
763 struct client_obd *cli = &imp->imp_obd->u.cli;
764 struct lustre_handle old_hdl;
765 __u64 old_connect_flags;
767 struct obd_connect_data *ocd;
768 struct obd_export *exp;
772 spin_lock(&imp->imp_lock);
773 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
774 spin_unlock(&imp->imp_lock);
779 /* if this reconnect to busy export - not need select new target
781 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
782 spin_unlock(&imp->imp_lock);
783 ptlrpc_maybe_ping_import_soon(imp);
786 spin_unlock(&imp->imp_lock);
788 LASSERT(imp->imp_conn_current);
790 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
792 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
794 /* server replied obd_connect_data is always bigger */
795 ocd = req_capsule_server_sized_get(&request->rq_pill,
796 &RMF_CONNECT_DATA, ret);
799 CERROR("%s: no connect data from server\n",
800 imp->imp_obd->obd_name);
805 spin_lock(&imp->imp_lock);
807 /* All imports are pingable */
808 imp->imp_pingable = 1;
809 imp->imp_force_reconnect = 0;
810 imp->imp_force_verify = 0;
812 imp->imp_connect_data = *ocd;
814 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
815 imp->imp_obd->obd_name, ocd->ocd_instance);
816 exp = class_conn2export(&imp->imp_dlm_handle);
818 spin_unlock(&imp->imp_lock);
820 /* check that server granted subset of flags we asked for. */
821 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
822 ocd->ocd_connect_flags) {
823 CERROR("%s: Server didn't granted asked subset of flags: "
824 "asked="LPX64" grranted="LPX64"\n",
825 imp->imp_obd->obd_name,imp->imp_connect_flags_orig,
826 ocd->ocd_connect_flags);
827 GOTO(out, rc = -EPROTO);
831 /* This could happen if export is cleaned during the
833 CERROR("%s: missing export after connect\n",
834 imp->imp_obd->obd_name);
835 GOTO(out, rc = -ENODEV);
837 old_connect_flags = exp_connect_flags(exp);
838 exp->exp_connect_data = *ocd;
839 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
840 class_export_put(exp);
842 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
844 if (aa->pcaa_initial_connect) {
845 spin_lock(&imp->imp_lock);
846 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
847 imp->imp_replayable = 1;
848 spin_unlock(&imp->imp_lock);
849 CDEBUG(D_HA, "connected to replayable target: %s\n",
850 obd2cli_tgt(imp->imp_obd));
852 imp->imp_replayable = 0;
853 spin_unlock(&imp->imp_lock);
856 /* if applies, adjust the imp->imp_msg_magic here
857 * according to reply flags */
859 imp->imp_remote_handle =
860 *lustre_msg_get_handle(request->rq_repmsg);
862 /* Initial connects are allowed for clients with non-random
863 * uuids when servers are in recovery. Simply signal the
864 * servers replay is complete and wait in REPLAY_WAIT. */
865 if (msg_flags & MSG_CONNECT_RECOVERING) {
866 CDEBUG(D_HA, "connect to %s during recovery\n",
867 obd2cli_tgt(imp->imp_obd));
868 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
870 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
871 ptlrpc_activate_import(imp);
874 GOTO(finish, rc = 0);
877 /* Determine what recovery state to move the import to. */
878 if (MSG_CONNECT_RECONNECT & msg_flags) {
879 memset(&old_hdl, 0, sizeof(old_hdl));
880 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
882 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
883 "bad handle "LPX64"\n",
884 obd2cli_tgt(imp->imp_obd),
885 imp->imp_connection->c_remote_uuid.uuid,
886 imp->imp_dlm_handle.cookie);
887 GOTO(out, rc = -ENOTCONN);
890 if (memcmp(&imp->imp_remote_handle,
891 lustre_msg_get_handle(request->rq_repmsg),
892 sizeof(imp->imp_remote_handle))) {
893 int level = msg_flags & MSG_CONNECT_RECOVERING ?
896 /* Bug 16611/14775: if server handle have changed,
897 * that means some sort of disconnection happened.
898 * If the server is not in recovery, that also means it
899 * already erased all of our state because of previous
900 * eviction. If it is in recovery - we are safe to
901 * participate since we can reestablish all of our state
902 * with server again */
903 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
904 CDEBUG(level,"%s@%s changed server handle from "
906 " but is still in recovery\n",
907 obd2cli_tgt(imp->imp_obd),
908 imp->imp_connection->c_remote_uuid.uuid,
909 imp->imp_remote_handle.cookie,
910 lustre_msg_get_handle(
911 request->rq_repmsg)->cookie);
913 LCONSOLE_WARN("Evicted from %s (at %s) "
914 "after server handle changed from "
915 LPX64" to "LPX64"\n",
916 obd2cli_tgt(imp->imp_obd),
917 imp->imp_connection-> \
919 imp->imp_remote_handle.cookie,
920 lustre_msg_get_handle(
921 request->rq_repmsg)->cookie);
925 imp->imp_remote_handle =
926 *lustre_msg_get_handle(request->rq_repmsg);
928 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
929 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
930 GOTO(finish, rc = 0);
934 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
935 obd2cli_tgt(imp->imp_obd),
936 imp->imp_connection->c_remote_uuid.uuid);
939 if (imp->imp_invalid) {
940 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
941 "marking evicted\n", imp->imp_obd->obd_name);
942 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
943 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
944 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
945 imp->imp_obd->obd_name,
946 obd2cli_tgt(imp->imp_obd));
948 spin_lock(&imp->imp_lock);
949 imp->imp_resend_replay = 1;
950 spin_unlock(&imp->imp_lock);
952 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
954 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
956 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
957 LASSERT(imp->imp_replayable);
958 imp->imp_remote_handle =
959 *lustre_msg_get_handle(request->rq_repmsg);
960 imp->imp_last_replay_transno = 0;
961 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
963 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
964 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
965 imp->imp_remote_handle =
966 *lustre_msg_get_handle(request->rq_repmsg);
967 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
970 /* Sanity checks for a reconnected import. */
971 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
972 CERROR("imp_replayable flag does not match server "
973 "after reconnect. We should LBUG right here.\n");
976 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
977 lustre_msg_get_last_committed(request->rq_repmsg) <
978 aa->pcaa_peer_committed) {
979 CERROR("%s went back in time (transno "LPD64
980 " was previously committed, server now claims "LPD64
981 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
983 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
984 lustre_msg_get_last_committed(request->rq_repmsg));
988 rc = ptlrpc_import_recovery_state_machine(imp);
990 if (rc == -ENOTCONN) {
991 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
992 "invalidating and reconnecting\n",
993 obd2cli_tgt(imp->imp_obd),
994 imp->imp_connection->c_remote_uuid.uuid);
995 ptlrpc_connect_import(imp);
1000 spin_lock(&imp->imp_lock);
1001 cfs_list_del(&imp->imp_conn_current->oic_item);
1002 cfs_list_add(&imp->imp_conn_current->oic_item,
1003 &imp->imp_conn_list);
1004 imp->imp_last_success_conn =
1005 imp->imp_conn_current->oic_last_attempt;
1007 spin_unlock(&imp->imp_lock);
1009 if (!ocd->ocd_ibits_known &&
1010 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
1011 CERROR("Inodebits aware server returned zero compatible"
1014 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1015 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1016 LUSTRE_VERSION_OFFSET_WARN ||
1017 ocd->ocd_version < LUSTRE_VERSION_CODE -
1018 LUSTRE_VERSION_OFFSET_WARN)) {
1019 /* Sigh, some compilers do not like #ifdef in the middle
1020 of macro arguments */
1022 const char *older = "older. Consider upgrading server "
1023 "or downgrading client";
1025 const char *older = "older. Consider recompiling this "
1028 const char *newer = "newer than client version. "
1029 "Consider upgrading client";
1031 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
1032 "is much %s (%s)\n",
1033 obd2cli_tgt(imp->imp_obd),
1034 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1035 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1036 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1037 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1038 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1039 newer : older, LUSTRE_VERSION_STRING);
1042 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1043 /* Check if server has LU-1252 fix applied to not always swab
1044 * the IR MNE entries. Do this only once per connection. This
1045 * fixup is version-limited, because we don't want to carry the
1046 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
1047 * need interop with unpatched 2.2 servers. For newer servers,
1048 * the client will do MNE swabbing only as needed. LU-1644 */
1049 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1050 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
1051 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
1052 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
1053 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
1054 strcmp(imp->imp_obd->obd_type->typ_name,
1055 LUSTRE_MGC_NAME) == 0))
1056 imp->imp_need_mne_swab = 1;
1057 else /* clear if server was upgraded since last connect */
1058 imp->imp_need_mne_swab = 0;
1060 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1063 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1064 /* We sent to the server ocd_cksum_types with bits set
1065 * for algorithms we understand. The server masked off
1066 * the checksum types it doesn't support */
1067 if ((ocd->ocd_cksum_types &
1068 cksum_types_supported_client()) == 0) {
1069 LCONSOLE_WARN("The negotiation of the checksum "
1070 "alogrithm to use with server %s "
1071 "failed (%x/%x), disabling "
1073 obd2cli_tgt(imp->imp_obd),
1074 ocd->ocd_cksum_types,
1075 cksum_types_supported_client());
1076 cli->cl_checksum = 0;
1077 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1079 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1082 /* The server does not support OBD_CONNECT_CKSUM.
1083 * Enforce ADLER for backward compatibility*/
1084 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1086 cli->cl_cksum_type =cksum_type_select(cli->cl_supp_cksum_types);
1088 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
1089 cli->cl_max_pages_per_rpc =
1090 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
1091 else if (imp->imp_connect_op == MDS_CONNECT ||
1092 imp->imp_connect_op == MGS_CONNECT)
1093 cli->cl_max_pages_per_rpc = 1;
1095 /* Reset ns_connect_flags only for initial connect. It might be
1096 * changed in while using FS and if we reset it in reconnect
1097 * this leads to losing user settings done before such as
1098 * disable lru_resize, etc. */
1099 if (old_connect_flags != exp_connect_flags(exp) ||
1100 aa->pcaa_initial_connect) {
1101 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
1102 "flags: "LPX64"\n", imp->imp_obd->obd_name,
1103 ocd->ocd_connect_flags);
1104 imp->imp_obd->obd_namespace->ns_connect_flags =
1105 ocd->ocd_connect_flags;
1106 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1107 ocd->ocd_connect_flags;
1110 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1111 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1112 /* We need a per-message support flag, because
1113 a. we don't know if the incoming connect reply
1114 supports AT or not (in reply_in_callback)
1116 b. failovered server means export and flags are gone
1117 (in ptlrpc_send_reply).
1118 Can only be set when we know AT is supported at
1120 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1122 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1124 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1125 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1126 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1128 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1130 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1131 (cli->cl_max_pages_per_rpc > 0));
1136 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1137 if (rc == -EACCES) {
1139 * Give up trying to reconnect
1140 * EACCES means client has no permission for connection
1142 imp->imp_obd->obd_no_recov = 1;
1143 ptlrpc_deactivate_import(imp);
1146 if (rc == -EPROTO) {
1147 struct obd_connect_data *ocd;
1149 /* reply message might not be ready */
1150 if (request->rq_repmsg == NULL)
1153 ocd = req_capsule_server_get(&request->rq_pill,
1156 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1157 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1158 /* Actually servers are only supposed to refuse
1159 connection from liblustre clients, so we should
1160 never see this from VFS context */
1161 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1163 " refused connection from this client "
1164 "with an incompatible version (%s). "
1165 "Client must be recompiled\n",
1166 obd2cli_tgt(imp->imp_obd),
1167 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1168 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1169 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1170 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1171 LUSTRE_VERSION_STRING);
1172 ptlrpc_deactivate_import(imp);
1173 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1178 ptlrpc_maybe_ping_import_soon(imp);
1180 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1181 obd2cli_tgt(imp->imp_obd),
1182 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1185 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1190 * interpret callback for "completed replay" RPCs.
1191 * \see signal_completed_replay
1193 static int completed_replay_interpret(const struct lu_env *env,
1194 struct ptlrpc_request *req,
1195 void * data, int rc)
1198 cfs_atomic_dec(&req->rq_import->imp_replay_inflight);
1199 if (req->rq_status == 0 &&
1200 !req->rq_import->imp_vbr_failed) {
1201 ptlrpc_import_recovery_state_machine(req->rq_import);
1203 if (req->rq_import->imp_vbr_failed) {
1205 "%s: version recovery fails, reconnecting\n",
1206 req->rq_import->imp_obd->obd_name);
1208 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1210 req->rq_import->imp_obd->obd_name,
1213 ptlrpc_connect_import(req->rq_import);
1220 * Let server know that we have no requests to replay anymore.
1221 * Achieved by just sending a PING request
1223 static int signal_completed_replay(struct obd_import *imp)
1225 struct ptlrpc_request *req;
1228 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1231 LASSERT(cfs_atomic_read(&imp->imp_replay_inflight) == 0);
1232 cfs_atomic_inc(&imp->imp_replay_inflight);
1234 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1237 cfs_atomic_dec(&imp->imp_replay_inflight);
1241 ptlrpc_request_set_replen(req);
1242 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1243 lustre_msg_add_flags(req->rq_reqmsg,
1244 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1246 req->rq_timeout *= 3;
1247 req->rq_interpret_reply = completed_replay_interpret;
1249 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
1255 * In kernel code all import invalidation happens in its own
1256 * separate thread, so that whatever application happened to encounter
1257 * a problem could still be killed or otherwise continue
1259 static int ptlrpc_invalidate_import_thread(void *data)
1261 struct obd_import *imp = data;
1265 cfs_daemonize_ctxt("ll_imp_inval");
1267 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1268 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1269 imp->imp_connection->c_remote_uuid.uuid);
1271 ptlrpc_invalidate_import(imp);
1273 if (obd_dump_on_eviction) {
1274 CERROR("dump the log upon eviction\n");
1275 libcfs_debug_dumplog();
1278 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1279 ptlrpc_import_recovery_state_machine(imp);
1281 class_import_put(imp);
1287 * This is the state machine for client-side recovery on import.
1289 * Typicaly we have two possibly paths. If we came to server and it is not
1290 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1291 * state and reconnect from scratch.
1292 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1293 * We go through our list of requests to replay and send them to server one by
1295 * After sending all request from the list we change import state to
1296 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1297 * and also all the locks we don't yet have and wait for server to grant us.
1298 * After that we send a special "replay completed" request and change import
1299 * state to IMP_REPLAY_WAIT.
1300 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1301 * state and resend all requests from sending list.
1302 * After that we promote import to FULL state and send all delayed requests
1303 * and import is fully operational after that.
1306 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1314 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1315 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1316 &target_start, &target_len);
1317 /* Don't care about MGC eviction */
1318 if (strcmp(imp->imp_obd->obd_type->typ_name,
1319 LUSTRE_MGC_NAME) != 0) {
1320 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1321 "by %.*s; in progress operations "
1322 "using this service will fail.\n",
1323 imp->imp_obd->obd_name, target_len,
1326 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1327 obd2cli_tgt(imp->imp_obd),
1328 imp->imp_connection->c_remote_uuid.uuid);
1329 /* reset vbr_failed flag upon eviction */
1330 spin_lock(&imp->imp_lock);
1331 imp->imp_vbr_failed = 0;
1332 spin_unlock(&imp->imp_lock);
1335 /* bug 17802: XXX client_disconnect_export vs connect request
1336 * race. if client will evicted at this time, we start
1337 * invalidate thread without reference to import and import can
1338 * be freed at same time. */
1339 class_import_get(imp);
1340 rc = cfs_create_thread(ptlrpc_invalidate_import_thread, imp,
1343 class_import_put(imp);
1344 CERROR("error starting invalidate thread: %d\n", rc);
1350 ptlrpc_invalidate_import(imp);
1352 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1356 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1357 CDEBUG(D_HA, "replay requested by %s\n",
1358 obd2cli_tgt(imp->imp_obd));
1359 rc = ptlrpc_replay_next(imp, &inflight);
1360 if (inflight == 0 &&
1361 cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1362 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1363 rc = ldlm_replay_locks(imp);
1370 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1371 if (cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1372 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1373 rc = signal_completed_replay(imp);
1380 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1381 if (cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1382 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1386 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1387 CDEBUG(D_HA, "reconnected to %s@%s\n",
1388 obd2cli_tgt(imp->imp_obd),
1389 imp->imp_connection->c_remote_uuid.uuid);
1391 rc = ptlrpc_resend(imp);
1394 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1395 ptlrpc_activate_import(imp);
1397 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1398 &target_start, &target_len);
1399 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1400 imp->imp_obd->obd_name,
1401 target_len, target_start,
1402 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1405 if (imp->imp_state == LUSTRE_IMP_FULL) {
1406 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1407 ptlrpc_wake_delayed(imp);
1414 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1416 struct ptlrpc_request *req;
1418 int nowait = imp->imp_obd->obd_force;
1422 GOTO(set_state, rc);
1424 switch (imp->imp_connect_op) {
1425 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1426 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1427 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1429 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1430 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1434 if (ptlrpc_import_in_recovery(imp)) {
1435 struct l_wait_info lwi;
1436 cfs_duration_t timeout;
1440 if (imp->imp_server_timeout)
1441 timeout = cfs_time_seconds(obd_timeout / 2);
1443 timeout = cfs_time_seconds(obd_timeout);
1445 int idx = import_at_get_index(imp,
1446 imp->imp_client->cli_request_portal);
1447 timeout = cfs_time_seconds(
1448 at_get(&imp->imp_at.iat_service_estimate[idx]));
1451 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1452 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1453 rc = l_wait_event(imp->imp_recovery_waitq,
1454 !ptlrpc_import_in_recovery(imp), &lwi);
1458 spin_lock(&imp->imp_lock);
1459 if (imp->imp_state != LUSTRE_IMP_FULL)
1462 spin_unlock(&imp->imp_lock);
1464 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1465 LUSTRE_OBD_VERSION, rq_opc);
1467 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1468 * it fails. We can get through the above with a down server
1469 * if the client doesn't know the server is gone yet. */
1470 req->rq_no_resend = 1;
1472 /* We want client umounts to happen quickly, no matter the
1474 req->rq_timeout = min_t(int, req->rq_timeout,
1475 INITIAL_CONNECT_TIMEOUT);
1477 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1478 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1479 ptlrpc_request_set_replen(req);
1480 rc = ptlrpc_queue_wait(req);
1481 ptlrpc_req_finished(req);
1485 spin_lock(&imp->imp_lock);
1488 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1490 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1491 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1492 spin_unlock(&imp->imp_lock);
1496 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1498 void ptlrpc_cleanup_imp(struct obd_import *imp)
1502 spin_lock(&imp->imp_lock);
1503 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1504 imp->imp_generation++;
1505 spin_unlock(&imp->imp_lock);
1506 ptlrpc_abort_inflight(imp);
1510 EXPORT_SYMBOL(ptlrpc_cleanup_imp);
1512 /* Adaptive Timeout utils */
1513 extern unsigned int at_min, at_max, at_history;
1515 /* Bin into timeslices using AT_BINS bins.
1516 This gives us a max of the last binlimit*AT_BINS secs without the storage,
1517 but still smoothing out a return to normalcy from a slow response.
1518 (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1519 int at_measured(struct adaptive_timeout *at, unsigned int val)
1521 unsigned int old = at->at_current;
1522 time_t now = cfs_time_current_sec();
1523 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1526 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1527 val, at, now - at->at_binstart, at->at_current,
1528 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1531 /* 0's don't count, because we never want our timeout to
1532 drop to 0, and because 0 could mean an error */
1535 spin_lock(&at->at_lock);
1537 if (unlikely(at->at_binstart == 0)) {
1538 /* Special case to remove default from history */
1539 at->at_current = val;
1540 at->at_worst_ever = val;
1541 at->at_worst_time = now;
1542 at->at_hist[0] = val;
1543 at->at_binstart = now;
1544 } else if (now - at->at_binstart < binlimit ) {
1546 at->at_hist[0] = max(val, at->at_hist[0]);
1547 at->at_current = max(val, at->at_current);
1550 unsigned int maxv = val;
1551 /* move bins over */
1552 shift = (now - at->at_binstart) / binlimit;
1554 for(i = AT_BINS - 1; i >= 0; i--) {
1556 at->at_hist[i] = at->at_hist[i - shift];
1557 maxv = max(maxv, at->at_hist[i]);
1562 at->at_hist[0] = val;
1563 at->at_current = maxv;
1564 at->at_binstart += shift * binlimit;
1567 if (at->at_current > at->at_worst_ever) {
1568 at->at_worst_ever = at->at_current;
1569 at->at_worst_time = now;
1572 if (at->at_flags & AT_FLG_NOHIST)
1573 /* Only keep last reported val; keeping the rest of the history
1575 at->at_current = val;
1578 at->at_current = min(at->at_current, at_max);
1579 at->at_current = max(at->at_current, at_min);
1581 if (at->at_current != old)
1582 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1583 "(val=%u) hist %u %u %u %u\n", at,
1584 old, at->at_current, at->at_current - old, val,
1585 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1588 /* if we changed, report the old value */
1589 old = (at->at_current != old) ? old : 0;
1591 spin_unlock(&at->at_lock);
1595 /* Find the imp_at index for a given portal; assign if space available */
1596 int import_at_get_index(struct obd_import *imp, int portal)
1598 struct imp_at *at = &imp->imp_at;
1601 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1602 if (at->iat_portal[i] == portal)
1604 if (at->iat_portal[i] == 0)
1609 /* Not found in list, add it under a lock */
1610 spin_lock(&imp->imp_lock);
1612 /* Check unused under lock */
1613 for (; i < IMP_AT_MAX_PORTALS; i++) {
1614 if (at->iat_portal[i] == portal)
1616 if (at->iat_portal[i] == 0)
1621 /* Not enough portals? */
1622 LASSERT(i < IMP_AT_MAX_PORTALS);
1624 at->iat_portal[i] = portal;
1626 spin_unlock(&imp->imp_lock);