4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/import.c
38 * Author: Mike Shaver <shaver@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_RPC
43 #include <linux/kthread.h>
44 #include <obd_support.h>
45 #include <lustre_ha.h>
46 #include <lustre_net.h>
47 #include <lustre_import.h>
48 #include <lustre_export.h>
50 #include <obd_cksum.h>
51 #include <obd_class.h>
53 #include "ptlrpc_internal.h"
55 struct ptlrpc_connect_async_args {
56 __u64 pcaa_peer_committed;
57 int pcaa_initial_connect;
61 * Updates import \a imp current state to provided \a state value
62 * Helper function. Must be called under imp_lock.
64 static void __import_set_state(struct obd_import *imp,
65 enum lustre_imp_state state)
68 case LUSTRE_IMP_CLOSED:
70 case LUSTRE_IMP_DISCON:
71 case LUSTRE_IMP_CONNECTING:
73 case LUSTRE_IMP_REPLAY_WAIT:
74 imp->imp_replay_state = LUSTRE_IMP_REPLAY_LOCKS;
77 imp->imp_replay_state = LUSTRE_IMP_REPLAY;
79 imp->imp_state = state;
80 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
81 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
82 cfs_time_current_sec();
83 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
87 /* A CLOSED import should remain so. */
88 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
90 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
91 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
92 imp, obd2cli_tgt(imp->imp_obd), \
93 ptlrpc_import_state_name(imp->imp_state), \
94 ptlrpc_import_state_name(state)); \
95 __import_set_state(imp, state); \
99 #define IMPORT_SET_STATE(imp, state) \
101 spin_lock(&imp->imp_lock); \
102 IMPORT_SET_STATE_NOLOCK(imp, state); \
103 spin_unlock(&imp->imp_lock); \
106 void ptlrpc_import_enter_resend(struct obd_import *imp)
108 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
110 EXPORT_SYMBOL(ptlrpc_import_enter_resend);
113 static int ptlrpc_connect_interpret(const struct lu_env *env,
114 struct ptlrpc_request *request,
115 void * data, int rc);
116 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
118 /* Only this function is allowed to change the import state when it is
119 * CLOSED. I would rather refcount the import and free it after
120 * disconnection like we do with exports. To do that, the client_obd
121 * will need to save the peer info somewhere other than in the import,
123 int ptlrpc_init_import(struct obd_import *imp)
125 spin_lock(&imp->imp_lock);
127 imp->imp_generation++;
128 imp->imp_state = LUSTRE_IMP_NEW;
130 spin_unlock(&imp->imp_lock);
134 EXPORT_SYMBOL(ptlrpc_init_import);
136 #define UUID_STR "_UUID"
137 void deuuidify(char *uuid, const char *prefix, char **uuid_start, int *uuid_len)
139 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
140 ? uuid : uuid + strlen(prefix);
142 *uuid_len = strlen(*uuid_start);
144 if (*uuid_len < strlen(UUID_STR))
147 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
148 UUID_STR, strlen(UUID_STR)))
149 *uuid_len -= strlen(UUID_STR);
153 * Returns true if import was FULL, false if import was already not
155 * @imp - import to be disconnected
156 * @conn_cnt - connection count (epoch) of the request that timed out
157 * and caused the disconnection. In some cases, multiple
158 * inflight requests can fail to a single target (e.g. OST
159 * bulk requests) and if one has already caused a reconnection
160 * (increasing the import->conn_cnt) the older failure should
161 * not also cause a reconnection. If zero it forces a reconnect.
163 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
167 spin_lock(&imp->imp_lock);
169 if (imp->imp_state == LUSTRE_IMP_FULL &&
170 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
174 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
175 &target_start, &target_len);
177 if (imp->imp_replayable) {
178 LCONSOLE_WARN("%s: Connection to %.*s (at %s) was "
179 "lost; in progress operations using this "
180 "service will wait for recovery to complete\n",
181 imp->imp_obd->obd_name, target_len, target_start,
182 libcfs_nid2str(imp->imp_connection->c_peer.nid));
184 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to "
185 "%.*s (at %s) was lost; in progress "
186 "operations using this service will fail\n",
187 imp->imp_obd->obd_name,
188 target_len, target_start,
189 libcfs_nid2str(imp->imp_connection->c_peer.nid));
191 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
192 spin_unlock(&imp->imp_lock);
194 if (obd_dump_on_timeout)
195 libcfs_debug_dumplog();
197 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
200 spin_unlock(&imp->imp_lock);
201 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
202 imp->imp_client->cli_name, imp,
203 (imp->imp_state == LUSTRE_IMP_FULL &&
204 imp->imp_conn_cnt > conn_cnt) ?
205 "reconnected" : "not connected", imp->imp_conn_cnt,
206 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
212 /* Must be called with imp_lock held! */
213 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
216 assert_spin_locked(&imp->imp_lock);
218 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
219 imp->imp_invalid = 1;
220 imp->imp_generation++;
221 spin_unlock(&imp->imp_lock);
223 ptlrpc_abort_inflight(imp);
224 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
230 * This acts as a barrier; all existing requests are rejected, and
231 * no new requests will be accepted until the import is valid again.
233 void ptlrpc_deactivate_import(struct obd_import *imp)
235 spin_lock(&imp->imp_lock);
236 ptlrpc_deactivate_and_unlock_import(imp);
238 EXPORT_SYMBOL(ptlrpc_deactivate_import);
241 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
245 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
246 (req->rq_phase == RQ_PHASE_BULK) ||
247 (req->rq_phase == RQ_PHASE_NEW)))
250 if (req->rq_timedout)
253 if (req->rq_phase == RQ_PHASE_NEW)
256 dl = req->rq_deadline;
264 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
266 time_t now = cfs_time_current_sec();
267 struct list_head *tmp, *n;
268 struct ptlrpc_request *req;
269 unsigned int timeout = 0;
271 spin_lock(&imp->imp_lock);
272 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
273 req = list_entry(tmp, struct ptlrpc_request, rq_list);
274 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
276 spin_unlock(&imp->imp_lock);
281 * This function will invalidate the import, if necessary, then block
282 * for all the RPC completions, and finally notify the obd to
283 * invalidate its state (ie cancel locks, clear pending requests,
286 void ptlrpc_invalidate_import(struct obd_import *imp)
288 struct list_head *tmp, *n;
289 struct ptlrpc_request *req;
290 struct l_wait_info lwi;
291 unsigned int timeout;
294 atomic_inc(&imp->imp_inval_count);
296 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
297 ptlrpc_deactivate_import(imp);
299 CFS_FAIL_TIMEOUT(OBD_FAIL_MGS_CONNECT_NET, 3 * cfs_fail_val / 2);
300 LASSERT(imp->imp_invalid);
302 /* Wait forever until inflight == 0. We really can't do it another
303 * way because in some cases we need to wait for very long reply
304 * unlink. We can't do anything before that because there is really
305 * no guarantee that some rdma transfer is not in progress right now. */
307 /* Calculate max timeout for waiting on rpcs to error
308 * out. Use obd_timeout if calculated value is smaller
310 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
311 timeout = ptlrpc_inflight_timeout(imp);
312 timeout += timeout / 3;
315 timeout = obd_timeout;
317 /* decrease the interval to increase race condition */
321 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
324 /* Wait for all requests to error out and call completion
325 * callbacks. Cap it at obd_timeout -- these should all
326 * have been locally cancelled by ptlrpc_abort_inflight. */
327 lwi = LWI_TIMEOUT_INTERVAL(
328 cfs_timeout_cap(cfs_time_seconds(timeout)),
329 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
331 rc = l_wait_event(imp->imp_recovery_waitq,
332 (atomic_read(&imp->imp_inflight) == 0),
335 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
337 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
338 cli_tgt, rc, atomic_read(&imp->imp_inflight));
340 spin_lock(&imp->imp_lock);
341 if (atomic_read(&imp->imp_inflight) == 0) {
342 int count = atomic_read(&imp->imp_unregistering);
344 /* We know that "unregistering" rpcs only can
345 * survive in sending or delaying lists (they
346 * maybe waiting for long reply unlink in
347 * sluggish nets). Let's check this. If there
348 * is no inflight and unregistering != 0, this
350 LASSERTF(count == 0, "Some RPCs are still "
351 "unregistering: %d\n", count);
353 /* Let's save one loop as soon as inflight have
354 * dropped to zero. No new inflights possible at
358 list_for_each_safe(tmp, n,
359 &imp->imp_sending_list) {
360 req = list_entry(tmp,
361 struct ptlrpc_request,
363 DEBUG_REQ(D_ERROR, req,
364 "still on sending list");
366 list_for_each_safe(tmp, n,
367 &imp->imp_delayed_list) {
368 req = list_entry(tmp,
369 struct ptlrpc_request,
371 DEBUG_REQ(D_ERROR, req,
372 "still on delayed list");
375 CERROR("%s: RPCs in \"%s\" phase found (%d). "
376 "Network is sluggish? Waiting them "
377 "to error out.\n", cli_tgt,
378 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
379 atomic_read(&imp->imp_unregistering));
381 spin_unlock(&imp->imp_lock);
386 * Let's additionally check that no new rpcs added to import in
387 * "invalidate" state.
389 LASSERT(atomic_read(&imp->imp_inflight) == 0);
390 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
391 sptlrpc_import_flush_all_ctx(imp);
393 atomic_dec(&imp->imp_inval_count);
394 wake_up_all(&imp->imp_recovery_waitq);
396 EXPORT_SYMBOL(ptlrpc_invalidate_import);
398 /* unset imp_invalid */
399 void ptlrpc_activate_import(struct obd_import *imp)
401 struct obd_device *obd = imp->imp_obd;
403 spin_lock(&imp->imp_lock);
404 if (imp->imp_deactive != 0) {
405 spin_unlock(&imp->imp_lock);
409 imp->imp_invalid = 0;
410 spin_unlock(&imp->imp_lock);
411 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
413 EXPORT_SYMBOL(ptlrpc_activate_import);
415 void ptlrpc_pinger_force(struct obd_import *imp)
417 CDEBUG(D_HA, "%s: waking up pinger s:%s\n", obd2cli_tgt(imp->imp_obd),
418 ptlrpc_import_state_name(imp->imp_state));
420 spin_lock(&imp->imp_lock);
421 imp->imp_force_verify = 1;
422 spin_unlock(&imp->imp_lock);
424 if (imp->imp_state != LUSTRE_IMP_CONNECTING)
425 ptlrpc_pinger_wake_up();
427 EXPORT_SYMBOL(ptlrpc_pinger_force);
429 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
433 LASSERT(!imp->imp_dlm_fake);
435 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
436 if (!imp->imp_replayable) {
437 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
438 "auto-deactivating\n",
439 obd2cli_tgt(imp->imp_obd),
440 imp->imp_connection->c_remote_uuid.uuid,
441 imp->imp_obd->obd_name);
442 ptlrpc_deactivate_import(imp);
445 ptlrpc_pinger_force(imp);
450 int ptlrpc_reconnect_import(struct obd_import *imp)
453 struct l_wait_info lwi;
454 int secs = cfs_time_seconds(obd_timeout);
457 ptlrpc_pinger_force(imp);
459 CDEBUG(D_HA, "%s: recovery started, waiting %u seconds\n",
460 obd2cli_tgt(imp->imp_obd), secs);
462 lwi = LWI_TIMEOUT(secs, NULL, NULL);
463 rc = l_wait_event(imp->imp_recovery_waitq,
464 !ptlrpc_import_in_recovery(imp), &lwi);
465 CDEBUG(D_HA, "%s: recovery finished s:%s\n", obd2cli_tgt(imp->imp_obd),
466 ptlrpc_import_state_name(imp->imp_state));
469 ptlrpc_set_import_discon(imp, 0);
470 /* Force a new connect attempt */
471 ptlrpc_invalidate_import(imp);
472 /* Do a fresh connect next time by zeroing the handle */
473 ptlrpc_disconnect_import(imp, 1);
474 /* Wait for all invalidate calls to finish */
475 if (atomic_read(&imp->imp_inval_count) > 0) {
476 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
479 rc = l_wait_event(imp->imp_recovery_waitq,
480 (atomic_read(&imp->imp_inval_count) == 0),
483 CERROR("Interrupted, inval=%d\n",
484 atomic_read(&imp->imp_inval_count));
487 /* Allow reconnect attempts */
488 imp->imp_obd->obd_no_recov = 0;
489 /* Remove 'invalid' flag */
490 ptlrpc_activate_import(imp);
491 /* Attempt a new connect */
492 ptlrpc_recover_import(imp, NULL, 0);
496 EXPORT_SYMBOL(ptlrpc_reconnect_import);
499 * Connection on import \a imp is changed to another one (if more than one is
500 * present). We typically chose connection that we have not tried to connect to
503 static int import_select_connection(struct obd_import *imp)
505 struct obd_import_conn *imp_conn = NULL, *conn;
506 struct obd_export *dlmexp;
508 int target_len, tried_all = 1;
511 spin_lock(&imp->imp_lock);
513 if (list_empty(&imp->imp_conn_list)) {
514 CERROR("%s: no connections available\n",
515 imp->imp_obd->obd_name);
516 spin_unlock(&imp->imp_lock);
520 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
521 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
522 imp->imp_obd->obd_name,
523 libcfs_nid2str(conn->oic_conn->c_peer.nid),
524 conn->oic_last_attempt);
526 /* If we have not tried this connection since
527 the last successful attempt, go with this one */
528 if ((conn->oic_last_attempt == 0) ||
529 cfs_time_beforeq_64(conn->oic_last_attempt,
530 imp->imp_last_success_conn)) {
536 /* If all of the connections have already been tried
537 since the last successful connection; just choose the
538 least recently used */
541 else if (cfs_time_before_64(conn->oic_last_attempt,
542 imp_conn->oic_last_attempt))
546 /* if not found, simply choose the current one */
547 if (!imp_conn || imp->imp_force_reconnect) {
548 LASSERT(imp->imp_conn_current);
549 imp_conn = imp->imp_conn_current;
552 LASSERT(imp_conn->oic_conn);
554 /* If we've tried everything, and we're back to the beginning of the
555 list, increase our timeout and try again. It will be reset when
556 we do finally connect. (FIXME: really we should wait for all network
557 state associated with the last connection attempt to drain before
558 trying to reconnect on it.) */
559 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item)) {
560 struct adaptive_timeout *at = &imp->imp_at.iat_net_latency;
561 if (at_get(at) < CONNECTION_SWITCH_MAX) {
562 at_measured(at, at_get(at) + CONNECTION_SWITCH_INC);
563 if (at_get(at) > CONNECTION_SWITCH_MAX)
564 at_reset(at, CONNECTION_SWITCH_MAX);
566 LASSERT(imp_conn->oic_last_attempt);
567 CDEBUG(D_HA, "%s: tried all connections, increasing latency "
568 "to %ds\n", imp->imp_obd->obd_name, at_get(at));
571 imp_conn->oic_last_attempt = cfs_time_current_64();
573 /* switch connection, don't mind if it's same as the current one */
574 if (imp->imp_connection)
575 ptlrpc_connection_put(imp->imp_connection);
576 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
578 dlmexp = class_conn2export(&imp->imp_dlm_handle);
579 LASSERT(dlmexp != NULL);
580 if (dlmexp->exp_connection)
581 ptlrpc_connection_put(dlmexp->exp_connection);
582 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
583 class_export_put(dlmexp);
585 if (imp->imp_conn_current != imp_conn) {
586 if (imp->imp_conn_current) {
587 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
588 &target_start, &target_len);
590 CDEBUG(D_HA, "%s: Connection changing to"
592 imp->imp_obd->obd_name,
593 target_len, target_start,
594 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
597 imp->imp_conn_current = imp_conn;
600 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
601 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
602 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
604 spin_unlock(&imp->imp_lock);
610 * must be called under imp_lock
612 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
614 struct ptlrpc_request *req;
615 struct list_head *tmp;
617 /* The requests in committed_list always have smaller transnos than
618 * the requests in replay_list */
619 if (!list_empty(&imp->imp_committed_list)) {
620 tmp = imp->imp_committed_list.next;
621 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
622 *transno = req->rq_transno;
623 if (req->rq_transno == 0) {
624 DEBUG_REQ(D_ERROR, req, "zero transno in committed_list");
629 if (!list_empty(&imp->imp_replay_list)) {
630 tmp = imp->imp_replay_list.next;
631 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
632 *transno = req->rq_transno;
633 if (req->rq_transno == 0) {
634 DEBUG_REQ(D_ERROR, req, "zero transno in replay_list");
643 * Attempt to (re)connect import \a imp. This includes all preparations,
644 * initializing CONNECT RPC request and passing it to ptlrpcd for
646 * Returns 0 on success or error code.
648 int ptlrpc_connect_import(struct obd_import *imp)
650 struct obd_device *obd = imp->imp_obd;
651 int initial_connect = 0;
653 __u64 committed_before_reconnect = 0;
654 struct ptlrpc_request *request;
655 char *bufs[] = { NULL,
656 obd2cli_tgt(imp->imp_obd),
658 (char *)&imp->imp_dlm_handle,
659 (char *)&imp->imp_connect_data };
660 struct ptlrpc_connect_async_args *aa;
664 spin_lock(&imp->imp_lock);
665 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
666 spin_unlock(&imp->imp_lock);
667 CERROR("can't connect to a closed import\n");
669 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
670 spin_unlock(&imp->imp_lock);
671 CERROR("already connected\n");
673 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
674 spin_unlock(&imp->imp_lock);
675 CERROR("already connecting\n");
679 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
682 imp->imp_resend_replay = 0;
684 if (!lustre_handle_is_used(&imp->imp_remote_handle))
687 committed_before_reconnect = imp->imp_peer_committed_transno;
689 set_transno = ptlrpc_first_transno(imp,
690 &imp->imp_connect_data.ocd_transno);
691 spin_unlock(&imp->imp_lock);
693 rc = import_select_connection(imp);
697 rc = sptlrpc_import_sec_adapt(imp, NULL, NULL);
701 /* Reset connect flags to the originally requested flags, in case
702 * the server is updated on-the-fly we will get the new features. */
703 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
704 /* Reset ocd_version each time so the server knows the exact versions */
705 imp->imp_connect_data.ocd_version = LUSTRE_VERSION_CODE;
706 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
707 imp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
709 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
710 &obd->obd_uuid, &imp->imp_connect_data, NULL);
714 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
716 GOTO(out, rc = -ENOMEM);
718 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
719 imp->imp_connect_op, bufs, NULL);
721 ptlrpc_request_free(request);
725 /* Report the rpc service time to the server so that it knows how long
726 * to wait for clients to join recovery */
727 lustre_msg_set_service_time(request->rq_reqmsg,
728 at_timeout2est(request->rq_timeout));
730 /* The amount of time we give the server to process the connect req.
731 * import_select_connection will increase the net latency on
732 * repeated reconnect attempts to cover slow networks.
733 * We override/ignore the server rpc completion estimate here,
734 * which may be large if this is a reconnect attempt */
735 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
736 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
738 request->rq_no_resend = request->rq_no_delay = 1;
739 request->rq_send_state = LUSTRE_IMP_CONNECTING;
740 /* Allow a slightly larger reply for future growth compatibility */
741 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
742 sizeof(struct obd_connect_data)+16*sizeof(__u64));
743 ptlrpc_request_set_replen(request);
744 request->rq_interpret_reply = ptlrpc_connect_interpret;
746 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
747 aa = ptlrpc_req_async_args(request);
748 memset(aa, 0, sizeof *aa);
750 aa->pcaa_peer_committed = committed_before_reconnect;
751 aa->pcaa_initial_connect = initial_connect;
753 if (aa->pcaa_initial_connect) {
754 spin_lock(&imp->imp_lock);
755 imp->imp_replayable = 1;
756 spin_unlock(&imp->imp_lock);
757 lustre_msg_add_op_flags(request->rq_reqmsg,
758 MSG_CONNECT_INITIAL);
762 lustre_msg_add_op_flags(request->rq_reqmsg,
763 MSG_CONNECT_TRANSNO);
765 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request (timeout %d)",
766 request->rq_timeout);
767 ptlrpcd_add_req(request);
771 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
775 EXPORT_SYMBOL(ptlrpc_connect_import);
777 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
781 spin_lock(&imp->imp_lock);
782 force_verify = imp->imp_force_verify != 0;
783 spin_unlock(&imp->imp_lock);
786 ptlrpc_pinger_wake_up();
789 static int ptlrpc_busy_reconnect(int rc)
791 return (rc == -EBUSY) || (rc == -EAGAIN);
794 static int ptlrpc_connect_set_flags(struct obd_import *imp,
795 struct obd_connect_data *ocd,
796 __u64 old_connect_flags,
797 struct obd_export *exp, int init_connect)
800 struct client_obd *cli = &imp->imp_obd->u.cli;
802 if ((imp->imp_connect_flags_orig & OBD_CONNECT_IBITS) &&
803 !(ocd->ocd_connect_flags & OBD_CONNECT_IBITS)) {
804 LCONSOLE_WARN("%s: MDS %s does not support ibits "
805 "lock, either very old or invalid: "
806 "requested "LPX64", replied "LPX64"\n",
807 imp->imp_obd->obd_name,
808 imp->imp_connection->c_remote_uuid.uuid,
809 imp->imp_connect_flags_orig,
810 ocd->ocd_connect_flags);
814 spin_lock(&imp->imp_lock);
815 list_del(&imp->imp_conn_current->oic_item);
816 list_add(&imp->imp_conn_current->oic_item,
817 &imp->imp_conn_list);
818 imp->imp_last_success_conn =
819 imp->imp_conn_current->oic_last_attempt;
821 spin_unlock(&imp->imp_lock);
824 if (!warned && (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
825 (ocd->ocd_version > LUSTRE_VERSION_CODE +
826 LUSTRE_VERSION_OFFSET_WARN ||
827 ocd->ocd_version < LUSTRE_VERSION_CODE -
828 LUSTRE_VERSION_OFFSET_WARN)) {
829 /* Sigh, some compilers do not like #ifdef in the middle
830 of macro arguments */
831 const char *older = "older than client. "
832 "Consider upgrading server";
833 const char *newer = "newer than client. "
834 "Consider recompiling application";
836 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
838 obd2cli_tgt(imp->imp_obd),
839 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
840 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
841 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
842 OBD_OCD_VERSION_FIX(ocd->ocd_version),
843 ocd->ocd_version > LUSTRE_VERSION_CODE ?
844 newer : older, LUSTRE_VERSION_STRING);
848 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
849 /* Check if server has LU-1252 fix applied to not always swab
850 * the IR MNE entries. Do this only once per connection. This
851 * fixup is version-limited, because we don't want to carry the
852 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we
853 * need interop with unpatched 2.2 servers. For newer servers,
854 * the client will do MNE swabbing only as needed. LU-1644 */
855 if (unlikely((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
856 !(ocd->ocd_connect_flags & OBD_CONNECT_MNE_SWAB) &&
857 OBD_OCD_VERSION_MAJOR(ocd->ocd_version) == 2 &&
858 OBD_OCD_VERSION_MINOR(ocd->ocd_version) == 2 &&
859 OBD_OCD_VERSION_PATCH(ocd->ocd_version) < 55 &&
860 strcmp(imp->imp_obd->obd_type->typ_name,
861 LUSTRE_MGC_NAME) == 0))
862 imp->imp_need_mne_swab = 1;
863 else /* clear if server was upgraded since last connect */
864 imp->imp_need_mne_swab = 0;
867 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
868 /* We sent to the server ocd_cksum_types with bits set
869 * for algorithms we understand. The server masked off
870 * the checksum types it doesn't support */
871 if ((ocd->ocd_cksum_types &
872 cksum_types_supported_client()) == 0) {
873 LCONSOLE_WARN("The negotiation of the checksum "
874 "alogrithm to use with server %s "
875 "failed (%x/%x), disabling "
877 obd2cli_tgt(imp->imp_obd),
878 ocd->ocd_cksum_types,
879 cksum_types_supported_client());
880 cli->cl_checksum = 0;
881 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
883 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
886 /* The server does not support OBD_CONNECT_CKSUM.
887 * Enforce ADLER for backward compatibility*/
888 cli->cl_supp_cksum_types = OBD_CKSUM_ADLER;
890 cli->cl_cksum_type = cksum_type_select(cli->cl_supp_cksum_types);
892 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE)
893 cli->cl_max_pages_per_rpc =
894 min(ocd->ocd_brw_size >> PAGE_CACHE_SHIFT,
895 cli->cl_max_pages_per_rpc);
896 else if (imp->imp_connect_op == MDS_CONNECT ||
897 imp->imp_connect_op == MGS_CONNECT)
898 cli->cl_max_pages_per_rpc = 1;
900 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
901 (cli->cl_max_pages_per_rpc > 0));
903 client_adjust_max_dirty(cli);
905 /* Update client max modify RPCs in flight with value returned
907 if (ocd->ocd_connect_flags & OBD_CONNECT_MULTIMODRPCS)
908 cli->cl_max_mod_rpcs_in_flight = min(
909 cli->cl_max_mod_rpcs_in_flight,
910 ocd->ocd_maxmodrpcs);
912 cli->cl_max_mod_rpcs_in_flight = 1;
914 /* Reset ns_connect_flags only for initial connect. It might be
915 * changed in while using FS and if we reset it in reconnect
916 * this leads to losing user settings done before such as
917 * disable lru_resize, etc. */
918 if (old_connect_flags != exp_connect_flags(exp) || init_connect) {
919 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
920 "flags: "LPX64"\n", imp->imp_obd->obd_name,
921 ocd->ocd_connect_flags);
922 imp->imp_obd->obd_namespace->ns_connect_flags =
923 ocd->ocd_connect_flags;
924 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
925 ocd->ocd_connect_flags;
928 if (ocd->ocd_connect_flags & OBD_CONNECT_AT)
929 /* We need a per-message support flag, because
930 * a. we don't know if the incoming connect reply
931 * supports AT or not (in reply_in_callback)
932 * until we unpack it.
933 * b. failovered server means export and flags are gone
934 * (in ptlrpc_send_reply).
935 * Can only be set when we know AT is supported at
937 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
939 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
941 imp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
947 * interpret_reply callback for connect RPCs.
948 * Looks into returned status of connect operation and decides
949 * what to do with the import - i.e enter recovery, promote it to
950 * full state for normal operations of disconnect it due to an error.
952 static int ptlrpc_connect_interpret(const struct lu_env *env,
953 struct ptlrpc_request *request,
956 struct ptlrpc_connect_async_args *aa = data;
957 struct obd_import *imp = request->rq_import;
958 struct lustre_handle old_hdl;
959 __u64 old_connect_flags;
961 struct obd_connect_data *ocd;
962 struct obd_export *exp;
966 spin_lock(&imp->imp_lock);
967 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
968 imp->imp_connect_tried = 1;
969 spin_unlock(&imp->imp_lock);
974 /* if this reconnect to busy export - not need select new target
976 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
977 spin_unlock(&imp->imp_lock);
978 ptlrpc_maybe_ping_import_soon(imp);
981 spin_unlock(&imp->imp_lock);
983 LASSERT(imp->imp_conn_current);
985 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
987 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
989 /* server replied obd_connect_data is always bigger */
990 ocd = req_capsule_server_sized_get(&request->rq_pill,
991 &RMF_CONNECT_DATA, ret);
994 CERROR("%s: no connect data from server\n",
995 imp->imp_obd->obd_name);
1000 spin_lock(&imp->imp_lock);
1002 /* All imports are pingable */
1003 imp->imp_pingable = 1;
1004 imp->imp_force_reconnect = 0;
1005 imp->imp_force_verify = 0;
1007 imp->imp_connect_data = *ocd;
1009 CDEBUG(D_HA, "%s: connect to target with instance %u\n",
1010 imp->imp_obd->obd_name, ocd->ocd_instance);
1011 exp = class_conn2export(&imp->imp_dlm_handle);
1013 spin_unlock(&imp->imp_lock);
1015 /* check that server granted subset of flags we asked for. */
1016 if ((ocd->ocd_connect_flags & imp->imp_connect_flags_orig) !=
1017 ocd->ocd_connect_flags) {
1018 CERROR("%s: Server didn't granted asked subset of flags: "
1019 "asked="LPX64" grranted="LPX64"\n",
1020 imp->imp_obd->obd_name,imp->imp_connect_flags_orig,
1021 ocd->ocd_connect_flags);
1022 GOTO(out, rc = -EPROTO);
1025 if (!(imp->imp_connect_flags_orig & OBD_CONNECT_LIGHTWEIGHT) &&
1026 (imp->imp_connect_flags_orig & OBD_CONNECT_MDS_MDS) &&
1027 (imp->imp_connect_flags_orig & OBD_CONNECT_FID) &&
1028 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1029 __u32 major = OBD_OCD_VERSION_MAJOR(ocd->ocd_version);
1030 __u32 minor = OBD_OCD_VERSION_MINOR(ocd->ocd_version);
1031 __u32 patch = OBD_OCD_VERSION_PATCH(ocd->ocd_version);
1033 /* We do not support the MDT-MDT interoperations with
1034 * different version MDT because of protocol changes. */
1035 if (unlikely(major != LUSTRE_MAJOR ||
1036 minor != LUSTRE_MINOR ||
1037 abs(patch - LUSTRE_PATCH) > 3)) {
1038 LCONSOLE_WARN("%s: import %p (%u.%u.%u.%u) tried the "
1039 "connection to different version MDT "
1040 "(%d.%d.%d.%d) %s\n",
1041 imp->imp_obd->obd_name, imp, LUSTRE_MAJOR,
1042 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1043 major, minor, patch,
1044 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1045 imp->imp_connection->c_remote_uuid.uuid);
1047 GOTO(out, rc = -EPROTO);
1052 /* This could happen if export is cleaned during the
1054 CERROR("%s: missing export after connect\n",
1055 imp->imp_obd->obd_name);
1056 GOTO(out, rc = -ENODEV);
1058 old_connect_flags = exp_connect_flags(exp);
1059 exp->exp_connect_data = *ocd;
1060 imp->imp_obd->obd_self_export->exp_connect_data = *ocd;
1062 /* The net statistics after (re-)connect is not valid anymore,
1063 * because may reflect other routing, etc. */
1064 at_reinit(&imp->imp_at.iat_net_latency, 0, 0);
1065 ptlrpc_at_adj_net_latency(request,
1066 lustre_msg_get_service_time(request->rq_repmsg));
1068 /* Import flags should be updated before waking import at FULL state */
1069 rc = ptlrpc_connect_set_flags(imp, ocd, old_connect_flags, exp,
1070 aa->pcaa_initial_connect);
1071 class_export_put(exp);
1075 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1077 if (aa->pcaa_initial_connect) {
1078 spin_lock(&imp->imp_lock);
1079 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
1080 imp->imp_replayable = 1;
1081 spin_unlock(&imp->imp_lock);
1082 CDEBUG(D_HA, "connected to replayable target: %s\n",
1083 obd2cli_tgt(imp->imp_obd));
1085 imp->imp_replayable = 0;
1086 spin_unlock(&imp->imp_lock);
1089 /* if applies, adjust the imp->imp_msg_magic here
1090 * according to reply flags */
1092 imp->imp_remote_handle =
1093 *lustre_msg_get_handle(request->rq_repmsg);
1095 /* Initial connects are allowed for clients with non-random
1096 * uuids when servers are in recovery. Simply signal the
1097 * servers replay is complete and wait in REPLAY_WAIT. */
1098 if (msg_flags & MSG_CONNECT_RECOVERING) {
1099 CDEBUG(D_HA, "connect to %s during recovery\n",
1100 obd2cli_tgt(imp->imp_obd));
1101 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1103 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1104 ptlrpc_activate_import(imp);
1107 GOTO(finish, rc = 0);
1110 /* Determine what recovery state to move the import to. */
1111 if (MSG_CONNECT_RECONNECT & msg_flags) {
1112 memset(&old_hdl, 0, sizeof(old_hdl));
1113 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
1114 sizeof (old_hdl))) {
1115 LCONSOLE_WARN("Reconnect to %s (at @%s) failed due "
1116 "bad handle "LPX64"\n",
1117 obd2cli_tgt(imp->imp_obd),
1118 imp->imp_connection->c_remote_uuid.uuid,
1119 imp->imp_dlm_handle.cookie);
1120 GOTO(out, rc = -ENOTCONN);
1123 if (memcmp(&imp->imp_remote_handle,
1124 lustre_msg_get_handle(request->rq_repmsg),
1125 sizeof(imp->imp_remote_handle))) {
1126 int level = msg_flags & MSG_CONNECT_RECOVERING ?
1129 /* Bug 16611/14775: if server handle have changed,
1130 * that means some sort of disconnection happened.
1131 * If the server is not in recovery, that also means it
1132 * already erased all of our state because of previous
1133 * eviction. If it is in recovery - we are safe to
1134 * participate since we can reestablish all of our state
1135 * with server again */
1136 if ((MSG_CONNECT_RECOVERING & msg_flags)) {
1137 CDEBUG(level,"%s@%s changed server handle from "
1139 " but is still in recovery\n",
1140 obd2cli_tgt(imp->imp_obd),
1141 imp->imp_connection->c_remote_uuid.uuid,
1142 imp->imp_remote_handle.cookie,
1143 lustre_msg_get_handle(
1144 request->rq_repmsg)->cookie);
1146 LCONSOLE_WARN("Evicted from %s (at %s) "
1147 "after server handle changed from "
1148 LPX64" to "LPX64"\n",
1149 obd2cli_tgt(imp->imp_obd),
1150 imp->imp_connection-> \
1152 imp->imp_remote_handle.cookie,
1153 lustre_msg_get_handle(
1154 request->rq_repmsg)->cookie);
1158 imp->imp_remote_handle =
1159 *lustre_msg_get_handle(request->rq_repmsg);
1161 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
1162 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1163 GOTO(finish, rc = 0);
1167 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
1168 obd2cli_tgt(imp->imp_obd),
1169 imp->imp_connection->c_remote_uuid.uuid);
1172 if (imp->imp_invalid) {
1173 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
1174 "marking evicted\n", imp->imp_obd->obd_name);
1175 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1176 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
1177 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
1178 imp->imp_obd->obd_name,
1179 obd2cli_tgt(imp->imp_obd));
1181 spin_lock(&imp->imp_lock);
1182 imp->imp_resend_replay = 1;
1183 spin_unlock(&imp->imp_lock);
1185 IMPORT_SET_STATE(imp, imp->imp_replay_state);
1187 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1189 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
1190 LASSERT(imp->imp_replayable);
1191 imp->imp_remote_handle =
1192 *lustre_msg_get_handle(request->rq_repmsg);
1193 imp->imp_last_replay_transno = 0;
1194 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
1196 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
1197 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
1198 imp->imp_remote_handle =
1199 *lustre_msg_get_handle(request->rq_repmsg);
1200 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
1203 /* Sanity checks for a reconnected import. */
1204 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
1205 CERROR("imp_replayable flag does not match server "
1206 "after reconnect. We should LBUG right here.\n");
1209 if (lustre_msg_get_last_committed(request->rq_repmsg) > 0 &&
1210 lustre_msg_get_last_committed(request->rq_repmsg) <
1211 aa->pcaa_peer_committed) {
1212 CERROR("%s went back in time (transno "LPD64
1213 " was previously committed, server now claims "LPD64
1214 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
1216 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
1217 lustre_msg_get_last_committed(request->rq_repmsg));
1221 rc = ptlrpc_import_recovery_state_machine(imp);
1222 if (rc == -ENOTCONN) {
1223 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
1224 "invalidating and reconnecting\n",
1225 obd2cli_tgt(imp->imp_obd),
1226 imp->imp_connection->c_remote_uuid.uuid);
1227 ptlrpc_connect_import(imp);
1228 imp->imp_connect_tried = 1;
1233 imp->imp_connect_tried = 1;
1236 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1237 if (rc == -EACCES) {
1239 * Give up trying to reconnect
1240 * EACCES means client has no permission for connection
1242 imp->imp_obd->obd_no_recov = 1;
1243 ptlrpc_deactivate_import(imp);
1246 if (rc == -EPROTO) {
1247 struct obd_connect_data *ocd;
1249 /* reply message might not be ready */
1250 if (request->rq_repmsg == NULL)
1253 ocd = req_capsule_server_get(&request->rq_pill,
1256 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1257 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1258 /* Actually servers are only supposed to refuse
1259 connection from liblustre clients, so we should
1260 never see this from VFS context */
1261 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1263 " refused connection from this client "
1264 "with an incompatible version (%s). "
1265 "Client must be recompiled\n",
1266 obd2cli_tgt(imp->imp_obd),
1267 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1268 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1269 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1270 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1271 LUSTRE_VERSION_STRING);
1272 ptlrpc_deactivate_import(imp);
1273 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1278 ptlrpc_maybe_ping_import_soon(imp);
1280 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1281 obd2cli_tgt(imp->imp_obd),
1282 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1285 wake_up_all(&imp->imp_recovery_waitq);
1290 * interpret callback for "completed replay" RPCs.
1291 * \see signal_completed_replay
1293 static int completed_replay_interpret(const struct lu_env *env,
1294 struct ptlrpc_request *req,
1295 void * data, int rc)
1298 atomic_dec(&req->rq_import->imp_replay_inflight);
1299 if (req->rq_status == 0 &&
1300 !req->rq_import->imp_vbr_failed) {
1301 ptlrpc_import_recovery_state_machine(req->rq_import);
1303 if (req->rq_import->imp_vbr_failed) {
1305 "%s: version recovery fails, reconnecting\n",
1306 req->rq_import->imp_obd->obd_name);
1308 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1310 req->rq_import->imp_obd->obd_name,
1313 ptlrpc_connect_import(req->rq_import);
1320 * Let server know that we have no requests to replay anymore.
1321 * Achieved by just sending a PING request
1323 static int signal_completed_replay(struct obd_import *imp)
1325 struct ptlrpc_request *req;
1328 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_FINISH_REPLAY)))
1331 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1332 atomic_inc(&imp->imp_replay_inflight);
1334 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1337 atomic_dec(&imp->imp_replay_inflight);
1341 ptlrpc_request_set_replen(req);
1342 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1343 lustre_msg_add_flags(req->rq_reqmsg,
1344 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1346 req->rq_timeout *= 3;
1347 req->rq_interpret_reply = completed_replay_interpret;
1349 ptlrpcd_add_req(req);
1354 * In kernel code all import invalidation happens in its own
1355 * separate thread, so that whatever application happened to encounter
1356 * a problem could still be killed or otherwise continue
1358 static int ptlrpc_invalidate_import_thread(void *data)
1360 struct obd_import *imp = data;
1364 unshare_fs_struct();
1366 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1367 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1368 imp->imp_connection->c_remote_uuid.uuid);
1370 ptlrpc_invalidate_import(imp);
1372 if (obd_dump_on_eviction) {
1373 CERROR("dump the log upon eviction\n");
1374 libcfs_debug_dumplog();
1377 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1378 ptlrpc_import_recovery_state_machine(imp);
1380 class_import_put(imp);
1385 * This is the state machine for client-side recovery on import.
1387 * Typicaly we have two possibly paths. If we came to server and it is not
1388 * in recovery, we just enter IMP_EVICTED state, invalidate our import
1389 * state and reconnect from scratch.
1390 * If we came to server that is in recovery, we enter IMP_REPLAY import state.
1391 * We go through our list of requests to replay and send them to server one by
1393 * After sending all request from the list we change import state to
1394 * IMP_REPLAY_LOCKS and re-request all the locks we believe we have from server
1395 * and also all the locks we don't yet have and wait for server to grant us.
1396 * After that we send a special "replay completed" request and change import
1397 * state to IMP_REPLAY_WAIT.
1398 * Upon receiving reply to that "replay completed" RPC we enter IMP_RECOVER
1399 * state and resend all requests from sending list.
1400 * After that we promote import to FULL state and send all delayed requests
1401 * and import is fully operational after that.
1404 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1412 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1413 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1414 &target_start, &target_len);
1415 /* Don't care about MGC eviction */
1416 if (strcmp(imp->imp_obd->obd_type->typ_name,
1417 LUSTRE_MGC_NAME) != 0) {
1418 LCONSOLE_ERROR_MSG(0x167, "%s: This client was evicted "
1419 "by %.*s; in progress operations "
1420 "using this service will fail.\n",
1421 imp->imp_obd->obd_name, target_len,
1424 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1425 obd2cli_tgt(imp->imp_obd),
1426 imp->imp_connection->c_remote_uuid.uuid);
1427 /* reset vbr_failed flag upon eviction */
1428 spin_lock(&imp->imp_lock);
1429 imp->imp_vbr_failed = 0;
1430 spin_unlock(&imp->imp_lock);
1433 struct task_struct *task;
1434 /* bug 17802: XXX client_disconnect_export vs connect request
1435 * race. if client is evicted at this time then we start
1436 * invalidate thread without reference to import and import can
1437 * be freed at same time. */
1438 class_import_get(imp);
1439 task = kthread_run(ptlrpc_invalidate_import_thread, imp,
1442 class_import_put(imp);
1443 CERROR("error starting invalidate thread: %d\n", rc);
1452 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1453 CDEBUG(D_HA, "replay requested by %s\n",
1454 obd2cli_tgt(imp->imp_obd));
1455 rc = ptlrpc_replay_next(imp, &inflight);
1456 if (inflight == 0 &&
1457 atomic_read(&imp->imp_replay_inflight) == 0) {
1458 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1459 rc = ldlm_replay_locks(imp);
1466 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1467 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1468 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1469 rc = signal_completed_replay(imp);
1475 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1476 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1477 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1481 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1482 struct ptlrpc_connection *conn = imp->imp_connection;
1484 rc = ptlrpc_resend(imp);
1487 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1488 ptlrpc_activate_import(imp);
1490 LCONSOLE_INFO("%s: Connection restored to %s (at %s)\n",
1491 imp->imp_obd->obd_name,
1492 obd_uuid2str(&conn->c_remote_uuid),
1493 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1496 if (imp->imp_state == LUSTRE_IMP_FULL) {
1497 wake_up_all(&imp->imp_recovery_waitq);
1498 ptlrpc_wake_delayed(imp);
1505 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1507 struct ptlrpc_request *req;
1511 if (imp->imp_obd->obd_force)
1512 GOTO(set_state, rc);
1514 switch (imp->imp_connect_op) {
1516 rq_opc = OST_DISCONNECT;
1519 rq_opc = MDS_DISCONNECT;
1522 rq_opc = MGS_DISCONNECT;
1526 CERROR("%s: don't know how to disconnect from %s "
1527 "(connect_op %d): rc = %d\n",
1528 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1529 imp->imp_connect_op, rc);
1533 if (ptlrpc_import_in_recovery(imp)) {
1534 struct l_wait_info lwi;
1535 cfs_duration_t timeout;
1538 if (imp->imp_server_timeout)
1539 timeout = cfs_time_seconds(obd_timeout / 2);
1541 timeout = cfs_time_seconds(obd_timeout);
1543 int idx = import_at_get_index(imp,
1544 imp->imp_client->cli_request_portal);
1545 timeout = cfs_time_seconds(
1546 at_get(&imp->imp_at.iat_service_estimate[idx]));
1549 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1550 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1551 rc = l_wait_event(imp->imp_recovery_waitq,
1552 !ptlrpc_import_in_recovery(imp), &lwi);
1556 spin_lock(&imp->imp_lock);
1557 if (imp->imp_state != LUSTRE_IMP_FULL)
1559 spin_unlock(&imp->imp_lock);
1561 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1562 LUSTRE_OBD_VERSION, rq_opc);
1564 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1565 * it fails. We can get through the above with a down server
1566 * if the client doesn't know the server is gone yet. */
1567 req->rq_no_resend = 1;
1569 /* We want client umounts to happen quickly, no matter the
1571 req->rq_timeout = min_t(int, req->rq_timeout,
1572 INITIAL_CONNECT_TIMEOUT);
1574 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1575 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1576 ptlrpc_request_set_replen(req);
1577 rc = ptlrpc_queue_wait(req);
1578 ptlrpc_req_finished(req);
1582 spin_lock(&imp->imp_lock);
1585 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1587 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1588 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1589 spin_unlock(&imp->imp_lock);
1591 if (rc == -ETIMEDOUT || rc == -ENOTCONN || rc == -ESHUTDOWN)
1595 EXPORT_SYMBOL(ptlrpc_disconnect_import);
1597 void ptlrpc_cleanup_imp(struct obd_import *imp)
1601 spin_lock(&imp->imp_lock);
1602 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1603 imp->imp_generation++;
1604 spin_unlock(&imp->imp_lock);
1605 ptlrpc_abort_inflight(imp);
1610 /* Adaptive Timeout utils */
1611 extern unsigned int at_min, at_max, at_history;
1613 /* Update at_current with the specified value (bounded by at_min and at_max),
1614 * as well as the AT history "bins".
1615 * - Bin into timeslices using AT_BINS bins.
1616 * - This gives us a max of the last at_history seconds without the storage,
1617 * but still smoothing out a return to normalcy from a slow response.
1618 * - (E.g. remember the maximum latency in each minute of the last 4 minutes.)
1620 int at_measured(struct adaptive_timeout *at, unsigned int val)
1622 unsigned int old = at->at_current;
1623 time_t now = cfs_time_current_sec();
1624 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1627 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1628 val, at, now - at->at_binstart, at->at_current,
1629 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1632 /* 0's don't count, because we never want our timeout to
1633 drop to 0, and because 0 could mean an error */
1636 spin_lock(&at->at_lock);
1638 if (unlikely(at->at_binstart == 0)) {
1639 /* Special case to remove default from history */
1640 at->at_current = val;
1641 at->at_worst_ever = val;
1642 at->at_worst_time = now;
1643 at->at_hist[0] = val;
1644 at->at_binstart = now;
1645 } else if (now - at->at_binstart < binlimit ) {
1647 at->at_hist[0] = max(val, at->at_hist[0]);
1648 at->at_current = max(val, at->at_current);
1651 unsigned int maxv = val;
1652 /* move bins over */
1653 shift = (now - at->at_binstart) / binlimit;
1655 for(i = AT_BINS - 1; i >= 0; i--) {
1657 at->at_hist[i] = at->at_hist[i - shift];
1658 maxv = max(maxv, at->at_hist[i]);
1663 at->at_hist[0] = val;
1664 at->at_current = maxv;
1665 at->at_binstart += shift * binlimit;
1668 if (at->at_current > at->at_worst_ever) {
1669 at->at_worst_ever = at->at_current;
1670 at->at_worst_time = now;
1673 if (at->at_flags & AT_FLG_NOHIST)
1674 /* Only keep last reported val; keeping the rest of the history
1676 at->at_current = val;
1679 at->at_current = min(at->at_current, at_max);
1680 at->at_current = max(at->at_current, at_min);
1682 if (at->at_current != old)
1683 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1684 "(val=%u) hist %u %u %u %u\n", at,
1685 old, at->at_current, at->at_current - old, val,
1686 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1689 /* if we changed, report the old value */
1690 old = (at->at_current != old) ? old : 0;
1692 spin_unlock(&at->at_lock);
1696 /* Find the imp_at index for a given portal; assign if space available */
1697 int import_at_get_index(struct obd_import *imp, int portal)
1699 struct imp_at *at = &imp->imp_at;
1702 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1703 if (at->iat_portal[i] == portal)
1705 if (at->iat_portal[i] == 0)
1710 /* Not found in list, add it under a lock */
1711 spin_lock(&imp->imp_lock);
1713 /* Check unused under lock */
1714 for (; i < IMP_AT_MAX_PORTALS; i++) {
1715 if (at->iat_portal[i] == portal)
1717 if (at->iat_portal[i] == 0)
1722 /* Not enough portals? */
1723 LASSERT(i < IMP_AT_MAX_PORTALS);
1725 at->iat_portal[i] = portal;
1727 spin_unlock(&imp->imp_lock);