1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/import.c
38 * Author: Mike Shaver <shaver@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_RPC
43 # include <liblustre.h>
46 #include <obd_support.h>
47 #include <lustre_ha.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50 #include <lustre_export.h>
52 #include <obd_cksum.h>
53 #include <obd_class.h>
55 #include "ptlrpc_internal.h"
57 struct ptlrpc_connect_async_args {
58 __u64 pcaa_peer_committed;
59 int pcaa_initial_connect;
62 static void __import_set_state(struct obd_import *imp,
63 enum lustre_imp_state state)
65 imp->imp_state = state;
66 imp->imp_state_hist[imp->imp_state_hist_idx].ish_state = state;
67 imp->imp_state_hist[imp->imp_state_hist_idx].ish_time =
68 cfs_time_current_sec();
69 imp->imp_state_hist_idx = (imp->imp_state_hist_idx + 1) %
73 /* A CLOSED import should remain so. */
74 #define IMPORT_SET_STATE_NOLOCK(imp, state) \
76 if (imp->imp_state != LUSTRE_IMP_CLOSED) { \
77 CDEBUG(D_HA, "%p %s: changing import state from %s to %s\n", \
78 imp, obd2cli_tgt(imp->imp_obd), \
79 ptlrpc_import_state_name(imp->imp_state), \
80 ptlrpc_import_state_name(state)); \
81 __import_set_state(imp, state); \
85 #define IMPORT_SET_STATE(imp, state) \
87 spin_lock(&imp->imp_lock); \
88 IMPORT_SET_STATE_NOLOCK(imp, state); \
89 spin_unlock(&imp->imp_lock); \
93 static int ptlrpc_connect_interpret(const struct lu_env *env,
94 struct ptlrpc_request *request,
96 int ptlrpc_import_recovery_state_machine(struct obd_import *imp);
98 /* Only this function is allowed to change the import state when it is
99 * CLOSED. I would rather refcount the import and free it after
100 * disconnection like we do with exports. To do that, the client_obd
101 * will need to save the peer info somewhere other than in the import,
103 int ptlrpc_init_import(struct obd_import *imp)
105 spin_lock(&imp->imp_lock);
107 imp->imp_generation++;
108 imp->imp_state = LUSTRE_IMP_NEW;
110 spin_unlock(&imp->imp_lock);
114 EXPORT_SYMBOL(ptlrpc_init_import);
116 #define UUID_STR "_UUID"
117 static void deuuidify(char *uuid, const char *prefix, char **uuid_start,
120 *uuid_start = !prefix || strncmp(uuid, prefix, strlen(prefix))
121 ? uuid : uuid + strlen(prefix);
123 *uuid_len = strlen(*uuid_start);
125 if (*uuid_len < strlen(UUID_STR))
128 if (!strncmp(*uuid_start + *uuid_len - strlen(UUID_STR),
129 UUID_STR, strlen(UUID_STR)))
130 *uuid_len -= strlen(UUID_STR);
133 /* Returns true if import was FULL, false if import was already not
135 * @imp - import to be disconnected
136 * @conn_cnt - connection count (epoch) of the request that timed out
137 * and caused the disconnection. In some cases, multiple
138 * inflight requests can fail to a single target (e.g. OST
139 * bulk requests) and if one has already caused a reconnection
140 * (increasing the import->conn_cnt) the older failure should
141 * not also cause a reconnection. If zero it forces a reconnect.
143 int ptlrpc_set_import_discon(struct obd_import *imp, __u32 conn_cnt)
147 spin_lock(&imp->imp_lock);
149 if (imp->imp_state == LUSTRE_IMP_FULL &&
150 (conn_cnt == 0 || conn_cnt == imp->imp_conn_cnt)) {
154 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
155 &target_start, &target_len);
157 if (imp->imp_replayable) {
158 LCONSOLE_WARN("%s: Connection to service %.*s via nid "
159 "%s was lost; in progress operations using this "
160 "service will wait for recovery to complete.\n",
161 imp->imp_obd->obd_name, target_len, target_start,
162 libcfs_nid2str(imp->imp_connection->c_peer.nid));
164 LCONSOLE_ERROR_MSG(0x166, "%s: Connection to service "
165 "%.*s via nid %s was lost; in progress "
166 "operations using this service will fail.\n",
167 imp->imp_obd->obd_name,
168 target_len, target_start,
169 libcfs_nid2str(imp->imp_connection->c_peer.nid));
171 ptlrpc_deactivate_timeouts(imp);
172 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
173 spin_unlock(&imp->imp_lock);
175 if (obd_dump_on_timeout)
176 libcfs_debug_dumplog();
178 obd_import_event(imp->imp_obd, imp, IMP_EVENT_DISCON);
181 spin_unlock(&imp->imp_lock);
182 CDEBUG(D_HA, "%s: import %p already %s (conn %u, was %u): %s\n",
183 imp->imp_client->cli_name, imp,
184 (imp->imp_state == LUSTRE_IMP_FULL &&
185 imp->imp_conn_cnt > conn_cnt) ?
186 "reconnected" : "not connected", imp->imp_conn_cnt,
187 conn_cnt, ptlrpc_import_state_name(imp->imp_state));
193 /* Must be called with imp_lock held! */
194 static void ptlrpc_deactivate_and_unlock_import(struct obd_import *imp)
197 LASSERT_SPIN_LOCKED(&imp->imp_lock);
199 CDEBUG(D_HA, "setting import %s INVALID\n", obd2cli_tgt(imp->imp_obd));
200 imp->imp_invalid = 1;
201 imp->imp_generation++;
202 spin_unlock(&imp->imp_lock);
204 ptlrpc_abort_inflight(imp);
205 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INACTIVE);
211 * This acts as a barrier; all existing requests are rejected, and
212 * no new requests will be accepted until the import is valid again.
214 void ptlrpc_deactivate_import(struct obd_import *imp)
216 spin_lock(&imp->imp_lock);
217 ptlrpc_deactivate_and_unlock_import(imp);
221 ptlrpc_inflight_deadline(struct ptlrpc_request *req, time_t now)
225 if (!(((req->rq_phase == RQ_PHASE_RPC) && !req->rq_waiting) ||
226 (req->rq_phase == RQ_PHASE_BULK) ||
227 (req->rq_phase == RQ_PHASE_NEW)))
230 if (req->rq_timedout)
233 if (req->rq_phase == RQ_PHASE_NEW)
236 dl = req->rq_deadline;
244 static unsigned int ptlrpc_inflight_timeout(struct obd_import *imp)
246 time_t now = cfs_time_current_sec();
247 struct list_head *tmp, *n;
248 struct ptlrpc_request *req;
249 unsigned int timeout = 0;
251 spin_lock(&imp->imp_lock);
252 list_for_each_safe(tmp, n, &imp->imp_sending_list) {
253 req = list_entry(tmp, struct ptlrpc_request, rq_list);
254 timeout = max(ptlrpc_inflight_deadline(req, now), timeout);
256 spin_unlock(&imp->imp_lock);
261 * This function will invalidate the import, if necessary, then block
262 * for all the RPC completions, and finally notify the obd to
263 * invalidate its state (ie cancel locks, clear pending requests,
266 void ptlrpc_invalidate_import(struct obd_import *imp)
268 struct list_head *tmp, *n;
269 struct ptlrpc_request *req;
270 struct l_wait_info lwi;
271 unsigned int timeout;
274 atomic_inc(&imp->imp_inval_count);
277 * If this is an invalid MGC connection, then don't bother
278 * waiting for imp_inflight to drop to 0.
280 if (imp->imp_invalid && imp->imp_recon_bk &&!imp->imp_obd->obd_no_recov)
283 if (!imp->imp_invalid || imp->imp_obd->obd_no_recov)
284 ptlrpc_deactivate_import(imp);
286 LASSERT(imp->imp_invalid);
288 /* Wait forever until inflight == 0. We really can't do it another
289 * way because in some cases we need to wait for very long reply
290 * unlink. We can't do anything before that because there is really
291 * no guarantee that some rdma transfer is not in progress right now. */
293 /* Calculate max timeout for waiting on rpcs to error
294 * out. Use obd_timeout if calculated value is smaller
296 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_LONG_REPL_UNLINK)) {
297 timeout = ptlrpc_inflight_timeout(imp);
298 timeout += timeout / 3;
301 timeout = obd_timeout;
303 /* decrease the interval to increase race condition */
307 CDEBUG(D_RPCTRACE,"Sleeping %d sec for inflight to error out\n",
310 /* Wait for all requests to error out and call completion
311 * callbacks. Cap it at obd_timeout -- these should all
312 * have been locally cancelled by ptlrpc_abort_inflight. */
313 lwi = LWI_TIMEOUT_INTERVAL(
314 cfs_timeout_cap(cfs_time_seconds(timeout)),
315 (timeout > 1)?cfs_time_seconds(1):cfs_time_seconds(1)/2,
317 rc = l_wait_event(imp->imp_recovery_waitq,
318 (atomic_read(&imp->imp_inflight) == 0), &lwi);
320 const char *cli_tgt = obd2cli_tgt(imp->imp_obd);
322 CERROR("%s: rc = %d waiting for callback (%d != 0)\n",
323 cli_tgt, rc, atomic_read(&imp->imp_inflight));
325 spin_lock(&imp->imp_lock);
326 if (atomic_read(&imp->imp_inflight) == 0) {
327 int count = atomic_read(&imp->imp_unregistering);
329 /* We know that "unregistering" rpcs only can
330 * survive in sending or delaying lists (they
331 * maybe waiting for long reply unlink in
332 * sluggish nets). Let's check this. If there
333 * is no inflight and unregistering != 0, this
335 LASSERTF(count == 0, "Some RPCs are still "
336 "unregistering: %d\n", count);
338 /* Let's save one loop as soon as inflight have
339 * dropped to zero. No new inflights possible at
343 list_for_each_safe(tmp, n,
344 &imp->imp_sending_list) {
345 req = list_entry(tmp,
346 struct ptlrpc_request,
348 DEBUG_REQ(D_ERROR, req,
349 "still on sending list");
351 list_for_each_safe(tmp, n,
352 &imp->imp_delayed_list) {
353 req = list_entry(tmp,
354 struct ptlrpc_request,
356 DEBUG_REQ(D_ERROR, req,
357 "still on delayed list");
360 CERROR("%s: RPCs in \"%s\" phase found (%d). "
361 "Network is sluggish? Waiting them "
362 "to error out.\n", cli_tgt,
363 ptlrpc_phase2str(RQ_PHASE_UNREGISTERING),
364 atomic_read(&imp->imp_unregistering));
366 spin_unlock(&imp->imp_lock);
371 * Let's additionally check that no new rpcs added to import in
372 * "invalidate" state.
374 LASSERT(atomic_read(&imp->imp_inflight) == 0);
376 obd_import_event(imp->imp_obd, imp, IMP_EVENT_INVALIDATE);
377 sptlrpc_import_flush_all_ctx(imp);
379 atomic_dec(&imp->imp_inval_count);
380 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
383 /* unset imp_invalid */
384 void ptlrpc_activate_import(struct obd_import *imp)
386 struct obd_device *obd = imp->imp_obd;
388 spin_lock(&imp->imp_lock);
389 imp->imp_invalid = 0;
390 ptlrpc_activate_timeouts(imp);
391 spin_unlock(&imp->imp_lock);
392 obd_import_event(obd, imp, IMP_EVENT_ACTIVE);
395 void ptlrpc_fail_import(struct obd_import *imp, __u32 conn_cnt)
399 LASSERT(!imp->imp_dlm_fake);
401 if (ptlrpc_set_import_discon(imp, conn_cnt)) {
402 if (!imp->imp_replayable) {
403 CDEBUG(D_HA, "import %s@%s for %s not replayable, "
404 "auto-deactivating\n",
405 obd2cli_tgt(imp->imp_obd),
406 imp->imp_connection->c_remote_uuid.uuid,
407 imp->imp_obd->obd_name);
408 ptlrpc_deactivate_import(imp);
411 CDEBUG(D_HA, "%s: waking up pinger\n",
412 obd2cli_tgt(imp->imp_obd));
414 spin_lock(&imp->imp_lock);
415 imp->imp_force_verify = 1;
416 spin_unlock(&imp->imp_lock);
418 ptlrpc_pinger_wake_up();
423 int ptlrpc_reconnect_import(struct obd_import *imp)
425 ptlrpc_set_import_discon(imp, 0);
426 /* Force a new connect attempt */
427 ptlrpc_invalidate_import(imp);
428 /* Do a fresh connect next time by zeroing the handle */
429 ptlrpc_disconnect_import(imp, 1);
430 /* Wait for all invalidate calls to finish */
431 if (atomic_read(&imp->imp_inval_count) > 0) {
433 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
434 rc = l_wait_event(imp->imp_recovery_waitq,
435 (atomic_read(&imp->imp_inval_count) == 0),
438 CERROR("Interrupted, inval=%d\n",
439 atomic_read(&imp->imp_inval_count));
442 /* Allow reconnect attempts */
443 imp->imp_obd->obd_no_recov = 0;
444 /* Remove 'invalid' flag */
445 ptlrpc_activate_import(imp);
446 /* Attempt a new connect */
447 ptlrpc_recover_import(imp, NULL);
451 EXPORT_SYMBOL(ptlrpc_reconnect_import);
453 static int import_select_connection(struct obd_import *imp)
455 struct obd_import_conn *imp_conn = NULL, *conn;
456 struct obd_export *dlmexp;
460 spin_lock(&imp->imp_lock);
462 if (list_empty(&imp->imp_conn_list)) {
463 CERROR("%s: no connections available\n",
464 imp->imp_obd->obd_name);
465 spin_unlock(&imp->imp_lock);
469 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
470 CDEBUG(D_HA, "%s: connect to NID %s last attempt "LPU64"\n",
471 imp->imp_obd->obd_name,
472 libcfs_nid2str(conn->oic_conn->c_peer.nid),
473 conn->oic_last_attempt);
474 /* Don't thrash connections */
475 if (cfs_time_before_64(cfs_time_current_64(),
476 conn->oic_last_attempt +
477 cfs_time_seconds(CONNECTION_SWITCH_MIN))) {
481 /* If we have not tried this connection since
482 the last successful attempt, go with this one */
483 if ((conn->oic_last_attempt == 0) ||
484 cfs_time_beforeq_64(conn->oic_last_attempt,
485 imp->imp_last_success_conn)) {
491 /* If all of the connections have already been tried
492 since the last successful connection; just choose the
493 least recently used */
496 else if (cfs_time_before_64(conn->oic_last_attempt,
497 imp_conn->oic_last_attempt))
501 /* if not found, simply choose the current one */
502 if (!imp_conn || imp->imp_force_reconnect) {
503 LASSERT(imp->imp_conn_current);
504 imp_conn = imp->imp_conn_current;
507 LASSERT(imp_conn->oic_conn);
509 /* If we've tried everything, and we're back to the beginning of the
510 list, increase our timeout and try again. It will be reset when
511 we do finally connect. (FIXME: really we should wait for all network
512 state associated with the last connection attempt to drain before
513 trying to reconnect on it.) */
514 if (tried_all && (imp->imp_conn_list.next == &imp_conn->oic_item) &&
515 !imp->imp_recon_bk /* not retrying */) {
516 if (at_get(&imp->imp_at.iat_net_latency) <
517 CONNECTION_SWITCH_MAX) {
518 at_add(&imp->imp_at.iat_net_latency,
519 at_get(&imp->imp_at.iat_net_latency) +
520 CONNECTION_SWITCH_INC);
522 LASSERT(imp_conn->oic_last_attempt);
523 CWARN("%s: tried all connections, increasing latency to %ds\n",
524 imp->imp_obd->obd_name,
525 at_get(&imp->imp_at.iat_net_latency));
528 imp_conn->oic_last_attempt = cfs_time_current_64();
530 /* switch connection, don't mind if it's same as the current one */
531 if (imp->imp_connection)
532 ptlrpc_connection_put(imp->imp_connection);
533 imp->imp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
535 dlmexp = class_conn2export(&imp->imp_dlm_handle);
536 LASSERT(dlmexp != NULL);
537 if (dlmexp->exp_connection)
538 ptlrpc_connection_put(dlmexp->exp_connection);
539 dlmexp->exp_connection = ptlrpc_connection_addref(imp_conn->oic_conn);
540 class_export_put(dlmexp);
542 if (imp->imp_conn_current != imp_conn) {
543 if (imp->imp_conn_current)
544 CDEBUG(D_HA, "Changing connection for %s to %s/%s\n",
545 imp->imp_obd->obd_name, imp_conn->oic_uuid.uuid,
546 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
547 imp->imp_conn_current = imp_conn;
550 CDEBUG(D_HA, "%s: import %p using connection %s/%s\n",
551 imp->imp_obd->obd_name, imp, imp_conn->oic_uuid.uuid,
552 libcfs_nid2str(imp_conn->oic_conn->c_peer.nid));
554 spin_unlock(&imp->imp_lock);
560 * must be called under imp_lock
562 static int ptlrpc_first_transno(struct obd_import *imp, __u64 *transno)
564 struct ptlrpc_request *req;
565 struct list_head *tmp;
567 if (list_empty(&imp->imp_replay_list))
569 tmp = imp->imp_replay_list.next;
570 req = list_entry(tmp, struct ptlrpc_request, rq_replay_list);
571 *transno = req->rq_transno;
572 if (req->rq_transno == 0) {
573 DEBUG_REQ(D_ERROR, req, "zero transno in replay");
580 int ptlrpc_connect_import(struct obd_import *imp, char *new_uuid)
582 struct obd_device *obd = imp->imp_obd;
583 int initial_connect = 0;
585 __u64 committed_before_reconnect = 0;
586 struct ptlrpc_request *request;
587 char *bufs[] = { NULL,
588 obd2cli_tgt(imp->imp_obd),
590 (char *)&imp->imp_dlm_handle,
591 (char *)&imp->imp_connect_data };
592 struct ptlrpc_connect_async_args *aa;
596 spin_lock(&imp->imp_lock);
597 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
598 spin_unlock(&imp->imp_lock);
599 CERROR("can't connect to a closed import\n");
601 } else if (imp->imp_state == LUSTRE_IMP_FULL) {
602 spin_unlock(&imp->imp_lock);
603 CERROR("already connected\n");
605 } else if (imp->imp_state == LUSTRE_IMP_CONNECTING) {
606 spin_unlock(&imp->imp_lock);
607 CERROR("already connecting\n");
611 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CONNECTING);
614 imp->imp_resend_replay = 0;
616 if (!lustre_handle_is_used(&imp->imp_remote_handle))
619 committed_before_reconnect = imp->imp_peer_committed_transno;
621 set_transno = ptlrpc_first_transno(imp,
622 &imp->imp_connect_data.ocd_transno);
623 spin_unlock(&imp->imp_lock);
626 struct obd_uuid uuid;
628 obd_str2uuid(&uuid, new_uuid);
629 rc = import_set_conn_priority(imp, &uuid);
634 rc = import_select_connection(imp);
638 /* last in connection list */
639 if (imp->imp_conn_current->oic_item.next == &imp->imp_conn_list) {
640 if (imp->imp_initial_recov_bk && initial_connect) {
641 CDEBUG(D_HA, "Last connection attempt (%d) for %s\n",
642 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
643 /* Don't retry if connect fails */
645 obd_set_info_async(obd->obd_self_export,
646 sizeof(KEY_INIT_RECOV),
648 sizeof(rc), &rc, NULL);
650 if (imp->imp_recon_bk) {
651 CDEBUG(D_HA, "Last reconnection attempt (%d) for %s\n",
652 imp->imp_conn_cnt, obd2cli_tgt(imp->imp_obd));
653 spin_lock(&imp->imp_lock);
654 imp->imp_last_recon = 1;
655 spin_unlock(&imp->imp_lock);
659 rc = sptlrpc_import_sec_adapt(imp, NULL, 0);
663 /* Reset connect flags to the originally requested flags, in case
664 * the server is updated on-the-fly we will get the new features. */
665 imp->imp_connect_data.ocd_connect_flags = imp->imp_connect_flags_orig;
666 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
668 rc = obd_reconnect(NULL, imp->imp_obd->obd_self_export, obd,
669 &obd->obd_uuid, &imp->imp_connect_data, NULL);
673 request = ptlrpc_request_alloc(imp, &RQF_MDS_CONNECT);
675 GOTO(out, rc = -ENOMEM);
677 rc = ptlrpc_request_bufs_pack(request, LUSTRE_OBD_VERSION,
678 imp->imp_connect_op, bufs, NULL);
680 ptlrpc_request_free(request);
684 /* Report the rpc service time to the server so that it knows how long
685 * to wait for clients to join recovery */
686 lustre_msg_set_service_time(request->rq_reqmsg,
687 at_timeout2est(request->rq_timeout));
689 /* The amount of time we give the server to process the connect req.
690 * import_select_connection will increase the net latency on
691 * repeated reconnect attempts to cover slow networks.
692 * We override/ignore the server rpc completion estimate here,
693 * which may be large if this is a reconnect attempt */
694 request->rq_timeout = INITIAL_CONNECT_TIMEOUT;
695 lustre_msg_set_timeout(request->rq_reqmsg, request->rq_timeout);
698 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_LIBCLIENT);
700 lustre_msg_add_op_flags(request->rq_reqmsg, MSG_CONNECT_NEXT_VER);
702 request->rq_no_resend = request->rq_no_delay = 1;
703 request->rq_send_state = LUSTRE_IMP_CONNECTING;
704 /* Allow a slightly larger reply for future growth compatibility */
705 req_capsule_set_size(&request->rq_pill, &RMF_CONNECT_DATA, RCL_SERVER,
706 sizeof(struct obd_connect_data)+16*sizeof(__u64));
707 ptlrpc_request_set_replen(request);
708 request->rq_interpret_reply = ptlrpc_connect_interpret;
710 CLASSERT(sizeof (*aa) <= sizeof (request->rq_async_args));
711 aa = ptlrpc_req_async_args(request);
712 memset(aa, 0, sizeof *aa);
714 aa->pcaa_peer_committed = committed_before_reconnect;
715 aa->pcaa_initial_connect = initial_connect;
717 if (aa->pcaa_initial_connect) {
718 spin_lock(&imp->imp_lock);
719 imp->imp_replayable = 1;
720 spin_unlock(&imp->imp_lock);
721 lustre_msg_add_op_flags(request->rq_reqmsg,
722 MSG_CONNECT_INITIAL);
726 lustre_msg_add_op_flags(request->rq_reqmsg,
727 MSG_CONNECT_TRANSNO);
729 DEBUG_REQ(D_RPCTRACE, request, "(re)connect request");
730 ptlrpcd_add_req(request, PSCOPE_OTHER);
734 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
739 EXPORT_SYMBOL(ptlrpc_connect_import);
741 static void ptlrpc_maybe_ping_import_soon(struct obd_import *imp)
744 struct obd_import_conn *imp_conn;
750 spin_lock(&imp->imp_lock);
751 if (list_empty(&imp->imp_conn_list))
755 imp_conn = list_entry(imp->imp_conn_list.prev,
756 struct obd_import_conn,
759 /* XXX: When the failover node is the primary node, it is possible
760 * to have two identical connections in imp_conn_list. We must
761 * compare not conn's pointers but NIDs, otherwise we can defeat
762 * connection throttling. (See bug 14774.) */
763 if (imp->imp_conn_current->oic_conn->c_peer.nid !=
764 imp_conn->oic_conn->c_peer.nid) {
765 ptlrpc_ping_import_soon(imp);
769 /* liblustre has no pinger thread, so we wakeup pinger anyway */
774 spin_unlock(&imp->imp_lock);
777 ptlrpc_pinger_wake_up();
782 static int ptlrpc_busy_reconnect(int rc)
784 return (rc == -EBUSY) || (rc == -EAGAIN);
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;
800 spin_lock(&imp->imp_lock);
801 if (imp->imp_state == LUSTRE_IMP_CLOSED) {
802 spin_unlock(&imp->imp_lock);
807 /* if this reconnect to busy export - not need select new target
809 imp->imp_force_reconnect = ptlrpc_busy_reconnect(rc);
810 spin_unlock(&imp->imp_lock);
814 LASSERT(imp->imp_conn_current);
816 msg_flags = lustre_msg_get_op_flags(request->rq_repmsg);
818 /* All imports are pingable */
819 imp->imp_pingable = 1;
820 imp->imp_force_reconnect = 0;
822 if (aa->pcaa_initial_connect) {
823 if (msg_flags & MSG_CONNECT_REPLAYABLE) {
824 imp->imp_replayable = 1;
825 spin_unlock(&imp->imp_lock);
826 CDEBUG(D_HA, "connected to replayable target: %s\n",
827 obd2cli_tgt(imp->imp_obd));
829 imp->imp_replayable = 0;
830 spin_unlock(&imp->imp_lock);
833 /* if applies, adjust the imp->imp_msg_magic here
834 * according to reply flags */
836 imp->imp_remote_handle =
837 *lustre_msg_get_handle(request->rq_repmsg);
839 /* Initial connects are allowed for clients with non-random
840 * uuids when servers are in recovery. Simply signal the
841 * servers replay is complete and wait in REPLAY_WAIT. */
842 if (msg_flags & MSG_CONNECT_RECOVERING) {
843 CDEBUG(D_HA, "connect to %s during recovery\n",
844 obd2cli_tgt(imp->imp_obd));
845 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
847 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
848 ptlrpc_activate_import(imp);
851 GOTO(finish, rc = 0);
853 spin_unlock(&imp->imp_lock);
856 /* Determine what recovery state to move the import to. */
857 if (MSG_CONNECT_RECONNECT & msg_flags) {
858 memset(&old_hdl, 0, sizeof(old_hdl));
859 if (!memcmp(&old_hdl, lustre_msg_get_handle(request->rq_repmsg),
861 CERROR("%s@%s didn't like our handle "LPX64
862 ", failed\n", obd2cli_tgt(imp->imp_obd),
863 imp->imp_connection->c_remote_uuid.uuid,
864 imp->imp_dlm_handle.cookie);
865 GOTO(out, rc = -ENOTCONN);
868 if (memcmp(&imp->imp_remote_handle,
869 lustre_msg_get_handle(request->rq_repmsg),
870 sizeof(imp->imp_remote_handle))) {
871 int level = msg_flags & MSG_CONNECT_RECOVERING ?
874 /* Bug 16611/14775: if server handle have changed,
875 * that means some sort of disconnection happened.
876 * If the server is not in recovery, that also means it
877 * already erased all of our state because of previous
878 * eviction. If it is in recovery - we are safe to
879 * participate since we can reestablish all of our state
880 * with server again */
881 CDEBUG(level,"%s@%s changed server handle from "
882 LPX64" to "LPX64"%s\n",
883 obd2cli_tgt(imp->imp_obd),
884 imp->imp_connection->c_remote_uuid.uuid,
885 imp->imp_remote_handle.cookie,
886 lustre_msg_get_handle(request->rq_repmsg)->
888 (MSG_CONNECT_RECOVERING & msg_flags) ?
889 " but is still in recovery" : "");
891 imp->imp_remote_handle =
892 *lustre_msg_get_handle(request->rq_repmsg);
894 if (!(MSG_CONNECT_RECOVERING & msg_flags)) {
895 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
896 GOTO(finish, rc = 0);
900 CDEBUG(D_HA, "reconnected to %s@%s after partition\n",
901 obd2cli_tgt(imp->imp_obd),
902 imp->imp_connection->c_remote_uuid.uuid);
905 if (imp->imp_invalid) {
906 CDEBUG(D_HA, "%s: reconnected but import is invalid; "
907 "marking evicted\n", imp->imp_obd->obd_name);
908 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
909 } else if (MSG_CONNECT_RECOVERING & msg_flags) {
910 CDEBUG(D_HA, "%s: reconnected to %s during replay\n",
911 imp->imp_obd->obd_name,
912 obd2cli_tgt(imp->imp_obd));
914 spin_lock(&imp->imp_lock);
915 imp->imp_resend_replay = 1;
916 spin_unlock(&imp->imp_lock);
918 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
920 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
922 } else if ((MSG_CONNECT_RECOVERING & msg_flags) && !imp->imp_invalid) {
923 LASSERT(imp->imp_replayable);
924 imp->imp_remote_handle =
925 *lustre_msg_get_handle(request->rq_repmsg);
926 imp->imp_last_replay_transno = 0;
927 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY);
929 DEBUG_REQ(D_HA, request, "%s: evicting (reconnect/recover flags"
930 " not set: %x)", imp->imp_obd->obd_name, msg_flags);
931 imp->imp_remote_handle =
932 *lustre_msg_get_handle(request->rq_repmsg);
933 IMPORT_SET_STATE(imp, LUSTRE_IMP_EVICTED);
936 /* Sanity checks for a reconnected import. */
937 if (!(imp->imp_replayable) != !(msg_flags & MSG_CONNECT_REPLAYABLE)) {
938 CERROR("imp_replayable flag does not match server "
939 "after reconnect. We should LBUG right here.\n");
942 if (lustre_msg_get_last_committed(request->rq_repmsg) <
943 aa->pcaa_peer_committed) {
944 CERROR("%s went back in time (transno "LPD64
945 " was previously committed, server now claims "LPD64
946 ")! See https://bugzilla.lustre.org/show_bug.cgi?"
948 obd2cli_tgt(imp->imp_obd), aa->pcaa_peer_committed,
949 lustre_msg_get_last_committed(request->rq_repmsg));
953 rc = ptlrpc_import_recovery_state_machine(imp);
955 if (rc == -ENOTCONN) {
956 CDEBUG(D_HA, "evicted/aborted by %s@%s during recovery;"
957 "invalidating and reconnecting\n",
958 obd2cli_tgt(imp->imp_obd),
959 imp->imp_connection->c_remote_uuid.uuid);
960 ptlrpc_connect_import(imp, NULL);
964 struct obd_connect_data *ocd;
965 struct obd_export *exp;
967 ret = req_capsule_get_size(&request->rq_pill, &RMF_CONNECT_DATA,
969 /* server replied obd_connect_data is always bigger */
970 ocd = req_capsule_server_sized_get(&request->rq_pill,
971 &RMF_CONNECT_DATA, ret);
973 spin_lock(&imp->imp_lock);
974 list_del(&imp->imp_conn_current->oic_item);
975 list_add(&imp->imp_conn_current->oic_item, &imp->imp_conn_list);
976 imp->imp_last_success_conn =
977 imp->imp_conn_current->oic_last_attempt;
980 spin_unlock(&imp->imp_lock);
981 CERROR("Wrong connect data from server\n");
986 imp->imp_connect_data = *ocd;
988 exp = class_conn2export(&imp->imp_dlm_handle);
989 spin_unlock(&imp->imp_lock);
991 /* check that server granted subset of flags we asked for. */
992 LASSERTF((ocd->ocd_connect_flags &
993 imp->imp_connect_flags_orig) ==
994 ocd->ocd_connect_flags, LPX64" != "LPX64,
995 imp->imp_connect_flags_orig, ocd->ocd_connect_flags);
998 /* This could happen if export is cleaned during the
1000 CERROR("Missing export for %s\n",
1001 imp->imp_obd->obd_name);
1002 GOTO(out, rc = -ENODEV);
1004 old_connect_flags = exp->exp_connect_flags;
1005 exp->exp_connect_flags = ocd->ocd_connect_flags;
1006 imp->imp_obd->obd_self_export->exp_connect_flags =
1007 ocd->ocd_connect_flags;
1008 class_export_put(exp);
1010 obd_import_event(imp->imp_obd, imp, IMP_EVENT_OCD);
1012 if (!ocd->ocd_ibits_known &&
1013 ocd->ocd_connect_flags & OBD_CONNECT_IBITS)
1014 CERROR("Inodebits aware server returned zero compatible"
1017 if ((ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1018 (ocd->ocd_version > LUSTRE_VERSION_CODE +
1019 LUSTRE_VERSION_OFFSET_WARN ||
1020 ocd->ocd_version < LUSTRE_VERSION_CODE -
1021 LUSTRE_VERSION_OFFSET_WARN)) {
1022 /* Sigh, some compilers do not like #ifdef in the middle
1023 of macro arguments */
1026 "older. Consider upgrading this client";
1029 "older. Consider recompiling this application";
1031 const char *newer = "newer than client version";
1033 LCONSOLE_WARN("Server %s version (%d.%d.%d.%d) "
1034 "is much %s (%s)\n",
1035 obd2cli_tgt(imp->imp_obd),
1036 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1037 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1038 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1039 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1040 ocd->ocd_version > LUSTRE_VERSION_CODE ?
1041 newer : older, LUSTRE_VERSION_STRING);
1044 if (ocd->ocd_connect_flags & OBD_CONNECT_CKSUM) {
1045 /* We sent to the server ocd_cksum_types with bits set
1046 * for algorithms we understand. The server masked off
1047 * the checksum types it doesn't support */
1048 if ((ocd->ocd_cksum_types & OBD_CKSUM_ALL) == 0) {
1049 LCONSOLE_WARN("The negotiation of the checksum "
1050 "alogrithm to use with server %s "
1051 "failed (%x/%x), disabling "
1053 obd2cli_tgt(imp->imp_obd),
1054 ocd->ocd_cksum_types,
1056 cli->cl_checksum = 0;
1057 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
1058 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1060 cli->cl_supp_cksum_types = ocd->ocd_cksum_types;
1062 if (ocd->ocd_cksum_types & OSC_DEFAULT_CKSUM)
1063 cli->cl_cksum_type = OSC_DEFAULT_CKSUM;
1064 else if (ocd->ocd_cksum_types & OBD_CKSUM_ADLER)
1065 cli->cl_cksum_type = OBD_CKSUM_ADLER;
1067 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1070 /* The server does not support OBD_CONNECT_CKSUM.
1071 * Enforce CRC32 for backward compatibility*/
1072 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
1073 cli->cl_cksum_type = OBD_CKSUM_CRC32;
1076 if (ocd->ocd_connect_flags & OBD_CONNECT_BRW_SIZE) {
1077 cli->cl_max_pages_per_rpc =
1078 ocd->ocd_brw_size >> CFS_PAGE_SHIFT;
1081 /* Reset ns_connect_flags only for initial connect. It might be
1082 * changed in while using FS and if we reset it in reconnect
1083 * this leads to losing user settings done before such as
1084 * disable lru_resize, etc. */
1085 if (old_connect_flags != exp->exp_connect_flags ||
1086 aa->pcaa_initial_connect) {
1087 CDEBUG(D_HA, "%s: Resetting ns_connect_flags to server "
1088 "flags: "LPX64"\n", imp->imp_obd->obd_name,
1089 ocd->ocd_connect_flags);
1090 imp->imp_obd->obd_namespace->ns_connect_flags =
1091 ocd->ocd_connect_flags;
1092 imp->imp_obd->obd_namespace->ns_orig_connect_flags =
1093 ocd->ocd_connect_flags;
1096 if ((ocd->ocd_connect_flags & OBD_CONNECT_AT) &&
1097 (imp->imp_msg_magic == LUSTRE_MSG_MAGIC_V2))
1098 /* We need a per-message support flag, because
1099 a. we don't know if the incoming connect reply
1100 supports AT or not (in reply_in_callback)
1102 b. failovered server means export and flags are gone
1103 (in ptlrpc_send_reply).
1104 Can only be set when we know AT is supported at
1106 imp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1108 imp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1110 LASSERT((cli->cl_max_pages_per_rpc <= PTLRPC_MAX_BRW_PAGES) &&
1111 (cli->cl_max_pages_per_rpc > 0));
1116 IMPORT_SET_STATE(imp, LUSTRE_IMP_DISCON);
1117 spin_lock(&imp->imp_lock);
1118 if (aa->pcaa_initial_connect && !imp->imp_initial_recov &&
1119 (request->rq_import_generation == imp->imp_generation))
1120 ptlrpc_deactivate_and_unlock_import(imp);
1122 spin_unlock(&imp->imp_lock);
1124 if ((imp->imp_recon_bk && imp->imp_last_recon) ||
1127 * Give up trying to reconnect
1128 * EACCES means client has no permission for connection
1130 imp->imp_obd->obd_no_recov = 1;
1131 ptlrpc_deactivate_import(imp);
1134 if (rc == -EPROTO) {
1135 struct obd_connect_data *ocd;
1137 /* reply message might not be ready */
1138 if (request->rq_repmsg == NULL)
1141 ocd = req_capsule_server_get(&request->rq_pill,
1144 (ocd->ocd_connect_flags & OBD_CONNECT_VERSION) &&
1145 (ocd->ocd_version != LUSTRE_VERSION_CODE)) {
1146 /* Actually servers are only supposed to refuse
1147 connection from liblustre clients, so we should
1148 never see this from VFS context */
1149 LCONSOLE_ERROR_MSG(0x16a, "Server %s version "
1151 " refused connection from this client "
1152 "with an incompatible version (%s). "
1153 "Client must be recompiled\n",
1154 obd2cli_tgt(imp->imp_obd),
1155 OBD_OCD_VERSION_MAJOR(ocd->ocd_version),
1156 OBD_OCD_VERSION_MINOR(ocd->ocd_version),
1157 OBD_OCD_VERSION_PATCH(ocd->ocd_version),
1158 OBD_OCD_VERSION_FIX(ocd->ocd_version),
1159 LUSTRE_VERSION_STRING);
1160 ptlrpc_deactivate_import(imp);
1161 IMPORT_SET_STATE(imp, LUSTRE_IMP_CLOSED);
1166 ptlrpc_maybe_ping_import_soon(imp);
1168 CDEBUG(D_HA, "recovery of %s on %s failed (%d)\n",
1169 obd2cli_tgt(imp->imp_obd),
1170 (char *)imp->imp_connection->c_remote_uuid.uuid, rc);
1173 spin_lock(&imp->imp_lock);
1174 imp->imp_last_recon = 0;
1175 spin_unlock(&imp->imp_lock);
1177 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1181 static int completed_replay_interpret(const struct lu_env *env,
1182 struct ptlrpc_request *req,
1183 void * data, int rc)
1186 atomic_dec(&req->rq_import->imp_replay_inflight);
1187 if (req->rq_status == 0 &&
1188 !req->rq_import->imp_vbr_failed) {
1189 ptlrpc_import_recovery_state_machine(req->rq_import);
1191 if (req->rq_import->imp_vbr_failed) {
1193 "%s: version recovery fails, reconnecting\n",
1194 req->rq_import->imp_obd->obd_name);
1195 spin_lock(&req->rq_import->imp_lock);
1196 req->rq_import->imp_vbr_failed = 0;
1197 spin_unlock(&req->rq_import->imp_lock);
1199 CDEBUG(D_HA, "%s: LAST_REPLAY message error: %d, "
1201 req->rq_import->imp_obd->obd_name,
1204 ptlrpc_connect_import(req->rq_import, NULL);
1210 static int signal_completed_replay(struct obd_import *imp)
1212 struct ptlrpc_request *req;
1215 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1216 atomic_inc(&imp->imp_replay_inflight);
1218 req = ptlrpc_request_alloc_pack(imp, &RQF_OBD_PING, LUSTRE_OBD_VERSION,
1221 atomic_dec(&imp->imp_replay_inflight);
1225 ptlrpc_request_set_replen(req);
1226 req->rq_send_state = LUSTRE_IMP_REPLAY_WAIT;
1227 lustre_msg_add_flags(req->rq_reqmsg,
1228 MSG_LOCK_REPLAY_DONE | MSG_REQ_REPLAY_DONE);
1230 req->rq_timeout *= 3;
1231 req->rq_interpret_reply = completed_replay_interpret;
1233 ptlrpcd_add_req(req, PSCOPE_OTHER);
1238 static int ptlrpc_invalidate_import_thread(void *data)
1240 struct obd_import *imp = data;
1244 cfs_daemonize_ctxt("ll_imp_inval");
1246 CDEBUG(D_HA, "thread invalidate import %s to %s@%s\n",
1247 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
1248 imp->imp_connection->c_remote_uuid.uuid);
1250 ptlrpc_invalidate_import(imp);
1252 if (obd_dump_on_eviction) {
1253 CERROR("dump the log upon eviction\n");
1254 libcfs_debug_dumplog();
1257 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1258 ptlrpc_import_recovery_state_machine(imp);
1260 class_import_put(imp);
1265 int ptlrpc_import_recovery_state_machine(struct obd_import *imp)
1273 if (imp->imp_state == LUSTRE_IMP_EVICTED) {
1274 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1275 &target_start, &target_len);
1276 /* Don't care about MGC eviction */
1277 if (strcmp(imp->imp_obd->obd_type->typ_name,
1278 LUSTRE_MGC_NAME) != 0) {
1279 LCONSOLE_ERROR_MSG(0x167, "This client was evicted by "
1280 "%.*s; in progress operations using "
1281 "this service will fail.\n",
1282 target_len, target_start);
1284 CDEBUG(D_HA, "evicted from %s@%s; invalidating\n",
1285 obd2cli_tgt(imp->imp_obd),
1286 imp->imp_connection->c_remote_uuid.uuid);
1289 /* bug 17802: XXX client_disconnect_export vs connect request
1290 * race. if client will evicted at this time, we start
1291 * invalidate thread without reference to import and import can
1292 * be freed at same time. */
1293 class_import_get(imp);
1294 rc = cfs_kernel_thread(ptlrpc_invalidate_import_thread, imp,
1295 CLONE_VM | CLONE_FILES);
1297 class_import_put(imp);
1298 CERROR("error starting invalidate thread: %d\n", rc);
1304 ptlrpc_invalidate_import(imp);
1306 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1310 if (imp->imp_state == LUSTRE_IMP_REPLAY) {
1311 CDEBUG(D_HA, "replay requested by %s\n",
1312 obd2cli_tgt(imp->imp_obd));
1313 rc = ptlrpc_replay_next(imp, &inflight);
1314 if (inflight == 0 &&
1315 atomic_read(&imp->imp_replay_inflight) == 0) {
1316 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_LOCKS);
1317 rc = ldlm_replay_locks(imp);
1324 if (imp->imp_state == LUSTRE_IMP_REPLAY_LOCKS) {
1325 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1326 IMPORT_SET_STATE(imp, LUSTRE_IMP_REPLAY_WAIT);
1327 rc = signal_completed_replay(imp);
1334 if (imp->imp_state == LUSTRE_IMP_REPLAY_WAIT) {
1335 if (atomic_read(&imp->imp_replay_inflight) == 0) {
1336 IMPORT_SET_STATE(imp, LUSTRE_IMP_RECOVER);
1340 if (imp->imp_state == LUSTRE_IMP_RECOVER) {
1341 CDEBUG(D_HA, "reconnected to %s@%s\n",
1342 obd2cli_tgt(imp->imp_obd),
1343 imp->imp_connection->c_remote_uuid.uuid);
1345 rc = ptlrpc_resend(imp);
1348 IMPORT_SET_STATE(imp, LUSTRE_IMP_FULL);
1349 ptlrpc_activate_import(imp);
1351 deuuidify(obd2cli_tgt(imp->imp_obd), NULL,
1352 &target_start, &target_len);
1353 LCONSOLE_INFO("%s: Connection restored to service %.*s "
1354 "using nid %s.\n", imp->imp_obd->obd_name,
1355 target_len, target_start,
1356 libcfs_nid2str(imp->imp_connection->c_peer.nid));
1359 if (imp->imp_state == LUSTRE_IMP_FULL) {
1360 cfs_waitq_broadcast(&imp->imp_recovery_waitq);
1361 ptlrpc_wake_delayed(imp);
1368 static int back_to_sleep(void *unused)
1373 int ptlrpc_disconnect_import(struct obd_import *imp, int noclose)
1375 struct ptlrpc_request *req;
1377 int nowait = imp->imp_obd->obd_force;
1381 GOTO(set_state, rc);
1383 switch (imp->imp_connect_op) {
1384 case OST_CONNECT: rq_opc = OST_DISCONNECT; break;
1385 case MDS_CONNECT: rq_opc = MDS_DISCONNECT; break;
1386 case MGS_CONNECT: rq_opc = MGS_DISCONNECT; break;
1388 CERROR("don't know how to disconnect from %s (connect_op %d)\n",
1389 obd2cli_tgt(imp->imp_obd), imp->imp_connect_op);
1393 if (ptlrpc_import_in_recovery(imp)) {
1394 struct l_wait_info lwi;
1395 cfs_duration_t timeout;
1399 if (imp->imp_server_timeout)
1400 timeout = cfs_time_seconds(obd_timeout / 2);
1402 timeout = cfs_time_seconds(obd_timeout);
1404 int idx = import_at_get_index(imp,
1405 imp->imp_client->cli_request_portal);
1406 timeout = cfs_time_seconds(
1407 at_get(&imp->imp_at.iat_service_estimate[idx]));
1410 lwi = LWI_TIMEOUT_INTR(cfs_timeout_cap(timeout),
1411 back_to_sleep, LWI_ON_SIGNAL_NOOP, NULL);
1412 rc = l_wait_event(imp->imp_recovery_waitq,
1413 !ptlrpc_import_in_recovery(imp), &lwi);
1417 spin_lock(&imp->imp_lock);
1418 if (imp->imp_state != LUSTRE_IMP_FULL)
1421 spin_unlock(&imp->imp_lock);
1423 req = ptlrpc_request_alloc_pack(imp, &RQF_MDS_DISCONNECT,
1424 LUSTRE_OBD_VERSION, rq_opc);
1426 /* We are disconnecting, do not retry a failed DISCONNECT rpc if
1427 * it fails. We can get through the above with a down server
1428 * if the client doesn't know the server is gone yet. */
1429 req->rq_no_resend = 1;
1432 /* We want client umounts to happen quickly, no matter the
1434 req->rq_timeout = min_t(int, req->rq_timeout,
1435 INITIAL_CONNECT_TIMEOUT);
1437 /* ... but we always want liblustre clients to nicely
1438 disconnect, so only use the adaptive value. */
1440 req->rq_timeout = obd_timeout / 3;
1443 IMPORT_SET_STATE(imp, LUSTRE_IMP_CONNECTING);
1444 req->rq_send_state = LUSTRE_IMP_CONNECTING;
1445 ptlrpc_request_set_replen(req);
1446 rc = ptlrpc_queue_wait(req);
1447 ptlrpc_req_finished(req);
1451 spin_lock(&imp->imp_lock);
1454 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_DISCON);
1456 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1457 memset(&imp->imp_remote_handle, 0, sizeof(imp->imp_remote_handle));
1458 /* Try all connections in the future - bz 12758 */
1459 imp->imp_last_recon = 0;
1460 spin_unlock(&imp->imp_lock);
1465 void ptlrpc_cleanup_imp(struct obd_import *imp)
1469 spin_lock(&imp->imp_lock);
1470 IMPORT_SET_STATE_NOLOCK(imp, LUSTRE_IMP_CLOSED);
1471 imp->imp_generation++;
1472 spin_unlock(&imp->imp_lock);
1473 ptlrpc_abort_inflight(imp);
1478 /* Adaptive Timeout utils */
1479 extern unsigned int at_min, at_max, at_history;
1481 /* Bin into timeslices using AT_BINS bins.
1482 This gives us a max of the last binlimit*AT_BINS secs without the storage,
1483 but still smoothing out a return to normalcy from a slow response.
1484 (E.g. remember the maximum latency in each minute of the last 4 minutes.) */
1485 int at_add(struct adaptive_timeout *at, unsigned int val)
1487 unsigned int old = at->at_current;
1488 time_t now = cfs_time_current_sec();
1489 time_t binlimit = max_t(time_t, at_history / AT_BINS, 1);
1492 CDEBUG(D_OTHER, "add %u to %p time=%lu v=%u (%u %u %u %u)\n",
1493 val, at, now - at->at_binstart, at->at_current,
1494 at->at_hist[0], at->at_hist[1], at->at_hist[2], at->at_hist[3]);
1497 /* 0's don't count, because we never want our timeout to
1498 drop to 0, and because 0 could mean an error */
1501 spin_lock(&at->at_lock);
1503 if (unlikely(at->at_binstart == 0)) {
1504 /* Special case to remove default from history */
1505 at->at_current = val;
1506 at->at_worst_ever = val;
1507 at->at_worst_time = now;
1508 at->at_hist[0] = val;
1509 at->at_binstart = now;
1510 } else if (now - at->at_binstart < binlimit ) {
1512 at->at_hist[0] = max(val, at->at_hist[0]);
1513 at->at_current = max(val, at->at_current);
1516 unsigned int maxv = val;
1517 /* move bins over */
1518 shift = (now - at->at_binstart) / binlimit;
1520 for(i = AT_BINS - 1; i >= 0; i--) {
1522 at->at_hist[i] = at->at_hist[i - shift];
1523 maxv = max(maxv, at->at_hist[i]);
1528 at->at_hist[0] = val;
1529 at->at_current = maxv;
1530 at->at_binstart += shift * binlimit;
1533 if (at->at_current > at->at_worst_ever) {
1534 at->at_worst_ever = at->at_current;
1535 at->at_worst_time = now;
1538 if (at->at_flags & AT_FLG_NOHIST)
1539 /* Only keep last reported val; keeping the rest of the history
1541 at->at_current = val;
1544 at->at_current = min(at->at_current, at_max);
1545 at->at_current = max(at->at_current, at_min);
1547 if (at->at_current != old)
1548 CDEBUG(D_OTHER, "AT %p change: old=%u new=%u delta=%d "
1549 "(val=%u) hist %u %u %u %u\n", at,
1550 old, at->at_current, at->at_current - old, val,
1551 at->at_hist[0], at->at_hist[1], at->at_hist[2],
1554 /* if we changed, report the old value */
1555 old = (at->at_current != old) ? old : 0;
1557 spin_unlock(&at->at_lock);
1561 /* Find the imp_at index for a given portal; assign if space available */
1562 int import_at_get_index(struct obd_import *imp, int portal)
1564 struct imp_at *at = &imp->imp_at;
1567 for (i = 0; i < IMP_AT_MAX_PORTALS; i++) {
1568 if (at->iat_portal[i] == portal)
1570 if (at->iat_portal[i] == 0)
1575 /* Not found in list, add it under a lock */
1576 spin_lock(&imp->imp_lock);
1578 /* Check unused under lock */
1579 for (; i < IMP_AT_MAX_PORTALS; i++) {
1580 if (at->iat_portal[i] == portal)
1582 if (at->iat_portal[i] == 0)
1587 /* Not enough portals? */
1588 LASSERT(i < IMP_AT_MAX_PORTALS);
1590 at->iat_portal[i] = portal;
1592 spin_unlock(&imp->imp_lock);