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, Whamcloud, Inc.
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 cfs_spin_lock(&imp->imp_lock); \
92 IMPORT_SET_STATE_NOLOCK(imp, state); \
93 cfs_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 cfs_spin_lock(&imp->imp_lock);
111 imp->imp_generation++;
112 imp->imp_state = LUSTRE_IMP_NEW;
114 cfs_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 cfs_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 cfs_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 cfs_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 cfs_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 cfs_spin_lock(&imp->imp_lock);
222 ptlrpc_deactivate_and_unlock_import(imp);
226 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
230 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
231 (req->rq_phase == RQ_PHASE_BULK) ||
232 (req->rq_phase == RQ_PHASE_NEW)))
235 if (req->rq_timedout)
238 if (req->rq_phase == RQ_PHASE_NEW)
241 dl = req->rq_deadline;
249 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
251 time_t now = cfs_time_current_sec();
253 struct ptlrpc_request *req;
254 unsigned int timeout = 0;
256 cfs_spin_lock(&imp->imp_lock);
257 cfs_list_for_each_safe(tmp, n, &imp->imp_sending_list) {
258 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
259 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
261 cfs_spin_unlock(&imp->imp_lock);
266 * This function will invalidate the import, if necessary, then block
267 * for all the RPC completions, and finally notify the obd to
268 * invalidate its state (ie cancel locks, clear pending requests,
271 void ptlrpc_invalidate_import(struct obd_import *imp)
274 struct ptlrpc_request *req;
275 struct l_wait_info lwi;
276 unsigned int timeout;
279 cfs_atomic_inc(&imp->imp_inval_count);
281 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
282 ptlrpc_deactivate_import(imp);
284 LASSERT(imp->imp_invalid);
286 /* Wait forever until inflight == 0. We really can't do it another
287 * way because in some cases we need to wait for very long reply
288 * unlink. We can't do anything before that because there is really
289 * no guarantee that some rdma transfer is not in progress right now. */
291 /* Calculate max timeout for waiting on rpcs to error
292 * out. Use obd_timeout if calculated value is smaller
294 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
295 timeout = ptlrpc_inflight_timeout(imp);
296 timeout += timeout / 3;
299 timeout = obd_timeout;
301 /* decrease the interval to increase race condition */
305 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
308 /* Wait for all requests to error out and call completion
309 * callbacks. Cap it at obd_timeout -- these should all
310 * have been locally cancelled by ptlrpc_abort_inflight. */
311 lwi = LWI_TIMEOUT_INTERVAL(
312 cfs_timeout_cap(cfs_time_seconds(timeout)),
313 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
315 rc = l_wait_event(imp->imp_recovery_waitq,
316 (cfs_atomic_read(&imp->imp_inflight) == 0),
319 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
321 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
323 cfs_atomic_read(&imp->imp_inflight));
325 cfs_spin_lock(&imp->imp_lock);
326 if (cfs_atomic_read(&imp->imp_inflight) == 0) {
327 int count = cfs_atomic_read(&imp->imp_unregistering);
329 /* We know that "unregistering" rpcs only can
330 * survive in sending or delaying lists (they
331 * maybe waiting for long reply unlink in
332 * sluggish nets). Let's check this. If there
333 * is no inflight and unregistering != 0, this
335 LASSERTF(count == 0, "Some RPCs are still "
336 "unregistering: %d\n", count);
338 /* Let's save one loop as soon as inflight have
339 * dropped to zero. No new inflights possible at
343 cfs_list_for_each_safe(tmp, n,
344 &imp->imp_sending_list) {
345 req = cfs_list_entry(tmp,
346 struct ptlrpc_request,
348 DEBUG_REQ(D_ERROR, req,
349 "still on sending list");
351 cfs_list_for_each_safe(tmp, n,
352 &imp->imp_delayed_list) {
353 req = cfs_list_entry(tmp,
354 struct ptlrpc_request,
356 DEBUG_REQ(D_ERROR, req,
357 "still on delayed list");
360 CERROR("%s: RPCs in \"%s\" phase found (%d). "
361 "Network is sluggish? Waiting them "
362 "to error out.\n", cli_tgt,
363 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
364 cfs_atomic_read(&imp->
367 cfs_spin_unlock(&imp->imp_lock);
372 * Let's additionally check that no new rpcs added to import in
373 * "invalidate" state.
375 LASSERT(cfs_atomic_read(&imp->imp_inflight) == 0);
376 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
377 sptlrpc_import_flush_all_ctx(imp);
379 cfs_atomic_dec(&imp->imp_inval_count);
380 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
383 /* unset imp_invalid */
384 void ptlrpc_activate_import(struct obd_import *imp)
386 struct obd_device *obd = imp->imp_obd;
388 cfs_spin_lock(&imp->imp_lock);
389 imp->imp_invalid = 0;
390 ptlrpc_activate_timeouts(imp);
391 cfs_spin_unlock(&imp->imp_lock);
392 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
395 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
399 LASSERT(!imp->imp_dlm_fake);
401 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
402 if (!imp->imp_replayable) {
403 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
404 "auto-deactivating\n",
405 obd2cli_tgt(imp->imp_obd),
406 imp->imp_connection->c_remote_uuid.uuid,
407 imp->imp_obd->obd_name);
408 ptlrpc_deactivate_import(imp);
411 CDEBUG(D_HA, "%s: waking up pinger\n",
412 obd2cli_tgt(imp->imp_obd));
414 cfs_spin_lock(&imp->imp_lock);
415 imp->imp_force_verify = 1;
416 cfs_spin_unlock(&imp->imp_lock);
418 ptlrpc_pinger_wake_up();
423 int ptlrpc_reconnect_import(struct obd_import *imp)
425 ptlrpc_set_import_discon(imp, 0);
426 /* Force a new connect attempt */
427 ptlrpc_invalidate_import(imp);
428 /* Do a fresh connect next time by zeroing the handle */
429 ptlrpc_disconnect_import(imp, 1);
430 /* Wait for all invalidate calls to finish */
431 if (cfs_atomic_read(&imp->imp_inval_count) > 0) {
433 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
434 rc = l_wait_event(imp->imp_recovery_waitq,
435 (cfs_atomic_read(&imp->imp_inval_count) == 0),
438 CERROR("Interrupted, inval=%d\n",
439 cfs_atomic_read(&imp->imp_inval_count));
442 /* Allow reconnect attempts */
443 imp->imp_obd->obd_no_recov = 0;
444 /* Remove 'invalid' flag */
445 ptlrpc_activate_import(imp);
446 /* Attempt a new connect */
447 ptlrpc_recover_import(imp, NULL, 0);
450 EXPORT_SYMBOL(ptlrpc_reconnect_import);
453 * Connection on import \a imp is changed to another one (if more than one is
454 * present). We typically chose connection that we have not tried to connect to
457 static int import_select_connection(struct obd_import *imp)
459 struct obd_import_conn *imp_conn = NULL, *conn;
460 struct obd_export *dlmexp;
462 int target_len, tried_all = 1;
465 cfs_spin_lock(&imp->imp_lock);
467 if (cfs_list_empty(&imp->imp_conn_list)) {
468 CERROR("%s: no connections available\n",
469 imp->imp_obd->obd_name);
470 cfs_spin_unlock(&imp->imp_lock);
474 cfs_list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
475 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
476 imp->imp_obd->obd_name,
477 libcfs_nid2str(conn->oic_conn->c_peer.nid),
478 conn->oic_last_attempt);
480 /* If we have not tried this connection since
481 the last successful attempt, go with this one */
482 if ((conn->oic_last_attempt == 0) ||
483 cfs_time_beforeq_64(conn->oic_last_attempt,
484 imp->imp_last_success_conn)) {
490 /* If all of the connections have already been tried
491 since the last successful connection; just choose the
492 least recently used */
495 else if (cfs_time_before_64(conn->oic_last_attempt,
496 imp_conn->oic_last_attempt))
500 /* if not found, simply choose the current one */
501 if (!imp_conn || imp->imp_force_reconnect) {
502 LASSERT(imp->imp_conn_current);
503 imp_conn = imp->imp_conn_current;
506 LASSERT(imp_conn->oic_conn);
508 /* If we've tried everything, and we're back to the beginning of the
509 list, increase our timeout and try again. It will be reset when
510 we do finally connect. (FIXME: really we should wait for all network
511 state associated with the last connection attempt to drain before
512 trying to reconnect on it.) */
513 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
514 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
515 if (at_get(at) < CONNECTION_SWITCH_MAX) {
516 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
517 if (at_get(at) > CONNECTION_SWITCH_MAX)
518 at_reset(at, CONNECTION_SWITCH_MAX);
520 LASSERT(imp_conn->oic_last_attempt);
521 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
522 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
525 imp_conn->oic_last_attempt = cfs_time_current_64();
527 /* switch connection, don't mind if it's same as the current one */
528 if (imp->imp_connection)
529 ptlrpc_connection_put(imp->imp_connection);
530 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
532 dlmexp = class_conn2export(&imp->imp_dlm_handle);
533 LASSERT(dlmexp != NULL);
534 if (dlmexp->exp_connection)
535 ptlrpc_connection_put(dlmexp->exp_connection);
536 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
537 class_export_put(dlmexp);
539 if (imp->imp_conn_current != imp_conn) {
540 if (imp->imp_conn_current) {
541 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
542 &target_start, &target_len);
544 CDEBUG(D_HA, "%s: Connection changing to"
546 imp->imp_obd->obd_name,
547 target_len, target_start,
548 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
551 imp->imp_conn_current = imp_conn;
554 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
555 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
556 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
558 cfs_spin_unlock(&imp->imp_lock);
564 * must be called under imp_lock
566 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
568 struct ptlrpc_request *req;
571 if (cfs_list_empty(&imp->imp_replay_list))
573 tmp = imp->imp_replay_list.next;
574 req = cfs_list_entry(tmp, struct ptlrpc_request, rq_replay_list);
575 *transno = req->rq_transno;
576 if (req->rq_transno == 0) {
577 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
585 * Attempt to (re)connect import \a imp. This includes all preparations,
586 * initializing CONNECT RPC request and passing it to ptlrpcd for
588 * Returns 0 on success or error code.
590 int ptlrpc_connect_import(struct obd_import *imp)
592 struct obd_device *obd = imp->imp_obd;
593 int initial_connect = 0;
595 __u64 committed_before_reconnect = 0;
596 struct ptlrpc_request *request;
597 char *bufs[] = { NULL,
598 obd2cli_tgt(imp->imp_obd),
600 (char *)&imp->imp_dlm_handle,
601 (char *)&imp->imp_connect_data };
602 struct ptlrpc_connect_async_args *aa;
606 cfs_spin_lock(&imp->imp_lock);
607 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
608 cfs_spin_unlock(&imp->imp_lock);
609 CERROR("can't connect to a closed import\n");
611 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
612 cfs_spin_unlock(&imp->imp_lock);
613 CERROR("already connected\n");
615 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
616 cfs_spin_unlock(&imp->imp_lock);
617 CERROR("already connecting\n");
621 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
624 imp->imp_resend_replay = 0;
626 if (!lustre_handle_is_used(&imp->imp_remote_handle))
629 committed_before_reconnect = imp->imp_peer_committed_transno;
631 set_transno = ptlrpc_first_transno(imp,
632 &imp->imp_connect_data.ocd_transno);
633 cfs_spin_unlock(&imp->imp_lock);
635 rc = import_select_connection(imp);
639 rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
643 /* Reset connect flags to the originally requested flags, in case
644 * the server is updated on-the-fly we will get the new features. */
645 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
646 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
647 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
649 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
650 &obd->obd_uuid, &imp->imp_connect_data, NULL);
654 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
656 GOTO(out, rc = -ENOMEM);
658 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
659 imp->imp_connect_op, bufs, NULL);
661 ptlrpc_request_free(request);
665 /* Report the rpc service time to the server so that it knows how long
666 * to wait for clients to join recovery */
667 lustre_msg_set_service_time(request->rq_reqmsg,
668 at_timeout2est(request->rq_timeout));
670 /* The amount of time we give the server to process the connect req.
671 * import_select_connection will increase the net latency on
672 * repeated reconnect attempts to cover slow networks.
673 * We override/ignore the server rpc completion estimate here,
674 * which may be large if this is a reconnect attempt */
675 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
676 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
679 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
681 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
683 request->rq_no_resend = request->rq_no_delay = 1;
684 request->rq_send_state = LUSTRE_IMP_CONNECTING;
685 /* Allow a slightly larger reply for future growth compatibility */
686 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
687 sizeof(struct obd_connect_data)+16*sizeof(__u64));
688 ptlrpc_request_set_replen(request);
689 request->rq_interpret_reply = ptlrpc_connect_interpret;
691 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
692 aa = ptlrpc_req_async_args(request);
693 memset(aa, 0, sizeof *aa);
695 aa->pcaa_peer_committed = committed_before_reconnect;
696 aa->pcaa_initial_connect = initial_connect;
698 if (aa->pcaa_initial_connect) {
699 cfs_spin_lock(&imp->imp_lock);
700 imp->imp_replayable = 1;
701 cfs_spin_unlock(&imp->imp_lock);
702 lustre_msg_add_op_flags(request->rq_reqmsg,
703 MSG_CONNECT_INITIAL);
707 lustre_msg_add_op_flags(request->rq_reqmsg,
708 MSG_CONNECT_TRANSNO);
710 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
711 request->rq_timeout);
712 ptlrpcd_add_req(request, PDL_POLICY_ROUND, -1);
716 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
721 EXPORT_SYMBOL(ptlrpc_connect_import);
723 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
728 cfs_spin_lock(&imp->imp_lock);
729 force_verify = imp->imp_force_verify != 0;
730 cfs_spin_unlock(&imp->imp_lock);
733 ptlrpc_pinger_wake_up();
735 /* liblustre has no pinger thread, so we wakeup pinger anyway */
736 ptlrpc_pinger_wake_up();
740 static int ptlrpc_busy_reconnect(int rc)
742 return (rc == -EBUSY) || (rc == -EAGAIN);
746 * interpret_reply callback for connect RPCs.
747 * Looks into returned status of connect operation and decides
748 * what to do with the import - i.e enter recovery, promote it to
749 * full state for normal operations of disconnect it due to an error.
751 static int ptlrpc_connect_interpret(const struct lu_env *env,
752 struct ptlrpc_request *request,
755 struct ptlrpc_connect_async_args *aa = data;
756 struct obd_import *imp = request->rq_import;
757 struct client_obd *cli = &imp->imp_obd->u.cli;
758 struct lustre_handle old_hdl;
759 __u64 old_connect_flags;
763 cfs_spin_lock(&imp->imp_lock);
764 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
765 cfs_spin_unlock(&imp->imp_lock);
770 /* if this reconnect to busy export - not need select new target
772 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
773 cfs_spin_unlock(&imp->imp_lock);
774 ptlrpc_maybe_ping_import_soon(imp);
778 LASSERT(imp->imp_conn_current);
780 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
782 /* All imports are pingable */
783 imp->imp_pingable = 1;
784 imp->imp_force_reconnect = 0;
785 imp->imp_force_verify = 0;
787 if (aa->pcaa_initial_connect) {
788 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
789 imp->imp_replayable = 1;
790 cfs_spin_unlock(&imp->imp_lock);
791 CDEBUG(D_HA, "connected to replayable target: %s\n",
792 obd2cli_tgt(imp->imp_obd));
794 imp->imp_replayable = 0;
795 cfs_spin_unlock(&imp->imp_lock);
798 /* if applies, adjust the imp->imp_msg_magic here
799 * according to reply flags */
801 imp->imp_remote_handle =
802 *lustre_msg_get_handle(request->rq_repmsg);
804 /* Initial connects are allowed for clients with non-random
805 * uuids when servers are in recovery. Simply signal the
806 * servers replay is complete and wait in REPLAY_WAIT. */
807 if (msg_flags & MSG_CONNECT_RECOVERING) {
808 CDEBUG(D_HA, "connect to %s during recovery\n",
809 obd2cli_tgt(imp->imp_obd));
810 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
812 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
813 ptlrpc_activate_import(imp);
816 GOTO(finish, rc = 0);
818 cfs_spin_unlock(&imp->imp_lock);
821 /* Determine what recovery state to move the import to. */
822 if (MSG_CONNECT_RECONNECT & msg_flags) {
823 memset(&old_hdl, 0, sizeof(old_hdl));
824 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
826 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
827 "bad handle "LPX64"\n",
828 obd2cli_tgt(imp->imp_obd),
829 imp->imp_connection->c_remote_uuid.uuid,
830 imp->imp_dlm_handle.cookie);
831 GOTO(out, rc = -ENOTCONN);
834 if (memcmp(&imp->imp_remote_handle,
835 lustre_msg_get_handle(request->rq_repmsg),
836 sizeof(imp->imp_remote_handle))) {
837 int level = msg_flags & MSG_CONNECT_RECOVERING ?
840 /* Bug 16611/14775: if server handle have changed,
841 * that means some sort of disconnection happened.
842 * If the server is not in recovery, that also means it
843 * already erased all of our state because of previous
844 * eviction. If it is in recovery - we are safe to
845 * participate since we can reestablish all of our state
846 * with server again */
847 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
848 CDEBUG(level,"%s@%s changed server handle from "
850 " but is still in recovery\n",
851 obd2cli_tgt(imp->imp_obd),
852 imp->imp_connection->c_remote_uuid.uuid,
853 imp->imp_remote_handle.cookie,
854 lustre_msg_get_handle(
855 request->rq_repmsg)->cookie);
857 LCONSOLE_WARN("Evicted from %s (at %s) "
858 "after server handle changed from "
859 LPX64" to "LPX64"\n",
860 obd2cli_tgt(imp->imp_obd),
861 imp->imp_connection-> \
863 imp->imp_remote_handle.cookie,
864 lustre_msg_get_handle(
865 request->rq_repmsg)->cookie);
869 imp->imp_remote_handle =
870 *lustre_msg_get_handle(request->rq_repmsg);
872 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
873 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
874 GOTO(finish, rc = 0);
878 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
879 obd2cli_tgt(imp->imp_obd),
880 imp->imp_connection->c_remote_uuid.uuid);
883 if (imp->imp_invalid) {
884 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
885 "marking evicted\n", imp->imp_obd->obd_name);
886 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
887 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
888 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
889 imp->imp_obd->obd_name,
890 obd2cli_tgt(imp->imp_obd));
892 cfs_spin_lock(&imp->imp_lock);
893 imp->imp_resend_replay = 1;
894 cfs_spin_unlock(&imp->imp_lock);
896 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
898 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
900 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
901 LASSERT(imp->imp_replayable);
902 imp->imp_remote_handle =
903 *lustre_msg_get_handle(request->rq_repmsg);
904 imp->imp_last_replay_transno = 0;
905 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
907 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
908 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
909 imp->imp_remote_handle =
910 *lustre_msg_get_handle(request->rq_repmsg);
911 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
914 /* Sanity checks for a reconnected import. */
915 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
916 CERROR("imp_replayable flag does not match server "
917 "after reconnect. We should LBUG right here.\n");
920 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
921 lustre_msg_get_last_committed(request->rq_repmsg) <
922 aa->pcaa_peer_committed) {
923 CERROR("%s went back in time (transno "LPD64
924 " was previously committed, server now claims "LPD64
925 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
927 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
928 lustre_msg_get_last_committed(request->rq_repmsg));
932 rc = ptlrpc_import_recovery_state_machine(imp);
934 if (rc == -ENOTCONN) {
935 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
936 "invalidating and reconnecting\n",
937 obd2cli_tgt(imp->imp_obd),
938 imp->imp_connection->c_remote_uuid.uuid);
939 ptlrpc_connect_import(imp);
943 struct obd_connect_data *ocd;
944 struct obd_export *exp;
946 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
948 /* server replied obd_connect_data is always bigger */
949 ocd = req_capsule_server_sized_get(&request->rq_pill,
950 &RMF_CONNECT_DATA, ret);
952 cfs_spin_lock(&imp->imp_lock);
953 cfs_list_del(&imp->imp_conn_current->oic_item);
954 cfs_list_add(&imp->imp_conn_current->oic_item,
955 &imp->imp_conn_list);
956 imp->imp_last_success_conn =
957 imp->imp_conn_current->oic_last_attempt;
960 cfs_spin_unlock(&imp->imp_lock);
961 CERROR("Wrong connect data from server\n");
966 imp->imp_connect_data = *ocd;
967 CDEBUG(D_HA, "obd %s to target with inst %u\n",
968 imp->imp_obd->obd_name, ocd->ocd_instance);
970 exp = class_conn2export(&imp->imp_dlm_handle);
971 cfs_spin_unlock(&imp->imp_lock);
973 /* check that server granted subset of flags we asked for. */
974 LASSERTF((ocd->ocd_connect_flags &
975 imp->imp_connect_flags_orig) ==
976 ocd->ocd_connect_flags, LPX64" != "LPX64,
977 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
980 /* This could happen if export is cleaned during the
982 CERROR("Missing export for %s\n",
983 imp->imp_obd->obd_name);
984 GOTO(out, rc = -ENODEV);
986 old_connect_flags = exp->exp_connect_flags;
987 exp->exp_connect_flags = ocd->ocd_connect_flags;
988 imp->imp_obd->obd_self_export->exp_connect_flags =
989 ocd->ocd_connect_flags;
990 class_export_put(exp);
992 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
994 if (!ocd->ocd_ibits_known &&
995 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
996 CERROR("Inodebits aware server returned zero compatible"
999 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1000 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1001 LUSTRE_VERSION_OFFSET_WARN ||
1002 ocd->ocd_version < LUSTRE_VERSION_CODE -
1003 LUSTRE_VERSION_OFFSET_WARN)) {
1004 /* Sigh, some compilers do not like #ifdef in the middle
1005 of macro arguments */
1007 const char *older = "older. Consider upgrading server "
1008 "or downgrading client";
1010 const char *older = "older. Consider recompiling this "
1013 const char *newer = "newer than client version. "
1014 "Consider upgrading client";
1016 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
1017 "is much %s (%s)\n",
1018 obd2cli_tgt(imp->imp_obd),
1019 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1020 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1021 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1022 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1023 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1024 newer : older, LUSTRE_VERSION_STRING);
1027 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1028 /* We sent to the server ocd_cksum_types with bits set
1029 * for algorithms we understand. The server masked off
1030 * the checksum types it doesn't support */
1031 if ((ocd->ocd_cksum_types &
1032 cksum_types_supported_client()) == 0) {
1033 LCONSOLE_WARN("The negotiation of the checksum "
1034 "alogrithm to use with server %s "
1035 "failed (%x/%x), disabling "
1037 obd2cli_tgt(imp->imp_obd),
1038 ocd->ocd_cksum_types,
1039 cksum_types_supported_client());
1040 cli->cl_checksum = 0;
1041 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1043 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1046 /* The server does not support OBD_CONNECT_CKSUM.
1047 * Enforce ADLER for backward compatibility*/
1048 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1050 cli->cl_cksum_type =cksum_type_select(cli->cl_supp_cksum_types);
1052 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
1053 cli->cl_max_pages_per_rpc =
1054 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
1055 else if (imp->imp_connect_op == MDS_CONNECT ||
1056 imp->imp_connect_op == MGS_CONNECT)
1057 cli->cl_max_pages_per_rpc = 1;
1059 /* Reset ns_connect_flags only for initial connect. It might be
1060 * changed in while using FS and if we reset it in reconnect
1061 * this leads to losing user settings done before such as
1062 * disable lru_resize, etc. */
1063 if (old_connect_flags != exp->exp_connect_flags ||
1064 aa->pcaa_initial_connect) {
1065 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
1066 "flags: "LPX64"\n", imp->imp_obd->obd_name,
1067 ocd->ocd_connect_flags);
1068 imp->imp_obd->obd_namespace->ns_connect_flags =
1069 ocd->ocd_connect_flags;
1070 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1071 ocd->ocd_connect_flags;
1074 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1075 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1076 /* We need a per-message support flag, because
1077 a. we don't know if the incoming connect reply
1078 supports AT or not (in reply_in_callback)
1080 b. failovered server means export and flags are gone
1081 (in ptlrpc_send_reply).
1082 Can only be set when we know AT is supported at
1084 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1086 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1088 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1089 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1090 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1092 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1094 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1095 (cli->cl_max_pages_per_rpc > 0));
1100 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1101 if (rc == -EACCES) {
1103 * Give up trying to reconnect
1104 * EACCES means client has no permission for connection
1106 imp->imp_obd->obd_no_recov = 1;
1107 ptlrpc_deactivate_import(imp);
1110 if (rc == -EPROTO) {
1111 struct obd_connect_data *ocd;
1113 /* reply message might not be ready */
1114 if (request->rq_repmsg == NULL)
1117 ocd = req_capsule_server_get(&request->rq_pill,
1120 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1121 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1122 /* Actually servers are only supposed to refuse
1123 connection from liblustre clients, so we should
1124 never see this from VFS context */
1125 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1127 " refused connection from this client "
1128 "with an incompatible version (%s). "
1129 "Client must be recompiled\n",
1130 obd2cli_tgt(imp->imp_obd),
1131 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1132 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1133 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1134 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1135 LUSTRE_VERSION_STRING);
1136 ptlrpc_deactivate_import(imp);
1137 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1142 ptlrpc_maybe_ping_import_soon(imp);
1144 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1145 obd2cli_tgt(imp->imp_obd),
1146 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1149 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1154 * interpret callback for "completed replay" RPCs.
1155 * \see signal_completed_replay
1157 static int completed_replay_interpret(const struct lu_env *env,
1158 struct ptlrpc_request *req,
1159 void * data, int rc)
1162 cfs_atomic_dec(&req->rq_import->imp_replay_inflight);
1163 if (req->rq_status == 0 &&
1164 !req->rq_import->imp_vbr_failed) {
1165 ptlrpc_import_recovery_state_machine(req->rq_import);
1167 if (req->rq_import->imp_vbr_failed) {
1169 "%s: version recovery fails, reconnecting\n",
1170 req->rq_import->imp_obd->obd_name);
1172 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1174 req->rq_import->imp_obd->obd_name,
1177 ptlrpc_connect_import(req->rq_import);
1184 * Let server know that we have no requests to replay anymore.
1185 * Achieved by just sending a PING request
1187 static int signal_completed_replay(struct obd_import *imp)
1189 struct ptlrpc_request *req;
1192 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1195 LASSERT(cfs_atomic_read(&imp->imp_replay_inflight) == 0);
1196 cfs_atomic_inc(&imp->imp_replay_inflight);
1198 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1201 cfs_atomic_dec(&imp->imp_replay_inflight);
1205 ptlrpc_request_set_replen(req);
1206 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1207 lustre_msg_add_flags(req->rq_reqmsg,
1208 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1210 req->rq_timeout *= 3;
1211 req->rq_interpret_reply = completed_replay_interpret;
1213 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
1219 * In kernel code all import invalidation happens in its own
1220 * separate thread, so that whatever application happened to encounter
1221 * a problem could still be killed or otherwise continue
1223 static int ptlrpc_invalidate_import_thread(void *data)
1225 struct obd_import *imp = data;
1229 cfs_daemonize_ctxt("ll_imp_inval");
1231 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1232 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1233 imp->imp_connection->c_remote_uuid.uuid);
1235 ptlrpc_invalidate_import(imp);
1237 if (obd_dump_on_eviction) {
1238 CERROR("dump the log upon eviction\n");
1239 libcfs_debug_dumplog();
1242 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1243 ptlrpc_import_recovery_state_machine(imp);
1245 class_import_put(imp);
1251 * This is the state machine for client-side recovery on import.
1253 * Typicaly we have two possibly paths. If we came to server and it is not
1254 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1255 * state and reconnect from scratch.
1256 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1257 * We go through our list of requests to replay and send them to server one by
1259 * After sending all request from the list we change import state to
1260 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1261 * and also all the locks we don't yet have and wait for server to grant us.
1262 * After that we send a special "replay completed" request and change import
1263 * state to IMP_REPLAY_WAIT.
1264 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1265 * state and resend all requests from sending list.
1266 * After that we promote import to FULL state and send all delayed requests
1267 * and import is fully operational after that.
1270 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1278 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1279 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1280 &target_start, &target_len);
1281 /* Don't care about MGC eviction */
1282 if (strcmp(imp->imp_obd->obd_type->typ_name,
1283 LUSTRE_MGC_NAME) != 0) {
1284 LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1285 "%.*s; in progress operations using "
1286 "this service will fail.\n",
1287 target_len, target_start);
1289 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1290 obd2cli_tgt(imp->imp_obd),
1291 imp->imp_connection->c_remote_uuid.uuid);
1292 /* reset vbr_failed flag upon eviction */
1293 cfs_spin_lock(&imp->imp_lock);
1294 imp->imp_vbr_failed = 0;
1295 cfs_spin_unlock(&imp->imp_lock);
1298 /* bug 17802: XXX client_disconnect_export vs connect request
1299 * race. if client will evicted at this time, we start
1300 * invalidate thread without reference to import and import can
1301 * be freed at same time. */
1302 class_import_get(imp);
1303 rc = cfs_create_thread(ptlrpc_invalidate_import_thread, imp,
1306 class_import_put(imp);
1307 CERROR("error starting invalidate thread: %d\n", rc);
1313 ptlrpc_invalidate_import(imp);
1315 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1319 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1320 CDEBUG(D_HA, "replay requested by %s\n",
1321 obd2cli_tgt(imp->imp_obd));
1322 rc = ptlrpc_replay_next(imp, &inflight);
1323 if (inflight == 0 &&
1324 cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1325 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1326 rc = ldlm_replay_locks(imp);
1333 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1334 if (cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1335 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1336 rc = signal_completed_replay(imp);
1343 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1344 if (cfs_atomic_read(&imp->imp_replay_inflight) == 0) {
1345 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1349 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1350 CDEBUG(D_HA, "reconnected to %s@%s\n",
1351 obd2cli_tgt(imp->imp_obd),
1352 imp->imp_connection->c_remote_uuid.uuid);
1354 rc = ptlrpc_resend(imp);
1357 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1358 ptlrpc_activate_import(imp);
1360 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1361 &target_start, &target_len);
1362 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1363 imp->imp_obd->obd_name,
1364 target_len, target_start,
1365 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1368 if (imp->imp_state == LUSTRE_IMP_FULL) {
1369 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1370 ptlrpc_wake_delayed(imp);
1377 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1379 struct ptlrpc_request *req;
1381 int nowait = imp->imp_obd->obd_force;
1385 GOTO(set_state, rc);
1387 switch (imp->imp_connect_op) {
1388 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1389 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1390 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1392 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1393 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1397 if (ptlrpc_import_in_recovery(imp)) {
1398 struct l_wait_info lwi;
1399 cfs_duration_t timeout;
1403 if (imp->imp_server_timeout)
1404 timeout = cfs_time_seconds(obd_timeout / 2);
1406 timeout = cfs_time_seconds(obd_timeout);
1408 int idx = import_at_get_index(imp,
1409 imp->imp_client->cli_request_portal);
1410 timeout = cfs_time_seconds(
1411 at_get(&imp->imp_at.iat_service_estimate[idx]));
1414 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1415 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1416 rc = l_wait_event(imp->imp_recovery_waitq,
1417 !ptlrpc_import_in_recovery(imp), &lwi);
1421 cfs_spin_lock(&imp->imp_lock);
1422 if (imp->imp_state != LUSTRE_IMP_FULL)
1425 cfs_spin_unlock(&imp->imp_lock);
1427 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1428 LUSTRE_OBD_VERSION, rq_opc);
1430 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1431 * it fails. We can get through the above with a down server
1432 * if the client doesn't know the server is gone yet. */
1433 req->rq_no_resend = 1;
1435 /* We want client umounts to happen quickly, no matter the
1437 req->rq_timeout = min_t(int, req->rq_timeout,
1438 INITIAL_CONNECT_TIMEOUT);
1440 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1441 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1442 ptlrpc_request_set_replen(req);
1443 rc = ptlrpc_queue_wait(req);
1444 ptlrpc_req_finished(req);
1448 cfs_spin_lock(&imp->imp_lock);
1451 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1453 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1454 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1455 cfs_spin_unlock(&imp->imp_lock);
1460 void ptlrpc_cleanup_imp(struct obd_import *imp)
1464 cfs_spin_lock(&imp->imp_lock);
1465 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1466 imp->imp_generation++;
1467 cfs_spin_unlock(&imp->imp_lock);
1468 ptlrpc_abort_inflight(imp);
1473 /* Adaptive Timeout utils */
1474 extern unsigned int at_min, at_max, at_history;
1476 /* Bin into timeslices using AT_BINS bins.
1477 This gives us a max of the last binlimit*AT_BINS secs without the storage,
1478 but still smoothing out a return to normalcy from a slow response.
1479 (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1480 int at_measured(struct adaptive_timeout *at, unsigned int val)
1482 unsigned int old = at->at_current;
1483 time_t now = cfs_time_current_sec();
1484 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1487 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1488 val, at, now - at->at_binstart, at->at_current,
1489 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1492 /* 0's don't count, because we never want our timeout to
1493 drop to 0, and because 0 could mean an error */
1496 cfs_spin_lock(&at->at_lock);
1498 if (unlikely(at->at_binstart == 0)) {
1499 /* Special case to remove default from history */
1500 at->at_current = val;
1501 at->at_worst_ever = val;
1502 at->at_worst_time = now;
1503 at->at_hist[0] = val;
1504 at->at_binstart = now;
1505 } else if (now - at->at_binstart < binlimit ) {
1507 at->at_hist[0] = max(val, at->at_hist[0]);
1508 at->at_current = max(val, at->at_current);
1511 unsigned int maxv = val;
1512 /* move bins over */
1513 shift = (now - at->at_binstart) / binlimit;
1515 for(i = AT_BINS - 1; i >= 0; i--) {
1517 at->at_hist[i] = at->at_hist[i - shift];
1518 maxv = max(maxv, at->at_hist[i]);
1523 at->at_hist[0] = val;
1524 at->at_current = maxv;
1525 at->at_binstart += shift * binlimit;
1528 if (at->at_current > at->at_worst_ever) {
1529 at->at_worst_ever = at->at_current;
1530 at->at_worst_time = now;
1533 if (at->at_flags & AT_FLG_NOHIST)
1534 /* Only keep last reported val; keeping the rest of the history
1536 at->at_current = val;
1539 at->at_current = min(at->at_current, at_max);
1540 at->at_current = max(at->at_current, at_min);
1542 if (at->at_current != old)
1543 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1544 "(val=%u) hist %u %u %u %u\n", at,
1545 old, at->at_current, at->at_current - old, val,
1546 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1549 /* if we changed, report the old value */
1550 old = (at->at_current != old) ? old : 0;
1552 cfs_spin_unlock(&at->at_lock);
1556 /* Find the imp_at index for a given portal; assign if space available */
1557 int import_at_get_index(struct obd_import *imp, int portal)
1559 struct imp_at *at = &imp->imp_at;
1562 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1563 if (at->iat_portal[i] == portal)
1565 if (at->iat_portal[i] == 0)
1570 /* Not found in list, add it under a lock */
1571 cfs_spin_lock(&imp->imp_lock);
1573 /* Check unused under lock */
1574 for (; i < IMP_AT_MAX_PORTALS; i++) {
1575 if (at->iat_portal[i] == portal)
1577 if (at->iat_portal[i] == 0)
1582 /* Not enough portals? */
1583 LASSERT(i < IMP_AT_MAX_PORTALS);
1585 at->iat_portal[i] = portal;
1587 cfs_spin_unlock(&imp->imp_lock);