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 "../include/obd_support.h"
44 #include "../include/lustre_ha.h"
45 #include "../include/lustre_net.h"
46 #include "../include/lustre_import.h"
47 #include "../include/lustre_export.h"
48 #include "../include/obd.h"
49 #include "../include/obd_cksum.h"
50 #include "../include/obd_class.h"
52 #include "ptlrpc_internal.h"
54 struct ptlrpc_connect_async_args {
55 __u64 pcaa_peer_committed;
56 int pcaa_initial_connect;
60 * Updates import \a imp current state to provided \a state value
61 * Helper function. Must be called under imp_lock.
63 static void __import_set_state(struct obd_import *imp,
64 enum lustre_imp_state state)
67 case LUSTRE_IMP_CLOSED:
69 case LUSTRE_IMP_DISCON:
70 case LUSTRE_IMP_CONNECTING:
72 case LUSTRE_IMP_REPLAY_WAIT:
73 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
76 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
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 =
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,
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);
145 EXPORT_SYMBOL(deuuidify);
148 * Returns true if import was FULL, false if import was already not
150 * @imp - import to be disconnected
151 * @conn_cnt - connection count (epoch) of the request that timed out
152 * and caused the disconnection. In some cases, multiple
153 * inflight requests can fail to a single target (e.g. OST
154 * bulk requests) and if one has already caused a reconnection
155 * (increasing the import->conn_cnt) the older failure should
156 * not also cause a reconnection. If zero it forces a reconnect.
158 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
162 spin_lock(&imp->imp_lock);
164 if (imp->imp_state == LUSTRE_IMP_FULL &&
165 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
169 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
170 &target_start, &target_len);
172 if (imp->imp_replayable) {
173 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was lost; in progress operations using this service will wait for recovery to complete\n",
174 imp->imp_obd->obd_name, target_len, target_start,
175 libcfs_nid2str(imp->imp_connection->c_peer.nid));
177 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to %.*s (at %s) was lost; in progress operations using this service will fail\n",
178 imp->imp_obd->obd_name,
179 target_len, target_start,
180 libcfs_nid2str(imp->imp_connection->c_peer.nid));
182 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
183 spin_unlock(&imp->imp_lock);
185 if (obd_dump_on_timeout)
186 libcfs_debug_dumplog();
188 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
191 spin_unlock(&imp->imp_lock);
192 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
193 imp->imp_client->cli_name, imp,
194 (imp->imp_state == LUSTRE_IMP_FULL &&
195 imp->imp_conn_cnt > conn_cnt) ?
196 "reconnected" : "not connected", imp->imp_conn_cnt,
197 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
203 /* Must be called with imp_lock held! */
204 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
206 assert_spin_locked(&imp->imp_lock);
208 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
209 imp->imp_invalid = 1;
210 imp->imp_generation++;
211 spin_unlock(&imp->imp_lock);
213 ptlrpc_abort_inflight(imp);
214 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
218 * This acts as a barrier; all existing requests are rejected, and
219 * no new requests will be accepted until the import is valid again.
221 void ptlrpc_deactivate_import(struct obd_import *imp)
223 spin_lock(&imp->imp_lock);
224 ptlrpc_deactivate_and_unlock_import(imp);
226 EXPORT_SYMBOL(ptlrpc_deactivate_import);
229 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
233 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
234 (req->rq_phase == RQ_PHASE_BULK) ||
235 (req->rq_phase == RQ_PHASE_NEW)))
238 if (req->rq_timedout)
241 if (req->rq_phase == RQ_PHASE_NEW)
244 dl = req->rq_deadline;
252 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
254 time_t now = get_seconds();
255 struct list_head *tmp, *n;
256 struct ptlrpc_request *req;
257 unsigned int timeout = 0;
259 spin_lock(&imp->imp_lock);
260 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
261 req = list_entry(tmp, struct ptlrpc_request, rq_list);
262 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
264 spin_unlock(&imp->imp_lock);
269 * This function will invalidate the import, if necessary, then block
270 * for all the RPC completions, and finally notify the obd to
271 * invalidate its state (ie cancel locks, clear pending requests,
274 void ptlrpc_invalidate_import(struct obd_import *imp)
276 struct list_head *tmp, *n;
277 struct ptlrpc_request *req;
278 struct l_wait_info lwi;
279 unsigned int timeout;
282 atomic_inc(&imp->imp_inval_count);
284 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
285 ptlrpc_deactivate_import(imp);
287 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
288 LASSERT(imp->imp_invalid);
290 /* Wait forever until inflight == 0. We really can't do it another
291 * way because in some cases we need to wait for very long reply
292 * unlink. We can't do anything before that because there is really
293 * no guarantee that some rdma transfer is not in progress right now. */
295 /* Calculate max timeout for waiting on rpcs to error
296 * out. Use obd_timeout if calculated value is smaller
298 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
299 timeout = ptlrpc_inflight_timeout(imp);
300 timeout += timeout / 3;
303 timeout = obd_timeout;
305 /* decrease the interval to increase race condition */
310 "Sleeping %d sec for inflight to error out\n",
313 /* Wait for all requests to error out and call completion
314 * callbacks. Cap it at obd_timeout -- these should all
315 * have been locally cancelled by ptlrpc_abort_inflight. */
316 lwi = LWI_TIMEOUT_INTERVAL(
317 cfs_timeout_cap(cfs_time_seconds(timeout)),
318 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
320 rc = l_wait_event(imp->imp_recovery_waitq,
321 (atomic_read(&imp->imp_inflight) == 0),
324 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
326 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
328 atomic_read(&imp->imp_inflight));
330 spin_lock(&imp->imp_lock);
331 if (atomic_read(&imp->imp_inflight) == 0) {
332 int count = atomic_read(&imp->imp_unregistering);
334 /* We know that "unregistering" rpcs only can
335 * survive in sending or delaying lists (they
336 * maybe waiting for long reply unlink in
337 * sluggish nets). Let's check this. If there
338 * is no inflight and unregistering != 0, this
340 LASSERTF(count == 0, "Some RPCs are still unregistering: %d\n",
343 /* Let's save one loop as soon as inflight have
344 * dropped to zero. No new inflights possible at
348 list_for_each_safe(tmp, n,
349 &imp->imp_sending_list) {
350 req = list_entry(tmp,
351 struct ptlrpc_request,
353 DEBUG_REQ(D_ERROR, req,
354 "still on sending list");
356 list_for_each_safe(tmp, n,
357 &imp->imp_delayed_list) {
358 req = list_entry(tmp,
359 struct ptlrpc_request,
361 DEBUG_REQ(D_ERROR, req,
362 "still on delayed list");
365 CERROR("%s: RPCs in \"%s\" phase found (%d). Network is sluggish? Waiting them to error out.\n",
367 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
371 spin_unlock(&imp->imp_lock);
376 * Let's additionally check that no new rpcs added to import in
377 * "invalidate" state.
379 LASSERT(atomic_read(&imp->imp_inflight) == 0);
380 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
381 sptlrpc_import_flush_all_ctx(imp);
383 atomic_dec(&imp->imp_inval_count);
384 wake_up_all(&imp->imp_recovery_waitq);
386 EXPORT_SYMBOL(ptlrpc_invalidate_import);
388 /* unset imp_invalid */
389 void ptlrpc_activate_import(struct obd_import *imp)
391 struct obd_device *obd = imp->imp_obd;
393 spin_lock(&imp->imp_lock);
394 if (imp->imp_deactive != 0) {
395 spin_unlock(&imp->imp_lock);
399 imp->imp_invalid = 0;
400 spin_unlock(&imp->imp_lock);
401 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
403 EXPORT_SYMBOL(ptlrpc_activate_import);
405 static void ptlrpc_pinger_force(struct obd_import *imp)
407 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
408 ptlrpc_import_state_name(imp->imp_state));
410 spin_lock(&imp->imp_lock);
411 imp->imp_force_verify = 1;
412 spin_unlock(&imp->imp_lock);
414 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
415 ptlrpc_pinger_wake_up();
418 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
420 LASSERT(!imp->imp_dlm_fake);
422 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
423 if (!imp->imp_replayable) {
424 CDEBUG(D_HA, "import %s@%s for %s not replayable, auto-deactivating\n",
425 obd2cli_tgt(imp->imp_obd),
426 imp->imp_connection->c_remote_uuid.uuid,
427 imp->imp_obd->obd_name);
428 ptlrpc_deactivate_import(imp);
431 ptlrpc_pinger_force(imp);
434 EXPORT_SYMBOL(ptlrpc_fail_import);
436 int ptlrpc_reconnect_import(struct obd_import *imp)
439 struct l_wait_info lwi;
440 int secs = cfs_time_seconds(obd_timeout);
443 ptlrpc_pinger_force(imp);
445 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
446 obd2cli_tgt(imp->imp_obd), secs);
448 lwi = LWI_TIMEOUT(secs, NULL, NULL);
449 rc = l_wait_event(imp->imp_recovery_waitq,
450 !ptlrpc_import_in_recovery(imp), &lwi);
451 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
452 ptlrpc_import_state_name(imp->imp_state));
455 ptlrpc_set_import_discon(imp, 0);
456 /* Force a new connect attempt */
457 ptlrpc_invalidate_import(imp);
458 /* Do a fresh connect next time by zeroing the handle */
459 ptlrpc_disconnect_import(imp, 1);
460 /* Wait for all invalidate calls to finish */
461 if (atomic_read(&imp->imp_inval_count) > 0) {
463 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
464 rc = l_wait_event(imp->imp_recovery_waitq,
465 (atomic_read(&imp->imp_inval_count) == 0),
468 CERROR("Interrupted, inval=%d\n",
469 atomic_read(&imp->imp_inval_count));
472 /* Allow reconnect attempts */
473 imp->imp_obd->obd_no_recov = 0;
474 /* Remove 'invalid' flag */
475 ptlrpc_activate_import(imp);
476 /* Attempt a new connect */
477 ptlrpc_recover_import(imp, NULL, 0);
481 EXPORT_SYMBOL(ptlrpc_reconnect_import);
484 * Connection on import \a imp is changed to another one (if more than one is
485 * present). We typically chose connection that we have not tried to connect to
488 static int import_select_connection(struct obd_import *imp)
490 struct obd_import_conn *imp_conn = NULL, *conn;
491 struct obd_export *dlmexp;
493 int target_len, tried_all = 1;
495 spin_lock(&imp->imp_lock);
497 if (list_empty(&imp->imp_conn_list)) {
498 CERROR("%s: no connections available\n",
499 imp->imp_obd->obd_name);
500 spin_unlock(&imp->imp_lock);
504 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
505 CDEBUG(D_HA, "%s: connect to NID %s last attempt %llu\n",
506 imp->imp_obd->obd_name,
507 libcfs_nid2str(conn->oic_conn->c_peer.nid),
508 conn->oic_last_attempt);
510 /* If we have not tried this connection since
511 the last successful attempt, go with this one */
512 if ((conn->oic_last_attempt == 0) ||
513 cfs_time_beforeq_64(conn->oic_last_attempt,
514 imp->imp_last_success_conn)) {
520 /* If all of the connections have already been tried
521 since the last successful connection; just choose the
522 least recently used */
525 else if (cfs_time_before_64(conn->oic_last_attempt,
526 imp_conn->oic_last_attempt))
530 /* if not found, simply choose the current one */
531 if (!imp_conn || imp->imp_force_reconnect) {
532 LASSERT(imp->imp_conn_current);
533 imp_conn = imp->imp_conn_current;
536 LASSERT(imp_conn->oic_conn);
538 /* If we've tried everything, and we're back to the beginning of the
539 list, increase our timeout and try again. It will be reset when
540 we do finally connect. (FIXME: really we should wait for all network
541 state associated with the last connection attempt to drain before
542 trying to reconnect on it.) */
543 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
544 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
545 if (at_get(at) < CONNECTION_SWITCH_MAX) {
546 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
547 if (at_get(at) > CONNECTION_SWITCH_MAX)
548 at_reset(at, CONNECTION_SWITCH_MAX);
550 LASSERT(imp_conn->oic_last_attempt);
551 CDEBUG(D_HA, "%s: tried all connections, increasing latency to %ds\n",
552 imp->imp_obd->obd_name, at_get(at));
555 imp_conn->oic_last_attempt = cfs_time_current_64();
557 /* switch connection, don't mind if it's same as the current one */
558 if (imp->imp_connection)
559 ptlrpc_connection_put(imp->imp_connection);
560 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
562 dlmexp = class_conn2export(&imp->imp_dlm_handle);
563 LASSERT(dlmexp != NULL);
564 if (dlmexp->exp_connection)
565 ptlrpc_connection_put(dlmexp->exp_connection);
566 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
567 class_export_put(dlmexp);
569 if (imp->imp_conn_current != imp_conn) {
570 if (imp->imp_conn_current) {
571 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
572 &target_start, &target_len);
574 CDEBUG(D_HA, "%s: Connection changing to %.*s (at %s)\n",
575 imp->imp_obd->obd_name,
576 target_len, target_start,
577 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
580 imp->imp_conn_current = imp_conn;
583 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
584 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
585 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
587 spin_unlock(&imp->imp_lock);
593 * must be called under imp_lock
595 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
597 struct ptlrpc_request *req;
598 struct list_head *tmp;
600 /* The requests in committed_list always have smaller transnos than
601 * the requests in replay_list */
602 if (!list_empty(&imp->imp_committed_list)) {
603 tmp = imp->imp_committed_list.next;
604 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
605 *transno = req->rq_transno;
606 if (req->rq_transno == 0) {
607 DEBUG_REQ(D_ERROR, req,
608 "zero transno in committed_list");
613 if (!list_empty(&imp->imp_replay_list)) {
614 tmp = imp->imp_replay_list.next;
615 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
616 *transno = req->rq_transno;
617 if (req->rq_transno == 0) {
618 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
627 * Attempt to (re)connect import \a imp. This includes all preparations,
628 * initializing CONNECT RPC request and passing it to ptlrpcd for
630 * Returns 0 on success or error code.
632 int ptlrpc_connect_import(struct obd_import *imp)
634 struct obd_device *obd = imp->imp_obd;
635 int initial_connect = 0;
637 __u64 committed_before_reconnect = 0;
638 struct ptlrpc_request *request;
639 char *bufs[] = { NULL,
640 obd2cli_tgt(imp->imp_obd),
642 (char *)&imp->imp_dlm_handle,
643 (char *)&imp->imp_connect_data };
644 struct ptlrpc_connect_async_args *aa;
647 spin_lock(&imp->imp_lock);
648 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
649 spin_unlock(&imp->imp_lock);
650 CERROR("can't connect to a closed import\n");
652 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
653 spin_unlock(&imp->imp_lock);
654 CERROR("already connected\n");
656 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
657 spin_unlock(&imp->imp_lock);
658 CERROR("already connecting\n");
662 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
665 imp->imp_resend_replay = 0;
667 if (!lustre_handle_is_used(&imp->imp_remote_handle))
670 committed_before_reconnect = imp->imp_peer_committed_transno;
672 set_transno = ptlrpc_first_transno(imp,
673 &imp->imp_connect_data.ocd_transno);
674 spin_unlock(&imp->imp_lock);
676 rc = import_select_connection(imp);
680 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
684 /* Reset connect flags to the originally requested flags, in case
685 * the server is updated on-the-fly we will get the new features. */
686 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
687 /* Reset ocd_version each time so the server knows the exact versions */
688 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
689 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
690 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
692 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
693 &obd->obd_uuid, &imp->imp_connect_data, NULL);
697 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
698 if (request == NULL) {
703 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
704 imp->imp_connect_op, bufs, NULL);
706 ptlrpc_request_free(request);
710 /* Report the rpc service time to the server so that it knows how long
711 * to wait for clients to join recovery */
712 lustre_msg_set_service_time(request->rq_reqmsg,
713 at_timeout2est(request->rq_timeout));
715 /* The amount of time we give the server to process the connect req.
716 * import_select_connection will increase the net latency on
717 * repeated reconnect attempts to cover slow networks.
718 * We override/ignore the server rpc completion estimate here,
719 * which may be large if this is a reconnect attempt */
720 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
721 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
723 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
725 request->rq_no_resend = request->rq_no_delay = 1;
726 request->rq_send_state = LUSTRE_IMP_CONNECTING;
727 /* Allow a slightly larger reply for future growth compatibility */
728 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
729 sizeof(struct obd_connect_data)+16*sizeof(__u64));
730 ptlrpc_request_set_replen(request);
731 request->rq_interpret_reply = ptlrpc_connect_interpret;
733 CLASSERT(sizeof(*aa) <= sizeof(request->rq_async_args));
734 aa = ptlrpc_req_async_args(request);
735 memset(aa, 0, sizeof(*aa));
737 aa->pcaa_peer_committed = committed_before_reconnect;
738 aa->pcaa_initial_connect = initial_connect;
740 if (aa->pcaa_initial_connect) {
741 spin_lock(&imp->imp_lock);
742 imp->imp_replayable = 1;
743 spin_unlock(&imp->imp_lock);
744 lustre_msg_add_op_flags(request->rq_reqmsg,
745 MSG_CONNECT_INITIAL);
749 lustre_msg_add_op_flags(request->rq_reqmsg,
750 MSG_CONNECT_TRANSNO);
752 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
753 request->rq_timeout);
754 ptlrpcd_add_req(request, PDL_POLICY_ROUND, -1);
758 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
763 EXPORT_SYMBOL(ptlrpc_connect_import);
765 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
769 spin_lock(&imp->imp_lock);
770 force_verify = imp->imp_force_verify != 0;
771 spin_unlock(&imp->imp_lock);
774 ptlrpc_pinger_wake_up();
777 static int ptlrpc_busy_reconnect(int rc)
779 return (rc == -EBUSY) || (rc == -EAGAIN);
783 * interpret_reply callback for connect RPCs.
784 * Looks into returned status of connect operation and decides
785 * what to do with the import - i.e enter recovery, promote it to
786 * full state for normal operations of disconnect it due to an error.
788 static int ptlrpc_connect_interpret(const struct lu_env *env,
789 struct ptlrpc_request *request,
792 struct ptlrpc_connect_async_args *aa = data;
793 struct obd_import *imp = request->rq_import;
794 struct client_obd *cli = &imp->imp_obd->u.cli;
795 struct lustre_handle old_hdl;
796 __u64 old_connect_flags;
798 struct obd_connect_data *ocd;
799 struct obd_export *exp;
802 spin_lock(&imp->imp_lock);
803 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
804 imp->imp_connect_tried = 1;
805 spin_unlock(&imp->imp_lock);
810 /* if this reconnect to busy export - not need select new target
812 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
813 spin_unlock(&imp->imp_lock);
814 ptlrpc_maybe_ping_import_soon(imp);
817 spin_unlock(&imp->imp_lock);
819 LASSERT(imp->imp_conn_current);
821 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
823 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
825 /* server replied obd_connect_data is always bigger */
826 ocd = req_capsule_server_sized_get(&request->rq_pill,
827 &RMF_CONNECT_DATA, ret);
830 CERROR("%s: no connect data from server\n",
831 imp->imp_obd->obd_name);
836 spin_lock(&imp->imp_lock);
838 /* All imports are pingable */
839 imp->imp_pingable = 1;
840 imp->imp_force_reconnect = 0;
841 imp->imp_force_verify = 0;
843 imp->imp_connect_data = *ocd;
845 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
846 imp->imp_obd->obd_name, ocd->ocd_instance);
847 exp = class_conn2export(&imp->imp_dlm_handle);
849 spin_unlock(&imp->imp_lock);
851 /* check that server granted subset of flags we asked for. */
852 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
853 ocd->ocd_connect_flags) {
854 CERROR("%s: Server didn't granted asked subset of flags: asked=%#llx grranted=%#llx\n",
855 imp->imp_obd->obd_name, imp->imp_connect_flags_orig,
856 ocd->ocd_connect_flags);
862 /* This could happen if export is cleaned during the
864 CERROR("%s: missing export after connect\n",
865 imp->imp_obd->obd_name);
869 old_connect_flags = exp_connect_flags(exp);
870 exp->exp_connect_data = *ocd;
871 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
872 class_export_put(exp);
874 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
876 if (aa->pcaa_initial_connect) {
877 spin_lock(&imp->imp_lock);
878 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
879 imp->imp_replayable = 1;
880 spin_unlock(&imp->imp_lock);
881 CDEBUG(D_HA, "connected to replayable target: %s\n",
882 obd2cli_tgt(imp->imp_obd));
884 imp->imp_replayable = 0;
885 spin_unlock(&imp->imp_lock);
888 /* if applies, adjust the imp->imp_msg_magic here
889 * according to reply flags */
891 imp->imp_remote_handle =
892 *lustre_msg_get_handle(request->rq_repmsg);
894 /* Initial connects are allowed for clients with non-random
895 * uuids when servers are in recovery. Simply signal the
896 * servers replay is complete and wait in REPLAY_WAIT. */
897 if (msg_flags & MSG_CONNECT_RECOVERING) {
898 CDEBUG(D_HA, "connect to %s during recovery\n",
899 obd2cli_tgt(imp->imp_obd));
900 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
902 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
903 ptlrpc_activate_import(imp);
910 /* Determine what recovery state to move the import to. */
911 if (MSG_CONNECT_RECONNECT & msg_flags) {
912 memset(&old_hdl, 0, sizeof(old_hdl));
913 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
915 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due bad handle %#llx\n",
916 obd2cli_tgt(imp->imp_obd),
917 imp->imp_connection->c_remote_uuid.uuid,
918 imp->imp_dlm_handle.cookie);
923 if (memcmp(&imp->imp_remote_handle,
924 lustre_msg_get_handle(request->rq_repmsg),
925 sizeof(imp->imp_remote_handle))) {
926 int level = msg_flags & MSG_CONNECT_RECOVERING ?
929 /* Bug 16611/14775: if server handle have changed,
930 * that means some sort of disconnection happened.
931 * If the server is not in recovery, that also means it
932 * already erased all of our state because of previous
933 * eviction. If it is in recovery - we are safe to
934 * participate since we can reestablish all of our state
935 * with server again */
936 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
937 CDEBUG(level, "%s@%s changed server handle from %#llx to %#llx but is still in recovery\n",
938 obd2cli_tgt(imp->imp_obd),
939 imp->imp_connection->c_remote_uuid.uuid,
940 imp->imp_remote_handle.cookie,
941 lustre_msg_get_handle(
942 request->rq_repmsg)->cookie);
944 LCONSOLE_WARN("Evicted from %s (at %s) after server handle changed from %#llx to %#llx\n",
945 obd2cli_tgt(imp->imp_obd),
946 imp->imp_connection-> \
948 imp->imp_remote_handle.cookie,
949 lustre_msg_get_handle(
950 request->rq_repmsg)->cookie);
954 imp->imp_remote_handle =
955 *lustre_msg_get_handle(request->rq_repmsg);
957 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
958 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
964 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
965 obd2cli_tgt(imp->imp_obd),
966 imp->imp_connection->c_remote_uuid.uuid);
969 if (imp->imp_invalid) {
970 CDEBUG(D_HA, "%s: reconnected but import is invalid; marking evicted\n",
971 imp->imp_obd->obd_name);
972 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
973 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
974 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
975 imp->imp_obd->obd_name,
976 obd2cli_tgt(imp->imp_obd));
978 spin_lock(&imp->imp_lock);
979 imp->imp_resend_replay = 1;
980 spin_unlock(&imp->imp_lock);
982 IMPORT_SET_STATE(imp, imp->imp_replay_state);
984 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
986 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
987 LASSERT(imp->imp_replayable);
988 imp->imp_remote_handle =
989 *lustre_msg_get_handle(request->rq_repmsg);
990 imp->imp_last_replay_transno = 0;
991 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
993 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags not set: %x)",
994 imp->imp_obd->obd_name, msg_flags);
995 imp->imp_remote_handle =
996 *lustre_msg_get_handle(request->rq_repmsg);
997 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1000 /* Sanity checks for a reconnected import. */
1001 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1002 CERROR("imp_replayable flag does not match server after reconnect. We should LBUG right here.\n");
1005 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1006 lustre_msg_get_last_committed(request->rq_repmsg) <
1007 aa->pcaa_peer_committed) {
1008 CERROR("%s went back in time (transno %lld was previously committed, server now claims %lld)! See https://bugzilla.lustre.org/show_bug.cgi?id=9646\n",
1009 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1010 lustre_msg_get_last_committed(request->rq_repmsg));
1014 rc = ptlrpc_import_recovery_state_machine(imp);
1016 if (rc == -ENOTCONN) {
1017 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery; invalidating and reconnecting\n",
1018 obd2cli_tgt(imp->imp_obd),
1019 imp->imp_connection->c_remote_uuid.uuid);
1020 ptlrpc_connect_import(imp);
1021 imp->imp_connect_tried = 1;
1026 spin_lock(&imp->imp_lock);
1027 list_del(&imp->imp_conn_current->oic_item);
1028 list_add(&imp->imp_conn_current->oic_item,
1029 &imp->imp_conn_list);
1030 imp->imp_last_success_conn =
1031 imp->imp_conn_current->oic_last_attempt;
1033 spin_unlock(&imp->imp_lock);
1035 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
1036 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
1037 LCONSOLE_WARN("%s: MDS %s does not support ibits lock, either very old or invalid: requested %llx, replied %llx\n",
1038 imp->imp_obd->obd_name,
1039 imp->imp_connection->c_remote_uuid.uuid,
1040 imp->imp_connect_flags_orig,
1041 ocd->ocd_connect_flags);
1046 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1047 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1048 LUSTRE_VERSION_OFFSET_WARN ||
1049 ocd->ocd_version < LUSTRE_VERSION_CODE -
1050 LUSTRE_VERSION_OFFSET_WARN)) {
1051 /* Sigh, some compilers do not like #ifdef in the middle
1052 of macro arguments */
1053 const char *older = "older. Consider upgrading server or downgrading client"
1055 const char *newer = "newer than client version. Consider upgrading client"
1058 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) is much %s (%s)\n",
1059 obd2cli_tgt(imp->imp_obd),
1060 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1061 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1062 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1063 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1064 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1065 newer : older, LUSTRE_VERSION_STRING);
1068 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 2, 50, 0)
1069 /* Check if server has LU-1252 fix applied to not always swab
1070 * the IR MNE entries. Do this only once per connection. This
1071 * fixup is version-limited, because we don't want to carry the
1072 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
1073 * need interop with unpatched 2.2 servers. For newer servers,
1074 * the client will do MNE swabbing only as needed. LU-1644 */
1075 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1076 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
1077 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
1078 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
1079 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
1080 strcmp(imp->imp_obd->obd_type->typ_name,
1081 LUSTRE_MGC_NAME) == 0))
1082 imp->imp_need_mne_swab = 1;
1083 else /* clear if server was upgraded since last connect */
1084 imp->imp_need_mne_swab = 0;
1086 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and imp_need_mne_swab"
1089 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1090 /* We sent to the server ocd_cksum_types with bits set
1091 * for algorithms we understand. The server masked off
1092 * the checksum types it doesn't support */
1093 if ((ocd->ocd_cksum_types &
1094 cksum_types_supported_client()) == 0) {
1095 LCONSOLE_WARN("The negotiation of the checksum algorithm to use with server %s failed (%x/%x), disabling checksums\n",
1096 obd2cli_tgt(imp->imp_obd),
1097 ocd->ocd_cksum_types,
1098 cksum_types_supported_client());
1099 cli->cl_checksum = 0;
1100 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1102 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1105 /* The server does not support OBD_CONNECT_CKSUM.
1106 * Enforce ADLER for backward compatibility*/
1107 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
1109 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
1111 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
1112 cli->cl_max_pages_per_rpc =
1113 min(ocd->ocd_brw_size >> PAGE_CACHE_SHIFT,
1114 cli->cl_max_pages_per_rpc);
1115 else if (imp->imp_connect_op == MDS_CONNECT ||
1116 imp->imp_connect_op == MGS_CONNECT)
1117 cli->cl_max_pages_per_rpc = 1;
1119 /* Reset ns_connect_flags only for initial connect. It might be
1120 * changed in while using FS and if we reset it in reconnect
1121 * this leads to losing user settings done before such as
1122 * disable lru_resize, etc. */
1123 if (old_connect_flags != exp_connect_flags(exp) ||
1124 aa->pcaa_initial_connect) {
1125 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server flags: %#llx\n",
1126 imp->imp_obd->obd_name, ocd->ocd_connect_flags);
1127 imp->imp_obd->obd_namespace->ns_connect_flags =
1128 ocd->ocd_connect_flags;
1129 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1130 ocd->ocd_connect_flags;
1133 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1134 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1135 /* We need a per-message support flag, because
1136 a. we don't know if the incoming connect reply
1137 supports AT or not (in reply_in_callback)
1139 b. failovered server means export and flags are gone
1140 (in ptlrpc_send_reply).
1141 Can only be set when we know AT is supported at
1143 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1145 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1147 if ((ocd->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1148 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1149 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1151 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1153 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1154 (cli->cl_max_pages_per_rpc > 0));
1158 imp->imp_connect_tried = 1;
1161 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1162 if (rc == -EACCES) {
1164 * Give up trying to reconnect
1165 * EACCES means client has no permission for connection
1167 imp->imp_obd->obd_no_recov = 1;
1168 ptlrpc_deactivate_import(imp);
1171 if (rc == -EPROTO) {
1172 struct obd_connect_data *ocd;
1174 /* reply message might not be ready */
1175 if (request->rq_repmsg == NULL)
1178 ocd = req_capsule_server_get(&request->rq_pill,
1181 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1182 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1184 * Actually servers are only supposed to refuse
1185 * connection from liblustre clients, so we
1186 * should never see this from VFS context
1188 LCONSOLE_ERROR_MSG(0x16a, "Server %s version (%d.%d.%d.%d) refused connection from this client with an incompatible version (%s). Client must be recompiled\n",
1189 obd2cli_tgt(imp->imp_obd),
1190 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1191 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1192 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1193 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1194 LUSTRE_VERSION_STRING);
1195 ptlrpc_deactivate_import(imp);
1196 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1201 ptlrpc_maybe_ping_import_soon(imp);
1203 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1204 obd2cli_tgt(imp->imp_obd),
1205 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1208 wake_up_all(&imp->imp_recovery_waitq);
1213 * interpret callback for "completed replay" RPCs.
1214 * \see signal_completed_replay
1216 static int completed_replay_interpret(const struct lu_env *env,
1217 struct ptlrpc_request *req,
1220 atomic_dec(&req->rq_import->imp_replay_inflight);
1221 if (req->rq_status == 0 &&
1222 !req->rq_import->imp_vbr_failed) {
1223 ptlrpc_import_recovery_state_machine(req->rq_import);
1225 if (req->rq_import->imp_vbr_failed) {
1227 "%s: version recovery fails, reconnecting\n",
1228 req->rq_import->imp_obd->obd_name);
1230 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, reconnecting\n",
1231 req->rq_import->imp_obd->obd_name,
1234 ptlrpc_connect_import(req->rq_import);
1241 * Let server know that we have no requests to replay anymore.
1242 * Achieved by just sending a PING request
1244 static int signal_completed_replay(struct obd_import *imp)
1246 struct ptlrpc_request *req;
1248 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1251 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1252 atomic_inc(&imp->imp_replay_inflight);
1254 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1257 atomic_dec(&imp->imp_replay_inflight);
1261 ptlrpc_request_set_replen(req);
1262 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1263 lustre_msg_add_flags(req->rq_reqmsg,
1264 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1266 req->rq_timeout *= 3;
1267 req->rq_interpret_reply = completed_replay_interpret;
1269 ptlrpcd_add_req(req, PDL_POLICY_ROUND, -1);
1274 * In kernel code all import invalidation happens in its own
1275 * separate thread, so that whatever application happened to encounter
1276 * a problem could still be killed or otherwise continue
1278 static int ptlrpc_invalidate_import_thread(void *data)
1280 struct obd_import *imp = data;
1282 unshare_fs_struct();
1284 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1285 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1286 imp->imp_connection->c_remote_uuid.uuid);
1288 ptlrpc_invalidate_import(imp);
1290 if (obd_dump_on_eviction) {
1291 CERROR("dump the log upon eviction\n");
1292 libcfs_debug_dumplog();
1295 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1296 ptlrpc_import_recovery_state_machine(imp);
1298 class_import_put(imp);
1303 * This is the state machine for client-side recovery on import.
1305 * Typically we have two possibly paths. If we came to server and it is not
1306 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1307 * state and reconnect from scratch.
1308 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1309 * We go through our list of requests to replay and send them to server one by
1311 * After sending all request from the list we change import state to
1312 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1313 * and also all the locks we don't yet have and wait for server to grant us.
1314 * After that we send a special "replay completed" request and change import
1315 * state to IMP_REPLAY_WAIT.
1316 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1317 * state and resend all requests from sending list.
1318 * After that we promote import to FULL state and send all delayed requests
1319 * and import is fully operational after that.
1322 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1329 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1330 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1331 &target_start, &target_len);
1332 /* Don't care about MGC eviction */
1333 if (strcmp(imp->imp_obd->obd_type->typ_name,
1334 LUSTRE_MGC_NAME) != 0) {
1335 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted by %.*s; in progress operations using this service will fail.\n",
1336 imp->imp_obd->obd_name, target_len,
1339 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1340 obd2cli_tgt(imp->imp_obd),
1341 imp->imp_connection->c_remote_uuid.uuid);
1342 /* reset vbr_failed flag upon eviction */
1343 spin_lock(&imp->imp_lock);
1344 imp->imp_vbr_failed = 0;
1345 spin_unlock(&imp->imp_lock);
1348 struct task_struct *task;
1349 /* bug 17802: XXX client_disconnect_export vs connect request
1350 * race. if client will evicted at this time, we start
1351 * invalidate thread without reference to import and import can
1352 * be freed at same time. */
1353 class_import_get(imp);
1354 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1357 class_import_put(imp);
1358 CERROR("error starting invalidate thread: %d\n", rc);
1367 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1368 CDEBUG(D_HA, "replay requested by %s\n",
1369 obd2cli_tgt(imp->imp_obd));
1370 rc = ptlrpc_replay_next(imp, &inflight);
1371 if (inflight == 0 &&
1372 atomic_read(&imp->imp_replay_inflight) == 0) {
1373 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1374 rc = ldlm_replay_locks(imp);
1381 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1382 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1383 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1384 rc = signal_completed_replay(imp);
1391 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1392 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1393 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1397 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1398 CDEBUG(D_HA, "reconnected to %s@%s\n",
1399 obd2cli_tgt(imp->imp_obd),
1400 imp->imp_connection->c_remote_uuid.uuid);
1402 rc = ptlrpc_resend(imp);
1405 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1406 ptlrpc_activate_import(imp);
1408 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1409 &target_start, &target_len);
1410 LCONSOLE_INFO("%s: Connection restored to %.*s (at %s)\n",
1411 imp->imp_obd->obd_name,
1412 target_len, target_start,
1413 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1416 if (imp->imp_state == LUSTRE_IMP_FULL) {
1417 wake_up_all(&imp->imp_recovery_waitq);
1418 ptlrpc_wake_delayed(imp);
1425 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1427 struct ptlrpc_request *req;
1430 if (imp->imp_obd->obd_force)
1433 switch (imp->imp_connect_op) {
1435 rq_opc = OST_DISCONNECT;
1438 rq_opc = MDS_DISCONNECT;
1441 rq_opc = MGS_DISCONNECT;
1445 CERROR("%s: don't know how to disconnect from %s (connect_op %d): rc = %d\n",
1446 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1447 imp->imp_connect_op, rc);
1451 if (ptlrpc_import_in_recovery(imp)) {
1452 struct l_wait_info lwi;
1456 if (imp->imp_server_timeout)
1457 timeout = cfs_time_seconds(obd_timeout / 2);
1459 timeout = cfs_time_seconds(obd_timeout);
1461 int idx = import_at_get_index(imp,
1462 imp->imp_client->cli_request_portal);
1463 timeout = cfs_time_seconds(
1464 at_get(&imp->imp_at.iat_service_estimate[idx]));
1467 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1468 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1469 rc = l_wait_event(imp->imp_recovery_waitq,
1470 !ptlrpc_import_in_recovery(imp), &lwi);
1474 spin_lock(&imp->imp_lock);
1475 if (imp->imp_state != LUSTRE_IMP_FULL)
1477 spin_unlock(&imp->imp_lock);
1479 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1480 LUSTRE_OBD_VERSION, rq_opc);
1482 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1483 * it fails. We can get through the above with a down server
1484 * if the client doesn't know the server is gone yet. */
1485 req->rq_no_resend = 1;
1487 /* We want client umounts to happen quickly, no matter the
1489 req->rq_timeout = min_t(int, req->rq_timeout,
1490 INITIAL_CONNECT_TIMEOUT);
1492 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1493 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1494 ptlrpc_request_set_replen(req);
1495 rc = ptlrpc_queue_wait(req);
1496 ptlrpc_req_finished(req);
1500 spin_lock(&imp->imp_lock);
1503 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1505 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1506 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1507 spin_unlock(&imp->imp_lock);
1509 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1514 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1516 void ptlrpc_cleanup_imp(struct obd_import *imp)
1518 spin_lock(&imp->imp_lock);
1519 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1520 imp->imp_generation++;
1521 spin_unlock(&imp->imp_lock);
1522 ptlrpc_abort_inflight(imp);
1524 EXPORT_SYMBOL(ptlrpc_cleanup_imp);
1526 /* Adaptive Timeout utils */
1527 extern unsigned int at_min, at_max, at_history;
1529 /* Bin into timeslices using AT_BINS bins.
1530 This gives us a max of the last binlimit*AT_BINS secs without the storage,
1531 but still smoothing out a return to normalcy from a slow response.
1532 (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1533 int at_measured(struct adaptive_timeout *at, unsigned int val)
1535 unsigned int old = at->at_current;
1536 time_t now = get_seconds();
1537 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1540 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1541 val, at, now - at->at_binstart, at->at_current,
1542 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1545 /* 0's don't count, because we never want our timeout to
1546 drop to 0, and because 0 could mean an error */
1549 spin_lock(&at->at_lock);
1551 if (unlikely(at->at_binstart == 0)) {
1552 /* Special case to remove default from history */
1553 at->at_current = val;
1554 at->at_worst_ever = val;
1555 at->at_worst_time = now;
1556 at->at_hist[0] = val;
1557 at->at_binstart = now;
1558 } else if (now - at->at_binstart < binlimit) {
1560 at->at_hist[0] = max(val, at->at_hist[0]);
1561 at->at_current = max(val, at->at_current);
1564 unsigned int maxv = val;
1565 /* move bins over */
1566 shift = (now - at->at_binstart) / binlimit;
1568 for (i = AT_BINS - 1; i >= 0; i--) {
1570 at->at_hist[i] = at->at_hist[i - shift];
1571 maxv = max(maxv, at->at_hist[i]);
1576 at->at_hist[0] = val;
1577 at->at_current = maxv;
1578 at->at_binstart += shift * binlimit;
1581 if (at->at_current > at->at_worst_ever) {
1582 at->at_worst_ever = at->at_current;
1583 at->at_worst_time = now;
1586 if (at->at_flags & AT_FLG_NOHIST)
1587 /* Only keep last reported val; keeping the rest of the history
1589 at->at_current = val;
1592 at->at_current = min(at->at_current, at_max);
1593 at->at_current = max(at->at_current, at_min);
1595 if (at->at_current != old)
1596 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d (val=%u) hist %u %u %u %u\n",
1598 old, at->at_current, at->at_current - old, val,
1599 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1602 /* if we changed, report the old value */
1603 old = (at->at_current != old) ? old : 0;
1605 spin_unlock(&at->at_lock);
1609 /* Find the imp_at index for a given portal; assign if space available */
1610 int import_at_get_index(struct obd_import *imp, int portal)
1612 struct imp_at *at = &imp->imp_at;
1615 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1616 if (at->iat_portal[i] == portal)
1618 if (at->iat_portal[i] == 0)
1623 /* Not found in list, add it under a lock */
1624 spin_lock(&imp->imp_lock);
1626 /* Check unused under lock */
1627 for (; i < IMP_AT_MAX_PORTALS; i++) {
1628 if (at->iat_portal[i] == portal)
1630 if (at->iat_portal[i] == 0)
1635 /* Not enough portals? */
1636 LASSERT(i < IMP_AT_MAX_PORTALS);
1638 at->iat_portal[i] = portal;
1640 spin_unlock(&imp->imp_lock);